Index: modules/forum.module
===================================================================
--- modules/forum.module	(revision 5760)
+++ modules/forum.module	(working copy)
@@ -332,6 +332,9 @@
   $form['forum_per_page'] = array('#type' => 'select', '#title' => t('Topics per page'), '#default_value' => variable_get('forum_per_page', 25), '#options' => $number, '#description' => t('The default number of topics displayed per page; links to browse older messages are automatically being displayed.'));
   $forder = array(1 => t('Date - newest first'), 2 => t('Date - oldest first'), 3 => t('Posts - most active first'), 4=> t('Posts - least active first'));
   $form['forum_order'] = array('#type' => 'radios', '#title' => t('Default order'), '#default_value' => variable_get('forum_order', '1'), '#options' => $forder, '#description' => t('The default display order for topics.'));
+  if (_forum_collecting_statistics()) {
+    $form['forum_show_views'] = array('#type' => 'checkbox', '#title' => t('Show views'), '#default_value' => variable_get('forum_show_views', 1), '#description' => t('Adds a column to the forum overview page showing how many views each topic received.'));
+  }
 
   return system_settings_form('forum_admin_configure', $form);
 }
@@ -727,8 +730,11 @@
     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'),
   );
+  if (_forum_show_views()) {
+    $forum_topic_list_header[] = array('data' => t('Views'), 'field' => 'nc.totalcount');
+  }
+  $forum_topic_list_header[] = 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++) {
@@ -739,7 +745,7 @@
 
   $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} n, {node_comment_statistics} l, {users} cu, {term_node} r, {users} u, {forum} f 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 = db_rewrite_sql("SELECT n.nid, f.tid, n.title, n.sticky, nc.totalcount, 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} n, {node_comment_statistics} l, {users} cu, {term_node} r, {users} u, {forum} f LEFT JOIN {node_counter} nc ON nc.nid = n.nid 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.
 
@@ -965,17 +971,21 @@
         $rows[] = array(
           array('data' => theme('forum_icon', $topic->new, $topic->num_comments, $topic->comment_mode, $topic->sticky), 'class' => 'icon'),
           array('data' => check_plain($topic->title), 'class' => 'title'),
-          array('data' => l(t('This topic has been moved'), "forum/$topic->tid"), 'colspan' => '3')
+          array('data' => l(t('This topic has been moved'), "forum/$topic->tid"), 'colspan' => _forum_show_views() ? '4' : '3')
         );
       }
       else {
-        $rows[] = array(
+        $row = array(
           array('data' => theme('forum_icon', $topic->new, $topic->num_comments, $topic->comment_mode, $topic->sticky), 'class' => 'icon'),
           array('data' => l($topic->title, "node/$topic->nid"), 'class' => 'topic'),
           array('data' => $topic->num_comments . ($topic->new_replies ? '<br />'. l(t('%a new', array('%a' => $topic->new_replies)), "node/$topic->nid", NULL, NULL, 'new') : ''), 'class' => 'replies'),
           array('data' => _forum_format($topic), 'class' => 'created'),
-          array('data' => _forum_format($topic->last_reply), 'class' => 'last-reply')
         );
+        if (_forum_show_views()) {
+          $row[] = array('data' => $topic->totalcount, 'class' => 'views');
+        }
+        $row[] = array('data' => _forum_format($topic->last_reply), 'class' => 'last-reply');
+        $rows[] = $row;
       }
     }
   }
@@ -1052,4 +1062,10 @@
   return $order['field'] .' '. $order['sort'];
 }
 
+function _forum_collecting_statistics() {
+  return module_exist('statistics') && variable_get('statistics_count_content_views', 0);
+}
 
+function _forum_show_views() {
+  return _forum_collecting_statistics() && variable_get('forum_show_views', 1);
+}
