tracker.module 5.45 KB
Newer Older
Dries's avatar
 
Dries committed
1
<?php
2
// $Id$
Dries's avatar
 
Dries committed
3

Dries's avatar
 
Dries committed
4 5 6 7 8
/**
 * @file
 * Enables tracking of recent posts for users.
 */

Dries's avatar
Dries committed
9 10 11 12
/**
 * Implementation of hook_help().
 */
function tracker_help($section) {
Dries's avatar
 
Dries committed
13
  switch ($section) {
Dries's avatar
 
Dries committed
14
    case 'admin/help#tracker':
15 16 17 18 19 20 21 22 23
      $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>';
      $output .= t('<p>You can</p>
<ul>
<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>
<li>not administer this module.</li>
</ul>
', array('%tracker' => url('tracker'), '%profile' => url('profile')));
24
      $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>';
25
      return $output;
Dries's avatar
 
Dries committed
26
    case 'admin/modules#description':
Kjartan's avatar
Kjartan committed
27
      return t('Enables tracking of recent posts for users.');
Dries's avatar
 
Dries committed
28
  }
Dries's avatar
 
Dries committed
29 30
}

Dries's avatar
 
Dries committed
31
/**
Dries's avatar
 
Dries committed
32
 * Implementation of hook_menu().
Dries's avatar
 
Dries committed
33
 */
Dries's avatar
 
Dries committed
34
function tracker_menu($may_cache) {
Dries's avatar
 
Dries committed
35
  global $user;
Dries's avatar
 
Dries committed
36
  $items = array();
Dries's avatar
 
Dries committed
37 38 39 40 41 42 43 44 45 46 47 48

  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's avatar
 
Dries committed
49
  }
Dries's avatar
 
Dries committed
50 51
  else {
    if (arg(0) == 'user' && is_numeric(arg(1))) {
Dries's avatar
Dries committed
52
      $items[] = array('path' => 'user/'. arg(1) .'/track', 'title' => t('track'),
Dries's avatar
 
Dries committed
53
          'callback' => 'tracker_track_user', 'access' => user_access('access content'),
54 55 56
          'type' => MENU_IS_LOCAL_TASK);
      $items[] = array('path' => 'user/'. arg(1) .'/track/posts', 'title' => t('track posts'),
          'type' => MENU_DEFAULT_LOCAL_TASK);
Dries's avatar
 
Dries committed
57 58
    }
  }
Dries's avatar
 
Dries committed
59

Dries's avatar
 
Dries committed
60
  return $items;
Dries's avatar
 
Dries committed
61 62
}

Dries's avatar
 
Dries committed
63 64 65 66
/**
 * Menu callback. Prints a listing of active nodes on the site.
 */
function tracker_track_user() {
67
  if ($account = user_load(array('uid' => arg(1)))) {
68 69 70 71 72 73 74 75 76 77
    if ($account->status || user_access('administer users')) {
      drupal_set_title($account->name);
      return tracker_page($account->uid);      
    }
    else {
      drupal_access_denied();
    }
  }
  else {
    drupal_not_found();
78
  }
Dries's avatar
 
Dries committed
79 80
}

Dries's avatar
Dries committed
81 82 83 84 85
/**
 * Menu callback. Prints a listing of active nodes on the site.
 */
function tracker_page($uid = 0) {
  if ($uid) {
86
    $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';
87
    $sql = db_rewrite_sql($sql);
88
    $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)';
89
    $sql_count = db_rewrite_sql($sql_count);
90
    $result = pager_query($sql, 25, 0, $sql_count, COMMENT_PUBLISHED, $uid, $uid);
Dries's avatar
 
Dries committed
91 92
  }
  else {
Dries's avatar
 
Dries committed
93
    $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';
94
    $sql = db_rewrite_sql($sql);
95
    $sql_count = 'SELECT COUNT(n.nid) FROM {node} n WHERE n.status = 1';
96
    $sql_count = db_rewrite_sql($sql_count);
Steven Wittens's avatar
Steven Wittens committed
97
    $result = pager_query($sql, 25, 0, $sql_count);
Dries's avatar
 
Dries committed
98
  }
Dries's avatar
 
Dries committed
99

Dries's avatar
 
Dries committed
100 101
  while ($node = db_fetch_object($result)) {
    // Determine the number of comments:
Dries's avatar
 
Dries committed
102
    $comments = 0;
103 104
    if (module_exist('comment') && $node->comment_count) {
      $comments = $node->comment_count;
Dries's avatar
 
Dries committed
105 106 107

      if ($new = comment_num_new($node->nid)) {
        $comments .= '<br />';
108
        $comments .= l(format_plural($new, '1 new', '%count new'), "node/$node->nid", NULL, NULL, 'new');
Dries's avatar
 
Dries committed
109
      }
Dries's avatar
 
Dries committed
110 111
    }

Dries's avatar
 
Dries committed
112
    $rows[] = array(
113
      node_get_name($node->type),
114
      l($node->title, "node/$node->nid") .' '. theme('mark', node_mark($node->nid, $node->changed)),
115
      theme('username', $node),
Dries's avatar
 
Dries committed
116
      array('class' => 'replies', 'data' => $comments),
Dries's avatar
 
Dries committed
117
      t('%time ago', array('%time' => format_interval(time() - $node->last_post)))
Dries's avatar
 
Dries committed
118
    );
Dries's avatar
 
Dries committed
119 120
  }

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

123
  $output = '<div id="tracker">';
Dries's avatar
Dries committed
124
  $output .= theme('table', $header, $rows);
125
  $output .= theme('pager', NULL, 25, 0);
Dries's avatar
Dries committed
126
  $output .= '</div>';
Dries's avatar
 
Dries committed
127

Dries's avatar
 
Dries committed
128
  return $output;
Dries's avatar
 
Dries committed
129
}