You are here

function drush_votingapi_recalculate in Voting API 8.3

Same name and namespace in other branches
  1. 7.3 votingapi.drush.inc \drush_votingapi_recalculate()
  2. 7.2 votingapi.drush.inc \drush_votingapi_recalculate()

Command callback. Recalculate voting results.

File

./votingapi.drush.inc, line 162
Voting API module integration with Drush 8 and earlier.

Code

function drush_votingapi_recalculate($entity_type = 'node', $entity_id = NULL, $vote_type = 'vote') {

  // Prep some starter query objects.
  if (empty($entity_id)) {
    $votes = \Drupal::database()
      ->select('votingapi_vote', 'vv')
      ->fields('vv', [
      'entity_type',
      'entity_id',
    ])
      ->condition('entity_type', $entity_type, '=')
      ->distinct(TRUE)
      ->execute()
      ->fetchAll(PDO::FETCH_ASSOC);
    $message = dt('Rebuilt voting results for @type votes.', [
      '@type' => $entity_type,
    ]);
  }
  else {
    $votes[] = [
      'entity_type' => $entity_type,
      'entity_id' => $entity_id,
    ];
    $message = dt('Rebuilt voting results for @type id: @entity_id.', [
      '@type' => $entity_type,
      '@entity_id' => $entity_id,
    ]);
  }
  $manager = \Drupal::service('plugin.manager.votingapi.resultfunction');
  foreach ($votes as $vote) {
    $manager
      ->recalculateResults($vote['entity_type'], $vote['entity_id'], $vote_type);
  }
  drush_log($message, 'success');
}