3 * Functions to make changes to groups in the database i.e. functions that
5 * groups_courses_groups, groups and groups_members.
7 * @copyright © 2006 The Open University
8 * @author J.White AT open.ac.uk
9 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
12 require_once($CFG->libdir
.'/datalib.php');
13 require_once($CFG->dirroot
.'/group/lib.php');
16 /*******************************************************************************
18 ******************************************************************************/
21 * Returns the user record for a given userid - I can't seem to find a function
22 * anywhere else to do this
23 * (and we need it to use the fullname() function).
24 * @param int $userid The id of the user to get the record for
25 * @return object The user record
27 function groups_db_get_user($userid) {
28 $query = get_record('user', 'id', $userid);
33 /*******************************************************************************
35 ******************************************************************************/
39 * Returns all the ids of all the groups for the specified course.
41 * @param int $courseid The courseid to get the groups for.
42 * @return array|false Returns an array of the group ids or false if no groups
43 * or an error returned
45 function groups_db_get_groups($courseid) {
49 $records = get_records('groups_courses_groups', 'courseid', $courseid,
50 '', $fields='id, groupid');
54 // Put the results into an array, note these are NOT 'group' objects.
56 foreach ($records as $record) {
57 array_push($groupids, $record->groupid
);
65 * Returns the ids of the users in the specified group.
66 * @param int $groupid The groupid to get the users for
67 * @return array| false Returns an array of the user ids for the specified
68 * group or false if no users or an error returned.
70 function groups_db_get_members($groupid) {
74 $users = get_records('groups_members', 'groupid ', $groupid, '',
75 $fields='id, userid');
80 foreach ($users as $user) {
81 array_push($userids, $user->userid
);
90 * Gets the groups to which a user belongs for a specified course.
92 * @param int $userid The id of the specified user
93 * @param int $courseid The id of the course.
94 * @return array | false An array of the group ids of the groups to which the
95 * user belongs or false if there are no groups or an error occurred.
97 function groups_db_get_groups_for_user($userid, $courseid) {
98 if (!$userid or !$courseid) {
102 $sql = "SELECT g.id, gm.userid
103 FROM {$CFG->prefix}groups_members gm
104 INNER JOIN {$CFG->prefix}groups g
106 INNER JOIN {$CFG->prefix}groups_courses_groups cg
108 WHERE cg.courseid = '$courseid' AND gm.userid = '$userid'";
110 $groups = get_records_sql($sql);
111 $groupids = groups_groups_to_groupids($groups);
119 * Get the group settings object for a group - this contains the following
121 * name, description, lang, theme, picture, hidepicture
122 * @param int $groupid The id of the group
123 * @param $courseid Optionally add the course ID, for backwards compatibility.
124 * @return object The group settings object
126 function groups_db_get_group_settings($groupid, $courseid=false, $alldata=false) {
128 $groupsettings = false;
131 $select = ($alldata) ?
'*' : 'id, name, description, lang, theme, picture, hidepicture';
132 $sql = "SELECT $select
133 FROM {$CFG->prefix}groups
134 WHERE id = $groupid";
135 $groupsettings = get_record_sql($sql);
136 if ($courseid && $groupsettings) {
137 $groupsettings->courseid
= $courseid;
141 return $groupsettings;
146 * Given two users, determines if there exists a group to which they both belong
147 * @param int $userid1 The id of the first user
148 * @param int $userid2 The id of the second user
149 * @return boolean True if the users are in a common group, false otherwise or
150 * if an error occurred.
152 function groups_db_users_in_common_group($userid1, $userid2) {
154 $havecommongroup = false;
155 $sql = "SELECT gm1.groupid, 1 FROM {$CFG->prefix}groups_members gm1 " .
156 "INNER JOIN {$CFG->prefix}groups_members gm2 " .
157 "ON gm1.groupid = gm2.groupid" .
158 "WHERE gm1.userid = '$userid1' AND gm2.userid = '$userid2'";
159 $commongroups = get_record_sql($sql);
161 $havecommongroup = true;
164 return $havecommongroup;
169 /*******************************************************************************
171 ******************************************************************************/
175 * Determines if a group with a given groupid exists.
176 * @param int $groupid The groupid to check for
177 * @return boolean True if the group exists, false otherwise or if an error
180 function groups_db_group_exists($groupid) {
184 $exists = record_exists($table = 'groups', 'id', $groupid);
192 * Determine if a course ID, group name and description match a group in the database.
193 * For backup/restorelib.php
194 * @return mixed A group-like object with $group->id, or false.
196 function groups_db_group_matches($courseid, $grp_name, $grp_description) {
197 //$gro_db->id; $gro_db = get_record("groups","courseid",$restore->course_id,"name",$gro->name,"description",$gro->description);
199 $sql = "SELECT g.id, g.name, g.description
200 FROM {$CFG->prefix}groups g
201 INNER JOIN {$CFG->prefix}groups_courses_groups cg ON g.id = cg.groupid
202 WHERE g.name = '$grp_name'
203 AND g.description = '$grp_description'
204 AND cg.courseid = '$courseid'";
205 $records = get_records_sql($sql);
208 $group = array_shift($records);
214 * Determine if a course ID, and group name match a group in the database.
215 * @return mixed A group-like object with $group->id, or false.
217 function groups_db_group_name_exists($courseid, $grp_name) {
219 $sql = "SELECT g.id, g.name
220 FROM {$CFG->prefix}groups g
221 INNER JOIN {$CFG->prefix}groups_courses_groups cg ON g.id = cg.groupid
222 WHERE g.name = '$grp_name'
223 AND cg.courseid = '$courseid'";
224 $records = get_records_sql($sql);
227 $group = current($records);
233 * Determines if a specified user is a member of a specified group
234 * @param int $groupid The group about which the request has been made
235 * @param int $userid The user about which the request has been made
236 * @return boolean True if the user is a member of the group, false otherwise
238 function groups_db_is_member($groupid, $userid) {
239 if (!$groupid or !$userid) {
242 $ismember = record_exists($table = 'groups_members', 'groupid',
243 $groupid, 'userid', $userid);
251 * Determines if a specified group is a group for a specified course
252 * @param int $groupid The group about which the request has been made
253 * @param int $courseid The course for which the request has been made
254 * @return boolean True if the group belongs to the course, false otherwise
256 function groups_db_group_belongs_to_course($groupid, $courseid) {
257 if (!$groupid or !$courseid) {
260 $ismember = record_exists($table = 'groups_courses_groups',
262 'courseid', $courseid);
269 /*******************************************************************************
271 ******************************************************************************/
275 * Creates a group for a specified course
276 * @param int $courseid The course to create the group for
277 * @return int The id of the group created or false if the create failed.
279 function groups_db_create_group($courseid, $groupsettings=false, $copytime=false) {
280 // Check we have a valid course id
284 $groupsettings = groups_set_default_group_settings($groupsettings);
286 $record = $groupsettings;
289 $record->timecreated
= $now;
290 $record->timemodified
= $now;
293 $groupid = insert_record('groups', $record);
295 if ($groupid != false) {
296 $record2 = new Object();
297 $record2->courseid
= $courseid;
298 $record2->groupid
= $groupid;
300 $record2->timeadded
= $record->timemodified
;
302 $record2->timeadded
= $now;
304 $groupadded = insert_record('groups_courses_groups', $record2);
314 * Upgrades a group for a specified course. To preserve the group ID we do a raw insert.
315 * @param int $courseid The course to create the group for
316 * @return int The id of the group created or false if the insert failed.
318 function groups_db_upgrade_group($courseid, $group) {
320 // Check we have a valid course id
321 if (!$courseid ||
!$group ||
!isset($group->id
)) {
325 $r = addslashes_object($group);
326 $sql = "INSERT INTO {$CFG->prefix}groups
327 (id,name,description, enrolmentkey,lang,theme,picture,hidepicture, timecreated,timemodified)
328 VALUES ('$r->id','$r->name','$r->description', '$r->enrolmentkey','$r->lang',
329 '$r->theme','$r->picture','$r->hidepicture', '$r->timecreated','$r->timemodified')";
331 if ($result = execute_sql($sql)) {
332 $record2 = new Object();
333 $record2->courseid
= $courseid;
334 $record2->groupid
= $group->id
;
335 $record2->timeadded
= $group->timemodified
;
337 $groupadded = insert_record('groups_courses_groups', $record2);
347 * Adds a specified user to a group
348 * @param int $groupid The group id
349 * @param int $userid The user id
350 * @return boolean True if user added successfully, false otherwise.
352 function groups_db_add_member($groupid, $userid, $copytime=false) {
353 // Check that the user and group are valid
354 if (!$userid or !$groupid or !groups_db_group_exists($groupid)) {
356 // If the user is already a member of the group, just return success
357 } elseif (groups_is_member($groupid, $userid)) {
360 // Add the user to the group
361 $record = new Object();
362 $record->groupid
= $groupid;
363 $record->userid
= $userid;
365 $record->timeadded
= $copytime;
367 $record->timeadded
= time();
369 $useradded = insert_record($table = 'groups_members', $record);
377 * Sets the information about a group
378 * @param object $groupsettings An object containing some or all of the
379 * following properties:
380 * name, description, lang, theme, picture, hidepicture
381 * @return boolean True if info was added successfully, false otherwise.
383 function groups_db_set_group_settings($groupid, $groupsettings) {
385 if (!$groupid or !$groupsettings or !groups_db_group_exists($groupid)) {
388 $record = $groupsettings;
389 $record->id
= $groupid;
390 $record->timemodified
= time();
391 $result = update_record('groups', $record);
400 /*******************************************************************************
402 ******************************************************************************/
406 * Deletes the specified user from the specified group
407 * @param int $groupid The group to delete the user from
408 * @param int $userid The user to delete
409 * @return boolean True if deletion was successful, false otherwise
411 function groups_db_remove_member($groupid, $userid) {
412 if (!$userid or !$groupid) {
415 $results = delete_records('groups_members',
416 'groupid', $groupid, 'userid', $userid);
417 // delete_records returns an array of the results from the sql call,
418 // not a boolean, so we have to set our return variable
419 if ($results == false) {
431 * Delete a specified group, first removing members and links with courses and groupings.
432 * @param int $groupid The group to delete
433 * @return boolean True if deletion was successful, false otherwise
435 function groups_db_delete_group($groupid) {
440 // Get a list of users for the group and remove them all.
442 $userids = groups_db_get_members($groupid);
443 if ($userids != false) {
444 foreach($userids as $userid) {
445 $userdeleted = groups_db_remove_member($userid, $groupid);
452 // Remove any links with groupings to which the group belongs.
453 //TODO: dbgroupinglib also seems to delete these links - duplication?
454 $groupingids = groups_get_groupings_for_group($groupid);
455 if ($groupingids != false) {
456 foreach($groupingids as $groupingid) {
457 $groupremoved = groups_remove_group_from_grouping($groupid,
465 // Remove links with courses.
466 $results = delete_records('groups_courses_groups', 'groupid', $groupid);
467 if ($results == false) {
471 // Delete the group itself
472 $results = delete_records($table = 'groups', $field1 = 'id',
474 // delete_records returns an array of the results from the sql call,
475 // not a boolean, so we have to set our return variable
476 if ($results == false) {
485 * Internal function to set the time a group was modified.
487 function groups_db_set_group_modified($groupid) {
488 return set_field('groups', 'timemodified', time(), 'id', $groupid);
492 /******************************************************************************
493 * Groups SQL clauses for modules and core.
497 * Returns the table in which group members are stored, with a prefix 'gm'.
498 * @return SQL string.
500 function groups_members_from_sql() {
502 return " {$CFG->prefix}groups_members gm ";
506 * Returns a join testing user.id against member's user ID.
507 * Relies on 'user' table being included as 'user u'.
508 * Used in Quiz module reports.
509 * @param group ID, optional to include a test for this in the SQL.
510 * @return SQL string.
512 function groups_members_join_sql($groupid=false) {
513 $sql = ' JOIN '.groups_members_from_sql().' ON u.id = gm.userid ';
515 $sql = "AND gm.groupid = '$groupid' ";
518 //return ' INNER JOIN '.$CFG->prefix.'role_assignments ra ON u.id=ra.userid'.
519 // ' INNER JOIN '.$CFG->prefix.'context c ON ra.contextid=c.id AND c.contextlevel='.CONTEXT_GROUP.' AND c.instanceid='.$groupid;
523 * Returns SQL for a WHERE clause testing the group ID.
524 * Optionally test the member's ID against another table's user ID column.
526 * @param userid_sql Optional user ID column selector, example "mdl_user.id", or false.
527 * @return SQL string.
529 function groups_members_where_sql($groupid, $userid_sql=false) {
530 $sql = " gm.groupid = '$groupid' ";
532 $sql .= "AND $userid_sql = gm.userid ";