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