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) {
90 if ($groups = $DB->get_records('groups', array('courseid'=>$courseid, 'name'=>$name))) {
97 * Returns the groupingid of a grouping with the name specified for the course.
98 * Grouping names should be unique in course
101 * @param int $courseid The id of the course
102 * @param string $name name of group (without magic quotes)
103 * @return int $groupid
105 function groups_get_grouping_by_name($courseid, $name) {
107 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid, 'name'=>$name))) {
108 return key($groupings);
114 * Get the group object
117 * @param int $groupid ID of the group.
118 * @param string $fields (default is all fields)
119 * @param int $strictness (IGNORE_MISSING - default)
120 * @return stdGlass group object
122 function groups_get_group($groupid, $fields='*', $strictness=IGNORE_MISSING
) {
124 return $DB->get_record('groups', array('id'=>$groupid), $fields, $strictness);
128 * Get the grouping object
131 * @param int $groupingid ID of the group.
132 * @param string $fields
133 * @param int $strictness (IGNORE_MISSING - default)
134 * @return stdClass group object
136 function groups_get_grouping($groupingid, $fields='*', $strictness=IGNORE_MISSING
) {
138 return $DB->get_record('groupings', array('id'=>$groupingid), $fields, $strictness);
142 * Gets array of all groups in a specified course.
145 * @param int $courseid The id of the course.
146 * @param mixed $userid optional user id or array of ids, returns only groups of the user.
147 * @param int $groupingid optional returns only groups in the specified grouping.
148 * @param string $fields
149 * @return array Returns an array of the group objects (userid field returned if array in $userid)
151 function groups_get_all_groups($courseid, $userid=0, $groupingid=0, $fields='g.*') {
154 if (empty($userid)) {
160 list($usql, $params) = $DB->get_in_or_equal($userid);
161 $userfrom = ", {groups_members} gm";
162 $userwhere = "AND g.id = gm.groupid AND gm.userid $usql";
165 if (!empty($groupingid)) {
166 $groupingfrom = ", {groupings_groups} gg";
167 $groupingwhere = "AND g.id = gg.groupid AND gg.groupingid = ?";
168 $params[] = $groupingid;
174 array_unshift($params, $courseid);
176 return $DB->get_records_sql("SELECT $fields
177 FROM {groups} g $userfrom $groupingfrom
178 WHERE g.courseid = ? $userwhere $groupingwhere
179 ORDER BY name ASC", $params);
183 * Returns info about user's groups in course.
186 * @param int $courseid
187 * @param int $userid $USER if not specified
188 * @return array Array[groupingid][groupid] including grouping id 0 which means all groups
190 function groups_get_user_groups($courseid, $userid=0) {
193 if (empty($userid)) {
197 $sql = "SELECT g.id, gg.groupingid
199 JOIN {groups_members} gm ON gm.groupid = g.id
200 LEFT JOIN {groupings_groups} gg ON gg.groupid = g.id
201 WHERE gm.userid = ? AND g.courseid = ?";
202 $params = array($userid, $courseid);
204 $rs = $DB->get_recordset_sql($sql, $params);
207 $rs->close(); // Not going to iterate (but exit), close rs
208 return array('0' => array());
212 $allgroups = array();
214 foreach ($rs as $group) {
215 $allgroups[$group->id
] = $group->id
;
216 if (is_null($group->groupingid
)) {
219 if (!array_key_exists($group->groupingid
, $result)) {
220 $result[$group->groupingid
] = array();
222 $result[$group->groupingid
][$group->id
] = $group->id
;
226 $result['0'] = array_keys($allgroups); // all groups
232 * Gets an array of all groupings in a specified course.
235 * @param int $courseid return only groupings in this with this courseid
236 * @return array|bool Returns an array of the grouping objects or false if no records
237 * or an error occurred.
239 function groups_get_all_groupings($courseid) {
242 return $DB->get_records_sql("SELECT *
245 ORDER BY name ASC", array($courseid));
251 * Determines if the user is a member of the given group.
253 * If $userid is null, use the 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 bool True if the user is a member, false otherwise.
260 function groups_is_member($groupid, $userid=null) {
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
274 * @staticvar array $cache
275 * @param cm_info $cm course module object
276 * @param int $userid id of user, null means $USER->id
277 * @return bool true if user member of at least one group used in activity
279 function groups_has_membership($cm, $userid=null) {
280 global $CFG, $USER, $DB;
282 static $cache = array();
284 if (empty($userid)) {
288 $cachekey = $userid.'|'.$cm->course
.'|'.$cm->groupingid
;
289 if (isset($cache[$cachekey])) {
290 return($cache[$cachekey]);
293 if ($cm->groupingid
) {
294 // find out if member of any group in selected activity grouping
296 FROM {groups_members} gm, {groupings_groups} gg
297 WHERE gm.userid = ? AND gm.groupid = gg.groupid AND gg.groupingid = ?";
298 $params = array($userid, $cm->groupingid
);
301 // no grouping used - check all groups in course
303 FROM {groups_members} gm, {groups} g
304 WHERE gm.userid = ? AND gm.groupid = g.id AND g.courseid = ?";
305 $params = array($userid, $cm->course
);
308 $cache[$cachekey] = $DB->record_exists_sql($sql, $params);
310 return $cache[$cachekey];
314 * Returns the users in the specified group.
317 * @param int $groupid The groupid to get the users for
318 * @param int $fields The fields to return
319 * @param int $sort optional sorting of returned users
320 * @return array|bool Returns an array of the users for the specified
321 * group or false if no users or an error returned.
323 function groups_get_members($groupid, $fields='u.*', $sort='lastname ASC') {
326 return $DB->get_records_sql("SELECT $fields
327 FROM {user} u, {groups_members} gm
328 WHERE u.id = gm.userid AND gm.groupid = ?
329 ORDER BY $sort", array($groupid));
334 * Returns the users in the specified grouping.
337 * @param int $groupingid The groupingid to get the users for
338 * @param string $fields The fields to return
339 * @param string $sort optional sorting of returned users
340 * @return array|bool Returns an array of the users for the specified
341 * group or false if no users or an error returned.
343 function groups_get_grouping_members($groupingid, $fields='u.*', $sort='lastname ASC') {
346 return $DB->get_records_sql("SELECT $fields
348 INNER JOIN {groups_members} gm ON u.id = gm.userid
349 INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid
350 WHERE gg.groupingid = ?
351 ORDER BY $sort", array($groupingid));
355 * Returns effective groupmode used in course
358 * @param stdClass $course course object.
359 * @return int 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.
370 * @param cm_info $cm the course module object. Only the ->course and ->groupmode need to be set.
371 * @param stdClass $course object optional course object to improve perf
372 * @return int group mode
374 function groups_get_activity_groupmode($cm, $course=null) {
377 // get course object (reuse COURSE if possible)
378 if (isset($course->id
) and $course->id
== $cm->course
) {
380 } else if ($cm->course
== $COURSE->id
) {
383 if (!$course = $DB->get_record('course', array('id'=>$cm->course
))) {
384 print_error('invalidcourseid');
388 return empty($course->groupmodeforce
) ?
$cm->groupmode
: $course->groupmode
;
392 * Print group menu selector for course level.
395 * @param stdClass $course course object
396 * @param mixed $urlroot return address. Accepts either a string or a moodle_url
397 * @param bool $return return as string instead of printing
398 * @return mixed void or string depending on $return param
400 function groups_print_course_menu($course, $urlroot, $return=false) {
401 global $USER, $OUTPUT;
403 if (!$groupmode = $course->groupmode
) {
411 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
412 $aag = has_capability('moodle/site:accessallgroups', $context);
414 if ($groupmode == VISIBLEGROUPS
or $aag) {
415 $allowedgroups = groups_get_all_groups($course->id
, 0, $course->defaultgroupingid
);
417 $allowedgroups = groups_get_all_groups($course->id
, $USER->id
, $course->defaultgroupingid
);
420 $activegroup = groups_get_course_group($course, true, $allowedgroups);
422 $groupsmenu = array();
423 if (!$allowedgroups or $groupmode == VISIBLEGROUPS
or $aag) {
424 $groupsmenu[0] = get_string('allparticipants');
427 if ($allowedgroups) {
428 foreach ($allowedgroups as $group) {
429 $groupsmenu[$group->id
] = format_string($group->name
);
433 if ($groupmode == VISIBLEGROUPS
) {
434 $grouplabel = get_string('groupsvisible');
436 $grouplabel = get_string('groupsseparate');
439 if ($aag and $course->defaultgroupingid
) {
440 if ($grouping = groups_get_grouping($course->defaultgroupingid
)) {
441 $grouplabel = $grouplabel . ' (' . format_string($grouping->name
) . ')';
445 if (count($groupsmenu) == 1) {
446 $groupname = reset($groupsmenu);
447 $output = $grouplabel.': '.$groupname;
449 $select = new single_select(new moodle_url($urlroot), 'group', $groupsmenu, $activegroup, null, 'selectgroup');
450 $select->label
= $grouplabel;
451 $output = $OUTPUT->render($select);
454 $output = '<div class="groupselector">'.$output.'</div>';
464 * Print group menu selector for activity.
467 * @param stdClass $cm course module object
468 * @param string|moodle_url $urlroot return address that users get to if they choose an option;
469 * should include any parameters needed, e.g. "$CFG->wwwroot/mod/forum/view.php?id=34"
470 * @param bool $return return as string instead of printing
471 * @param bool $hideallparticipants If true, this prevents the 'All participants'
472 * option from appearing in cases where it normally would. This is intended for
473 * use only by activities that cannot display all groups together. (Note that
474 * selecting this option does not prevent groups_get_activity_group from
475 * returning 0; it will still do that if the user has chosen 'all participants'
476 * in another activity, or not chosen anything.)
477 * @return mixed void or string depending on $return param
479 function groups_print_activity_menu($cm, $urlroot, $return=false, $hideallparticipants=false) {
480 global $USER, $OUTPUT;
482 if ($urlroot instanceof moodle_url
) {
483 // no changes necessary
486 if (strpos($urlroot, 'http') !== 0) { // Will also work for https
487 // Display error if urlroot is not absolute (this causes the non-JS version to break)
488 debugging('groups_print_activity_menu requires absolute URL for ' .
489 '$urlroot, not <tt>' . s($urlroot) . '</tt>. Example: ' .
490 'groups_print_activity_menu($cm, $CFG->wwwroot . \'/mod/mymodule/view.php?id=13\');',
493 $urlroot = new moodle_url($urlroot);
496 if (!$groupmode = groups_get_activity_groupmode($cm)) {
504 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
505 $aag = has_capability('moodle/site:accessallgroups', $context);
507 if ($groupmode == VISIBLEGROUPS
or $aag) {
508 $allowedgroups = groups_get_all_groups($cm->course
, 0, $cm->groupingid
); // any group in grouping
510 $allowedgroups = groups_get_all_groups($cm->course
, $USER->id
, $cm->groupingid
); // only assigned groups
513 $activegroup = groups_get_activity_group($cm, true, $allowedgroups);
515 $groupsmenu = array();
516 if ((!$allowedgroups or $groupmode == VISIBLEGROUPS
or $aag) and !$hideallparticipants) {
517 $groupsmenu[0] = get_string('allparticipants');
520 if ($allowedgroups) {
521 foreach ($allowedgroups as $group) {
522 $groupsmenu[$group->id
] = format_string($group->name
);
526 if ($groupmode == VISIBLEGROUPS
) {
527 $grouplabel = get_string('groupsvisible');
529 $grouplabel = get_string('groupsseparate');
532 if ($aag and $cm->groupingid
) {
533 if ($grouping = groups_get_grouping($cm->groupingid
)) {
534 $grouplabel = $grouplabel . ' (' . format_string($grouping->name
) . ')';
538 if (count($groupsmenu) == 1) {
539 $groupname = reset($groupsmenu);
540 $output = $grouplabel.': '.$groupname;
542 $select = new single_select($urlroot, 'group', $groupsmenu, $activegroup, null, 'selectgroup');
543 $select->label
= $grouplabel;
544 $output = $OUTPUT->render($select);
547 $output = '<div class="groupselector">'.$output.'</div>';
557 * Returns group active in course, changes the group by default if 'group' page param present
560 * @param stdClass $course course bject
561 * @param bool $update change active group if group param submitted
562 * @param array $allowedgroups list of groups user may access (INTERNAL, to be used only from groups_print_course_menu())
563 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
565 function groups_get_course_group($course, $update=false, $allowedgroups=null) {
566 global $USER, $SESSION;
568 if (!$groupmode = $course->groupmode
) {
573 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
574 if (has_capability('moodle/site:accessallgroups', $context)) {
578 if (!is_array($allowedgroups)) {
579 if ($groupmode == VISIBLEGROUPS
or $groupmode === 'aag') {
580 $allowedgroups = groups_get_all_groups($course->id
, 0, $course->defaultgroupingid
);
582 $allowedgroups = groups_get_all_groups($course->id
, $USER->id
, $course->defaultgroupingid
);
586 _group_verify_activegroup($course->id
, $groupmode, $course->defaultgroupingid
, $allowedgroups);
588 // set new active group if requested
589 $changegroup = optional_param('group', -1, PARAM_INT
);
590 if ($update and $changegroup != -1) {
592 if ($changegroup == 0) {
593 // do not allow changing to all groups without accessallgroups capability
594 if ($groupmode == VISIBLEGROUPS
or $groupmode === 'aag') {
595 $SESSION->activegroup
[$course->id
][$groupmode][$course->defaultgroupingid
] = 0;
599 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
600 $SESSION->activegroup
[$course->id
][$groupmode][$course->defaultgroupingid
] = $changegroup;
605 return $SESSION->activegroup
[$course->id
][$groupmode][$course->defaultgroupingid
];
609 * Returns group active in activity, changes the group by default if 'group' page param present
612 * @param stdClass $cm course module object
613 * @param bool $update change active group if group param submitted
614 * @param array $allowedgroups list of groups user may access (INTERNAL, to be used only from groups_print_activity_menu())
615 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
617 function groups_get_activity_group($cm, $update=false, $allowedgroups=null) {
618 global $USER, $SESSION;
620 if (!$groupmode = groups_get_activity_groupmode($cm)) {
625 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
626 if (has_capability('moodle/site:accessallgroups', $context)) {
630 if (!is_array($allowedgroups)) {
631 if ($groupmode == VISIBLEGROUPS
or $groupmode === 'aag') {
632 $allowedgroups = groups_get_all_groups($cm->course
, 0, $cm->groupingid
);
634 $allowedgroups = groups_get_all_groups($cm->course
, $USER->id
, $cm->groupingid
);
638 _group_verify_activegroup($cm->course
, $groupmode, $cm->groupingid
, $allowedgroups);
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 // allgroups visible only in VISIBLEGROUPS or when accessallgroups
646 if ($groupmode == VISIBLEGROUPS
or $groupmode === 'aag') {
647 $SESSION->activegroup
[$cm->course
][$groupmode][$cm->groupingid
] = 0;
651 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
652 $SESSION->activegroup
[$cm->course
][$groupmode][$cm->groupingid
] = $changegroup;
657 return $SESSION->activegroup
[$cm->course
][$groupmode][$cm->groupingid
];
661 * Gets a list of groups that the user is allowed to access within the
662 * specified activity.
665 * @param stdClass $cm Course-module
666 * @param int $userid User ID (defaults to current user)
667 * @return array An array of group objects, or false if none
669 function groups_get_activity_allowed_groups($cm,$userid=0) {
670 // Use current user by default
676 // Get groupmode for activity, taking into account course settings
677 $groupmode=groups_get_activity_groupmode($cm);
679 // If visible groups mode, or user has the accessallgroups capability,
680 // then they can access all groups for the activity...
681 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
682 if ($groupmode == VISIBLEGROUPS
or has_capability('moodle/site:accessallgroups', $context)) {
683 return groups_get_all_groups($cm->course
, 0, $cm->groupingid
);
685 // ...otherwise they can only access groups they belong to
686 return groups_get_all_groups($cm->course
, $userid, $cm->groupingid
);
691 * Determine if a course module is currently visible to a user
693 * $USER If $userid is null, use the global object.
696 * @param stdClass $cm The course module
697 * @param int $userid The user to check against the group.
698 * @return bool True if the user can view the course module, false otherwise.
700 function groups_course_module_visible($cm, $userid=null) {
703 if (empty($userid)) {
706 if (empty($CFG->enablegroupmembersonly
)) {
709 if (empty($cm->groupmembersonly
)) {
712 if (has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE
, $cm->id
), $userid) or groups_has_membership($cm, $userid)) {
719 * Internal method, sets up $SESSION->activegroup and verifies previous value
721 * @param int $courseid
722 * @param int|string $groupmode SEPARATEGROUPS, VISIBLEGROUPS or 'aag' (access all groups)
723 * @param int $groupingid 0 means all groups
724 * @param array $allowedgroups list of groups user can see
726 function _group_verify_activegroup($courseid, $groupmode, $groupingid, array $allowedgroups) {
727 global $SESSION, $USER;
729 // init activegroup array if necessary
730 if (!isset($SESSION->activegroup
)) {
731 $SESSION->activegroup
= array();
733 if (!array_key_exists($courseid, $SESSION->activegroup
)) {
734 $SESSION->activegroup
[$courseid] = array(SEPARATEGROUPS
=>array(), VISIBLEGROUPS
=>array(), 'aag'=>array());
737 // make sure that the current group info is ok
738 if (array_key_exists($groupingid, $SESSION->activegroup
[$courseid][$groupmode]) and !array_key_exists($SESSION->activegroup
[$courseid][$groupmode][$groupingid], $allowedgroups)) {
739 // active group does not exist anymore or is 0
740 if ($SESSION->activegroup
[$courseid][$groupmode][$groupingid] > 0 or $groupmode == SEPARATEGROUPS
) {
741 // do not do this if all groups selected and groupmode is not separate
742 unset($SESSION->activegroup
[$courseid][$groupmode][$groupingid]);
746 // set up defaults if necessary
747 if (!array_key_exists($groupingid, $SESSION->activegroup
[$courseid][$groupmode])) {
748 if ($groupmode == 'aag') {
749 $SESSION->activegroup
[$courseid][$groupmode][$groupingid] = 0; // all groups by default if user has accessallgroups
751 } else if ($allowedgroups) {
752 if ($groupmode != SEPARATEGROUPS
and $mygroups = groups_get_all_groups($courseid, $USER->id
, $groupingid)) {
753 $firstgroup = reset($mygroups);
755 $firstgroup = reset($allowedgroups);
757 $SESSION->activegroup
[$courseid][$groupmode][$groupingid] = $firstgroup->id
;
760 // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet
761 // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum)
762 $SESSION->activegroup
[$courseid][$groupmode][$groupingid] = 0;