--- advanced_forum.module	2008-05-12 20:26:01.000000000 +0800
+++ advanced_forum.module	2008-05-13 18:47:18.000000000 +0800
@@ -462,9 +462,14 @@ function phptemplate_forum_list($forums,
 }
 
 function advanced_forum_preprocess_forum_list(&$variables) {
-  // Add in a link to the last topic in each forum
-  $last_topics = advanced_forum_get_all_last_topics();
+  $containers = variable_get('forum_containers', array());
   foreach ($variables['forums'] as $tid => $forum) {
+    // Don't fetch the last post if tid is a container
+    if (in_array($tid, $containers)) {
+      continue;
+    }
+    // Add in a link to the last topic in each forum
+    $last_topics = advanced_forum_get_all_last_topics($tid);
     // In the forum overview, we want to overwrite the last reply with our custom one
     $variables['forums'][$tid]->last_reply = theme('forum_submitted', $last_topics[$tid]);
   }
@@ -700,54 +705,78 @@ function advanced_forum_markasread() {
 /**
  * Returns information about the last updated topics in all forums.
  */
-function advanced_forum_get_all_last_topics() {
-  // Query the info about the latest topic for the given forum
-  /*
-SELECT f.nid, f.tid, n.title, u.name, n.created FROM tbj_forum f INNER JOIN tbj_node n ON f.nid = n.nid INNER JOIN shared_users u ON n.uid = u.uid GROUP BY f.tid ORDER BY n.created DESC;
-  */
-  $query = "
-    SELECT n.title AS topictitle, 
-           r.title AS replytitle, 
-           r.time timestamp, 
-           r.type AS replytype, 
-           n.type AS topictype, 
-           n.nid AS topicid, 
-           t.tid, 
-           r.cid AS cid, 
-           r.uid,
-           u.name
-    FROM {node} AS n 
-    INNER JOIN 
-      (
-        (SELECT title, 
-                created AS time,
-                nid, 
-                uid, 
-                type, 
-                'cid' AS cid 
-         FROM {node}
-         )
-         UNION 
-         (SELECT subject, 
-                  timestamp, 
-                  nid, 
-                  uid, 
-                  'comment', 
-                  cid 
-           FROM {comments}
-           WHERE {comments}.status = %d
-         ) 
-         ORDER BY time DESC
-      ) AS r ON n.nid=r.nid 
-      INNER JOIN {term_node} AS t ON n.nid = t.nid 
-      INNER JOIN {users} AS u ON r.uid = u.uid
-      WHERE n.status = 1 
-      GROUP BY tid;";
-
-  $result = db_query($query, COMMENT_PUBLISHED);
-  $topics = array();
-  while ($topic = db_fetch_object($result)) {
-    $topics[$topic->tid] = $topic;
+function advanced_forum_get_all_last_topics($tid) {
+  // Extract the latest node creation data to use as a floor
+  // for querying the node and comments tables
+  $max_time = db_result(db_query('SELECT MAX(n.created) AS max_time FROM {forum} f INNER JOIN {node} n ON f.nid = n.nid WHERE f.tid = %d AND n.status = 1', $tid));
+
+  // Query the node details of the latest node for the given forum tid
+  $node_sql = '
+SELECT
+  n.title AS topictitle, n.created AS timestamp, 
+  n.nid AS topicid, NULL AS replyid, 
+  n.uid, u.name, f.tid 
+FROM
+  {forum} f 
+INNER JOIN 
+  {node} n 
+ON 
+  f.nid = n.nid 
+INNER JOIN 
+  {users} u 
+ON 
+  n.uid = u.uid 
+WHERE 
+  n.created >= %d AND 
+  n.status = 1 AND 
+  f.tid = %d 
+ORDER BY 
+  n.created DESC LIMIT 1
+';
+
+  // Query the comment details of the latest comment for the given forum tid
+  $comment_sql = '
+SELECT 
+  c.subject AS topictitle, c.timestamp AS timestamp, 
+  c.nid AS topicid, c.cid AS replyid, 
+  c.uid, u.name, f.tid 
+FROM 
+  {forum} f 
+INNER JOIN 
+  {node} n 
+ON 
+  f.nid = n.nid 
+INNER JOIN 
+  {comments} c 
+ON 
+  f.nid = c.nid 
+INNER JOIN 
+  {users} u 
+ON 
+  c.uid = u.uid 
+WHERE 
+  n.created >= %d AND 
+  n.status = 1 AND 
+  c.status = 0 AND 
+  f.tid = %d 
+ORDER BY 
+  c.timestamp DESC LIMIT 1
+';
+
+  $nodes = array();
+  $results = db_query($node_sql, $max_time, $tid);
+  while ($topic = db_fetch_object($results)) {
+    $nodes[$topic->tid] = $topic;
+  }
+  
+  $comments = array();
+  $results = db_query($comment_sql, $max_time, $tid);
+  while ($reply = db_fetch_object($results)) {
+    $comments[$reply->tid] = $reply;
+  }
+
+  foreach ($nodes as $tid => $node) {
+    $topics[$tid] = ($node->timestamp > $comments[$tid]->timestamp ? $node : $comments[$tid]);
   }   
 
   return $topics;
