You are here

function apply_for_role_available_roles in Apply for role 5

Same name and namespace in other branches
  1. 6 apply_for_role.module \apply_for_role_available_roles()
  2. 7.2 apply_for_role.module \apply_for_role_available_roles()
  3. 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 User object:

Return value

array keyed by role id with the role names as values.

File

./apply_for_role.module, line 443
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());

  // ...the roles the user has already applied for...
  $applied = array();
  $result = db_query("SELECT rid FROM {users_roles_apply} WHERE uid = %d", $user->uid);
  while ($row = db_fetch_object($result)) {
    $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($enabled, $used);
}