Index: role_weights.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/role_weights/role_weights.module,v retrieving revision 1.12.2.4 diff -u -r1.12.2.4 role_weights.module --- role_weights.module 18 Oct 2008 00:09:05 -0000 1.12.2.4 +++ role_weights.module 18 Oct 2008 01:15:47 -0000 @@ -176,14 +176,40 @@ * and returns the 'highest' based on the current weights * settings. * + * @TODO Perhaps deprecate this in favour of calling role_weights_get_weighted_max() with parameter. + * Included here so we don't break things in 5.x + * Leaving out a "role_weights_get_lowest" function + * * @param $roles * A role array containing a users roles - $rid -> $role_name * @return * A role id defining the users' single, 'highest' role */ function role_weights_get_highest($roles) { + return role_weights_get_weighted_max($roles, 'lightest'); +} + +/** + * Accepts a standard roles array from user object + * and returns the 'lightest' or 'heaviest' based on + * the current weights settings. + * + * @param $roles + * A role array containing a users roles - $rid -> $role_name + * @param $weight_end + * Which weight to get - 'lightest' or 'heaviest' + * @return + * The lighest or heaviest role id from $roles + */ +function role_weights_get_weighted_max($roles, $weight_end = 'lightest') { $role_weights = _role_weights_get_weights(); - asort($role_weights); + + if ($weight_end == 'lightest') { + asort($role_weights); + } + else { + arsort($role_weights); + } // Run through $roles, returning FIRST role matched foreach ($role_weights as $rid => $weight) { @@ -191,8 +217,6 @@ return $rid; } } - - return array(); } /** @@ -281,3 +305,31 @@ return $tables; } + +/** + * Token support + */ +function role_weights_token_values($type, $object = NULL, $options = array()) { + if ($type == 'user') { + $user = $object; + $lightest_role = role_weights_get_weighted_max($user->roles, 'lightest'); + $tokens['lightest-role'] = $user->roles[$lightest_role]; + $tokens['lightest-role-id'] = $lightest_role; + + $heaviest_role = role_weights_get_weighted_max($user->roles, 'heaviest'); + $tokens['heaviest-role'] = $user->roles[$heaviest_role]; + $tokens['heaviest-role-id'] = $heaviest_role; + + return $tokens; + } +} + +function role_weights_token_list($type = 'all') { + if ($type == 'user' || $type == 'all') { + $tokens['user']['lightest-role'] = t("The user's lightest role name"); + $tokens['user']['lightest-role-id'] = t("The user's lightest role id"); + $tokens['user']['heaviest-role'] = t("The user's heaviest role name"); + $tokens['user']['heaviest-role-id'] = t("The user's heaviest role id"); + return $tokens; + } +}