/** calculate an array with acls related to user $user_id via group memberships * * this calculates the related acls for user $user_id. The results are returned * as an array keyed by acl_id. It can containt 0 or more elements. The values * of the array elements are groupname/capacity-pairs. * This routine is referenced from both {@link useraccount.class.php} * and {@link usermanager.class.php}. * * @param int $user_id the user we're looking at * @return array 0, 1 or more acl_id => groupname/capacity pairs * @ues $DB */ function calc_user_related_acls($user_id) { global $DB; $related_acls = array(); $sql = sprintf("SELECT gc.acl_id, ugc.group_id, ugc.capacity_code, g.groupname, g.full_name " . "FROM %susers_groups_capacities ugc " . "INNER JOIN %sgroups_capacities gc USING (group_id, capacity_code) " . "INNER JOIN %sgroups g USING (group_id) " . "WHERE ugc.capacity_code <> 0 AND ugc.user_id = %d " . "ORDER BY g.groupname", $DB->prefix, $DB->prefix, $DB->prefix, $user_id); if (($DBResult = $DB->query($sql)) !== FALSE) { $records = $DBResult->fetch_all_assoc('acl_id'); $DBResult->close(); foreach ($records as $acl_id => $record) { $related_acls[$acl_id] = $record['groupname'] . "/" . capacity_name($record['capacity_code']); } } else { logger("calc_related_acls(): cannot retrieve acls for user '{$user_id}': " . db_errormessage()); } return $related_acls; }
/** shortcut to retrieve the name and full name of the selected group and optionally a capacity name * * @param int $group_id identifies the group of interest * @param int $capacity_code identifies the capacity (optional) * @return array with ready-to-use name/full_name/capacity name */ function get_group_capacity_names($group_id, $capacity_code = 0) { if (($record = $this->get_group_record($group_id)) === FALSE) { $record = array('groupname' => strval($group_id), 'full_name' => strval($group_id)); } return array('{GROUP}' => $record['groupname'], '{GROUP_FULL_NAME}' => $record['full_name'], '{CAPACITY}' => capacity_name($capacity_code)); }
/** construct a list of groups still available for this user * * this constructs an array with available groups/capacities for the user $user_id * If the user is already a member of all available groups or there are no groups at all, * the list consists of a single option 'No groups available'. * * The values in this list are constructed from the primary key values of the * underlying groups_capacities table. These two numbers (group_id and capacity_code) * are separated with a colon ':' to make it easier to parse once we are to save * the values (in the table users_groups_capacities). * * The SQL-statement looks quite complex. What it does is using the table groups_capacities * as a starting point for _all_ valid (ie capacity_id != CAPACITY_NONE) combinations of * group and capacity. By left-joining the table users_groups_capacities with a very specific * ON-clause, and leaving out the column capacity_code, the resulting list consists of * all combinations of group and capacity buy without any entries that have a group of * which the user is already a member, no matter what capacity. * In other words: if a user is already a member of a group with capacity A, this user * cannot be member of the same group with capacity B. Finally, the table groups is used * to retrieve the group information such as the groupname and the active-flag. * * The resulting list is ordered by groupname and subsequently by the sort_order of the * capacity_code. However, inactive groups are sorted after the active groups so they * appear near the bottom of the list. * * @param int $user_id the user to which this list of available groups applies * @return array with available groups/capacities ready-to-use in a F_LISTBOX */ function get_options_available_groups_capacities($user_id) { global $DB; $options = array(); $sql = sprintf("SELECT gc.group_id, gc.capacity_code, g.groupname, g.full_name, g.is_active " . "FROM %sgroups_capacities gc " . "INNER JOIN %sgroups g USING (group_id) " . "LEFT JOIN %susers_groups_capacities ugc ON (gc.group_id = ugc.group_id AND ugc.user_id = %d) " . "WHERE gc.capacity_code <> 0 AND ugc.user_id IS NULL " . "ORDER BY CASE WHEN (g.is_active) THEN 0 ELSE 1 END, g.groupname, gc.sort_order", $DB->prefix, $DB->prefix, $DB->prefix, intval($user_id)); if (($DBResult = $DB->query($sql)) !== FALSE) { $records = $DBResult->fetch_all_assoc(); $DBResult->close(); foreach ($records as $record) { $key = $record['group_id'] . ":" . $record['capacity_code']; $is_inactive = db_bool_is(TRUE, $record['is_active']) ? '' : sprintf(' (%s)', t('inactive', 'admin')); $options[$key] = array('option' => $record['groupname'] . $is_inactive . " / " . capacity_name($record['capacity_code']), 'title' => $record['full_name']); } } if (empty($options)) { $options = array('0:0' => t('usermanager_user_groupadd_groupcapacity_none_available', 'admin')); } return $options; }