/[drupal]/contributions/modules/activity_log/activity_log.module
ViewVC logotype

Contents of /contributions/modules/activity_log/activity_log.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.8 - (show annotations) (download) (as text)
Sat Sep 27 05:23:07 2008 UTC (14 months ago) by mercmobily
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +2 -2 lines
File MIME type: text/x-php
Wooops debug message taken out
1 <?php
2 // $Id: activity_log.module,v 1.7 2008/09/24 09:24:26 mercmobily Exp $
3
4
5 /**
6 * Implementation of hook_views_api()
7 *
8 * This hook will tell "views" that this module interfaces with it
9 */
10 function activity_log_views_api() {
11
12 return array(
13 'api' => 2,
14 'path' => drupal_get_path('module', 'activity_log'),
15 );
16 }
17
18
19 /**
20 * Implementation of hook_menu()
21 *
22 */
23 function activity_log_menu() {
24 $items = array();
25 global $user;
26
27 // Basic settings
28
29 # This was not needed in the end. Leaving it here in case
30 # I will need it later.
31 $items['admin/settings/activity_log'] = array(
32 'title' => 'Activity Log module',
33 'description' => 'Settings for the Activity Log module',
34 'page callback' => 'drupal_get_form',
35 'page arguments' => array('activity_log_admin_settings'),
36 'access callback' => 'user_access',
37 'access arguments' => array('administer site configuration'),
38 'file' => 'activity_log.admin.inc',
39 'type' => MENU_NORMAL_ITEM,
40 );
41
42
43 return $items;
44 }
45
46
47
48 ####################################################################
49 # NON-GLUE CODE. This code is for people who don't use Views.
50 # It's also intended as a testbed for people trying this out
51 # while VIEWS support gets improved/implemented
52 # In a perfect world, where views integration is complete and
53 # FULLY functional, the following code is useless. HOWEVER,
54 # it might take a while
55 ####################################################################
56
57
58
59 /**
60 * This function returns a $query object which will be ready to be
61 * used to look into the activity_log table.
62 * To make things sane, there is always a "center of attention" here: a central
63 * "uid". The next couple of flags will specify if the filter will be done
64 * considering $uid the *creator* of the action, or the *target* of the
65 * action.
66 * There can also be a $friendlist_params, which specify which friend management
67 * module is being used, and which parameters to pass it. The right hook
68 * will then hopefully enrich the query so that filtering is added on a
69 * friendship basis.
70 * It's important to remember that this is an *OR* based query. So:
71 * - If none of the parameters are set, then the query will return EVERY
72 * activity line (no filtering)
73 * - If $uid and $search_as_creator are specified, then the query will return
74 * entries where $uid was the creator
75 * - If $uid and $search_as_creator AND $search_as_target are specified, then
76 * the query wil return entries with $uid and creator OR target
77 * - $friendlist_param is _also_ OR'ed - which means that it will _add_ to
78 * the results
79 *
80 * @param
81 * $uid - The "center of attention"
82 * $search_as_creator - If TRUE, entries where $uid was the creator
83 * will be shown
84 * $search_as_target - If true, entries where $uid was the target will
85 * be shown.
86 * $limit - Limit the query to N records
87 * $friendlist_params - An associative array of parameters to be passed to
88 * the hook hook_activity_log_conditions(). For example:
89 * array('module' => 'friendlist_api', 'status' => 'TW_BOTH')
90 *
91 * @return
92 * $query_result object, to which you can do db_fetch_array (for example)
93 */
94 function activity_log_query($uid, $search_as_creator, $search_as_target, $limit=NULL, $friendlist_params=NULL ) {
95
96 // Just to make sure that we have just IDs and nothing else
97 $uid = activity_log_object_get_id('user', $uid);
98
99 // Set the basic arrays
100 $where = array();
101 $join = array();
102
103
104 // Filter on the required criteria
105 if($search_as_creator){
106 $where[] = "al.uid_creator = $uid";
107 }
108 if($search_as_target){
109 $where[] = "alt.uid_target = $uid";
110 }
111
112
113 // Get only the messages from the list of friends
114 if($friendlist_params){
115 $function=$friendlist_params['module']."_activity_log_conditions";
116 $function($uid, $friendlist_params,$where,$join);
117 }
118
119
120 // If nothing was set as a filter, it will return all of the records
121 if( count($where) == 0 ){
122 $where[] = '1=1';
123 }
124
125 $where = implode(' OR ', $where);
126 $join = implode(' ', $join);
127
128 if($limit){
129 $limit_string="LIMIT $limit";
130 }
131
132 $query="SELECT * FROM {activity_log} al LEFT JOIN activity_log_targets alt ON al.aid = alt.aid $join WHERE $where GROUP BY al.aid ORDER BY al.activity_timestamp DESC $limit_string";
133 #drupal_set_message("Query: $query");
134
135 return db_query($query, $parm);
136 }
137
138
139 /**
140 * This function gathers the information in an array in the format "liked"
141 * by the API functions.
142 *
143 * @param
144 * $result - A query result object
145 * $limit - How many entries will be added. This is important in case the
146 * query returned a zillion records
147 *
148 * @return
149 * $data, which is the information from the query in a nice
150 * array format.
151 * The format is simple: one array row per activity_log result, PLUS an extra
152 * "targets" keys with an array row per activity_log_targets results.
153 * Each activity_log key also has a 'multiple_flag' key which tells
154 * you that the row has more than one results.
155 *
156 */
157 function activity_log_gather_data($result, $limit){
158
159 $data = array();
160
161 // Get the unfo
162 $c=0;
163 while($row=db_fetch_array($result)){
164 $data[$c] = $row;
165
166 // For each row, the 'oid_targets' index contains the list
167 // of targets. For single fields, there is simply only one. By
168 // default, the multiple_flag is FALSE
169 $result_targets=db_query("SELECT * FROM {activity_log_targets} alt WHERE alt.aid = %d",$row['aid']);
170
171 $how_many_targets=0;;
172 while($row_targets=db_fetch_array($result_targets)){
173 $data[$c]['targets'][] = array(
174 'uid_target' => $row_targets['uid_target'],
175 'oid_target' => $row_targets['oid_target'],
176 'oid_target_type' => $row_targets['oid_target_type']
177 );
178 $how_many_targets++;
179 }
180 $data[$c]['multiple_flag'] = $how_many_targets == 1 ? FALSE : TRUE;
181
182 // Don't go over the limit. this is to prevent results with
183 // 3384243 records from killing the host
184 if ( $c++ >= $limit-1 ){
185 break;
186 }
187 }
188
189 return $data;
190
191 }
192
193
194 /**
195 * Utulity function to load the an object
196 *
197 * @param
198 * $type - it can be "comment", "node" or "user"
199 * $oid - the object's ID
200
201 *
202 * @return
203 * The loaded object after calling the right drupal _load function
204 */
205 function activity_log_object_load($type,$oid){
206
207 // It's probably already an object
208 if (is_object($oid)){
209 return $oid;
210 }
211
212 if ($type == 'user') {
213 return user_load( array('uid' => $oid) );
214 }
215 if ($type == 'node') {
216 return node_load( $oid );
217 }
218 if ($type == 'comment') {
219 return _comment_load( $oid );
220 }
221 }
222
223
224 /**
225 * Utulity function to get an object's ID
226 *
227 * @param
228 * $type - it can be "comment", "node" or "user"
229 * $object - the object's
230
231 *
232 * @return
233 * The right ID depending on the object type
234 */
235 function activity_log_object_get_id($type, $object){
236
237 // It's probably ALREADY an ID
238 if (!is_object($object) ){
239 return $object;
240 }
241
242 if ($type == 'user') {
243 return $object->uid;
244 }
245 if ($type == 'node') {
246 return $object->nid;
247 }
248 if ($type == 'comment') {
249 return $object->cid;
250 }
251
252 }
253
254
255 /**
256 * Implementation of hook_activity_log_conditions()
257 *
258 * THIS FUNCTION IS MEANT TO LIVE IN THE "friendlist" MODULE! IT'S ONLY
259 * HERE BECAuSE THIS _IS_ A TESTBED AFTER ALL.
260 *
261 * This hook will enrich $where and $join based on how $friendlist_params will
262 * filter things to show people with a relation with $uid
263 */
264 function friendlist_api_activity_log_conditions($uid, $friendlist_params, &$where, &$join){
265
266 $status = $friendlist_params['status'];
267 $rtid = $friendlist_params['rtid'];
268
269 // Only add the conditions if they make sense
270 if($status != ''){
271 $status_condition = "AND fs1.status = '$status'";
272 }
273 if($rtid != ''){
274 $rtid_condition = "AND fs1.rtid = $rtid";
275 }
276
277 $join[] = "LEFT JOIN friendlist_statuses fs1 ON al.uid_creator = fs1.requester_id";
278 #fs1.requestee_id = $uid OR fs1.requester_id = $uid AND
279 $where[] = "(
280 fs1.requestee_id = $uid
281 $status_condition
282 $rtid_condition
283 ) ";
284
285 }
286
287
288
289 ####################################################################
290 # THEME oriented functions
291 ####################################################################
292
293 /**
294 * This function is a simple helper which will call the right
295 * theme function according to the object type
296 *
297 * @param
298 * $type - it can be 'node', 'comment' or 'user'
299 * $object - the object to be rendered
300 *
301 * @return
302 * The rendered object, rendered with the "right" theme function
303 */
304 function theme_activity_log_object_render($type,$object){
305
306 if ($type == 'user') {
307 return theme('activity_log_user',$object);
308 }
309 if ($type == 'node') {
310 return theme('activity_log_node',$object);
311 }
312 if ($type == 'comment') {
313 return theme('activity_log_comment',$object);
314 }
315
316 }
317
318
319 /**
320 * Implementation of theme_activity_log_user()
321 *
322 * This function will return the formatted node. Normally just the user's name
323 */
324 function theme_activity_log_user($account){
325 global $user;
326
327 if($user->uid == $account->uid){
328 return t('You');
329 } else {
330 return theme('username',$account);
331 }
332 }
333
334 /**
335 * Implementation of theme_activity_log_node()
336 *
337 * This function will return the formatted node. Normally just the title.
338 */
339 function theme_activity_log_node($node){
340 return l($node->title,'node/'.$node->nid);
341 }
342
343 /**
344 * Implementation of theme_activity_log_comment()
345 *
346 * This function will return the formatted comment. Normally just the title
347 */
348 function theme_activity_log_comment($comment){
349 return l($comment->title,"node/$comment->nid", NULL, NULL, "comment-{$comment->cid}");
350 }
351
352 /**
353 * Implementation of theme_activity_log_line()
354 *
355 * This function will return the formatted line
356 */
357 function theme_activity_log_line($line){
358
359 global $user;
360
361 $result='';
362
363 $u = activity_log_object_load('user', $line['uid_creator']);
364
365 // If the UID is the same as the viewing user, use the "you" form
366 // which is nicer
367 if ($u->uid == $user->uid){
368 $verb = t($line['verb_you']);
369 } else {
370 $verb = t($line['verb']);
371 }
372
373 // If there are multuple targets, use the action_multiple
374 // form, which will probably work best
375 if ($line['multiple_flag'] ){
376 $action = t($line['action_multiple']);
377 } else {
378 $action = t($line['action']);
379 }
380
381 $result .= $line['aid'] .
382 ' - ' .
383 theme_activity_log_object_render('user',$u) .
384 ' ' .
385 $verb .
386 ' ' .
387 $action .
388 ' ' .
389 theme(
390 'activity_log_targets',
391 $line['targets'],
392 $line['action_multiple_separator']
393 );
394
395 return $result;
396
397 }
398
399 /**
400 * Implementation of theme_activity_log_targets()
401 *
402 * This function will return the formatted list of targets
403 */
404 function theme_activity_log_targets($targets, $separator){
405
406 $result = '';
407
408 foreach($targets as $target){
409
410 $target_object = activity_log_object_load(
411 $target['oid_target_type'],
412 $target['oid_target']
413 );
414
415 $result .= theme_activity_log_object_render(
416 $target['oid_target_type'] ,
417 $target_object
418 );
419
420 $result .= t($separator);
421
422 }
423
424 // Easy way of taking the last separator out
425 $result = substr($result,0,-strlen($separator) );
426
427 return $result;
428 }
429
430 /**
431 * Implementation of theme_activity_log_list()
432 *
433 * This function will return the whole list
434 */
435 function theme_activity_log_list($list){
436
437 // Cycle through the results
438 foreach($list as $line){
439 $rendered_lines[] = theme('activity_log_line', $line);
440 }
441
442 // Display them
443 $result .= theme('item_list',
444 $rendered_lines,
445 NULL,
446 'ul',
447 array('class' => 'activity-log-list')
448 );
449
450 return $result;
451
452 }
453
454 /**
455 * Implementation of hook_theme().
456 */
457 function activity_log_theme() {
458
459
460 return array(
461
462 'activity_log_object_render' => array(
463 'arguments' => array(
464 'type' => NULL,
465 'object' => NULL,
466 ),
467 ),
468 'activity_log_user' => array(
469 'arguments' => array(
470 'user' => NULL,
471 ),
472 ),
473 'activity_log_node' => array(
474 'arguments' => array(
475 'node' => NULL,
476 ),
477 ),
478 'activity_log_comment' => array(
479 'arguments' => array(
480 'comment' => NULL,
481 ),
482 ),
483
484 'activity_log_line' => array(
485 'arguments' => array(
486 'line' => NULL,
487 ),
488 ),
489 'activity_log_targets' => array(
490 'arguments' => array(
491 'targets' => NULL,
492 ),
493 ),
494 'activity_log_list' => array(
495 'arguments' => array(
496 'list' => NULL,
497 ),
498 ),
499
500 );
501
502 }
503
504
505
506 /**
507 * Implementation of hook_block()
508 */
509 function activity_log_block($op = 'list', $i = 0, $edit = array()) {
510
511 switch ($op) {
512 case 'list':
513 for($index=0;$index<4;$index++){
514 $blocks[$index]['info'] = t('Activity block !index', array('!index' => $index) );
515 }
516 return $blocks;
517
518 case 'configure':
519
520 $form["activity_log_user_$i"] = array(
521 '#type' => 'select',
522 '#title' => t("User to match with"),
523 '#description' => t("User to match with"),
524 '#required' => TRUE,
525 '#options' => array(
526 'logged_in'=>'Logged in user',
527 'from_profile' => 'User whose profile is being seen',
528 'none' => 'None'
529 ),
530
531 '#default_value'=>variable_get("activity_log_user_$i", 'none')
532 );
533
534 $form["activity_log_user_creator_$i"] = array(
535 '#type' => 'checkbox',
536 '#title' => t("Chose activity entries where the user above is the creator"),
537 '#required' => FALSE,
538 '#default_value' => variable_get("activity_log_user_creator_$i", FALSE),
539 );
540 $form["activity_log_user_target_$i"] = array(
541 '#type' => 'checkbox',
542 '#title' => t("Chose activity entries where the user above is the target"),
543 '#required' => FALSE,
544 '#default_value'=> variable_get("activity_log_user_target_$i", FALSE),
545 );
546 $form["activity_log_limit_$i"] = array(
547 '#type' => 'textfield',
548 '#title' => t("How many entries will be shown"),
549 '#required' => FALSE,
550 '#default_value'=> variable_get("activity_log_limit_$i", FALSE),
551 );
552 $form["activity_log_friendlist_array_$i"] = array(
553 '#type' => 'textarea',
554 '#title' => t("The array to be passed to the Relation module to decide if a user is a friend"),
555 '#description' => t("An example of this could be: <c>return array('module' => 'friendlist_api', 'status' => 'TW_BOTH')</c> "),
556 '#required' => FALSE,
557 '#default_value'=> variable_get("activity_log_friendlist_array_$i", FALSE),
558 );
559
560 return $form;
561 break;
562
563 case 'save':
564
565 variable_set("activity_log_user_$i", $edit["activity_log_user_$i"]);
566 variable_set("activity_log_user_creator_$i", $edit["activity_log_user_creator_$i"]);
567 variable_set("activity_log_user_target_$i", $edit["activity_log_user_target_$i"]);
568 variable_set("activity_log_limit_$i", $edit["activity_log_limit_$i"]);
569 variable_set("activity_log_friendlist_array_$i", $edit["activity_log_friendlist_array_$i"]);
570
571 break;
572
573 case 'view':
574 global $user;
575
576 if (user_access('access content')) {
577
578 // Easy one: User Parameter is "none".
579 $user_config=variable_get("activity_log_user_$i", 'none');
580 if($user_config == 'none'){
581 $user_param=NULL;
582 }
583
584 // The user to filter by needs to be the logged in user
585 if($user_config == 'logged_in'){
586 if($user->uid == 0){
587 $block['content'] = '';
588 $block['subject'] = '';
589 return array('content' => '', 'subject' => '');
590 }
591 $user_param=$user->uid;
592 }
593
594 // The user to filter by needs to be the current profile's
595 if($user_config == 'from_profile'){
596 if(arg(0) != 'user' || ! is_numeric(arg(1) ) ){
597 return array('content' => '', 'subject' => '');
598 }
599 $user_param=(int)arg(1);
600 }
601
602
603 // Other, easy parameters
604 $creator = variable_get("activity_log_user_creator_$i", FALSE);
605 $target = variable_get("activity_log_user_target_$i", FALSE);
606 $limit = variable_get("activity_log_limit_$i", 10);
607
608 $to_eval = variable_get("activity_log_friendlist_array_$i", '' );
609
610 if($to_eval != ''){
611 $parameters=eval($to_eval);
612 } else {
613 $parameters=NULL;
614 }
615
616 // Render the results
617 $r=activity_log_query($user_param, $creator, $target, $limit, $parameters);
618 $data = activity_log_gather_data($r,$limit);
619 if(count($data) == 0){
620 #drupal_set_message("Empty!");
621 return array('content' => '', 'subject' => '');
622 }
623 $list = theme('activity_log_list', $data);
624
625 // Return the block!
626 $block['content'] = $list;
627 $block['subject'] = '';
628 return $block;
629 }
630 // } End of switch
631 }
632
633 }
634
635

  ViewVC Help
Powered by ViewVC 1.1.2