function advuser_multiple_role_edit in Advanced User 7.3
Same name and namespace in other branches
- 6.3 advuser.module \advuser_multiple_role_edit()
Callback function for admin mass adding/deleting a user role.
Parameters
$accounts - Not used:
$operation - add_role or remove_role:
$rid - the requested role id.:
File
- ./
advuser.module, line 773 - Advanced user module allows you to select users based on an advanced set of filtering and apply actions to block, unblock, delete or email the selected users.
Code
function advuser_multiple_role_edit($accounts, $operation, $rid) {
// The role name is not necessary as user_save() will reload the user
// object, but some modules' hook_user() may look at this first.
$advuser =& $_SESSION['advuser'];
$accounts =& $advuser['accounts'];
$selectall =& $advuser['selectall'];
$deselected =& $advuser['deselected'];
$filter = advuser_build_filter_query();
$role_name = db_select('role', 'r')
->fields('r', array(
'name',
))
->condition('r.rid', $rid, '=')
->execute()
->fetchField();
switch ($operation) {
case 'add_role':
if ($selectall) {
$sql = advuser_build_query('uid');
$result = db_query($sql, $filter['args']);
while ($user = $result
->fetchAssoc()) {
$account = user_load((int) $user['uid'], TRUE);
if ($account !== FALSE && !isset($account->roles[$rid])) {
$roles = $account->roles + array(
$rid => $role_name,
);
user_save($account, array(
'roles' => $roles,
));
}
}
$selectall = FALSE;
}
else {
foreach ($accounts as $uid) {
$account = user_load((int) $uid, TRUE);
// Skip adding the role to the user if they already have it.
if ($account !== FALSE && !isset($account->roles[$rid])) {
$roles = $account->roles + array(
$rid => $role_name,
);
user_save($account, array(
'roles' => $roles,
));
}
unset($accounts[$uid]);
}
}
break;
case 'remove_role':
if ($selectall) {
$sql = advuser_build_query('uid');
$result = db_query($sql, $filter['args']);
while ($user = $result
->fetchAssoc()) {
$account = user_load((int) $user['uid'], TRUE);
if ($account !== FALSE && isset($account->roles[$rid])) {
$roles = array_diff($account->roles, array(
$rid => $role_name,
));
user_save($account, array(
'roles' => $roles,
));
}
}
$selectall = FALSE;
}
else {
foreach ($accounts as $uid) {
$account = user_load((int) $uid, TRUE);
// Skip removing the role from the user if they already don't have it.
if ($account !== FALSE && isset($account->roles[$rid])) {
$roles = array_diff($account->roles, array(
$rid => $role_name,
));
user_save($account, array(
'roles' => $roles,
));
}
unset($accounts[$uid]);
}
}
break;
}
}