Icon for new comments in forum

robotjox's picture

Currently it is to my knowledge not possible to show with icons if a subforum contains new comments - it's only possible to show new topics.
This is pretty standard functionality and a feature a lot of users have asked about on my sites.

It's just nice to have a quick overview of activity in all forums on the foum listings page.

I asked around for a long itme in forums and on irc, but nobody had some ready-to-go code snippets, so I eventually set upon doing it myself.

I promised Michelle to post my results in this group, but I'm a bit hesitant because A: I don't know if I'm the only one interested in this, and B: I still regard myself a newbie in code hacking, so this might be/probably is a totally wrong way to do this.

But whatever, here's the code, that I put in my template file - maybe someone can turn it into something useful. I'm pretty sure you're not supposed to put db queries in a template file, but I didn't know where else to put it - please enlighten me. It does give me the result I was looking for. Thanks to merlinofchaos for some hints.

This is the query:

<?php
$forumname
= $forum->tid;
$newcomments = db_result(db_query("SELECT h.nid FROM {history} h INNER JOIN {node_comment_statistics} ncs ON ncs.nid = h.nid INNER JOIN {forum} f ON f.nid = ncs.nid WHERE h.uid = '" . $user->uid ."' AND f.tid = '" . $forumname . "' AND ncs.last_comment_timestamp > h.timestamp"));
?>

and this is the code snippet that I use to show the appropriate forum icons for subforums:

<?php
if ($new_topics > 0) {
 
$forumicon = '<img src="http://mywebsite.com/themes/mytheme/images/forum_unread.gif">';
} else if (
$newcomments != "") {
 
$forumicon = '<img src="http://mywebsite.com/themes/mytheme/images/forum_unread.gif">';
    } else  if (
$newcomments == "") {
     
$forumicon = '<img src="http://mywebsite.com/themes/mytheme/images/forum_read.gif">';
    }
?>

Would love to hear some opinions on this approach - could this be achived in a more effective way. Please be gentle ;)

oh, and here's a working example: http://ammenet.dk/forum but it's not showing much if you're not a member of the site.

Login to post comments

...

Michelle - Tue, 2008-01-01 14:33

Thanks for this. If no one comes up and says there's something evil about the query, I can work it into advanced forum.

BTW, using php tags makes the post format better. I fixed it up for you. the code tags are fairly useless for code.

Michelle

See my Drupal articles and tutorials or come check out life in the Coulee Region


Source of additional information

kmillecam-gdo's picture
kmillecam-gdo - Thu, 2008-01-03 17:00

I really like the idea of exposing new posts on the forum listings page.

A colleague of mine came up with a solution a year or so ago that you might want to check out for implementation ideas.

Here's his post: http://www.drupal.org/node/77074

HTH,
Kevin


Changes core

Michelle - Thu, 2008-01-03 21:46

Unfortunately, it looks like he's modding the core forum module. I need methods that work without touching core.

Michelle

See my Drupal articles and tutorials or come check out the Coulee Region


Advanced Forums

seradares - Fri, 2008-01-04 04:27

I'm fairly new to Drupal, but I have managed to integrate this into the wonderful Advanced Forums module. I hope it's correct, it seems to work.

In theme/advforum/forum-list.tpl.php at around line 33
AFTER

<?php
     
else {
       
$new_topics = _forum_topics_unread($forum->tid, $user->uid);
       
$forum->old_topics = $forum->num_topics - $new_topics;
        if (!
$user->uid) {
         
$new_topics = 0;
        }
?>

ADD
<?php
      
// Get icons for forums
       
$forumname = $forum->tid;
      
$newcomments = db_result(db_query("SELECT h.nid FROM {history} h INNER JOIN {node_comment_statistics} ncs ON ncs.nid = h.nid INNER JOIN {forum} f ON f.nid = ncs.nid WHERE h.uid = '" . $user->uid ."' AND f.tid = '" .               $forumname . "' AND ncs.last_comment_timestamp > h.timestamp"));
     if (
$new_topics > 0) {
       
$forumicon = '<img src="sites/default/themes/theme/advforum/images/forum-new.png">&nbsp;&nbsp;';
       } else if (
$newcomments != "") {
      
$forumicon = '<img src="sites/default/themes/theme/advforum/images/forum-new.png">&nbsp;&nbsp;';
       } else  if (
$newcomments == "") {
         
$forumicon = '<img src="sites/default/themes/theme/advforum/images/forum-default.png">&nbsp;&nbsp;';
       }
?>

AFTER

<?php
   
       
// Instead of forcing a 30px hard-coded style for indented columns, add a class to the div
               
if(($forum->depth * 30) > 0) {
              
$description  = '<div class="forum-indented">' . "\n";
             } else {
              
$description  = '<div class="forum-default">' . "\n";
              }
?>

Concatenate $forumicon into $description before the forum name
<?php
           
        $description
.= ' <div class="name">'. $forumicon . l($forum->name, "forum/$forum->tid") ."</div>\n";
?>

I am just pointing to the default Advanced forum icons for threads, and adding a couple of spaces at the end to separate the icon from the forum name (you might want to use css). Hope that's helpful! Thanks for the snippet!