You are here

function advuser_multiple_role_edit in Advanced User 6.3

Same name and namespace in other branches
  1. 7.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.:

1 string reference to 'advuser_multiple_role_edit'
advuser_advuser_operations in ./advuser.module
Implement a hook_advuser_operations function.

File

./advuser.module, line 772
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_result(db_query('SELECT name FROM {role} WHERE rid = %d', $rid));
  switch ($operation) {
    case 'add_role':
      if ($selectall) {
        $sql = advuser_build_query('uid');
        $result = db_query($sql, $filter['args']);
        while ($user = db_fetch_array($result)) {
          $account = user_load($user);
          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(array(
            'uid' => (int) $uid,
          ));

          // 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 = db_fetch_array($result)) {
          $account = user_load($user);
          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(array(
            'uid' => (int) $uid,
          ));

          // 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;
  }
}