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