Skip to content
Snippets Groups Projects
tracker.module 5.39 KiB
Newer Older
Dries Buytaert's avatar
Dries Buytaert committed
<?php
Dries Buytaert's avatar
Dries Buytaert committed

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * @file
 * Enables tracking of recent posts for users.
 */

/**
 * Implementation of hook_help().
 */
function tracker_help($section) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  switch ($section) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    case 'admin/help#tracker':
Dries Buytaert's avatar
 
Dries Buytaert committed
      return t('<p>The tracker module is a handy module for displaying the most recently added or updated content to a Drupal site.  The link to the tracker is labeled <em>recent posts</em> in the user\'s navigation block.  Updates include changes to the text by either the original author or someone else that has permission to edit the content, such as an editor or administrator as well as all comments added to an item.</p>
<p>The Tracker module presents a page listing the recently-updated content written by the user with the content type, the title, the user\'s name, how many comments that item has received, as well as how long ago it was updated.  If an item was written by someone else, tracker will show that item at the top of the list.  An example:</p>
Dries Buytaert's avatar
 
Dries Buytaert committed
<p>A user named Jessica writes a blog post, then some time passes, and others write blog posts.  Then if John posts a comment to Jessica\'s post, and you have bookmarked John\'s tracker page (see below on how to do this) then Jessica\'s content will appear at the top.</p>
<p>If an user with <i>administer comments</i> (e.g. an administrator or editor of a site) deletes a comment (e.g. it is off-topic, inappropriate language, or unsolicited advertisement), the content item will drop down to when it was updated previous to that deleted comment.</p>
<p>To use the Tracker module to "watch" for a user\'s updated content, click on that user\'s profile, then the "track" tab.</p>');
Dries Buytaert's avatar
 
Dries Buytaert committed
    case 'admin/modules#description':
Kjartan Mannes's avatar
Kjartan Mannes committed
      return t('Enables tracking of recent posts for users.');
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
Dries Buytaert's avatar
 
Dries Buytaert committed
 * Implementation of hook_menu().
Dries Buytaert's avatar
 
Dries Buytaert committed
 */
Dries Buytaert's avatar
 
Dries Buytaert committed
function tracker_menu($may_cache) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  global $user;
Dries Buytaert's avatar
 
Dries Buytaert committed
  $items = array();
Dries Buytaert's avatar
 
Dries Buytaert committed

  if ($may_cache) {
    $items[] = array('path' => 'tracker', 'title' => t('recent posts'),
      'callback' => 'tracker_page', 'access' => user_access('access content'),
      'weight' => 1);

    if ($user->uid) {
      $items[] = array('path' => 'tracker/all', 'title' => t('all recent posts'),
        'type' => MENU_DEFAULT_LOCAL_TASK);
      $items[] = array('path' => 'tracker/'. $user->uid, 'title' => t('my recent posts'),
        'type' => MENU_LOCAL_TASK);
    }
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
  else {
    if (arg(0) == 'user' && is_numeric(arg(1))) {
      $items[] = array('path' => 'user/'. arg(1) .'/track', 'title' => t('track'),
Dries Buytaert's avatar
 
Dries Buytaert committed
          'callback' => 'tracker_track_user', 'access' => user_access('access content'),
          'type' => MENU_IS_LOCAL_TASK);
      $items[] = array('path' => 'user/'. arg(1) .'/track/posts', 'title' => t('track posts'),
          'type' => MENU_DEFAULT_LOCAL_TASK);
Dries Buytaert's avatar
 
Dries Buytaert committed
    }
  }
Dries Buytaert's avatar
 
Dries Buytaert committed

Dries Buytaert's avatar
 
Dries Buytaert committed
  return $items;
Dries Buytaert's avatar
Dries Buytaert committed
}

Dries Buytaert's avatar
 
Dries Buytaert committed
/**
 * Menu callback. Prints a listing of active nodes on the site.
 */
function tracker_track_user() {
  if ($account = user_load(array('uid' => arg(1)))) {
    drupal_set_title($account->name);
Dries Buytaert's avatar
 
Dries Buytaert committed
}

/**
 * Menu callback. Prints a listing of active nodes on the site.
 */
function tracker_page($uid = 0) {
Dries Buytaert's avatar
 
Dries Buytaert committed
  global $user;

  $output .= '';

  if ($uid) {
Dries Buytaert's avatar
 
Dries Buytaert committed
    $sql = 'SELECT DISTINCT(n.nid), n.title, n.type, n.changed, n.uid, u.name, l.last_comment_timestamp AS last_post, l.comment_count FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid INNER JOIN {users} u ON n.uid = u.uid LEFT JOIN {comments} c ON n.nid = c.nid AND (c.status = 0 OR c.status IS NULL) WHERE n.status = 1 AND (n.uid = %d OR c.uid = %d) ORDER BY last_post DESC';
    $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n LEFT JOIN {comments} c ON n.nid = c.nid AND (c.status = 0 OR c.status IS NULL) WHERE n.status = 1 AND (n.uid = %d OR c.uid = %d)';
    $sql_count = db_rewrite_sql($sql_count);
Steven Wittens's avatar
Steven Wittens committed
    $result = pager_query($sql, 25, 0, $sql_count, $uid, $uid);
Dries Buytaert's avatar
Dries Buytaert committed
  }
  else {
Dries Buytaert's avatar
 
Dries Buytaert committed
    $sql = 'SELECT DISTINCT(n.nid), n.title, n.type, n.changed, n.uid, u.name, l.last_comment_timestamp AS last_post, l.comment_count FROM {node} n INNER JOIN {users} u ON n.uid = u.uid INNER JOIN {node_comment_statistics} l ON n.nid = l.nid WHERE n.status = 1 ORDER BY last_post DESC';
    $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n WHERE n.status = 1';
    $sql_count = db_rewrite_sql($sql_count);
Steven Wittens's avatar
Steven Wittens committed
    $result = pager_query($sql, 25, 0, $sql_count);
Dries Buytaert's avatar
 
Dries Buytaert committed
  }
Dries Buytaert's avatar
 
Dries Buytaert committed

Dries Buytaert's avatar
 
Dries Buytaert committed
  while ($node = db_fetch_object($result)) {
    // Determine the number of comments:
Dries Buytaert's avatar
 
Dries Buytaert committed
    $comments = 0;
    if (module_exist('comment') && $node->comment_count) {
      $comments = $node->comment_count;
Dries Buytaert's avatar
 
Dries Buytaert committed

      if ($new = comment_num_new($node->nid)) {
        $comments .= '<br />';
        $comments .= l(t('%num new', array('%num' => $new)), "node/$node->nid", NULL, NULL, 'new');
Dries Buytaert's avatar
 
Dries Buytaert committed
      }
Dries Buytaert's avatar
 
Dries Buytaert committed
    }

Dries Buytaert's avatar
 
Dries Buytaert committed
    $rows[] = array(
Dries Buytaert's avatar
 
Dries Buytaert committed
      node_invoke($node->type, 'node_name'),
      l($node->title, "node/$node->nid") .' '. theme('mark', node_mark($node->nid, $node->changed)),
Dries Buytaert's avatar
 
Dries Buytaert committed
      format_name($node),
      array('class' => 'replies', 'data' => $comments),
Dries Buytaert's avatar
 
Dries Buytaert committed
      t('%time ago', array('%time' => format_interval(time() - $node->last_post)))
Dries Buytaert's avatar
 
Dries Buytaert committed
    );
Dries Buytaert's avatar
Dries Buytaert committed
  }

Dries Buytaert's avatar
 
Dries Buytaert committed
  if ($pager = theme('pager', NULL, 25, 0)) {
Dries Buytaert's avatar
 
Dries Buytaert committed
   $rows[] = array(array('data' => $pager, 'colspan' => '5'));
Dries Buytaert's avatar
 
Dries Buytaert committed
  }

Dries Buytaert's avatar
 
Dries Buytaert committed
  $header = array(t('Type'), t('Post'), t('Author'), t('Replies'), t('Last post'));
Dries Buytaert's avatar
 
Dries Buytaert committed

  $output .= '<div id="tracker">';
  $output .= theme('table', $header, $rows);
  $output .= '</div>';
Dries Buytaert's avatar
 
Dries Buytaert committed

Dries Buytaert's avatar
 
Dries Buytaert committed
  return $output;
Dries Buytaert's avatar
Dries Buytaert committed
}

Dries Buytaert's avatar
 
Dries Buytaert committed
?>