Révision fcc9430f
Ajouté par Assos Assos il y a environ 6 ans
drupal7/sites/all/modules/votingapi/votingapi.module | ||
---|---|---|
8 | 8 |
* data. Modules can cast votes with arbitrary properties and VotingAPI will |
9 | 9 |
* total them automatically. Support for basic anonymous voting by IP address, |
10 | 10 |
* multi-criteria voting, arbitrary aggregation functions, etc. |
11 |
* |
|
12 |
* /** |
|
13 |
* Implementation of hook_help(). |
|
11 | 14 |
*/ |
15 |
function votingapi_help($path, $arg) { |
|
16 |
switch ($path) { |
|
17 |
case 'admin/help#votingapi': |
|
18 |
$output = ''; |
|
19 |
$text = 'module helps developers who want to use a standardized API'; |
|
20 |
$text .= ' and schema for storing, retrieving, and tabulating votes for'; |
|
21 |
$text .= ' Drupal content. Note that this module does NOT directly'; |
|
22 |
$text .= ' expose any voting mechanisms to end users. It is a framework'; |
|
23 |
$text .= ' designed to make life easier for other developers, and to'; |
|
24 |
$text .= ' standardize voting data for consumption by other modules'; |
|
25 |
$text .= ' (like Views). For more information, see the online'; |
|
26 |
$text .= ' documentation for the '; |
|
27 |
$output .= '<h3>' . t('About') . '</h3>'; |
|
28 |
$output .= '<p>' . t('The <a href="@votingapi">VotingAPI</a> ' . $text . ' <a href="@doco">VotingAPI module</a>.', |
|
29 |
array( |
|
30 |
'@votingapi' => 'http://drupal.org/project/votingapi', |
|
31 |
'@doco' => 'https://www.drupal.org/node/68851', |
|
32 |
)) . '</p>'; |
|
33 |
$output .= '<h3>' . t('Uses') . '</h3>'; |
|
34 |
$output .= '<dl>'; |
|
35 |
$output .= '<dt>' . t('General') . '</dt>'; |
|
36 |
$output .= '<dd>' . t('VotingAPI is used to allow module developers to focus on their ideas (say, providing a "rate this thread" widget at the bottom of each forum post) without worrying about the grunt work of storing votes, preventing ballot-stuffing, calculating the results, and so on.') . '</dd>'; |
|
37 |
$output .= '<dt>' . t('Basic Concepts and Features ') . '</dt>'; |
|
38 |
$output .= '<dd>' . t('VotingAPI does four key jobs for module developers: ') . '</dd>'; |
|
39 |
$output .= '<dd>' . t('1. CRUD: Create/Retrieve/Update/Delete operations for voting data. ') . '</dd>'; |
|
40 |
$output .= '<dd>' . t('2. Calculation: Every time a user casts a vote, VotingAPI calculates the results and caches them for you. ') . '</dd>'; |
|
41 |
$output .= '<dd>' . t('3. Workflow: By integrating with Actions.module, VotingAPI can trigger workflow steps (like promoting a node to the front page, hiding a comment thathas been flagged as spam, or emailing an administrator) when votes are cast and results are tallied. ') . '</dd>'; |
|
42 |
$output .= '<dd>' . t('4. Display: VotingAPI integrates with Views.module, allowing you to slice and dice the content of your site based on user consensus. ') . '</dd>'; |
|
43 |
return $output; |
|
44 |
} |
|
45 |
} |
|
12 | 46 |
|
13 | 47 |
/** |
14 | 48 |
* Implements of hook_menu(). |
... | ... | |
23 | 57 |
'access callback' => 'user_access', |
24 | 58 |
'access arguments' => array('administer voting api'), |
25 | 59 |
'file' => 'votingapi.admin.inc', |
26 |
'type' => MENU_NORMAL_ITEM |
|
60 |
'type' => MENU_NORMAL_ITEM,
|
|
27 | 61 |
); |
28 | 62 |
|
29 | 63 |
if (module_exists('devel_generate')) { |
... | ... | |
45 | 79 |
*/ |
46 | 80 |
function votingapi_permission() { |
47 | 81 |
return array( |
48 |
'administer voting api' => array(
|
|
49 |
'title' => t('Administer Voting API'),
|
|
50 |
),
|
|
82 |
'administer voting api' => array( |
|
83 |
'title' => t('Administer Voting API'), |
|
84 |
), |
|
51 | 85 |
); |
52 | 86 |
} |
53 | 87 |
|
... | ... | |
92 | 126 |
* Modules that need more explicit control can call votingapi_add_votes() and |
93 | 127 |
* manage the deletion/recalculation tasks manually. |
94 | 128 |
* |
95 |
* @param $votes |
|
129 |
* @param array $votes
|
|
96 | 130 |
* An array of votes, each with the following structure: |
97 | 131 |
* $vote['entity_type'] (Optional, defaults to 'node') |
98 | 132 |
* $vote['entity_id'] (Required) |
... | ... | |
102 | 136 |
* $vote['uid'] (Optional, defaults to current user) |
103 | 137 |
* $vote['vote_source'] (Optional, defaults to current IP) |
104 | 138 |
* $vote['timestamp'] (Optional, defaults to REQUEST_TIME) |
105 |
* @param $criteria |
|
139 |
* @param array $criteria
|
|
106 | 140 |
* A keyed array used to determine what votes will be deleted when the current |
107 | 141 |
* vote is cast. If no value is specified, all votes for the current content |
108 | 142 |
* by the current user will be reset. If an empty array is passed in, no votes |
... | ... | |
117 | 151 |
* $criteria['vote_source'] |
118 | 152 |
* $criteria['timestamp'] (If this is set, records with timestamps |
119 | 153 |
* GREATER THAN the set value will be selected.) |
120 |
* @return |
|
154 |
* |
|
155 |
* @return array |
|
121 | 156 |
* An array of vote result records affected by the vote. The values are |
122 | 157 |
* contained in a nested array keyed thusly: |
123 | 158 |
* $value = $results[$entity_type][$entity_id][$tag][$value_type][$function] |
... | ... | |
229 | 264 |
* Save a collection of votes to the database. |
230 | 265 |
* |
231 | 266 |
* This function does most of the heavy lifting for VotingAPI the main |
232 |
* votingapi_set_votes() function, but does NOT automatically triger re-tallying |
|
267 |
* votingapi_set_votes() function, but does NOT automatically triger |
|
268 |
* re-tallying |
|
233 | 269 |
* of results. As such, it's useful for modules that must insert their votes in |
234 | 270 |
* separate batches without triggering unecessary recalculation. |
235 | 271 |
* |
236 |
* Remember that any module calling this function implicitly assumes responsibility |
|
237 |
* for calling votingapi_recalculate_results() when all votes have been inserted. |
|
272 |
* Remember that any module calling this function implicitly assumes |
|
273 |
* responsibility for calling votingapi_recalculate_results() when all votes |
|
274 |
* have been inserted. |
|
238 | 275 |
* |
239 |
* @param $votes |
|
276 |
* @param array $votes
|
|
240 | 277 |
* A vote or array of votes, each with the following structure: |
241 | 278 |
* $vote['entity_type'] (Optional, defaults to 'node') |
242 | 279 |
* $vote['entity_id'] (Required) |
... | ... | |
246 | 283 |
* $vote['uid'] (Optional, defaults to current user) |
247 | 284 |
* $vote['vote_source'] (Optional, defaults to current IP) |
248 | 285 |
* $vote['timestamp'] (Optional, defaults to REQUEST_TIME) |
249 |
* @return |
|
286 |
* |
|
287 |
* @return array |
|
250 | 288 |
* The same votes, with vote_id keys populated. |
251 | 289 |
* |
252 | 290 |
* @see votingapi_set_votes() |
... | ... | |
274 | 312 |
* little use for most third-party modules, unless they manually insert their |
275 | 313 |
* own result data. |
276 | 314 |
* |
277 |
* @param vote_results |
|
315 |
* @param array vote_results
|
|
278 | 316 |
* An array of vote results, each with the following properties: |
279 | 317 |
* $vote_result['entity_type'] |
280 | 318 |
* $vote_result['entity_id'] |
... | ... | |
327 | 365 |
foreach ($vote_results as $vote) { |
328 | 366 |
$vids[] = $vote['vote_cache_id']; |
329 | 367 |
} |
330 |
db_delete('votingapi_cache')->condition('vote_cache_id', $vids, 'IN')->execute(); |
|
368 |
db_delete('votingapi_cache') |
|
369 |
->condition('vote_cache_id', $vids, 'IN') |
|
370 |
->execute(); |
|
331 | 371 |
} |
332 | 372 |
} |
333 | 373 |
|
... | ... | |
348 | 388 |
* GREATER THAN the set value will be selected.) |
349 | 389 |
* @param $limit |
350 | 390 |
* An optional integer specifying the maximum number of votes to return. |
391 |
* |
|
351 | 392 |
* @return |
352 | 393 |
* An array of votes matching the criteria. |
353 | 394 |
*/ |
... | ... | |
357 | 398 |
if (!empty($criteria['vote_source'])) { |
358 | 399 |
$window = variable_get('votingapi_anonymous_window', 86400); |
359 | 400 |
} |
360 |
} else { |
|
401 |
} |
|
402 |
else { |
|
361 | 403 |
$window = variable_get('votingapi_user_window', -1); |
362 | 404 |
} |
363 | 405 |
if ($window >= 0) { |
... | ... | |
383 | 425 |
* GREATER THAN the set value will be selected.) |
384 | 426 |
* @param $limit |
385 | 427 |
* An optional integer specifying the maximum number of votes to return. |
428 |
* |
|
386 | 429 |
* @return |
387 | 430 |
* An array of vote results matching the criteria. |
388 | 431 |
*/ |
... | ... | |
405 | 448 |
* responsibility for the full voting cycle: the votingapi_set_vote() function |
406 | 449 |
* recalculates automatically. |
407 | 450 |
* |
408 |
* @param $entity_type |
|
451 |
* @param string $entity_type
|
|
409 | 452 |
* A string identifying the type of content being rated. Node, comment, |
410 | 453 |
* aggregator item, etc. |
411 |
* @param $entity_id |
|
454 |
* @param string $entity_id
|
|
412 | 455 |
* The key ID of the content being rated. |
413 |
* @return |
|
456 |
* @param bool $force_calculation |
|
457 |
* |
|
458 |
* @return array |
|
414 | 459 |
* An array of the resulting votingapi_cache records, structured thusly: |
415 | 460 |
* $value = $results[$ag][$value_type][$function] |
416 | 461 |
* |
... | ... | |
460 | 505 |
|
461 | 506 |
|
462 | 507 |
/** |
463 |
* Returns metadata about tags, value_types, and results defined by vote modules. |
|
508 |
* Returns metadata about tags, value_types, and results defined by vote |
|
509 |
* modules. |
|
464 | 510 |
* |
465 | 511 |
* If your module needs to determine what existing tags, value_types, etc., are |
466 | 512 |
* being supplied by other modules, call this function. Querying the votingapi |
467 |
* tables for this information directly is discouraged, as values may not appear |
|
468 |
* consistently. (For example, 'average' does not appear in the cache table until |
|
469 |
* votes have actually been cast in the cache table.) |
|
513 |
* tables for this information directly is discouraged, as values may not |
|
514 |
* appear |
|
515 |
* consistently. (For example, 'average' does not appear in the cache table |
|
516 |
* until votes have actually been cast in the cache table.) |
|
470 | 517 |
* |
471 | 518 |
* Three major bins of data are stored: tags, value_types, and functions. Each |
472 | 519 |
* entry in these bins is keyed by the value stored in the actual VotingAPI |
473 |
* tables, and contains an array with (minimally) 'name' and 'description' keys. |
|
520 |
* tables, and contains an array with (minimally) 'name' and 'description' |
|
521 |
* keys. |
|
474 | 522 |
* Modules can add extra keys to their entries if desired. |
475 | 523 |
* |
476 |
* This metadata can be modified or expanded using hook_votingapi_metadata_alter(). |
|
524 |
* This metadata can be modified or expanded using |
|
525 |
* hook_votingapi_metadata_alter(). |
|
477 | 526 |
* |
478 |
* @return |
|
527 |
* @param bool $reset |
|
528 |
* A reset status. |
|
529 |
* |
|
530 |
* @return array |
|
479 | 531 |
* An array of metadata defined by VotingAPI and altered by vote modules. |
480 | 532 |
* |
481 | 533 |
* @see hook_votingapi_metadata_alter() |
... | ... | |
529 | 581 |
function votingapi_votingapi_storage_standard_results($entity_type, $entity_id) { |
530 | 582 |
$cache = array(); |
531 | 583 |
|
532 |
$sql = "SELECT v.value_type, v.tag, ";
|
|
584 |
$sql = "SELECT v.value_type, v.tag, "; |
|
533 | 585 |
$sql .= "COUNT(v.value) as value_count, SUM(v.value) as value_sum "; |
534 | 586 |
$sql .= "FROM {votingapi_vote} v "; |
535 | 587 |
$sql .= "WHERE v.entity_type = :type AND v.entity_id = :id AND v.value_type IN ('points', 'percent') "; |
536 | 588 |
$sql .= "GROUP BY v.value_type, v.tag"; |
537 |
$results = db_query($sql, array(':type' => $entity_type, ':id' => $entity_id)); |
|
589 |
$results = db_query($sql, array( |
|
590 |
':type' => $entity_type, |
|
591 |
':id' => $entity_id, |
|
592 |
)); |
|
538 | 593 |
|
539 | 594 |
foreach ($results as $result) { |
540 | 595 |
$cache[$result->tag][$result->value_type]['count'] = $result->value_count; |
... | ... | |
544 | 599 |
} |
545 | 600 |
} |
546 | 601 |
|
547 |
$sql = "SELECT v.tag, v.value, v.value_type, COUNT(1) AS score ";
|
|
602 |
$sql = "SELECT v.tag, v.value, v.value_type, COUNT(1) AS score "; |
|
548 | 603 |
$sql .= "FROM {votingapi_vote} v "; |
549 | 604 |
$sql .= "WHERE v.entity_type = :type AND v.entity_id = :id AND v.value_type = 'option' "; |
550 | 605 |
$sql .= "GROUP BY v.value, v.tag, v.value_type"; |
551 |
$results = db_query($sql, array(':type' => $entity_type, ':id' => $entity_id)); |
|
606 |
$results = db_query($sql, array( |
|
607 |
':type' => $entity_type, |
|
608 |
':id' => $entity_id, |
|
609 |
)); |
|
552 | 610 |
|
553 | 611 |
foreach ($results as $result) { |
554 | 612 |
$cache[$result->tag][$result->value_type]['option-' . $result->value] = $result->score; |
... | ... | |
580 | 638 |
* |
581 | 639 |
* @param $vote |
582 | 640 |
* A single vote. |
583 |
* @return |
|
584 |
* A vote object with all required properties filled in with |
|
585 |
* their default values. |
|
586 | 641 |
*/ |
587 | 642 |
function _votingapi_prep_vote(&$vote) { |
588 | 643 |
global $user; |
... | ... | |
594 | 649 |
'uid' => $user->uid, |
595 | 650 |
'timestamp' => REQUEST_TIME, |
596 | 651 |
'vote_source' => ip_address(), |
597 |
'prepped' => TRUE |
|
652 |
'prepped' => TRUE,
|
|
598 | 653 |
); |
599 | 654 |
} |
600 | 655 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules