forum_get_topics

5 forum.module forum_get_topics($tid, $sortby, $forum_per_page)
6 forum.module forum_get_topics($tid, $sortby, $forum_per_page)
7 forum.module forum_get_topics($tid, $sortby, $forum_per_page)
8 forum.module forum_get_topics($tid, $sortby, $forum_per_page)

1 call to forum_get_topics()

File

core/modules/forum/forum.module, line 879
Provides discussion forums.

Code

function forum_get_topics($tid, $sortby, $forum_per_page) {
  global $user, $forum_topic_list_header;

  $forum_topic_list_header = array(
    NULL,
    array(
      'data' => t('Topic'),
      'field' => 'f.title',
    ),
    array(
      'data' => t('Replies'),
      'field' => 'f.comment_count',
    ),
    array(
      'data' => t('Last reply'),
      'field' => 'f.last_comment_timestamp',
    ),
  );

  $order = _forum_get_topic_order($sortby);
  for ($i = 0; $i < count($forum_topic_list_header); $i++) {
    if ($forum_topic_list_header[$i]['field'] == $order['field']) {
      $forum_topic_list_header[$i]['sort'] = $order['sort'];
    }
  }

  $query = db_select('forum_index', 'f')->extend('PagerDefault')->extend('TableSort');
  $query->fields('f');
  $query
    ->condition('f.tid', $tid)
    ->addTag('node_access')
    ->orderBy('f.sticky', 'DESC')
    ->orderByHeader($forum_topic_list_header)
    ->limit($forum_per_page);

  $count_query = db_select('forum_index', 'f');
  $count_query->condition('f.tid', $tid);
  $count_query->addExpression('COUNT(*)');
  $count_query->addTag('node_access');

  $query->setCountQuery($count_query);
  $result = $query->execute();
  $nids = array();
  foreach ($result as $record) {
    $nids[] = $record->nid;
  }
  if ($nids) {
    $nodes = node_load_multiple($nids);

    $query = db_select('node', 'n')->extend('TableSort');
    $query->fields('n', array('nid'));

    $query->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
    $query->fields('ncs', array('cid', 'last_comment_uid', 'last_comment_timestamp', 'comment_count'));

    $query->join('forum_index', 'f', 'f.nid = ncs.nid');
    $query->addField('f', 'tid', 'forum_tid');

    $query->join('users', 'u', 'n.uid = u.uid');
    $query->addField('u', 'name');

    $query->join('users', 'u2', 'ncs.last_comment_uid = u2.uid');

    $query->addExpression('CASE ncs.last_comment_uid WHEN 0 THEN ncs.last_comment_name ELSE u2.name END', 'last_comment_name');

    $query
      ->orderBy('f.sticky', 'DESC')
      ->orderByHeader($forum_topic_list_header)
      ->condition('n.nid', $nids);

    $result = array();
    foreach ($query->execute() as $row) {
      $topic = $nodes[$row->nid];
      $topic->comment_mode = $topic->comment;

      foreach ($row as $key => $value) {
        $topic->{$key} = $value;
      }
      $result[] = $topic;
    }
  }
  else {
    $result = array();
  }

  $topics = array();
  $first_new_found = FALSE;
  foreach ($result as $topic) {
    if ($user->uid) {
      // folder is new if topic is new or there are new comments since last visit
      if ($topic->forum_tid != $tid) {
        $topic->new = 0;
      }
      else {
        $history = _forum_user_last_visit($topic->nid);
        $topic->new_replies = comment_num_new($topic->nid, $history);
        $topic->new = $topic->new_replies || ($topic->last_comment_timestamp > $history);
      }
    }
    else {
      // Do not track "new replies" status for topics if the user is anonymous.
      $topic->new_replies = 0;
      $topic->new = 0;
    }

    // Make sure only one topic is indicated as the first new topic.
    $topic->first_new = FALSE;
    if ($topic->new != 0 && !$first_new_found) {
      $topic->first_new = TRUE;
      $first_new_found = TRUE;
    }

    if ($topic->comment_count > 0) {
      $last_reply = new stdClass();
      $last_reply->created = $topic->last_comment_timestamp;
      $last_reply->name = $topic->last_comment_name;
      $last_reply->uid = $topic->last_comment_uid;
      $topic->last_reply = $last_reply;
    }
    $topics[] = $topic;
  }

  return $topics;
}
Login or register to post comments