You are here

function votingapi_cron in Voting API 8.3

Same name and namespace in other branches
  1. 5 votingapi.module \votingapi_cron()
  2. 6.2 votingapi.module \votingapi_cron()
  3. 6 votingapi.module \votingapi_cron()
  4. 7.3 votingapi.module \votingapi_cron()
  5. 7.2 votingapi.module \votingapi_cron()

Implements hook_cron().

Recalculate results for any entities that have gotten votes since the last run.

File

./votingapi.module, line 75
Contains votingapi.module.

Code

function votingapi_cron() {
  if (Drupal::config('votingapi.settings')
    ->get('calculation_schedule') == 'cron') {
    $vote_storage = \Drupal::entityTypeManager()
      ->getStorage('vote');
    $results = $vote_storage
      ->getVotesSinceMoment();
    $manager = \Drupal::service('plugin.manager.votingapi.resultfunction');
    foreach ($results as $entity) {
      $manager
        ->recalculateResults($entity['entity_type'], $entity['entity_id'], $entity['type']);
    }
    \Drupal::state()
      ->set('votingapi.last_cron', \Drupal::time()
      ->getRequestTime());
  }
}