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