You are here

function _votingapi_cast_votes in Voting API 8.3

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

Utility function to generate votes on a node by a set of users.

1 call to _votingapi_cast_votes()
votingapi_generate_votes in ./votingapi.drush.inc
Utility function to generate votes.

File

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

Code

function _votingapi_cast_votes($entity_type, $entity_id, $timestamp = 0, array $uids = [], $style = 'percent') {
  foreach ($uids as $uid) {
    $request_time = \Drupal::time()
      ->getRequestTime();
    $value = $style === 'points' ? rand(0, 1) ? 1 : -1 : mt_rand(1, 5) * 20;
    $vote = Vote::create([
      'type' => 'vote',
    ]);
    $vote
      ->setVotedEntityId($entity_id);
    $vote
      ->setVotedEntityType($entity_type);
    $vote
      ->setOwnerId($uid);
    $vote
      ->setCreatedTime($request_time - mt_rand(0, $timestamp));
    $vote
      ->setValueType($style);
    $vote
      ->setValue($value);
    $vote
      ->save();
  }
}