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