MDL-24471, FILEMANAGER change <button> to <input type="button" />, moodle form may...
[moodle.git] / lib / grouplib.php
blob5eaf10831d36991d874459018d0c8effcc71f3b9
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 if (!$rs = $DB->get_recordset_sql($sql, $params)) {
203 return array('0' => array());
206 $result = array();
207 $allgroups = array();
209 foreach ($rs as $group) {
210 $allgroups[$group->id] = $group->id;
211 if (is_null($group->groupingid)) {
212 continue;
214 if (!array_key_exists($group->groupingid, $result)) {
215 $result[$group->groupingid] = array();
217 $result[$group->groupingid][$group->id] = $group->id;
219 $rs->close();
221 $result['0'] = array_keys($allgroups); // all groups
223 return $result;
227 * Gets array of all groupings in a specified course.
229 * @global object
230 * @global object
231 * @param int $courseid return only groupings in this with this courseid
232 * @return array|bool Returns an array of the grouping objects or false if no records
233 * or an error occurred.
235 function groups_get_all_groupings($courseid) {
236 global $CFG, $DB;
238 return $DB->get_records_sql("SELECT *
239 FROM {groupings}
240 WHERE courseid = ?
241 ORDER BY name ASC", array($courseid));
247 * Determines if the user is a member of the given group.
249 * If $userid is null, use the global object.
251 * @global object
252 * @global object
253 * @param int $groupid The group to check for membership.
254 * @param int $userid The user to check against the group.
255 * @return boolean True if the user is a member, false otherwise.
257 function groups_is_member($groupid, $userid=null) {
258 global $USER, $DB;
260 if (!$userid) {
261 $userid = $USER->id;
264 return $DB->record_exists('groups_members', array('groupid'=>$groupid, 'userid'=>$userid));
268 * Determines if current or specified is member of any active group in activity
270 * @global object
271 * @global object
272 * @global object
273 * @staticvar array $cache
274 * @param object $cm coruse module object
275 * @param int $userid id of user, null menas $USER->id
276 * @return booelan true if user member of at least one group used in activity
278 function groups_has_membership($cm, $userid=null) {
279 global $CFG, $USER, $DB;
281 static $cache = array();
283 if (empty($userid)) {
284 $userid = $USER->id;
287 $cachekey = $userid.'|'.$cm->course.'|'.$cm->groupingid;
288 if (isset($cache[$cachekey])) {
289 return($cache[$cachekey]);
292 if ($cm->groupingid) {
293 // find out if member of any group in selected activity grouping
294 $sql = "SELECT 'x'
295 FROM {groups_members} gm, {groupings_groups} gg
296 WHERE gm.userid = ? AND gm.groupid = gg.groupid AND gg.groupingid = ?";
297 $params = array($userid, $cm->groupingid);
299 } else {
300 // no grouping used - check all groups in course
301 $sql = "SELECT 'x'
302 FROM {groups_members} gm, {groups} g
303 WHERE gm.userid = ? AND gm.groupid = g.id AND g.courseid = ?";
304 $params = array($userid, $cm->course);
307 $cache[$cachekey] = $DB->record_exists_sql($sql, $params);
309 return $cache[$cachekey];
313 * Returns the users in the specified group.
315 * @global object
316 * @param int $groupid The groupid to get the users for
317 * @param int $fields The fields to return
318 * @param int $sort optional sorting of returned users
319 * @return array|bool Returns an array of the users for the specified
320 * group or false if no users or an error returned.
322 function groups_get_members($groupid, $fields='u.*', $sort='lastname ASC') {
323 global $DB;
325 return $DB->get_records_sql("SELECT $fields
326 FROM {user} u, {groups_members} gm
327 WHERE u.id = gm.userid AND gm.groupid = ?
328 ORDER BY $sort", array($groupid));
333 * Returns the users in the specified grouping.
335 * @global object
336 * @param int $groupingid The groupingid to get the users for
337 * @param int $fields The fields to return
338 * @param int $sort optional sorting of returned users
339 * @return array|bool Returns an array of the users for the specified
340 * group or false if no users or an error returned.
342 function groups_get_grouping_members($groupingid, $fields='u.*', $sort='lastname ASC') {
343 global $DB;
345 return $DB->get_records_sql("SELECT $fields
346 FROM {user} u
347 INNER JOIN {groups_members} gm ON u.id = gm.userid
348 INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid
349 WHERE gg.groupingid = ?
350 ORDER BY $sort", array($groupingid));
354 * Returns effective groupmode used in course
356 * @return integer group mode
358 function groups_get_course_groupmode($course) {
359 return $course->groupmode;
363 * Returns effective groupmode used in activity, course setting
364 * overrides activity setting if groupmodeforce enabled.
366 * @global object
367 * @global object
368 * @param object $cm the course module object. Only the ->course and ->groupmode need to be set.
369 * @param object $course object optional course object to improve perf
370 * @return integer group mode
372 function groups_get_activity_groupmode($cm, $course=null) {
373 global $COURSE, $DB;
375 // get course object (reuse COURSE if possible)
376 if (isset($course->id) and $course->id == $cm->course) {
377 //ok
378 } else if ($cm->course == $COURSE->id) {
379 $course = $COURSE;
380 } else {
381 if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
382 print_error('invalidcourseid');
386 return empty($course->groupmodeforce) ? $cm->groupmode : $course->groupmode;
390 * Print group menu selector for course level.
392 * @global object
393 * @global object
394 * @param object $course course object
395 * @param string $urlroot return address
396 * @param boolean $return return as string instead of printing
397 * @return mixed void or string depending on $return param
399 function groups_print_course_menu($course, $urlroot, $return=false) {
400 global $CFG, $USER, $SESSION, $OUTPUT;
402 if (!$groupmode = $course->groupmode) {
403 if ($return) {
404 return '';
405 } else {
406 return;
410 $context = get_context_instance(CONTEXT_COURSE, $course->id);
411 if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
412 $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
413 // detect changes related to groups and fix active group
414 if (!empty($SESSION->activegroup[$course->id][VISIBLEGROUPS][0])) {
415 if (!array_key_exists($SESSION->activegroup[$course->id][VISIBLEGROUPS][0], $allowedgroups)) {
416 // active does not exist anymore
417 unset($SESSION->activegroup[$course->id][VISIBLEGROUPS][0]);
420 if (!empty($SESSION->activegroup[$course->id]['aag'][0])) {
421 if (!array_key_exists($SESSION->activegroup[$course->id]['aag'][0], $allowedgroups)) {
422 // active group does not exist anymore
423 unset($SESSION->activegroup[$course->id]['aag'][0]);
427 } else {
428 $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
429 // detect changes related to groups and fix active group
430 if (isset($SESSION->activegroup[$course->id][SEPARATEGROUPS][0])) {
431 if ($SESSION->activegroup[$course->id][SEPARATEGROUPS][0] == 0) {
432 if ($allowedgroups) {
433 // somebody must have assigned at least one group, we can select it now - yay!
434 unset($SESSION->activegroup[$course->id][SEPARATEGROUPS][0]);
436 } else {
437 if (!array_key_exists($SESSION->activegroup[$course->id][SEPARATEGROUPS][0], $allowedgroups)) {
438 // active group not allowed or does not exist anymore
439 unset($SESSION->activegroup[$course->id][SEPARATEGROUPS][0]);
445 $activegroup = groups_get_course_group($course, true);
447 $groupsmenu = array();
448 if (!$allowedgroups or $groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
449 $groupsmenu[0] = get_string('allparticipants');
452 if ($allowedgroups) {
453 foreach ($allowedgroups as $group) {
454 $groupsmenu[$group->id] = format_string($group->name);
458 if ($groupmode == VISIBLEGROUPS) {
459 $grouplabel = get_string('groupsvisible');
460 } else {
461 $grouplabel = get_string('groupsseparate');
464 if (count($groupsmenu) == 1) {
465 $groupname = reset($groupsmenu);
466 $output = $grouplabel.': '.$groupname;
467 } else {
468 $select = new single_select(new moodle_url($urlroot), 'group', $groupsmenu, $activegroup, null, 'selectgroup');
469 $select->label = $grouplabel;
470 $output = $OUTPUT->render($select);
473 $output = '<div class="groupselector">'.$output.'</div>';
475 if ($return) {
476 return $output;
477 } else {
478 echo $output;
483 * Print group menu selector for activity.
485 * @global object
486 * @global object
487 * @global object
488 * @param object $cm course module object
489 * @param string $urlroot return address that users get to if they choose an option;
490 * should include any parameters needed, e.g. "$CFG->wwwroot/mod/forum/view.php?id=34"
491 * @param boolean $return return as string instead of printing
492 * @param boolean $hideallparticipants If true, this prevents the 'All participants'
493 * option from appearing in cases where it normally would. This is intended for
494 * use only by activities that cannot display all groups together. (Note that
495 * selecting this option does not prevent groups_get_activity_group from
496 * returning 0; it will still do that if the user has chosen 'all participants'
497 * in another activity, or not chosen anything.)
498 * @return mixed void or string depending on $return param
500 function groups_print_activity_menu($cm, $urlroot, $return=false, $hideallparticipants=false) {
501 global $CFG, $USER, $SESSION, $OUTPUT;
503 // Display error if urlroot is not absolute (this causes the non-JS version
504 // to break)
505 if (strpos($urlroot, 'http') !== 0) { // Will also work for https
506 debugging('groups_print_activity_menu requires absolute URL for ' .
507 '$urlroot, not <tt>' . s($urlroot) . '</tt>. Example: ' .
508 'groups_print_activity_menu($cm, $CFG->wwwroot . \'/mod/mymodule/view.php?id=13\');',
509 DEBUG_DEVELOPER);
512 if (!$groupmode = groups_get_activity_groupmode($cm)) {
513 if ($return) {
514 return '';
515 } else {
516 return;
520 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
521 if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
522 $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping (all if groupings not used)
523 // detect changes related to groups and fix active group
524 if (!empty($SESSION->activegroup[$cm->course][VISIBLEGROUPS][$cm->groupingid])) {
525 if (!array_key_exists($SESSION->activegroup[$cm->course][VISIBLEGROUPS][$cm->groupingid], $allowedgroups)) {
526 // active group does not exist anymore
527 unset($SESSION->activegroup[$cm->course][VISIBLEGROUPS][$cm->groupingid]);
530 if (!empty($SESSION->activegroup[$cm->course]['aag'][$cm->groupingid])) {
531 if (!array_key_exists($SESSION->activegroup[$cm->course]['aag'][$cm->groupingid], $allowedgroups)) {
532 // active group does not exist anymore
533 unset($SESSION->activegroup[$cm->course]['aag'][$cm->groupingid]);
537 } else {
538 $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups
539 // detect changes related to groups and fix active group
540 if (isset($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid])) {
541 if ($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid] == 0) {
542 if ($allowedgroups) {
543 // somebody must have assigned at least one group, we can select it now - yay!
544 unset($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid]);
546 } else {
547 if (!array_key_exists($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid], $allowedgroups)) {
548 // active group not allowed or does not exist anymore
549 unset($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid]);
555 $activegroup = groups_get_activity_group($cm, true);
557 $groupsmenu = array();
558 if ((!$allowedgroups or $groupmode == VISIBLEGROUPS or
559 has_capability('moodle/site:accessallgroups', $context)) and !$hideallparticipants) {
560 $groupsmenu[0] = get_string('allparticipants');
563 if ($allowedgroups) {
564 foreach ($allowedgroups as $group) {
565 $groupsmenu[$group->id] = format_string($group->name);
569 if ($groupmode == VISIBLEGROUPS) {
570 $grouplabel = get_string('groupsvisible');
571 } else {
572 $grouplabel = get_string('groupsseparate');
575 if (count($groupsmenu) == 1) {
576 $groupname = reset($groupsmenu);
577 $output = $grouplabel.': '.$groupname;
578 } else {
579 $select = new single_select(new moodle_url($urlroot), 'group', $groupsmenu, $activegroup, null, 'selectgroup');
580 $select->label = $grouplabel;
581 $output = $OUTPUT->render($select);
584 $output = '<div class="groupselector">'.$output.'</div>';
586 if ($return) {
587 return $output;
588 } else {
589 echo $output;
594 * Returns group active in course, changes the group by default if 'group' page param present
596 * @global object
597 * @global object
598 * @global object
599 * @param object $course course bject
600 * @param boolean $update change active group if group param submitted
601 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
603 function groups_get_course_group($course, $update=false) {
604 global $CFG, $USER, $SESSION;
606 if (!$groupmode = $course->groupmode) {
607 // NOGROUPS used
608 return false;
611 // init activegroup array
612 if (!isset($SESSION->activegroup)) {
613 $SESSION->activegroup = array();
615 if (!array_key_exists($course->id, $SESSION->activegroup)) {
616 $SESSION->activegroup[$course->id] = array(SEPARATEGROUPS=>array(), VISIBLEGROUPS=>array(), 'aag'=>array());
619 $context = get_context_instance(CONTEXT_COURSE, $course->id);
620 if (has_capability('moodle/site:accessallgroups', $context)) {
621 $groupmode = 'aag';
624 // grouping used the first time - add first user group as default
625 if (!array_key_exists(0, $SESSION->activegroup[$course->id][$groupmode])) {
626 if ($groupmode == 'aag') {
627 $SESSION->activegroup[$course->id][$groupmode][0] = 0; // all groups by default if user has accessallgroups
629 } else if ($usergroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid)) {
630 $firstgroup = reset($usergroups);
631 $SESSION->activegroup[$course->id][$groupmode][0] = $firstgroup->id;
633 } else {
634 // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet
635 // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum)
636 $SESSION->activegroup[$course->id][$groupmode][0] = 0;
640 // set new active group if requested
641 $changegroup = optional_param('group', -1, PARAM_INT);
642 if ($update and $changegroup != -1) {
644 if ($changegroup == 0) {
645 // do not allow changing to all groups without accessallgroups capability
646 if ($groupmode == VISIBLEGROUPS or $groupmode == 'aag') {
647 $SESSION->activegroup[$course->id][$groupmode][0] = 0;
650 } else {
651 // first make list of allowed groups
652 if ($groupmode == VISIBLEGROUPS or $groupmode == 'aag') {
653 $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
654 } else {
655 $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
658 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
659 $SESSION->activegroup[$course->id][$groupmode][0] = $changegroup;
664 return $SESSION->activegroup[$course->id][$groupmode][0];
668 * Returns group active in activity, changes the group by default if 'group' page param present
670 * @global object
671 * @global object
672 * @global object
673 * @param object $cm course module object
674 * @param boolean $update change active group if group param submitted
675 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
677 function groups_get_activity_group($cm, $update=false) {
678 global $CFG, $USER, $SESSION;
680 if (!$groupmode = groups_get_activity_groupmode($cm)) {
681 // NOGROUPS used
682 return false;
685 // init activegroup array
686 if (!isset($SESSION->activegroup)) {
687 $SESSION->activegroup = array();
689 if (!array_key_exists($cm->course, $SESSION->activegroup)) {
690 $SESSION->activegroup[$cm->course] = array(SEPARATEGROUPS=>array(), VISIBLEGROUPS=>array(), 'aag'=>array());
693 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
694 if (has_capability('moodle/site:accessallgroups', $context)) {
695 $groupmode = 'aag';
698 // grouping used the first time - add first user group as default
699 if (!array_key_exists($cm->groupingid, $SESSION->activegroup[$cm->course][$groupmode])) {
700 if ($groupmode == 'aag') {
701 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0; // all groups by default if user has accessallgroups
703 } else if ($usergroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid)) {
704 $firstgroup = reset($usergroups);
705 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $firstgroup->id;
707 } else {
708 // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet
709 // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum)
710 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0;
714 // set new active group if requested
715 $changegroup = optional_param('group', -1, PARAM_INT);
716 if ($update and $changegroup != -1) {
718 if ($changegroup == 0) {
719 // allgroups visible only in VISIBLEGROUPS or when accessallgroups
720 if ($groupmode == VISIBLEGROUPS or $groupmode == 'aag') {
721 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0;
724 } else {
725 // first make list of allowed groups
726 if ($groupmode == VISIBLEGROUPS or $groupmode == 'aag') {
727 $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping (all if groupings not used)
728 } else {
729 $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups
732 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
733 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $changegroup;
738 return $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid];
742 * Gets a list of groups that the user is allowed to access within the
743 * specified activity.
745 * @global object
746 * @param object $cm Course-module
747 * @param int $userid User ID (defaults to current user)
748 * @return array An array of group objects, or false if none
750 function groups_get_activity_allowed_groups($cm,$userid=0) {
751 // Use current user by default
752 global $USER;
753 if(!$userid) {
754 $userid=$USER->id;
757 // Get groupmode for activity, taking into account course settings
758 $groupmode=groups_get_activity_groupmode($cm);
760 // If visible groups mode, or user has the accessallgroups capability,
761 // then they can access all groups for the activity...
762 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
763 if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
764 return groups_get_all_groups($cm->course, 0, $cm->groupingid);
765 } else {
766 // ...otherwise they can only access groups they belong to
767 return groups_get_all_groups($cm->course, $userid, $cm->groupingid);
772 * Determine if a course module is currently visible to a user
774 * $USER If $userid is null, use the global object.
776 * @global object
777 * @global object
778 * @param int $cm The course module
779 * @param int $userid The user to check against the group.
780 * @return boolean True if the user can view the course module, false otherwise.
782 function groups_course_module_visible($cm, $userid=null) {
783 global $CFG, $USER;
785 if (empty($userid)) {
786 $userid = $USER->id;
788 if (empty($CFG->enablegroupmembersonly)) {
789 return true;
791 if (empty($cm->groupmembersonly)) {
792 return true;
794 if (has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE, $cm->id), $userid) or groups_has_membership($cm, $userid)) {
795 return true;
797 return false;