public function VoteResultFunctionManager::recalculateResults in Voting API 8.3
Recalculates the aggregate voting results of all votes for a given entity.
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
string $entity_type_id: A string identifying the type of content being rated. Node, comment, aggregator item, etc.
string $entity_id: The key ID of the content being rated.
string $vote_type: The type of vote cast.
File
- src/
VoteResultFunctionManager.php, line 115
Class
- VoteResultFunctionManager
- Manages vote result plugins.
Namespace
Drupal\votingapiCode
public function recalculateResults($entity_type_id, $entity_id, $vote_type) {
$this->database
->delete('votingapi_result')
->condition('entity_type', $entity_type_id)
->condition('entity_id', $entity_id)
->condition('type', $vote_type)
->execute();
$vote_storage = $this->entityTypeManager
->getStorage('vote');
$vote_ids = $vote_storage
->getQuery()
->condition('entity_type', $entity_type_id)
->condition('entity_id', $entity_id)
->condition('type', $vote_type)
->sort('type')
->execute();
if (!empty($vote_ids)) {
$votes = [];
$vote_type = '';
foreach ($vote_ids as $vote_id) {
$vote = $vote_storage
->load($vote_id);
// Votes are sorted by vote type, so when we hit a new type, we can run
// find the results of the current set and then start over.
if (!empty($vote_type) && $vote_type != $vote
->bundle()) {
$this
->performAndStore($votes);
$vote_type = $vote
->bundle();
$votes = [];
}
$votes[] = $vote;
}
// Still one last set to process.
$this
->performAndStore($votes);
}
}