Skip to content
Snippets Groups Projects
tracker.module 5.53 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':
      $output = '<p>'. t('The tracker module displays the most recently added or updated content to the website allowing users to see the most recent contributions. The tracker module provides user level tracking for those who like to follow the contributions of particular authors.') .'</p>';
      $output .= '<p>'. t('The  &quot;recent posts&quot; page is available via a link in the navigation menu block and contains a reverse chronological list of new and recently-updated content. The table displays  the content type, the title, the author\'s name, how many comments that item has received, and when it was last updated. Updates include any changes to the text, either by the original author or someone else, as well as any new comments added to an item. To use the tracker module to <em>watch</em> for a user\'s updated content, click on that user\'s profile, then the <em>track</em> tab.') .'</p>';
<li>view the <a href="@tracker">most recent posts</a>.</li>
<li>view <a href="@profile">user profiles</a> and select the track tab.</li>
', array('@tracker' => url('tracker'), '@profile' => url('profile')));
      $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="@tracker">Tracker page</a>.', array('@tracker' => 'http://drupal.org/handbook/modules/tracker/')) .'</p>';
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 {
    // Add the CSS for this module
    // We put this in !$may_cache so it's only added once per request
    drupal_add_css(drupal_get_path('module', 'tracker') .'/tracker.css');

Dries Buytaert's avatar
 
Dries Buytaert committed
    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)))) {
    if ($account->status || user_access('administer users')) {
      drupal_set_title($account->name);
      return tracker_page($account->uid);
Dries Buytaert's avatar
 
Dries Buytaert committed
}

/**
 * Menu callback. Prints a listing of active nodes on the site.
 */
function tracker_page($uid = 0) {
  if ($uid) {
    $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 = %d 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 = %d OR c.status IS NULL) WHERE n.status = 1 AND (n.uid = %d OR c.uid = %d)';
    $sql_count = db_rewrite_sql($sql_count);
    $result = pager_query($sql, 25, 0, $sql_count, COMMENT_PUBLISHED, $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(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_exists('comment') && $node->comment_count) {
Dries Buytaert's avatar
 
Dries Buytaert committed

      if ($new = comment_num_new($node->nid)) {
        $comments .= '<br />';
        $comments .= l(format_plural($new, '1 new', '@count 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(
      l($node->title, "node/$node->nid") .' '. theme('mark', node_mark($node->nid, $node->changed)),
Dries Buytaert's avatar
 
Dries Buytaert committed
      array('class' => 'replies', 'data' => $comments),
      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
  $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 .= theme('pager', NULL, 25, 0);
  $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
}