diff --git a/uc_roles/uc_roles.module b/uc_roles/uc_roles.module index 9739964..f54c107 100644 --- a/uc_roles/uc_roles.module +++ b/uc_roles/uc_roles.module @@ -31,7 +31,19 @@ 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}"); + $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); @@ -48,22 +60,12 @@ function uc_roles_cron() { // Remind the user about an upcoming expiration. elseif ($reminder_granularity != 'never') { - // Only if not already notified. - if (intval($expiration->notified) >= 1) { - continue; - } - - // If we're past the expiration time minus the reminder time. - $threshold = _uc_roles_get_expiration(-$reminder_qty, $reminder_granularity, $expiration->expiration); - if ($threshold <= REQUEST_TIME) { - 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(); - } + 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(); } } }