MDL-32003 fix phpdocs in xmldb abstraction
[moodle.git] / lib / grouplib.php
blobcecba9cb05c2c3547ac9cf58904d88f57e9fcd90
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
20 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21 * @package core_group
24 defined('MOODLE_INTERNAL') || die();
26 /**
27 * Groups not used in course or activity
29 define('NOGROUPS', 0);
31 /**
32 * Groups used, users do not see other groups
34 define('SEPARATEGROUPS', 1);
36 /**
37 * Groups used, students see other groups
39 define('VISIBLEGROUPS', 2);
42 /**
43 * Determines if a group with a given groupid exists.
45 * @category group
46 * @param int $groupid The groupid to check for
47 * @return bool True if the group exists, false otherwise or if an error
48 * occurred.
50 function groups_group_exists($groupid) {
51 global $DB;
52 return $DB->record_exists('groups', array('id'=>$groupid));
55 /**
56 * Gets the name of a group with a specified id
58 * @category group
59 * @param int $groupid The id of the group
60 * @return string The name of the group
62 function groups_get_group_name($groupid) {
63 global $DB;
64 return $DB->get_field('groups', 'name', array('id'=>$groupid));
67 /**
68 * Gets the name of a grouping with a specified id
70 * @category group
71 * @param int $groupingid The id of the grouping
72 * @return string The name of the grouping
74 function groups_get_grouping_name($groupingid) {
75 global $DB;
76 return $DB->get_field('groupings', 'name', array('id'=>$groupingid));
79 /**
80 * Returns the groupid of a group with the name specified for the course.
81 * Group names should be unique in course
83 * @category group
84 * @param int $courseid The id of the course
85 * @param string $name name of group (without magic quotes)
86 * @return int $groupid
88 function groups_get_group_by_name($courseid, $name) {
89 global $DB;
90 if ($groups = $DB->get_records('groups', array('courseid'=>$courseid, 'name'=>$name))) {
91 return key($groups);
93 return false;
96 /**
97 * Returns the groupid of a group with the idnumber specified for the course.
98 * Group idnumbers should be unique within course
100 * @category group
101 * @param int $courseid The id of the course
102 * @param string $idnumber idnumber of group
103 * @return group object
105 function groups_get_group_by_idnumber($courseid, $idnumber) {
106 global $DB;
107 if (empty($idnumber)) {
108 return false;
109 } else if ($group = $DB->get_record('groups', array('courseid' => $courseid, 'idnumber' => $idnumber))) {
110 return $group;
112 return false;
116 * Returns the groupingid of a grouping with the name specified for the course.
117 * Grouping names should be unique in course
119 * @category group
120 * @param int $courseid The id of the course
121 * @param string $name name of group (without magic quotes)
122 * @return int $groupid
124 function groups_get_grouping_by_name($courseid, $name) {
125 global $DB;
126 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid, 'name'=>$name))) {
127 return key($groupings);
129 return false;
133 * Returns the groupingid of a grouping with the idnumber specified for the course.
134 * Grouping names should be unique within course
136 * @category group
137 * @param int $courseid The id of the course
138 * @param string $idnumber idnumber of the group
139 * @return grouping object
141 function groups_get_grouping_by_idnumber($courseid, $idnumber) {
142 global $DB;
143 if (empty($idnumber)) {
144 return false;
145 } else if ($grouping = $DB->get_record('groupings', array('courseid' => $courseid, 'idnumber' => $idnumber))) {
146 return $grouping;
148 return false;
152 * Get the group object
154 * @category group
155 * @param int $groupid ID of the group.
156 * @param string $fields (default is all fields)
157 * @param int $strictness (IGNORE_MISSING - default)
158 * @return stdGlass group object
160 function groups_get_group($groupid, $fields='*', $strictness=IGNORE_MISSING) {
161 global $DB;
162 return $DB->get_record('groups', array('id'=>$groupid), $fields, $strictness);
166 * Get the grouping object
168 * @category group
169 * @param int $groupingid ID of the group.
170 * @param string $fields
171 * @param int $strictness (IGNORE_MISSING - default)
172 * @return stdClass group object
174 function groups_get_grouping($groupingid, $fields='*', $strictness=IGNORE_MISSING) {
175 global $DB;
176 return $DB->get_record('groupings', array('id'=>$groupingid), $fields, $strictness);
180 * Gets array of all groups in a specified course.
182 * @category group
183 * @param int $courseid The id of the course.
184 * @param mixed $userid optional user id or array of ids, returns only groups of the user.
185 * @param int $groupingid optional returns only groups in the specified grouping.
186 * @param string $fields
187 * @return array Returns an array of the group objects (userid field returned if array in $userid)
189 function groups_get_all_groups($courseid, $userid=0, $groupingid=0, $fields='g.*') {
190 global $DB;
192 if (empty($userid)) {
193 $userfrom = "";
194 $userwhere = "";
195 $params = array();
197 } else {
198 list($usql, $params) = $DB->get_in_or_equal($userid);
199 $userfrom = ", {groups_members} gm";
200 $userwhere = "AND g.id = gm.groupid AND gm.userid $usql";
203 if (!empty($groupingid)) {
204 $groupingfrom = ", {groupings_groups} gg";
205 $groupingwhere = "AND g.id = gg.groupid AND gg.groupingid = ?";
206 $params[] = $groupingid;
207 } else {
208 $groupingfrom = "";
209 $groupingwhere = "";
212 array_unshift($params, $courseid);
214 return $DB->get_records_sql("SELECT $fields
215 FROM {groups} g $userfrom $groupingfrom
216 WHERE g.courseid = ? $userwhere $groupingwhere
217 ORDER BY name ASC", $params);
221 * Returns info about user's groups in course.
223 * @category group
224 * @param int $courseid
225 * @param int $userid $USER if not specified
226 * @return array Array[groupingid][groupid] including grouping id 0 which means all groups
228 function groups_get_user_groups($courseid, $userid=0) {
229 global $USER, $DB;
231 if (empty($userid)) {
232 $userid = $USER->id;
235 $sql = "SELECT g.id, gg.groupingid
236 FROM {groups} g
237 JOIN {groups_members} gm ON gm.groupid = g.id
238 LEFT JOIN {groupings_groups} gg ON gg.groupid = g.id
239 WHERE gm.userid = ? AND g.courseid = ?";
240 $params = array($userid, $courseid);
242 $rs = $DB->get_recordset_sql($sql, $params);
244 if (!$rs->valid()) {
245 $rs->close(); // Not going to iterate (but exit), close rs
246 return array('0' => array());
249 $result = array();
250 $allgroups = array();
252 foreach ($rs as $group) {
253 $allgroups[$group->id] = $group->id;
254 if (is_null($group->groupingid)) {
255 continue;
257 if (!array_key_exists($group->groupingid, $result)) {
258 $result[$group->groupingid] = array();
260 $result[$group->groupingid][$group->id] = $group->id;
262 $rs->close();
264 $result['0'] = array_keys($allgroups); // all groups
266 return $result;
270 * Gets an array of all groupings in a specified course. This value is cached
271 * for a single course (so you can call it repeatedly for the same course
272 * without a performance penalty).
274 * @category group
275 * @param int $courseid return all groupings from course with this courseid
276 * @return array Returns an array of the grouping objects (empty if none)
278 function groups_get_all_groupings($courseid) {
279 global $CFG, $DB, $GROUPLIB_CACHE;
281 // Use cached data if available. (Note: We only cache a single request, so
282 // as not to waste memory if processing is happening for multiple courses.)
283 if (!empty($GROUPLIB_CACHE->groupings) &&
284 $GROUPLIB_CACHE->groupings->courseid == $courseid) {
285 return $GROUPLIB_CACHE->groupings->result;
287 if (empty($GROUPLIB_CACHE)) {
288 $GROUPLIB_CACHE = new stdClass();
290 $GROUPLIB_CACHE->groupings = new stdClass();
291 $GROUPLIB_CACHE->groupings->courseid = $courseid;
292 $GROUPLIB_CACHE->groupings->result =
293 $DB->get_records('groupings', array('courseid' => $courseid), 'name ASC');
294 return $GROUPLIB_CACHE->groupings->result;
298 * Determines if the user is a member of the given group.
300 * If $userid is null, use the global object.
302 * @category group
303 * @param int $groupid The group to check for membership.
304 * @param int $userid The user to check against the group.
305 * @return bool True if the user is a member, false otherwise.
307 function groups_is_member($groupid, $userid=null) {
308 global $USER, $DB;
310 if (!$userid) {
311 $userid = $USER->id;
314 return $DB->record_exists('groups_members', array('groupid'=>$groupid, 'userid'=>$userid));
318 * Determines if current or specified is member of any active group in activity
320 * @category group
321 * @staticvar array $cache
322 * @param cm_info $cm course module object
323 * @param int $userid id of user, null means $USER->id
324 * @return bool true if user member of at least one group used in activity
326 function groups_has_membership($cm, $userid=null) {
327 global $CFG, $USER, $DB;
329 static $cache = array();
331 if (empty($userid)) {
332 $userid = $USER->id;
335 $cachekey = $userid.'|'.$cm->course.'|'.$cm->groupingid;
336 if (isset($cache[$cachekey])) {
337 return($cache[$cachekey]);
340 if ($cm->groupingid) {
341 // find out if member of any group in selected activity grouping
342 $sql = "SELECT 'x'
343 FROM {groups_members} gm, {groupings_groups} gg
344 WHERE gm.userid = ? AND gm.groupid = gg.groupid AND gg.groupingid = ?";
345 $params = array($userid, $cm->groupingid);
347 } else {
348 // no grouping used - check all groups in course
349 $sql = "SELECT 'x'
350 FROM {groups_members} gm, {groups} g
351 WHERE gm.userid = ? AND gm.groupid = g.id AND g.courseid = ?";
352 $params = array($userid, $cm->course);
355 $cache[$cachekey] = $DB->record_exists_sql($sql, $params);
357 return $cache[$cachekey];
361 * Returns the users in the specified group.
363 * @category group
364 * @param int $groupid The groupid to get the users for
365 * @param int $fields The fields to return
366 * @param int $sort optional sorting of returned users
367 * @return array|bool Returns an array of the users for the specified
368 * group or false if no users or an error returned.
370 function groups_get_members($groupid, $fields='u.*', $sort='lastname ASC') {
371 global $DB;
373 return $DB->get_records_sql("SELECT $fields
374 FROM {user} u, {groups_members} gm
375 WHERE u.id = gm.userid AND gm.groupid = ?
376 ORDER BY $sort", array($groupid));
381 * Returns the users in the specified grouping.
383 * @category group
384 * @param int $groupingid The groupingid to get the users for
385 * @param string $fields The fields to return
386 * @param string $sort optional sorting of returned users
387 * @return array|bool Returns an array of the users for the specified
388 * group or false if no users or an error returned.
390 function groups_get_grouping_members($groupingid, $fields='u.*', $sort='lastname ASC') {
391 global $DB;
393 return $DB->get_records_sql("SELECT $fields
394 FROM {user} u
395 INNER JOIN {groups_members} gm ON u.id = gm.userid
396 INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid
397 WHERE gg.groupingid = ?
398 ORDER BY $sort", array($groupingid));
402 * Returns effective groupmode used in course
404 * @category group
405 * @param stdClass $course course object.
406 * @return int group mode
408 function groups_get_course_groupmode($course) {
409 return $course->groupmode;
413 * Returns effective groupmode used in activity, course setting
414 * overrides activity setting if groupmodeforce enabled.
416 * @category group
417 * @param cm_info $cm the course module object. Only the ->course and ->groupmode need to be set.
418 * @param stdClass $course object optional course object to improve perf
419 * @return int group mode
421 function groups_get_activity_groupmode($cm, $course=null) {
422 global $COURSE, $DB;
424 // get course object (reuse COURSE if possible)
425 if (isset($course->id) and $course->id == $cm->course) {
426 //ok
427 } else if ($cm->course == $COURSE->id) {
428 $course = $COURSE;
429 } else {
430 if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
431 print_error('invalidcourseid');
435 return empty($course->groupmodeforce) ? $cm->groupmode : $course->groupmode;
439 * Print group menu selector for course level.
441 * @category group
442 * @param stdClass $course course object
443 * @param mixed $urlroot return address. Accepts either a string or a moodle_url
444 * @param bool $return return as string instead of printing
445 * @return mixed void or string depending on $return param
447 function groups_print_course_menu($course, $urlroot, $return=false) {
448 global $USER, $OUTPUT;
450 if (!$groupmode = $course->groupmode) {
451 if ($return) {
452 return '';
453 } else {
454 return;
458 $context = get_context_instance(CONTEXT_COURSE, $course->id);
459 $aag = has_capability('moodle/site:accessallgroups', $context);
461 if ($groupmode == VISIBLEGROUPS or $aag) {
462 $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
463 } else {
464 $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
467 $activegroup = groups_get_course_group($course, true, $allowedgroups);
469 $groupsmenu = array();
470 if (!$allowedgroups or $groupmode == VISIBLEGROUPS or $aag) {
471 $groupsmenu[0] = get_string('allparticipants');
474 if ($allowedgroups) {
475 foreach ($allowedgroups as $group) {
476 $groupsmenu[$group->id] = format_string($group->name);
480 if ($groupmode == VISIBLEGROUPS) {
481 $grouplabel = get_string('groupsvisible');
482 } else {
483 $grouplabel = get_string('groupsseparate');
486 if ($aag and $course->defaultgroupingid) {
487 if ($grouping = groups_get_grouping($course->defaultgroupingid)) {
488 $grouplabel = $grouplabel . ' (' . format_string($grouping->name) . ')';
492 if (count($groupsmenu) == 1) {
493 $groupname = reset($groupsmenu);
494 $output = $grouplabel.': '.$groupname;
495 } else {
496 $select = new single_select(new moodle_url($urlroot), 'group', $groupsmenu, $activegroup, null, 'selectgroup');
497 $select->label = $grouplabel;
498 $output = $OUTPUT->render($select);
501 $output = '<div class="groupselector">'.$output.'</div>';
503 if ($return) {
504 return $output;
505 } else {
506 echo $output;
511 * Print group menu selector for activity.
513 * @category group
514 * @param stdClass $cm course module object
515 * @param string|moodle_url $urlroot return address that users get to if they choose an option;
516 * should include any parameters needed, e.g. "$CFG->wwwroot/mod/forum/view.php?id=34"
517 * @param bool $return return as string instead of printing
518 * @param bool $hideallparticipants If true, this prevents the 'All participants'
519 * option from appearing in cases where it normally would. This is intended for
520 * use only by activities that cannot display all groups together. (Note that
521 * selecting this option does not prevent groups_get_activity_group from
522 * returning 0; it will still do that if the user has chosen 'all participants'
523 * in another activity, or not chosen anything.)
524 * @return mixed void or string depending on $return param
526 function groups_print_activity_menu($cm, $urlroot, $return=false, $hideallparticipants=false) {
527 global $USER, $OUTPUT;
529 if ($urlroot instanceof moodle_url) {
530 // no changes necessary
532 } else {
533 if (strpos($urlroot, 'http') !== 0) { // Will also work for https
534 // Display error if urlroot is not absolute (this causes the non-JS version to break)
535 debugging('groups_print_activity_menu requires absolute URL for ' .
536 '$urlroot, not <tt>' . s($urlroot) . '</tt>. Example: ' .
537 'groups_print_activity_menu($cm, $CFG->wwwroot . \'/mod/mymodule/view.php?id=13\');',
538 DEBUG_DEVELOPER);
540 $urlroot = new moodle_url($urlroot);
543 if (!$groupmode = groups_get_activity_groupmode($cm)) {
544 if ($return) {
545 return '';
546 } else {
547 return;
551 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
552 $aag = has_capability('moodle/site:accessallgroups', $context);
554 if ($groupmode == VISIBLEGROUPS or $aag) {
555 $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping
556 } else {
557 $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups
560 $activegroup = groups_get_activity_group($cm, true, $allowedgroups);
562 $groupsmenu = array();
563 if ((!$allowedgroups or $groupmode == VISIBLEGROUPS or $aag) and !$hideallparticipants) {
564 $groupsmenu[0] = get_string('allparticipants');
567 if ($allowedgroups) {
568 foreach ($allowedgroups as $group) {
569 $groupsmenu[$group->id] = format_string($group->name);
573 if ($groupmode == VISIBLEGROUPS) {
574 $grouplabel = get_string('groupsvisible');
575 } else {
576 $grouplabel = get_string('groupsseparate');
579 if ($aag and $cm->groupingid) {
580 if ($grouping = groups_get_grouping($cm->groupingid)) {
581 $grouplabel = $grouplabel . ' (' . format_string($grouping->name) . ')';
585 if (count($groupsmenu) == 1) {
586 $groupname = reset($groupsmenu);
587 $output = $grouplabel.': '.$groupname;
588 } else {
589 $select = new single_select($urlroot, 'group', $groupsmenu, $activegroup, null, 'selectgroup');
590 $select->label = $grouplabel;
591 $output = $OUTPUT->render($select);
594 $output = '<div class="groupselector">'.$output.'</div>';
596 if ($return) {
597 return $output;
598 } else {
599 echo $output;
604 * Returns group active in course, changes the group by default if 'group' page param present
606 * @category group
607 * @param stdClass $course course bject
608 * @param bool $update change active group if group param submitted
609 * @param array $allowedgroups list of groups user may access (INTERNAL, to be used only from groups_print_course_menu())
610 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
612 function groups_get_course_group($course, $update=false, $allowedgroups=null) {
613 global $USER, $SESSION;
615 if (!$groupmode = $course->groupmode) {
616 // NOGROUPS used
617 return false;
620 $context = get_context_instance(CONTEXT_COURSE, $course->id);
621 if (has_capability('moodle/site:accessallgroups', $context)) {
622 $groupmode = 'aag';
625 if (!is_array($allowedgroups)) {
626 if ($groupmode == VISIBLEGROUPS or $groupmode === 'aag') {
627 $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
628 } else {
629 $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
633 _group_verify_activegroup($course->id, $groupmode, $course->defaultgroupingid, $allowedgroups);
635 // set new active group if requested
636 $changegroup = optional_param('group', -1, PARAM_INT);
637 if ($update and $changegroup != -1) {
639 if ($changegroup == 0) {
640 // do not allow changing to all groups without accessallgroups capability
641 if ($groupmode == VISIBLEGROUPS or $groupmode === 'aag') {
642 $SESSION->activegroup[$course->id][$groupmode][$course->defaultgroupingid] = 0;
645 } else {
646 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
647 $SESSION->activegroup[$course->id][$groupmode][$course->defaultgroupingid] = $changegroup;
652 return $SESSION->activegroup[$course->id][$groupmode][$course->defaultgroupingid];
656 * Returns group active in activity, changes the group by default if 'group' page param present
658 * @category group
659 * @param stdClass $cm course module object
660 * @param bool $update change active group if group param submitted
661 * @param array $allowedgroups list of groups user may access (INTERNAL, to be used only from groups_print_activity_menu())
662 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
664 function groups_get_activity_group($cm, $update=false, $allowedgroups=null) {
665 global $USER, $SESSION;
667 if (!$groupmode = groups_get_activity_groupmode($cm)) {
668 // NOGROUPS used
669 return false;
672 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
673 if (has_capability('moodle/site:accessallgroups', $context)) {
674 $groupmode = 'aag';
677 if (!is_array($allowedgroups)) {
678 if ($groupmode == VISIBLEGROUPS or $groupmode === 'aag') {
679 $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid);
680 } else {
681 $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid);
685 _group_verify_activegroup($cm->course, $groupmode, $cm->groupingid, $allowedgroups);
687 // set new active group if requested
688 $changegroup = optional_param('group', -1, PARAM_INT);
689 if ($update and $changegroup != -1) {
691 if ($changegroup == 0) {
692 // allgroups visible only in VISIBLEGROUPS or when accessallgroups
693 if ($groupmode == VISIBLEGROUPS or $groupmode === 'aag') {
694 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0;
697 } else {
698 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
699 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $changegroup;
704 return $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid];
708 * Gets a list of groups that the user is allowed to access within the
709 * specified activity.
711 * @category group
712 * @param stdClass $cm Course-module
713 * @param int $userid User ID (defaults to current user)
714 * @return array An array of group objects, or false if none
716 function groups_get_activity_allowed_groups($cm,$userid=0) {
717 // Use current user by default
718 global $USER;
719 if(!$userid) {
720 $userid=$USER->id;
723 // Get groupmode for activity, taking into account course settings
724 $groupmode=groups_get_activity_groupmode($cm);
726 // If visible groups mode, or user has the accessallgroups capability,
727 // then they can access all groups for the activity...
728 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
729 if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
730 return groups_get_all_groups($cm->course, 0, $cm->groupingid);
731 } else {
732 // ...otherwise they can only access groups they belong to
733 return groups_get_all_groups($cm->course, $userid, $cm->groupingid);
738 * Determine if a course module is currently visible to a user
740 * $USER If $userid is null, use the global object.
742 * @category group
743 * @param stdClass $cm The course module
744 * @param int $userid The user to check against the group.
745 * @return bool True if the user can view the course module, false otherwise.
747 function groups_course_module_visible($cm, $userid=null) {
748 global $CFG, $USER;
750 if (empty($userid)) {
751 $userid = $USER->id;
753 if (empty($CFG->enablegroupmembersonly)) {
754 return true;
756 if (empty($cm->groupmembersonly)) {
757 return true;
759 if (has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE, $cm->id), $userid) or groups_has_membership($cm, $userid)) {
760 return true;
762 return false;
766 * Internal method, sets up $SESSION->activegroup and verifies previous value
768 * @param int $courseid
769 * @param int|string $groupmode SEPARATEGROUPS, VISIBLEGROUPS or 'aag' (access all groups)
770 * @param int $groupingid 0 means all groups
771 * @param array $allowedgroups list of groups user can see
773 function _group_verify_activegroup($courseid, $groupmode, $groupingid, array $allowedgroups) {
774 global $SESSION, $USER;
776 // init activegroup array if necessary
777 if (!isset($SESSION->activegroup)) {
778 $SESSION->activegroup = array();
780 if (!array_key_exists($courseid, $SESSION->activegroup)) {
781 $SESSION->activegroup[$courseid] = array(SEPARATEGROUPS=>array(), VISIBLEGROUPS=>array(), 'aag'=>array());
784 // make sure that the current group info is ok
785 if (array_key_exists($groupingid, $SESSION->activegroup[$courseid][$groupmode]) and !array_key_exists($SESSION->activegroup[$courseid][$groupmode][$groupingid], $allowedgroups)) {
786 // active group does not exist anymore or is 0
787 if ($SESSION->activegroup[$courseid][$groupmode][$groupingid] > 0 or $groupmode == SEPARATEGROUPS) {
788 // do not do this if all groups selected and groupmode is not separate
789 unset($SESSION->activegroup[$courseid][$groupmode][$groupingid]);
793 // set up defaults if necessary
794 if (!array_key_exists($groupingid, $SESSION->activegroup[$courseid][$groupmode])) {
795 if ($groupmode == 'aag') {
796 $SESSION->activegroup[$courseid][$groupmode][$groupingid] = 0; // all groups by default if user has accessallgroups
798 } else if ($allowedgroups) {
799 if ($groupmode != SEPARATEGROUPS and $mygroups = groups_get_all_groups($courseid, $USER->id, $groupingid)) {
800 $firstgroup = reset($mygroups);
801 } else {
802 $firstgroup = reset($allowedgroups);
804 $SESSION->activegroup[$courseid][$groupmode][$groupingid] = $firstgroup->id;
806 } else {
807 // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet
808 // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum)
809 $SESSION->activegroup[$courseid][$groupmode][$groupingid] = 0;