You are here

function votingapi_generate_votes in Voting API 8.3

Same name and namespace in other branches
  1. 6.2 votingapi.admin.inc \votingapi_generate_votes()
  2. 7.3 votingapi.drush.inc \votingapi_generate_votes()
  3. 7.2 votingapi.devel.inc \votingapi_generate_votes()

Utility function to generate votes.

1 call to votingapi_generate_votes()
drush_votingapi_generate_votes in ./votingapi.drush.inc
Command callback. Generate a number of votes.

File

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

Code

function votingapi_generate_votes($entity_type = 'node', $vote_type = 'percent', $options = []) {
  $options += [
    'age' => 36000,
    'node_types' => [],
    'kill_votes' => FALSE,
  ];
  if (!empty($options['kill_votes'])) {
    $cache = \Drupal::database()
      ->delete('votingapi_result')
      ->condition('entity_type', $entity_type)
      ->execute();
    $votes = \Drupal::database()
      ->delete('votingapi_vote')
      ->condition('entity_type', $entity_type)
      ->execute();
  }
  $uids = \Drupal::entityQuery('user')
    ->condition('status', 1)
    ->execute();
  $query = \Drupal::database()
    ->select($entity_type, 'e')
    ->fields('e', [
    'nid',
  ]);
  if ($entity_type == 'node' && !empty($options['types'])) {
    $query
      ->condition('e.type', $options['types'], 'IN');
  }
  $results = $query
    ->execute()
    ->fetchAll(PDO::FETCH_ASSOC);
  foreach ($results as $entity) {
    _votingapi_cast_votes($entity_type, $entity['nid'], $options['age'], $uids, $vote_type);
  }
}