4 * Groups not used in course or activity
9 * Groups used, users do not see other groups
11 define('SEPARATEGROUPS', 1);
14 * Groups used, students see other groups
16 define('VISIBLEGROUPS', 2);
20 * Determines if a group with a given groupid exists.
21 * @param int $groupid The groupid to check for
22 * @return boolean True if the group exists, false otherwise or if an error
25 function groups_group_exists($groupid) {
26 return record_exists('groups', 'id', $groupid);
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
34 function groups_get_group_name($groupid) {
35 return get_field('groups', 'name', 'id', $groupid);
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
45 function groups_get_group_by_name($courseid, $name) {
46 if ($groups = get_records_select('groups', "courseid=$courseid AND name='".addslashes($name)."'")) {
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
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);
67 * Get the group object
68 * @param groupid ID of the group.
69 * @return group object
71 function groups_get_group($groupid) {
72 return get_record('groups', 'id', $groupid);
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.
79 * @param int $groupingid optional returns only groups in the specified grouping.
80 * @return array | false Returns an array of the group IDs or false if no records
81 * or an error occurred.
83 function groups_get_all_groups($courseid, $userid=0, $groupingid=0) {
86 // groupings are ignored when not enabled
87 if (empty($CFG->enablegroupings
)) {
91 if (!empty($userid)) {
92 $userfrom = ", {$CFG->prefix}groups_members gm";
93 $userwhere = "AND g.id = gm.groupid AND gm.userid = '$userid'";
99 if (!empty($groupingid)) {
100 $groupingfrom = ", {$CFG->prefix}groupings_groups gg";
101 $groupingwhere = "AND g.id = gg.groupid AND gg.groupingid = '$groupingid'";
107 return get_records_sql("SELECT g.*
108 FROM {$CFG->prefix}groups g $userfrom $groupingfrom
109 WHERE g.courseid = $courseid $userwhere $groupingwhere
115 * Gets array of all groupings in a specified course.
116 * @param int $coursid return only groupings in this with this courseid
117 * @return array | false Returns an array of the group IDs or false if no records
118 * or an error occurred.
120 function groups_get_all_groupings($courseid) {
123 // groupings are ignored when not enabled
124 if (empty($CFG->enablegroupings
)) {
127 return get_records_sql("SELECT *
128 FROM {$CFG->prefix}groupings
129 WHERE courseid = $courseid
136 * Determines if the user is a member of the given group.
138 * @uses $USER If $userid is null, use the global object.
139 * @param int $groupid The group to check for membership.
140 * @param int $userid The user to check against the group.
141 * @return boolean True if the user is a member, false otherwise.
143 function groups_is_member($groupid, $userid=null) {
150 return record_exists('groups_members', 'groupid', $groupid, 'userid', $userid);
154 * Determines if current or specified is member of any active group in activity
155 * @param object $cm coruse module object
156 * @param int $userid id of user, null menas $USER->id
157 * @return booelan true if user member of at least one group used in activity
159 function groups_has_membership($cm, $userid=null) {
162 static $cache = array();
164 // groupings are ignored when not enabled
165 if (empty($CFG->enablegroupings
)) {
169 if (empty($userid)) {
173 $cachekey = $userid.'|'.$cm->course
.'|'.$cm->groupingid
;
174 if (isset($cache[$cachekey])) {
175 return($cache[$cachekey]);
178 if ($cm->groupingid
) {
179 // find out if member of any group in selected activity grouping
181 FROM {$CFG->prefix}groups_members gm, {$CFG->prefix}groupings_groups gg
182 WHERE gm.userid = $userid AND gm.groupid = gg.groupid AND gg.groupingid = {$cm->groupingid}";
185 // no grouping used - check all groups in course
187 FROM {$CFG->prefix}groups_members gm, {$CFG->prefix}groups g
188 WHERE gm.userid = $userid AND gm.groupid = g.id AND g.courseid = {$cm->course}";
191 $cache[$cachekey] = record_exists_sql($sql);
193 return $cache[$cachekey];
197 * Returns the users in the specified group.
198 * @param int $groupid The groupid to get the users for
199 * @param int $fields The fields to return
200 * @param int $sort optional sorting of returned users
201 * @return array | false Returns an array of the users for the specified
202 * group or false if no users or an error returned.
204 function groups_get_members($groupid, $fields='u.*', $sort='lastname ASC') {
207 return get_records_sql("SELECT $fields
208 FROM {$CFG->prefix}user u, {$CFG->prefix}groups_members gm
209 WHERE u.id = gm.userid AND gm.groupid = '$groupid'
215 * Returns the users in the specified grouping.
216 * @param int $groupingid The groupingid to get the users for
217 * @param int $fields The fields to return
218 * @param int $sort optional sorting of returned users
219 * @return array | false Returns an array of the users for the specified
220 * group or false if no users or an error returned.
222 function groups_get_grouping_members($groupingid, $fields='u.*', $sort='lastname ASC') {
225 return get_records_sql("SELECT $fields
226 FROM {$CFG->prefix}user u
227 INNER JOIN {$CFG->prefix}groups_members gm ON u.id = gm.userid
228 INNER JOIN {$CFG->prefix}groupings_groups gg ON gm.groupid = gg.groupid
229 WHERE gg.groupingid = $groupingid
234 * Returns effective groupmode used in course
235 * @return integer group mode
237 function groups_get_course_groupmode($course) {
238 return $course->groupmode
;
242 * Returns effective groupmode used in activity, course setting
243 * overrides activity setting if groupmodeforce enabled.
244 * @return integer group mode
246 function groups_get_activity_groupmode($cm) {
249 // get course object (reuse COURSE if possible)
250 if ($cm->course
== $COURSE->id
) {
253 if (!$course = get_record('course', 'id', $cm->course
)) {
254 error('Incorrect course id in cm');
258 return empty($course->groupmodeforce
) ?
$cm->groupmode
: $course->groupmode
;
262 * Print group menu selector for course level.
263 * @param object $course course object
264 * @param string $urlroot return address
265 * @param boolean $return return as string instead of printing
266 * @return mixed void or string depending on $return param
268 function groups_print_course_menu($course, $urlroot, $return=false) {
271 if (!$groupmode = $course->groupmode
) {
279 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
280 if ($groupmode == VISIBLEGROUPS
or has_capability('moodle/site:accessallgroups', $context)) {
281 $allowedgroups = groups_get_all_groups($course->id
, 0);
283 $allowedgroups = groups_get_all_groups($course->id
, $USER->id
);
286 $activegroup = groups_get_course_group($course, true);
288 $groupsmenu = array();
289 if (!$allowedgroups or $groupmode == VISIBLEGROUPS
or has_capability('moodle/site:accessallgroups', $context)) {
290 $groupsmenu[0] = get_string('allparticipants');
293 if ($allowedgroups) {
294 foreach ($allowedgroups as $group) {
295 $groupsmenu[$group->id
] = format_string($group->name
);
299 if ($groupmode == VISIBLEGROUPS
) {
300 $grouplabel = get_string('groupsvisible');
302 $grouplabel = get_string('groupsseparate');
305 if (count($groupsmenu) == 1) {
306 $groupname = reset($groupsmenu);
307 $output = $grouplabel.': '.$groupname;
309 $output = popup_form($urlroot.'&group=', $groupsmenu, 'selectgroup', $activegroup, '', '', '', true, 'self', $grouplabel);
312 $output = '<div class="groupselector">'.$output.'</div>';
322 * Print group menu selector for activity.
323 * @param object $cm course module object
324 * @param string $urlroot return address
325 * @param boolean $return return as string instead of printing
326 * @return mixed void or string depending on $return param
328 function groups_print_activity_menu($cm, $urlroot, $return=false) {
331 // groupings are ignored when not enabled
332 if (empty($CFG->enablegroupings
)) {
336 if (!$groupmode = groups_get_activity_groupmode($cm)) {
344 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
345 if ($groupmode == VISIBLEGROUPS
or has_capability('moodle/site:accessallgroups', $context)) {
346 $allowedgroups = groups_get_all_groups($cm->course
, 0, $cm->groupingid
); // any group in grouping (all if groupings not used)
348 $allowedgroups = groups_get_all_groups($cm->course
, $USER->id
, $cm->groupingid
); // only assigned groups
351 $activegroup = groups_get_activity_group($cm, true);
353 $groupsmenu = array();
354 if (!$allowedgroups or $groupmode == VISIBLEGROUPS
or has_capability('moodle/site:accessallgroups', $context)) {
355 $groupsmenu[0] = get_string('allparticipants');
358 if ($allowedgroups) {
359 foreach ($allowedgroups as $group) {
360 $groupsmenu[$group->id
] = format_string($group->name
);
364 if ($groupmode == VISIBLEGROUPS
) {
365 $grouplabel = get_string('groupsvisible');
367 $grouplabel = get_string('groupsseparate');
370 if (count($groupsmenu) == 1) {
371 $groupname = reset($groupsmenu);
372 $output = $grouplabel.': '.$groupname;
374 $output = popup_form($urlroot.'&group=', $groupsmenu, 'selectgroup', $activegroup, '', '', '', true, 'self', $grouplabel);
377 $output = '<div class="groupselector">'.$output.'</div>';
387 * Returns group active in course, changes the group by default if 'group' page param present
389 * @param object $course course bject
390 * @param boolean $update change active group if group param submitted
391 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
393 function groups_get_course_group($course, $update=false) {
394 global $CFG, $USER, $SESSION;
396 if (!$groupmode = $course->groupmode
) {
401 // init activegroup array
402 if (!array_key_exists('activegroup', $SESSION)) {
403 $SESSION->activegroup
= array();
405 if (!array_key_exists($course->id
, $SESSION->activegroup
)) {
406 $SESSION->activegroup
[$course->id
] = array(SEPARATEGROUPS
=>array(), VISIBLEGROUPS
=>array());
409 // grouping used the first time - add first user group as default
410 if (!array_key_exists(0, $SESSION->activegroup
[$course->id
][$groupmode])) {
411 if ($usergroups = groups_get_all_groups($course->id
, $USER->id
, 0)) {
412 $fistgroup = reset($usergroups);
413 $SESSION->activegroup
[$course->id
][$groupmode][0] = $fistgroup->id
;
415 // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet
416 // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum)
417 $SESSION->activegroup
[$course->id
][$groupmode][0] = 0;
421 // set new active group if requested
422 $changegroup = optional_param('group', -1, PARAM_INT
);
423 if ($update and $changegroup != -1) {
424 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
426 if ($changegroup == 0) {
427 // do not allow changing to all groups without accessallgroups capability
428 if ($groupmode == VISIBLEGROUPS
or has_capability('moodle/site:accessallgroups', $context)) {
429 $SESSION->activegroup
[$course->id
][$groupmode][0] = 0;
433 // first make list of allowed groups
434 if ($groupmode == VISIBLEGROUPS
or has_capability('moodle/site:accessallgroups', $context)) {
435 $allowedgroups = groups_get_all_groups($course->id
, 0, 0);
437 $allowedgroups = groups_get_all_groups($course->id
, $USER->id
, 0);
440 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
441 $SESSION->activegroup
[$course->id
][$groupmode][0] = $changegroup;
446 return $SESSION->activegroup
[$course->id
][$groupmode][0];
450 * Returns group active in activity, changes the group by default if 'group' page param present
452 * @param object $cm course module object
453 * @param boolean $update change active group if group param submitted
454 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
456 function groups_get_activity_group($cm, $update=false) {
457 global $CFG, $USER, $SESSION;
459 // groupings are ignored when not enabled
460 if (empty($CFG->enablegroupings
)) {
464 if (!$groupmode = groups_get_activity_groupmode($cm)) {
469 // init activegroup array
470 if (!array_key_exists('activegroup', $SESSION)) {
471 $SESSION->activegroup
= array();
473 if (!array_key_exists($cm->course
, $SESSION->activegroup
)) {
474 $SESSION->activegroup
[$cm->course
] = array(SEPARATEGROUPS
=>array(), VISIBLEGROUPS
=>array());
477 // grouping used the first time - add first user group as default
478 if (!array_key_exists($cm->groupingid
, $SESSION->activegroup
[$cm->course
][$groupmode])) {
479 if ($usergroups = groups_get_all_groups($cm->course
, $USER->id
, $cm->groupingid
)) {
480 $fistgroup = reset($usergroups);
481 $SESSION->activegroup
[$cm->course
][$groupmode][$cm->groupingid
] = $fistgroup->id
;
483 // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet
484 // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum)
485 $SESSION->activegroup
[$cm->course
][$groupmode][$cm->groupingid
] = 0;
489 // set new active group if requested
490 $changegroup = optional_param('group', -1, PARAM_INT
);
491 if ($update and $changegroup != -1) {
492 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
494 if ($changegroup == 0) {
495 // do not allow changing to all groups without accessallgroups capability
496 if ($groupmode == VISIBLEGROUPS
or has_capability('moodle/site:accessallgroups', $context)) {
497 $SESSION->activegroup
[$cm->course
][$groupmode][$cm->groupingid
] = 0;
501 // first make list of allowed groups
502 if ($groupmode == VISIBLEGROUPS
or has_capability('moodle/site:accessallgroups', $context)) {
503 $allowedgroups = groups_get_all_groups($cm->course
, 0, $cm->groupingid
); // any group in grouping (all if groupings not used)
505 $allowedgroups = groups_get_all_groups($cm->course
, $USER->id
, $cm->groupingid
); // only assigned groups
508 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
509 $SESSION->activegroup
[$cm->course
][$groupmode][$cm->groupingid
] = $changegroup;
514 return $SESSION->activegroup
[$cm->course
][$groupmode][$cm->groupingid
];
518 * Determine if a course module is currently visible to a user
519 * @uses $USER If $userid is null, use the global object.
520 * @param int $cm The course module
521 * @param int $userid The user to check against the group.
522 * @return boolean True if the user can view the course module, false otherwise.
524 function groups_course_module_visible($cm, $userid=null) {
527 if (empty($userid)) {
530 if (empty($CFG->enablegroupings
)) {
533 if (empty($cm->groupmembersonly
)) {
536 if (groups_has_membership($cm, $userid) ||
has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE
, $cm->id
), $userid)) {