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 | |
38 | /** | |
39 | * Returns the groupid of a group with the name specified for the course. | |
40 | * Group names should be unique in course | |
41 | * @param int $courseid The id of the course | |
42 | * @param string $name name of group (without magic quotes) | |
43 | * @return int $groupid | |
44 | */ | |
45 | function groups_get_group_by_name($courseid, $name) { | |
ddff2fa8 | 46 | if ($groups = get_records_select('groups', "courseid=$courseid AND name='".addslashes($name)."'")) { |
47 | return key($groups); | |
2c386f82 | 48 | } |
ddff2fa8 | 49 | return false; |
50 | } | |
2c386f82 | 51 | |
ddff2fa8 | 52 | /** |
53 | * Returns the groupingid of a grouping with the name specified for the course. | |
54 | * Grouping names should be unique in course | |
55 | * @param int $courseid The id of the course | |
56 | * @param string $name name of group (without magic quotes) | |
57 | * @return int $groupid | |
58 | */ | |
59 | function groups_get_grouping_by_name($courseid, $name) { | |
60 | if ($groupings = get_records_select('groupings', "courseid=$courseid AND name='".addslashes($name)."'")) { | |
61 | return key($groupings); | |
62 | } | |
63 | return false; | |
2c386f82 | 64 | } |
65 | ||
66 | /** | |
67 | * Get the group object | |
68 | * @param groupid ID of the group. | |
69 | * @return group object | |
70 | */ | |
71 | function groups_get_group($groupid) { | |
72 | return get_record('groups', 'id', $groupid); | |
73 | } | |
74 | ||
75 | /** | |
76 | * Gets array of all groups in a specified course. | |
77 | * @param int $courseid The id of the course. | |
78 | * @param int $userid optional user id, returns only groups of the user. | |
62d63838 | 79 | * @param int $groupingid optional returns only groups in the specified grouping. |
2c386f82 | 80 | * @return array | false Returns an array of the group IDs or false if no records |
81 | * or an error occurred. | |
82 | */ | |
62d63838 | 83 | function groups_get_all_groups($courseid, $userid=0, $groupingid=0) { |
2c386f82 | 84 | global $CFG; |
85 | ||
7e4fdf25 | 86 | // groupings are ignored when not enabled |
c0d4238d | 87 | if (empty($CFG->enablegroupings)) { |
88 | $groupingid = 0; | |
89 | } | |
90 | ||
62d63838 | 91 | if (!empty($userid)) { |
92 | $userfrom = ", {$CFG->prefix}groups_members gm"; | |
93 | $userwhere = "AND g.id = gm.groupid AND gm.userid = '$userid'"; | |
94 | } else { | |
95 | $userfrom = ""; | |
96 | $userwhere = ""; | |
97 | } | |
2c386f82 | 98 | |
62d63838 | 99 | if (!empty($groupingid)) { |
100 | $groupingfrom = ", {$CFG->prefix}groupings_groups gg"; | |
101 | $groupingwhere = "AND g.id = gg.groupid AND gg.groupingid = '$groupingid'"; | |
2c386f82 | 102 | } else { |
62d63838 | 103 | $groupingfrom = ""; |
104 | $groupingwhere = ""; | |
2c386f82 | 105 | } |
62d63838 | 106 | |
107 | return get_records_sql("SELECT g.* | |
108 | FROM {$CFG->prefix}groups g $userfrom $groupingfrom | |
109 | WHERE g.courseid = '$courseid' $userwhere $groupingwhere | |
110 | ORDER BY name ASC"); | |
2c386f82 | 111 | } |
112 | ||
113 | /** | |
114 | * Determines if the user is a member of the given group. | |
115 | * | |
116 | * @uses $USER If $userid is null, use the global object. | |
117 | * @param int $groupid The group to check for membership. | |
118 | * @param int $userid The user to check against the group. | |
119 | * @return boolean True if the user is a member, false otherwise. | |
120 | */ | |
121 | function groups_is_member($groupid, $userid=null) { | |
122 | global $USER; | |
123 | ||
124 | if (!$userid) { | |
125 | $userid = $USER->id; | |
126 | } | |
127 | ||
128 | return record_exists('groups_members', 'groupid', $groupid, 'userid', $userid); | |
129 | } | |
130 | ||
f8e3d5f0 | 131 | /** |
132 | * Determines if current or specified is member of any active group in activity | |
133 | * @param object $cm coruse module object | |
134 | * @param int $userid id of user, null menas $USER->id | |
135 | * @return booelan true if user member of at least one group used in activity | |
136 | */ | |
137 | function groups_has_membership($cm, $userid=null) { | |
138 | global $CFG, $USER; | |
e0bc99e4 | 139 | |
140 | static $cache = array(); | |
141 | ||
7e4fdf25 | 142 | // groupings are ignored when not enabled |
c0d4238d | 143 | if (empty($CFG->enablegroupings)) { |
144 | $cm->groupingid = 0; | |
145 | } | |
146 | ||
f8e3d5f0 | 147 | if (empty($userid)) { |
148 | $userid = $USER->id; | |
149 | } | |
150 | ||
e0bc99e4 | 151 | $cachekey = $userid.'|'.$cm->course.'|'.$cm->groupingid; |
152 | if (isset($cache[$cachekey])) { | |
153 | return($cache[$cachekey]); | |
154 | } | |
155 | ||
f8e3d5f0 | 156 | if ($cm->groupingid) { |
157 | // find out if member of any group in selected activity grouping | |
158 | $sql = "SELECT 'x' | |
159 | FROM {$CFG->prefix}groups_members gm, {$CFG->prefix}groupings_groups gg | |
160 | WHERE gm.userid = $userid AND gm.groupid = gg.groupid AND gg.groupingid = {$cm->groupingid}"; | |
161 | ||
162 | } else { | |
163 | // no grouping used - check all groups in course | |
164 | $sql = "SELECT 'x' | |
165 | FROM {$CFG->prefix}groups_members gm, {$CFG->prefix}groups g | |
166 | WHERE gm.userid = $userid AND gm.groupid = g.id AND g.courseid = {$cm->course}"; | |
167 | } | |
e0bc99e4 | 168 | |
169 | $cache[$cachekey] = record_exists_sql($sql); | |
170 | ||
171 | return $cache[$cachekey]; | |
f8e3d5f0 | 172 | } |
173 | ||
62d63838 | 174 | /** |
175 | * Returns the users in the specified group. | |
176 | * @param int $groupid The groupid to get the users for | |
e6839677 | 177 | * @param int $fields The fields to return |
62d63838 | 178 | * @param int $sort optional sorting of returned users |
179 | * @return array | false Returns an array of the users for the specified | |
180 | * group or false if no users or an error returned. | |
181 | */ | |
e6839677 | 182 | function groups_get_members($groupid, $fields='u.*', $sort='lastname ASC') { |
62d63838 | 183 | global $CFG; |
184 | ||
e6839677 | 185 | return get_records_sql("SELECT $fields |
62d63838 | 186 | FROM {$CFG->prefix}user u, {$CFG->prefix}groups_members gm |
187 | WHERE u.id = gm.userid AND gm.groupid = '$groupid' | |
188 | ORDER BY $sort"); | |
189 | } | |
190 | ||
e6839677 | 191 | |
192 | /** | |
193 | * Returns the users in the specified grouping. | |
194 | * @param int $groupingid The groupingid to get the users for | |
195 | * @param int $fields The fields to return | |
196 | * @param int $sort optional sorting of returned users | |
197 | * @return array | false Returns an array of the users for the specified | |
198 | * group or false if no users or an error returned. | |
199 | */ | |
200 | function groups_get_grouping_members($groupingid, $fields='u.*', $sort='lastname ASC') { | |
201 | global $CFG; | |
202 | ||
203 | return get_records_sql("SELECT $fields | |
204 | FROM {$CFG->prefix}user u | |
205 | INNER JOIN {$CFG->prefix}groups_members gm ON u.id = gm.userid | |
206 | INNER JOIN {$CFG->prefix}groupings_groups gg ON gm.groupid = gg.groupid | |
207 | WHERE gg.groupingid = $groupingid | |
208 | ORDER BY $sort"); | |
209 | } | |
210 | ||
b2bc96d1 | 211 | /** |
212 | * Returns effective groupmode used in course | |
213 | * @return integer group mode | |
214 | */ | |
215 | function groups_get_course_groupmode($course) { | |
216 | return $course->groupmode; | |
217 | } | |
218 | ||
13534ef7 ML |
219 | /** |
220 | * Returns effective groupmode used in activity, course setting | |
221 | * overrides activity setting if groupmodeforce enabled. | |
222 | * @return integer group mode | |
223 | */ | |
224 | function groups_get_activity_groupmode($cm) { | |
225 | global $COURSE; | |
226 | ||
227 | // get course object (reuse COURSE if possible) | |
228 | if ($cm->course == $COURSE->id) { | |
229 | $course = $COURSE; | |
230 | } else { | |
231 | if (!$course = get_record('course', 'id', $cm->course)) { | |
232 | error('Incorrect course id in cm'); | |
233 | } | |
234 | } | |
235 | ||
236 | return empty($course->groupmodeforce) ? $cm->groupmode : $course->groupmode; | |
237 | } | |
238 | ||
b2bc96d1 | 239 | /** |
240 | * Print group menu selector for course level. | |
241 | * @param object $course course object | |
242 | * @param string $urlroot return address | |
243 | * @param boolean $return return as string instead of printing | |
244 | * @return mixed void or string depending on $return param | |
245 | */ | |
246 | function groups_print_course_menu($course, $urlroot, $return=false) { | |
247 | global $CFG, $USER; | |
248 | ||
249 | if (!$groupmode = $course->groupmode) { | |
250 | if ($return) { | |
251 | return ''; | |
252 | } else { | |
253 | return; | |
254 | } | |
255 | } | |
256 | ||
257 | $context = get_context_instance(CONTEXT_COURSE, $course->id); | |
258 | if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) { | |
259 | $allowedgroups = groups_get_all_groups($course->id, 0); | |
260 | } else { | |
261 | $allowedgroups = groups_get_all_groups($course->id, $USER->id); | |
262 | } | |
263 | ||
264 | $activegroup = groups_get_course_group($course, true); | |
265 | ||
266 | $groupsmenu = array(); | |
267 | if (!$allowedgroups or $groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) { | |
268 | $groupsmenu[0] = get_string('allparticipants'); | |
269 | } | |
270 | ||
271 | if ($allowedgroups) { | |
272 | foreach ($allowedgroups as $group) { | |
273 | $groupsmenu[$group->id] = format_string($group->name); | |
274 | } | |
275 | } | |
276 | ||
277 | if ($groupmode == VISIBLEGROUPS) { | |
278 | $grouplabel = get_string('groupsvisible'); | |
279 | } else { | |
280 | $grouplabel = get_string('groupsseparate'); | |
281 | } | |
282 | ||
283 | if (count($groupsmenu) == 1) { | |
284 | $groupname = reset($groupsmenu); | |
285 | $output = $grouplabel.': '.$groupname; | |
286 | } else { | |
287 | $output = popup_form($urlroot.'&group=', $groupsmenu, 'selectgroup', $activegroup, '', '', '', true, 'self', $grouplabel); | |
288 | } | |
289 | ||
290 | $output = '<div class="groupselector">'.$output.'</div>'; | |
291 | ||
292 | if ($return) { | |
293 | return $output; | |
294 | } else { | |
295 | echo $output; | |
296 | } | |
297 | } | |
298 | ||
13534ef7 ML |
299 | /** |
300 | * Print group menu selector for activity. | |
301 | * @param object $cm course module object | |
302 | * @param string $urlroot return address | |
303 | * @param boolean $return return as string instead of printing | |
304 | * @return mixed void or string depending on $return param | |
305 | */ | |
306 | function groups_print_activity_menu($cm, $urlroot, $return=false) { | |
c0d4238d | 307 | global $CFG, $USER; |
308 | ||
7e4fdf25 | 309 | // groupings are ignored when not enabled |
c0d4238d | 310 | if (empty($CFG->enablegroupings)) { |
311 | $cm->groupingid = 0; | |
312 | } | |
13534ef7 ML |
313 | |
314 | if (!$groupmode = groups_get_activity_groupmode($cm)) { | |
315 | if ($return) { | |
316 | return ''; | |
317 | } else { | |
318 | return; | |
319 | } | |
320 | } | |
321 | ||
322 | $context = get_context_instance(CONTEXT_MODULE, $cm->id); | |
323 | if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) { | |
324 | $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping (all if groupings not used) | |
325 | } else { | |
326 | $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups | |
327 | } | |
328 | ||
329 | $activegroup = groups_get_activity_group($cm, true); | |
330 | ||
331 | $groupsmenu = array(); | |
332 | if (!$allowedgroups or $groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) { | |
333 | $groupsmenu[0] = get_string('allparticipants'); | |
334 | } | |
335 | ||
336 | if ($allowedgroups) { | |
337 | foreach ($allowedgroups as $group) { | |
338 | $groupsmenu[$group->id] = format_string($group->name); | |
339 | } | |
340 | } | |
341 | ||
342 | if ($groupmode == VISIBLEGROUPS) { | |
343 | $grouplabel = get_string('groupsvisible'); | |
344 | } else { | |
345 | $grouplabel = get_string('groupsseparate'); | |
346 | } | |
347 | ||
348 | if (count($groupsmenu) == 1) { | |
349 | $groupname = reset($groupsmenu); | |
350 | $output = $grouplabel.': '.$groupname; | |
351 | } else { | |
352 | $output = popup_form($urlroot.'&group=', $groupsmenu, 'selectgroup', $activegroup, '', '', '', true, 'self', $grouplabel); | |
353 | } | |
354 | ||
355 | $output = '<div class="groupselector">'.$output.'</div>'; | |
356 | ||
357 | if ($return) { | |
358 | return $output; | |
359 | } else { | |
360 | echo $output; | |
361 | } | |
362 | } | |
363 | ||
b2bc96d1 | 364 | /** |
365 | * Returns group active in course, changes the group by default if 'group' page param present | |
366 | * | |
367 | * @param object $course course bject | |
368 | * @param boolean $update change active group if group param submitted | |
369 | * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode) | |
370 | */ | |
371 | function groups_get_course_group($course, $update=false) { | |
372 | global $CFG, $USER, $SESSION; | |
373 | ||
374 | if (!$groupmode = $course->groupmode) { | |
375 | // NOGROUPS used | |
376 | return false; | |
377 | } | |
378 | ||
379 | // init activegroup array | |
380 | if (!array_key_exists('activegroup', $SESSION)) { | |
381 | $SESSION->activegroup = array(); | |
382 | } | |
383 | if (!array_key_exists($course->id, $SESSION->activegroup)) { | |
384 | $SESSION->activegroup[$course->id] = array(SEPARATEGROUPS=>array(), VISIBLEGROUPS=>array()); | |
385 | } | |
386 | ||
387 | // grouping used the first time - add first user group as default | |
388 | if (!array_key_exists(0, $SESSION->activegroup[$course->id][$groupmode])) { | |
389 | if ($usergroups = groups_get_all_groups($course->id, $USER->id, 0)) { | |
390 | $fistgroup = reset($usergroups); | |
391 | $SESSION->activegroup[$course->id][$groupmode][0] = $fistgroup->id; | |
392 | } else { | |
393 | // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet | |
394 | // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum) | |
395 | $SESSION->activegroup[$course->id][$groupmode][0] = 0; | |
396 | } | |
397 | } | |
398 | ||
399 | // set new active group if requested | |
400 | $changegroup = optional_param('group', -1, PARAM_INT); | |
401 | if ($update and $changegroup != -1) { | |
402 | $context = get_context_instance(CONTEXT_COURSE, $course->id); | |
403 | ||
404 | if ($changegroup == 0) { | |
405 | // do not allow changing to all groups without accessallgroups capability | |
406 | if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) { | |
407 | $SESSION->activegroup[$course->id][$groupmode][0] = 0; | |
408 | } | |
409 | ||
410 | } else { | |
411 | // first make list of allowed groups | |
412 | if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) { | |
413 | $allowedgroups = groups_get_all_groups($course->id, 0, 0); | |
414 | } else { | |
415 | $allowedgroups = groups_get_all_groups($course->id, $USER->id, 0); | |
416 | } | |
417 | ||
418 | if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) { | |
419 | $SESSION->activegroup[$course->id][$groupmode][0] = $changegroup; | |
420 | } | |
421 | } | |
422 | } | |
423 | ||
424 | return $SESSION->activegroup[$course->id][$groupmode][0]; | |
425 | } | |
426 | ||
13534ef7 ML |
427 | /** |
428 | * Returns group active in activity, changes the group by default if 'group' page param present | |
429 | * | |
430 | * @param object $cm course module object | |
431 | * @param boolean $update change active group if group param submitted | |
432 | * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode) | |
433 | */ | |
434 | function groups_get_activity_group($cm, $update=false) { | |
c0d4238d | 435 | global $CFG, $USER, $SESSION; |
436 | ||
7e4fdf25 | 437 | // groupings are ignored when not enabled |
c0d4238d | 438 | if (empty($CFG->enablegroupings)) { |
439 | $cm->groupingid = 0; | |
440 | } | |
13534ef7 ML |
441 | |
442 | if (!$groupmode = groups_get_activity_groupmode($cm)) { | |
443 | // NOGROUPS used | |
444 | return false; | |
445 | } | |
446 | ||
b2bc96d1 | 447 | // init activegroup array |
13534ef7 ML |
448 | if (!array_key_exists('activegroup', $SESSION)) { |
449 | $SESSION->activegroup = array(); | |
450 | } | |
451 | if (!array_key_exists($cm->course, $SESSION->activegroup)) { | |
452 | $SESSION->activegroup[$cm->course] = array(SEPARATEGROUPS=>array(), VISIBLEGROUPS=>array()); | |
453 | } | |
454 | ||
455 | // grouping used the first time - add first user group as default | |
456 | if (!array_key_exists($cm->groupingid, $SESSION->activegroup[$cm->course][$groupmode])) { | |
457 | if ($usergroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid)) { | |
458 | $fistgroup = reset($usergroups); | |
459 | $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $fistgroup->id; | |
460 | } else { | |
461 | // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet | |
462 | // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum) | |
463 | $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0; | |
464 | } | |
465 | } | |
466 | ||
467 | // set new active group if requested | |
468 | $changegroup = optional_param('group', -1, PARAM_INT); | |
469 | if ($update and $changegroup != -1) { | |
470 | $context = get_context_instance(CONTEXT_MODULE, $cm->id); | |
471 | ||
472 | if ($changegroup == 0) { | |
473 | // do not allow changing to all groups without accessallgroups capability | |
474 | if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) { | |
475 | $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0; | |
476 | } | |
477 | ||
478 | } else { | |
479 | // first make list of allowed groups | |
480 | if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) { | |
481 | $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping (all if groupings not used) | |
482 | } else { | |
483 | $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups | |
484 | } | |
485 | ||
486 | if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) { | |
487 | $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $changegroup; | |
488 | } | |
489 | } | |
490 | } | |
491 | ||
492 | return $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid]; | |
493 | } | |
62d63838 | 494 | |
dcd8db68 | 495 | /** |
496 | * Determine if a course module is currently visible to a user | |
497 | * @uses $USER If $userid is null, use the global object. | |
498 | * @param int $cm The course module | |
499 | * @param int $userid The user to check against the group. | |
500 | * @return boolean True if the user can view the course module, false otherwise. | |
501 | */ | |
502 | function groups_course_module_visible($cm, $userid=null) { | |
503 | global $CFG, $USER; | |
504 | ||
505 | if (empty($userid)) { | |
506 | $userid = $USER->id; | |
507 | } | |
508 | if (empty($CFG->enablegroupings)) { | |
509 | return(true); | |
510 | } | |
511 | if (empty($cm->groupmembersonly)) { | |
512 | return(true); | |
513 | } | |
e0bc99e4 | 514 | if (groups_has_membership($cm, $userid) || has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE, $cm->id), $userid)) { |
dcd8db68 | 515 | return(true); |
516 | } | |
517 | return(false); | |
518 | } | |
519 | ||
2c386f82 | 520 | ?> |