You are here

function uc_roles_cron in Ubercart 7.3

Same name and namespace in other branches
  1. 5 uc_roles/uc_roles.module \uc_roles_cron()
  2. 6.2 uc_roles/uc_roles.module \uc_roles_cron()

Implements hook_cron().

File

uc_roles/uc_roles.module, line 29
Grants roles upon accepted payment of products.

Code

function uc_roles_cron() {
  $reminder_granularity = variable_get('uc_roles_reminder_granularity', 'never');
  $reminder_qty = variable_get('uc_roles_reminder_length', NULL);
  $query = db_select('uc_roles_expirations', 'e')
    ->fields('e');
  $condition = db_or()
    ->condition('e.expiration', REQUEST_TIME, '<=');
  if ($reminder_granularity != 'never') {
    $condition
      ->condition(db_and()
      ->isNull('e.notified')
      ->condition('e.expiration', _uc_roles_get_expiration($reminder_qty, $reminder_granularity, REQUEST_TIME), '<='));
  }
  $query
    ->condition($condition);
  $result = $query
    ->execute();
  foreach ($result as $expiration) {
    $account = user_load($expiration->uid);

    // Cleanup if user or role was deleted already.
    if (!$account || !in_array($expiration->rid, array_keys($account->roles))) {
      uc_roles_delete($expiration, $expiration->rid, TRUE);
    }
    elseif ($expiration->expiration <= REQUEST_TIME) {
      rules_invoke_event('uc_roles_notify_revoke', $account, $expiration);
      uc_roles_revoke($account, $expiration->rid);
    }
    elseif ($reminder_granularity != 'never') {
      rules_invoke_event('uc_roles_notify_reminder', $account, $expiration);
      db_update('uc_roles_expirations')
        ->fields(array(
        'notified' => 1,
      ))
        ->condition('uid', $account->uid)
        ->condition('rid', $expiration->rid)
        ->execute();
    }
  }
}