3 * Legacy groups functions - these were in moodlelib.php, datalib.php, weblib.php
5 * @@@ Don't look at this file - still tons to do!
7 * TODO: For the moment these functions are in /lib/deprecatedlib.php
11 * @copyright © 2006 The Open University
12 * @author J.White AT open.ac.uk and others
13 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
19 * Returns the groupid of a group with the name specified for the course
20 * specified. If there's more than one with the name specified it returns the
21 * first one it finds it the database, so you need to be careful how you use it!
22 * This is needed to support upload of users into the database
23 * @param int $courseid The id of the course
24 * @param string $groupname
25 * @return int $groupid
27 function groups_get_group_by_name($courseid, $groupname) {
28 //uploaduser.php, get_record("groups","courseid",$course[$i]->id,"name",$addgroup[$i])
29 $groupids = groups_db_get_groups($courseid);
33 foreach ($groupids as $id) {
34 if (groups_get_group_name($id) == $groupname) {
42 * Returns an array of group objects that the user is a member of
43 * in the given course. If userid isn't specified, then return a
44 * list of all groups in the course.
47 * @param int $courseid The id of the course in question.
48 * @param int $userid The id of the user in question as found in the 'user'
52 function get_groups($courseid, $userid=0) {
54 $groupids = groups_get_groups_for_user($userid, $courseid);
56 $groupids = groups_get_groups($courseid);
59 return groups_groupids_to_groups($groupids, $courseid, $alldata=true);
64 * Returns the user's group in a particular course
67 * @param int $courseid The course in question.
68 * @param int $userid The id of the user as found in the 'user' table.
69 * @param int $groupid The id of the group the user is in.
72 function user_group($courseid, $userid) {
73 $groupids = groups_get_groups_for_user($userid, $courseid);
74 return groups_groupids_to_groups($groupids);
79 * Determines if the user a member of the given group
82 * @param int $groupid The group to check the membership of
83 * @param int $userid The user to check against the group
86 function ismember($groupid, $userid=0) {
89 return groups_is_member($groupid, $userid);
93 * Returns an array of user objects
96 * @param int $groupid The group in question.
97 * @param string $sort ?
98 * @param string $exceptions ?
100 * @todo Finish documenting this function
102 function get_group_users($groupid, $sort='u.lastaccess DESC', $exceptions='',
105 if (!empty($exceptions)) {
106 $except = ' AND u.id NOT IN ('. $exceptions .') ';
110 // in postgres, you can't have things in sort that aren't in the select, so...
111 $extrafield = str_replace('ASC','',$sort);
112 $extrafield = str_replace('DESC','',$extrafield);
113 $extrafield = trim($extrafield);
114 if (!empty($extrafield)) {
115 $extrafield = ','.$extrafield;
117 return get_records_sql("SELECT DISTINCT $fields $extrafield
118 FROM {$CFG->prefix}user u,
119 {$CFG->prefix}groups_members m
120 WHERE m.groupid = '$groupid'
121 AND m.userid = u.id $except
129 * Add a user to a group, return true upon success or if user already a group
132 * @param int $groupid The group id to add user to
133 * @param int $userid The user id to add to the group
136 function add_user_to_group($groupid, $userid) {
137 return groups_add_member($groupid, $userid);
142 * Get the IDs for the user's groups in the given course.
145 * @param int $courseid The course being examined - the 'course' table id field.
146 * @return array An _array_ of groupids.
147 * (Was return $groupids[0] - consequences!)
149 function mygroupid($courseid) {
151 $groupids = groups_get_groups_for_user($USER->id
, $courseid);
156 * This now returns either false or SEPARATEGROUPS. If you want VISIBLE GROUPS
157 * with legacy code, you'll need to upgrade.
159 function groupmode($course, $cm=null) {
161 if ($cm and !$course->groupmodeforce
) {
162 return $cm->groupmode
;
164 return $course->groupmode
;
166 /*if ($cm and !$course->groupingid) {
167 //TODO: was $coursemodule
168 return groups_has_groups_setup_for_instance($cm);
170 return groups_has_groups_setup($course->id);
176 * Sets the current group in the session variable
177 * When $SESSION->currentgroup[$courseid] is set to 0 it means, show all groups.
178 * Sets currentgroup[$courseid] in the session variable appropriately.
179 * Does not do any permission checking.
181 * @param int $courseid The course being examined - relates to id field in
183 * @param int $groupid The group being examined.
184 * @return int Current group id which was set by this function
186 function set_current_group($courseid, $groupid) {
188 return $SESSION->currentgroup
[$courseid] = $groupid;
193 * Gets the current group - either from the session variable or from the database.
197 * @param int $courseid The course being examined - relates to id field in
199 * @param bool $full If true, the return value is a full record object.
200 * If false, just the id of the record.
202 function get_current_group($courseid, $full = false) {
205 $mygroupid = mygroupid($courseid);
206 if (is_array($mygroupid)) {
207 $mygroupid = array_shift($mygroupid);
210 if (isset($SESSION->currentgroup
[$courseid])) {
211 $currentgroup = $SESSION->currentgroup
[$courseid];
213 $currentgroup = $mygroupid;
217 $SESSION->currentgroup
[$courseid] = $mygroupid;
221 return groups_groupid_to_group($currentgroup);
223 return $currentgroup;
229 * A combination function to make it easier for modules
232 * It will use a given "groupid" parameter and try to use
233 * that to reset the current group for the user.
235 * @uses VISIBLEGROUPS
236 * @param course $course A {@link $COURSE} object
237 * @param int $groupmode Either NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS
238 * @param int $groupid Will try to use this optional parameter to
239 * reset the current group for the user
240 * @return int|false Returns the current group id or false if error.
242 function get_and_set_current_group($course, $groupmode, $groupid=-1) {
243 //TODO: ?? groups_has_permission($userid, $groupingid, $courseid, $groupid, $permissiontype);
245 // Sets to the specified group, provided the current user has view permission
246 if (!$groupmode) { // Groups don't even apply
250 $currentgroupid = get_current_group($course->id
);
252 if ($groupid < 0) { // No change was specified
253 return $currentgroupid;
256 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
257 if ($groupid) { // Try to change the current group to this groupid
258 if (groups_group_belongs_to_course($groupid, $course->id
)) { // Exists TODO:check.
259 if (has_capability('moodle/site:accessallgroups', $context)) { // Sets current default group
260 $currentgroupid = set_current_group($course->id
, $groupid);
262 } elseif ($groupmode == VISIBLEGROUPS
) {
263 // All groups are visible
264 //if (ismember($group->id)){
265 $currentgroupid = set_current_group($course->id
, $groupid); //set this since he might post
267 $currentgroupid = $group->id;*/
268 } elseif ($groupmode == SEPARATEGROUPS
) { // student in separate groups switching
269 if (ismember($group->id
)) { //check if is a member
270 $currentgroupid = set_current_group($course->id
, $groupid); //might need to set_current_group?
274 notify('you do not belong to this group!',error
);
278 } else { // When groupid = 0 it means show ALL groups
279 // this is changed, non editting teacher needs access to group 0 as well,
280 // for viewing work in visible groups (need to set current group for multiple pages)
281 if (has_capability('moodle/site:accessallgroups', $context) AND ($groupmode == VISIBLEGROUPS
)) { // Sets current default group
282 $currentgroupid = set_current_group($course->id
, 0);
284 } elseif ($groupmode == VISIBLEGROUPS
) { // All groups are visible
289 return $currentgroupid;
294 * A big combination function to make it easier for modules
297 * Terminates if the current user shouldn't be looking at this group
298 * Otherwise returns the current group if there is one
299 * Otherwise returns false if groups aren't relevant
301 * @uses SEPARATEGROUPS
302 * @uses VISIBLEGROUPS
303 * @param course $course A {@link $COURSE} object
304 * @param int $groupmode Either NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS
305 * @param string $urlroot ?
308 function setup_and_print_groups($course, $groupmode, $urlroot) {
310 global $USER, $SESSION; //needs his id, need to hack his groups in session
312 $changegroup = optional_param('group', -1, PARAM_INT
);
314 $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
315 if ($currentgroup === false) {
319 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
321 if ($groupmode == VISIBLEGROUPS
322 or ($groupmode and has_capability('moodle/site:accessallgroups', $context))) {
323 groups_instance_print_grouping_selector();
324 }//added code here to allow non-editting teacher to swap in-between his own groups
325 //added code for students in separategrous to swtich groups
326 else if ($groupmode == SEPARATEGROUPS
and has_capability('moodle/course:view', $context)) {
327 groups_instance_print_group_selector();
330 return $currentgroup;
334 function groups_instance_print_grouping_selector() {
337 function groups_instance_print_group_selector() {
342 function oldgroups_print_user_group_info($currentgroup, $isseparategroups, $courseid) {
344 $context = get_context_instance(CONTEXT_COURSE
, $courseid);
346 if ($currentgroup and (!$isseparategroups or has_capability('moodle/site:accessallgroups', $context))) { /// Display info about the group
347 if ($group = get_record('groups', 'id', $currentgroup)) {
348 if (!empty($group->description
) or (!empty($group->picture
) and empty($group->hidepicture
))) {
349 echo '<table class="groupinfobox"><tr><td class="left side picture">';
350 print_group_picture($group, $course->id
, true, false, false);
351 echo '</td><td class="content">';
352 echo '<h3>'.$group->name
;
353 if (has_capability('moodle/site:accessallgroups', $context)) {
354 echo ' <a title="'.get_string('editgroupprofile').'" href="../course/groups.php?id='.$course->id
.'&group='.$group->id
.'">';
355 echo '<img src="'.$CFG->pixpath
.'/t/edit.gif" alt="" border="0">';
359 echo format_text($group->description
);
360 echo '</td></tr></table>';
367 * Get the group object, including the course ID by default.
368 * @param groupid ID of the group.
369 * @param getcourse (default true), include the course ID in the return.
370 * @return group object, optionally including 'courseid'.
372 function groups_get_group($groupid, $getcourse=true) {
373 $group = groups_db_get_group_settings($groupid);
374 if ($group && $getcourse) {
375 $group->courseid
= groups_get_course($groupid);
382 * Get an array of groups, as id => name.
383 * Replaces, get_records_menu("groups", "courseid", $course->id, "name ASC", "id,name")
384 * (For /user/index.php)
386 function groups_get_groups_names($courseid) {
387 $groupids = groups_db_get_groups($courseid);
391 $groups_names = array();
392 foreach ($groupids as $id) {
393 $groups_names[$id] = groups_get_group_name($id);
396 return $groups_names;
400 * Get the groups that a number of users are in.
401 * (For block_quiz_results.php)
403 function groups_get_groups_users($userids, $courseid) {
405 $groups_users = get_records_sql(
406 'SELECT gm.userid, gm.groupid, g.name FROM '.$CFG->prefix
.'groups g LEFT JOIN '.$CFG->prefix
.'groups_members gm ON g.id = gm.groupid '.
407 'WHERE g.courseid = '.$courseid.' AND gm.userid IN ('.implode(',', $userids).')'
409 return $groups_users;