MDL-23726 fixed phpdocs - credit goes to Henning Bostelmann
[moodle.git] / lib / grouplib.php
blobebd469ed1f5704020b021364f20354027c99e1fb
1 <?php //$Id$
3 /**
4 * Groups not used in course or activity
5 */
6 define('NOGROUPS', 0);
8 /**
9 * Groups used, users do not see other groups
11 define('SEPARATEGROUPS', 1);
13 /**
14 * Groups used, students see other groups
16 define('VISIBLEGROUPS', 2);
19 /**
20 * Determines if a group with a given groupid exists.
21 * @param int $groupid The groupid to check for
22 * @return boolean True if the group exists, false otherwise or if an error
23 * occurred.
25 function groups_group_exists($groupid) {
26 return record_exists('groups', 'id', $groupid);
29 /**
30 * Gets the name of a group with a specified id
31 * @param int $groupid The id of the group
32 * @return string The name of the group
34 function groups_get_group_name($groupid) {
35 return get_field('groups', 'name', 'id', $groupid);
38 /**
39 * Gets the name of a grouping with a specified id
40 * @param int $groupingid The id of the grouping
41 * @return string The name of the grouping
43 function groups_get_grouping_name($groupingid) {
44 return get_field('groupings', 'name', 'id', $groupingid);
47 /**
48 * Returns the groupid of a group with the name specified for the course.
49 * Group names should be unique in course
50 * @param int $courseid The id of the course
51 * @param string $name name of group (without magic quotes)
52 * @return int $groupid
54 function groups_get_group_by_name($courseid, $name) {
55 if ($groups = get_records_select('groups', "courseid=$courseid AND name='".addslashes($name)."'")) {
56 return key($groups);
58 return false;
61 /**
62 * Returns the groupingid of a grouping with the name specified for the course.
63 * Grouping names should be unique in course
64 * @param int $courseid The id of the course
65 * @param string $name name of group (without magic quotes)
66 * @return int $groupid
68 function groups_get_grouping_by_name($courseid, $name) {
69 if ($groupings = get_records_select('groupings', "courseid=$courseid AND name='".addslashes($name)."'")) {
70 return key($groupings);
72 return false;
75 /**
76 * Get the group object
77 * @param groupid ID of the group.
78 * @return group object
80 function groups_get_group($groupid) {
81 return get_record('groups', 'id', $groupid);
84 /**
85 * Get the grouping object
86 * @param groupingid ID of the group.
87 * @return group object
89 function groups_get_grouping($groupingid) {
90 return get_record('groupings', 'id', $groupingid);
93 /**
94 * Gets array of all groups in a specified course.
95 * @param int $courseid The id of the course.
96 * @param mixed $userid optional user id or array of ids, returns only groups of the user.
97 * @param int $groupingid optional returns only groups in the specified grouping.
98 * @return array | false Returns an array of the group objects or false if no records
99 * or an error occurred. (userid field returned if array in $userid)
101 function groups_get_all_groups($courseid, $userid=0, $groupingid=0, $fields='g.*') {
102 global $CFG;
104 // groupings are ignored when not enabled
105 if (empty($CFG->enablegroupings)) {
106 $groupingid = 0;
109 if (empty($userid)) {
110 $userfrom = "";
111 $userwhere = "";
113 } else if (is_array($userid)) {
114 $userids = implode(',', $userid);
115 $userfrom = ", {$CFG->prefix}groups_members gm";
116 $userwhere = "AND g.id = gm.groupid AND gm.userid IN ($userids)";
118 } else {
119 $userfrom = ", {$CFG->prefix}groups_members gm";
120 $userwhere = "AND g.id = gm.groupid AND gm.userid = '$userid'";
123 if (!empty($groupingid)) {
124 $groupingfrom = ", {$CFG->prefix}groupings_groups gg";
125 $groupingwhere = "AND g.id = gg.groupid AND gg.groupingid = '$groupingid'";
126 } else {
127 $groupingfrom = "";
128 $groupingwhere = "";
131 return get_records_sql("SELECT $fields
132 FROM {$CFG->prefix}groups g $userfrom $groupingfrom
133 WHERE g.courseid = $courseid $userwhere $groupingwhere
134 ORDER BY name ASC");
138 * Returns info about user's groups in course.
139 * @param int $courseid
140 * @param int $userid $USER if not specified
141 * @return array[groupingid][groupid] including grouping id 0 which means all groups
143 function groups_get_user_groups($courseid, $userid=0) {
144 global $CFG, $USER;
146 if (empty($userid)) {
147 $userid = $USER->id;
150 if (!$rs = get_recordset_sql("SELECT g.id, gg.groupingid
151 FROM {$CFG->prefix}groups g
152 JOIN {$CFG->prefix}groups_members gm ON gm.groupid = g.id
153 LEFT JOIN {$CFG->prefix}groupings_groups gg ON gg.groupid = g.id
154 WHERE gm.userid = $userid AND g.courseid = $courseid")) {
155 return array('0' => array());
158 $result = array();
159 $allgroups = array();
161 while ($group = rs_fetch_next_record($rs)) {
162 $allgroups[$group->id] = $group->id;
163 if (is_null($group->groupingid)) {
164 continue;
166 if (!array_key_exists($group->groupingid, $result)) {
167 $result[$group->groupingid] = array();
169 $result[$group->groupingid][$group->id] = $group->id;
171 rs_close($rs);
173 $result['0'] = array_keys($allgroups); // all groups
175 return $result;
179 * Gets array of all groupings in a specified course.
180 * @param int $courseid return only groupings in this with this courseid
181 * @return array | false Returns an array of the grouping objects or false if no records
182 * or an error occurred.
184 function groups_get_all_groupings($courseid) {
185 global $CFG;
187 // groupings are ignored when not enabled
188 if (empty($CFG->enablegroupings)) {
189 return(false);
191 return get_records_sql("SELECT *
192 FROM {$CFG->prefix}groupings
193 WHERE courseid = $courseid
194 ORDER BY name ASC");
200 * Determines if the user is a member of the given group.
202 * @uses $USER If $userid is null, use the global object.
203 * @param int $groupid The group to check for membership.
204 * @param int $userid The user to check against the group.
205 * @return boolean True if the user is a member, false otherwise.
207 function groups_is_member($groupid, $userid=null) {
208 global $USER;
210 if (!$userid) {
211 $userid = $USER->id;
214 return record_exists('groups_members', 'groupid', $groupid, 'userid', $userid);
218 * Determines if current or specified is member of any active group in activity
219 * @param object $cm coruse module object
220 * @param int $userid id of user, null menas $USER->id
221 * @return booelan true if user member of at least one group used in activity
223 function groups_has_membership($cm, $userid=null) {
224 global $CFG, $USER;
226 static $cache = array();
228 // groupings are ignored when not enabled
229 if (empty($CFG->enablegroupings)) {
230 $cm->groupingid = 0;
233 if (empty($userid)) {
234 $userid = $USER->id;
237 $cachekey = $userid.'|'.$cm->course.'|'.$cm->groupingid;
238 if (isset($cache[$cachekey])) {
239 return($cache[$cachekey]);
242 if ($cm->groupingid) {
243 // find out if member of any group in selected activity grouping
244 $sql = "SELECT 'x'
245 FROM {$CFG->prefix}groups_members gm, {$CFG->prefix}groupings_groups gg
246 WHERE gm.userid = $userid AND gm.groupid = gg.groupid AND gg.groupingid = {$cm->groupingid}";
248 } else {
249 // no grouping used - check all groups in course
250 $sql = "SELECT 'x'
251 FROM {$CFG->prefix}groups_members gm, {$CFG->prefix}groups g
252 WHERE gm.userid = $userid AND gm.groupid = g.id AND g.courseid = {$cm->course}";
255 $cache[$cachekey] = record_exists_sql($sql);
257 return $cache[$cachekey];
261 * Returns the users in the specified group.
262 * @param int $groupid The groupid to get the users for
263 * @param int $fields The fields to return
264 * @param int $sort optional sorting of returned users
265 * @return array | false Returns an array of the users for the specified
266 * group or false if no users or an error returned.
268 function groups_get_members($groupid, $fields='u.*', $sort='lastname ASC') {
269 global $CFG;
271 return get_records_sql("SELECT $fields
272 FROM {$CFG->prefix}user u, {$CFG->prefix}groups_members gm
273 WHERE u.id = gm.userid AND gm.groupid = '$groupid'
274 ORDER BY $sort");
279 * Returns the users in the specified grouping.
280 * @param int $groupingid The groupingid to get the users for
281 * @param int $fields The fields to return
282 * @param int $sort optional sorting of returned users
283 * @return array | false Returns an array of the users for the specified
284 * group or false if no users or an error returned.
286 function groups_get_grouping_members($groupingid, $fields='u.*', $sort='lastname ASC') {
287 global $CFG;
289 return get_records_sql("SELECT $fields
290 FROM {$CFG->prefix}user u
291 INNER JOIN {$CFG->prefix}groups_members gm ON u.id = gm.userid
292 INNER JOIN {$CFG->prefix}groupings_groups gg ON gm.groupid = gg.groupid
293 WHERE gg.groupingid = $groupingid
294 ORDER BY $sort");
298 * Returns effective groupmode used in course
299 * @return integer group mode
301 function groups_get_course_groupmode($course) {
302 return $course->groupmode;
306 * Returns effective groupmode used in activity, course setting
307 * overrides activity setting if groupmodeforce enabled.
308 * @param $cm the course module object. Only the ->course and ->groupmode need to be set.
309 * @param $course object optional course object to improve perf
310 * @return integer group mode
312 function groups_get_activity_groupmode($cm, $course=null) {
313 global $COURSE;
315 // get course object (reuse COURSE if possible)
316 if (isset($course->id) and $course->id == $cm->course) {
317 //ok
318 } else if ($cm->course == $COURSE->id) {
319 $course = $COURSE;
320 } else {
321 if (!$course = get_record('course', 'id', $cm->course)) {
322 error('Incorrect course id in cm');
326 return empty($course->groupmodeforce) ? $cm->groupmode : $course->groupmode;
330 * Print group menu selector for course level.
331 * @param object $course course object
332 * @param string $urlroot return address
333 * @param boolean $return return as string instead of printing
334 * @return mixed void or string depending on $return param
336 function groups_print_course_menu($course, $urlroot, $return=false) {
337 global $CFG, $USER, $SESSION;
339 if (!$groupmode = $course->groupmode) {
340 if ($return) {
341 return '';
342 } else {
343 return;
347 $context = get_context_instance(CONTEXT_COURSE, $course->id);
348 if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
349 $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
350 // detect changes related to groups and fix active group
351 if (!empty($SESSION->activegroup[$course->id][VISIBLEGROUPS][0])) {
352 if (!array_key_exists($SESSION->activegroup[$course->id][VISIBLEGROUPS][0], $allowedgroups)) {
353 // active does not exist anymore
354 unset($SESSION->activegroup[$course->id][VISIBLEGROUPS][0]);
357 if (!empty($SESSION->activegroup[$course->id]['aag'][0])) {
358 if (!array_key_exists($SESSION->activegroup[$course->id]['aag'][0], $allowedgroups)) {
359 // active group does not exist anymore
360 unset($SESSION->activegroup[$course->id]['aag'][0]);
364 } else {
365 $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
366 // detect changes related to groups and fix active group
367 if (isset($SESSION->activegroup[$course->id][SEPARATEGROUPS][0])) {
368 if ($SESSION->activegroup[$course->id][SEPARATEGROUPS][0] == 0) {
369 if ($allowedgroups) {
370 // somebody must have assigned at least one group, we can select it now - yay!
371 unset($SESSION->activegroup[$course->id][SEPARATEGROUPS][0]);
373 } else {
374 if (!array_key_exists($SESSION->activegroup[$course->id][SEPARATEGROUPS][0], $allowedgroups)) {
375 // active group not allowed or does not exist anymore
376 unset($SESSION->activegroup[$course->id][SEPARATEGROUPS][0]);
382 $activegroup = groups_get_course_group($course, true);
384 $groupsmenu = array();
385 if (!$allowedgroups or $groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
386 $groupsmenu[0] = get_string('allparticipants');
389 if ($allowedgroups) {
390 foreach ($allowedgroups as $group) {
391 $groupsmenu[$group->id] = format_string($group->name);
395 if ($groupmode == VISIBLEGROUPS) {
396 $grouplabel = get_string('groupsvisible');
397 } else {
398 $grouplabel = get_string('groupsseparate');
401 if (count($groupsmenu) == 1) {
402 $groupname = reset($groupsmenu);
403 $output = $grouplabel.': '.$groupname;
404 } else {
405 $output = popup_form($urlroot.'&amp;group=', $groupsmenu, 'selectgroup', $activegroup, '', '', '', true, 'self', $grouplabel);
408 $output = '<div class="groupselector">'.$output.'</div>';
410 if ($return) {
411 return $output;
412 } else {
413 echo $output;
418 * Print group menu selector for activity.
419 * @param object $cm course module object
420 * @param string $urlroot return address that users get to if they choose an option;
421 * should include any parameters needed, e.g. "$CFG->wwwroot/mod/forum/view.php?id=34"
422 * @param boolean $return return as string instead of printing
423 * @param boolean $hideallparticipants If true, this prevents the 'All participants'
424 * option from appearing in cases where it normally would. This is intended for
425 * use only by activities that cannot display all groups together. (Note that
426 * selecting this option does not prevent groups_get_activity_group from
427 * returning 0; it will still do that if the user has chosen 'all participants'
428 * in another activity, or not chosen anything.)
429 * @return mixed void or string depending on $return param
431 function groups_print_activity_menu($cm, $urlroot, $return=false, $hideallparticipants=false) {
432 global $CFG, $USER, $SESSION;
434 // Display error if urlroot is not absolute (this causes the non-JS version
435 // to break)
436 if (strpos($urlroot, 'http') !== 0) { // Will also work for https
437 debugging('groups_print_activity_menu requires absolute URL for ' .
438 '$urlroot, not <tt>' . s($urlroot) . '</tt>. Example: ' .
439 'groups_print_activity_menu($cm, $CFG->wwwroot . \'/mod/mymodule/view.php?id=13\');',
440 DEBUG_DEVELOPER);
442 // groupings are ignored when not enabled
443 if (empty($CFG->enablegroupings)) {
444 $cm->groupingid = 0;
447 if (!$groupmode = groups_get_activity_groupmode($cm)) {
448 if ($return) {
449 return '';
450 } else {
451 return;
455 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
456 if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
457 $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping (all if groupings not used)
458 // detect changes related to groups and fix active group
459 if (!empty($SESSION->activegroup[$cm->course][VISIBLEGROUPS][$cm->groupingid])) {
460 if (!array_key_exists($SESSION->activegroup[$cm->course][VISIBLEGROUPS][$cm->groupingid], $allowedgroups)) {
461 // active group does not exist anymore
462 unset($SESSION->activegroup[$cm->course][VISIBLEGROUPS][$cm->groupingid]);
465 if (!empty($SESSION->activegroup[$cm->course]['aag'][$cm->groupingid])) {
466 if (!array_key_exists($SESSION->activegroup[$cm->course]['aag'][$cm->groupingid], $allowedgroups)) {
467 // active group does not exist anymore
468 unset($SESSION->activegroup[$cm->course]['aag'][$cm->groupingid]);
472 } else {
473 $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups
474 // detect changes related to groups and fix active group
475 if (isset($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid])) {
476 if ($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid] == 0) {
477 if ($allowedgroups) {
478 // somebody must have assigned at least one group, we can select it now - yay!
479 unset($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid]);
481 } else {
482 if (!array_key_exists($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid], $allowedgroups)) {
483 // active group not allowed or does not exist anymore
484 unset($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid]);
490 $activegroup = groups_get_activity_group($cm, true);
492 $groupsmenu = array();
493 if ((!$allowedgroups or $groupmode == VISIBLEGROUPS or
494 has_capability('moodle/site:accessallgroups', $context)) and !$hideallparticipants) {
495 $groupsmenu[0] = get_string('allparticipants');
498 if ($allowedgroups) {
499 foreach ($allowedgroups as $group) {
500 $groupsmenu[$group->id] = format_string($group->name);
504 if ($groupmode == VISIBLEGROUPS) {
505 $grouplabel = get_string('groupsvisible');
506 } else {
507 $grouplabel = get_string('groupsseparate');
510 if (count($groupsmenu) == 1) {
511 $groupname = reset($groupsmenu);
512 $output = $grouplabel.': '.$groupname;
513 } else {
514 $output = popup_form($urlroot.'&amp;group=', $groupsmenu, 'selectgroup', $activegroup, '', '', '', true, 'self', $grouplabel);
517 $output = '<div class="groupselector">'.$output.'</div>';
519 if ($return) {
520 return $output;
521 } else {
522 echo $output;
527 * Returns group active in course, changes the group by default if 'group' page param present
529 * @param object $course course bject
530 * @param boolean $update change active group if group param submitted
531 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
533 function groups_get_course_group($course, $update=false) {
534 global $CFG, $USER, $SESSION;
536 if (!$groupmode = $course->groupmode) {
537 // NOGROUPS used
538 return false;
541 // init activegroup array
542 if (!array_key_exists('activegroup', $SESSION)) {
543 $SESSION->activegroup = array();
545 if (!array_key_exists($course->id, $SESSION->activegroup)) {
546 $SESSION->activegroup[$course->id] = array(SEPARATEGROUPS=>array(), VISIBLEGROUPS=>array(), 'aag'=>array());
549 $context = get_context_instance(CONTEXT_COURSE, $course->id);
550 if (has_capability('moodle/site:accessallgroups', $context)) {
551 $groupmode = 'aag';
554 // grouping used the first time - add first user group as default
555 if (!array_key_exists(0, $SESSION->activegroup[$course->id][$groupmode])) {
556 if ($groupmode == 'aag') {
557 $SESSION->activegroup[$course->id][$groupmode][0] = 0; // all groups by default if user has accessallgroups
559 } else if ($usergroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid)) {
560 $fistgroup = reset($usergroups);
561 $SESSION->activegroup[$course->id][$groupmode][0] = $fistgroup->id;
563 } else {
564 // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet
565 // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum)
566 $SESSION->activegroup[$course->id][$groupmode][0] = 0;
570 // set new active group if requested
571 $changegroup = optional_param('group', -1, PARAM_INT);
572 if ($update and $changegroup != -1) {
574 if ($changegroup == 0) {
575 // do not allow changing to all groups without accessallgroups capability
576 if ($groupmode == VISIBLEGROUPS or $groupmode == 'aag') {
577 $SESSION->activegroup[$course->id][$groupmode][0] = 0;
580 } else {
581 // first make list of allowed groups
582 if ($groupmode == VISIBLEGROUPS or $groupmode == 'aag') {
583 $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
584 } else {
585 $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
588 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
589 $SESSION->activegroup[$course->id][$groupmode][0] = $changegroup;
594 return $SESSION->activegroup[$course->id][$groupmode][0];
598 * Returns group active in activity, changes the group by default if 'group' page param present
600 * @param object $cm course module object
601 * @param boolean $update change active group if group param submitted
602 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
604 function groups_get_activity_group($cm, $update=false) {
605 global $CFG, $USER, $SESSION;
607 // groupings are ignored when not enabled
608 if (empty($CFG->enablegroupings)) {
609 $cm->groupingid = 0;
612 if (!$groupmode = groups_get_activity_groupmode($cm)) {
613 // NOGROUPS used
614 return false;
617 // init activegroup array
618 if (!array_key_exists('activegroup', $SESSION)) {
619 $SESSION->activegroup = array();
621 if (!array_key_exists($cm->course, $SESSION->activegroup)) {
622 $SESSION->activegroup[$cm->course] = array(SEPARATEGROUPS=>array(), VISIBLEGROUPS=>array(), 'aag'=>array());
625 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
626 if (has_capability('moodle/site:accessallgroups', $context)) {
627 $groupmode = 'aag';
630 // grouping used the first time - add first user group as default
631 if (!array_key_exists($cm->groupingid, $SESSION->activegroup[$cm->course][$groupmode])) {
632 if ($groupmode == 'aag') {
633 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0; // all groups by default if user has accessallgroups
635 } else if ($usergroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid)) {
636 $fistgroup = reset($usergroups);
637 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $fistgroup->id;
639 } else {
640 // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet
641 // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum)
642 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0;
646 // set new active group if requested
647 $changegroup = optional_param('group', -1, PARAM_INT);
648 if ($update and $changegroup != -1) {
650 if ($changegroup == 0) {
651 // allgroups visible only in VISIBLEGROUPS or when accessallgroups
652 if ($groupmode == VISIBLEGROUPS or $groupmode == 'aag') {
653 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0;
656 } else {
657 // first make list of allowed groups
658 if ($groupmode == VISIBLEGROUPS or $groupmode == 'aag') {
659 $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping (all if groupings not used)
660 } else {
661 $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups
664 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
665 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $changegroup;
670 return $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid];
674 * Gets a list of groups that the user is allowed to access within the
675 * specified activity.
676 * @param object $cm Course-module
677 * @param int $userid User ID (defaults to current user)
678 * @return array An array of group objects, or false if none
680 function groups_get_activity_allowed_groups($cm,$userid=0) {
681 // Use current user by default
682 global $USER;
683 if(!$userid) {
684 $userid=$USER->id;
687 // Get groupmode for activity, taking into account course settings
688 $groupmode=groups_get_activity_groupmode($cm);
690 // If visible groups mode, or user has the accessallgroups capability,
691 // then they can access all groups for the activity...
692 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
693 if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
694 return groups_get_all_groups($cm->course, 0, $cm->groupingid);
695 } else {
696 // ...otherwise they can only access groups they belong to
697 return groups_get_all_groups($cm->course, $userid, $cm->groupingid);
702 * Determine if a course module is currently visible to a user
703 * @uses $USER If $userid is null, use the global object.
704 * @param int $cm The course module
705 * @param int $userid The user to check against the group.
706 * @return boolean True if the user can view the course module, false otherwise.
708 function groups_course_module_visible($cm, $userid=null) {
709 global $CFG, $USER;
711 if (empty($userid)) {
712 $userid = $USER->id;
714 if (empty($CFG->enablegroupings)) {
715 return true;
717 if (empty($cm->groupmembersonly)) {
718 return true;
720 if (has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE, $cm->id), $userid) or groups_has_membership($cm, $userid)) {
721 return true;
723 return false;