function _votingapi_results_rebuild in Voting API 7.2
Batch API callback for the vote rebuilding operation.
1 string reference to '_votingapi_results_rebuild'
- votingapi_rebuild_form_submit in ./
votingapi.admin.inc - Submit handler for votingapi_rebuild_form.
File
- ./
votingapi.admin.inc, line 185 - Configuration forms and helper functions for VotingAPI module.
Code
function _votingapi_results_rebuild($tag, &$context) {
if (empty($context['sandbox']['max_entity_id'])) {
// Set up the sandbox to work within.
$context['sandbox']['current_entity_id'] = 0;
$context['sandbox']['current_entity_types'] = array();
// Get the lowest/highest entity_id for this context, for later comparisons.
$res = db_query('SELECT MIN(entity_id) as min_id, MAX(entity_id) as max_id FROM {votingapi_vote} WHERE tag = :tag', array(
':tag' => $tag,
))
->fetchAssoc();
$context['sandbox']['max_entity_id'] = $res['max_id'];
$context['sandbox']['min_entity_id'] = $res['min_id'] - 1;
$context['sandbox']['max_entity_id_type'] = db_query('SELECT max(entity_type) FROM {votingapi_vote} WHERE entity_id = :id AND tag = :tag', array(
':id' => $context['sandbox']['max_entity_id'],
':tag' => $tag,
))
->fetchField();
$context['results']['count'] = 0;
}
// Grab all entity_ids from the votingapi_vote table.
// Theoretically, there can be an endless loop if there are $limit entities
// (of different types) with the same entity_id... We'll take that chance.
$limit = 100;
// $result = db_query_range('SELECT DISTINCT entity_type, entity_id FROM {votingapi_vote} WHERE tag = :tag AND entity_id >= :cid ORDER BY entity_id ASC',
$result = db_query_range('SELECT DISTINCT entity_type, entity_id FROM {votingapi_vote}
WHERE tag = :tag AND (entity_id > :current_entity_id OR (entity_id = :current_entity_id AND entity_type > :current_entity_type))
ORDER BY entity_id, entity_type', 0, $limit, array(
':tag' => $tag,
':current_entity_id' => $context['sandbox']['current_entity_id'],
':current_entity_type' => $context['sandbox']['current_entity_type'],
));
foreach ($result as $row) {
// Force vote recalculation for this entity.
votingapi_recalculate_results($row->entity_type, $row->entity_id, TRUE);
$context['results']['count']++;
$context['sandbox']['current_entity_id'] = $row->entity_id;
$context['sandbox']['current_entity_type'] = $row->entity_type;
}
// Check if the process has not finished yet.
if ($context['sandbox']['current_entity_id'] != $context['sandbox']['max_entity_id'] || $context['sandbox']['current_entity_type'] != $context['sandbox']['max_entity_id_type']) {
// Make sure 'finished' < 1. Using entity IDs here may cause the progress
// bar to behave eratically, but on the other hand, we make sure we actually
// process all items instead of the number of items we had at the start.
// (If we compared the items processed against the number of items at the
// start of the process, we would not process the highest item if an extra
// (older) entity got a vote, during processing.)
// Using min_entity_id as 'base' should help smoothness at the start a bit.
$context['finished'] = ($context['sandbox']['current_entity_id'] - $context['sandbox']['min_entity_id']) / ($context['sandbox']['max_entity_id'] + 1 - $context['sandbox']['min_entity_id']);
}
}