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