/[drupal]/contributions/modules/votingapi/votingapi_actions.inc
ViewVC logotype

Contents of /contributions/modules/votingapi/votingapi_actions.inc

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


Revision 1.15 - (show annotations) (download) (as text)
Sat Oct 14 02:01:43 2006 UTC (3 years, 1 month ago) by eaton
Branch: MAIN
CVS Tags: DRUPAL-5--1-2, HEAD
Branch point for: DRUPAL-5
Changes since 1.14: +15 -359 lines
File MIME type: text/x-php
Hooray for Drupal 5. Views integration is no longer inherent to the module; votingapi_views.module will be coming shortly.
1 <?php
2 // $Id: votingapi_actions.inc,v 1.14 2006/06/09 00:20:19 eaton Exp $
3
4 /*********************************************
5 * VOTINGAPI ACTION CONDITION HANDLERS
6 * TO BE USED BY OTHER MODULES AND USERS
7 *********************************************/
8
9 function votingapi_actions_votingapi_action_handlers() {
10 $handlers = array(
11 'votingapi_vote_result_handler' => array(
12 'name' => 'Vote result properties'
13 ),
14 'votingapi_vote_handler' => array(
15 'name' => 'Individual vote properties'
16 ),
17 'votingapi_node_properties_handler' => array(
18 'name' => 'Node properties'
19 ),
20 );
21 return $handlers;
22 }
23
24 function votingapi_vote_result_handler($op, $content, $votes, $results, $rule) {
25 if ($op == 'process') {
26 // for this handler, $rule->data is an array in the following format:
27 //
28 // $value = array(
29 // 'value_type' => 'percent', // an array of 1-n value types.
30 // 'tag' => 'vote', // an array of 1-n tags
31 // 'comparison' = '<', // the comparison operator
32 // 'value' => '90', // the value to be compared
33 // ),
34 // );
35 //
36 // In the example above, any aggregate vote result in which a piece of content receives an
37 // average percentage vote between 75% and 90% would match. Obviously, the specific values
38 // will change based on the specific action. If one of the above values is NOT specified
39 // it will be skipped.
40
41 $data = (object)$rule->data;
42 $passed = FALSE;
43
44 // loop through all the result objects and see if there's one that satisfies all the conditions.
45 foreach ($results as $result) {
46 if (isset($data->value_type)) {
47 if ($result->value_type != $data->value_type) {
48 continue;
49 }
50 }
51
52 if (isset($data->tag)) {
53 if ($result->tag != $data->tag) {
54 continue;
55 }
56 }
57
58 if (isset($data->function)) {
59 if ($result->function != $data->function) {
60 continue;
61 }
62 }
63
64 switch ($data->comparison) {
65 case '<' :
66 if (!($result->value < $data->value)) {
67 continue;
68 }
69 break;
70
71 case '<=' :
72 if (!($result->value <= $data->value)) {
73 continue;
74 }
75 break;
76
77 case '==' :
78 if (!($result->value == $data->value)) {
79 continue;
80 }
81 break;
82
83 case '!=' :
84 if (!($result->value != $data->value)) {
85 continue;
86 }
87 break;
88
89 case '>=' :
90 if (!($result->value >= $data->value)) {
91 continue;
92 }
93
94 break;
95
96 case '>' :
97 if (!($result->value > $data->value)) {
98 continue;
99 }
100 break;
101 }
102
103 // if we get this far, one of the result records has passed successfully.
104 $passed = TRUE;
105 break;
106 }
107
108 return $passed;
109 }
110 else if ($op == 'form') {
111 $form['value_type'] = array(
112 '#type' => 'select',
113 '#options' => votingapi_cache_value_types(),
114 );
115
116 $form['tag'] = array(
117 '#type' => 'select',
118 '#options' => votingapi_cache_tags(),
119 );
120
121 $form['function'] = array(
122 '#type' => 'select',
123 '#options' => votingapi_cache_functions(),
124 );
125 $form['comparison'] = array(
126 '#type' => 'select',
127 '#options' => array('==' => 'Is', '!=' => 'Is not', '<' => 'Is less than', '>' => 'Is greater than'),
128 );
129 $form['value'] = array(
130 '#type' => 'textfield',
131 '#maxlength' => 10,
132 );
133
134 return $form;
135 }
136 else if ($op == 'name') {
137 return array('votingapi_vote_result_handler' => 'Vote result properties');
138 }
139 }
140
141
142 function votingapi_vote_handler($op, $content, $votes, $results, $rule) {
143 if ($op == 'process') {
144 // for this handler, $rule->data is an array in the following format:
145 //
146 // $value = array(
147 // 'value_type' => 'percent', // an array of 1-n value types.
148 // 'tag' => 'vote', // an array of 1-n tags
149 // 'function' => 'average', // a single aggregate function
150 // 'comparison' = '<', // the comparison operator
151 // 'value' => '90', // the value to be compared
152 // ),
153 // );
154 //
155 // In the example above, any aggregate vote result in which a piece of content receives an
156 // average percentage vote between 75% and 90% would match. Obviously, the specific values
157 // will change based on the specific action. If one of the above values is NOT specified
158 // it will be skipped.
159
160 $data = (object)$rule->data;
161 $passed = FALSE;
162
163 // loop through all the result objects and see if there's one that satisfies all the conditions.
164 foreach ($results as $result) {
165 if (isset($data->value_type)) {
166 if ($result->value_type != $data->value_type) {
167 continue;
168 }
169 }
170
171 if (isset($data->tag)) {
172 if ($result->tag != $data->tag) {
173 continue;
174 }
175 }
176
177 switch ($data->comparison) {
178 case '<' :
179 if (!($result->value < $data->value)) {
180 continue;
181 }
182 break;
183
184 case '<=' :
185 if (!($result->value <= $data->value)) {
186 continue;
187 }
188 break;
189
190 case '==' :
191 if (!($result->value == $data->value)) {
192 continue;
193 }
194 break;
195
196 case '!=' :
197 if (!($result->value != $data->value)) {
198 continue;
199 }
200 break;
201
202 case '>=' :
203 if (!($result->value >= $data->value)) {
204 continue;
205 }
206
207 break;
208
209 case '>' :
210 if (!($result->value > $data->value)) {
211 continue;
212 }
213 break;
214 }
215
216 // if we get this far, one of the result records has passed successfully.
217 $passed = TRUE;
218 break;
219 }
220
221 return $passed;
222 }
223 else if ($op == 'form') {
224 $form['value_type'] = array(
225 '#type' => 'select',
226 '#options' => votingapi_vote_value_types(),
227 );
228
229 $form['tag'] = array(
230 '#type' => 'select',
231 '#options' => votingapi_vote_tags(),
232 );
233
234 $form['comparison'] = array(
235 '#type' => 'select',
236 '#options' => array('==' => 'Is', '!=' => 'Is not', '<' => 'Is less than', '>' => 'Is greater than'),
237 );
238 $form['value'] = array(
239 '#type' => 'textfield',
240 '#maxlength' => 10,
241 );
242
243 return $form;
244 }
245 else if ($op == 'name') {
246 return array('votingapi_vote_handler' => 'Individual vote properties');
247 }
248 }
249
250 function votingapi_node_properties_handler($op, $content = NULL, $votes = NULL, $results = NULL, $rule = NULL) {
251 if ($op == 'process') {
252 // for this handler, $rule->data is a keyed array of comaprisons by node property name:
253 //
254 // $rule->data = array(
255 // 'status' => array('==' => 1), // must be published
256 // 'uid' => array('!=' => 1), // not authored by the admin account
257 // );
258 //
259 // The keys in the sub-array are comparison operators, and the values are the value to
260 // compare to. This is mainly useful for ensuring that a node hasn't yet been promoted
261 // before promoting it, etc. At present only == and != are supported by this handler.
262
263 $property = $rule['data']['property'];
264 $comparison = $rule['data']['comparison'];
265 $value = $rule['data']['value'];;
266
267 switch ($comparison) {
268 case '==' :
269 if (!($content->$property == $value)) {
270 return FALSE;
271 }
272 break;
273
274 case '!=' :
275 if (!($content->$property != $value)) {
276 return FALSE;
277 }
278 break;
279 }
280
281 return TRUE;
282 }
283 else if ($op == 'form') {
284 $form['property'] = array(
285 '#type' => 'textfield',
286 '#maxlength' => 10,
287 );
288 $form['comparison'] = array(
289 '#type' => 'select',
290 '#options' => array('==' => 'Is', '!=' => 'Is not'),
291 );
292 $form['value'] = array(
293 '#type' => 'textfield',
294 '#maxlength' => 10,
295 );
296
297 return $form;
298 }
299 else if ($op == 'name') {
300 return array('votingapi_node_properties_handler' => 'Node properties');
301 }
302 }
303
304 /*********************************************
305 * VOTINGAPI IMPLEMENTED ACTIONS. SHOULD
306 * PROBABLY BE ADDED TO ACTIONS.MODULE
307 *********************************************/
308
309 /**
310 * Touches the creation date of a node. Useful for moderated nodes that should appear
311 * 'fresh' as soon as they're promoted.
312 */
313 function action_node_touch_created($op, $edit = array(), &$node) {
314 switch($op) {
315 case 'do':
316 $node->created = time();
317 if (!$edit['defer']) {
318 node_save($node);
319 }
320 watchdog('action', t('Touched creation date of node id %id', array('%id' => intval($node->nid))));
321 break;
322
323 case 'metadata':
324 return array(
325 'description' => t('Touch node creation date'),
326 'type' => t('Node'),
327 'batchable' => true,
328 'configurable' => false,
329 );
330
331 // return an HTML config form for the action
332 case 'form':
333 return '';
334
335 // validate the HTML form
336 case 'validate':
337 return TRUE;
338
339 // process the HTML form to store configuration
340 case 'submit':
341 return '';
342 }
343 }
344
345 /**
346 * Touches the change date of a node. Useful for moderated nodes that should appear
347 * 'fresh' as soon as they're promoted.
348 */
349 function action_node_touch_changed($op, $edit = array(), &$node) {
350 switch($op) {
351 case 'do':
352 $node->changed = time();
353 if (!$edit['defer']) {
354 node_save($node);
355 }
356 watchdog('action', t('Touched change date of node id %id', array('%id' => intval($node->nid))));
357 break;
358
359 case 'metadata':
360 return array(
361 'description' => t('Touch node change date'),
362 'type' => t('Node'),
363 'batchable' => true,
364 'configurable' => false,
365 );
366
367 // return an HTML config form for the action
368 case 'form':
369 return '';
370
371 // validate the HTML form
372 case 'validate':
373 return TRUE;
374
375 // process the HTML form to store configuration
376 case 'submit':
377 return '';
378 }
379 }
380
381 /**
382 * Sets the status of a comment to PUBLISHED.
383 */
384 function action_comment_publish($op, $edit = array(), &$comment) {
385 switch($op) {
386 case 'do':
387 $comment->status = COMMENT_PUBLISHED;
388 comment_save((array)$comment);
389 watchdog('action', t('Set comment id %id to Published', array('%id' => intval($comment->cid))));
390 break;
391
392 case 'metadata':
393 return array(
394 'description' => t('Publish comment'),
395 'type' => t('Comment'),
396 'batchable' => true,
397 'configurable' => false,
398 );
399
400 // return an HTML config form for the action
401 case 'form':
402 return '';
403
404 // validate the HTML form
405 case 'validate':
406 return TRUE;
407
408 // process the HTML form to store configuration
409 case 'submit':
410 return '';
411 }
412 }
413
414 function action_comment_unpublish($op, $edit = array(), &$comment) {
415 switch($op) {
416 case 'do':
417 $comment->status = COMMENT_NOT_PUBLISHED;
418 comment_save((array)$comment);
419 watchdog('action', t('Set comment id %id to Unpublished', array('%id' => intval($comment->cid))));
420 break;
421
422 case 'metadata':
423 return array(
424 'description' => t('Unpublish comment'),
425 'type' => t('Comment'),
426 'batchable' => true,
427 'configurable' => false,
428 );
429
430 // return an HTML config form for the action
431 case 'form':
432 return '';
433
434 // validate the HTML form
435 case 'validate':
436 return TRUE;
437
438 // process the HTML form to store configuration
439 case 'submit':
440 return '';
441 }
442 }

  ViewVC Help
Powered by ViewVC 1.1.2