You are here

function drush_votingapi_recalculate in Voting API 7.2

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

Command callback. Recalculate voting results.

File

./votingapi.drush.inc, line 84
Drush commands to generate votingapi votes, recalculate results for existing votes, or flush VotingAPI data entirely.

Code

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

  // Prep some starter query objects.
  $data = array();
  if (empty($entity_id)) {
    $votes = db_select('votingapi_vote', 'vv')
      ->fields('vv', array(
      'entity_type',
      'entity_id',
    ))
      ->condition('entity_type', $entity_type, '=')
      ->distinct(TRUE)
      ->execute()
      ->fetchAll(PDO::FETCH_ASSOC);
    $message = t('Rebuilt voting results for @type votes.', array(
      '@type' => $entity_type,
    ));
  }
  else {
    $votes[] = array(
      'entity_type' => $entity_type,
      'entity_id' => $entity_id,
    );
    $message = t('Rebuilt all voting results.');
  }
  foreach ($votes as $vote) {
    votingapi_recalculate_results($vote['entity_type'], $vote['entity_id'], TRUE);
  }
  drush_log($message, 'success');
}