Commit | Line | Data |
---|---|---|
2c386f82 | 1 | <?php //$Id$ |
2 | ||
13534ef7 | 3 | /** |
ffc536af | 4 | * Groups not used in course or activity |
13534ef7 ML |
5 | */ |
6 | define('NOGROUPS', 0); | |
7 | ||
8 | /** | |
ffc536af | 9 | * Groups used, users do not see other groups |
13534ef7 ML |
10 | */ |
11 | define('SEPARATEGROUPS', 1); | |
5bf243d1 | 12 | |
5bf243d1 | 13 | /** |
ffc536af | 14 | * Groups used, students see other groups |
13534ef7 ML |
15 | */ |
16 | define('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 | */ |
25 | function 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 | */ | |
34 | function 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 | */ | |
43 | function 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 | */ | |
54 | function 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 | */ | |
68 | function 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 | */ | |
80 | function 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 | */ | |
89 | function 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 | 101 | function 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 | */ | |
137 | function 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 | */ | |
160 | function 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 | */ | |
176 | function 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 | 221 | function 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 | */ | |
239 | function 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 | */ | |
254 | function 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 | */ | |
264 | function 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 | */ | |
286 | function groups_print_course_menu($course, $urlroot, $return=false) { | |
287 | global $CFG, $USER; | |
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); | |
300 | } else { | |
301 | $allowedgroups = groups_get_all_groups($course->id, $USER->id); | |
302 | } | |
303 | ||
304 | $activegroup = groups_get_course_group($course, true); | |
305 | ||
306 | $groupsmenu = array(); | |
307 | if (!$allowedgroups or $groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) { | |
308 | $groupsmenu[0] = get_string('allparticipants'); | |
309 | } | |
310 | ||
311 | if ($allowedgroups) { | |
312 | foreach ($allowedgroups as $group) { | |
313 | $groupsmenu[$group->id] = format_string($group->name); | |
314 | } | |
315 | } | |
316 | ||
317 | if ($groupmode == VISIBLEGROUPS) { | |
318 | $grouplabel = get_string('groupsvisible'); | |
319 | } else { | |
320 | $grouplabel = get_string('groupsseparate'); | |
321 | } | |
322 | ||
323 | if (count($groupsmenu) == 1) { | |
324 | $groupname = reset($groupsmenu); | |
325 | $output = $grouplabel.': '.$groupname; | |
326 | } else { | |
327 | $output = popup_form($urlroot.'&group=', $groupsmenu, 'selectgroup', $activegroup, '', '', '', true, 'self', $grouplabel); | |
328 | } | |
329 | ||
330 | $output = '<div class="groupselector">'.$output.'</div>'; | |
331 | ||
332 | if ($return) { | |
333 | return $output; | |
334 | } else { | |
335 | echo $output; | |
336 | } | |
337 | } | |
338 | ||
13534ef7 ML |
339 | /** |
340 | * Print group menu selector for activity. | |
341 | * @param object $cm course module object | |
f16fa0a3 | 342 | * @param string $urlroot return address that users get to if they choose an option; |
18d43e96 | 343 | * should include any parameters needed, e.g. 'view.php?id=34' |
13534ef7 | 344 | * @param boolean $return return as string instead of printing |
f16fa0a3 | 345 | * @param boolean $hideallparticipants If true, this prevents the 'All participants' |
346 | * option from appearing in cases where it normally would. This is intended for | |
347 | * use only by activities that cannot display all groups together. (Note that | |
348 | * selecting this option does not prevent groups_get_activity_group from | |
349 | * returning 0; it will still do that if the user has chosen 'all participants' | |
350 | * in another activity, or not chosen anything.) | |
13534ef7 ML |
351 | * @return mixed void or string depending on $return param |
352 | */ | |
18d43e96 | 353 | function groups_print_activity_menu($cm, $urlroot, $return=false, $hideallparticipants=false) { |
c0d4238d | 354 | global $CFG, $USER; |
355 | ||
7e4fdf25 | 356 | // groupings are ignored when not enabled |
c0d4238d | 357 | if (empty($CFG->enablegroupings)) { |
358 | $cm->groupingid = 0; | |
359 | } | |
13534ef7 ML |
360 | |
361 | if (!$groupmode = groups_get_activity_groupmode($cm)) { | |
362 | if ($return) { | |
363 | return ''; | |
364 | } else { | |
365 | return; | |
366 | } | |
367 | } | |
368 | ||
369 | $context = get_context_instance(CONTEXT_MODULE, $cm->id); | |
370 | if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) { | |
371 | $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping (all if groupings not used) | |
372 | } else { | |
373 | $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups | |
374 | } | |
375 | ||
376 | $activegroup = groups_get_activity_group($cm, true); | |
377 | ||
378 | $groupsmenu = array(); | |
f16fa0a3 | 379 | if ((!$allowedgroups or $groupmode == VISIBLEGROUPS or |
18d43e96 | 380 | has_capability('moodle/site:accessallgroups', $context)) and !$hideallparticipants) { |
13534ef7 ML |
381 | $groupsmenu[0] = get_string('allparticipants'); |
382 | } | |
383 | ||
384 | if ($allowedgroups) { | |
385 | foreach ($allowedgroups as $group) { | |
386 | $groupsmenu[$group->id] = format_string($group->name); | |
387 | } | |
388 | } | |
389 | ||
390 | if ($groupmode == VISIBLEGROUPS) { | |
391 | $grouplabel = get_string('groupsvisible'); | |
392 | } else { | |
393 | $grouplabel = get_string('groupsseparate'); | |
394 | } | |
395 | ||
396 | if (count($groupsmenu) == 1) { | |
397 | $groupname = reset($groupsmenu); | |
398 | $output = $grouplabel.': '.$groupname; | |
399 | } else { | |
400 | $output = popup_form($urlroot.'&group=', $groupsmenu, 'selectgroup', $activegroup, '', '', '', true, 'self', $grouplabel); | |
401 | } | |
402 | ||
403 | $output = '<div class="groupselector">'.$output.'</div>'; | |
404 | ||
405 | if ($return) { | |
406 | return $output; | |
407 | } else { | |
408 | echo $output; | |
409 | } | |
410 | } | |
411 | ||
b2bc96d1 | 412 | /** |
413 | * Returns group active in course, changes the group by default if 'group' page param present | |
414 | * | |
415 | * @param object $course course bject | |
416 | * @param boolean $update change active group if group param submitted | |
417 | * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode) | |
418 | */ | |
419 | function groups_get_course_group($course, $update=false) { | |
420 | global $CFG, $USER, $SESSION; | |
421 | ||
422 | if (!$groupmode = $course->groupmode) { | |
423 | // NOGROUPS used | |
424 | return false; | |
425 | } | |
426 | ||
427 | // init activegroup array | |
428 | if (!array_key_exists('activegroup', $SESSION)) { | |
429 | $SESSION->activegroup = array(); | |
430 | } | |
431 | if (!array_key_exists($course->id, $SESSION->activegroup)) { | |
b0dcd128 | 432 | $SESSION->activegroup[$course->id] = array(SEPARATEGROUPS=>array(), VISIBLEGROUPS=>array(), 'aag'=>array()); |
433 | } | |
434 | ||
e873679b | 435 | $context = get_context_instance(CONTEXT_COURSE, $course->id); |
b0dcd128 | 436 | if (has_capability('moodle/site:accessallgroups', $context)) { |
437 | $groupmode = 'aag'; | |
b2bc96d1 | 438 | } |
439 | ||
440 | // grouping used the first time - add first user group as default | |
441 | if (!array_key_exists(0, $SESSION->activegroup[$course->id][$groupmode])) { | |
b0dcd128 | 442 | if ($groupmode == 'aag') { |
443 | $SESSION->activegroup[$course->id][$groupmode][0] = 0; // all groups by default if user has accessallgroups | |
444 | ||
445 | } else if ($usergroups = groups_get_all_groups($course->id, $USER->id, 0)) { | |
b2bc96d1 | 446 | $fistgroup = reset($usergroups); |
447 | $SESSION->activegroup[$course->id][$groupmode][0] = $fistgroup->id; | |
b0dcd128 | 448 | |
b2bc96d1 | 449 | } else { |
450 | // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet | |
451 | // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum) | |
452 | $SESSION->activegroup[$course->id][$groupmode][0] = 0; | |
453 | } | |
454 | } | |
455 | ||
456 | // set new active group if requested | |
457 | $changegroup = optional_param('group', -1, PARAM_INT); | |
458 | if ($update and $changegroup != -1) { | |
b2bc96d1 | 459 | |
460 | if ($changegroup == 0) { | |
461 | // do not allow changing to all groups without accessallgroups capability | |
b0dcd128 | 462 | if ($groupmode == VISIBLEGROUPS or $groupmode == 'aag') { |
b2bc96d1 | 463 | $SESSION->activegroup[$course->id][$groupmode][0] = 0; |
464 | } | |
465 | ||
466 | } else { | |
467 | // first make list of allowed groups | |
b0dcd128 | 468 | if ($groupmode == VISIBLEGROUPS or $groupmode == 'aag') { |
b2bc96d1 | 469 | $allowedgroups = groups_get_all_groups($course->id, 0, 0); |
470 | } else { | |
471 | $allowedgroups = groups_get_all_groups($course->id, $USER->id, 0); | |
472 | } | |
473 | ||
474 | if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) { | |
475 | $SESSION->activegroup[$course->id][$groupmode][0] = $changegroup; | |
476 | } | |
477 | } | |
478 | } | |
479 | ||
480 | return $SESSION->activegroup[$course->id][$groupmode][0]; | |
481 | } | |
482 | ||
13534ef7 ML |
483 | /** |
484 | * Returns group active in activity, changes the group by default if 'group' page param present | |
485 | * | |
486 | * @param object $cm course module object | |
487 | * @param boolean $update change active group if group param submitted | |
488 | * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode) | |
489 | */ | |
490 | function groups_get_activity_group($cm, $update=false) { | |
c0d4238d | 491 | global $CFG, $USER, $SESSION; |
492 | ||
7e4fdf25 | 493 | // groupings are ignored when not enabled |
c0d4238d | 494 | if (empty($CFG->enablegroupings)) { |
495 | $cm->groupingid = 0; | |
496 | } | |
13534ef7 ML |
497 | |
498 | if (!$groupmode = groups_get_activity_groupmode($cm)) { | |
499 | // NOGROUPS used | |
500 | return false; | |
501 | } | |
502 | ||
b2bc96d1 | 503 | // init activegroup array |
13534ef7 ML |
504 | if (!array_key_exists('activegroup', $SESSION)) { |
505 | $SESSION->activegroup = array(); | |
506 | } | |
507 | if (!array_key_exists($cm->course, $SESSION->activegroup)) { | |
b0dcd128 | 508 | $SESSION->activegroup[$cm->course] = array(SEPARATEGROUPS=>array(), VISIBLEGROUPS=>array(), 'aag'=>array()); |
509 | } | |
510 | ||
511 | $context = get_context_instance(CONTEXT_MODULE, $cm->id); | |
512 | if (has_capability('moodle/site:accessallgroups', $context)) { | |
513 | $groupmode = 'aag'; | |
13534ef7 ML |
514 | } |
515 | ||
516 | // grouping used the first time - add first user group as default | |
517 | if (!array_key_exists($cm->groupingid, $SESSION->activegroup[$cm->course][$groupmode])) { | |
b0dcd128 | 518 | if ($groupmode == 'aag') { |
519 | $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0; // all groups by default if user has accessallgroups | |
520 | ||
521 | } else if ($usergroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid)) { | |
13534ef7 ML |
522 | $fistgroup = reset($usergroups); |
523 | $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $fistgroup->id; | |
b0dcd128 | 524 | |
13534ef7 ML |
525 | } else { |
526 | // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet | |
527 | // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum) | |
528 | $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0; | |
529 | } | |
530 | } | |
531 | ||
532 | // set new active group if requested | |
533 | $changegroup = optional_param('group', -1, PARAM_INT); | |
534 | if ($update and $changegroup != -1) { | |
13534ef7 ML |
535 | |
536 | if ($changegroup == 0) { | |
b0dcd128 | 537 | // allgroups visible only in VISIBLEGROUPS or when accessallgroups |
538 | if ($groupmode == VISIBLEGROUPS or $groupmode == 'aag') { | |
13534ef7 ML |
539 | $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0; |
540 | } | |
541 | ||
542 | } else { | |
543 | // first make list of allowed groups | |
b0dcd128 | 544 | if ($groupmode == VISIBLEGROUPS or $groupmode == 'aag') { |
13534ef7 ML |
545 | $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping (all if groupings not used) |
546 | } else { | |
547 | $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups | |
548 | } | |
549 | ||
550 | if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) { | |
551 | $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $changegroup; | |
552 | } | |
553 | } | |
554 | } | |
555 | ||
556 | return $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid]; | |
557 | } | |
62d63838 | 558 | |
18d43e96 | 559 | /** |
f16fa0a3 | 560 | * Gets a list of groups that the user is allowed to access within the |
18d43e96 | 561 | * specified activity. |
562 | * @param object $cm Course-module | |
563 | * @param int $userid User ID (defaults to current user) | |
564 | * @return array An array of group objects, or false if none | |
565 | */ | |
cdaa9410 | 566 | function groups_get_activity_allowed_groups($cm,$userid=0) { |
18d43e96 | 567 | // Use current user by default |
568 | global $USER; | |
569 | if(!$userid) { | |
570 | $userid=$USER->id; | |
571 | } | |
f16fa0a3 | 572 | |
18d43e96 | 573 | // Get groupmode for activity, taking into account course settings |
574 | $groupmode=groups_get_activity_groupmode($cm); | |
575 | ||
576 | // If visible groups mode, or user has the accessallgroups capability, | |
577 | // then they can access all groups for the activity... | |
f16fa0a3 | 578 | $context = get_context_instance(CONTEXT_MODULE, $cm->id); |
18d43e96 | 579 | if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) { |
f16fa0a3 | 580 | return groups_get_all_groups($cm->course, 0, $cm->groupingid); |
18d43e96 | 581 | } else { |
582 | // ...otherwise they can only access groups they belong to | |
f16fa0a3 | 583 | return groups_get_all_groups($cm->course, $userid, $cm->groupingid); |
584 | } | |
18d43e96 | 585 | } |
586 | ||
dcd8db68 | 587 | /** |
588 | * Determine if a course module is currently visible to a user | |
589 | * @uses $USER If $userid is null, use the global object. | |
590 | * @param int $cm The course module | |
591 | * @param int $userid The user to check against the group. | |
592 | * @return boolean True if the user can view the course module, false otherwise. | |
593 | */ | |
594 | function groups_course_module_visible($cm, $userid=null) { | |
595 | global $CFG, $USER; | |
f16fa0a3 | 596 | |
dcd8db68 | 597 | if (empty($userid)) { |
598 | $userid = $USER->id; | |
599 | } | |
600 | if (empty($CFG->enablegroupings)) { | |
601 | return(true); | |
602 | } | |
603 | if (empty($cm->groupmembersonly)) { | |
604 | return(true); | |
605 | } | |
e0bc99e4 | 606 | if (groups_has_membership($cm, $userid) || has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE, $cm->id), $userid)) { |
dcd8db68 | 607 | return(true); |
608 | } | |
609 | return(false); | |
610 | } | |
611 | ||
2c386f82 | 612 | ?> |