]> git.mjollnir.org Git - moodle.git/commitdiff
Follow-up fix for Bug MDL-8617 "Implement groupings & course modules..." internal...
authornfreear <nfreear>
Thu, 1 Mar 2007 11:37:55 +0000 (11:37 +0000)
committernfreear <nfreear>
Thu, 1 Mar 2007 11:37:55 +0000 (11:37 +0000)
group/lib/modulelib.php
group/lib/utillib.php

index 98b692e4667a2704c8e680f6bc8e1a5a3ffc2763..1458e73f73bc3a590bf4dd19e95a86c2aed566a2 100644 (file)
@@ -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
index 19dc2c3c0a2dcd00b395e57464f63a46dab24529..acd412f1f9791f7a2b77e670859de44a8f0a193f 100644 (file)
@@ -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;
 }