You are here

function votingapi_recalculate_results in Voting API 5

Same name and namespace in other branches
  1. 6.2 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()

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: array($tag => array($value_type => array($calculation_function => $value)));

4 calls to votingapi_recalculate_results()
votingapi_cron in ./votingapi.module
Implementation of hook_cron. Allows db-intensive recalculations to put off until cron-time.
votingapi_generate_create_votes in ./votingapi_generate.module
votingapi_set_vote in ./votingapi.module
Cast a vote on a particular piece of content. If a vote already exists, its value is changed. In most cases, this is the function that should be used by external modules.
votingapi_unset_vote in ./votingapi.module
Deletes all votes cast on a particular content-object by a user. In most cases, this is the function that should be used by external modules.

File

./votingapi.module, line 428

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) {

    // blow away the existing cache records.
    db_query("DELETE FROM {votingapi_cache} WHERE content_type = '%s' AND content_id = %d", $content_type, $content_id);
    $cache = array();
    $votes = _votingapi_get_raw_votes($content_type, $content_id);

    // Loop through, calculate per-type and per-tag totals, etc.
    foreach ($votes as $vote) {
      switch ($vote->value_type) {
        case 'percent':
          $cache[$vote->tag][$vote->value_type]['count'] += 1;
          $cache[$vote->tag][$vote->value_type]['sum'] += $vote->value;
          break;
        case 'points':
          $cache[$vote->tag][$vote->value_type]['count'] += 1;
          $cache[$vote->tag][$vote->value_type]['sum'] += $vote->value;
          break;
        case 'option':
          $cache[$vote->tag][$vote->value_type][$vote->value] += 1;
          break;
      }
    }

    // Do a quick loop through to calculate averages.
    // This is also a good example of how external modules can do their own processing.
    foreach ($cache as $tag => $types) {
      foreach ($types as $type => $functions) {
        if ($type == 'percent' || $type == 'points') {
          $cache[$tag][$type]['average'] = $functions['sum'] / $functions['count'];
        }
        if ($type == 'percent') {

          // we don't actually need the sum for this. discard it to avoid cluttering the db.
          unset($cache[$tag][$type]['sum']);
        }
      }
    }

    // Give other modules a chance to alter the collection of votes.
    votingapi_invoke('calculate', $cache, $votes, $content_type, $content_id);

    // Now, do the caching. Woo.
    foreach ($cache as $tag => $types) {
      foreach ($types as $type => $functions) {
        foreach ($functions as $function => $value) {
          $cached[] = _votingapi_insert_cache_result($content_type, $content_id, $value, $type, $tag, $function);
        }
      }
    }

    // Give other modules a chance to act on the results of the vote totaling.
    votingapi_invoke('results', $cached, $votes, $content_type, $content_id);
    return $cached;
  }
}