You are here

function rate_bots_delete_votes in Rate 7

Delete votes from bots.

1 call to rate_bots_delete_votes()
rate_cron in ./rate.module
Implements hook_cron().

File

./rate.bots.inc, line 144

Code

function rate_bots_delete_votes() {
  $limit = variable_get(RATE_VAR_CRON_DELETE_LIMIT, 25);
  $queue = DrupalQueue::get('rate_delete_votes');
  while ($item = $queue
    ->claimItem()) {
    $ip = $item->data;
    $votes = db_select('votingapi_vote', 'v')
      ->fields('v', array(
      'vote_id',
      'entity_type',
      'entity_id',
    ))
      ->condition('v.vote_source', $ip)
      ->range(0, $limit)
      ->execute()
      ->fetchAll();
    foreach ($votes as $vote) {
      db_delete('votingapi_vote')
        ->condition('vote_id', $vote->vote_id)
        ->execute();
      votingapi_recalculate_results($vote->entity_type, $vote->entity_id, TRUE);
    }
    $queue
      ->deleteItem($item);
    if (count($votes) == $limit) {

      // There may be more votes from this IP which needs to be deleted.
      $queue
        ->createItem($ip);
    }
    $limit -= count($votes) + 1;
    if ($limit <= 0) {
      break;
    }
  }
}