weekly release 2.3dev
[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/**
4d8e2417 232 * Gets an array of all groupings in a specified course.
117bd748 233 *
4d8e2417 234 * @category group
18d43e96 235 * @param int $courseid return only groupings in this with this courseid
e4f0a85e 236 * @return array|bool Returns an array of the grouping objects or false if no records
acf000b0 237 * or an error occurred.
238 */
239function groups_get_all_groupings($courseid) {
f33e1ed4 240 global $CFG, $DB;
acf000b0 241
f33e1ed4 242 return $DB->get_records_sql("SELECT *
243 FROM {groupings}
244 WHERE courseid = ?
245 ORDER BY name ASC", array($courseid));
acf000b0 246}
247
248
249
2c386f82 250/**
251 * Determines if the user is a member of the given group.
252 *
aaeba371 253 * If $userid is null, use the global object.
254 *
4d8e2417 255 * @category group
2c386f82 256 * @param int $groupid The group to check for membership.
257 * @param int $userid The user to check against the group.
4d8e2417 258 * @return bool True if the user is a member, false otherwise.
2c386f82 259 */
260function groups_is_member($groupid, $userid=null) {
f33e1ed4 261 global $USER, $DB;
2c386f82 262
263 if (!$userid) {
264 $userid = $USER->id;
265 }
266
f33e1ed4 267 return $DB->record_exists('groups_members', array('groupid'=>$groupid, 'userid'=>$userid));
2c386f82 268}
269
f8e3d5f0 270/**
271 * Determines if current or specified is member of any active group in activity
e4f0a85e 272 *
4d8e2417 273 * @category group
e4f0a85e 274 * @staticvar array $cache
4d8e2417
AG
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
f8e3d5f0 278 */
279function groups_has_membership($cm, $userid=null) {
f33e1ed4 280 global $CFG, $USER, $DB;
f16fa0a3 281
e0bc99e4 282 static $cache = array();
f16fa0a3 283
f8e3d5f0 284 if (empty($userid)) {
285 $userid = $USER->id;
286 }
287
e0bc99e4 288 $cachekey = $userid.'|'.$cm->course.'|'.$cm->groupingid;
289 if (isset($cache[$cachekey])) {
290 return($cache[$cachekey]);
291 }
292
f8e3d5f0 293 if ($cm->groupingid) {
294 // find out if member of any group in selected activity grouping
295 $sql = "SELECT 'x'
f33e1ed4 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);
f8e3d5f0 299
300 } else {
301 // no grouping used - check all groups in course
302 $sql = "SELECT 'x'
f33e1ed4 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);
f8e3d5f0 306 }
f16fa0a3 307
f33e1ed4 308 $cache[$cachekey] = $DB->record_exists_sql($sql, $params);
f16fa0a3 309
e0bc99e4 310 return $cache[$cachekey];
f8e3d5f0 311}
312
62d63838 313/**
314 * Returns the users in the specified group.
e4f0a85e 315 *
4d8e2417 316 * @category group
62d63838 317 * @param int $groupid The groupid to get the users for
e6839677 318 * @param int $fields The fields to return
62d63838 319 * @param int $sort optional sorting of returned users
e4f0a85e 320 * @return array|bool Returns an array of the users for the specified
62d63838 321 * group or false if no users or an error returned.
322 */
e6839677 323function groups_get_members($groupid, $fields='u.*', $sort='lastname ASC') {
f33e1ed4 324 global $DB;
62d63838 325
f33e1ed4 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));
62d63838 330}
331
e6839677 332
333/**
334 * Returns the users in the specified grouping.
e4f0a85e 335 *
4d8e2417 336 * @category group
e6839677 337 * @param int $groupingid The groupingid to get the users for
4d8e2417
AG
338 * @param string $fields The fields to return
339 * @param string $sort optional sorting of returned users
e4f0a85e 340 * @return array|bool Returns an array of the users for the specified
e6839677 341 * group or false if no users or an error returned.
342 */
343function groups_get_grouping_members($groupingid, $fields='u.*', $sort='lastname ASC') {
f33e1ed4 344 global $DB;
345
346 return $DB->get_records_sql("SELECT $fields
347 FROM {user} u
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));
e6839677 352}
353
b2bc96d1 354/**
355 * Returns effective groupmode used in course
117bd748 356 *
4d8e2417
AG
357 * @category group
358 * @param stdClass $course course object.
359 * @return int group mode
b2bc96d1 360 */
361function groups_get_course_groupmode($course) {
362 return $course->groupmode;
363}
364
13534ef7
ML
365/**
366 * Returns effective groupmode used in activity, course setting
367 * overrides activity setting if groupmodeforce enabled.
e4f0a85e 368 *
4d8e2417
AG
369 * @category group
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
13534ef7 373 */
dd97c328 374function groups_get_activity_groupmode($cm, $course=null) {
f33e1ed4 375 global $COURSE, $DB;
13534ef7
ML
376
377 // get course object (reuse COURSE if possible)
dd97c328 378 if (isset($course->id) and $course->id == $cm->course) {
379 //ok
380 } else if ($cm->course == $COURSE->id) {
13534ef7
ML
381 $course = $COURSE;
382 } else {
f33e1ed4 383 if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
06e84d52 384 print_error('invalidcourseid');
13534ef7
ML
385 }
386 }
387
388 return empty($course->groupmodeforce) ? $cm->groupmode : $course->groupmode;
389}
390
b2bc96d1 391/**
392 * Print group menu selector for course level.
e4f0a85e 393 *
4d8e2417 394 * @category group
998ced08 395 * @param stdClass $course course object
4d8e2417
AG
396 * @param mixed $urlroot return address. Accepts either a string or a moodle_url
397 * @param bool $return return as string instead of printing
b2bc96d1 398 * @return mixed void or string depending on $return param
399 */
400function groups_print_course_menu($course, $urlroot, $return=false) {
998ced08 401 global $USER, $OUTPUT;
b2bc96d1 402
403 if (!$groupmode = $course->groupmode) {
404 if ($return) {
405 return '';
406 } else {
407 return;
408 }
409 }
1d57f862 410
b2bc96d1 411 $context = get_context_instance(CONTEXT_COURSE, $course->id);
998ced08 412 $aag = has_capability('moodle/site:accessallgroups', $context);
1d57f862 413
998ced08
PS
414 if ($groupmode == VISIBLEGROUPS or $aag) {
415 $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
1d57f862
PS
416 } else {
417 $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
1d57f862 418 }
b2bc96d1 419
998ced08 420 $activegroup = groups_get_course_group($course, true, $allowedgroups);
b2bc96d1 421
422 $groupsmenu = array();
998ced08 423 if (!$allowedgroups or $groupmode == VISIBLEGROUPS or $aag) {
b2bc96d1 424 $groupsmenu[0] = get_string('allparticipants');
425 }
426
427 if ($allowedgroups) {
428 foreach ($allowedgroups as $group) {
429 $groupsmenu[$group->id] = format_string($group->name);
430 }
431 }
432
433 if ($groupmode == VISIBLEGROUPS) {
434 $grouplabel = get_string('groupsvisible');
435 } else {
436 $grouplabel = get_string('groupsseparate');
437 }
438
998ced08
PS
439 if ($aag and $course->defaultgroupingid) {
440 if ($grouping = groups_get_grouping($course->defaultgroupingid)) {
441 $grouplabel = $grouplabel . ' (' . format_string($grouping->name) . ')';
442 }
443 }
444
b2bc96d1 445 if (count($groupsmenu) == 1) {
446 $groupname = reset($groupsmenu);
447 $output = $grouplabel.': '.$groupname;
448 } else {
f8dab966
PS
449 $select = new single_select(new moodle_url($urlroot), 'group', $groupsmenu, $activegroup, null, 'selectgroup');
450 $select->label = $grouplabel;
451 $output = $OUTPUT->render($select);
b2bc96d1 452 }
453
454 $output = '<div class="groupselector">'.$output.'</div>';
455
456 if ($return) {
457 return $output;
458 } else {
459 echo $output;
460 }
461}
462
13534ef7
ML
463/**
464 * Print group menu selector for activity.
e4f0a85e 465 *
4d8e2417 466 * @category group
998ced08
PS
467 * @param stdClass $cm course module object
468 * @param string|moodle_url $urlroot return address that users get to if they choose an option;
f1035deb 469 * should include any parameters needed, e.g. "$CFG->wwwroot/mod/forum/view.php?id=34"
4d8e2417
AG
470 * @param bool $return return as string instead of printing
471 * @param bool $hideallparticipants If true, this prevents the 'All participants'
f16fa0a3 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.)
13534ef7
ML
477 * @return mixed void or string depending on $return param
478 */
18d43e96 479function groups_print_activity_menu($cm, $urlroot, $return=false, $hideallparticipants=false) {
998ced08 480 global $USER, $OUTPUT;
c0d4238d 481
998ced08
PS
482 if ($urlroot instanceof moodle_url) {
483 // no changes necessary
484
485 } else {
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\');',
491 DEBUG_DEVELOPER);
492 }
493 $urlroot = new moodle_url($urlroot);
f1035deb
SM
494 }
495
13534ef7
ML
496 if (!$groupmode = groups_get_activity_groupmode($cm)) {
497 if ($return) {
498 return '';
499 } else {
500 return;
501 }
502 }
1d57f862 503
13534ef7 504 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
998ced08 505 $aag = has_capability('moodle/site:accessallgroups', $context);
1d57f862 506
998ced08
PS
507 if ($groupmode == VISIBLEGROUPS or $aag) {
508 $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping
1d57f862
PS
509 } else {
510 $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups
1d57f862
PS
511 }
512
998ced08 513 $activegroup = groups_get_activity_group($cm, true, $allowedgroups);
13534ef7
ML
514
515 $groupsmenu = array();
998ced08 516 if ((!$allowedgroups or $groupmode == VISIBLEGROUPS or $aag) and !$hideallparticipants) {
13534ef7
ML
517 $groupsmenu[0] = get_string('allparticipants');
518 }
519
520 if ($allowedgroups) {
521 foreach ($allowedgroups as $group) {
522 $groupsmenu[$group->id] = format_string($group->name);
523 }
524 }
525
526 if ($groupmode == VISIBLEGROUPS) {
527 $grouplabel = get_string('groupsvisible');
528 } else {
529 $grouplabel = get_string('groupsseparate');
530 }
531
998ced08
PS
532 if ($aag and $cm->groupingid) {
533 if ($grouping = groups_get_grouping($cm->groupingid)) {
534 $grouplabel = $grouplabel . ' (' . format_string($grouping->name) . ')';
535 }
536 }
537
13534ef7
ML
538 if (count($groupsmenu) == 1) {
539 $groupname = reset($groupsmenu);
540 $output = $grouplabel.': '.$groupname;
541 } else {
998ced08 542 $select = new single_select($urlroot, 'group', $groupsmenu, $activegroup, null, 'selectgroup');
f8dab966
PS
543 $select->label = $grouplabel;
544 $output = $OUTPUT->render($select);
13534ef7
ML
545 }
546
547 $output = '<div class="groupselector">'.$output.'</div>';
548
549 if ($return) {
550 return $output;
551 } else {
552 echo $output;
553 }
554}
555
b2bc96d1 556/**
557 * Returns group active in course, changes the group by default if 'group' page param present
558 *
4d8e2417 559 * @category group
998ced08 560 * @param stdClass $course course bject
4d8e2417 561 * @param bool $update change active group if group param submitted
998ced08 562 * @param array $allowedgroups list of groups user may access (INTERNAL, to be used only from groups_print_course_menu())
b2bc96d1 563 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
564 */
998ced08
PS
565function groups_get_course_group($course, $update=false, $allowedgroups=null) {
566 global $USER, $SESSION;
b2bc96d1 567
568 if (!$groupmode = $course->groupmode) {
569 // NOGROUPS used
570 return false;
571 }
572
e873679b 573 $context = get_context_instance(CONTEXT_COURSE, $course->id);
1d57f862 574 if (has_capability('moodle/site:accessallgroups', $context)) {
b0dcd128 575 $groupmode = 'aag';
b2bc96d1 576 }
577
998ced08
PS
578 if (!is_array($allowedgroups)) {
579 if ($groupmode == VISIBLEGROUPS or $groupmode === 'aag') {
580 $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
b2bc96d1 581 } else {
998ced08 582 $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
b2bc96d1 583 }
584 }
585
998ced08
PS
586 _group_verify_activegroup($course->id, $groupmode, $course->defaultgroupingid, $allowedgroups);
587
b2bc96d1 588 // set new active group if requested
589 $changegroup = optional_param('group', -1, PARAM_INT);
590 if ($update and $changegroup != -1) {
b2bc96d1 591
592 if ($changegroup == 0) {
593 // do not allow changing to all groups without accessallgroups capability
998ced08
PS
594 if ($groupmode == VISIBLEGROUPS or $groupmode === 'aag') {
595 $SESSION->activegroup[$course->id][$groupmode][$course->defaultgroupingid] = 0;
b2bc96d1 596 }
597
598 } else {
b2bc96d1 599 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
998ced08 600 $SESSION->activegroup[$course->id][$groupmode][$course->defaultgroupingid] = $changegroup;
b2bc96d1 601 }
602 }
603 }
604
998ced08 605 return $SESSION->activegroup[$course->id][$groupmode][$course->defaultgroupingid];
b2bc96d1 606}
607
13534ef7
ML
608/**
609 * Returns group active in activity, changes the group by default if 'group' page param present
610 *
4d8e2417 611 * @category group
998ced08 612 * @param stdClass $cm course module object
4d8e2417 613 * @param bool $update change active group if group param submitted
998ced08 614 * @param array $allowedgroups list of groups user may access (INTERNAL, to be used only from groups_print_activity_menu())
13534ef7
ML
615 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
616 */
998ced08
PS
617function groups_get_activity_group($cm, $update=false, $allowedgroups=null) {
618 global $USER, $SESSION;
c0d4238d 619
13534ef7
ML
620 if (!$groupmode = groups_get_activity_groupmode($cm)) {
621 // NOGROUPS used
622 return false;
623 }
624
b0dcd128 625 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
1d57f862 626 if (has_capability('moodle/site:accessallgroups', $context)) {
b0dcd128 627 $groupmode = 'aag';
13534ef7
ML
628 }
629
998ced08
PS
630 if (!is_array($allowedgroups)) {
631 if ($groupmode == VISIBLEGROUPS or $groupmode === 'aag') {
632 $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid);
13534ef7 633 } else {
998ced08 634 $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid);
13534ef7
ML
635 }
636 }
637
998ced08
PS
638 _group_verify_activegroup($cm->course, $groupmode, $cm->groupingid, $allowedgroups);
639
13534ef7
ML
640 // set new active group if requested
641 $changegroup = optional_param('group', -1, PARAM_INT);
642 if ($update and $changegroup != -1) {
13534ef7
ML
643
644 if ($changegroup == 0) {
b0dcd128 645 // allgroups visible only in VISIBLEGROUPS or when accessallgroups
998ced08 646 if ($groupmode == VISIBLEGROUPS or $groupmode === 'aag') {
13534ef7
ML
647 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0;
648 }
649
650 } else {
13534ef7
ML
651 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
652 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $changegroup;
653 }
654 }
655 }
656
657 return $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid];
658}
62d63838 659
18d43e96 660/**
f16fa0a3 661 * Gets a list of groups that the user is allowed to access within the
18d43e96 662 * specified activity.
e4f0a85e 663 *
4d8e2417
AG
664 * @category group
665 * @param stdClass $cm Course-module
18d43e96 666 * @param int $userid User ID (defaults to current user)
667 * @return array An array of group objects, or false if none
668 */
cdaa9410 669function groups_get_activity_allowed_groups($cm,$userid=0) {
18d43e96 670 // Use current user by default
671 global $USER;
672 if(!$userid) {
673 $userid=$USER->id;
674 }
f16fa0a3 675
18d43e96 676 // Get groupmode for activity, taking into account course settings
677 $groupmode=groups_get_activity_groupmode($cm);
678
679 // If visible groups mode, or user has the accessallgroups capability,
680 // then they can access all groups for the activity...
f16fa0a3 681 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
18d43e96 682 if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
f16fa0a3 683 return groups_get_all_groups($cm->course, 0, $cm->groupingid);
18d43e96 684 } else {
685 // ...otherwise they can only access groups they belong to
f16fa0a3 686 return groups_get_all_groups($cm->course, $userid, $cm->groupingid);
687 }
18d43e96 688}
689
dcd8db68 690/**
691 * Determine if a course module is currently visible to a user
aaeba371 692 *
693 * $USER If $userid is null, use the global object.
694 *
4d8e2417
AG
695 * @category group
696 * @param stdClass $cm The course module
dcd8db68 697 * @param int $userid The user to check against the group.
4d8e2417 698 * @return bool True if the user can view the course module, false otherwise.
dcd8db68 699 */
700function groups_course_module_visible($cm, $userid=null) {
701 global $CFG, $USER;
f16fa0a3 702
dcd8db68 703 if (empty($userid)) {
704 $userid = $USER->id;
705 }
98da6021 706 if (empty($CFG->enablegroupmembersonly)) {
dd97c328 707 return true;
dcd8db68 708 }
709 if (empty($cm->groupmembersonly)) {
dd97c328 710 return true;
dcd8db68 711 }
dd97c328 712 if (has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE, $cm->id), $userid) or groups_has_membership($cm, $userid)) {
713 return true;
dcd8db68 714 }
dd97c328 715 return false;
dcd8db68 716}
998ced08
PS
717
718/**
719 * Internal method, sets up $SESSION->activegroup and verifies previous value
720 *
998ced08
PS
721 * @param int $courseid
722 * @param int|string $groupmode SEPARATEGROUPS, VISIBLEGROUPS or 'aag' (access all groups)
723 * @param int $groupingid 0 means all groups
4d8e2417 724 * @param array $allowedgroups list of groups user can see
998ced08
PS
725 */
726function _group_verify_activegroup($courseid, $groupmode, $groupingid, array $allowedgroups) {
c160da9c 727 global $SESSION, $USER;
998ced08
PS
728
729 // init activegroup array if necessary
730 if (!isset($SESSION->activegroup)) {
731 $SESSION->activegroup = array();
732 }
733 if (!array_key_exists($courseid, $SESSION->activegroup)) {
734 $SESSION->activegroup[$courseid] = array(SEPARATEGROUPS=>array(), VISIBLEGROUPS=>array(), 'aag'=>array());
735 }
736
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]);
743 }
744 }
745
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
750
751 } else if ($allowedgroups) {
c160da9c
PS
752 if ($groupmode != SEPARATEGROUPS and $mygroups = groups_get_all_groups($courseid, $USER->id, $groupingid)) {
753 $firstgroup = reset($mygroups);
754 } else {
755 $firstgroup = reset($allowedgroups);
756 }
998ced08
PS
757 $SESSION->activegroup[$courseid][$groupmode][$groupingid] = $firstgroup->id;
758
759 } else {
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;
763 }
764 }
765}