You are here

function hook_votingapi_results_alter in Voting API 7.2

Same name and namespace in other branches
  1. 8.3 votingapi.api.php \hook_votingapi_results_alter()
  2. 6.2 votingapi.api.php \hook_votingapi_results_alter()
  3. 7.3 votingapi.api.php \hook_votingapi_results_alter()

Adds to or changes the calculated vote results for a piece of content.

VotingAPI calculates a number of common aggregate functions automatically, including the average vote and total number of votes cast. Results are grouped by 'tag', 'value_type', and then 'function' in the following format:

$results[$tag][$value_type][$aggregate_function] = $value;

If no custom tag is being used for votes, the catch-all "vote" tag should be used. In cases where custom tags are used to vote on different aspects of a piece of content, a catch-all "vote" value should still be calculated for use on summary screens, etc.

Parameters

$vote_results: An alterable array of aggregate vote results.

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

See also

votingapi_recalculate_results()

1 invocation of hook_votingapi_results_alter()
votingapi_recalculate_results in ./votingapi.module
Recalculates the aggregate results of all votes for a piece of content.

File

./votingapi.api.php, line 34
Provides hook documentation for the VotingAPI module.

Code

function hook_votingapi_results_alter(&$vote_results, $content_type, $content_id) {

  // We're using a MySQLism (STDDEV isn't ANSI SQL), but it's OK because this is
  // an example. And no one would ever base real code on sample code. Ever. Never.
  $sql = "SELECT v.tag, STDDEV(v.value) as standard_deviation ";
  $sql .= "FROM {votingapi_vote} v ";
  $sql .= "WHERE v.content_type = '%s' AND v.content_id = %d AND v.value_type = 'percent' ";
  $sql .= "GROUP BY v.tag";

  // VotingAPI wants the data in the following format:
  // $vote_results[$tag][$value_type][$aggregate_function] = $value;
  while ($aggregate = db_query($sql, $content_type, $content_id)
    ->fetchAssoc()) {
    $vote_results[$aggregate['tag']]['percent']['standard_deviation'] = $aggregate['standard_deviation'];
  }
}