Display # of unread messages in recent topics

Started by Nao 尚, March 31, 2006, 03:50:44 AM

Previous topic - Next topic

Nao 尚

Hello there,
This is my first post here. I'm currently maintaining a few SMF-based websites. I also had a custom-made forum on my professional website (www.kyodai.com), but I recently switched it to SMF 1.1 RC2 after I realized most of my "original" custom features were already in there. Most? Well, I can deal with a few of them not being there, but I really missed one of my favorites, which, as far as I know, has never been implemented in any other forum (and I haven't found it on this coding board either). I'd love to see the idea on other forums, so I'll post my code for you to enjoy. This is a function that will show, as the topic title indicates, the most recent topics (as in calling ssi_recentTopics), but with a twist: it will indicate how many unread messages are in the "new" topics. (The ones with a "new" icon.)

Over the months of using this on my own forum, I realized this is a very nice feature because there are some topics you don't want to read much, so you only read them when you have some free time. Now let's say you have 20 minutes left. There's this topic I haven't read in months... Will it take me 20 minutes to catch up... Or 20 hours? Ah, if only I could know in advance how many messages are in there! Then I would know if I can read the topic now, or if I should wait a bit before I catch up. See what I mean? :)
Another reason to use it: I've been away for weeks. The latest topics feature sends me back dozens of topics. I want to catch up with them all. It would be best to catch up first with all of the small topics with few messages, then save the long topics for the last sessions. There are really lots of reasons why one would enjoy this.

Anyway, enough with the endless rant, on to the code! It uses SSI. Each "new" topic will use an additional SQL request. I think it can all be gathered into one single SQL request, but I didn't have time to try it. I'd say the current code is quite elegant, but of course you're welcome to post improvements!
You can see a demo on http://blog.cyna.net/ but I think it'll be faster for you to just put that function in your templates and call it (echo latest_smf()) ;)

function latest_smf()
{
global $scripturl, $db_prefix, $txt, $ID_MEMBER;
global $settings, $modSettings, $context, $user_info;

require_once('SSI.php');

$posts = ssi_recentTopics(20,'','nao'); // 20 topics to show.

$t = '
<table border="0" cellpadding="5" class="ssi_table">';
foreach ($posts as $post)
{
// the actual magic code is here :)
if ((!$post['new']) && (!$user_info['is_guest'])) {
$req = "SELECT COUNT(*) AS co FROM {$db_prefix}messages AS m
LEFT JOIN {$db_prefix}log_topics AS lt ON (lt.ID_TOPIC = m.ID_TOPIC AND lt.ID_MEMBER = $ID_MEMBER)
LEFT JOIN {$db_prefix}log_mark_read AS lmr ON (lmr.ID_BOARD = '". $post['board']['id'] . "' AND lmr.ID_MEMBER = $ID_MEMBER)
WHERE m.ID_TOPIC = '" . $post['topic'] . "'
AND (m.ID_MSG > IFNULL(lt.ID_MSG, IFNULL(lmr.ID_MSG, 0)))
LIMIT 1";
$request = db_query($req, __FILE__, __LINE__);
$row = mysql_fetch_assoc($request);
mysql_free_result($request);
$nb_new = $row['co'];
}
// end of magic code, we have our number of new messages :)

$t .= '
<tr>
<td align="right" nowrap="nowrap">
'. $post['time']. '
</td>
<td valign="top">
'. ($post['new'] ? '' : '<a href="' . $scripturl . '?topic=' . $post['topic'] . '.msg'
. $post['new_from'] . ';topicseen#new"><img src="' . $settings['images_url']
. '/' . $context['user']['language'] . '/new.gif" alt="' . $txt[302] . '" border="0" /></a> ('.$nb_new.') ').
'<a href="'. $post['href']. '">'. $post['subject']. '</a>'. $txt[525]. ' '. $post['poster']['link'].
'<br />'. $post['preview']. '</td>
<td align="right" valign="top" nowrap="nowrap">
['. $post['board']['link']. ']
</td>
</tr>';
}

$t .= '
</table>';

return $t;
}


Enjoy!
PS: would there be a way to make sure the post preview correctly removes quotes and closes html tags?
I will not make any deals with you. I've resigned. I will not be pushed, filed, stamped, indexed, briefed, debriefed or numbered.

Aeva Media rocks your life.


Nao 尚

I will not make any deals with you. I've resigned. I will not be pushed, filed, stamped, indexed, briefed, debriefed or numbered.

Aeva Media rocks your life.

Advertisement: