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