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 | ||
62d63838 | 86 | if (!empty($userid)) { |
87 | $userfrom = ", {$CFG->prefix}groups_members gm"; | |
88 | $userwhere = "AND g.id = gm.groupid AND gm.userid = '$userid'"; | |
89 | } else { | |
90 | $userfrom = ""; | |
91 | $userwhere = ""; | |
92 | } | |
2c386f82 | 93 | |
62d63838 | 94 | if (!empty($groupingid)) { |
95 | $groupingfrom = ", {$CFG->prefix}groupings_groups gg"; | |
96 | $groupingwhere = "AND g.id = gg.groupid AND gg.groupingid = '$groupingid'"; | |
2c386f82 | 97 | } else { |
62d63838 | 98 | $groupingfrom = ""; |
99 | $groupingwhere = ""; | |
2c386f82 | 100 | } |
62d63838 | 101 | |
102 | return get_records_sql("SELECT g.* | |
103 | FROM {$CFG->prefix}groups g $userfrom $groupingfrom | |
104 | WHERE g.courseid = '$courseid' $userwhere $groupingwhere | |
105 | ORDER BY name ASC"); | |
2c386f82 | 106 | } |
107 | ||
108 | /** | |
109 | * Determines if the user is a member of the given group. | |
110 | * | |
111 | * @uses $USER If $userid is null, use the global object. | |
112 | * @param int $groupid The group to check for membership. | |
113 | * @param int $userid The user to check against the group. | |
114 | * @return boolean True if the user is a member, false otherwise. | |
115 | */ | |
116 | function groups_is_member($groupid, $userid=null) { | |
117 | global $USER; | |
118 | ||
119 | if (!$userid) { | |
120 | $userid = $USER->id; | |
121 | } | |
122 | ||
123 | return record_exists('groups_members', 'groupid', $groupid, 'userid', $userid); | |
124 | } | |
125 | ||
f8e3d5f0 | 126 | /** |
127 | * Determines if current or specified is member of any active group in activity | |
128 | * @param object $cm coruse module object | |
129 | * @param int $userid id of user, null menas $USER->id | |
130 | * @return booelan true if user member of at least one group used in activity | |
131 | */ | |
132 | function groups_has_membership($cm, $userid=null) { | |
133 | global $CFG, $USER; | |
134 | ||
135 | if (empty($userid)) { | |
136 | $userid = $USER->id; | |
137 | } | |
138 | ||
139 | if ($cm->groupingid) { | |
140 | // find out if member of any group in selected activity grouping | |
141 | $sql = "SELECT 'x' | |
142 | FROM {$CFG->prefix}groups_members gm, {$CFG->prefix}groupings_groups gg | |
143 | WHERE gm.userid = $userid AND gm.groupid = gg.groupid AND gg.groupingid = {$cm->groupingid}"; | |
144 | ||
145 | } else { | |
146 | // no grouping used - check all groups in course | |
147 | $sql = "SELECT 'x' | |
148 | FROM {$CFG->prefix}groups_members gm, {$CFG->prefix}groups g | |
149 | WHERE gm.userid = $userid AND gm.groupid = g.id AND g.courseid = {$cm->course}"; | |
150 | } | |
151 | ||
152 | return record_exists_sql($sql); | |
153 | } | |
154 | ||
62d63838 | 155 | /** |
156 | * Returns the users in the specified group. | |
157 | * @param int $groupid The groupid to get the users for | |
158 | * @param int $sort optional sorting of returned users | |
159 | * @return array | false Returns an array of the users for the specified | |
160 | * group or false if no users or an error returned. | |
161 | */ | |
162 | function groups_get_members($groupid, $sort='lastname ASC') { | |
163 | global $CFG; | |
164 | ||
165 | return get_records_sql("SELECT u.* | |
166 | FROM {$CFG->prefix}user u, {$CFG->prefix}groups_members gm | |
167 | WHERE u.id = gm.userid AND gm.groupid = '$groupid' | |
168 | ORDER BY $sort"); | |
169 | } | |
170 | ||
13534ef7 ML |
171 | /** |
172 | * Returns effective groupmode used in activity, course setting | |
173 | * overrides activity setting if groupmodeforce enabled. | |
174 | * @return integer group mode | |
175 | */ | |
176 | function groups_get_activity_groupmode($cm) { | |
177 | global $COURSE; | |
178 | ||
179 | // get course object (reuse COURSE if possible) | |
180 | if ($cm->course == $COURSE->id) { | |
181 | $course = $COURSE; | |
182 | } else { | |
183 | if (!$course = get_record('course', 'id', $cm->course)) { | |
184 | error('Incorrect course id in cm'); | |
185 | } | |
186 | } | |
187 | ||
188 | return empty($course->groupmodeforce) ? $cm->groupmode : $course->groupmode; | |
189 | } | |
190 | ||
191 | /** | |
192 | * Print group menu selector for activity. | |
193 | * @param object $cm course module object | |
194 | * @param string $urlroot return address | |
195 | * @param boolean $return return as string instead of printing | |
196 | * @return mixed void or string depending on $return param | |
197 | */ | |
198 | function groups_print_activity_menu($cm, $urlroot, $return=false) { | |
9d66deb3 | 199 | global $USER; |
13534ef7 ML |
200 | |
201 | if (!$groupmode = groups_get_activity_groupmode($cm)) { | |
202 | if ($return) { | |
203 | return ''; | |
204 | } else { | |
205 | return; | |
206 | } | |
207 | } | |
208 | ||
209 | $context = get_context_instance(CONTEXT_MODULE, $cm->id); | |
210 | if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) { | |
211 | $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping (all if groupings not used) | |
212 | } else { | |
213 | $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups | |
214 | } | |
215 | ||
216 | $activegroup = groups_get_activity_group($cm, true); | |
217 | ||
218 | $groupsmenu = array(); | |
219 | if (!$allowedgroups or $groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) { | |
220 | $groupsmenu[0] = get_string('allparticipants'); | |
221 | } | |
222 | ||
223 | if ($allowedgroups) { | |
224 | foreach ($allowedgroups as $group) { | |
225 | $groupsmenu[$group->id] = format_string($group->name); | |
226 | } | |
227 | } | |
228 | ||
229 | if ($groupmode == VISIBLEGROUPS) { | |
230 | $grouplabel = get_string('groupsvisible'); | |
231 | } else { | |
232 | $grouplabel = get_string('groupsseparate'); | |
233 | } | |
234 | ||
235 | if (count($groupsmenu) == 1) { | |
236 | $groupname = reset($groupsmenu); | |
237 | $output = $grouplabel.': '.$groupname; | |
238 | } else { | |
239 | $output = popup_form($urlroot.'&group=', $groupsmenu, 'selectgroup', $activegroup, '', '', '', true, 'self', $grouplabel); | |
240 | } | |
241 | ||
242 | $output = '<div class="groupselector">'.$output.'</div>'; | |
243 | ||
244 | if ($return) { | |
245 | return $output; | |
246 | } else { | |
247 | echo $output; | |
248 | } | |
249 | } | |
250 | ||
251 | /** | |
252 | * Returns group active in activity, changes the group by default if 'group' page param present | |
253 | * | |
254 | * @param object $cm course module object | |
255 | * @param boolean $update change active group if group param submitted | |
256 | * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode) | |
257 | */ | |
258 | function groups_get_activity_group($cm, $update=false) { | |
9d66deb3 | 259 | global $USER, $SESSION; |
13534ef7 ML |
260 | |
261 | if (!$groupmode = groups_get_activity_groupmode($cm)) { | |
262 | // NOGROUPS used | |
263 | return false; | |
264 | } | |
265 | ||
266 | // innit activegroup array | |
267 | if (!array_key_exists('activegroup', $SESSION)) { | |
268 | $SESSION->activegroup = array(); | |
269 | } | |
270 | if (!array_key_exists($cm->course, $SESSION->activegroup)) { | |
271 | $SESSION->activegroup[$cm->course] = array(SEPARATEGROUPS=>array(), VISIBLEGROUPS=>array()); | |
272 | } | |
273 | ||
274 | // grouping used the first time - add first user group as default | |
275 | if (!array_key_exists($cm->groupingid, $SESSION->activegroup[$cm->course][$groupmode])) { | |
276 | if ($usergroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid)) { | |
277 | $fistgroup = reset($usergroups); | |
278 | $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $fistgroup->id; | |
279 | } else { | |
280 | // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet | |
281 | // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum) | |
282 | $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0; | |
283 | } | |
284 | } | |
285 | ||
286 | // set new active group if requested | |
287 | $changegroup = optional_param('group', -1, PARAM_INT); | |
288 | if ($update and $changegroup != -1) { | |
289 | $context = get_context_instance(CONTEXT_MODULE, $cm->id); | |
290 | ||
291 | if ($changegroup == 0) { | |
292 | // do not allow changing to all groups without accessallgroups capability | |
293 | if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) { | |
294 | $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0; | |
295 | } | |
296 | ||
297 | } else { | |
298 | // first make list of allowed groups | |
299 | if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) { | |
300 | $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping (all if groupings not used) | |
301 | } else { | |
302 | $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups | |
303 | } | |
304 | ||
305 | if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) { | |
306 | $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $changegroup; | |
307 | } | |
308 | } | |
309 | } | |
310 | ||
311 | return $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid]; | |
312 | } | |
62d63838 | 313 | |
2c386f82 | 314 | ?> |