1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* A generalized voting API for Drupal.
|
6
|
*
|
7
|
* Maintains and provides an interface for a shared bin of vote and rating
|
8
|
* data. Modules can cast votes with arbitrary properties and VotingAPI will
|
9
|
* total them automatically. Support for basic anonymous voting by IP address,
|
10
|
* multi-criteria voting, arbitrary aggregation functions, etc.
|
11
|
*
|
12
|
* /**
|
13
|
* Implementation of hook_help().
|
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
|
}
|
46
|
|
47
|
/**
|
48
|
* Implements of hook_menu().
|
49
|
*/
|
50
|
function votingapi_menu() {
|
51
|
$items = array();
|
52
|
$items['admin/config/search/votingapi'] = array(
|
53
|
'title' => 'Voting API',
|
54
|
'description' => 'Configure sitewide settings for user-generated ratings and votes.',
|
55
|
'page callback' => 'drupal_get_form',
|
56
|
'page arguments' => array('votingapi_settings_form'),
|
57
|
'access callback' => 'user_access',
|
58
|
'access arguments' => array('administer voting api'),
|
59
|
'file' => 'votingapi.admin.inc',
|
60
|
'type' => MENU_NORMAL_ITEM,
|
61
|
);
|
62
|
|
63
|
if (module_exists('devel_generate')) {
|
64
|
$items['admin/config/development/generate/votingapi'] = array(
|
65
|
'title' => 'Generate votes',
|
66
|
'description' => 'Generate a given number of votes on site content. Optionally delete existing votes.',
|
67
|
'page callback' => 'drupal_get_form',
|
68
|
'page arguments' => array('votingapi_generate_votes_form'),
|
69
|
'access arguments' => array('administer voting api'),
|
70
|
'file' => 'votingapi.admin.inc',
|
71
|
);
|
72
|
}
|
73
|
|
74
|
$items['admin/config/search/votingapi/general'] = array(
|
75
|
'title' => 'General Settings',
|
76
|
'type' => MENU_DEFAULT_LOCAL_TASK,
|
77
|
'weight' => -10,
|
78
|
);
|
79
|
$items['admin/config/search/votingapi/rebuild'] = array(
|
80
|
'title' => t('Rebuild voting results'),
|
81
|
'description' => t('Allows rebuilding of voting results cache'),
|
82
|
'page callback' => 'drupal_get_form',
|
83
|
'page arguments' => array('votingapi_rebuild_form'),
|
84
|
'access callback' => 'user_access',
|
85
|
'access arguments' => array('administer voting api'),
|
86
|
'file' => 'votingapi.admin.inc',
|
87
|
'type' => MENU_LOCAL_TASK,
|
88
|
);
|
89
|
|
90
|
return $items;
|
91
|
}
|
92
|
|
93
|
/**
|
94
|
* Implements hook_permission().
|
95
|
*/
|
96
|
function votingapi_permission() {
|
97
|
return array(
|
98
|
'administer voting api' => array(
|
99
|
'title' => t('Administer Voting API'),
|
100
|
),
|
101
|
);
|
102
|
}
|
103
|
|
104
|
/**
|
105
|
* Implements of hook_views_api().
|
106
|
*/
|
107
|
function votingapi_views_api() {
|
108
|
return array(
|
109
|
'api' => 3,
|
110
|
'path' => drupal_get_path('module', 'votingapi') . '/views',
|
111
|
);
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* Implements of hook_cron().
|
116
|
*
|
117
|
* Allows db-intensive recalculations to be deferred until cron-time.
|
118
|
*/
|
119
|
function votingapi_cron() {
|
120
|
if (variable_get('votingapi_calculation_schedule', 'immediate') == 'cron') {
|
121
|
$time = REQUEST_TIME;
|
122
|
$last_cron = variable_get('votingapi_last_cron', 0);
|
123
|
$result = db_query('SELECT DISTINCT entity_type, entity_id FROM {votingapi_vote} WHERE timestamp > :timestamp', array(':timestamp' => $last_cron));
|
124
|
foreach ($result as $content) {
|
125
|
votingapi_recalculate_results($content->entity_type, $content->entity_id, TRUE);
|
126
|
}
|
127
|
|
128
|
variable_set('votingapi_last_cron', $time);
|
129
|
}
|
130
|
|
131
|
_votingapi_cron_delete_orphaned();
|
132
|
}
|
133
|
|
134
|
/**
|
135
|
* Cast a vote on a particular piece of content.
|
136
|
*
|
137
|
* This function does most of the heavy lifting needed by third-party modules
|
138
|
* based on VotingAPI. Handles clearing out old votes for a given piece of
|
139
|
* content, saving the incoming votes, and re-tallying the results given the
|
140
|
* new data.
|
141
|
*
|
142
|
* Modules that need more explicit control can call votingapi_add_votes() and
|
143
|
* manage the deletion/recalculation tasks manually.
|
144
|
*
|
145
|
* @param array $votes
|
146
|
* An array of votes, each with the following structure:
|
147
|
* $vote['entity_type'] (Optional, defaults to 'node')
|
148
|
* $vote['entity_id'] (Required)
|
149
|
* $vote['value_type'] (Optional, defaults to 'percent')
|
150
|
* $vote['value'] (Required)
|
151
|
* $vote['tag'] (Optional, defaults to 'vote')
|
152
|
* $vote['uid'] (Optional, defaults to current user)
|
153
|
* $vote['vote_source'] (Optional, defaults to current IP)
|
154
|
* $vote['timestamp'] (Optional, defaults to REQUEST_TIME)
|
155
|
* @param array $criteria
|
156
|
* A keyed array used to determine what votes will be deleted when the current
|
157
|
* vote is cast. If no value is specified, all votes for the current content
|
158
|
* by the current user will be reset. If an empty array is passed in, no votes
|
159
|
* will be reset and all incoming votes will be saved IN ADDITION to existing
|
160
|
* ones.
|
161
|
* $criteria['vote_id'] (If this is set, all other keys are skipped)
|
162
|
* $criteria['entity_type']
|
163
|
* $criteria['entity_type']
|
164
|
* $criteria['value_type']
|
165
|
* $criteria['tag']
|
166
|
* $criteria['uid']
|
167
|
* $criteria['vote_source']
|
168
|
* $criteria['timestamp'] (If this is set, records with timestamps
|
169
|
* GREATER THAN the set value will be selected.)
|
170
|
*
|
171
|
* @return array
|
172
|
* An array of vote result records affected by the vote. The values are
|
173
|
* contained in a nested array keyed thusly:
|
174
|
* $value = $results[$entity_type][$entity_id][$tag][$value_type][$function]
|
175
|
*
|
176
|
* @see votingapi_add_votes()
|
177
|
* @see votingapi_recalculate_results()
|
178
|
*/
|
179
|
function votingapi_set_votes(&$votes, $criteria = NULL) {
|
180
|
$touched = array();
|
181
|
if (!empty($votes['entity_id'])) {
|
182
|
$votes = array($votes);
|
183
|
}
|
184
|
|
185
|
// Allow other modules to modify or unset/remove votes.
|
186
|
// module_invoke_all does not allow passing variables by reference
|
187
|
// http://api.drupal.org/api/drupal/includes%21module.inc/function/module_invoke_all/7#comment-35778
|
188
|
drupal_alter(array('votingapi_preset_votes'), $votes);
|
189
|
// Handle clearing out old votes if they exist.
|
190
|
if (!isset($criteria)) {
|
191
|
// If the calling function didn't explicitly set criteria for vote deletion,
|
192
|
// build up the delete queries here.
|
193
|
foreach ($votes as $vote) {
|
194
|
$identifier_info = votingapi_current_user_identifier();
|
195
|
// Make sure we're dealing with an array to avoid errors.
|
196
|
if (is_array($vote) && is_array($identifier_info)) {
|
197
|
$tmp = $vote + $identifier_info;
|
198
|
}
|
199
|
if (isset($tmp['value'])) {
|
200
|
unset($tmp['value']);
|
201
|
}
|
202
|
votingapi_delete_votes(votingapi_select_votes($tmp));
|
203
|
}
|
204
|
}
|
205
|
elseif (is_array($criteria)) {
|
206
|
// The calling function passed in an explicit set of delete filters.
|
207
|
if (!empty($criteria['entity_id'])) {
|
208
|
$criteria = array($criteria);
|
209
|
}
|
210
|
foreach ($criteria as $c) {
|
211
|
votingapi_delete_votes(votingapi_select_votes($c));
|
212
|
}
|
213
|
}
|
214
|
|
215
|
foreach ($votes as $key => $vote) {
|
216
|
_votingapi_prep_vote($vote);
|
217
|
$votes[$key] = $vote; // Is this needed? Check to see how PHP4 handles refs.
|
218
|
}
|
219
|
|
220
|
// Cast the actual votes, inserting them into the table.
|
221
|
votingapi_add_votes($votes);
|
222
|
|
223
|
foreach ($votes as $vote) {
|
224
|
$touched[$vote['entity_type']][$vote['entity_id']] = TRUE;
|
225
|
}
|
226
|
|
227
|
if (variable_get('votingapi_calculation_schedule', 'immediate') != 'cron') {
|
228
|
foreach ($touched as $type => $ids) {
|
229
|
foreach ($ids as $id => $bool) {
|
230
|
$touched[$type][$id] = votingapi_recalculate_results($type, $id);
|
231
|
}
|
232
|
}
|
233
|
}
|
234
|
return $touched;
|
235
|
}
|
236
|
|
237
|
/**
|
238
|
* Generate a proper identifier for the current user: if they have an account,
|
239
|
* return their UID. Otherwise, return their IP address.
|
240
|
*/
|
241
|
function votingapi_current_user_identifier() {
|
242
|
global $user;
|
243
|
$criteria = array('uid' => $user->uid);
|
244
|
if (!$user->uid) {
|
245
|
$criteria['vote_source'] = ip_address();
|
246
|
}
|
247
|
return $criteria;
|
248
|
}
|
249
|
|
250
|
/**
|
251
|
* Implements of hook_votingapi_storage_add_vote().
|
252
|
*/
|
253
|
function votingapi_votingapi_storage_add_vote(&$vote) {
|
254
|
drupal_write_record('votingapi_vote', $vote);
|
255
|
}
|
256
|
|
257
|
/**
|
258
|
* Implements of hook_votingapi_storage_delete_votes().
|
259
|
*/
|
260
|
function votingapi_votingapi_storage_delete_votes($votes, $vids) {
|
261
|
db_delete('votingapi_vote')->condition('vote_id', $vids, 'IN')->execute();
|
262
|
}
|
263
|
|
264
|
/**
|
265
|
* Implements of hook_votingapi_storage_select_votes().
|
266
|
*/
|
267
|
function votingapi_votingapi_storage_select_votes($criteria, $limit) {
|
268
|
$query = db_select('votingapi_vote')->fields('votingapi_vote');
|
269
|
foreach ($criteria as $key => $value) {
|
270
|
if ($key == 'timestamp') {
|
271
|
$query->condition($key, $value, '>');
|
272
|
}
|
273
|
else {
|
274
|
$query->condition($key, $value, is_array($value) ? 'IN' : '=');
|
275
|
}
|
276
|
}
|
277
|
if (!empty($limit)) {
|
278
|
$query->range(0, $limit);
|
279
|
}
|
280
|
return $query->execute()->fetchAll(PDO::FETCH_ASSOC);
|
281
|
}
|
282
|
|
283
|
/**
|
284
|
* Save a collection of votes to the database.
|
285
|
*
|
286
|
* This function does most of the heavy lifting for VotingAPI the main
|
287
|
* votingapi_set_votes() function, but does NOT automatically triger
|
288
|
* re-tallying
|
289
|
* of results. As such, it's useful for modules that must insert their votes in
|
290
|
* separate batches without triggering unecessary recalculation.
|
291
|
*
|
292
|
* Remember that any module calling this function implicitly assumes
|
293
|
* responsibility for calling votingapi_recalculate_results() when all votes
|
294
|
* have been inserted.
|
295
|
*
|
296
|
* @param array $votes
|
297
|
* A vote or array of votes, each with the following structure:
|
298
|
* $vote['entity_type'] (Optional, defaults to 'node')
|
299
|
* $vote['entity_id'] (Required)
|
300
|
* $vote['value_type'] (Optional, defaults to 'percent')
|
301
|
* $vote['value'] (Required)
|
302
|
* $vote['tag'] (Optional, defaults to 'vote')
|
303
|
* $vote['uid'] (Optional, defaults to current user)
|
304
|
* $vote['vote_source'] (Optional, defaults to current IP)
|
305
|
* $vote['timestamp'] (Optional, defaults to REQUEST_TIME)
|
306
|
*
|
307
|
* @return array
|
308
|
* The same votes, with vote_id keys populated.
|
309
|
*
|
310
|
* @see votingapi_set_votes()
|
311
|
* @see votingapi_recalculate_results()
|
312
|
*/
|
313
|
function votingapi_add_votes(&$votes) {
|
314
|
if (!empty($votes['entity_id'])) {
|
315
|
$votes = array($votes);
|
316
|
}
|
317
|
$function = variable_get('votingapi_storage_module', 'votingapi') . '_votingapi_storage_add_vote';
|
318
|
foreach ($votes as $key => $vote) {
|
319
|
_votingapi_prep_vote($vote);
|
320
|
$function($vote);
|
321
|
$votes[$key] = $vote;
|
322
|
}
|
323
|
module_invoke_all('votingapi_insert', $votes);
|
324
|
return $votes;
|
325
|
}
|
326
|
|
327
|
/**
|
328
|
* Save a bundle of vote results to the database.
|
329
|
*
|
330
|
* This function is called by votingapi_recalculate_results() after tallying
|
331
|
* the values of all cast votes on a piece of content. This function will be of
|
332
|
* little use for most third-party modules, unless they manually insert their
|
333
|
* own result data.
|
334
|
*
|
335
|
* @param array vote_results
|
336
|
* An array of vote results, each with the following properties:
|
337
|
* $vote_result['entity_type']
|
338
|
* $vote_result['entity_id']
|
339
|
* $vote_result['value_type']
|
340
|
* $vote_result['value']
|
341
|
* $vote_result['tag']
|
342
|
* $vote_result['function']
|
343
|
* $vote_result['timestamp'] (Optional, defaults to REQUEST_TIME)
|
344
|
*/
|
345
|
function votingapi_add_results($vote_results = array()) {
|
346
|
if (!empty($vote_results['entity_id'])) {
|
347
|
$vote_results = array($vote_results);
|
348
|
}
|
349
|
|
350
|
foreach ($vote_results as $vote_result) {
|
351
|
$vote_result['timestamp'] = REQUEST_TIME;
|
352
|
drupal_write_record('votingapi_cache', $vote_result);
|
353
|
}
|
354
|
}
|
355
|
|
356
|
/**
|
357
|
* Delete votes from the database.
|
358
|
*
|
359
|
* @param $votes
|
360
|
* An array of votes to delete. Minimally, each vote must have the 'vote_id'
|
361
|
* key set.
|
362
|
*/
|
363
|
function votingapi_delete_votes($votes = array()) {
|
364
|
if (!empty($votes)) {
|
365
|
module_invoke_all('votingapi_delete', $votes);
|
366
|
$vids = array();
|
367
|
foreach ($votes as $vote) {
|
368
|
$vids[] = $vote['vote_id'];
|
369
|
}
|
370
|
$function = variable_get('votingapi_storage_module', 'votingapi') . '_votingapi_storage_delete_votes';
|
371
|
$function($votes, $vids);
|
372
|
}
|
373
|
}
|
374
|
|
375
|
/**
|
376
|
* Delete cached vote results from the database.
|
377
|
*
|
378
|
* @param $vote_results
|
379
|
* An array of vote results to delete. Minimally, each vote result must have
|
380
|
* the 'vote_cache_id' key set.
|
381
|
*/
|
382
|
function votingapi_delete_results($vote_results = array()) {
|
383
|
if (!empty($vote_results)) {
|
384
|
$vids = array();
|
385
|
foreach ($vote_results as $vote) {
|
386
|
$vids[] = $vote['vote_cache_id'];
|
387
|
}
|
388
|
db_delete('votingapi_cache')
|
389
|
->condition('vote_cache_id', $vids, 'IN')
|
390
|
->execute();
|
391
|
}
|
392
|
}
|
393
|
|
394
|
/**
|
395
|
* Select individual votes from the database.
|
396
|
*
|
397
|
* @param $criteria
|
398
|
* A keyed array used to build the select query. Keys can contain
|
399
|
* a single value or an array of values to be matched.
|
400
|
* $criteria['vote_id'] (If this is set, all other keys are skipped)
|
401
|
* $criteria['entity_id']
|
402
|
* $criteria['entity_type']
|
403
|
* $criteria['value_type']
|
404
|
* $criteria['tag']
|
405
|
* $criteria['uid']
|
406
|
* $criteria['vote_source']
|
407
|
* $criteria['timestamp'] (If this is set, records with timestamps
|
408
|
* GREATER THAN the set value will be selected.)
|
409
|
* @param $limit
|
410
|
* An optional integer specifying the maximum number of votes to return.
|
411
|
*
|
412
|
* @return
|
413
|
* An array of votes matching the criteria.
|
414
|
*/
|
415
|
function votingapi_select_votes($criteria = array(), $limit = 0) {
|
416
|
$window = -1;
|
417
|
if (empty($criteria['uid']) || $criteria['uid'] == 0) {
|
418
|
if (!empty($criteria['vote_source'])) {
|
419
|
$window = variable_get('votingapi_anonymous_window', 86400);
|
420
|
}
|
421
|
}
|
422
|
else {
|
423
|
$window = variable_get('votingapi_user_window', -1);
|
424
|
}
|
425
|
if ($window >= 0) {
|
426
|
$criteria['timestamp'] = REQUEST_TIME - $window;
|
427
|
}
|
428
|
$function = variable_get('votingapi_storage_module', 'votingapi') . '_votingapi_storage_select_votes';
|
429
|
return $function($criteria, $limit);
|
430
|
}
|
431
|
|
432
|
/**
|
433
|
* Select cached vote results from the database.
|
434
|
*
|
435
|
* @param $criteria
|
436
|
* A keyed array used to build the select query. Keys can contain
|
437
|
* a single value or an array of values to be matched.
|
438
|
* $criteria['vote_cache_id'] (If this is set, all other keys are skipped)
|
439
|
* $criteria['entity_id']
|
440
|
* $criteria['entity_type']
|
441
|
* $criteria['value_type']
|
442
|
* $criteria['tag']
|
443
|
* $criteria['function']
|
444
|
* $criteria['timestamp'] (If this is set, records with timestamps
|
445
|
* GREATER THAN the set value will be selected.)
|
446
|
* @param $limit
|
447
|
* An optional integer specifying the maximum number of votes to return.
|
448
|
*
|
449
|
* @return
|
450
|
* An array of vote results matching the criteria.
|
451
|
*/
|
452
|
function votingapi_select_results($criteria = array(), $limit = 0) {
|
453
|
$query = db_select('votingapi_cache')->fields('votingapi_cache');
|
454
|
foreach ($criteria as $key => $value) {
|
455
|
$query->condition($key, $value, is_array($value) ? 'IN' : '=');
|
456
|
}
|
457
|
if (!empty($limit)) {
|
458
|
$query->range(0, $limit);
|
459
|
}
|
460
|
return $query->execute()->fetchAll(PDO::FETCH_ASSOC);
|
461
|
}
|
462
|
|
463
|
/**
|
464
|
* Recalculates the aggregate results of all votes for a piece of content.
|
465
|
*
|
466
|
* Loads all votes for a given piece of content, then calculates and caches the
|
467
|
* aggregate vote results. This is only intended for modules that have assumed
|
468
|
* responsibility for the full voting cycle: the votingapi_set_vote() function
|
469
|
* recalculates automatically.
|
470
|
*
|
471
|
* @param string $entity_type
|
472
|
* A string identifying the type of content being rated. Node, comment,
|
473
|
* aggregator item, etc.
|
474
|
* @param string $entity_id
|
475
|
* The key ID of the content being rated.
|
476
|
* @param bool $force_calculation
|
477
|
*
|
478
|
* @return array
|
479
|
* An array of the resulting votingapi_cache records, structured thusly:
|
480
|
* $value = $results[$ag][$value_type][$function]
|
481
|
*
|
482
|
* @see votingapi_set_votes()
|
483
|
*/
|
484
|
function votingapi_recalculate_results($entity_type, $entity_id, $force_calculation = FALSE) {
|
485
|
// if we're operating in cron mode, and the 'force recalculation' flag is NOT set,
|
486
|
// bail out. The cron run will pick up the results.
|
487
|
|
488
|
if (variable_get('votingapi_calculation_schedule', 'immediate') != 'cron' || $force_calculation == TRUE) {
|
489
|
$query = db_delete('votingapi_cache')
|
490
|
->condition('entity_type', $entity_type)
|
491
|
->condition('entity_id', $entity_id)
|
492
|
->execute();
|
493
|
|
494
|
$function = variable_get('votingapi_storage_module', 'votingapi') . '_votingapi_storage_standard_results';
|
495
|
// Bulk query to pull the majority of the results we care about.
|
496
|
$cache = $function($entity_type, $entity_id);
|
497
|
|
498
|
// Give other modules a chance to alter the collection of votes.
|
499
|
drupal_alter('votingapi_results', $cache, $entity_type, $entity_id);
|
500
|
|
501
|
// Now, do the caching. Woo.
|
502
|
$cached = array();
|
503
|
foreach ($cache as $tag => $types) {
|
504
|
foreach ($types as $type => $functions) {
|
505
|
foreach ($functions as $function => $value) {
|
506
|
$cached[] = array(
|
507
|
'entity_type' => $entity_type,
|
508
|
'entity_id' => $entity_id,
|
509
|
'value_type' => $type,
|
510
|
'value' => $value,
|
511
|
'tag' => $tag,
|
512
|
'function' => $function,
|
513
|
);
|
514
|
}
|
515
|
}
|
516
|
}
|
517
|
votingapi_add_results($cached);
|
518
|
|
519
|
// Give other modules a chance to act on the results of the vote totaling.
|
520
|
module_invoke_all('votingapi_results', $cached, $entity_type, $entity_id);
|
521
|
|
522
|
return $cached;
|
523
|
}
|
524
|
}
|
525
|
|
526
|
|
527
|
/**
|
528
|
* Returns metadata about tags, value_types, and results defined by vote
|
529
|
* modules.
|
530
|
*
|
531
|
* If your module needs to determine what existing tags, value_types, etc., are
|
532
|
* being supplied by other modules, call this function. Querying the votingapi
|
533
|
* tables for this information directly is discouraged, as values may not
|
534
|
* appear
|
535
|
* consistently. (For example, 'average' does not appear in the cache table
|
536
|
* until votes have actually been cast in the cache table.)
|
537
|
*
|
538
|
* Three major bins of data are stored: tags, value_types, and functions. Each
|
539
|
* entry in these bins is keyed by the value stored in the actual VotingAPI
|
540
|
* tables, and contains an array with (minimally) 'name' and 'description'
|
541
|
* keys.
|
542
|
* Modules can add extra keys to their entries if desired.
|
543
|
*
|
544
|
* This metadata can be modified or expanded using
|
545
|
* hook_votingapi_metadata_alter().
|
546
|
*
|
547
|
* @param bool $reset
|
548
|
* A reset status.
|
549
|
*
|
550
|
* @return array
|
551
|
* An array of metadata defined by VotingAPI and altered by vote modules.
|
552
|
*
|
553
|
* @see hook_votingapi_metadata_alter()
|
554
|
*/
|
555
|
function votingapi_metadata($reset = FALSE) {
|
556
|
static $data;
|
557
|
if ($reset || !isset($data)) {
|
558
|
$data = array(
|
559
|
'tags' => array(
|
560
|
'vote' => array(
|
561
|
'name' => t('Normal vote'),
|
562
|
'description' => t('The default tag for votes on content. If multiple votes with different tags are being cast on a piece of content, consider casting a "summary" vote with this tag as well.'),
|
563
|
),
|
564
|
),
|
565
|
'value_types' => array(
|
566
|
'percent' => array(
|
567
|
'name' => t('Percent'),
|
568
|
'description' => t('Votes in a specific range. Values are stored in a 1-100 range, but can be represented as any scale when shown to the user.'),
|
569
|
),
|
570
|
'points' => array(
|
571
|
'name' => t('Points'),
|
572
|
'description' => t('Votes that contribute points/tokens/karma towards a total. May be positive or negative.'),
|
573
|
),
|
574
|
),
|
575
|
'functions' => array(
|
576
|
'count' => array(
|
577
|
'name' => t('Number of votes'),
|
578
|
'description' => t('The number of votes cast for a given piece of content.'),
|
579
|
),
|
580
|
'average' => array(
|
581
|
'name' => t('Average vote'),
|
582
|
'description' => t('The average vote cast on a given piece of content.'),
|
583
|
),
|
584
|
'sum' => array(
|
585
|
'name' => t('Total score'),
|
586
|
'description' => t('The sum of all votes for a given piece of content.'),
|
587
|
'value_types' => array('points'),
|
588
|
),
|
589
|
),
|
590
|
);
|
591
|
|
592
|
drupal_alter('votingapi_metadata', $data);
|
593
|
}
|
594
|
|
595
|
return $data;
|
596
|
}
|
597
|
|
598
|
/**
|
599
|
* Builds the default VotingAPI results for the three supported voting styles.
|
600
|
*/
|
601
|
function votingapi_votingapi_storage_standard_results($entity_type, $entity_id) {
|
602
|
$cache = array();
|
603
|
|
604
|
$sql = "SELECT v.value_type, v.tag, ";
|
605
|
$sql .= "COUNT(v.value) as value_count, SUM(v.value) as value_sum ";
|
606
|
$sql .= "FROM {votingapi_vote} v ";
|
607
|
$sql .= "WHERE v.entity_type = :type AND v.entity_id = :id AND v.value_type IN ('points', 'percent') ";
|
608
|
$sql .= "GROUP BY v.value_type, v.tag";
|
609
|
$results = db_query($sql, array(
|
610
|
':type' => $entity_type,
|
611
|
':id' => $entity_id,
|
612
|
));
|
613
|
|
614
|
foreach ($results as $result) {
|
615
|
$cache[$result->tag][$result->value_type]['count'] = $result->value_count;
|
616
|
$cache[$result->tag][$result->value_type]['average'] = $result->value_sum / $result->value_count;
|
617
|
if ($result->value_type == 'points') {
|
618
|
$cache[$result->tag][$result->value_type]['sum'] = $result->value_sum;
|
619
|
}
|
620
|
}
|
621
|
|
622
|
$sql = "SELECT v.tag, v.value, v.value_type, COUNT(1) AS score ";
|
623
|
$sql .= "FROM {votingapi_vote} v ";
|
624
|
$sql .= "WHERE v.entity_type = :type AND v.entity_id = :id AND v.value_type = 'option' ";
|
625
|
$sql .= "GROUP BY v.value, v.tag, v.value_type";
|
626
|
$results = db_query($sql, array(
|
627
|
':type' => $entity_type,
|
628
|
':id' => $entity_id,
|
629
|
));
|
630
|
|
631
|
foreach ($results as $result) {
|
632
|
$cache[$result->tag][$result->value_type]['option-' . $result->value] = $result->score;
|
633
|
}
|
634
|
|
635
|
return $cache;
|
636
|
}
|
637
|
|
638
|
/**
|
639
|
* Retrieve the value of the first vote matching the criteria passed in.
|
640
|
*/
|
641
|
function votingapi_select_single_vote_value($criteria = array()) {
|
642
|
if ($results = votingapi_select_votes($criteria, 1)) {
|
643
|
return $results[0]['value'];
|
644
|
}
|
645
|
}
|
646
|
|
647
|
/**
|
648
|
* Retrieve the value of the first result matching the criteria passed in.
|
649
|
*/
|
650
|
function votingapi_select_single_result_value($criteria = array()) {
|
651
|
if ($results = votingapi_select_results($criteria, 1)) {
|
652
|
return $results[0]['value'];
|
653
|
}
|
654
|
}
|
655
|
|
656
|
/**
|
657
|
* Populate the value of any unset vote properties.
|
658
|
*
|
659
|
* @param $vote
|
660
|
* A single vote.
|
661
|
*/
|
662
|
function _votingapi_prep_vote(&$vote) {
|
663
|
global $user;
|
664
|
if (is_array($vote) && empty($vote['prepped'])) {
|
665
|
$vote += array(
|
666
|
'entity_type' => 'node',
|
667
|
'value_type' => 'percent',
|
668
|
'tag' => 'vote',
|
669
|
'uid' => $user->uid,
|
670
|
'timestamp' => REQUEST_TIME,
|
671
|
'vote_source' => ip_address(),
|
672
|
'prepped' => TRUE,
|
673
|
);
|
674
|
}
|
675
|
}
|
676
|
|
677
|
/**
|
678
|
* Implements hook_entity_delete().
|
679
|
*
|
680
|
* Delete all votes and cache entries for the deleted entities
|
681
|
*/
|
682
|
function votingapi_entity_delete($entity, $type) {
|
683
|
$ids = entity_extract_ids($type, $entity);
|
684
|
$id = array($ids[0]);
|
685
|
_votingapi_delete_cache_by_entity($id, $type);
|
686
|
_votingapi_delete_votes_by_entity($id, $type);
|
687
|
}
|
688
|
|
689
|
/**
|
690
|
* Helper function to delete all cache entries on given entities.
|
691
|
*
|
692
|
* @param array entity ids
|
693
|
* @param string entity type
|
694
|
*/
|
695
|
function _votingapi_delete_cache_by_entity($entity_ids, $type) {
|
696
|
$result = db_select('votingapi_cache', 'v')
|
697
|
->fields('v', array('vote_cache_id'))
|
698
|
->condition('entity_type', $type)
|
699
|
->condition('entity_id', $entity_ids)
|
700
|
->execute();
|
701
|
$votes = array();
|
702
|
foreach ($result as $row) {
|
703
|
$votes[]['vote_cache_id'] = $row->vote_cache_id;
|
704
|
}
|
705
|
votingapi_delete_results($votes);
|
706
|
}
|
707
|
|
708
|
/**
|
709
|
* Helper function to delete all votes on given entities.
|
710
|
*
|
711
|
* @param array entity ids
|
712
|
* @param string entity type
|
713
|
*/
|
714
|
function _votingapi_delete_votes_by_entity($entity_ids, $type) {
|
715
|
$result = db_select('votingapi_vote', 'v')
|
716
|
->fields('v', array('vote_id', 'entity_type', 'entity_id'))
|
717
|
->condition('entity_type', $type)
|
718
|
->condition('entity_id', $entity_ids)
|
719
|
->execute();
|
720
|
$votes = array();
|
721
|
foreach ($result as $row) {
|
722
|
$votes[] = (array) $row;
|
723
|
}
|
724
|
votingapi_delete_votes($votes);
|
725
|
}
|
726
|
|
727
|
/**
|
728
|
* Delete votes and cache entries for a number of entities in the queue.
|
729
|
*/
|
730
|
function _votingapi_cron_delete_orphaned() {
|
731
|
$queue = DrupalQueue::get('VotingAPIOrphaned');
|
732
|
$limit = variable_get('votingapi_cron_orphaned_max', 50);
|
733
|
$done = 0;
|
734
|
while (($item = $queue->claimItem()) && $done++ < $limit) {
|
735
|
_votingapi_delete_cache_by_entity(array($item->data['entity_id']), $item->data['entity_type']);
|
736
|
_votingapi_delete_votes_by_entity(array($item->data['entity_id']), $item->data['entity_type']);
|
737
|
$queue->deleteItem($item);
|
738
|
}
|
739
|
}
|