You are here

function forum_get_topics in Drupal 6

Same name and namespace in other branches
  1. 4 modules/forum.module \forum_get_topics()
  2. 5 modules/forum/forum.module \forum_get_topics()
  3. 7 modules/forum/forum.module \forum_get_topics()
1 call to forum_get_topics()
forum_page in modules/forum/forum.pages.inc
Menu callback; prints a forum listing.

File

modules/forum/forum.module, line 573
Enable threaded discussions about general topics.

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' => 'n.title',
    ),
    array(
      'data' => t('Replies'),
      'field' => 'l.comment_count',
    ),
    array(
      'data' => t('Created'),
      'field' => 'n.created',
    ),
    array(
      'data' => t('Last reply'),
      'field' => 'l.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'];
    }
  }
  $term = taxonomy_get_term($tid);
  $sql = db_rewrite_sql("SELECT n.nid, r.tid, n.title, n.type, n.sticky, u.name, u.uid, n.created AS timestamp, n.comment AS comment_mode, l.last_comment_timestamp, IF(l.last_comment_uid != 0, cu.name, l.last_comment_name) AS last_comment_name, l.last_comment_uid, l.comment_count AS num_comments, f.tid AS forum_tid FROM {node_comment_statistics} l INNER JOIN {node} n ON n.nid = l.nid INNER JOIN {users} cu ON l.last_comment_uid = cu.uid INNER JOIN {term_node} r ON n.vid = r.vid INNER JOIN {users} u ON n.uid = u.uid INNER JOIN {forum} f ON n.vid = f.vid WHERE n.status = 1 AND r.tid = %d");
  $sql .= tablesort_sql($forum_topic_list_header, 'n.sticky DESC,');
  $sql .= ', n.created DESC';

  // Always add a secondary sort order so that the news forum topics are on top.
  $sql_count = db_rewrite_sql("SELECT COUNT(DISTINCT n.nid) FROM {node} n INNER JOIN {term_node} r ON n.vid = r.vid AND r.tid = %d WHERE n.status = 1");
  $result = pager_query($sql, $forum_per_page, 0, $sql_count, $tid);
  $topics = array();
  while ($topic = db_fetch_object($result)) {
    if ($user->uid) {

      // folder is new if topic is new or there are new comments since last visit
      if ($topic->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->timestamp > $history;
      }
    }
    else {

      // Do not track "new replies" status for topics if the user is anonymous.
      $topic->new_replies = 0;
      $topic->new = 0;
    }
    if ($topic->num_comments > 0) {
      $last_reply = new stdClass();
      $last_reply->timestamp = $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;
}