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