function role_expire_cron in Role Expire 7
Same name and namespace in other branches
- 8 role_expire.module \role_expire_cron()
- 6 role_expire.module \role_expire_cron()
- 2.x role_expire.module \role_expire_cron()
Implements hook_cron().
File
- ./
role_expire.module, line 531 - Role Expire module
Code
function role_expire_cron() {
if ($expires = role_expire_get_expired()) {
$roles = _role_expire_get_role();
foreach ($expires as $expire) {
// Remove the role expiration record from the role_expires table.
role_expire_delete_record($expire->uid, $expire->rid);
// Remove the role from the user.
// TODO Convert "user_load" to "user_load_multiple" if "$expire['uid']" is other than a uid.
// To return a single user object, wrap "user_load_multiple" with "array_shift" or equivalent.
// Example: array_shift(user_load_multiple(array(), $expire['uid']))
$account = user_load($expire->uid);
// If the account *does* exist, update it.
if (!empty($account)) {
// Assign a new role after expiration if requested given configuration.
$new_roles = role_expire_get_roles_after_expiration();
if (!empty($new_roles) && isset($new_roles[$expire->rid]) && $new_roles[$expire->rid] != 0) {
$new_rid = $new_roles[$expire->rid];
$new_role = user_role_load($new_rid);
$account->roles[$new_rid] = $new_role->name;
role_expire_process_default_role_duration_for_user($new_rid, $account->uid);
}
$edit = $account->roles;
unset($edit[$expire->rid]);
// In the documentation for the role_expire implementation of hook_user we
// state to use $category = 'account'. We don't do that here because
// that would cause the delete to occur twice.
user_save($account, array(
'roles' => $edit,
), NULL);
// Handle the bizarre case of role_expire not being a valid role.
$role_name = isset($roles[$expire->rid]) ? $roles[$expire->rid] : t('-unset-');
watchdog('role expire', 'Removed role @role from user @account.', array(
'@role' => $role_name,
'@account' => $account->name,
));
if (module_exists('rules')) {
rules_invoke_event('role_expire_rules_event_role_expires', $account, $role_name);
}
}
else {
// The account doesn't exist. Database altered outside of Drupal.
// Throw a warning message.
watchdog('role expire', 'Data integrity warning: Role_expire table updated, but no user with uid @uid.', array(
'@uid' => $expire->uid,
), WATCHDOG_WARNING);
}
}
}
}