You are here

function votingapi_recalculate_results in Voting API 7.2

Same name and namespace in other branches
  1. 5 votingapi.module \votingapi_recalculate_results()
  2. 6.2 votingapi.module \votingapi_recalculate_results()
  3. 6 votingapi.module \votingapi_recalculate_results()
  4. 7.3 votingapi.module \votingapi_recalculate_results()

Recalculates the aggregate results of all votes for a piece of content.

Loads all votes for a given piece of content, then calculates and caches the aggregate vote results. This is only intended for modules that have assumed responsibility for the full voting cycle: the votingapi_set_vote() function recalculates automatically.

Parameters

string $entity_type: A string identifying the type of content being rated. Node, comment, aggregator item, etc.

string $entity_id: The key ID of the content being rated.

bool $force_calculation:

Return value

array An array of the resulting votingapi_cache records, structured thusly: $value = $results[$ag][$value_type][$function]

See also

votingapi_set_votes()

7 calls to votingapi_recalculate_results()
drush_votingapi_recalculate in ./votingapi.drush.inc
Command callback. Recalculate voting results.
MigrateDestinationVotingApiVote::recalculateResults in ./votingapi.migrate.inc
Recalculate votes for a list of id's.
VotingAPITestCase::testAddMultipleVotes in tests/votingapi.test
Add multiple votes and ensure that the vote calculations are working.
VotingAPITestCase::testAddVote in tests/votingapi.test
Add a vote and ensure that the vote was stored properly.
votingapi_cron in ./votingapi.module
Implements of hook_cron().

... See full list

File

./votingapi.module, line 485
A generalized voting API for Drupal.

Code

function votingapi_recalculate_results($entity_type, $entity_id, $force_calculation = FALSE) {

  // if we're operating in cron mode, and the 'force recalculation' flag is NOT set,
  // bail out. The cron run will pick up the results.
  if (variable_get('votingapi_calculation_schedule', 'immediate') != 'cron' || $force_calculation == TRUE) {
    $query = db_delete('votingapi_cache')
      ->condition('entity_type', $entity_type)
      ->condition('entity_id', $entity_id)
      ->execute();
    $function = variable_get('votingapi_storage_module', 'votingapi') . '_votingapi_storage_standard_results';

    // Bulk query to pull the majority of the results we care about.
    $cache = $function($entity_type, $entity_id);

    // Give other modules a chance to alter the collection of votes.
    drupal_alter('votingapi_results', $cache, $entity_type, $entity_id);

    // Now, do the caching. Woo.
    $cached = array();
    foreach ($cache as $tag => $types) {
      foreach ($types as $type => $functions) {
        foreach ($functions as $function => $value) {
          $cached[] = array(
            'entity_type' => $entity_type,
            'entity_id' => $entity_id,
            'value_type' => $type,
            'value' => $value,
            'tag' => $tag,
            'function' => $function,
          );
        }
      }
    }
    votingapi_add_results($cached);

    // Give other modules a chance to act on the results of the vote totaling.
    module_invoke_all('votingapi_results', $cached, $entity_type, $entity_id);
    return $cached;
  }
}