function apply_for_role_available_roles in Apply for role 7.2
Same name and namespace in other branches
- 5 apply_for_role.module \apply_for_role_available_roles()
- 6 apply_for_role.module \apply_for_role_available_roles()
- 7 apply_for_role.module \apply_for_role_available_roles()
Return an array of the roles that are available for application by a user.
Parameters
$user: A user object.
Return value
An array keyed by role id with the role names as values.
1 call to apply_for_role_available_roles()
- apply_for_role_apply_form in ./
apply_for_role.module - Callback for the apply for role form.
File
- ./
apply_for_role.module, line 545 - Allows users to apply for roles.
Code
function apply_for_role_available_roles($user) {
// Get the complete list of roles (other than anonyous)...
$roles = user_roles(TRUE);
// ...the roles that can be applied for...
$enabled = (array) variable_get('users_apply_roles', array());
// Check that all of the enabled roles exist.
// TODO: Catch these when they are deleted.
$enabled = array_intersect_key($roles, $enabled);
// ...the roles the user has already applied for...
$applied = array();
$result = db_query('SELECT rid FROM {users_roles_apply} WHERE uid = :uid', array(
':uid' => $user->uid,
));
foreach ($result as $row) {
$applied[$row->rid] = isset($roles[$row->rid]) ? $roles[$row->rid] : t('Invalid role');
}
// ... now figure out what's left from the enabled roles list once you remove
// those they have and those they've applied for.
$used = $user->roles + $applied;
return array_diff_key($enabled, $used);
}