MDL-24419 (2): Moved groupings cache to get_all_groupings function
[moodle.git] / lib / grouplib.php
CommitLineData
e4f0a85e 1<?php
2
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.
14//
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/>.
17
18/**
78bfb562
PS
19 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
20 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4d8e2417 21 * @package core_group
e4f0a85e 22 */
2c386f82 23
78bfb562
PS
24defined('MOODLE_INTERNAL') || die();
25
13534ef7 26/**
ffc536af 27 * Groups not used in course or activity
13534ef7
ML
28 */
29define('NOGROUPS', 0);
30
31/**
ffc536af 32 * Groups used, users do not see other groups
13534ef7
ML
33 */
34define('SEPARATEGROUPS', 1);
5bf243d1 35
5bf243d1 36/**
ffc536af 37 * Groups used, students see other groups
13534ef7
ML
38 */
39define('VISIBLEGROUPS', 2);
40
41
42/**
43 * Determines if a group with a given groupid exists.
e4f0a85e 44 *
4d8e2417 45 * @category group
5bf243d1 46 * @param int $groupid The groupid to check for
4d8e2417 47 * @return bool True if the group exists, false otherwise or if an error
13534ef7 48 * occurred.
5bf243d1 49 */
50function groups_group_exists($groupid) {
f33e1ed4 51 global $DB;
52 return $DB->record_exists('groups', array('id'=>$groupid));
5bf243d1 53}
54
55/**
56 * Gets the name of a group with a specified id
e4f0a85e 57 *
4d8e2417 58 * @category group
5bf243d1 59 * @param int $groupid The id of the group
60 * @return string The name of the group
61 */
62function groups_get_group_name($groupid) {
f33e1ed4 63 global $DB;
64 return $DB->get_field('groups', 'name', array('id'=>$groupid));
5bf243d1 65}
2c386f82 66
4d8e3407 67/**
68 * Gets the name of a grouping with a specified id
e4f0a85e 69 *
4d8e2417 70 * @category group
4d8e3407 71 * @param int $groupingid The id of the grouping
72 * @return string The name of the grouping
73 */
74function groups_get_grouping_name($groupingid) {
f33e1ed4 75 global $DB;
76 return $DB->get_field('groupings', 'name', array('id'=>$groupingid));
4d8e3407 77}
78
2c386f82 79/**
80 * Returns the groupid of a group with the name specified for the course.
81 * Group names should be unique in course
e4f0a85e 82 *
4d8e2417 83 * @category group
2c386f82 84 * @param int $courseid The id of the course
85 * @param string $name name of group (without magic quotes)
86 * @return int $groupid
87 */
88function groups_get_group_by_name($courseid, $name) {
f33e1ed4 89 global $DB;
90 if ($groups = $DB->get_records('groups', array('courseid'=>$courseid, 'name'=>$name))) {
ddff2fa8 91 return key($groups);
2c386f82 92 }
ddff2fa8 93 return false;
94}
2c386f82 95
ddff2fa8 96/**
97 * Returns the groupingid of a grouping with the name specified for the course.
98 * Grouping names should be unique in course
e4f0a85e 99 *
4d8e2417 100 * @category group
ddff2fa8 101 * @param int $courseid The id of the course
102 * @param string $name name of group (without magic quotes)
103 * @return int $groupid
104 */
105function groups_get_grouping_by_name($courseid, $name) {
f33e1ed4 106 global $DB;
c70552d8 107 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid, 'name'=>$name))) {
ddff2fa8 108 return key($groupings);
109 }
110 return false;
2c386f82 111}
112
113/**
114 * Get the group object
e4f0a85e 115 *
4d8e2417 116 * @category group
e4f0a85e 117 * @param int $groupid ID of the group.
4d8e2417
AG
118 * @param string $fields (default is all fields)
119 * @param int $strictness (IGNORE_MISSING - default)
120 * @return stdGlass group object
2c386f82 121 */
9a0df45a 122function groups_get_group($groupid, $fields='*', $strictness=IGNORE_MISSING) {
f33e1ed4 123 global $DB;
9a0df45a 124 return $DB->get_record('groups', array('id'=>$groupid), $fields, $strictness);
2c386f82 125}
126
f16fa0a3 127/**
128 * Get the grouping object
e4f0a85e 129 *
4d8e2417 130 * @category group
e4f0a85e 131 * @param int $groupingid ID of the group.
9a0df45a 132 * @param string $fields
4d8e2417
AG
133 * @param int $strictness (IGNORE_MISSING - default)
134 * @return stdClass group object
f16fa0a3 135 */
9a0df45a 136function groups_get_grouping($groupingid, $fields='*', $strictness=IGNORE_MISSING) {
f33e1ed4 137 global $DB;
9a0df45a 138 return $DB->get_record('groupings', array('id'=>$groupingid), $fields, $strictness);
f16fa0a3 139}
140
2c386f82 141/**
142 * Gets array of all groups in a specified course.
e4f0a85e 143 *
4d8e2417 144 * @category group
2c386f82 145 * @param int $courseid The id of the course.
65bcf17b 146 * @param mixed $userid optional user id or array of ids, returns only groups of the user.
62d63838 147 * @param int $groupingid optional returns only groups in the specified grouping.
e4f0a85e 148 * @param string $fields
998ced08 149 * @return array Returns an array of the group objects (userid field returned if array in $userid)
2c386f82 150 */
65bcf17b 151function groups_get_all_groups($courseid, $userid=0, $groupingid=0, $fields='g.*') {
998ced08 152 global $DB;
2c386f82 153
65bcf17b 154 if (empty($userid)) {
62d63838 155 $userfrom = "";
156 $userwhere = "";
f33e1ed4 157 $params = array();
65bcf17b 158
159 } else {
f33e1ed4 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";
62d63838 163 }
2c386f82 164
62d63838 165 if (!empty($groupingid)) {
f33e1ed4 166 $groupingfrom = ", {groupings_groups} gg";
167 $groupingwhere = "AND g.id = gg.groupid AND gg.groupingid = ?";
168 $params[] = $groupingid;
2c386f82 169 } else {
62d63838 170 $groupingfrom = "";
171 $groupingwhere = "";
2c386f82 172 }
62d63838 173
f33e1ed4 174 array_unshift($params, $courseid);
175
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);
2c386f82 180}
181
dd97c328 182/**
183 * Returns info about user's groups in course.
e4f0a85e 184 *
4d8e2417 185 * @category group
dd97c328 186 * @param int $courseid
187 * @param int $userid $USER if not specified
e4f0a85e 188 * @return array Array[groupingid][groupid] including grouping id 0 which means all groups
dd97c328 189 */
190function groups_get_user_groups($courseid, $userid=0) {
998ced08 191 global $USER, $DB;
dd97c328 192
193 if (empty($userid)) {
194 $userid = $USER->id;
195 }
196
f33e1ed4 197 $sql = "SELECT g.id, gg.groupingid
198 FROM {groups} g
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);
203
b967c541
EL
204 $rs = $DB->get_recordset_sql($sql, $params);
205
206 if (!$rs->valid()) {
207 $rs->close(); // Not going to iterate (but exit), close rs
dd97c328 208 return array('0' => array());
209 }
210
0b943ef1 211 $result = array();
212 $allgroups = array();
117bd748 213
f33e1ed4 214 foreach ($rs as $group) {
ca182d90 215 $allgroups[$group->id] = $group->id;
dd97c328 216 if (is_null($group->groupingid)) {
217 continue;
218 }
219 if (!array_key_exists($group->groupingid, $result)) {
220 $result[$group->groupingid] = array();
221 }
222 $result[$group->groupingid][$group->id] = $group->id;
223 }
f33e1ed4 224 $rs->close();
0b943ef1 225
226 $result['0'] = array_keys($allgroups); // all groups
dd97c328 227
228 return $result;
229}
230
acf000b0 231/**
ae3fbf7b 232 * Gets an array of all groupings in a specified course. This value is cached
233 * for a single course (so you can call it repeatedly for the same course
234 * without a performance penalty).
117bd748 235 *
4d8e2417 236 * @category group
ae3fbf7b 237 * @param int $courseid return all groupings from course with this courseid
238 * @return array Returns an array of the grouping objects (empty if none)
acf000b0 239 */
240function groups_get_all_groupings($courseid) {
ae3fbf7b 241 global $CFG, $DB, $GROUPLIB_CACHE;
acf000b0 242
ae3fbf7b 243 // Use cached data if available. (Note: We only cache a single request, so
244 // as not to waste memory if processing is happening for multiple courses.)
245 if (!empty($GROUPLIB_CACHE->groupings) &&
246 $GROUPLIB_CACHE->groupings->courseid == $courseid) {
247 return $GROUPLIB_CACHE->groupings->result;
248 }
249 if (empty($GROUPLIB_CACHE)) {
250 $GROUPLIB_CACHE = new stdClass();
251 }
252 $GROUPLIB_CACHE->groupings = new stdClass();
253 $GROUPLIB_CACHE->groupings->courseid = $courseid;
254 $GROUPLIB_CACHE->groupings->result =
255 $DB->get_records('groupings', array('courseid' => $courseid), 'name ASC');
256 return $GROUPLIB_CACHE->groupings->result;
acf000b0 257}
258
2c386f82 259/**
260 * Determines if the user is a member of the given group.
261 *
aaeba371 262 * If $userid is null, use the global object.
263 *
4d8e2417 264 * @category group
2c386f82 265 * @param int $groupid The group to check for membership.
266 * @param int $userid The user to check against the group.
4d8e2417 267 * @return bool True if the user is a member, false otherwise.
2c386f82 268 */
269function groups_is_member($groupid, $userid=null) {
f33e1ed4 270 global $USER, $DB;
2c386f82 271
272 if (!$userid) {
273 $userid = $USER->id;
274 }
275
f33e1ed4 276 return $DB->record_exists('groups_members', array('groupid'=>$groupid, 'userid'=>$userid));
2c386f82 277}
278
f8e3d5f0 279/**
280 * Determines if current or specified is member of any active group in activity
e4f0a85e 281 *
4d8e2417 282 * @category group
e4f0a85e 283 * @staticvar array $cache
4d8e2417
AG
284 * @param cm_info $cm course module object
285 * @param int $userid id of user, null means $USER->id
286 * @return bool true if user member of at least one group used in activity
f8e3d5f0 287 */
288function groups_has_membership($cm, $userid=null) {
f33e1ed4 289 global $CFG, $USER, $DB;
f16fa0a3 290
e0bc99e4 291 static $cache = array();
f16fa0a3 292
f8e3d5f0 293 if (empty($userid)) {
294 $userid = $USER->id;
295 }
296
e0bc99e4 297 $cachekey = $userid.'|'.$cm->course.'|'.$cm->groupingid;
298 if (isset($cache[$cachekey])) {
299 return($cache[$cachekey]);
300 }
301
f8e3d5f0 302 if ($cm->groupingid) {
303 // find out if member of any group in selected activity grouping
304 $sql = "SELECT 'x'
f33e1ed4 305 FROM {groups_members} gm, {groupings_groups} gg
306 WHERE gm.userid = ? AND gm.groupid = gg.groupid AND gg.groupingid = ?";
307 $params = array($userid, $cm->groupingid);
f8e3d5f0 308
309 } else {
310 // no grouping used - check all groups in course
311 $sql = "SELECT 'x'
f33e1ed4 312 FROM {groups_members} gm, {groups} g
313 WHERE gm.userid = ? AND gm.groupid = g.id AND g.courseid = ?";
314 $params = array($userid, $cm->course);
f8e3d5f0 315 }
f16fa0a3 316
f33e1ed4 317 $cache[$cachekey] = $DB->record_exists_sql($sql, $params);
f16fa0a3 318
e0bc99e4 319 return $cache[$cachekey];
f8e3d5f0 320}
321
62d63838 322/**
323 * Returns the users in the specified group.
e4f0a85e 324 *
4d8e2417 325 * @category group
62d63838 326 * @param int $groupid The groupid to get the users for
e6839677 327 * @param int $fields The fields to return
62d63838 328 * @param int $sort optional sorting of returned users
e4f0a85e 329 * @return array|bool Returns an array of the users for the specified
62d63838 330 * group or false if no users or an error returned.
331 */
e6839677 332function groups_get_members($groupid, $fields='u.*', $sort='lastname ASC') {
f33e1ed4 333 global $DB;
62d63838 334
f33e1ed4 335 return $DB->get_records_sql("SELECT $fields
336 FROM {user} u, {groups_members} gm
337 WHERE u.id = gm.userid AND gm.groupid = ?
338 ORDER BY $sort", array($groupid));
62d63838 339}
340
e6839677 341
342/**
343 * Returns the users in the specified grouping.
e4f0a85e 344 *
4d8e2417 345 * @category group
e6839677 346 * @param int $groupingid The groupingid to get the users for
4d8e2417
AG
347 * @param string $fields The fields to return
348 * @param string $sort optional sorting of returned users
e4f0a85e 349 * @return array|bool Returns an array of the users for the specified
e6839677 350 * group or false if no users or an error returned.
351 */
352function groups_get_grouping_members($groupingid, $fields='u.*', $sort='lastname ASC') {
f33e1ed4 353 global $DB;
354
355 return $DB->get_records_sql("SELECT $fields
356 FROM {user} u
357 INNER JOIN {groups_members} gm ON u.id = gm.userid
358 INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid
359 WHERE gg.groupingid = ?
360 ORDER BY $sort", array($groupingid));
e6839677 361}
362
b2bc96d1 363/**
364 * Returns effective groupmode used in course
117bd748 365 *
4d8e2417
AG
366 * @category group
367 * @param stdClass $course course object.
368 * @return int group mode
b2bc96d1 369 */
370function groups_get_course_groupmode($course) {
371 return $course->groupmode;
372}
373
13534ef7
ML
374/**
375 * Returns effective groupmode used in activity, course setting
376 * overrides activity setting if groupmodeforce enabled.
e4f0a85e 377 *
4d8e2417
AG
378 * @category group
379 * @param cm_info $cm the course module object. Only the ->course and ->groupmode need to be set.
380 * @param stdClass $course object optional course object to improve perf
381 * @return int group mode
13534ef7 382 */
dd97c328 383function groups_get_activity_groupmode($cm, $course=null) {
f33e1ed4 384 global $COURSE, $DB;
13534ef7
ML
385
386 // get course object (reuse COURSE if possible)
dd97c328 387 if (isset($course->id) and $course->id == $cm->course) {
388 //ok
389 } else if ($cm->course == $COURSE->id) {
13534ef7
ML
390 $course = $COURSE;
391 } else {
f33e1ed4 392 if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
06e84d52 393 print_error('invalidcourseid');
13534ef7
ML
394 }
395 }
396
397 return empty($course->groupmodeforce) ? $cm->groupmode : $course->groupmode;
398}
399
b2bc96d1 400/**
401 * Print group menu selector for course level.
e4f0a85e 402 *
4d8e2417 403 * @category group
998ced08 404 * @param stdClass $course course object
4d8e2417
AG
405 * @param mixed $urlroot return address. Accepts either a string or a moodle_url
406 * @param bool $return return as string instead of printing
b2bc96d1 407 * @return mixed void or string depending on $return param
408 */
409function groups_print_course_menu($course, $urlroot, $return=false) {
998ced08 410 global $USER, $OUTPUT;
b2bc96d1 411
412 if (!$groupmode = $course->groupmode) {
413 if ($return) {
414 return '';
415 } else {
416 return;
417 }
418 }
1d57f862 419
b2bc96d1 420 $context = get_context_instance(CONTEXT_COURSE, $course->id);
998ced08 421 $aag = has_capability('moodle/site:accessallgroups', $context);
1d57f862 422
998ced08
PS
423 if ($groupmode == VISIBLEGROUPS or $aag) {
424 $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
1d57f862
PS
425 } else {
426 $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
1d57f862 427 }
b2bc96d1 428
998ced08 429 $activegroup = groups_get_course_group($course, true, $allowedgroups);
b2bc96d1 430
431 $groupsmenu = array();
998ced08 432 if (!$allowedgroups or $groupmode == VISIBLEGROUPS or $aag) {
b2bc96d1 433 $groupsmenu[0] = get_string('allparticipants');
434 }
435
436 if ($allowedgroups) {
437 foreach ($allowedgroups as $group) {
438 $groupsmenu[$group->id] = format_string($group->name);
439 }
440 }
441
442 if ($groupmode == VISIBLEGROUPS) {
443 $grouplabel = get_string('groupsvisible');
444 } else {
445 $grouplabel = get_string('groupsseparate');
446 }
447
998ced08
PS
448 if ($aag and $course->defaultgroupingid) {
449 if ($grouping = groups_get_grouping($course->defaultgroupingid)) {
450 $grouplabel = $grouplabel . ' (' . format_string($grouping->name) . ')';
451 }
452 }
453
b2bc96d1 454 if (count($groupsmenu) == 1) {
455 $groupname = reset($groupsmenu);
456 $output = $grouplabel.': '.$groupname;
457 } else {
f8dab966
PS
458 $select = new single_select(new moodle_url($urlroot), 'group', $groupsmenu, $activegroup, null, 'selectgroup');
459 $select->label = $grouplabel;
460 $output = $OUTPUT->render($select);
b2bc96d1 461 }
462
463 $output = '<div class="groupselector">'.$output.'</div>';
464
465 if ($return) {
466 return $output;
467 } else {
468 echo $output;
469 }
470}
471
13534ef7
ML
472/**
473 * Print group menu selector for activity.
e4f0a85e 474 *
4d8e2417 475 * @category group
998ced08
PS
476 * @param stdClass $cm course module object
477 * @param string|moodle_url $urlroot return address that users get to if they choose an option;
f1035deb 478 * should include any parameters needed, e.g. "$CFG->wwwroot/mod/forum/view.php?id=34"
4d8e2417
AG
479 * @param bool $return return as string instead of printing
480 * @param bool $hideallparticipants If true, this prevents the 'All participants'
f16fa0a3 481 * option from appearing in cases where it normally would. This is intended for
482 * use only by activities that cannot display all groups together. (Note that
483 * selecting this option does not prevent groups_get_activity_group from
484 * returning 0; it will still do that if the user has chosen 'all participants'
485 * in another activity, or not chosen anything.)
13534ef7
ML
486 * @return mixed void or string depending on $return param
487 */
18d43e96 488function groups_print_activity_menu($cm, $urlroot, $return=false, $hideallparticipants=false) {
998ced08 489 global $USER, $OUTPUT;
c0d4238d 490
998ced08
PS
491 if ($urlroot instanceof moodle_url) {
492 // no changes necessary
493
494 } else {
495 if (strpos($urlroot, 'http') !== 0) { // Will also work for https
496 // Display error if urlroot is not absolute (this causes the non-JS version to break)
497 debugging('groups_print_activity_menu requires absolute URL for ' .
498 '$urlroot, not <tt>' . s($urlroot) . '</tt>. Example: ' .
499 'groups_print_activity_menu($cm, $CFG->wwwroot . \'/mod/mymodule/view.php?id=13\');',
500 DEBUG_DEVELOPER);
501 }
502 $urlroot = new moodle_url($urlroot);
f1035deb
SM
503 }
504
13534ef7
ML
505 if (!$groupmode = groups_get_activity_groupmode($cm)) {
506 if ($return) {
507 return '';
508 } else {
509 return;
510 }
511 }
1d57f862 512
13534ef7 513 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
998ced08 514 $aag = has_capability('moodle/site:accessallgroups', $context);
1d57f862 515
998ced08
PS
516 if ($groupmode == VISIBLEGROUPS or $aag) {
517 $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping
1d57f862
PS
518 } else {
519 $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups
1d57f862
PS
520 }
521
998ced08 522 $activegroup = groups_get_activity_group($cm, true, $allowedgroups);
13534ef7
ML
523
524 $groupsmenu = array();
998ced08 525 if ((!$allowedgroups or $groupmode == VISIBLEGROUPS or $aag) and !$hideallparticipants) {
13534ef7
ML
526 $groupsmenu[0] = get_string('allparticipants');
527 }
528
529 if ($allowedgroups) {
530 foreach ($allowedgroups as $group) {
531 $groupsmenu[$group->id] = format_string($group->name);
532 }
533 }
534
535 if ($groupmode == VISIBLEGROUPS) {
536 $grouplabel = get_string('groupsvisible');
537 } else {
538 $grouplabel = get_string('groupsseparate');
539 }
540
998ced08
PS
541 if ($aag and $cm->groupingid) {
542 if ($grouping = groups_get_grouping($cm->groupingid)) {
543 $grouplabel = $grouplabel . ' (' . format_string($grouping->name) . ')';
544 }
545 }
546
13534ef7
ML
547 if (count($groupsmenu) == 1) {
548 $groupname = reset($groupsmenu);
549 $output = $grouplabel.': '.$groupname;
550 } else {
998ced08 551 $select = new single_select($urlroot, 'group', $groupsmenu, $activegroup, null, 'selectgroup');
f8dab966
PS
552 $select->label = $grouplabel;
553 $output = $OUTPUT->render($select);
13534ef7
ML
554 }
555
556 $output = '<div class="groupselector">'.$output.'</div>';
557
558 if ($return) {
559 return $output;
560 } else {
561 echo $output;
562 }
563}
564
b2bc96d1 565/**
566 * Returns group active in course, changes the group by default if 'group' page param present
567 *
4d8e2417 568 * @category group
998ced08 569 * @param stdClass $course course bject
4d8e2417 570 * @param bool $update change active group if group param submitted
998ced08 571 * @param array $allowedgroups list of groups user may access (INTERNAL, to be used only from groups_print_course_menu())
b2bc96d1 572 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
573 */
998ced08
PS
574function groups_get_course_group($course, $update=false, $allowedgroups=null) {
575 global $USER, $SESSION;
b2bc96d1 576
577 if (!$groupmode = $course->groupmode) {
578 // NOGROUPS used
579 return false;
580 }
581
e873679b 582 $context = get_context_instance(CONTEXT_COURSE, $course->id);
1d57f862 583 if (has_capability('moodle/site:accessallgroups', $context)) {
b0dcd128 584 $groupmode = 'aag';
b2bc96d1 585 }
586
998ced08
PS
587 if (!is_array($allowedgroups)) {
588 if ($groupmode == VISIBLEGROUPS or $groupmode === 'aag') {
589 $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
b2bc96d1 590 } else {
998ced08 591 $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
b2bc96d1 592 }
593 }
594
998ced08
PS
595 _group_verify_activegroup($course->id, $groupmode, $course->defaultgroupingid, $allowedgroups);
596
b2bc96d1 597 // set new active group if requested
598 $changegroup = optional_param('group', -1, PARAM_INT);
599 if ($update and $changegroup != -1) {
b2bc96d1 600
601 if ($changegroup == 0) {
602 // do not allow changing to all groups without accessallgroups capability
998ced08
PS
603 if ($groupmode == VISIBLEGROUPS or $groupmode === 'aag') {
604 $SESSION->activegroup[$course->id][$groupmode][$course->defaultgroupingid] = 0;
b2bc96d1 605 }
606
607 } else {
b2bc96d1 608 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
998ced08 609 $SESSION->activegroup[$course->id][$groupmode][$course->defaultgroupingid] = $changegroup;
b2bc96d1 610 }
611 }
612 }
613
998ced08 614 return $SESSION->activegroup[$course->id][$groupmode][$course->defaultgroupingid];
b2bc96d1 615}
616
13534ef7
ML
617/**
618 * Returns group active in activity, changes the group by default if 'group' page param present
619 *
4d8e2417 620 * @category group
998ced08 621 * @param stdClass $cm course module object
4d8e2417 622 * @param bool $update change active group if group param submitted
998ced08 623 * @param array $allowedgroups list of groups user may access (INTERNAL, to be used only from groups_print_activity_menu())
13534ef7
ML
624 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
625 */
998ced08
PS
626function groups_get_activity_group($cm, $update=false, $allowedgroups=null) {
627 global $USER, $SESSION;
c0d4238d 628
13534ef7
ML
629 if (!$groupmode = groups_get_activity_groupmode($cm)) {
630 // NOGROUPS used
631 return false;
632 }
633
b0dcd128 634 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
1d57f862 635 if (has_capability('moodle/site:accessallgroups', $context)) {
b0dcd128 636 $groupmode = 'aag';
13534ef7
ML
637 }
638
998ced08
PS
639 if (!is_array($allowedgroups)) {
640 if ($groupmode == VISIBLEGROUPS or $groupmode === 'aag') {
641 $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid);
13534ef7 642 } else {
998ced08 643 $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid);
13534ef7
ML
644 }
645 }
646
998ced08
PS
647 _group_verify_activegroup($cm->course, $groupmode, $cm->groupingid, $allowedgroups);
648
13534ef7
ML
649 // set new active group if requested
650 $changegroup = optional_param('group', -1, PARAM_INT);
651 if ($update and $changegroup != -1) {
13534ef7
ML
652
653 if ($changegroup == 0) {
b0dcd128 654 // allgroups visible only in VISIBLEGROUPS or when accessallgroups
998ced08 655 if ($groupmode == VISIBLEGROUPS or $groupmode === 'aag') {
13534ef7
ML
656 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0;
657 }
658
659 } else {
13534ef7
ML
660 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
661 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $changegroup;
662 }
663 }
664 }
665
666 return $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid];
667}
62d63838 668
18d43e96 669/**
f16fa0a3 670 * Gets a list of groups that the user is allowed to access within the
18d43e96 671 * specified activity.
e4f0a85e 672 *
4d8e2417
AG
673 * @category group
674 * @param stdClass $cm Course-module
18d43e96 675 * @param int $userid User ID (defaults to current user)
676 * @return array An array of group objects, or false if none
677 */
cdaa9410 678function groups_get_activity_allowed_groups($cm,$userid=0) {
18d43e96 679 // Use current user by default
680 global $USER;
681 if(!$userid) {
682 $userid=$USER->id;
683 }
f16fa0a3 684
18d43e96 685 // Get groupmode for activity, taking into account course settings
686 $groupmode=groups_get_activity_groupmode($cm);
687
688 // If visible groups mode, or user has the accessallgroups capability,
689 // then they can access all groups for the activity...
f16fa0a3 690 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
18d43e96 691 if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
f16fa0a3 692 return groups_get_all_groups($cm->course, 0, $cm->groupingid);
18d43e96 693 } else {
694 // ...otherwise they can only access groups they belong to
f16fa0a3 695 return groups_get_all_groups($cm->course, $userid, $cm->groupingid);
696 }
18d43e96 697}
698
dcd8db68 699/**
700 * Determine if a course module is currently visible to a user
aaeba371 701 *
702 * $USER If $userid is null, use the global object.
703 *
4d8e2417
AG
704 * @category group
705 * @param stdClass $cm The course module
dcd8db68 706 * @param int $userid The user to check against the group.
4d8e2417 707 * @return bool True if the user can view the course module, false otherwise.
dcd8db68 708 */
709function groups_course_module_visible($cm, $userid=null) {
710 global $CFG, $USER;
f16fa0a3 711
dcd8db68 712 if (empty($userid)) {
713 $userid = $USER->id;
714 }
98da6021 715 if (empty($CFG->enablegroupmembersonly)) {
dd97c328 716 return true;
dcd8db68 717 }
718 if (empty($cm->groupmembersonly)) {
dd97c328 719 return true;
dcd8db68 720 }
dd97c328 721 if (has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE, $cm->id), $userid) or groups_has_membership($cm, $userid)) {
722 return true;
dcd8db68 723 }
dd97c328 724 return false;
dcd8db68 725}
998ced08
PS
726
727/**
728 * Internal method, sets up $SESSION->activegroup and verifies previous value
729 *
998ced08
PS
730 * @param int $courseid
731 * @param int|string $groupmode SEPARATEGROUPS, VISIBLEGROUPS or 'aag' (access all groups)
732 * @param int $groupingid 0 means all groups
4d8e2417 733 * @param array $allowedgroups list of groups user can see
998ced08
PS
734 */
735function _group_verify_activegroup($courseid, $groupmode, $groupingid, array $allowedgroups) {
c160da9c 736 global $SESSION, $USER;
998ced08
PS
737
738 // init activegroup array if necessary
739 if (!isset($SESSION->activegroup)) {
740 $SESSION->activegroup = array();
741 }
742 if (!array_key_exists($courseid, $SESSION->activegroup)) {
743 $SESSION->activegroup[$courseid] = array(SEPARATEGROUPS=>array(), VISIBLEGROUPS=>array(), 'aag'=>array());
744 }
745
746 // make sure that the current group info is ok
747 if (array_key_exists($groupingid, $SESSION->activegroup[$courseid][$groupmode]) and !array_key_exists($SESSION->activegroup[$courseid][$groupmode][$groupingid], $allowedgroups)) {
748 // active group does not exist anymore or is 0
749 if ($SESSION->activegroup[$courseid][$groupmode][$groupingid] > 0 or $groupmode == SEPARATEGROUPS) {
750 // do not do this if all groups selected and groupmode is not separate
751 unset($SESSION->activegroup[$courseid][$groupmode][$groupingid]);
752 }
753 }
754
755 // set up defaults if necessary
756 if (!array_key_exists($groupingid, $SESSION->activegroup[$courseid][$groupmode])) {
757 if ($groupmode == 'aag') {
758 $SESSION->activegroup[$courseid][$groupmode][$groupingid] = 0; // all groups by default if user has accessallgroups
759
760 } else if ($allowedgroups) {
c160da9c
PS
761 if ($groupmode != SEPARATEGROUPS and $mygroups = groups_get_all_groups($courseid, $USER->id, $groupingid)) {
762 $firstgroup = reset($mygroups);
763 } else {
764 $firstgroup = reset($allowedgroups);
765 }
998ced08
PS
766 $SESSION->activegroup[$courseid][$groupmode][$groupingid] = $firstgroup->id;
767
768 } else {
769 // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet
770 // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum)
771 $SESSION->activegroup[$courseid][$groupmode][$groupingid] = 0;
772 }
773 }
774}