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