| 1 |
<?php
|
| 2 |
// $Id: xtracker.module,v 1.7.2.3 2008/02/19 18:49:02 kbahey Exp $
|
| 3 |
// modified for D6 hwd 2009/07/31
|
| 4 |
|
| 5 |
/**
|
| 6 |
* @file
|
| 7 |
* Enables tracking of recent posts for users.
|
| 8 |
*/
|
| 9 |
|
| 10 |
define ('XTRACKER_WEIGHT', 5);
|
| 11 |
define ('XTRACKER_PAGE_COUNT', 'xtracker_page_count');
|
| 12 |
define ('XTRACKER_VIEW_TOTALS', 'view nodevote totals');
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Implementation of hook_help().
|
| 16 |
*/
|
| 17 |
function xtracker_help($path, $arg) {
|
| 18 |
switch ($path) {
|
| 19 |
case 'admin/help#xtracker':
|
| 20 |
return t('<p>The xtracker module is a handy module for displaying the most recently added or updated content</p>');
|
| 21 |
case 'admin/modules#description':
|
| 22 |
return t('Enables extended tracking of recent posts for users.');
|
| 23 |
}
|
| 24 |
}
|
| 25 |
|
| 26 |
function xtracker_perm() {
|
| 27 |
return array (XTRACKER_VIEW_TOTALS);
|
| 28 |
}
|
| 29 |
|
| 30 |
function xtracker_admin_settings() {
|
| 31 |
|
| 32 |
$vocabs = array();
|
| 33 |
|
| 34 |
$vocabularies = taxonomy_get_vocabularies();
|
| 35 |
$vocabs[] = '<'. t('none') .'>';
|
| 36 |
foreach ($vocabularies as $vocabulary) {
|
| 37 |
$vocabs[$vocabulary->vid] = $vocabulary->name;
|
| 38 |
}
|
| 39 |
|
| 40 |
$form[XTRACKER_PAGE_COUNT] = array(
|
| 41 |
'#type' => 'select',
|
| 42 |
'#title' => t('Node per page'),
|
| 43 |
'#default_value' => variable_get(XTRACKER_PAGE_COUNT, 10),
|
| 44 |
'#options' => drupal_map_assoc(array(5, 10, 15, 20, 25, 30, 50, 100)),
|
| 45 |
'#description' => t('Number of nodes to show per page.'),
|
| 46 |
);
|
| 47 |
|
| 48 |
$form['xtracker_vid'] = array(
|
| 49 |
'#type' => 'select',
|
| 50 |
'#title' => t('Category vocabulary'),
|
| 51 |
'#default_value' => variable_get('xtracker_vid', 0),
|
| 52 |
'#options' => $vocabs,
|
| 53 |
'#description' => t('Select a vocabulary to show in xtracker. Choosing "none" will list by content type instead.'),
|
| 54 |
);
|
| 55 |
return system_settings_form($form);
|
| 56 |
}
|
| 57 |
|
| 58 |
/**
|
| 59 |
* Implementation of hook_menu().
|
| 60 |
*/
|
| 61 |
function xtracker_menu() {
|
| 62 |
global $user;
|
| 63 |
$items = array();
|
| 64 |
|
| 65 |
$vid = variable_get('xtracker_vid', 0);
|
| 66 |
$terms = xtracker_get_terms($vid);
|
| 67 |
|
| 68 |
$items['admin/settings/xtracker'] = array(
|
| 69 |
'title' => 'xtracker',
|
| 70 |
'description' => 'xtracker settings.',
|
| 71 |
'page callback' => 'drupal_get_form',
|
| 72 |
'page arguments' => array('xtracker_admin_settings'),
|
| 73 |
'access arguments' => array('administer site configuration'),
|
| 74 |
'type' => MENU_NORMAL_ITEM, // optional
|
| 75 |
);
|
| 76 |
|
| 77 |
$items['xtracker'] = array(
|
| 78 |
'title' => 'Recent posts',
|
| 79 |
'page callback' => 'xtracker_page',
|
| 80 |
'access arguments' => array('access content'),
|
| 81 |
'weight' => 1);
|
| 82 |
|
| 83 |
$items['xtracker/all'] = array(
|
| 84 |
'title' => 'all',
|
| 85 |
'type' => MENU_DEFAULT_LOCAL_TASK);
|
| 86 |
|
| 87 |
|
| 88 |
if (empty($terms)) {
|
| 89 |
foreach (node_get_types() as $node_type) {
|
| 90 |
$type = $node_type->type;
|
| 91 |
$name = $node_type->name;
|
| 92 |
$items['xtracker/type/'. $type] = array(
|
| 93 |
'page callback' => 'xtracker_page',
|
| 94 |
'title' => $name,
|
| 95 |
'access arguments' => array('access content'),
|
| 96 |
'type' => MENU_LOCAL_TASK,
|
| 97 |
'weight' => XTRACKER_WEIGHT+$i);
|
| 98 |
$i++;
|
| 99 |
}
|
| 100 |
}
|
| 101 |
else {
|
| 102 |
foreach ($terms as $tid => $name) {
|
| 103 |
$items['xtracker/tid/'. $tid] = array(
|
| 104 |
'page callback' => 'xtracker_page',
|
| 105 |
'title' => $name,
|
| 106 |
'access callback' => '_xtracker_view_totals',
|
| 107 |
// 'access arguments' => array('access content'),
|
| 108 |
'type' => MENU_LOCAL_TASK,
|
| 109 |
'weight' => XTRACKER_WEIGHT+$i);
|
| 110 |
$i++;
|
| 111 |
}
|
| 112 |
}
|
| 113 |
|
| 114 |
// this doesn't work properly - any help?
|
| 115 |
$items['xtracker/%user_uid_optional'] = array(
|
| 116 |
'title' => 'my posts',
|
| 117 |
'access callback' => '_xtracker_myrecent_access',
|
| 118 |
'access arguments' => array(1),
|
| 119 |
'type' => MENU_LOCAL_TASK);
|
| 120 |
|
| 121 |
$items['user/%user/xtrack'] = array(
|
| 122 |
'title' => 'track',
|
| 123 |
'page callback' => 'xtracker_track_user',
|
| 124 |
'access callback' => '_xtracker_user_access',
|
| 125 |
'access arguments' => array(1),
|
| 126 |
'type' => MENU_IS_LOCAL_TASK);
|
| 127 |
|
| 128 |
$items['user/%user/xtrack/posts'] = array(
|
| 129 |
'title' => 'track posts',
|
| 130 |
'type' => MENU_DEFAULT_LOCAL_TASK);
|
| 131 |
|
| 132 |
return $items;
|
| 133 |
}
|
| 134 |
|
| 135 |
/**
|
| 136 |
* Menu callback. Prints a listing of active nodes on the site.
|
| 137 |
*/
|
| 138 |
function xtracker_track_user() {
|
| 139 |
if ($account = user_load(array('uid' => arg(1)))) {
|
| 140 |
drupal_set_title($account->name);
|
| 141 |
xtracker_page($account->uid);
|
| 142 |
}
|
| 143 |
else {
|
| 144 |
drupal_set_message(t('user not found'));
|
| 145 |
print theme('page', ' ');
|
| 146 |
}
|
| 147 |
}
|
| 148 |
|
| 149 |
/**
|
| 150 |
* Menu callback. Prints a listing of active nodes on the site.
|
| 151 |
*/
|
| 152 |
function xtracker_page($uid = 0) {
|
| 153 |
$cmd = htmlspecialchars(arg(0));
|
| 154 |
$type = htmlspecialchars(arg(1));
|
| 155 |
$id = htmlspecialchars(arg(2));
|
| 156 |
|
| 157 |
if ($cmd == 'user' && is_numeric($type)) {
|
| 158 |
$id = (int)$type;
|
| 159 |
$type = 'uid';
|
| 160 |
}
|
| 161 |
|
| 162 |
$sql_where = ' WHERE n.status = 1 ';
|
| 163 |
|
| 164 |
if (module_exists('nodevote') && _xtracker_view_totals()) {
|
| 165 |
$score = TRUE; // Flag we use later
|
| 166 |
$score_column = ',AVG(v.vote) AS score';
|
| 167 |
$extra_join = ' LEFT JOIN {nodevote} v ON n.nid = v.nid ';
|
| 168 |
}
|
| 169 |
|
| 170 |
switch ($type) {
|
| 171 |
case 'uid':
|
| 172 |
$id = (int)$id;
|
| 173 |
$sql_where .= " AND (n.uid = $id OR c.uid = $id) ";
|
| 174 |
break;
|
| 175 |
|
| 176 |
case 'tid':
|
| 177 |
$id = (int)$id;
|
| 178 |
$extra_join .= ' INNER JOIN {term_node} t ON t.nid = n.nid ';
|
| 179 |
$sql_where .= " AND t.tid = $id ";
|
| 180 |
break;
|
| 181 |
|
| 182 |
case 'type':
|
| 183 |
$id = db_escape_string($id);
|
| 184 |
$sql_where .= " AND n.type = '$id' ";
|
| 185 |
break;
|
| 186 |
|
| 187 |
case 'all':
|
| 188 |
default:
|
| 189 |
break;
|
| 190 |
}
|
| 191 |
|
| 192 |
$header = array(
|
| 193 |
array('data' => t('Type'), 'field' => 'type'),
|
| 194 |
array('data' => t('Post'), 'field' => 'title'),
|
| 195 |
array('data' => t('Author'), 'field' => 'uid'),
|
| 196 |
array('data' => t('Replies'), 'field' => 'comment_count'),
|
| 197 |
array('data' => t('Last Post'), 'field' => 'last_update', 'sort' => 'desc')
|
| 198 |
);
|
| 199 |
|
| 200 |
if ($score) {
|
| 201 |
$header = array_merge($header, array(
|
| 202 |
array('data' => t('Score'), 'field' => 'score')
|
| 203 |
));
|
| 204 |
}
|
| 205 |
|
| 206 |
$sql_from = ' FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid LEFT JOIN {comments} c ON n.nid = c.nid'. $extra_join;
|
| 207 |
|
| 208 |
$sql = 'SELECT DISTINCT(n.nid), n.title, n.type, n.changed, n.uid, GREATEST(n.changed, l.last_comment_timestamp) AS last_update, l.comment_count '. $score_column . $sql_from . $sql_where .' GROUP BY n.nid ';
|
| 209 |
|
| 210 |
$sql_count = 'SELECT COUNT(DISTINCT(n.nid)) '. $sql_from . $sql_where;
|
| 211 |
|
| 212 |
$sql .= tablesort_sql($header);
|
| 213 |
$result = pager_query(db_rewrite_sql($sql), variable_get(XTRACKER_PAGE_COUNT, 10), 0, $sql_count);
|
| 214 |
|
| 215 |
while ($node = db_fetch_object($result)) {
|
| 216 |
// Determine the number of comments:
|
| 217 |
$comments = 0;
|
| 218 |
if (module_exists('comment') && $node->comment_count) {
|
| 219 |
$comments = $node->comment_count;
|
| 220 |
|
| 221 |
if ($new == comment_num_new($node->nid)) {
|
| 222 |
$comments .= '<br />';
|
| 223 |
// $comments .= l(t('@num new', array('@num' => $new)), "node/$node->nid", NULL, NULL, 'new');
|
| 224 |
$comments .= l(t('@num new', array('@num' => $new)), "node/$node->nid", array('fragment' => 'new'));
|
| 225 |
}
|
| 226 |
}
|
| 227 |
|
| 228 |
$result2 = db_query('SELECT uid, name FROM {users} WHERE uid = %d', $node->uid);
|
| 229 |
$author = db_fetch_object($result2);
|
| 230 |
$author_name = theme_username($author);
|
| 231 |
|
| 232 |
$row = array(
|
| 233 |
node_get_types('name', $node),
|
| 234 |
l($node->title, "node/$node->nid") .' '. theme('mark', node_mark($node->nid, $node->changed)),
|
| 235 |
$author_name,
|
| 236 |
array('class' => 'replies', 'data' => $comments),
|
| 237 |
t('@time ago', array('@time' => format_interval(time() - $node->last_update))),
|
| 238 |
);
|
| 239 |
|
| 240 |
if ($score) {
|
| 241 |
$rows[] = array_merge($row, array(number_format($node->score, 1)));
|
| 242 |
}
|
| 243 |
else {
|
| 244 |
$rows[] = $row;
|
| 245 |
}
|
| 246 |
}
|
| 247 |
|
| 248 |
if (!$rows) {
|
| 249 |
$rows[] = array(
|
| 250 |
array('data' => t('No data.'), 'colspan' => count($header))
|
| 251 |
);
|
| 252 |
}
|
| 253 |
|
| 254 |
$pager = theme('pager', NULL, variable_get(XTRACKER_PAGE_COUNT, 10), 0);
|
| 255 |
if (!empty($pager)) {
|
| 256 |
$rows[] = array(array('data' => $pager, 'colspan' => count($header)));
|
| 257 |
}
|
| 258 |
|
| 259 |
$attributes = array('class' => "xtracker");
|
| 260 |
|
| 261 |
print theme('page', theme('table', $header, $rows, $attributes));
|
| 262 |
}
|
| 263 |
|
| 264 |
function xtracker_get_terms($vid) {
|
| 265 |
$data = array();
|
| 266 |
if ($vid) {
|
| 267 |
$result = db_query('SELECT t.tid, t.name FROM {term_data} t WHERE t.vid = %d ORDER BY weight', $vid);
|
| 268 |
while ($row = db_fetch_object($result)) {
|
| 269 |
$data[$row->tid] = $row->name;
|
| 270 |
}
|
| 271 |
}
|
| 272 |
return $data;
|
| 273 |
}
|
| 274 |
|
| 275 |
/**
|
| 276 |
* Access callback for tracker/%user_uid_optional
|
| 277 |
*/
|
| 278 |
function _xtracker_myrecent_access($account) {
|
| 279 |
// This path is only allowed for authenticated users looking at their own posts.
|
| 280 |
return $account->uid && ($GLOBALS['user']->uid == $account->uid) && user_access('access content');
|
| 281 |
}
|
| 282 |
|
| 283 |
/**
|
| 284 |
* Access callback for user/%user/track
|
| 285 |
* from core tracker module
|
| 286 |
*/
|
| 287 |
function _xtracker_user_access($account) {
|
| 288 |
return user_view_access($account) && user_access('access content');
|
| 289 |
}
|
| 290 |
|
| 291 |
/**
|
| 292 |
* Access callback for viewing vote totals
|
| 293 |
*/
|
| 294 |
function _xtracker_view_totals() {
|
| 295 |
return user_access('access content') && user_access(XTRACKER_VIEW_TOTALS);
|
| 296 |
}
|