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 is a member of the given group.
80 * TODO: replace all calls with 'groups_is_member'.
82 * @param int $groupid The group to check for membership.
83 * @param int $userid The user to check against the group.
84 * @return boolean True if the user is a member, false otherwise.
86 function ismember($groupid, $userid = null) {
87 return groups_is_member($groupid, $userid);
91 * Returns an array of user objects
94 * @param int $groupid The group in question.
95 * @param string $sort ?
96 * @param string $exceptions ?
98 * @todo Finish documenting this function
100 function get_group_users($groupid, $sort='u.lastaccess DESC', $exceptions='',
103 if (!empty($exceptions)) {
104 $except = ' AND u.id NOT IN ('. $exceptions .') ';
108 // in postgres, you can't have things in sort that aren't in the select, so...
109 $extrafield = str_replace('ASC','',$sort);
110 $extrafield = str_replace('DESC','',$extrafield);
111 $extrafield = trim($extrafield);
112 if (!empty($extrafield)) {
113 $extrafield = ','.$extrafield;
115 return get_records_sql("SELECT DISTINCT $fields $extrafield
116 FROM {$CFG->prefix}user u,
117 {$CFG->prefix}groups_members m
118 WHERE m.groupid = '$groupid'
119 AND m.userid = u.id $except
127 * Add a user to a group, return true upon success or if user already a group
130 * @param int $groupid The group id to add user to
131 * @param int $userid The user id to add to the group
134 function add_user_to_group($groupid, $userid) {
135 return groups_add_member($groupid, $userid);
140 * Get the IDs for the user's groups in the given course.
143 * @param int $courseid The course being examined - the 'course' table id field.
144 * @return array An _array_ of groupids.
145 * (Was return $groupids[0] - consequences!)
147 function mygroupid($courseid) {
149 $groupids = groups_get_groups_for_user($USER->id
, $courseid);
154 * This now returns either false or SEPARATEGROUPS. If you want VISIBLE GROUPS
155 * with legacy code, you'll need to upgrade.
157 function groupmode($course, $cm=null) {
159 if ($cm and !$course->groupmodeforce
) {
160 return $cm->groupmode
;
162 return $course->groupmode
;
164 /*if ($cm and !$course->groupingid) {
165 //TODO: was $coursemodule
166 return groups_has_groups_setup_for_instance($cm);
168 return groups_has_groups_setup($course->id);
174 * Sets the current group in the session variable
175 * When $SESSION->currentgroup[$courseid] is set to 0 it means, show all groups.
176 * Sets currentgroup[$courseid] in the session variable appropriately.
177 * Does not do any permission checking.
179 * @param int $courseid The course being examined - relates to id field in
181 * @param int $groupid The group being examined.
182 * @return int Current group id which was set by this function
184 function set_current_group($courseid, $groupid) {
186 return $SESSION->currentgroup
[$courseid] = $groupid;
191 * Gets the current group - either from the session variable or from the database.
195 * @param int $courseid The course being examined - relates to id field in
197 * @param bool $full If true, the return value is a full record object.
198 * If false, just the id of the record.
200 function get_current_group($courseid, $full = false) {
203 $mygroupid = mygroupid($courseid);
204 if (is_array($mygroupid)) {
205 $mygroupid = array_shift($mygroupid);
208 if (isset($SESSION->currentgroup
[$courseid])) {
209 $currentgroup = $SESSION->currentgroup
[$courseid];
211 $currentgroup = $mygroupid;
215 $SESSION->currentgroup
[$courseid] = $mygroupid;
219 return groups_groupid_to_group($currentgroup);
221 return $currentgroup;
227 * A combination function to make it easier for modules
230 * It will use a given "groupid" parameter and try to use
231 * that to reset the current group for the user.
233 * @uses VISIBLEGROUPS
234 * @param course $course A {@link $COURSE} object
235 * @param int $groupmode Either NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS
236 * @param int $groupid Will try to use this optional parameter to
237 * reset the current group for the user
238 * @return int|false Returns the current group id or false if error.
240 function get_and_set_current_group($course, $groupmode, $groupid=-1) {
241 //TODO: ?? groups_has_permission($userid, $groupingid, $courseid, $groupid, $permissiontype);
243 // Sets to the specified group, provided the current user has view permission
244 if (!$groupmode) { // Groups don't even apply
248 $currentgroupid = get_current_group($course->id
);
250 if ($groupid < 0) { // No change was specified
251 return $currentgroupid;
254 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
255 if ($groupid) { // Try to change the current group to this groupid
256 if (groups_group_belongs_to_course($groupid, $course->id
)) { // Exists TODO:check.
257 if (has_capability('moodle/site:accessallgroups', $context)) { // Sets current default group
258 $currentgroupid = set_current_group($course->id
, $groupid);
260 } elseif ($groupmode == VISIBLEGROUPS
) {
261 // All groups are visible
262 //if (ismember($group->id)){
263 $currentgroupid = set_current_group($course->id
, $groupid); //set this since he might post
265 $currentgroupid = $group->id;*/
266 } elseif ($groupmode == SEPARATEGROUPS
) { // student in separate groups switching
267 if (ismember($group->id
)) { //check if is a member
268 $currentgroupid = set_current_group($course->id
, $groupid); //might need to set_current_group?
272 notify('you do not belong to this group!',error
);
276 } else { // When groupid = 0 it means show ALL groups
277 // this is changed, non editting teacher needs access to group 0 as well,
278 // for viewing work in visible groups (need to set current group for multiple pages)
279 if (has_capability('moodle/site:accessallgroups', $context) AND ($groupmode == VISIBLEGROUPS
)) { // Sets current default group
280 $currentgroupid = set_current_group($course->id
, 0);
282 } elseif ($groupmode == VISIBLEGROUPS
) { // All groups are visible
287 return $currentgroupid;
292 * A big combination function to make it easier for modules
295 * Terminates if the current user shouldn't be looking at this group
296 * Otherwise returns the current group if there is one
297 * Otherwise returns false if groups aren't relevant
299 * @uses SEPARATEGROUPS
300 * @uses VISIBLEGROUPS
301 * @param course $course A {@link $COURSE} object
302 * @param int $groupmode Either NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS
303 * @param string $urlroot ?
306 function setup_and_print_groups($course, $groupmode, $urlroot) {
308 global $USER, $SESSION; //needs his id, need to hack his groups in session
310 $changegroup = optional_param('group', -1, PARAM_INT
);
312 $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
313 if ($currentgroup === false) {
317 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
319 if ($groupmode == VISIBLEGROUPS
320 or ($groupmode and has_capability('moodle/site:accessallgroups', $context))) {
321 groups_instance_print_grouping_selector();
322 }//added code here to allow non-editting teacher to swap in-between his own groups
323 //added code for students in separategrous to swtich groups
324 else if ($groupmode == SEPARATEGROUPS
and has_capability('moodle/course:view', $context)) {
325 groups_instance_print_group_selector();
328 return $currentgroup;
332 function groups_instance_print_grouping_selector() {
335 function groups_instance_print_group_selector() {
340 function oldgroups_print_user_group_info($currentgroup, $isseparategroups, $courseid) {
342 $context = get_context_instance(CONTEXT_COURSE
, $courseid);
344 if ($currentgroup and (!$isseparategroups or has_capability('moodle/site:accessallgroups', $context))) { /// Display info about the group
345 if ($group = get_record('groups', 'id', $currentgroup)) {
346 if (!empty($group->description
) or (!empty($group->picture
) and empty($group->hidepicture
))) {
347 echo '<table class="groupinfobox"><tr><td class="left side picture">';
348 print_group_picture($group, $course->id
, true, false, false);
349 echo '</td><td class="content">';
350 echo '<h3>'.$group->name
;
351 if (has_capability('moodle/site:accessallgroups', $context)) {
352 echo ' <a title="'.get_string('editgroupprofile').'" href="../course/groups.php?id='.$course->id
.'&group='.$group->id
.'">';
353 echo '<img src="'.$CFG->pixpath
.'/t/edit.gif" alt="" border="0">';
357 echo format_text($group->description
);
358 echo '</td></tr></table>';
365 * Get the group object, including the course ID by default.
366 * @param groupid ID of the group.
367 * @param getcourse (default true), include the course ID in the return.
368 * @return group object, optionally including 'courseid'.
370 function groups_get_group($groupid, $getcourse=true) {
371 $group = groups_db_get_group_settings($groupid);
372 if ($group && $getcourse) {
373 $group->courseid
= groups_get_course($groupid);
380 * Get an array of groups, as id => name.
381 * Replaces, get_records_menu("groups", "courseid", $course->id, "name ASC", "id,name")
382 * (For /user/index.php)
384 function groups_get_groups_names($courseid) {
385 $groupids = groups_db_get_groups($courseid);
389 $groups_names = array();
390 foreach ($groupids as $id) {
391 $groups_names[$id] = groups_get_group_name($id);
394 return $groups_names;
398 * Get the groups that a number of users are in.
399 * (For block_quiz_results.php)
401 function groups_get_groups_users($userids, $courseid) {
403 $groups_users = get_records_sql(
404 'SELECT gm.userid, gm.groupid, g.name FROM '.$CFG->prefix
.'groups g LEFT JOIN '.$CFG->prefix
.'groups_members gm ON g.id = gm.groupid '.
405 'WHERE g.courseid = '.$courseid.' AND gm.userid IN ('.implode(',', $userids).')'
407 return $groups_users;