adding current groupid to grade_export class - soon to be used in plugins
[moodle-pu.git] / lib / grouplib.php
blob935e07eb3a47bd1ebfdc7cdeeb6bb80cba1433f6
1 <?php //$Id$
3 /**
4 * No groups used?
5 */
6 define('NOGROUPS', 0);
8 /**
9 * Groups used?
11 define('SEPARATEGROUPS', 1);
13 /**
14 * Groups visible?
16 define('VISIBLEGROUPS', 2);
19 /**
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
23 * occurred.
25 function groups_group_exists($groupid) {
26 return record_exists('groups', 'id', $groupid);
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
34 function groups_get_group_name($groupid) {
35 return get_field('groups', 'name', 'id', $groupid);
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
45 function groups_get_group_by_name($courseid, $name) {
46 if ($groups = get_records_select('groups', "courseid=$courseid AND name='".addslashes($name)."'")) {
47 return key($groups);
49 return false;
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
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);
63 return false;
66 /**
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);
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.
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) {
84 global $CFG;
86 // groupings are ignored when not enabled
87 if (empty($CFG->enablegroupings)) {
88 $groupingid = 0;
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 = "";
99 if (!empty($groupingid)) {
100 $groupingfrom = ", {$CFG->prefix}groupings_groups gg";
101 $groupingwhere = "AND g.id = gg.groupid AND gg.groupingid = '$groupingid'";
102 } else {
103 $groupingfrom = "";
104 $groupingwhere = "";
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");
114 * Determines if the user is a member of the given group.
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.
121 function groups_is_member($groupid, $userid=null) {
122 global $USER;
124 if (!$userid) {
125 $userid = $USER->id;
128 return record_exists('groups_members', 'groupid', $groupid, 'userid', $userid);
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
137 function groups_has_membership($cm, $userid=null) {
138 global $CFG, $USER;
140 static $cache = array();
142 // groupings are ignored when not enabled
143 if (empty($CFG->enablegroupings)) {
144 $cm->groupingid = 0;
147 if (empty($userid)) {
148 $userid = $USER->id;
151 $cachekey = $userid.'|'.$cm->course.'|'.$cm->groupingid;
152 if (isset($cache[$cachekey])) {
153 return($cache[$cachekey]);
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}";
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}";
169 $cache[$cachekey] = record_exists_sql($sql);
171 return $cache[$cachekey];
175 * Returns the users in the specified group.
176 * @param int $groupid The groupid to get the users for
177 * @param int $fields The fields to return
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.
182 function groups_get_members($groupid, $fields='u.*', $sort='lastname ASC') {
183 global $CFG;
185 return get_records_sql("SELECT $fields
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");
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.
200 function groups_get_grouping_members($groupingid, $fields='u.*', $sort='lastname ASC') {
201 global $CFG;
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");
212 * Returns effective groupmode used in activity, course setting
213 * overrides activity setting if groupmodeforce enabled.
214 * @return integer group mode
216 function groups_get_activity_groupmode($cm) {
217 global $COURSE;
219 // get course object (reuse COURSE if possible)
220 if ($cm->course == $COURSE->id) {
221 $course = $COURSE;
222 } else {
223 if (!$course = get_record('course', 'id', $cm->course)) {
224 error('Incorrect course id in cm');
228 return empty($course->groupmodeforce) ? $cm->groupmode : $course->groupmode;
232 * Print group menu selector for activity.
233 * @param object $cm course module object
234 * @param string $urlroot return address
235 * @param boolean $return return as string instead of printing
236 * @return mixed void or string depending on $return param
238 function groups_print_activity_menu($cm, $urlroot, $return=false) {
239 global $CFG, $USER;
241 // groupings are ignored when not enabled
242 if (empty($CFG->enablegroupings)) {
243 $cm->groupingid = 0;
246 if (!$groupmode = groups_get_activity_groupmode($cm)) {
247 if ($return) {
248 return '';
249 } else {
250 return;
254 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
255 if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
256 $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping (all if groupings not used)
257 } else {
258 $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups
261 $activegroup = groups_get_activity_group($cm, true);
263 $groupsmenu = array();
264 if (!$allowedgroups or $groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
265 $groupsmenu[0] = get_string('allparticipants');
268 if ($allowedgroups) {
269 foreach ($allowedgroups as $group) {
270 $groupsmenu[$group->id] = format_string($group->name);
274 if ($groupmode == VISIBLEGROUPS) {
275 $grouplabel = get_string('groupsvisible');
276 } else {
277 $grouplabel = get_string('groupsseparate');
280 if (count($groupsmenu) == 1) {
281 $groupname = reset($groupsmenu);
282 $output = $grouplabel.': '.$groupname;
283 } else {
284 $output = popup_form($urlroot.'&amp;group=', $groupsmenu, 'selectgroup', $activegroup, '', '', '', true, 'self', $grouplabel);
287 $output = '<div class="groupselector">'.$output.'</div>';
289 if ($return) {
290 return $output;
291 } else {
292 echo $output;
297 * Returns group active in activity, changes the group by default if 'group' page param present
299 * @param object $cm course module object
300 * @param boolean $update change active group if group param submitted
301 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
303 function groups_get_activity_group($cm, $update=false) {
304 global $CFG, $USER, $SESSION;
306 // groupings are ignored when not enabled
307 if (empty($CFG->enablegroupings)) {
308 $cm->groupingid = 0;
311 if (!$groupmode = groups_get_activity_groupmode($cm)) {
312 // NOGROUPS used
313 return false;
316 // innit activegroup array
317 if (!array_key_exists('activegroup', $SESSION)) {
318 $SESSION->activegroup = array();
320 if (!array_key_exists($cm->course, $SESSION->activegroup)) {
321 $SESSION->activegroup[$cm->course] = array(SEPARATEGROUPS=>array(), VISIBLEGROUPS=>array());
324 // grouping used the first time - add first user group as default
325 if (!array_key_exists($cm->groupingid, $SESSION->activegroup[$cm->course][$groupmode])) {
326 if ($usergroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid)) {
327 $fistgroup = reset($usergroups);
328 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $fistgroup->id;
329 } else {
330 // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet
331 // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum)
332 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0;
336 // set new active group if requested
337 $changegroup = optional_param('group', -1, PARAM_INT);
338 if ($update and $changegroup != -1) {
339 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
341 if ($changegroup == 0) {
342 // do not allow changing to all groups without accessallgroups capability
343 if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
344 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0;
347 } else {
348 // first make list of allowed groups
349 if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
350 $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping (all if groupings not used)
351 } else {
352 $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups
355 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
356 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $changegroup;
361 return $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid];
365 * Determine if a course module is currently visible to a user
366 * @uses $USER If $userid is null, use the global object.
367 * @param int $cm The course module
368 * @param int $userid The user to check against the group.
369 * @return boolean True if the user can view the course module, false otherwise.
371 function groups_course_module_visible($cm, $userid=null) {
372 global $CFG, $USER;
374 if (empty($userid)) {
375 $userid = $USER->id;
377 if (empty($CFG->enablegroupings)) {
378 return(true);
380 if (empty($cm->groupmembersonly)) {
381 return(true);
383 if (groups_has_membership($cm, $userid) || has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE, $cm->id), $userid)) {
384 return(true);
386 return(false);