Index: activity.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/activity/activity.module,v
retrieving revision 1.1.2.2
diff -u -r1.1.2.2 activity.module
--- activity.module 3 Dec 2007 17:40:47 -0000 1.1.2.2
+++ activity.module 12 Dec 2007 14:18:33 -0000
@@ -18,22 +18,32 @@
*/
function activity_menu($may_cache) {
$items = array();
+ global $user;
if ($may_cache) {
$items[] = array(
'path' => 'activity',
- 'title' => t('activity'),
+ 'title' => t('Activity'),
'callback' => 'activity_page',
- 'callback arguments' => array(arg(1)),
'access' => user_access('view activity'),
- 'type' => MENU_CALLBACK,
+ 'weight' => 1,
);
$items[] = array(
- 'path' => 'activity/feed',
+ 'path' => 'activity/all',
+ 'title' => t('All activity'),
+ 'type' => MENU_DEFAULT_LOCAL_TASK,
+ );
+ $items[] = array(
+ 'path' => 'activity/mine',
+ 'title' => t('My activity'),
+ 'access' => $user->uid,
+ 'type' => MENU_LOCAL_TASK,
+ );
+ $items[] = array(
+ 'path' => 'activity/all/feed',
+ 'title' => t('All activity'),
'callback' => 'activity_feed',
- 'callback arguments' => array(arg(2)),
- 'access' => user_access('view activity'),
- 'type' => MENU_CALLBACK,
+ 'callback_arguments' => array('*'),
);
$items[] = array(
'path' => 'admin/settings/activity',
@@ -59,6 +69,17 @@
'type' => MENU_LOCAL_TASK,
);
}
+ else {
+ if ($user->uid) {
+ $items[] = array(
+ 'path' => 'activity/'. $user->uid. '/feed',
+ 'title' => t('My activity'),
+ 'callback' => 'activity_feed',
+ 'callback_arguments' => array($user->uid),
+ 'type' => MENU_CALLBACK,
+ );
+ }
+ }
return $items;
}
@@ -66,8 +87,7 @@
* Admin section
*/
function activity_admin_settings() {
- $modules = activity_get_info();
- if (!empty($modules[0])) {
+ if ($modules = activity_get_info()) {
foreach ($modules as $module) {
if (!empty($module)) {
switch (arg(3)) {
@@ -187,7 +207,7 @@
$aid = db_next_id('activity');
db_query("INSERT INTO {activity} (aid, uid, module, type, action, tokens, timestamp) VALUES (%d, %d, '%s', '%s', '%s', '%s', %d)", $aid, $uid, $module, $type, $action, serialize($tokens), time());
return $aid;
- }
+}
function activity_get_activity($uids = NULL, $filters = NULL, $limit = NULL, $tablesort_headers = NULL) {
// When $uids == *, it is a wildcard meaning return activities of all users.
@@ -200,10 +220,10 @@
// The API supports passing in a single $uid. Wrap it in an array.
if (!is_array($uids)) {
$uids = array($uids);
- }
+ }
$wheres[] = "a.uid IN (%d)";
$params[] = implode(',', $uids);
-}
+ }
// Build sql limiting query to certain modules
if (!empty($filters) && is_array($filters)) {
@@ -269,100 +289,120 @@
elseif ($op == 'view') {
switch ($delta) {
case 'my':
- $activities = activity_get_activity($user->uid);
- return array('title' => t('My activity'),
- 'content' => theme('activity_block', $activities));
+ $activities = activity_get_activity($user->uid, NULL, 5);
+ if (count($activities)) {
+ return array('title' => t('My activity'),
+ 'content' => theme('activity_block', $activities));
+ }
case 'all':
- $activities = activity_get_activity();
- return array('title' => t('Recent activity'),
- 'content' => theme('activity_block', $activities));
+ $activities = activity_get_activity('*', NULL, 5);
+ if (count($activities)) {
+ return array('title' => t('Recent activity'),
+ 'content' => theme('activity_block', $activities));
+ }
}
return $block;
}
}
-/**
- * Menu callback to display the records in a page
- */
-function activity_page($uid) {
- $buddy_activity = activity_buddy_activity($uid);
- arsort($buddy_activity);
- $count = 0;
- if (!empty($buddy_activity)) {
- foreach ($buddy_activity as $ba) {
- if ($count < variable_get('activity_message_limit', 20)) {
- $offset = (strftime("%j") + strftime("%Y") * 365) - (strftime("%j", $ba->timestamp) + strftime("%Y", $ba->timestamp) * 365);
- $timeset = ($offset > 0 && $offset != $prev_offset) ? '
'. activity_format_offset($ba->timestamp) .'
' : '';
- $activity = activity_token_replace($ba);
- if ($activity) {
- $items[] = $timeset . $activity .' '. date('g:ia', $ba->timestamp) .' ';
- }
- $prev_offset = $offset;
- }
- $count++;
- }
- return theme('activity_page', $items);
+function activity_page($page = 'all') {
+ global $user;
+ if ($page == 'mine') {
+ $activities = activity_get_activity(NULL, NULL, 20);
+ $table = activity_table($activities);
+ $feed_url = url('activity/'. $user->uid. '/feed');
+ drupal_add_feed($feed_url);
+ $feed = theme('feed_icon', $feed_url);
+ return theme('activity_page', $activities, $table);
+ }
+ else if ($page == 'all') {
+ $activities = activity_get_activity('*', NULL, 20);
+ $table = activity_table($activities);
+ $feed_url = url('activity/all/feed');
+ drupal_add_feed($feed_url);
+ $feed = theme('feed_icon', $feed_url);
+ return theme('activity_page', $activities, $table);
}
}
-
-/**
- * theme function for displaying the users activity page
- */
-function theme_activity_page($items) {
- return theme('item_list', $items, NULL, 'ul', array('class' => 'activity-list'));
-}
-
/**
- * theme function for displaying the users activity page
+ * Menu callback to display the records in a page
*/
-function theme_activity_block($items) {
- $activites = array();
- foreach ($items as $activity) {
- $activities[] = activity_token_replace($activity);
+function activity_table($activities) {
+ $display_headers = array(
+ 'timestamp' => array('field' => 'timestamp', 'data' => t('Date')),
+ 'uid' => array('field' => 'uid', 'data' => t('User')),
+ 'module' => array('field' => 'module', 'data' => t('Module')),
+ 'type' => array('field' => 'type', 'data' => t('Type')),
+ 'action' => array('field' => 'action', 'data' => t('Action')),
+ t('Message'),
+ );
+ $rows = array();
+ foreach ($activities as $activity) {
+ $user = user_load(array('uid' => $activity['uid']));
+ $rows[] = array(
+ format_date($activity['timestamp'], 'small'),
+ theme('username', $user),
+ $activity['module'],
+ $activty['type'],
+ $activity['action'],
+ activity_token_replace($activity),
+ );
}
- return theme('item_list', $activities, NULL, 'ul', array('class' => 'activity-list'));
+ $output = theme('table', $display_headers, $rows);
+ $output .= theme('pager');
+ return $output;
}
/**
* menu callback to return a feed of a signed in user's activity page
*/
-function activity_feed($user_name) {
- $account = user_load(array('name' => $user_name));
- $buddy_activity = activity_buddy_activity($account->uid);
- drupal_set_header('Content-Type: text/xml; charset=utf-8');
- print theme('activity_feed', $buddy_activity);
- exit;
-}
+function activity_feed($arg) {
+ global $locale;
+ if (is_numeric($arg)) {
+ $user = user_load(array('uid' => $arg));
+ if ($user) {
+ $activities = activity_get_activity(NULL, NULL, 20);
+ $url = url('activity/'. $user->uid);
+ $feed_title = t('Activity for @username', array('@username' => theme('username', $user)));
+ }
+ }
+ else if ($arg == 'all') {
+ $activities = activity_get_activity('*', NULL, 20);
+ $url = url('activity/all');
+ $feed_title = t('All activity');
+ }
+
+ if (count($activities) > 0) {
+ foreach ($activities as $activity) {
+ $function = $activity['module']. '_format_rss_item';
+ if (function_exists($function)) {
+ // each module gets a chance to build its own feed item.
+ // They should use the $activity to prepare variables and
+ // call format_rss_item($title, $link, $item_text, $extra);
+ $items .= $function($activity);
+ }
+ }
+ }
-function theme_activity_feed($buddy_activity) {
- global $base_url;
$channel = array(
'version' => '2.0',
- 'title' => variable_get('site_name', 'drupal') .' - '. variable_get('site_slogan', ''),
- 'link' => $base_url,
+ 'title' => variable_get('site_name', 'Drupal') .' - '. $feed_title,
+ 'link' => $url,
'description' => variable_get('site_mission', ''),
- 'language' => $GLOBALS['locale'],
+ 'language' => $locale
);
- arsort($buddy_activity);
- if (!empty($buddy_activity)) {
- foreach ($buddy_activity as $ba) {
- $item_title = $ba->module;
- $item_tokens = unserialize($ba->tokens);
- $link = l($item_tokens['node-title'], 'node/'. $item_tokens['node-id']);
- $item_text = activity_token_replace($ba);
- $items .= format_rss_item($item->title, $link, $item_text);
- }
- }
-
+ // TODO: Figure out what the right namespace should be.
+ $namespaces = array('xmlns:dc="http://purl.org/dc/elements/1.1/"');
$output = "\n";
- $output .= "\n";
+ $output .= "\n";
$output .= format_rss_channel($channel['title'], $channel['link'], $channel['description'], $items, $channel['language']);
$output .= "\n";
- return $output;
+ drupal_set_header('Content-Type: application/rss+xml; charset=utf-8');
+ print $output;
}
/**
@@ -438,3 +478,23 @@
$tests = file_scan_directory($dir, '\.test$');
return array_keys($tests);
}
+
+
+/**
+ * theme function for displaying the users activity page
+ */
+function theme_activity_page($activities, $table) {
+ return $table;
+ //return theme('item_list', $items, NULL, 'ul', array('class' => 'activity-list'));
+}
+
+/**
+ * theme function for displaying the users activity page
+ */
+function theme_activity_block($items) {
+ $activites = array();
+ foreach ($items as $activity) {
+ $activities[] = activity_token_replace($activity);
+ }
+ return theme('item_list', $activities, NULL, 'ul', array('class' => 'activity-list'));
+}