Index: modules/role_weights/role_weights.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/role_weights/role_weights.module,v retrieving revision 1.7 diff -u -r1.7 role_weights.module --- modules/role_weights/role_weights.module 6 Jun 2006 17:40:33 -0000 1.7 +++ modules/role_weights/role_weights.module 26 Jun 2006 20:01:02 -0000 @@ -17,6 +17,61 @@ } /** + * Implementation of hook_block(). + * + * Create a block for changing the primary role for a user session. + */ +function role_weights_block($op = 'list', $delta = 0) { + global $user; + if ($op == 'list') { + $blocks[0]['info'] = t('Change role'); + $blocks[0]['status'] = 1; + return $blocks; + } + else if ($op == 'view') { + // Only display block is user has more than one role. + if (count($user->roles) > 1) { + // Enable by default. + $block['subject'] = t('Change role'); + $block['content'] = drupal_get_form('role_weights_change_role', role_weights_change_role()); + } + return $block; + } +} + +/** + * Generate a form for changing roles. + */ +function role_weights_change_role() { + global $user; + $form = array(); + $primary = role_weights_get_user_primary(); + $form['rid'] = array( + '#type' => 'select', + '#title' => t('New role'), + '#description' => t('Select a different main role for your current session.'), + '#default_value' => $primary['rid'], + '#options' => $user->roles + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Change') + ); + return $form; +} + +/** + * Based on form input, set a session variable for primary user role. + */ +function role_weights_change_role_submit($form_id, $form_values) { + $_SESSION['role_weights_current'] = $form_values['rid']; + // Forward to the same page. This forces a full page refresh, + // so that modules acting on the new role will make their + // changes. + return $_GET['q']; +} + +/** * Implementation of hook_menu(). */ function role_weights_menu($may_cache) { @@ -81,6 +136,24 @@ } /** + * Return the primary role for a user. + * + * If the user has selected a role for the session, this + * role is returned. Otherwise, the highest of the user's + * role is returned. + * + * @param $roles + * A role array containing a users roles - $rid -> $role_name + * @return + * A role array containing the single, 'highest' role + */ +function role_weights_get_user_primary() { + global $user; + $current = $_SESSION['role_weights_current']; + return $current ? array('rid' => $current, 'name' => $user->roles[$current]) : role_weights_get_highest($user->roles); +} + +/** * Accepts a standard roles array from user object * and returns the 'highest' based on the current weights * settings.