You are here

function forum_get_topics in Drupal 4

Same name and namespace in other branches
  1. 5 modules/forum/forum.module \forum_get_topics()
  2. 6 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.module
Menu callback; prints a forum listing.

File

modules/forum.module, line 751
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(
    array(
      'data' => ' ',
    ),
    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, f.tid, n.title, 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 FROM {node_comment_statistics} l, {users} cu, {term_node} r, {users} u, {forum} f, {node} n WHERE n.status = 1 AND l.last_comment_uid = cu.uid AND n.nid = l.nid AND n.nid = r.nid AND r.tid = %d AND n.uid = u.uid AND n.vid = f.vid");
  $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(n.nid) FROM {node} n INNER JOIN {term_node} r ON n.nid = r.nid AND r.tid = %d WHERE n.status = 1 AND n.type = 'forum'");
  $result = pager_query($sql, $forum_per_page, 0, $sql_count, $tid);
  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;
}