MDL-10969
[moodle.git] / lib / grouplib.php
CommitLineData
2c386f82 1<?php //$Id$
2
13534ef7
ML
3/**
4 * No groups used?
5 */
6define('NOGROUPS', 0);
7
8/**
9 * Groups used?
10 */
11define('SEPARATEGROUPS', 1);
5bf243d1 12
5bf243d1 13/**
13534ef7
ML
14 * Groups visible?
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) {
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 */
34function 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 */
45function 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 */
59function 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 */
71function 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 83function 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 */
121function 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 */
137function groups_has_membership($cm, $userid=null) {
138 global $CFG, $USER;
139
7e4fdf25 140 // groupings are ignored when not enabled
c0d4238d 141 if (empty($CFG->enablegroupings)) {
142 $cm->groupingid = 0;
143 }
144
f8e3d5f0 145 if (empty($userid)) {
146 $userid = $USER->id;
147 }
148
149 if ($cm->groupingid) {
150 // find out if member of any group in selected activity grouping
151 $sql = "SELECT 'x'
152 FROM {$CFG->prefix}groups_members gm, {$CFG->prefix}groupings_groups gg
153 WHERE gm.userid = $userid AND gm.groupid = gg.groupid AND gg.groupingid = {$cm->groupingid}";
154
155 } else {
156 // no grouping used - check all groups in course
157 $sql = "SELECT 'x'
158 FROM {$CFG->prefix}groups_members gm, {$CFG->prefix}groups g
159 WHERE gm.userid = $userid AND gm.groupid = g.id AND g.courseid = {$cm->course}";
160 }
161
162 return record_exists_sql($sql);
163}
164
62d63838 165/**
166 * Returns the users in the specified group.
167 * @param int $groupid The groupid to get the users for
168 * @param int $sort optional sorting of returned users
169 * @return array | false Returns an array of the users for the specified
170 * group or false if no users or an error returned.
171 */
172function groups_get_members($groupid, $sort='lastname ASC') {
173 global $CFG;
174
175 return get_records_sql("SELECT u.*
176 FROM {$CFG->prefix}user u, {$CFG->prefix}groups_members gm
177 WHERE u.id = gm.userid AND gm.groupid = '$groupid'
178 ORDER BY $sort");
179}
180
13534ef7
ML
181/**
182 * Returns effective groupmode used in activity, course setting
183 * overrides activity setting if groupmodeforce enabled.
184 * @return integer group mode
185 */
186function groups_get_activity_groupmode($cm) {
187 global $COURSE;
188
189 // get course object (reuse COURSE if possible)
190 if ($cm->course == $COURSE->id) {
191 $course = $COURSE;
192 } else {
193 if (!$course = get_record('course', 'id', $cm->course)) {
194 error('Incorrect course id in cm');
195 }
196 }
197
198 return empty($course->groupmodeforce) ? $cm->groupmode : $course->groupmode;
199}
200
201/**
202 * Print group menu selector for activity.
203 * @param object $cm course module object
204 * @param string $urlroot return address
205 * @param boolean $return return as string instead of printing
206 * @return mixed void or string depending on $return param
207 */
208function groups_print_activity_menu($cm, $urlroot, $return=false) {
c0d4238d 209 global $CFG, $USER;
210
7e4fdf25 211 // groupings are ignored when not enabled
c0d4238d 212 if (empty($CFG->enablegroupings)) {
213 $cm->groupingid = 0;
214 }
13534ef7
ML
215
216 if (!$groupmode = groups_get_activity_groupmode($cm)) {
217 if ($return) {
218 return '';
219 } else {
220 return;
221 }
222 }
223
224 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
225 if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
226 $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping (all if groupings not used)
227 } else {
228 $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups
229 }
230
231 $activegroup = groups_get_activity_group($cm, true);
232
233 $groupsmenu = array();
234 if (!$allowedgroups or $groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
235 $groupsmenu[0] = get_string('allparticipants');
236 }
237
238 if ($allowedgroups) {
239 foreach ($allowedgroups as $group) {
240 $groupsmenu[$group->id] = format_string($group->name);
241 }
242 }
243
244 if ($groupmode == VISIBLEGROUPS) {
245 $grouplabel = get_string('groupsvisible');
246 } else {
247 $grouplabel = get_string('groupsseparate');
248 }
249
250 if (count($groupsmenu) == 1) {
251 $groupname = reset($groupsmenu);
252 $output = $grouplabel.': '.$groupname;
253 } else {
254 $output = popup_form($urlroot.'&amp;group=', $groupsmenu, 'selectgroup', $activegroup, '', '', '', true, 'self', $grouplabel);
255 }
256
257 $output = '<div class="groupselector">'.$output.'</div>';
258
259 if ($return) {
260 return $output;
261 } else {
262 echo $output;
263 }
264}
265
266/**
267 * Returns group active in activity, changes the group by default if 'group' page param present
268 *
269 * @param object $cm course module object
270 * @param boolean $update change active group if group param submitted
271 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
272 */
273function groups_get_activity_group($cm, $update=false) {
c0d4238d 274 global $CFG, $USER, $SESSION;
275
7e4fdf25 276 // groupings are ignored when not enabled
c0d4238d 277 if (empty($CFG->enablegroupings)) {
278 $cm->groupingid = 0;
279 }
13534ef7
ML
280
281 if (!$groupmode = groups_get_activity_groupmode($cm)) {
282 // NOGROUPS used
283 return false;
284 }
285
286 // innit activegroup array
287 if (!array_key_exists('activegroup', $SESSION)) {
288 $SESSION->activegroup = array();
289 }
290 if (!array_key_exists($cm->course, $SESSION->activegroup)) {
291 $SESSION->activegroup[$cm->course] = array(SEPARATEGROUPS=>array(), VISIBLEGROUPS=>array());
292 }
293
294 // grouping used the first time - add first user group as default
295 if (!array_key_exists($cm->groupingid, $SESSION->activegroup[$cm->course][$groupmode])) {
296 if ($usergroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid)) {
297 $fistgroup = reset($usergroups);
298 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $fistgroup->id;
299 } else {
300 // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet
301 // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum)
302 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0;
303 }
304 }
305
306 // set new active group if requested
307 $changegroup = optional_param('group', -1, PARAM_INT);
308 if ($update and $changegroup != -1) {
309 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
310
311 if ($changegroup == 0) {
312 // do not allow changing to all groups without accessallgroups capability
313 if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
314 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0;
315 }
316
317 } else {
318 // first make list of allowed groups
319 if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
320 $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping (all if groupings not used)
321 } else {
322 $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups
323 }
324
325 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
326 $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $changegroup;
327 }
328 }
329 }
330
331 return $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid];
332}
62d63838 333
dcd8db68 334/**
335 * Determine if a course module is currently visible to a user
336 * @uses $USER If $userid is null, use the global object.
337 * @param int $cm The course module
338 * @param int $userid The user to check against the group.
339 * @return boolean True if the user can view the course module, false otherwise.
340 */
341function groups_course_module_visible($cm, $userid=null) {
342 global $CFG, $USER;
343
344 if (empty($userid)) {
345 $userid = $USER->id;
346 }
347 if (empty($CFG->enablegroupings)) {
348 return(true);
349 }
350 if (empty($cm->groupmembersonly)) {
351 return(true);
352 }
353 if (has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE, $cm->id), $userid)) {
354 return(true);
355 }
356 if (groups_has_membership($cm, $userid)) {
357 return(true);
358 }
359 return(false);
360}
361
2c386f82 362?>