MDL-20636 Fix some minor editing problems, and a bunch of coding style.
[moodle.git] / lib / grouplib.php
blob17cba0375d35fb51650cf02334ceddde33080171
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
22 * @subpackage group
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Groups not used in course or activity
30 define('NOGROUPS', 0);
32 /**
33 * Groups used, users do not see other groups
35 define('SEPARATEGROUPS', 1);
37 /**
38 * Groups used, students see other groups
40 define('VISIBLEGROUPS', 2);
43 /**
44 * Determines if a group with a given groupid exists.
46 * @global object
47 * @param int $groupid The groupid to check for
48 * @return boolean True if the group exists, false otherwise or if an error
49 * occurred.
51 function groups_group_exists($groupid) {
52 global $DB;
53 return $DB->record_exists('groups', array('id'=>$groupid));
56 /**
57 * Gets the name of a group with a specified id
59 * @global object
60 * @param int $groupid The id of the group
61 * @return string The name of the group
63 function groups_get_group_name($groupid) {
64 global $DB;
65 return $DB->get_field('groups', 'name', array('id'=>$groupid));
68 /**
69 * Gets the name of a grouping with a specified id
71 * @global object
72 * @param int $groupingid The id of the grouping
73 * @return string The name of the grouping
75 function groups_get_grouping_name($groupingid) {
76 global $DB;
77 return $DB->get_field('groupings', 'name', array('id'=>$groupingid));
80 /**
81 * Returns the groupid of a group with the name specified for the course.
82 * Group names should be unique in course
84 * @global object
85 * @param int $courseid The id of the course
86 * @param string $name name of group (without magic quotes)
87 * @return int $groupid
89 function groups_get_group_by_name($courseid, $name) {
90 global $DB;
91 if ($groups = $DB->get_records('groups', array('courseid'=>$courseid, 'name'=>$name))) {
92 return key($groups);
94 return false;
97 /**
98 * Returns the groupingid of a grouping with the name specified for the course.
99 * Grouping names should be unique in course
101 * @global object
102 * @param int $courseid The id of the course
103 * @param string $name name of group (without magic quotes)
104 * @return int $groupid
106 function groups_get_grouping_by_name($courseid, $name) {
107 global $DB;
108 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid, 'name'=>$name))) {
109 return key($groupings);
111 return false;
115 * Get the group object
117 * @param int $groupid ID of the group.
118 * @return object group object
120 function groups_get_group($groupid, $fields='*', $strictness=IGNORE_MISSING) {
121 global $DB;
122 return $DB->get_record('groups', array('id'=>$groupid), $fields, $strictness);
126 * Get the grouping object
128 * @param int $groupingid ID of the group.
129 * @param string $fields
130 * @return object group object
132 function groups_get_grouping($groupingid, $fields='*', $strictness=IGNORE_MISSING) {
133 global $DB;
134 return $DB->get_record('groupings', array('id'=>$groupingid), $fields, $strictness);
138 * Gets array of all groups in a specified course.
140 * @param int $courseid The id of the course.
141 * @param mixed $userid optional user id or array of ids, returns only groups of the user.
142 * @param int $groupingid optional returns only groups in the specified grouping.
143 * @param string $fields
144 * @return array|bool Returns an array of the group objects or false if no records
145 * or an error occurred. (userid field returned if array in $userid)
147 function groups_get_all_groups($courseid, $userid=0, $groupingid=0, $fields='g.*') {
148 global $CFG, $DB;
150 if (empty($userid)) {
151 $userfrom = "";
152 $userwhere = "";
153 $params = array();
155 } else {
156 list($usql, $params) = $DB->get_in_or_equal($userid);
157 $userfrom = ", {groups_members} gm";
158 $userwhere = "AND g.id = gm.groupid AND gm.userid $usql";
161 if (!empty($groupingid)) {
162 $groupingfrom = ", {groupings_groups} gg";
163 $groupingwhere = "AND g.id = gg.groupid AND gg.groupingid = ?";
164 $params[] = $groupingid;
165 } else {
166 $groupingfrom = "";
167 $groupingwhere = "";
170 array_unshift($params, $courseid);
172 return $DB->get_records_sql("SELECT $fields
173 FROM {groups} g $userfrom $groupingfrom
174 WHERE g.courseid = ? $userwhere $groupingwhere
175 ORDER BY name ASC", $params);
179 * Returns info about user's groups in course.
181 * @global object
182 * @global object
183 * @global object
184 * @param int $courseid
185 * @param int $userid $USER if not specified
186 * @return array Array[groupingid][groupid] including grouping id 0 which means all groups
188 function groups_get_user_groups($courseid, $userid=0) {
189 global $CFG, $USER, $DB;
191 if (empty($userid)) {
192 $userid = $USER->id;
195 $sql = "SELECT g.id, gg.groupingid
196 FROM {groups} g
197 JOIN {groups_members} gm ON gm.groupid = g.id
198 LEFT JOIN {groupings_groups} gg ON gg.groupid = g.id
199 WHERE gm.userid = ? AND g.courseid = ?";
200 $params = array($userid, $courseid);
202 $rs = $DB->get_recordset_sql($sql, $params);
204 if (!$rs->valid()) {
205 $rs->close(); // Not going to iterate (but exit), close rs
206 return array('0' => array());
209 $result = array();
210 $allgroups = array();
212 foreach ($rs as $group) {
213 $allgroups[$group->id] = $group->id;
214 if (is_null($group->groupingid)) {
215 continue;
217 if (!array_key_exists($group->groupingid, $result)) {
218 $result[$group->groupingid] = array();
220 $result[$group->groupingid][$group->id] = $group->id;
222 $rs->close();
224 $result['0'] = array_keys($allgroups); // all groups
226 return $result;
230 * Gets array of all groupings in a specified course.
232 * @global object
233 * @global object
234 * @param int $courseid return only groupings in this with this courseid
235 * @return array|bool Returns an array of the grouping objects or false if no records
236 * or an error occurred.
238 function groups_get_all_groupings($courseid) {
239 global $CFG, $DB;
241 return $DB->get_records_sql("SELECT *
242 FROM {groupings}
243 WHERE courseid = ?
244 ORDER BY name ASC", array($courseid));
250 * Determines if the user is a member of the given group.
252 * If $userid is null, use the global object.
254 * @global object
255 * @global object
256 * @param int $groupid The group to check for membership.
257 * @param int $userid The user to check against the group.
258 * @return boolean True if the user is a member, false otherwise.
260 function groups_is_member($groupid, $userid=null) {
261 global $USER, $DB;
263 if (!$userid) {
264 $userid = $USER->id;
267 return $DB->record_exists('groups_members', array('groupid'=>$groupid, 'userid'=>$userid));
271 * Determines if current or specified is member of any active group in activity
273 * @global object
274 * @global object
275 * @global object
276 * @staticvar array $cache
277 * @param object $cm coruse module object
278 * @param int $userid id of user, null menas $USER->id
279 * @return booelan true if user member of at least one group used in activity
281 function groups_has_membership($cm, $userid=null) {
282 global $CFG, $USER, $DB;
284 static $cache = array();
286 if (empty($userid)) {
287 $userid = $USER->id;
290 $cachekey = $userid.'|'.$cm->course.'|'.$cm->groupingid;
291 if (isset($cache[$cachekey])) {
292 return($cache[$cachekey]);
295 if ($cm->groupingid) {
296 // find out if member of any group in selected activity grouping
297 $sql = "SELECT 'x'
298 FROM {groups_members} gm, {groupings_groups} gg
299 WHERE gm.userid = ? AND gm.groupid = gg.groupid AND gg.groupingid = ?";
300 $params = array($userid, $cm->groupingid);
302 } else {
303 // no grouping used - check all groups in course
304 $sql = "SELECT 'x'
305 FROM {groups_members} gm, {groups} g
306 WHERE gm.userid = ? AND gm.groupid = g.id AND g.courseid = ?";
307 $params = array($userid, $cm->course);
310 $cache[$cachekey] = $DB->record_exists_sql($sql, $params);
312 return $cache[$cachekey];
316 * Returns the users in the specified group.
318 * @global object
319 * @param int $groupid The groupid to get the users for
320 * @param int $fields The fields to return
321 * @param int $sort optional sorting of returned users
322 * @return array|bool Returns an array of the users for the specified
323 * group or false if no users or an error returned.
325 function groups_get_members($groupid, $fields='u.*', $sort='lastname ASC') {
326 global $DB;
328 return $DB->get_records_sql("SELECT $fields
329 FROM {user} u, {groups_members} gm
330 WHERE u.id = gm.userid AND gm.groupid = ?
331 ORDER BY $sort", array($groupid));
336 * Returns the users in the specified grouping.
338 * @global object
339 * @param int $groupingid The groupingid to get the users for
340 * @param int $fields The fields to return
341 * @param int $sort optional sorting of returned users
342 * @return array|bool Returns an array of the users for the specified
343 * group or false if no users or an error returned.
345 function groups_get_grouping_members($groupingid, $fields='u.*', $sort='lastname ASC') {
346 global $DB;
348 return $DB->get_records_sql("SELECT $fields
349 FROM {user} u
350 INNER JOIN {groups_members} gm ON u.id = gm.userid
351 INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid
352 WHERE gg.groupingid = ?
353 ORDER BY $sort", array($groupingid));
357 * Returns effective groupmode used in course
359 * @return integer group mode
361 function groups_get_course_groupmode($course) {
362 return $course->groupmode;
366 * Returns effective groupmode used in activity, course setting
367 * overrides activity setting if groupmodeforce enabled.
369 * @global object
370 * @global object
371 * @param object $cm the course module object. Only the ->course and ->groupmode need to be set.
372 * @param object $course object optional course object to improve perf
373 * @return integer group mode
375 function groups_get_activity_groupmode($cm, $course=null) {
376 global $COURSE, $DB;
378 // get course object (reuse COURSE if possible)
379 if (isset($course->id) and $course->id == $cm->course) {
380 //ok
381 } else if ($cm->course == $COURSE->id) {
382 $course = $COURSE;
383 } else {
384 if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
385 print_error('invalidcourseid');
389 return empty($course->groupmodeforce) ? $cm->groupmode : $course->groupmode;
393 * Print group menu selector for course level.
395 * @global object
396 * @global object
397 * @param object $course course object
398 * @param string $urlroot return address
399 * @param boolean $return return as string instead of printing
400 * @return mixed void or string depending on $return param
402 function groups_print_course_menu($course, $urlroot, $return=false) {
403 global $CFG, $USER, $SESSION, $OUTPUT;
405 if (!$groupmode = $course->groupmode) {
406 if ($return) {
407 return '';
408 } else {
409 return;
413 $context = get_context_instance(CONTEXT_COURSE, $course->id);
414 if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
415 $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
416 // detect changes related to groups and fix active group
417 if (!empty($SESSION->activegroup[$course->id][VISIBLEGROUPS][0])) {
418 if (!array_key_exists($SESSION->activegroup[$course->id][VISIBLEGROUPS][0], $allowedgroups)) {
419 // active does not exist anymore
420 unset($SESSION->activegroup[$course->id][VISIBLEGROUPS][0]);
423 if (!empty($SESSION->activegroup[$course->id]['aag'][0])) {
424 if (!array_key_exists($SESSION->activegroup[$course->id]['aag'][0], $allowedgroups)) {
425 // active group does not exist anymore
426 unset($SESSION->activegroup[$course->id]['aag'][0]);
430 } else {
431 $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
432 // detect changes related to groups and fix active group
433 if (isset($SESSION->activegroup[$course->id][SEPARATEGROUPS][0])) {
434 if ($SESSION->activegroup[$course->id][SEPARATEGROUPS][0] == 0) {
435 if ($allowedgroups) {
436 // somebody must have assigned at least one group, we can select it now - yay!
437 unset($SESSION->activegroup[$course->id][SEPARATEGROUPS][0]);
439 } else {
440 if (!array_key_exists($SESSION->activegroup[$course->id][SEPARATEGROUPS][0], $allowedgroups)) {
441 // active group not allowed or does not exist anymore
442 unset($SESSION->activegroup[$course->id][SEPARATEGROUPS][0]);
448 $activegroup = groups_get_course_group($course, true);
450 $groupsmenu = array();
451 if (!$allowedgroups or $groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
452 $groupsmenu[0] = get_string('allparticipants');
455 if ($allowedgroups) {
456 foreach ($allowedgroups as $group) {
457 $groupsmenu[$group->id] = format_string($group->name);
461 if ($groupmode == VISIBLEGROUPS) {
462 $grouplabel = get_string('groupsvisible');
463 } else {
464 $grouplabel = get_string('groupsseparate');
467 if (count($groupsmenu) == 1) {
468 $groupname = reset($groupsmenu);
469 $output = $grouplabel.': '.$groupname;
470 } else {
471 $select = new single_select(new moodle_url($urlroot), 'group', $groupsmenu, $activegroup, null, 'selectgroup');
472 $select->label = $grouplabel;
473 $output = $OUTPUT->render($select);
476 $output = '<div class="groupselector">'.$output.'</div>';
478 if ($return) {
479 return $output;
480 } else {
481 echo $output;
486 * Print group menu selector for activity.
488 * @global object
489 * @global object
490 * @global object
491 * @param object $cm course module object
492 * @param string $urlroot return address that users get to if they choose an option;
493 * should include any parameters needed, e.g. "$CFG->wwwroot/mod/forum/view.php?id=34"
494 * @param boolean $return return as string instead of printing
495 * @param boolean $hideallparticipants If true, this prevents the 'All participants'
496 * option from appearing in cases where it normally would. This is intended for
497 * use only by activities that cannot display all groups together. (Note that
498 * selecting this option does not prevent groups_get_activity_group from
499 * returning 0; it will still do that if the user has chosen 'all participants'
500 * in another activity, or not chosen anything.)
501 * @return mixed void or string depending on $return param
503 function groups_print_activity_menu($cm, $urlroot, $return=false, $hideallparticipants=false) {
504 global $CFG, $USER, $SESSION, $OUTPUT;
506 // Display error if urlroot is not absolute (this causes the non-JS version
507 // to break)
508 if (strpos($urlroot, 'http') !== 0) { // Will also work for https
509 debugging('groups_print_activity_menu requires absolute URL for ' .
510 '$urlroot, not <tt>' . s($urlroot) . '</tt>. Example: ' .
511 'groups_print_activity_menu($cm, $CFG->wwwroot . \'/mod/mymodule/view.php?id=13\');',
512 DEBUG_DEVELOPER);
515 if (!$groupmode = groups_get_activity_groupmode($cm)) {
516 if ($return) {
517 return '';
518 } else {
519 return;
523 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
524 if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
525 $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping (all if groupings not used)
526 // detect changes related to groups and fix active group
527 if (!empty($SESSION->activegroup[$cm->course][VISIBLEGROUPS][$cm->groupingid])) {
528 if (!array_key_exists($SESSION->activegroup[$cm->course][VISIBLEGROUPS][$cm->groupingid], $allowedgroups)) {
529 // active group does not exist anymore
530 unset($SESSION->activegroup[$cm->course][VISIBLEGROUPS][$cm->groupingid]);
533 if (!empty($SESSION->activegroup[$cm->course]['aag'][$cm->groupingid])) {
534 if (!array_key_exists($SESSION->activegroup[$cm->course]['aag'][$cm->groupingid], $allowedgroups)) {
535 // active group does not exist anymore
536 unset($SESSION->activegroup[$cm->course]['aag'][$cm->groupingid]);
540 } else {
541 $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups
542 // detect changes related to groups and fix active group
543 if (isset($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid])) {
544 if ($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid] == 0) {
545 if ($allowedgroups) {
546 // somebody must have assigned at least one group, we can select it now - yay!
547 unset($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid]);
549 } else {
550 if (!array_key_exists($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid], $allowedgroups)) {
551 // active group not allowed or does not exist anymore
552 unset($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid]);
558 $activegroup = groups_get_activity_group($cm, true);
560 $groupsmenu = array();
561 if ((!$allowedgroups or $groupmode == VISIBLEGROUPS or
562 has_capability('moodle/site:accessallgroups', $context)) and !$hideallparticipants) {
563 $groupsmenu[0] = get_string('allparticipants');
566 if ($allowedgroups) {
567 foreach ($allowedgroups as $group) {
568 $groupsmenu[$group->id] = format_string($group->name);
572 if ($groupmode == VISIBLEGROUPS) {
573 $grouplabel = get_string('groupsvisible');
574 } else {
575 $grouplabel = get_string('groupsseparate');
578 if (count($groupsmenu) == 1) {
579 $groupname = reset($groupsmenu);
580 $output = $grouplabel.': '.$groupname;
581 } else {
582 $select = new single_select(new moodle_url($urlroot), 'group', $groupsmenu, $activegroup, null, 'selectgroup');
583 $select->label = $grouplabel;
584 $output = $OUTPUT->render($select);
587 $output = '<div class="groupselector">'.$output.'</div>';
589 if ($return) {
590 return $output;
591 } else {
592 echo $output;
597 * Returns group active in course, changes the group by default if 'group' page param present
599 * @global object
600 * @global object
601 * @global object
602 * @param object $course course bject
603 * @param boolean $update change active group if group param submitted
604 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
606 function groups_get_course_group($course, $update=false) {
607 global $CFG, $USER, $SESSION;
609 if (!$groupmode = $course->groupmode) {
610 // NOGROUPS used
611 return false;
614 // init activegroup array
615 if (!isset($SESSION->activegroup)) {
616 $SESSION->activegroup = array();
618 if (!array_key_exists($course->id, $SESSION->activegroup)) {
619 $SESSION->activegroup[$course->id] = array(SEPARATEGROUPS=>array(), VISIBLEGROUPS=>array(), 'aag'=>array());
622 $context = get_context_instance(CONTEXT_COURSE, $course->id);
623 if (has_capability('moodle/site:accessallgroups', $context)) {
624 $groupmode = 'aag';
627 // grouping used the first time - add first user group as default
628 if (!array_key_exists(0, $SESSION->activegroup[$course->id][$groupmode])) {
629 if ($groupmode == 'aag') {
630 $SESSION->activegroup[$course->id][$groupmode][0] = 0; // all groups by default if user has accessallgroups
632 } else if ($usergroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid)) {
633 $firstgroup = reset($usergroups);
634 $SESSION->activegroup[$course->id][$groupmode][0] = $firstgroup->id;
636 } else {
637 // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet
638 // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum)
639 $SESSION->activegroup[$course->id][$groupmode][0] = 0;
643 // set new active group if requested
644 $changegroup = optional_param('group', -1, PARAM_INT);
645 if ($update and $changegroup != -1) {
647 if ($changegroup == 0) {
648 // do not allow changing to all groups without accessallgroups capability
649 if ($groupmode == VISIBLEGROUPS or $groupmode == 'aag') {
650 $SESSION->activegroup[$course->id][$groupmode][0] = 0;
653 } else {
654 // first make list of allowed groups
655 if ($groupmode == VISIBLEGROUPS or $groupmode == 'aag') {
656 $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
657 } else {
658 $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
661 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
662 $SESSION->activegroup[$course->id][$groupmode][0] = $changegroup;
667 return $SESSION->activegroup[$course->id][$groupmode][0];
671 * Returns group active in activity, changes the group by default if 'group' page param present
673 * @global object
674 * @global object
675 * @global object
676 * @param object $cm course module object
677 * @param boolean $update change active group if group param submitted
678 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
680 function groups_get_activity_group($cm, $update=false) {
681 global $CFG, $USER, $SESSION;
683 if (!$groupmode = groups_get_activity_groupmode($cm)) {
684 // NOGROUPS used
685 return false;
688 // init activegroup array
689 if (!isset($SESSION->activegroup)) {
690 $SESSION->activegroup = array();
692 if (!array_key_exists($cm->course, $SESSION->activegroup)) {
693 $SESSION->activegroup[$cm->course] = array(SEPARATEGROUPS=>array(), VISIBLEGROUPS=>array(), 'aag'=>array());
696 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
697 if (has_capability('moodle/site:accessallgroups', $context)) {
698 $groupmode = 'aag';
701 // grouping used the first time - add first user group as default
702 if (!array_key_exists($cm->groupingid, $SESSION->activegroup[$cm->course][$groupmode])) {
703 if ($groupmode == 'aag') {
704 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0; // all groups by default if user has accessallgroups
706 } else if ($usergroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid)) {
707 $firstgroup = reset($usergroups);
708 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $firstgroup->id;
710 } else {
711 // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet
712 // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum)
713 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0;
717 // set new active group if requested
718 $changegroup = optional_param('group', -1, PARAM_INT);
719 if ($update and $changegroup != -1) {
721 if ($changegroup == 0) {
722 // allgroups visible only in VISIBLEGROUPS or when accessallgroups
723 if ($groupmode == VISIBLEGROUPS or $groupmode == 'aag') {
724 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0;
727 } else {
728 // first make list of allowed groups
729 if ($groupmode == VISIBLEGROUPS or $groupmode == 'aag') {
730 $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping (all if groupings not used)
731 } else {
732 $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups
735 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
736 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $changegroup;
741 return $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid];
745 * Gets a list of groups that the user is allowed to access within the
746 * specified activity.
748 * @global object
749 * @param object $cm Course-module
750 * @param int $userid User ID (defaults to current user)
751 * @return array An array of group objects, or false if none
753 function groups_get_activity_allowed_groups($cm,$userid=0) {
754 // Use current user by default
755 global $USER;
756 if(!$userid) {
757 $userid=$USER->id;
760 // Get groupmode for activity, taking into account course settings
761 $groupmode=groups_get_activity_groupmode($cm);
763 // If visible groups mode, or user has the accessallgroups capability,
764 // then they can access all groups for the activity...
765 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
766 if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
767 return groups_get_all_groups($cm->course, 0, $cm->groupingid);
768 } else {
769 // ...otherwise they can only access groups they belong to
770 return groups_get_all_groups($cm->course, $userid, $cm->groupingid);
775 * Determine if a course module is currently visible to a user
777 * $USER If $userid is null, use the global object.
779 * @global object
780 * @global object
781 * @param int $cm The course module
782 * @param int $userid The user to check against the group.
783 * @return boolean True if the user can view the course module, false otherwise.
785 function groups_course_module_visible($cm, $userid=null) {
786 global $CFG, $USER;
788 if (empty($userid)) {
789 $userid = $USER->id;
791 if (empty($CFG->enablegroupmembersonly)) {
792 return true;
794 if (empty($cm->groupmembersonly)) {
795 return true;
797 if (has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE, $cm->id), $userid) or groups_has_membership($cm, $userid)) {
798 return true;
800 return false;