You are here

function votingapi_recalculate_results in Voting API 6

Same name and namespace in other branches
  1. 5 votingapi.module \votingapi_recalculate_results()
  2. 6.2 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: $value = $results[$ag][$value_type][$function]

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

File

./votingapi.module, line 343

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.
    $criteria = array(
      'content_type' => $content_type,
      'content_id' => $content_id,
    );
    $old_cache = votingapi_select_results($criteria);
    votingapi_delete_results($old_cache);
    $votes = votingapi_select_votes($criteria);
    $cache = array();

    // 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.
    drupal_alter('votingapi_results', $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[] = (object) 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, $votes, $content_type, $content_id);
    return $cached;
  }
}