You are here

function uc_roles_cron in Ubercart 5

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

Implementation of hook_cron().

File

uc_roles/uc_roles.module, line 32
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);
  $result = db_query("SELECT * FROM {uc_roles_expirations}");
  while ($expiration = db_fetch_object($result)) {
    $user = user_load(array(
      'uid' => $expiration->uid,
    ));
    if (is_array($user->roles) && count($user->roles) > 0) {
      if (!in_array($expiration->rid, array_keys($user->roles))) {
        _role_action('delete', $user, $expiration->rid);
      }
      elseif ($expiration->expiration <= time()) {
        _role_action('revoke', $user, $expiration->rid);
        _role_email_user('revoke', $user, $expiration);
      }
      elseif ($reminder_granularity != 'never') {
        $threshold = _get_expiration_date(-$reminder_qty, $reminder_granularity, $expiration->expiration);
        if ($threshold <= time() && intval($expiration->notified) < 1) {
          _role_email_user('reminder', $user, $expiration);
          db_query("UPDATE {uc_roles_expirations} SET notified = 1 WHERE uid = %d AND rid = %d", $user->uid, $expiration->rid);
        }
      }
    }
  }
}