You are here

function hook_votingapi_results_alter in Voting API 8.3

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

Adds to or changes the calculated vote results for an entity.

VotingAPI calculates a number of common aggregate functions automatically, including the average vote and total number of votes cast.

Parameters

array $vote_results: An alterable array of aggregate vote results.

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

int $entity_id: The key ID of the entity being rated.

See also

VoteResultFunctionManager::recalculateResults()

1 function implements hook_votingapi_results_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

votingapi_test_votingapi_results_alter in tests/modules/votingapi_test/votingapi_test.module
Implements hook_votingapi_results_alter().
1 invocation of hook_votingapi_results_alter()
VoteResultFunctionManager::performAndStore in src/VoteResultFunctionManager.php
Perform the result calculations on a set of votes and store the results.

File

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

Code

function hook_votingapi_results_alter(array &$vote_results, $entity_type, $entity_id) {

  // Calculate a standard deviation for votes cast on an entity.
  $query = Database::getConnection()
    ->select('votingapi_vote', 'v');
  $query
    ->addExpression('STDDEV(v.value)', 'standard_deviation');
  $query
    ->condition('v.entity_type', $entity_type);
  $query
    ->condition('v.entity_id', $entity_id);
  $query
    ->groupBy('v.tag');
  $aggregate = $query
    ->execute()
    ->fetchObject();

  // Add the standard deviation to the voted entity results.
  $vote_results[] = [
    'entity_id' => $entity_id,
    'entity_type' => $entity_type,
    'type' => $vote_results[0]
      ->bundle(),
    'function' => 'standard_deviation',
    'value' => $aggregate->standard_deviation,
    'value_type' => $vote_results[0]
      ->get('value_type')->value,
    'timestamp' => \Drupal::time()
      ->getRequestTime(),
  ];
}