You are here

function votingapi_recalculate_results in Voting API 7.3

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.2 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

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

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

Return value

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

See also

votingapi_set_votes()

6 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 497
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();

    // Bulk query to pull the majority of the results we care about.
    $cache = VotingApi_VoteStorage::get()
      ->standardResults($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[] = new VotingApi_Result(array(
            'entity_type' => $entity_type,
            'entity_id' => $entity_id,
            'value_type' => $type,
            'value' => $value,
            'tag' => $tag,
            'function' => $function,
          ));
        }
      }
    }
    VotingApi_Result::saveMultiple($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;
  }
}