| 1 |
<?php
|
| 2 |
// $Id: activity.module,v 1.1.2.2.2.35 2008/05/19 07:12:27 jaydub Exp $
|
| 3 |
|
| 4 |
|
| 5 |
/**
|
| 6 |
* @file
|
| 7 |
* Activity module: Allow users to see their friends' activity on the site.
|
| 8 |
*/
|
| 9 |
|
| 10 |
define('ACTIVITY_ALL', -1);
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Implementation of hook_perm().
|
| 14 |
*/
|
| 15 |
function activity_perm() {
|
| 16 |
return array('administer activity', 'view own activity', 'view public activity');
|
| 17 |
}
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Implementation of hook_menu().
|
| 21 |
*/
|
| 22 |
function activity_menu($may_cache) {
|
| 23 |
$items = array();
|
| 24 |
global $user;
|
| 25 |
|
| 26 |
if ($may_cache) {
|
| 27 |
$items[] = array(
|
| 28 |
'path' => 'activity',
|
| 29 |
'title' => t('Activity'),
|
| 30 |
'callback' => 'activity_page',
|
| 31 |
'access' => user_access('view public activity'),
|
| 32 |
'weight' => 1,
|
| 33 |
);
|
| 34 |
$items[] = array(
|
| 35 |
'path' => 'activity/all',
|
| 36 |
'title' => t('All activity'),
|
| 37 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 38 |
'access' => user_access('view public activity'),
|
| 39 |
);
|
| 40 |
$items[] = array(
|
| 41 |
'path' => 'activity/mine',
|
| 42 |
'title' => t('My activity'),
|
| 43 |
'access' => $user->uid,
|
| 44 |
'type' => MENU_LOCAL_TASK,
|
| 45 |
'access' => user_access('view own activity'),
|
| 46 |
);
|
| 47 |
$items[] = array(
|
| 48 |
'path' => 'activity/all/feed',
|
| 49 |
'title' => t('All activity RSS'),
|
| 50 |
'callback' => 'activity_feed',
|
| 51 |
'callback arguments' => array(ACTIVITY_ALL),
|
| 52 |
'type' => MENU_CALLBACK,
|
| 53 |
'access' => user_access('view public activity'),
|
| 54 |
);
|
| 55 |
$items[] = array(
|
| 56 |
'path' => 'activity/all/json',
|
| 57 |
'title' => t('All activity JSON'),
|
| 58 |
'callback' => 'activity_json',
|
| 59 |
'callback arguments' => array(ACTIVITY_ALL, 1),
|
| 60 |
'type' => MENU_CALLBACK,
|
| 61 |
'access' => user_access('view public activity'),
|
| 62 |
);
|
| 63 |
$items[] = array(
|
| 64 |
'path' => 'admin/settings/activity',
|
| 65 |
'title' => t('Activity Settings'),
|
| 66 |
'description' => t('Customize what will display on your users activity page.'),
|
| 67 |
'callback' => 'drupal_get_form',
|
| 68 |
'callback arguments' => array('activity_admin_settings'),
|
| 69 |
);
|
| 70 |
}
|
| 71 |
else {
|
| 72 |
if ($user->uid) {
|
| 73 |
$items[] = array(
|
| 74 |
'path' => 'activity/'. $user->uid .'/feed',
|
| 75 |
'title' => t('My activity'),
|
| 76 |
'callback' => 'activity_feed',
|
| 77 |
'callback arguments' => array($user->uid),
|
| 78 |
'type' => MENU_CALLBACK,
|
| 79 |
);
|
| 80 |
$items[] = array(
|
| 81 |
'path' => 'activity/'. $user->uid .'/json',
|
| 82 |
'callback' => 'activity_json',
|
| 83 |
'callback arguments' => array($user->uid, 1),
|
| 84 |
'type' => MENU_CALLBACK,
|
| 85 |
);
|
| 86 |
}
|
| 87 |
}
|
| 88 |
return $items;
|
| 89 |
}
|
| 90 |
|
| 91 |
/**
|
| 92 |
* Admin section
|
| 93 |
* TODO: 1) the defaults aren't available at activity render time unless this
|
| 94 |
* this page has been saved. This is potentially confusing since they
|
| 95 |
* show up in the textfields.
|
| 96 |
*/
|
| 97 |
function activity_admin_settings() {
|
| 98 |
if ($activity_info = activity_get_info()) {
|
| 99 |
|
| 100 |
$form['general_settings'] = array(
|
| 101 |
'#type' => 'fieldset',
|
| 102 |
'#title' => t('General settings'),
|
| 103 |
'#collapsible' => FALSE,
|
| 104 |
);
|
| 105 |
$form['general_settings']['activity_time_limit'] = array(
|
| 106 |
'#type' => 'select',
|
| 107 |
'#title' => t('Activity time limiter'),
|
| 108 |
'#description' => t("Allows you to set a time limit for recording activity so repeated actions don't flood your activity feed. If the same action is submitted within X seconds of the last activity record of the same type from the same user then the activity is not logged."),
|
| 109 |
'#options' => drupal_map_assoc(array(5, 10, 20, 30, 60, 120, 300, 600, 1800), format_interval),
|
| 110 |
'#default_value' => variable_get('activity_time_limit', 30),
|
| 111 |
);
|
| 112 |
|
| 113 |
foreach ($activity_info as $module => $info) {
|
| 114 |
if (!empty($info)) {
|
| 115 |
$tokens = array();
|
| 116 |
$token_list = array();
|
| 117 |
foreach (token_get_list($module) as $name => $token_array) {
|
| 118 |
$token_list = array_merge($token_list, $token_array);
|
| 119 |
}
|
| 120 |
foreach (token_get_list('activity') as $name => $token_array) {
|
| 121 |
$token_list = array_merge($token_list, $token_array);
|
| 122 |
}
|
| 123 |
ksort($token_list);
|
| 124 |
foreach ($token_list as $token => $desc) {
|
| 125 |
$tokens[] = '['. $token .']: '. $desc;
|
| 126 |
}
|
| 127 |
|
| 128 |
if (count($info['types'])) {
|
| 129 |
$form[$module] = array(
|
| 130 |
'#type' => 'fieldset',
|
| 131 |
'#title' => t('Tokens for @name', array('@name' => t($module))),
|
| 132 |
'#collapsible' => true,
|
| 133 |
'#collapsed' => true,
|
| 134 |
'#description' => t('Available tokens') . theme('item_list', $tokens),
|
| 135 |
);
|
| 136 |
|
| 137 |
if ($ops = $info['ops']) {
|
| 138 |
if ($roles = $info['roles']) {
|
| 139 |
foreach ($roles as $role_name => $role) {
|
| 140 |
$form[$module][$role_name] = array(
|
| 141 |
'#type' => 'fieldset',
|
| 142 |
'#title' => t('Messages visible to the "@role_name" role.', array('@role_name' => $role['#name'])),
|
| 143 |
'#collapsible' => true,
|
| 144 |
'#collapsed' => false,
|
| 145 |
'#description' => $role['#description'] ? $role['#description'] : '',
|
| 146 |
);
|
| 147 |
}
|
| 148 |
|
| 149 |
if ($types = $info['types']) {
|
| 150 |
foreach ($ops as $op_name => $op) {
|
| 151 |
foreach ($info['types'] as $type_name => $type) {
|
| 152 |
foreach ($roles as $role_name => $role) {
|
| 153 |
$token_field = "{$module}_{$type_name}_{$op_name}_{$role_name}";
|
| 154 |
$form[$module][$role_name][$token_field] = array(
|
| 155 |
'#type' => 'textfield',
|
| 156 |
'#title' => $type .': '. $op,
|
| 157 |
'#default_value' => variable_get($token_field, $role['#default'] ? $role['#default'] : ''),
|
| 158 |
);
|
| 159 |
}
|
| 160 |
}
|
| 161 |
}
|
| 162 |
}
|
| 163 |
}
|
| 164 |
}
|
| 165 |
}
|
| 166 |
}
|
| 167 |
}
|
| 168 |
drupal_set_message('You must save this form in order for your tokens to work.');
|
| 169 |
return system_settings_form($form);
|
| 170 |
}
|
| 171 |
else {
|
| 172 |
drupal_set_message(t('No supported modules enabled. Check the !activity_section for supported modules.', array('!activity_section' => l(t('Activity section'), 'admin/build/modules'))));
|
| 173 |
}
|
| 174 |
}
|
| 175 |
|
| 176 |
/**
|
| 177 |
* API function
|
| 178 |
*
|
| 179 |
* @return an array of module names and metadata from those modules that
|
| 180 |
* implement hook_activity_info.
|
| 181 |
*/
|
| 182 |
function activity_get_info() {
|
| 183 |
foreach (module_implements('activity_info') as $module) {
|
| 184 |
$info[$module] = module_invoke($module, 'activity_info');
|
| 185 |
}
|
| 186 |
return $info;
|
| 187 |
}
|
| 188 |
|
| 189 |
/**
|
| 190 |
* API function
|
| 191 |
* Insert an activity record. This gets called by modules wishing to record
|
| 192 |
* their activities.
|
| 193 |
* @param $module The name of the module that is doing the recording, eg. 'node'
|
| 194 |
* @param $type Module's can track more than one type of activity. For example,
|
| 195 |
* the nodeactivity module tracks activities for each content type separately.
|
| 196 |
* $type should be an identifier for the calling module to use.
|
| 197 |
*/
|
| 198 |
function activity_insert($module, $type, $operation, $data, $target_users_roles) {
|
| 199 |
// check time limit, ignore activity if within the limit
|
| 200 |
$result = db_query("SELECT COUNT(*) FROM {activity} WHERE module = '%s' AND type = '%s' AND operation = '%s' AND data = '%s' AND created >= %d", $module, $type, $operation, serialize($data), (time() - variable_get('activity_time_limit', 30)));
|
| 201 |
|
| 202 |
if (db_fetch_object($result)->count != 0) {
|
| 203 |
return FALSE;
|
| 204 |
}
|
| 205 |
|
| 206 |
$aid = db_next_id('activity');
|
| 207 |
db_query("INSERT INTO {activity} (aid, module, type, operation, created, data)
|
| 208 |
VALUES (%d, '%s', '%s', '%s', %d, '%s')",
|
| 209 |
$aid, $module, $type, $operation, time(), serialize($data));
|
| 210 |
foreach ($target_users_roles as $uid => $role) {
|
| 211 |
db_query("INSERT INTO {activity_targets} (aid, target_uid, target_role) VALUES (%d, %d, '%s')", $aid, $uid, $role);
|
| 212 |
}
|
| 213 |
|
| 214 |
$activity = array(
|
| 215 |
'module' => $module,
|
| 216 |
'type' => $type,
|
| 217 |
'operation' => $operation,
|
| 218 |
'data' => $data,
|
| 219 |
'target_user_roles' => $target_users_roles,
|
| 220 |
'aid' => $aid
|
| 221 |
);
|
| 222 |
|
| 223 |
// Invoke activityapi
|
| 224 |
activity_invoke_activityapi($activity, 'insert');
|
| 225 |
|
| 226 |
return $aid;
|
| 227 |
}
|
| 228 |
|
| 229 |
/**
|
| 230 |
* The API supports:
|
| 231 |
* @param $uids
|
| 232 |
* - a single uid
|
| 233 |
* - an array of uids
|
| 234 |
* - can include the special uid ACTIVITY_ALL
|
| 235 |
* @param $filters
|
| 236 |
* - an array where keys are one of module, type, operation, target_role
|
| 237 |
* - values are arrays of possible values for the keys. The key of the
|
| 238 |
* array of possible values can be 'include' or 'exclude' to indicate
|
| 239 |
* if the filter is positive or negative
|
| 240 |
* For example:
|
| 241 |
* array('target_role' => array('include' => 'Author'), 'operation' => array('include' => 'delete'))
|
| 242 |
* this would find activity where the author had deleted something.
|
| 243 |
* Example 2:
|
| 244 |
* array('target_role' => array('include' => array('Requester', 'Requestee')))
|
| 245 |
* This shows that the values can be arrays as well.
|
| 246 |
* Example 3:
|
| 247 |
* array('module' => array('include' => array('nodeactivity', 'commentactivity')), 'operation' => array('exclude' => array('delete', 'unpublish')))
|
| 248 |
* @param $limit
|
| 249 |
* The number of results desired
|
| 250 |
* @param $tablesort_headers
|
| 251 |
* An array that determines the sorting of the result set.
|
| 252 |
*
|
| 253 |
*/
|
| 254 |
function activity_get_activity($uids = ACTIVITY_ALL, $filters = NULL, $limit = NULL, $tablesort_headers = NULL) {
|
| 255 |
$wheres = array();
|
| 256 |
|
| 257 |
// Build the WHERE clause for user id.
|
| 258 |
if (!is_array($uids)) {
|
| 259 |
$wheres[] = "activity_targets.target_uid = %d";
|
| 260 |
$params[] = $uids;
|
| 261 |
}
|
| 262 |
else {
|
| 263 |
foreach ($uids as $uid) {
|
| 264 |
$nums[] = "%d";
|
| 265 |
$params[] = $uid;
|
| 266 |
}
|
| 267 |
$wheres[] = 'activity_targets.target_uid IN ('. implode(',', $nums) .')';
|
| 268 |
}
|
| 269 |
|
| 270 |
// Build sql limiting query to on filtered fields
|
| 271 |
if (!empty($filters) && is_array($filters)) {
|
| 272 |
foreach ($filters as $column => $filter) {
|
| 273 |
// Of the possible columns, role is in the at table and all others in the
|
| 274 |
// a table. Prefix the column name with the appropriate table.
|
| 275 |
if ($column == 'target_role') {
|
| 276 |
$column = 'activity_targets.target_role';
|
| 277 |
}
|
| 278 |
else {
|
| 279 |
$column = "activity.{$column}";
|
| 280 |
}
|
| 281 |
|
| 282 |
// attempt to rewrite old filters to the new format
|
| 283 |
if (!is_array($filter) || (!array_key_exists('include', $filter) && !array_key_exists('exclude', $filter))) {
|
| 284 |
$filter = array('include' => $filter);
|
| 285 |
}
|
| 286 |
|
| 287 |
foreach ($filter as $criteria => $values) {
|
| 288 |
if (is_array($values)) {
|
| 289 |
$strings = array();
|
| 290 |
foreach ($values as $value) {
|
| 291 |
$strings[] = "'%s'";
|
| 292 |
$params[] = $value;
|
| 293 |
}
|
| 294 |
$wheres[] = $column . ($criteria == 'exclude' ? ' NOT IN ' : ' IN ') .'('. implode(',', $strings) .')';
|
| 295 |
}
|
| 296 |
else {
|
| 297 |
$wheres[] = $column . ($criteria == 'exclude' ? ' != ' : ' = ') ."'%s'";
|
| 298 |
// $values is a string with the single value.
|
| 299 |
$params[] = $values;
|
| 300 |
}
|
| 301 |
}
|
| 302 |
}
|
| 303 |
}
|
| 304 |
if (count($wheres) > 0) {
|
| 305 |
$where = implode(' AND ', $wheres);
|
| 306 |
$where = "WHERE $where";
|
| 307 |
}
|
| 308 |
|
| 309 |
// We always include tablesort_sql in the query so that this API is friendly
|
| 310 |
// to sortable tables. If no headers were passed in, use the default headers.
|
| 311 |
if (empty($tablesort_headers)) {
|
| 312 |
$tablesort_headers = activity_get_tablesort_headers();
|
| 313 |
$tablesort_headers['activity.created']['sort'] = 'desc';
|
| 314 |
}
|
| 315 |
|
| 316 |
// Build the sql and do the query. Wrapping it in db_rewrite_sql allows other
|
| 317 |
// modules to impose access restrictions on activity listings.
|
| 318 |
$sql = "SELECT activity.*, activity_targets.target_uid, activity_targets.target_role
|
| 319 |
FROM {activity_targets} activity_targets INNER JOIN {activity} activity ON activity.aid = activity_targets.aid
|
| 320 |
$where ";
|
| 321 |
$tablesort_sql = tablesort_sql($tablesort_headers);
|
| 322 |
|
| 323 |
$sql = db_rewrite_sql("$sql $tablesort_sql", 'activity_targets', 'aid', array('uids' => $uids));
|
| 324 |
|
| 325 |
if (is_numeric($limit)) {
|
| 326 |
$result = pager_query($sql, $limit, 0, NULL, $params);
|
| 327 |
}
|
| 328 |
else {
|
| 329 |
$result = db_query($sql, $params);
|
| 330 |
}
|
| 331 |
$activity = array();
|
| 332 |
while ($row = db_fetch_array($result)) {
|
| 333 |
$row['data'] = unserialize($row['data']);
|
| 334 |
$row['data']['created'] = $row['created'];
|
| 335 |
$row['data']['activity_id'] = $row['aid'];
|
| 336 |
$row['data']['module'] = $row['module'];
|
| 337 |
$row['data']['type'] = $row['type'];
|
| 338 |
$row['data']['operation'] = ($row['data']['operation'] ? $row['data']['operation'] : $row['operation']);
|
| 339 |
|
| 340 |
// Invoke activityapi
|
| 341 |
activity_invoke_activityapi($row, 'load');
|
| 342 |
|
| 343 |
$activity[] = $row;
|
| 344 |
}
|
| 345 |
|
| 346 |
return $activity;
|
| 347 |
}
|
| 348 |
|
| 349 |
/**
|
| 350 |
* Invoke a hook_activityapi() operation in all modules.
|
| 351 |
*
|
| 352 |
* @param &$activity
|
| 353 |
* An activity array.
|
| 354 |
* @param $op
|
| 355 |
* A string containing the name of the nodeapi operation.
|
| 356 |
* 'insert' is called when a new activity is created
|
| 357 |
* 'load' is called when an activity is loaded
|
| 358 |
* 'render' is called before token replacement begins
|
| 359 |
* @return
|
| 360 |
* The returned value of the invoked hooks.
|
| 361 |
*/
|
| 362 |
function activity_invoke_activityapi(&$activity, $op) {
|
| 363 |
$return = array();
|
| 364 |
foreach (module_implements('activityapi') as $name) {
|
| 365 |
$function = $name .'_activityapi';
|
| 366 |
$result = $function($activity, $op);
|
| 367 |
if (isset($result) && is_array($result)) {
|
| 368 |
$return = array_merge($return, $result);
|
| 369 |
}
|
| 370 |
else if (isset($result)) {
|
| 371 |
$return[] = $result;
|
| 372 |
}
|
| 373 |
}
|
| 374 |
return $return;
|
| 375 |
}
|
| 376 |
|
| 377 |
|
| 378 |
function activity_get_tablesort_headers() {
|
| 379 |
return array(
|
| 380 |
'activity_targets.aid' => array('field' => 'activity_targets.aid', 'data' => t('Id')),
|
| 381 |
'activity.module' => array('field' => 'activity.module', 'data' => t('Module')),
|
| 382 |
'activity.type' => array('field' => 'activity.type', 'data' => t('Type')),
|
| 383 |
'activity.operation' => array('field' => 'activity.operation', 'data' => t('Operation')),
|
| 384 |
'activity.created' => array('field' => 'activity.created', 'data' => t('Created')),
|
| 385 |
);
|
| 386 |
}
|
| 387 |
|
| 388 |
|
| 389 |
/**
|
| 390 |
* create a block for display
|
| 391 |
* @param op
|
| 392 |
* @param delta
|
| 393 |
* @returns block HTML
|
| 394 |
*
|
| 395 |
* TODO: Add "more activity" link to blocks which goes to activity table page.
|
| 396 |
*/
|
| 397 |
function activity_block($op = 'list', $delta = 0, $edit = array()) {
|
| 398 |
global $user;
|
| 399 |
if ($op == 'list') {
|
| 400 |
$block['my']['info'] = t("Activity: Mine: show the current user's activity.");
|
| 401 |
$block['all']['info'] = t("Activity: All: show all recent activity");
|
| 402 |
return $block;
|
| 403 |
}
|
| 404 |
elseif ($op == 'configure') {
|
| 405 |
$form['items'] = array(
|
| 406 |
'#type' => 'select',
|
| 407 |
'#title' => t('Number of items'),
|
| 408 |
'#default_value' => variable_get('activity_block_'. $delta, 5),
|
| 409 |
'#options' => drupal_map_assoc(range(1, 50)),
|
| 410 |
);
|
| 411 |
return $form;
|
| 412 |
}
|
| 413 |
elseif ($op == 'save') {
|
| 414 |
variable_set('activity_block_'. $delta, $edit['items']);
|
| 415 |
}
|
| 416 |
elseif ($op == 'view') {
|
| 417 |
switch ($delta) {
|
| 418 |
case 'my':
|
| 419 |
if (user_access('view own activity')) {
|
| 420 |
// Grab the number of requested activities plus one. We use this one
|
| 421 |
// to determine whether or not to show the "more" link and only display
|
| 422 |
// the correct number of items.
|
| 423 |
$activity = activity_get_activity($user->uid, NULL, variable_get('activity_block_'. $delta, 5) + 1);
|
| 424 |
if ($count = count($activity)) {
|
| 425 |
if ($count > variable_get('activity_block_'. $delta, 5)) {
|
| 426 |
$more_link = theme('activity_more_link', 'activity/mine');
|
| 427 |
array_pop($activity);
|
| 428 |
}
|
| 429 |
$activites = array();
|
| 430 |
foreach ($activity as $item) {
|
| 431 |
$activities[] = theme('activity', activity_token_replace($item), $item);
|
| 432 |
}
|
| 433 |
return array(
|
| 434 |
'subject' => t('My activity'),
|
| 435 |
'content' => theme('activity_block', $activities, $more_link)
|
| 436 |
);
|
| 437 |
}
|
| 438 |
}
|
| 439 |
break;
|
| 440 |
|
| 441 |
case 'all':
|
| 442 |
if (user_access('view public activity')) {
|
| 443 |
$activity = activity_get_activity(ACTIVITY_ALL, NULL, variable_get('activity_block_'. $delta, 5) + 1);
|
| 444 |
if ($count = count($activity)) {
|
| 445 |
if ($count > variable_get('activity_block_'. $delta, 5)) {
|
| 446 |
$more_link = theme('activity_more_link', 'activity');
|
| 447 |
array_pop($activity);
|
| 448 |
}
|
| 449 |
$activites = array();
|
| 450 |
foreach ($activity as $item) {
|
| 451 |
$activities[] = theme('activity', activity_token_replace($item), $item);
|
| 452 |
}
|
| 453 |
return array(
|
| 454 |
'subject' => t('Recent activity'),
|
| 455 |
'content' => theme('activity_block', $activities, $more_link)
|
| 456 |
);
|
| 457 |
}
|
| 458 |
}
|
| 459 |
break;
|
| 460 |
}
|
| 461 |
}
|
| 462 |
}
|
| 463 |
|
| 464 |
function activity_page($page = 'all') {
|
| 465 |
global $user;
|
| 466 |
|
| 467 |
if ($page == 'mine') {
|
| 468 |
$activities = activity_get_activity($user->uid, NULL, 20);
|
| 469 |
$table = activity_table($activities);
|
| 470 |
$feed_url = url('activity/'. $user->uid .'/feed');
|
| 471 |
drupal_add_feed($feed_url);
|
| 472 |
$feed = theme('feed_icon', $feed_url);
|
| 473 |
return theme('activity_page', $activities, $table);
|
| 474 |
}
|
| 475 |
else if ($page == 'all') {
|
| 476 |
$activities = activity_get_activity(ACTIVITY_ALL, NULL, 20);
|
| 477 |
$table = activity_table($activities);
|
| 478 |
$feed_url = url('activity/all/feed');
|
| 479 |
drupal_add_feed($feed_url);
|
| 480 |
$feed = theme('feed_icon', $feed_url);
|
| 481 |
return theme('activity_page', $activities, $table);
|
| 482 |
}
|
| 483 |
}
|
| 484 |
|
| 485 |
/**
|
| 486 |
* Menu callback to display the records in a page
|
| 487 |
*/
|
| 488 |
function activity_table($activities) {
|
| 489 |
global $user;
|
| 490 |
|
| 491 |
$display_headers = array(
|
| 492 |
'created' => array('field' => 'created', 'data' => t('Date')),
|
| 493 |
//t('Visible to'),
|
| 494 |
t('Message'),
|
| 495 |
);
|
| 496 |
|
| 497 |
$rows = array();
|
| 498 |
foreach ($activities as $activity) {
|
| 499 |
if ($activity['target_uid'] == ACTIVITY_ALL) {
|
| 500 |
$visible_to = t('Everyone');
|
| 501 |
}
|
| 502 |
else if ($activity['target_uid'] == $user->uid) {
|
| 503 |
$visible_to = t('You');
|
| 504 |
}
|
| 505 |
else {
|
| 506 |
// TODO: if this column gets reinstated load the user information differently
|
| 507 |
$visible_to = theme('username', user_load(array('uid' => $activity['target_uid'])));
|
| 508 |
}
|
| 509 |
if ($activity_message = activity_token_replace($activity)) {
|
| 510 |
$rows[] = array(
|
| 511 |
format_date($activity['created'], 'small'),
|
| 512 |
//$visible_to,
|
| 513 |
theme('activity', $activity_message, $activity),
|
| 514 |
);
|
| 515 |
}
|
| 516 |
}
|
| 517 |
$output = theme('table', $display_headers, $rows);
|
| 518 |
$output .= theme('pager');
|
| 519 |
return $output;
|
| 520 |
}
|
| 521 |
|
| 522 |
/**
|
| 523 |
* menu callback to return a feed of a signed in user's activity page
|
| 524 |
*/
|
| 525 |
function activity_feed($arg) {
|
| 526 |
global $locale;
|
| 527 |
if ($arg == ACTIVITY_ALL) {
|
| 528 |
$activities = activity_get_activity(ACTIVITY_ALL, NULL, 20);
|
| 529 |
$url = url('activity/all', NULL, NULL, TRUE);
|
| 530 |
$feed_title = t('All activity');
|
| 531 |
}
|
| 532 |
else if (is_numeric($arg)) {
|
| 533 |
$user = db_fetch_object(db_query('SELECT uid, name FROM {users} WHERE uid = %d', $arg));
|
| 534 |
if ($user) {
|
| 535 |
$activities = activity_get_activity($arg, NULL, 20);
|
| 536 |
$url = url('activity/'. $user->uid, NULL, NULL, TRUE);
|
| 537 |
$feed_title = t('Activity for @username', array('@username' => $user->name));
|
| 538 |
}
|
| 539 |
}
|
| 540 |
|
| 541 |
if (count($activities) > 0) {
|
| 542 |
foreach ($activities as $activity) {
|
| 543 |
$function = $activity['module'] .'_format_rss_item';
|
| 544 |
if (function_exists($function)) {
|
| 545 |
// each module gets a chance to build its own feed item.
|
| 546 |
// They should use the $activity to prepare variables and
|
| 547 |
// call format_rss_item($title, $link, $item_text, $extra);
|
| 548 |
$items .= $function($activity);
|
| 549 |
}
|
| 550 |
else {
|
| 551 |
$message = activity_token_replace($activity);
|
| 552 |
$items .= format_rss_item(strip_tags($message), url('activity/'. $user->uid, NULL, NULL, TRUE), format_date($activity['created']) .'<p>'. $message .'</p>');
|
| 553 |
}
|
| 554 |
}
|
| 555 |
}
|
| 556 |
$channel = array(
|
| 557 |
'version' => '2.0',
|
| 558 |
'title' => variable_get('site_name', 'Drupal') .' - '. $feed_title,
|
| 559 |
'link' => $url,
|
| 560 |
'description' => variable_get('site_mission', ''),
|
| 561 |
'language' => $locale,
|
| 562 |
);
|
| 563 |
|
| 564 |
// TODO: Figure out what the right namespace should be.
|
| 565 |
$namespaces = array('xmlns:dc="http://purl.org/dc/elements/1.1/"');
|
| 566 |
$output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
|
| 567 |
$output .= "<rss version=\"". $channel["version"] ."\" xml:base=\"". $url ."\" ". implode(' ', $namespaces) .">\n";
|
| 568 |
$output .= format_rss_channel($channel['title'], $channel['link'], $channel['description'], $items, $channel['language']);
|
| 569 |
$output .= "</rss>\n";
|
| 570 |
|
| 571 |
drupal_set_header('Content-Type: application/rss+xml; charset=utf-8');
|
| 572 |
print $output;
|
| 573 |
}
|
| 574 |
|
| 575 |
/**
|
| 576 |
* output our activity as json
|
| 577 |
* $arg[0] = ACTIVITY_ALL or $uid
|
| 578 |
* $arg[1] = number of activities to retreive
|
| 579 |
*/
|
| 580 |
function activity_json($arg) {
|
| 581 |
global $locale;
|
| 582 |
$args = func_get_args();
|
| 583 |
if ($args[0] == ACTIVITY_ALL) {
|
| 584 |
// get the latest activity posted
|
| 585 |
$activities = activity_get_activity(ACTIVITY_ALL, NULL, $args[1]);
|
| 586 |
$url = url('activity/all', NULL, NULL, TRUE);
|
| 587 |
$feed_title = t('All activity');
|
| 588 |
}
|
| 589 |
else if (is_numeric($args[0])) {
|
| 590 |
$user = db_fetch_object(db_query('SELECT uid, name FROM {users} WHERE uid = %d', $args[0]));
|
| 591 |
if ($user) {
|
| 592 |
// get the latest activity posted pertaining to this user
|
| 593 |
$activities = activity_get_activity($arg[0], NULL, $args[1]);
|
| 594 |
$url = url('activity/'. $user->uid, NULL, NULL, TRUE);
|
| 595 |
$feed_title = t('Activity for @username', array('@username' => theme('username', $user)));
|
| 596 |
}
|
| 597 |
}
|
| 598 |
if (count($activities) > 0) {
|
| 599 |
foreach ($activities as $activity) {
|
| 600 |
$message = activity_token_replace($activity);
|
| 601 |
$items .= '<item><date>'. format_date($activity['created'], 'small') .'</date>';
|
| 602 |
$items .= '<url>'. url('activity/'. $user->uid, NULL, NULL, TRUE) .'</url>';
|
| 603 |
$items .= '<message>'. $message .'</message></item>';
|
| 604 |
}
|
| 605 |
}
|
| 606 |
drupal_set_header('Content-Type: application/x-javascript');
|
| 607 |
print drupal_to_js($items);
|
| 608 |
die();
|
| 609 |
}
|
| 610 |
|
| 611 |
/**
|
| 612 |
* Token module integration.
|
| 613 |
*/
|
| 614 |
function activity_token_list($type = 'all') {
|
| 615 |
if ($type == 'activity') {
|
| 616 |
$tokens = array('activity' => array());
|
| 617 |
$tokens['activity'] = array(
|
| 618 |
'time-small' => t('Date and time in small format: @example', array('@example' => format_date(time(), 'small'))),
|
| 619 |
'time-medium' => t('Date and time in medium format: @example', array('@example' => format_date(time(), 'medium'))),
|
| 620 |
'time-large' => t('Date and time in large format: @example', array('@example' => format_date(time(), 'large'))),
|
| 621 |
'time-ago' => t('How long ago did this happen? Example: %time-ago', array('%time-ago' => format_interval(time() - 60*60*36))),
|
| 622 |
);
|
| 623 |
return $tokens;
|
| 624 |
}
|
| 625 |
}
|
| 626 |
|
| 627 |
function activity_token_values($type, $data = NULL, $options = array()) {
|
| 628 |
if ($type == 'activity' && !empty($data)) {
|
| 629 |
$tokens = array(
|
| 630 |
'time-small' => format_date($data['created'], 'small'),
|
| 631 |
'time-medium' => format_date($data['created'], 'medium'),
|
| 632 |
'time-large' => format_date($data['created'], 'large'),
|
| 633 |
'time-ago' => format_interval(time() - $data['created']),
|
| 634 |
);
|
| 635 |
return $tokens;
|
| 636 |
}
|
| 637 |
}
|
| 638 |
|
| 639 |
/**
|
| 640 |
* determine what the message should say
|
| 641 |
*/
|
| 642 |
function activity_token_replace($activity) {
|
| 643 |
extract($activity);
|
| 644 |
|
| 645 |
$var = "{$module}_{$type}_{$operation}_{$target_role}";
|
| 646 |
if ($pattern = variable_get($var, FALSE)) {
|
| 647 |
// Invoke activityapi
|
| 648 |
activity_invoke_activityapi($activity, 'render');
|
| 649 |
$message = token_replace($pattern, $module, $data);
|
| 650 |
$message = token_replace($message, 'activity', $data);
|
| 651 |
return $message;
|
| 652 |
}
|
| 653 |
}
|
| 654 |
|
| 655 |
/**
|
| 656 |
* Minimal user_load replacement for activity
|
| 657 |
*/
|
| 658 |
function activity_user_load($uid) {
|
| 659 |
static $users;
|
| 660 |
if (!isset($users[$uid])) {
|
| 661 |
$users[$uid] = db_fetch_object(db_query('SELECT uid, name FROM {users} WHERE uid = %d', $uid));
|
| 662 |
}
|
| 663 |
return $users[$uid];
|
| 664 |
}
|
| 665 |
|
| 666 |
/**
|
| 667 |
* check if user has activity privacy optout set
|
| 668 |
*/
|
| 669 |
function activity_user_privacy_optout($user) {
|
| 670 |
if (variable_get('activity_user_optout', 0) && $user->activity_optout) {
|
| 671 |
return TRUE;
|
| 672 |
}
|
| 673 |
return FALSE;
|
| 674 |
}
|
| 675 |
|
| 676 |
/**
|
| 677 |
* date formatter
|
| 678 |
* $timestamp is the unix timestamp() of when an activity occurred.
|
| 679 |
* $end is the human language formatted string expressing how long ago this was.
|
| 680 |
*/
|
| 681 |
function activity_format_offset($timestamp) {
|
| 682 |
$offset = (strftime("%j") + strftime("%Y") * 365) - (strftime("%j", $timestamp) + strftime("%Y", $timestamp) * 365);
|
| 683 |
if ($offset >= 7) {
|
| 684 |
$offset = (strftime("%V") + strftime("%Y") * 52) - (strftime("%V", $timestamp) + strftime("%Y", $timestamp) * 52);
|
| 685 |
$end = $offset != 0 ? format_plural($offset, t("a week ago"), t("@count weeks ago", array("@count" => $offset))) : t("Today");
|
| 686 |
}
|
| 687 |
else if ($offset > 0) {
|
| 688 |
$end = format_plural($offset, t('Yesterday'), t('@count days ago', array('@count' => $offset)));
|
| 689 |
}
|
| 690 |
else {
|
| 691 |
$end = t('Today');
|
| 692 |
}
|
| 693 |
return $end;
|
| 694 |
}
|
| 695 |
|
| 696 |
/**
|
| 697 |
* Implementation of hook_simpletest().
|
| 698 |
*/
|
| 699 |
function activity_simpletest() {
|
| 700 |
$module_name = 'activity';
|
| 701 |
$dir = drupal_get_path('module', $module_name) .'/tests';
|
| 702 |
$tests = file_scan_directory($dir, '\.test$');
|
| 703 |
return array_keys($tests);
|
| 704 |
}
|
| 705 |
|
| 706 |
/**
|
| 707 |
* theme function for displaying the users activity page
|
| 708 |
*/
|
| 709 |
function theme_activity_page($activities, $table) {
|
| 710 |
return $table;
|
| 711 |
//return theme('item_list', $items, NULL, 'ul', array('class' => 'activity-list'));
|
| 712 |
}
|
| 713 |
|
| 714 |
/**
|
| 715 |
* theme function to return username
|
| 716 |
*/
|
| 717 |
function theme_activity_username($account, $self = FALSE) {
|
| 718 |
global $user;
|
| 719 |
return ($self && $user->uid == $account->uid) ? t('You') : theme('username', $account);
|
| 720 |
}
|
| 721 |
|
| 722 |
/**
|
| 723 |
* theme function for displaying the users activity page
|
| 724 |
*/
|
| 725 |
function theme_activity_block($activities, $more_link = '') {
|
| 726 |
if ($content = theme('item_list', $activities, NULL, 'ul', array('class' => 'activity-list'))) {
|
| 727 |
$content .= $more_link;
|
| 728 |
return $content;
|
| 729 |
}
|
| 730 |
}
|
| 731 |
|
| 732 |
function theme_activity_more_link($path) {
|
| 733 |
return '<div class="more-link">'. l(t('more'), $path, array('title' => t('See all of your activity.'))) .'</div>';
|
| 734 |
}
|
| 735 |
|
| 736 |
function theme_activity($message, $item) {
|
| 737 |
// $item is the unprocessed activity item so that themers can do more with it.
|
| 738 |
return '<span class="activity">'. $message .'</span>';
|
| 739 |
}
|