You are here

function advanced_forum_get_forums in Advanced Forum 6

Same name and namespace in other branches
  1. 5 advanced_forum.module \advanced_forum_get_forums()
  2. 6.2 includes/core-overrides.inc \advanced_forum_get_forums()

Returns a list of all forums for a given taxonomy id

This is copied from the forum module and adapted.

Forum objects contain the following fields -num_topics Number of topics in the forum -num_posts Total number of posts in all topics -last_post Most recent post for the forum

Parameters

$tid: Taxonomy ID of the vocabulary that holds the forum list.

Return value

Array of object containing the forum information.

1 call to advanced_forum_get_forums()
advanced_forum_page in ./advanced_forum.module
Menu callback; prints a forum listing.

File

./advanced_forum.module, line 984
Enables the look and feel of other popular forum software.

Code

function advanced_forum_get_forums($tid = 0) {
  $forums = array();
  $vid = variable_get('forum_nav_vocabulary', '');
  $_forums = taxonomy_get_tree($vid, $tid);
  if (count($_forums)) {
    $counts = array();
    $sql = "\n      SELECT r.tid AS tid, n.nid AS nid, l.comment_count AS nid_comment_count\n        FROM {node} n\n        INNER JOIN {node_comment_statistics} l ON n.nid = l.nid\n        INNER JOIN {term_node} r ON n.vid = r.vid\n        WHERE n.status = 1\n        GROUP BY r.tid, n.nid, l.comment_count";
    $sql_rewritten = db_rewrite_sql($sql);
    if ($sql_rewritten == $sql) {
      $sql = "\n        SELECT r.tid, COUNT(n.nid) AS topic_count, SUM(l.comment_count) AS comment_count\n          FROM {node} n\n          INNER JOIN {node_comment_statistics} l ON n.nid = l.nid\n          INNER JOIN {term_node} r ON n.vid = r.vid\n          WHERE n.status = 1\n          GROUP BY r.tid";
      $sql = db_rewrite_sql($sql);
    }
    else {
      $sql = "\n        SELECT tid, COUNT(nid) AS topic_count, SUM(nid_comment_count) AS comment_count\n          FROM ({$sql_rewritten}) AS forum_content_list\n          GROUP BY tid";
    }
    $_counts = db_query($sql);
    while ($count = db_fetch_object($_counts)) {
      $counts[$count->tid] = $count;
    }
  }
  foreach ($_forums as $forum) {

    // Check if this term is a container
    if (in_array($forum->tid, variable_get('forum_containers', array()))) {
      $forum->container = 1;
    }
    if (!empty($counts[$forum->tid])) {
      $forum->num_topics = $counts[$forum->tid]->topic_count;
      $forum->num_posts = $counts[$forum->tid]->topic_count + $counts[$forum->tid]->comment_count;
    }
    else {
      $forum->num_topics = 0;
      $forum->num_posts = 0;
    }

    // This query does not use full ANSI syntax since MySQL 3.x does not support
    // table1 INNER JOIN table2 INNER JOIN table3 ON table2_criteria ON table3_criteria
    // used to join node_comment_statistics to users.
    $sql = "SELECT n.nid, n.title, n.type,\n                   ncs.last_comment_timestamp,\n                   IF (ncs.last_comment_uid != 0, u2.name, ncs.last_comment_name) AS last_comment_name,\n                   ncs.last_comment_uid\n            FROM {node} n\n            INNER JOIN {users} u1 ON n.uid = u1.uid\n            INNER JOIN {term_node} tn ON n.vid = tn.vid\n            INNER JOIN {node_comment_statistics} ncs ON n.nid = ncs.nid\n            INNER JOIN {users} u2 ON ncs.last_comment_uid=u2.uid\n            WHERE n.status = 1 AND tn.tid = %d\n            ORDER BY ncs.last_comment_timestamp DESC";
    $sql = db_rewrite_sql($sql);
    $topic = db_fetch_object(db_query_range($sql, $forum->tid, 0, 1));
    $last_post = new stdClass();
    if (!empty($topic->last_comment_timestamp)) {
      $last_post->timestamp = $topic->last_comment_timestamp;
      $last_post->name = $topic->last_comment_name;
      $last_post->uid = $topic->last_comment_uid;
      $last_post->nid = $topic->nid;

      // Note, we call it "node_title" otherwise it gets picked up on the
      // topic list as well.
      $last_post->node_title = $topic->title;
      $last_post->type = $topic->type;
    }
    $forum->last_post = $last_post;
    $forums[$forum->tid] = $forum;
  }
  return $forums;
}