3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
20 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 defined('MOODLE_INTERNAL') ||
die();
27 * Groups not used in course or activity
29 define('NOGROUPS', 0);
32 * Groups used, users do not see other groups
34 define('SEPARATEGROUPS', 1);
37 * Groups used, students see other groups
39 define('VISIBLEGROUPS', 2);
43 * Determines if a group with a given groupid exists.
46 * @param int $groupid The groupid to check for
47 * @return bool True if the group exists, false otherwise or if an error
50 function groups_group_exists($groupid) {
52 return $DB->record_exists('groups', array('id'=>$groupid));
56 * Gets the name of a group with a specified id
59 * @param int $groupid The id of the group
60 * @return string The name of the group
62 function groups_get_group_name($groupid) {
64 return $DB->get_field('groups', 'name', array('id'=>$groupid));
68 * Gets the name of a grouping with a specified id
71 * @param int $groupingid The id of the grouping
72 * @return string The name of the grouping
74 function groups_get_grouping_name($groupingid) {
76 return $DB->get_field('groupings', 'name', array('id'=>$groupingid));
80 * Returns the groupid of a group with the name specified for the course.
81 * Group names should be unique in course
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 $data = groups_get_course_data($courseid);
90 foreach ($data->groups
as $group) {
91 if ($group->name
== $name) {
99 * Returns the groupid of a group with the idnumber specified for the course.
100 * Group idnumbers should be unique within course
103 * @param int $courseid The id of the course
104 * @param string $idnumber idnumber of group
105 * @return group object
107 function groups_get_group_by_idnumber($courseid, $idnumber) {
108 if (empty($idnumber)) {
111 $data = groups_get_course_data($courseid);
112 foreach ($data->groups
as $group) {
113 if ($group->idnumber
== $idnumber) {
121 * Returns the groupingid of a grouping with the name specified for the course.
122 * Grouping names should be unique in course
125 * @param int $courseid The id of the course
126 * @param string $name name of group (without magic quotes)
127 * @return int $groupid
129 function groups_get_grouping_by_name($courseid, $name) {
130 $data = groups_get_course_data($courseid);
131 foreach ($data->groupings
as $grouping) {
132 if ($grouping->name
== $name) {
133 return $grouping->id
;
140 * Returns the groupingid of a grouping with the idnumber specified for the course.
141 * Grouping names should be unique within course
144 * @param int $courseid The id of the course
145 * @param string $idnumber idnumber of the group
146 * @return grouping object
148 function groups_get_grouping_by_idnumber($courseid, $idnumber) {
149 if (empty($idnumber)) {
152 $data = groups_get_course_data($courseid);
153 foreach ($data->groupings
as $grouping) {
154 if ($grouping->idnumber
== $idnumber) {
162 * Get the group object
165 * @param int $groupid ID of the group.
166 * @param string $fields (default is all fields)
167 * @param int $strictness (IGNORE_MISSING - default)
168 * @return stdGlass group object
170 function groups_get_group($groupid, $fields='*', $strictness=IGNORE_MISSING
) {
172 return $DB->get_record('groups', array('id'=>$groupid), $fields, $strictness);
176 * Get the grouping object
179 * @param int $groupingid ID of the group.
180 * @param string $fields
181 * @param int $strictness (IGNORE_MISSING - default)
182 * @return stdClass group object
184 function groups_get_grouping($groupingid, $fields='*', $strictness=IGNORE_MISSING
) {
186 return $DB->get_record('groupings', array('id'=>$groupingid), $fields, $strictness);
190 * Gets array of all groups in a specified course.
193 * @param int $courseid The id of the course.
194 * @param mixed $userid optional user id or array of ids, returns only groups of the user.
195 * @param int $groupingid optional returns only groups in the specified grouping.
196 * @param string $fields
197 * @return array Returns an array of the group objects (userid field returned if array in $userid)
199 function groups_get_all_groups($courseid, $userid=0, $groupingid=0, $fields='g.*') {
202 // We need to check that we each field in the fields list belongs to the group table and that it has not being
203 // aliased. If its something else we need to avoid the cache and run the query as who knows whats going on.
205 if ($fields !== 'g.*') {
206 $fieldbits = explode(',', $fields);
207 foreach ($fieldbits as $bit) {
209 if (strpos($bit, 'g.') !== 0 or stripos($bit, ' AS ') !== false) {
210 $knownfields = false;
216 if (empty($userid) && $knownfields) {
217 // We can use the cache.
218 $data = groups_get_course_data($courseid);
219 if (empty($groupingid)) {
220 // All groups.. Easy!
221 $groups = $data->groups
;
224 foreach ($data->mappings
as $mapping) {
225 if ($mapping->groupingid
!= $groupingid) {
228 if (isset($data->groups
[$mapping->groupid
])) {
229 $groups[$mapping->groupid
] = $data->groups
[$mapping->groupid
];
233 // Yay! We could use the cache. One more query saved.
238 if (empty($userid)) {
244 list($usql, $params) = $DB->get_in_or_equal($userid);
245 $userfrom = ", {groups_members} gm";
246 $userwhere = "AND g.id = gm.groupid AND gm.userid $usql";
249 if (!empty($groupingid)) {
250 $groupingfrom = ", {groupings_groups} gg";
251 $groupingwhere = "AND g.id = gg.groupid AND gg.groupingid = ?";
252 $params[] = $groupingid;
258 array_unshift($params, $courseid);
260 return $DB->get_records_sql("SELECT $fields
261 FROM {groups} g $userfrom $groupingfrom
262 WHERE g.courseid = ? $userwhere $groupingwhere
263 ORDER BY name ASC", $params);
268 * Gets array of all groups in current user.
272 * @return array Returns an array of the group objects.
274 function groups_get_my_groups() {
276 return $DB->get_records_sql("SELECT *
277 FROM {groups_members} gm
281 ORDER BY name ASC", array($USER->id
));
285 * Returns info about user's groups in course.
288 * @param int $courseid
289 * @param int $userid $USER if not specified
290 * @return array Array[groupingid][groupid] including grouping id 0 which means all groups
292 function groups_get_user_groups($courseid, $userid=0) {
295 if (empty($userid)) {
299 $sql = "SELECT g.id, gg.groupingid
301 JOIN {groups_members} gm ON gm.groupid = g.id
302 LEFT JOIN {groupings_groups} gg ON gg.groupid = g.id
303 WHERE gm.userid = ? AND g.courseid = ?";
304 $params = array($userid, $courseid);
306 $rs = $DB->get_recordset_sql($sql, $params);
309 $rs->close(); // Not going to iterate (but exit), close rs
310 return array('0' => array());
314 $allgroups = array();
316 foreach ($rs as $group) {
317 $allgroups[$group->id
] = $group->id
;
318 if (is_null($group->groupingid
)) {
321 if (!array_key_exists($group->groupingid
, $result)) {
322 $result[$group->groupingid
] = array();
324 $result[$group->groupingid
][$group->id
] = $group->id
;
328 $result['0'] = array_keys($allgroups); // all groups
334 * Gets an array of all groupings in a specified course. This value is cached
335 * for a single course (so you can call it repeatedly for the same course
336 * without a performance penalty).
339 * @param int $courseid return all groupings from course with this courseid
340 * @return array Returns an array of the grouping objects (empty if none)
342 function groups_get_all_groupings($courseid) {
343 $data = groups_get_course_data($courseid);
344 return $data->groupings
;
348 * Determines if the user is a member of the given group.
350 * If $userid is null, use the global object.
353 * @param int $groupid The group to check for membership.
354 * @param int $userid The user to check against the group.
355 * @return bool True if the user is a member, false otherwise.
357 function groups_is_member($groupid, $userid=null) {
364 return $DB->record_exists('groups_members', array('groupid'=>$groupid, 'userid'=>$userid));
368 * Determines if current or specified is member of any active group in activity
371 * @staticvar array $cache
372 * @param cm_info $cm course module object
373 * @param int $userid id of user, null means $USER->id
374 * @return bool true if user member of at least one group used in activity
376 function groups_has_membership($cm, $userid=null) {
377 global $CFG, $USER, $DB;
379 static $cache = array();
381 if (empty($userid)) {
385 $cachekey = $userid.'|'.$cm->course
.'|'.$cm->groupingid
;
386 if (isset($cache[$cachekey])) {
387 return($cache[$cachekey]);
390 if ($cm->groupingid
) {
391 // find out if member of any group in selected activity grouping
393 FROM {groups_members} gm, {groupings_groups} gg
394 WHERE gm.userid = ? AND gm.groupid = gg.groupid AND gg.groupingid = ?";
395 $params = array($userid, $cm->groupingid
);
398 // no grouping used - check all groups in course
400 FROM {groups_members} gm, {groups} g
401 WHERE gm.userid = ? AND gm.groupid = g.id AND g.courseid = ?";
402 $params = array($userid, $cm->course
);
405 $cache[$cachekey] = $DB->record_exists_sql($sql, $params);
407 return $cache[$cachekey];
411 * Returns the users in the specified group.
414 * @param int $groupid The groupid to get the users for
415 * @param int $fields The fields to return
416 * @param int $sort optional sorting of returned users
417 * @return array|bool Returns an array of the users for the specified
418 * group or false if no users or an error returned.
420 function groups_get_members($groupid, $fields='u.*', $sort='lastname ASC') {
423 return $DB->get_records_sql("SELECT $fields
424 FROM {user} u, {groups_members} gm
425 WHERE u.id = gm.userid AND gm.groupid = ?
426 ORDER BY $sort", array($groupid));
431 * Returns the users in the specified grouping.
434 * @param int $groupingid The groupingid to get the users for
435 * @param string $fields The fields to return
436 * @param string $sort optional sorting of returned users
437 * @return array|bool Returns an array of the users for the specified
438 * group or false if no users or an error returned.
440 function groups_get_grouping_members($groupingid, $fields='u.*', $sort='lastname ASC') {
443 return $DB->get_records_sql("SELECT $fields
445 INNER JOIN {groups_members} gm ON u.id = gm.userid
446 INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid
447 WHERE gg.groupingid = ?
448 ORDER BY $sort", array($groupingid));
452 * Returns effective groupmode used in course
455 * @param stdClass $course course object.
456 * @return int group mode
458 function groups_get_course_groupmode($course) {
459 return $course->groupmode
;
463 * Returns effective groupmode used in activity, course setting
464 * overrides activity setting if groupmodeforce enabled.
466 * If $cm is an instance of cm_info it is easier to use $cm->effectivegroupmode
469 * @param cm_info|stdClass $cm the course module object. Only the ->course and ->groupmode need to be set.
470 * @param stdClass $course object optional course object to improve perf
471 * @return int group mode
473 function groups_get_activity_groupmode($cm, $course=null) {
474 if ($cm instanceof cm_info
) {
475 return $cm->effectivegroupmode
;
477 if (isset($course->id
) and $course->id
== $cm->course
) {
480 // Get course object (reuse $COURSE if possible).
481 $course = get_course($cm->course
, false);
484 return empty($course->groupmodeforce
) ?
$cm->groupmode
: $course->groupmode
;
488 * Print group menu selector for course level.
491 * @param stdClass $course course object
492 * @param mixed $urlroot return address. Accepts either a string or a moodle_url
493 * @param bool $return return as string instead of printing
494 * @return mixed void or string depending on $return param
496 function groups_print_course_menu($course, $urlroot, $return=false) {
497 global $USER, $OUTPUT;
499 if (!$groupmode = $course->groupmode
) {
507 $context = context_course
::instance($course->id
);
508 $aag = has_capability('moodle/site:accessallgroups', $context);
510 if ($groupmode == VISIBLEGROUPS
or $aag) {
511 $allowedgroups = groups_get_all_groups($course->id
, 0, $course->defaultgroupingid
);
513 $allowedgroups = groups_get_all_groups($course->id
, $USER->id
, $course->defaultgroupingid
);
516 $activegroup = groups_get_course_group($course, true, $allowedgroups);
518 $groupsmenu = array();
519 if (!$allowedgroups or $groupmode == VISIBLEGROUPS
or $aag) {
520 $groupsmenu[0] = get_string('allparticipants');
523 if ($allowedgroups) {
524 foreach ($allowedgroups as $group) {
525 $groupsmenu[$group->id
] = format_string($group->name
);
529 if ($groupmode == VISIBLEGROUPS
) {
530 $grouplabel = get_string('groupsvisible');
532 $grouplabel = get_string('groupsseparate');
535 if ($aag and $course->defaultgroupingid
) {
536 if ($grouping = groups_get_grouping($course->defaultgroupingid
)) {
537 $grouplabel = $grouplabel . ' (' . format_string($grouping->name
) . ')';
541 if (count($groupsmenu) == 1) {
542 $groupname = reset($groupsmenu);
543 $output = $grouplabel.': '.$groupname;
545 $select = new single_select(new moodle_url($urlroot), 'group', $groupsmenu, $activegroup, null, 'selectgroup');
546 $select->label
= $grouplabel;
547 $output = $OUTPUT->render($select);
550 $output = '<div class="groupselector">'.$output.'</div>';
560 * Generates html to print menu selector for course level, listing all groups.
561 * Note: This api does not do any group mode check use groups_print_course_menu() instead if you want proper checks.
563 * @param stdclass $course course object.
564 * @param string|moodle_url $urlroot return address. Accepts either a string or a moodle_url.
565 * @param bool $update set this to true to update current active group based on the group param.
566 * @param int $activegroup Change group active to this group if $update set to true.
568 * @return string html or void
570 function groups_allgroups_course_menu($course, $urlroot, $update = false, $activegroup = 0) {
571 global $SESSION, $OUTPUT, $USER;
573 $groupmode = groups_get_course_groupmode($course);
574 $context = context_course
::instance($course->id
);
575 $groupsmenu = array();
577 if (has_capability('moodle/site:accessallgroups', $context)) {
578 $groupsmenu[0] = get_string('allparticipants');
579 $allowedgroups = groups_get_all_groups($course->id
, 0, $course->defaultgroupingid
);
581 $allowedgroups = groups_get_all_groups($course->id
, $USER->id
, $course->defaultgroupingid
);
584 foreach ($allowedgroups as $group) {
585 $groupsmenu[$group->id
] = format_string($group->name
);
589 // Init activegroup array if necessary.
590 if (!isset($SESSION->activegroup
)) {
591 $SESSION->activegroup
= array();
593 if (!isset($SESSION->activegroup
[$course->id
])) {
594 $SESSION->activegroup
[$course->id
] = array(SEPARATEGROUPS
=> array(), VISIBLEGROUPS
=> array(), 'aag' => array());
596 if (empty($groupsmenu[$activegroup])) {
597 $activegroup = key($groupsmenu); // Force set to one of accessible groups.
599 $SESSION->activegroup
[$course->id
][$groupmode][$course->defaultgroupingid
] = $activegroup;
602 $grouplabel = get_string('groups');
603 if (count($groupsmenu) == 0) {
605 } else if (count($groupsmenu) == 1) {
606 $groupname = reset($groupsmenu);
607 $output = $grouplabel.': '.$groupname;
609 $select = new single_select(new moodle_url($urlroot), 'group', $groupsmenu, $activegroup, null, 'selectgroup');
610 $select->label
= $grouplabel;
611 $output = $OUTPUT->render($select);
619 * Print group menu selector for activity.
622 * @param stdClass $cm course module object
623 * @param string|moodle_url $urlroot return address that users get to if they choose an option;
624 * should include any parameters needed, e.g. "$CFG->wwwroot/mod/forum/view.php?id=34"
625 * @param bool $return return as string instead of printing
626 * @param bool $hideallparticipants If true, this prevents the 'All participants'
627 * option from appearing in cases where it normally would. This is intended for
628 * use only by activities that cannot display all groups together. (Note that
629 * selecting this option does not prevent groups_get_activity_group from
630 * returning 0; it will still do that if the user has chosen 'all participants'
631 * in another activity, or not chosen anything.)
632 * @return mixed void or string depending on $return param
634 function groups_print_activity_menu($cm, $urlroot, $return=false, $hideallparticipants=false) {
635 global $USER, $OUTPUT;
637 if ($urlroot instanceof moodle_url
) {
638 // no changes necessary
641 if (strpos($urlroot, 'http') !== 0) { // Will also work for https
642 // Display error if urlroot is not absolute (this causes the non-JS version to break)
643 debugging('groups_print_activity_menu requires absolute URL for ' .
644 '$urlroot, not <tt>' . s($urlroot) . '</tt>. Example: ' .
645 'groups_print_activity_menu($cm, $CFG->wwwroot . \'/mod/mymodule/view.php?id=13\');',
648 $urlroot = new moodle_url($urlroot);
651 if (!$groupmode = groups_get_activity_groupmode($cm)) {
659 $context = context_module
::instance($cm->id
);
660 $aag = has_capability('moodle/site:accessallgroups', $context);
662 if ($groupmode == VISIBLEGROUPS
or $aag) {
663 $allowedgroups = groups_get_all_groups($cm->course
, 0, $cm->groupingid
); // any group in grouping
665 $allowedgroups = groups_get_all_groups($cm->course
, $USER->id
, $cm->groupingid
); // only assigned groups
668 $activegroup = groups_get_activity_group($cm, true, $allowedgroups);
670 $groupsmenu = array();
671 if ((!$allowedgroups or $groupmode == VISIBLEGROUPS
or $aag) and !$hideallparticipants) {
672 $groupsmenu[0] = get_string('allparticipants');
675 if ($allowedgroups) {
676 foreach ($allowedgroups as $group) {
677 $groupsmenu[$group->id
] = format_string($group->name
);
681 if ($groupmode == VISIBLEGROUPS
) {
682 $grouplabel = get_string('groupsvisible');
684 $grouplabel = get_string('groupsseparate');
687 if ($aag and $cm->groupingid
) {
688 if ($grouping = groups_get_grouping($cm->groupingid
)) {
689 $grouplabel = $grouplabel . ' (' . format_string($grouping->name
) . ')';
693 if (count($groupsmenu) == 1) {
694 $groupname = reset($groupsmenu);
695 $output = $grouplabel.': '.$groupname;
697 $select = new single_select($urlroot, 'group', $groupsmenu, $activegroup, null, 'selectgroup');
698 $select->label
= $grouplabel;
699 $output = $OUTPUT->render($select);
702 $output = '<div class="groupselector">'.$output.'</div>';
712 * Returns group active in course, changes the group by default if 'group' page param present
715 * @param stdClass $course course bject
716 * @param bool $update change active group if group param submitted
717 * @param array $allowedgroups list of groups user may access (INTERNAL, to be used only from groups_print_course_menu())
718 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
720 function groups_get_course_group($course, $update=false, $allowedgroups=null) {
721 global $USER, $SESSION;
723 if (!$groupmode = $course->groupmode
) {
728 $context = context_course
::instance($course->id
);
729 if (has_capability('moodle/site:accessallgroups', $context)) {
733 if (!is_array($allowedgroups)) {
734 if ($groupmode == VISIBLEGROUPS
or $groupmode === 'aag') {
735 $allowedgroups = groups_get_all_groups($course->id
, 0, $course->defaultgroupingid
);
737 $allowedgroups = groups_get_all_groups($course->id
, $USER->id
, $course->defaultgroupingid
);
741 _group_verify_activegroup($course->id
, $groupmode, $course->defaultgroupingid
, $allowedgroups);
743 // set new active group if requested
744 $changegroup = optional_param('group', -1, PARAM_INT
);
745 if ($update and $changegroup != -1) {
747 if ($changegroup == 0) {
748 // do not allow changing to all groups without accessallgroups capability
749 if ($groupmode == VISIBLEGROUPS
or $groupmode === 'aag') {
750 $SESSION->activegroup
[$course->id
][$groupmode][$course->defaultgroupingid
] = 0;
754 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
755 $SESSION->activegroup
[$course->id
][$groupmode][$course->defaultgroupingid
] = $changegroup;
760 return $SESSION->activegroup
[$course->id
][$groupmode][$course->defaultgroupingid
];
764 * Returns group active in activity, changes the group by default if 'group' page param present
767 * @param stdClass $cm course module object
768 * @param bool $update change active group if group param submitted
769 * @param array $allowedgroups list of groups user may access (INTERNAL, to be used only from groups_print_activity_menu())
770 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
772 function groups_get_activity_group($cm, $update=false, $allowedgroups=null) {
773 global $USER, $SESSION;
775 if (!$groupmode = groups_get_activity_groupmode($cm)) {
780 $context = context_module
::instance($cm->id
);
781 if (has_capability('moodle/site:accessallgroups', $context)) {
785 if (!is_array($allowedgroups)) {
786 if ($groupmode == VISIBLEGROUPS
or $groupmode === 'aag') {
787 $allowedgroups = groups_get_all_groups($cm->course
, 0, $cm->groupingid
);
789 $allowedgroups = groups_get_all_groups($cm->course
, $USER->id
, $cm->groupingid
);
793 _group_verify_activegroup($cm->course
, $groupmode, $cm->groupingid
, $allowedgroups);
795 // set new active group if requested
796 $changegroup = optional_param('group', -1, PARAM_INT
);
797 if ($update and $changegroup != -1) {
799 if ($changegroup == 0) {
800 // allgroups visible only in VISIBLEGROUPS or when accessallgroups
801 if ($groupmode == VISIBLEGROUPS
or $groupmode === 'aag') {
802 $SESSION->activegroup
[$cm->course
][$groupmode][$cm->groupingid
] = 0;
806 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
807 $SESSION->activegroup
[$cm->course
][$groupmode][$cm->groupingid
] = $changegroup;
812 return $SESSION->activegroup
[$cm->course
][$groupmode][$cm->groupingid
];
816 * Gets a list of groups that the user is allowed to access within the
817 * specified activity.
820 * @param stdClass $cm Course-module
821 * @param int $userid User ID (defaults to current user)
822 * @return array An array of group objects, or false if none
824 function groups_get_activity_allowed_groups($cm,$userid=0) {
825 // Use current user by default
831 // Get groupmode for activity, taking into account course settings
832 $groupmode=groups_get_activity_groupmode($cm);
834 // If visible groups mode, or user has the accessallgroups capability,
835 // then they can access all groups for the activity...
836 $context = context_module
::instance($cm->id
);
837 if ($groupmode == VISIBLEGROUPS
or has_capability('moodle/site:accessallgroups', $context, $userid)) {
838 return groups_get_all_groups($cm->course
, 0, $cm->groupingid
);
840 // ...otherwise they can only access groups they belong to
841 return groups_get_all_groups($cm->course
, $userid, $cm->groupingid
);
846 * Determine if a course module is currently visible to a user
848 * $USER If $userid is null, use the global object.
851 * @param stdClass $cm The course module
852 * @param int $userid The user to check against the group.
853 * @return bool True if the user can view the course module, false otherwise.
855 function groups_course_module_visible($cm, $userid=null) {
858 if (empty($userid)) {
861 if (empty($CFG->enablegroupmembersonly
)) {
864 if (empty($cm->groupmembersonly
)) {
867 if (has_capability('moodle/site:accessallgroups', context_module
::instance($cm->id
), $userid) or groups_has_membership($cm, $userid)) {
874 * Determine if a given group is visible to user or not in a given context.
877 * @param int $groupid Group id to test. 0 for all groups.
878 * @param stdClass $course Course object.
879 * @param stdClass $cm Course module object.
880 * @param int $userid user id to test against. Defaults to $USER.
881 * @return boolean true if visible, false otherwise
883 function groups_group_visible($groupid, $course, $cm = null, $userid = null) {
886 if (empty($userid)) {
890 $groupmode = empty($cm) ?
groups_get_course_groupmode($course) : groups_get_activity_groupmode($cm, $course);
891 if ($groupmode == NOGROUPS ||
$groupmode == VISIBLEGROUPS
) {
892 // Groups are not used, or everything is visible, no need to go any further.
896 $context = empty($cm) ? context_course
::instance($course->id
) : context_module
::instance($cm->id
);
897 if (has_capability('moodle/site:accessallgroups', $context, $userid)) {
898 // User can see everything. Groupid = 0 is handled here as well.
900 } else if ($groupid != 0) {
901 // Group mode is separate, and user doesn't have access all groups capability. Check if user can see requested group.
902 $groups = empty($cm) ?
groups_get_all_groups($course->id
, $userid) : groups_get_activity_allowed_groups($cm, $userid);
903 if (array_key_exists($groupid, $groups)) {
904 // User can see the group.
912 * Internal method, sets up $SESSION->activegroup and verifies previous value
914 * @param int $courseid
915 * @param int|string $groupmode SEPARATEGROUPS, VISIBLEGROUPS or 'aag' (access all groups)
916 * @param int $groupingid 0 means all groups
917 * @param array $allowedgroups list of groups user can see
919 function _group_verify_activegroup($courseid, $groupmode, $groupingid, array $allowedgroups) {
920 global $SESSION, $USER;
922 // init activegroup array if necessary
923 if (!isset($SESSION->activegroup
)) {
924 $SESSION->activegroup
= array();
926 if (!array_key_exists($courseid, $SESSION->activegroup
)) {
927 $SESSION->activegroup
[$courseid] = array(SEPARATEGROUPS
=>array(), VISIBLEGROUPS
=>array(), 'aag'=>array());
930 // make sure that the current group info is ok
931 if (array_key_exists($groupingid, $SESSION->activegroup
[$courseid][$groupmode]) and !array_key_exists($SESSION->activegroup
[$courseid][$groupmode][$groupingid], $allowedgroups)) {
932 // active group does not exist anymore or is 0
933 if ($SESSION->activegroup
[$courseid][$groupmode][$groupingid] > 0 or $groupmode == SEPARATEGROUPS
) {
934 // do not do this if all groups selected and groupmode is not separate
935 unset($SESSION->activegroup
[$courseid][$groupmode][$groupingid]);
939 // set up defaults if necessary
940 if (!array_key_exists($groupingid, $SESSION->activegroup
[$courseid][$groupmode])) {
941 if ($groupmode == 'aag') {
942 $SESSION->activegroup
[$courseid][$groupmode][$groupingid] = 0; // all groups by default if user has accessallgroups
944 } else if ($allowedgroups) {
945 if ($groupmode != SEPARATEGROUPS
and $mygroups = groups_get_all_groups($courseid, $USER->id
, $groupingid)) {
946 $firstgroup = reset($mygroups);
948 $firstgroup = reset($allowedgroups);
950 $SESSION->activegroup
[$courseid][$groupmode][$groupingid] = $firstgroup->id
;
953 // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet
954 // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum)
955 $SESSION->activegroup
[$courseid][$groupmode][$groupingid] = 0;
961 * Caches group data for a particular course to speed up subsequent requests.
963 * @param int $courseid The course id to cache data for.
964 * @param cache $cache The cache if it has already been initialised. If not a new one will be created.
965 * @return stdClass A data object containing groups, groupings, and mappings.
967 function groups_cache_groupdata($courseid, cache
$cache = null) {
970 if ($cache === null) {
971 // Initialise a cache if we wern't given one.
972 $cache = cache
::make('core', 'groupdata');
975 // Get the groups that belong to the course.
976 $groups = $DB->get_records('groups', array('courseid' => $courseid), 'name ASC');
977 // Get the groupings that belong to the course.
978 $groupings = $DB->get_records('groupings', array('courseid' => $courseid), 'name ASC');
980 if (!is_array($groups)) {
984 if (!is_array($groupings)) {
985 $groupings = array();
988 if (!empty($groupings)) {
989 // Finally get the mappings between the two.
990 $mappings = $DB->get_records_list('groupings_groups', 'groupingid', array_keys($groupings), '', 'id,groupingid,groupid');
995 // Prepare the data array.
996 $data = new stdClass
;
997 $data->groups
= $groups;
998 $data->groupings
= $groupings;
999 $data->mappings
= $mappings;
1001 $cache->set($courseid, $data);
1002 // Finally return it so it can be used if desired.
1007 * Gets group data for a course.
1009 * This returns an object with the following properties:
1010 * - groups : An array of all the groups in the course.
1011 * - groupings : An array of all the groupings within the course.
1012 * - mappings : An array of group to grouping mappings.
1014 * @param int $courseid The course id to get data for.
1015 * @param cache $cache The cache if it has already been initialised. If not a new one will be created.
1018 function groups_get_course_data($courseid, cache
$cache = null) {
1019 if ($cache === null) {
1020 // Initialise a cache if we wern't given one.
1021 $cache = cache
::make('core', 'groupdata');
1023 // Try to retrieve it from the cache.
1024 $data = $cache->get($courseid);
1025 if ($data === false) {
1026 $data = groups_cache_groupdata($courseid, $cache);