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
25 defined('MOODLE_INTERNAL') ||
die();
28 * Groups not used in course or activity
30 define('NOGROUPS', 0);
33 * Groups used, users do not see other groups
35 define('SEPARATEGROUPS', 1);
38 * Groups used, students see other groups
40 define('VISIBLEGROUPS', 2);
44 * Determines if a group with a given groupid exists.
47 * @param int $groupid The groupid to check for
48 * @return boolean True if the group exists, false otherwise or if an error
51 function groups_group_exists($groupid) {
53 return $DB->record_exists('groups', array('id'=>$groupid));
57 * Gets the name of a group with a specified id
60 * @param int $groupid The id of the group
61 * @return string The name of the group
63 function groups_get_group_name($groupid) {
65 return $DB->get_field('groups', 'name', array('id'=>$groupid));
69 * Gets the name of a grouping with a specified id
72 * @param int $groupingid The id of the grouping
73 * @return string The name of the grouping
75 function groups_get_grouping_name($groupingid) {
77 return $DB->get_field('groupings', 'name', array('id'=>$groupingid));
81 * Returns the groupid of a group with the name specified for the course.
82 * Group names should be unique in course
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) {
91 if ($groups = $DB->get_records('groups', array('courseid'=>$courseid, 'name'=>$name))) {
98 * Returns the groupingid of a grouping with the name specified for the course.
99 * Grouping names should be unique in course
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) {
108 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid, 'name'=>$name))) {
109 return key($groupings);
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
) {
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
) {
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.*') {
150 if (empty($userid)) {
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;
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.
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)) {
195 $sql = "SELECT g.id, gg.groupingid
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);
205 $rs->close(); // Not going to iterate (but exit), close rs
206 return array('0' => array());
210 $allgroups = array();
212 foreach ($rs as $group) {
213 $allgroups[$group->id
] = $group->id
;
214 if (is_null($group->groupingid
)) {
217 if (!array_key_exists($group->groupingid
, $result)) {
218 $result[$group->groupingid
] = array();
220 $result[$group->groupingid
][$group->id
] = $group->id
;
224 $result['0'] = array_keys($allgroups); // all groups
230 * Gets array of all groupings in a specified course.
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) {
241 return $DB->get_records_sql("SELECT *
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.
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) {
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
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)) {
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
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
);
303 // no grouping used - check all groups in course
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.
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') {
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.
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') {
348 return $DB->get_records_sql("SELECT $fields
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.
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) {
378 // get course object (reuse COURSE if possible)
379 if (isset($course->id
) and $course->id
== $cm->course
) {
381 } else if ($cm->course
== $COURSE->id
) {
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.
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
) {
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]);
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]);
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');
464 $grouplabel = get_string('groupsseparate');
467 if (count($groupsmenu) == 1) {
468 $groupname = reset($groupsmenu);
469 $output = $grouplabel.': '.$groupname;
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>';
486 * Print group menu selector for activity.
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
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\');',
515 if (!$groupmode = groups_get_activity_groupmode($cm)) {
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
]);
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
]);
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');
575 $grouplabel = get_string('groupsseparate');
578 if (count($groupsmenu) == 1) {
579 $groupname = reset($groupsmenu);
580 $output = $grouplabel.': '.$groupname;
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>';
597 * Returns group active in course, changes the group by default if 'group' page param present
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
) {
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)) {
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
;
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;
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
);
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
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)) {
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)) {
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
;
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;
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)
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.
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
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
);
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.
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) {
788 if (empty($userid)) {
791 if (empty($CFG->enablegroupmembersonly
)) {
794 if (empty($cm->groupmembersonly
)) {
797 if (has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE
, $cm->id
), $userid) or groups_has_membership($cm, $userid)) {