You are here

function votingapi_recalculate_results in Voting API 6.2

Same name and namespace in other branches
  1. 5 votingapi.module \votingapi_recalculate_results()
  2. 6 votingapi.module \votingapi_recalculate_results()
  3. 7.3 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

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

$content_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()

4 calls to votingapi_recalculate_results()
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
Implementation of hook_cron().
votingapi_set_votes in ./votingapi.module
Cast a vote on a particular piece of content.

File

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

Code

function votingapi_recalculate_results($content_type, $content_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) {
    $criteria = array(
      'content_type' => $content_type,
      'content_id' => $content_id,
    );
    _votingapi_delete('cache', $criteria);

    // Bulk query to pull the majority of the results we care about.
    $cache = _votingapi_get_standard_results($content_type, $content_id);

    // Give other modules a chance to alter the collection of votes.
    drupal_alter('votingapi_results', $cache, $content_type, $content_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(
            'content_type' => $content_type,
            'content_id' => $content_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, $content_type, $content_id);
    return $cached;
  }
}