Activity Module came about as a result of a need to show what your friends were doing on a site, something like what facebook does with your friends' news feed. The original idea came from the mind of quicksketch, coded by myself (with much help from quicksketch) then improved upon (5.x.3 branch) by robertDouglass.
I was asked to add a feature the other day that could have activity update in a fashion similar to the user reviews on the community tab of Netflix.com where you can just watch the list update all day. To accomplish this I first had to do two things, create a way to get activity in JSON format and then find some JavaScript to display it with an Ajax call. (AjaJ?)
So how does activity provide JSON data in order to do all this fun fancy stuff? Well, first it provides a path in the hook_menu(). There is a path for all activity ('activity/all/json') and then there is one for a particular user's activity ('activity/'. $user->uid .'/json').
<?php
/**
* Implementation of hook_menu().
*/
function activity_menu($may_cache) {
...
$items[] = array(
'path' => 'activity/all/json',
'callback' => 'activity_json',
'callback arguments' => array(ACTIVITY_ALL, 1),
'access' => user_access('view public activity'),
);
...
$items[] = array(
'path' => 'activity/'. $user->uid. '/json',
'callback' => 'activity_json',
'callback arguments' => array($user->uid, 1),
'type' => MENU_CALLBACK,
);
...
}
?><?php
/**
* output our activity as json
* $arg[0] = ACTIVITY_ALL or $uid
* $arg[1] = number of activities to retreive
*/
function activity_json($arg) {
global $locale;
$args = func_get_args();
if ($args[0] == ACTIVITY_ALL) {
// get the latest activity posted
$activities = activity_get_activity(ACTIVITY_ALL, NULL, $args[1]);
$url = url('activity/all', NULL, NULL, TRUE);
$feed_title = t('All activity');
}
else if (is_numeric($args[0])) {
$user = db_fetch_object(db_query('SELECT uid, name FROM {users} WHERE uid = %d', $args[0]));
if ($user) {
// get the latest activity posted pertaining to this user
$activities = activity_get_activity($arg[0], NULL, $args[1]);
$url = url('activity/'. $user->uid, NULL, NULL, TRUE);
$feed_title = t('Activity for @username', array('@username' => theme('username', $user)));
}
}
if (count($activities) > 0) {
foreach ($activities as $activity) {
$message = activity_token_replace($activity);
$items .= '<item><date>'. format_date($activity['created'], 'small') .'</date>';
$items .= '<url>'. url('activity/'. $user->uid, NULL, NULL, TRUE) .'</url>';
$items .= '<message>'. $message .'</message></item>';
}
}
drupal_set_header('Content-Type: application/x-javascript');
print drupal_to_js($items);
die();
}
?>I'd like to incorporate this as the default activity view, but need to get some feedback on it, which is the reason for this whole article in the first place. Until then, I stuck this on the theme layer.
<?php
function phptemplate_activity_page($activities, $table) {
// include the spy.js file
drupal_add_js(drupal_get_path('theme', 'garland') .'/js/spy.js', 'theme');
// do a little js voodoo, set a few settings, having a good ol' time
drupal_add_js("
$(document).ready(function() {
$('table tbody').spy({
'ajax': '/activity/all/json',
'push': custom_push,
'method': 'json',
'limit': 1,
'timeout': 7500,
});
});
function custom_push(response) {
eval(\"var json = \" + response); // convert to JSON
// Build the HTML
var html = '<tr class=\"even\"><td>' + json.date + '</td><td>' + json.message + '</td></tr>';
// Prepend the HTML inside the container
$('table tbody').prepend(html);
}",
'inline');
return $table;
}
?>Take a look at the issue here.
Tasks for Improving:
- convert viewsbookmarkactivity to new api - task
- convert votingapiactivity to new api - task
- upgrade path from 5.x.2
- this is currently about half done. check here.
- idea about the UI (build your choice of contrib's implementation)
- this is an idea I had the other night about creating a UI for activity that will allow anyone to implement a [module]activity of their own. Something like Views UI that will make so a user does not have to now how to write a definition/contrib module for activity, but hold it entirely in the database or a array definition. Needless to say I'll be studying a lot of merlinofchaos's code for this ;) I'll write more on this as it formulates in my brain.
Comments
Thanks for Sharing
Appreciate you sharing these tips and the plugin.
Thanks
Steve
Thanks!
Thanks!
Thanks
This is great! Thanks for sharing....
Post new comment