"The Blogs" block with new content monitor

Blocks including this snippet will show a list of blog names.
- This snippet orders the blog list by the number of posts in a particular blog. More prolific bloggers float to the top.
- This snippet drops blog names from the blog list if their content has not been updated for 30 days (we have a lot of bloggers), but that could easily be modified by changing the first db query (get rid of "and n.created > %d" and ", NODE_NEW_LIMIT".

If a particular blog has been updated since the reader's last visit, and within the last 7 days, it will have an asterisk next to it. A slight modification to the code would display the number of new blog posts and/or link to the latest new blog post, per blogger.
- If the reader is anonymous, no asterisks will be displayed.

This snippet is in use on 4.6.5 (at The Campus Press Blogs).

Suggestions/modifications welcome.

<?php
/* This code gets blogs that are newer than NODE_NEW_LIMIT (defaults to 30 days).
* If the reader is a registered user, it also figures out how many new posts
* are in each of the blogs, and puts an asterisk next to blog names that
* have new content in them.
*/

$result = db_query("SELECT u.uid, u.name, COUNT(0) AS num FROM {node} n LEFT JOIN {users} u ON u.uid = n.uid WHERE n.type = 'blog' and n.created > %d GROUP BY u.uid, u.name ORDER BY num DESC", NODE_NEW_LIMIT);

global
$user;
if (
$user->uid) {
 
$new_posts = blog_num_new();
}

while (
$blog = db_fetch_object($result)) {
 
$blogger = user_load(array('uid' => $blog->uid));
 
$num_new = (count($new_posts[$blog->uid]) ? count($new_posts[$blog->uid]) : 0);
 
$output .= '<li>' . l($blogger->blogtitle, 'blog/' . $blog->uid);
 
$output .= ($num_new ? " <span class='marker'>*<span></li>" : "</li>");
}
print
'<ul>'.$output.'</ul>';

if (
$user->uid) {
    print
'<small><span class="marker">*</span>new content</small>';
}

function
blog_num_new() {
  global
$user;
  if (
$user->uid) {
   
$result = db_query("select distinct(nid) nid from {history} where uid = %d", $user->uid);
    while (
$row = db_fetch_object($result)) {
     
$nodes_viewed[$row->nid] = 1;
    }
   
$result = db_query("SELECT nid, uid FROM {node} where type = 'blog' and status = 1 and created > %d", time() - 7 * 60 * 60 * 24);
    while (
$row = db_fetch_object($result)) {
      if (!
$nodes_viewed[$row->nid]) {
       
$new_posts[$row->uid][$row->nid] = 1;
      }
    }
    return
$new_posts;
  }
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.