tracker.module 5.35 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
      $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>';
17
      $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>';
18
      return $output;
Dries's avatar
 
Dries committed
19
  }
Dries's avatar
 
Dries committed
20 21
}

Dries's avatar
 
Dries committed
22
/**
Dries's avatar
 
Dries committed
23
 * Implementation of hook_menu().
Dries's avatar
 
Dries committed
24
 */
25 26
function tracker_menu() {
  $items['tracker'] = array(
27
    'title' => 'Recent posts',
28 29 30 31
    'page callback' => 'tracker_page',
    'access arguments' => array('access content'),
    'weight' => 1,
  );
Dries's avatar
 
Dries committed
32

33
  $items['tracker/all'] = array(
34
    'title' => 'All recent posts',
35 36 37
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'access callback' => 'user_is_logged_in',
  );
38 39

  $items['tracker/%user_current'] = array(
40
    'title' => 'My recent posts',
41 42 43
    'type' => MENU_LOCAL_TASK,
    'access callback' => 'user_is_logged_in',
  );
44 45

  $items['user/%user/track'] = array(
46
    'title' => 'Track',
47
    'page callback' => 'tracker_track_user',
48
    'access callback' => 'user_access',
49 50 51
    'access arguments' => array('access content'),
    'type' => MENU_LOCAL_TASK,
  );
52
  $items['user/%user/track/posts'] = array(
53
    'title' => 'Track posts',
54 55
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
Dries's avatar
 
Dries committed
56
  return $items;
Dries's avatar
 
Dries committed
57 58
}

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

Dries's avatar
Dries committed
77 78 79 80
/**
 * Menu callback. Prints a listing of active nodes on the site.
 */
function tracker_page($uid = 0) {
81
  // Add CSS
82
  drupal_add_css(drupal_get_path('module', 'tracker') .'/tracker.css', 'module', 'all', FALSE);
83

84
  // TODO: These queries are very expensive, see http://drupal.org/node/105639
Dries's avatar
Dries committed
85
  if ($uid) {
86
    $sql = 'SELECT DISTINCT(n.nid), n.title, n.type, n.changed, n.uid, u.name, GREATEST(n.changed, l.last_comment_timestamp) AS last_updated, 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_updated 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 {
93
    $sql = 'SELECT DISTINCT(n.nid), n.title, n.type, n.changed, n.uid, u.name, GREATEST(n.changed, l.last_comment_timestamp) AS last_updated, 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_updated 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

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

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

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

122 123 124 125
  if (!$rows) {
    $rows[] = array(array('data' => t('No posts available.'), 'colspan' => '5'));
  }

126
  $header = array(t('Type'), t('Post'), t('Author'), t('Replies'), t('Last updated'));
Dries's avatar
 
Dries committed
127

128
  $output = '<div id="tracker">';
Dries's avatar
Dries committed
129
  $output .= theme('table', $header, $rows);
130
  $output .= theme('pager', NULL, 25, 0);
Dries's avatar
Dries committed
131
  $output .= '</div>';
Dries's avatar
 
Dries committed
132

Dries's avatar
 
Dries committed
133
  return $output;
Dries's avatar
 
Dries committed
134
}