/[drupal]/contributions/modules/activity_log/activity_log.rules.inc
ViewVC logotype

Contents of /contributions/modules/activity_log/activity_log.rules.inc

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


Revision 1.4 - (show annotations) (download) (as text)
Wed Sep 24 08:57:20 2008 UTC (14 months ago) by mercmobily
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +60 -18 lines
File MIME type: text/x-php
First version UP
1 <?php
2
3
4 #######################################################################
5 # ACTIONS
6 #######################################################################
7
8 /**
9 * Implementation of hook_rules_action_info().
10 * @ingroup rules
11 */
12 function activity_log_rules_action_info() {
13 return array(
14
15 'rules_action_activity_log' => array(
16 'label' => t('Log an activity'),
17 'eval input' => array(
18 'uid_creator',
19
20 'verb',
21 'verb_you',
22
23 'action',
24 'action_multiple',
25 'action_multiple_separator',
26
27 'uid_target',
28 'oid_target_type',
29 'oid_target',
30 ),
31
32 'help' => 'Logs a social networking message',
33 'module' => 'Activity Log',
34 ),
35
36 );
37 }
38
39 /**
40 * Action drupal message configuration form.
41 */
42 function rules_action_activity_log_form($settings, &$form) {
43
44 $form['settings']['uid_creator'] = array(
45 '#type' => 'textfield',
46 '#size' => '15',
47 '#maxsize' => '25',
48 '#title' => t('User who created the activity'),
49 '#default_value' => $settings['uid_creator'],
50 '#description' => t('The user doing the activity'),
51 );
52
53 $form['settings']['verb'] = array(
54 '#type' => 'textfield',
55 '#size' => '35',
56 '#maxsize' => '35',
57 '#title' => t('The verb of the activity'),
58 '#default_value' => $settings['verb'],
59 '#description' => t('This will normally be placed after the user name'),
60 );
61 $form['settings']['verb_you'] = array(
62 '#type' => 'textfield',
63 '#size' => '35',
64 '#maxsize' => '35',
65 '#title' => t('The verb of the action when the "you" formed is being used'),
66 '#default_value' => $settings['verb_you'],
67 '#description' => t('This will normally be placed after the user name'),
68 );
69
70
71 $form['settings']['action'] = array(
72 '#type' => 'textfield',
73 '#size' => '255',
74 '#maxsize' => '255',
75 '#title' => t('The action string'),
76 '#default_value' => $settings['action'],
77 '#description' => t('This will be placed between the user id and the target'),
78 );
79 $form['settings']['action_multiple'] = array(
80 '#type' => 'textfield',
81 '#size' => '255',
82 '#maxsize' => '255',
83 '#title' => t('The action string, multiple format'),
84 '#default_value' => $settings['action_multiple'],
85 '#description' => t('Just like the action string, but used when there are multuple items'),
86 );
87 $form['settings']['action_multiple_separator'] = array(
88 '#type' => 'textfield',
89 '#size' => '25',
90 '#maxsize' => '25',
91 '#title' => t('Separator to use when there are multiple actions'),
92 '#default_value' => $settings['action_multiple_separator'],
93 '#description' => t('String to be used when multiple actions are put together'),
94 );
95
96 $form['settings']['uid_target'] = array(
97 '#type' => 'textfield',
98 '#size' => '15',
99 '#maxsize' => '25',
100 '#title' => t('User who was target of the action'),
101 '#default_value' => $settings['uid_target'],
102 '#description' => t('The user target of the activity'),
103 );
104
105
106 $options=array('node' => 'node','comment' => 'comment','user' => 'user');
107 $form['settings']['oid_target_type'] = array(
108 '#type' => 'select',
109 '#options' => $options,
110 '#title' => t('The type of the object that received the action'),
111 '#default_value' => $settings['oid_type'],
112 '#description' => t('The type of the object target of the action'),
113 );
114 $form['settings']['oid_target'] = array(
115 '#type' => 'textfield',
116 '#size' => '15',
117 '#maxsize' => '25',
118 '#title' => t('Object that received the activity'),
119 '#default_value' => $settings['oid_target'],
120 '#description' => t('The object target of the activity'),
121 );
122 }
123
124
125 /**
126 * Action Implementation: Log an activity
127 *
128 * Note that this function is tricky!!!
129 * 1) The same activity is not logged if it has happened within the last
130 * 60 minutes. Fields checked:
131 * 'uid_creator', 'verb', 'action', 'uid_target', 'oid_target_type',
132 * 'oid_target'
133 *
134 * 2) If an activity exists already after checking:
135 * 'uid_creator', 'verb', 'action'
136 * AND the activity is less than 1 hour old, AND the activity has less than
137 * 5 targets, then a new activity is NOT created. Instead, targets are
138 * added to the found action
139 *
140 * This is to ensure that 1) The same activity isn't logged too many times if
141 * something goes silly 2) Things are grouped properly
142 *
143 */
144 function rules_action_activity_log($settings){
145
146 // Never ever log anonymous' actions. It would be a phenomenal amount
147 // of clutter
148 if ($settings['uid_creator'] == 0 ){
149 return;
150 }
151
152 // Check if an identical message -- with identical everything -- was logged
153 // within the last hour
154 $r=db_result(db_query("SELECT COUNT(*) FROM activity_log al LEFT JOIN activity_log_targets alt ON al.aid = alt.aid WHERE al.uid_creator=%d AND al.verb='%s' AND al.action='%s' AND alt.uid_target=%d AND alt.oid_target_type='%s' AND alt.oid_target=%d AND %d - al.activity_timestamp < %d ORDER BY activity_timestamp DESC", $settings['uid_creator'], $settings['verb'], $settings['action'], $settings['uid_target'], $settings['oid_target_type'], $settings['oid_target'], time(), variable_get('activity_log_grouping_seconds',3600) ) );
155 if($r){
156 #drupal_set_message("NOPE: $r");
157 return;
158 }
159
160 #drupal_set_message("HERE: $r");
161
162 // Count how many times the user has done the same action, although on
163 // different nids
164
165 $r=db_fetch_object(db_query("SELECT COUNT(*) as count, al.aid FROM activity_log al LEFT JOIN activity_log_targets alt ON al.aid = alt.aid WHERE al.uid_creator=%d AND al.verb='%s' AND al.action='%s' AND alt.oid_target_type='%s' AND %d - al.activity_timestamp < %d GROUP BY alt.aid ORDER BY al.aid DESC", $settings['uid_creator'], $settings['verb'], $settings['action'], $settings['oid_target_type'], time() , variable_get('activity_log_grouping_seconds',3600) ) );
166
167
168
169 $count = $r->count;
170 $aid = $r->aid;
171 #drupal_set_message("READ COUNT: $count $aid ");
172
173
174 // If there are too many entries already, then create a NEW "master" one
175 // and then make sure that the "child" is added wit the right $aid
176
177 #drupal_set_message("Comparing $count with ". variable_get('activity_log_grouping_how_many',5) );
178
179 if($count >= variable_get('activity_log_grouping_how_many',5) || $count == 0 ){
180 db_query("INSERT INTO activity_log SET uid_creator=%d, verb='%s', verb_you='%s', action='%s', action_multiple='%s', action_multiple_separator='%s', activity_timestamp=%d", $settings['uid_creator'], $settings['verb'], $settings['verb_you'], $settings['action'], $settings['action_multiple'], $settings['action_multiple_separator'], time() );
181 $aid = db_last_insert_id('activity_log','aid');
182 }
183
184 db_query("INSERT INTO activity_log_targets SET aid = %d, uid_target=%d, oid_target_type='%s', oid_target=%d, target_timestamp=%d", $aid, $settings['uid_target'], $settings['oid_target_type'], $settings['oid_target'], time() );
185
186 }
187
188
189 ##################################################################
190 # TOKENS
191 ##################################################################
192
193
194 /**
195 * Implementation of hook_token_list().
196 */
197 function activity_log_token_list($type = 'all') {
198
199 if ($type == 'node') {
200 $tokens['node']['title-link'] = t("The node's title with a link to it");
201 }
202 if ($type == 'user') {
203 $tokens['user']['user-name-url'] = t("The user's name with a link to it");
204 }
205
206 return $tokens;
207 }
208
209
210 /**
211 * Implementation of hook_token_values().
212 */
213 function activity_log_token_values($type, $object = NULL, $options = array()) {
214 $values = array();
215 switch ($type) {
216
217 case 'node':
218 $values['title-link'] = l($object->title,'node/'.$object->nid);
219 break;
220
221 case 'user':
222 $values['user-name-url'] = l($object->name,'user/'.$object->uid);
223 break;
224
225 }
226 return $values;
227 }
228

  ViewVC Help
Powered by ViewVC 1.1.2