From 454a015ac4665195612403073e00eec06f6b7798 Mon Sep 17 00:00:00 2001 From: nfreear Date: Thu, 1 Mar 2007 11:37:55 +0000 Subject: [PATCH] Follow-up fix for Bug MDL-8617 "Implement groupings & course modules..." internal (sam's patch, thank you!) merged from branch. --- group/lib/modulelib.php | 88 +++++++++++++++++++++++++++++++++++++++++ group/lib/utillib.php | 16 +++++++- 2 files changed, 103 insertions(+), 1 deletion(-) diff --git a/group/lib/modulelib.php b/group/lib/modulelib.php index 98b692e466..1458e73f73 100644 --- a/group/lib/modulelib.php +++ b/group/lib/modulelib.php @@ -338,4 +338,92 @@ function groups_m_get_members($cmid, $groupid) { return $memberids; } +/** + * Stores a current group in the user's session, if not already present. + * + * Current group applies to all modules in the current course that share + * a grouping (or use no grouping). + * + * This function allows the user to change group if they want, but it + * checks they have permissions to access the new group and calls error() + * otherwise. + * @param object $cm Course-module object + * @param int $groupmode Group mode + * @param int $changegroup If specified, user wants to change to this group + * @return Group ID + */ +function groups_m_get_and_set_current($cm, $groupmode, $changegroup=-1) { + // Check group mode is turned on + if (!$groupmode) { + return false; + } + + // Get current group and return it if no change requested + $currentgroupid = groups_m_get_current($cm); + if ($changegroup<0) { + return $currentgroupid; + } + + // Check 'all groups' access + $context = get_context_instance(CONTEXT_COURSE, $cm->course); + $allgroups = has_capability('moodle/site:accessallgroups', $context); + + // 0 is a special case for 'all groups'. + if ($changegroup==0) { + if ($groupmode!=VISIBLEGROUPS && !$allgroups) { + error('You do not have access to view all groups'); + } + } else { // Normal group specified + // Check group is in the course... + if (!groups_group_belongs_to_course($changegroup, $cm->course)) { + error('Requested group is not in this course.'); + } + // ...AND in the right grouping if required... + if ($cm->groupingid && !groups_belongs_to_grouping($changegroup, $cm->groupingid)) { + print_object($cm); + print_object(groups_get_group($changegroup)); + error('Requested group is not in this grouping.'); + } + // ...AND user has access to all groups, or it's in visible groups mode, or + // user is a member. + if (!$allgroups && + $groupmode != VISIBLEGROUPS && !groups_is_member($changegroup)) { + } + } + // OK, now remember this group in session + global $SESSION; + $SESSION->currentgroupinggroup[$cm->course][$cm->groupingid] = $changegroup; + return $changegroup; +} + +/** + * Obtains the current group (see groups_m_get_and_set_current) either as an ID or object. + * @param object $cm Course-module object + * @param bool $full If true, returns group object rather than ID + * @return mixed Group ID (default) or object + */ +function groups_m_get_current($cm, $full=false) { + global $SESSION; + if (isset($SESSION->currentgroupinggroup[$cm->course][$cm->groupingid])) { + $currentgroup = $SESSION->currentgroupinggroup[$cm->course][$cm->groupingid]; + } else { + global $USER; + if ($cm->groupingid) { + $mygroupids=groups_get_groups_for_user_in_grouping($USER->id, $cm->groupingid); + } else { + $mygroupids=groups_get_groups_for_user($USER->id, $cm->course); + } + if (!$mygroupids) { + return false; + } + $currentgroup = array_shift($mygroupids); + $SESSION->currentgroupinggroup[$cm->course][$cm->groupingid]=$currentgroup; + } + if ($full) { + return groups_groupid_to_group($currentgroup); + } else { + return $currentgroup; + } +} + ?> \ No newline at end of file diff --git a/group/lib/utillib.php b/group/lib/utillib.php index 19dc2c3c0a..acd412f1f9 100644 --- a/group/lib/utillib.php +++ b/group/lib/utillib.php @@ -189,8 +189,13 @@ function groups_groupids_to_groups($groupids, $courseid=false, $alldata=false) { /** * Get a sorted array of group-id/display-name objects. + * @param array $groupids Array of group IDs + * @param bool $justnames Return names only as values, not objects. Needed + * for print_group_menu in weblib + * @return array If $justnames is set, returns an array of id=>name. Otherwise + * returns an array without specific keys of objects containing id, name */ -function groups_groupids_to_group_names($groupids) { +function groups_groupids_to_group_names($groupids, $justnames=false) { if (! $groupids) { return array(); } @@ -217,6 +222,15 @@ function groups_groupids_to_group_names($groupids) { $gname->name = $name; $group_names[] = $gname; }*/ + + if ($justnames) { + $namesonly = array(); + foreach ($group_names as $id => $object) { + $namesonly[$object->id] = $object->name; + } + return $namesonly; + } + return $group_names; } -- 2.39.5