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