forum_get_topics($tid, $sortby, $forum_per_page)modules/forum/forum.module, line 798
<?php
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)
->orderBy('f.last_comment_timestamp', 'DESC')
->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) {
$result = db_query("SELECT n.title, n.nid, n.sticky, n.created, n.uid, n.comment AS comment_mode, ncs.*, f.tid AS forum_tid, u.name, IF (ncs.last_comment_uid != 0, u2.name, ncs.last_comment_name) AS last_comment_name FROM {node} n INNER JOIN {node_comment_statistics} ncs ON n.nid = ncs.nid INNER JOIN {forum} f ON n.vid = f.vid INNER JOIN {users} u ON n.uid = u.uid INNER JOIN {users} u2 ON ncs.last_comment_uid = u2.uid WHERE n.nid IN (:nids)", array(':nids' => $nids));
}
else {
$result = array();
}
$topics = array();
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;
}
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;
}
?>