3 * Functions to make changes to groups in the database i.e. functions that
5 * 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', 'courseid', $courseid,
54 // Put the results into an array, note these are NOT 'group' objects.
56 foreach ($records as $record) {
57 array_push($groupids, $record->id
);
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 WHERE g.courseid = '$courseid' AND gm.userid = '$userid'";
108 $groups = get_records_sql($sql);
109 $groupids = groups_groups_to_groupids($groups);
117 * Get the group settings object for a group - this contains the following
119 * name, description, picture, hidepicture
120 * @param int $groupid The id of the group
121 * @param $courseid Optionally add the course ID, for backwards compatibility.
122 * @return object The group settings object
124 function groups_db_get_group_settings($groupid, $courseid=false, $alldata=false) {
126 $groupsettings = false;
129 $select = ($alldata) ?
'*' : 'id, name, description, picture, hidepicture';
130 $sql = "SELECT $select
131 FROM {$CFG->prefix}groups
132 WHERE id = $groupid";
133 $groupsettings = get_record_sql($sql);
134 if ($courseid && $groupsettings) {
135 $groupsettings->courseid
= $courseid;
139 return $groupsettings;
144 * Given two users, determines if there exists a group to which they both belong
145 * @param int $userid1 The id of the first user
146 * @param int $userid2 The id of the second user
147 * @return boolean True if the users are in a common group, false otherwise or
148 * if an error occurred.
150 function groups_db_users_in_common_group($userid1, $userid2) {
152 $havecommongroup = false;
153 $sql = "SELECT gm1.groupid, 1 FROM {$CFG->prefix}groups_members gm1 " .
154 "INNER JOIN {$CFG->prefix}groups_members gm2 " .
155 "ON gm1.groupid = gm2.groupid" .
156 "WHERE gm1.userid = '$userid1' AND gm2.userid = '$userid2'";
157 $commongroups = get_record_sql($sql);
159 $havecommongroup = true;
162 return $havecommongroup;
167 /*******************************************************************************
169 ******************************************************************************/
173 * Determines if a group with a given groupid exists.
174 * @param int $groupid The groupid to check for
175 * @return boolean True if the group exists, false otherwise or if an error
178 function groups_db_group_exists($groupid) {
182 $exists = record_exists($table = 'groups', 'id', $groupid);
190 * Determine if a course ID, group name and description match a group in the database.
191 * For backup/restorelib.php
192 * @return mixed A group-like object with $group->id, or false.
194 function groups_db_group_matches($courseid, $grp_name, $grp_description) {
195 //$gro_db->id; $gro_db = get_record("groups","courseid",$restore->course_id,"name",$gro->name,"description",$gro->description);
197 $sql = "SELECT g.id, g.name, g.description
198 FROM {$CFG->prefix}groups g
199 WHERE g.name = '$grp_name'
200 AND g.description = '$grp_description'
201 AND g.courseid = '$courseid'";
202 $records = get_records_sql($sql);
205 $group = array_shift($records);
211 * Determine if a course ID, and group name match a group in the database.
212 * @return mixed A group-like object with $group->id, or false.
214 function groups_db_group_name_exists($courseid, $grp_name) {
216 $sql = "SELECT g.id, g.name
217 FROM {$CFG->prefix}groups g
218 WHERE g.name = '$grp_name'
219 AND g.courseid = '$courseid'";
220 $records = get_records_sql($sql);
223 $group = current($records);
229 * Determines if a specified user is a member of a specified group
230 * @param int $groupid The group about which the request has been made
231 * @param int $userid The user about which the request has been made
232 * @return boolean True if the user is a member of the group, false otherwise
234 function groups_db_is_member($groupid, $userid) {
235 if (!$groupid or !$userid) {
238 $ismember = record_exists($table = 'groups_members', 'groupid',
239 $groupid, 'userid', $userid);
247 * Determines if a specified group is a group for a specified course
248 * @param int $groupid The group about which the request has been made
249 * @param int $courseid The course for which the request has been made
250 * @return boolean True if the group belongs to the course, false otherwise
252 function groups_db_group_belongs_to_course($groupid, $courseid) {
253 if (!$groupid or !$courseid) {
256 $ismember = record_exists($table = 'groups',
258 'courseid', $courseid);
265 /*******************************************************************************
267 ******************************************************************************/
271 * Creates a group for a specified course
272 * @param int $courseid The course to create the group for
273 * @return int The id of the group created or false if the create failed.
275 function groups_db_create_group($courseid, $groupsettings=false, $copytime=false) {
276 // Check we have a valid course id
280 $groupsettings = groups_set_default_group_settings($groupsettings);
282 $record = $groupsettings;
285 $record->timecreated
= $now;
286 $record->timemodified
= $now;
289 $groupid = insert_record('groups', $record);
296 * Upgrades a group for a specified course. To preserve the group ID we do a raw insert.
297 * @param int $courseid The course to create the group for
298 * @return int The id of the group created or false if the insert failed.
300 function groups_db_upgrade_group($courseid, $group) {
302 // Check we have a valid course id
303 if (!$courseid ||
!$group ||
!isset($group->id
)) {
307 $r = addslashes_object($group);
308 $sql = "INSERT INTO {$CFG->prefix}groups
309 (id,courseid,name,description, enrolmentkey,picture,hidepicture, timecreated,timemodified)
310 VALUES ('$r->id','$r->courseid','$r->name','$r->description', '$r->enrolmentkey','$r->picture',
311 '$r->hidepicture', '$r->timecreated','$r->timemodified')";
313 $result = execute_sql($sql);
319 * Adds a specified user to a group
320 * @param int $groupid The group id
321 * @param int $userid The user id
322 * @return boolean True if user added successfully, false otherwise.
324 function groups_db_add_member($groupid, $userid, $copytime=false) {
325 // Check that the user and group are valid
326 if (!$userid or !$groupid or !groups_db_group_exists($groupid)) {
328 // If the user is already a member of the group, just return success
329 } elseif (groups_is_member($groupid, $userid)) {
332 // Add the user to the group
333 $record = new Object();
334 $record->groupid
= $groupid;
335 $record->userid
= $userid;
337 $record->timeadded
= $copytime;
339 $record->timeadded
= time();
341 $useradded = insert_record($table = 'groups_members', $record);
349 * Sets the information about a group
350 * @param object $groupsettings An object containing some or all of the
351 * following properties:
352 * name, description, picture, hidepicture
353 * @return boolean True if info was added successfully, false otherwise.
355 function groups_db_set_group_settings($groupid, $groupsettings) {
357 if (!$groupid or !$groupsettings or !groups_db_group_exists($groupid)) {
360 $record = $groupsettings;
361 $record->id
= $groupid;
362 $record->timemodified
= time();
363 $result = update_record('groups', $record);
372 /*******************************************************************************
374 ******************************************************************************/
378 * Deletes the specified user from the specified group
379 * @param int $groupid The group to delete the user from
380 * @param int $userid The user to delete
381 * @return boolean True if deletion was successful, false otherwise
383 function groups_db_remove_member($groupid, $userid) {
384 if (!$userid or !$groupid) {
387 $results = delete_records('groups_members',
388 'groupid', $groupid, 'userid', $userid);
389 // delete_records returns an array of the results from the sql call,
390 // not a boolean, so we have to set our return variable
391 if ($results == false) {
403 * Delete a specified group, first removing members and links with courses and groupings.
404 * @param int $groupid The group to delete
405 * @return boolean True if deletion was successful, false otherwise
407 function groups_db_delete_group($groupid) {
412 // Get a list of users for the group and remove them all.
414 $userids = groups_db_get_members($groupid);
415 if ($userids != false) {
416 foreach($userids as $userid) {
417 $userdeleted = groups_db_remove_member($userid, $groupid);
424 // Remove any links with groupings to which the group belongs.
425 //TODO: dbgroupinglib also seems to delete these links - duplication?
426 $groupingids = groups_get_groupings_for_group($groupid);
427 if ($groupingids != false) {
428 foreach($groupingids as $groupingid) {
429 $groupremoved = groups_remove_group_from_grouping($groupid,
437 // Delete the group itself
438 $results = delete_records($table = 'groups', $field1 = 'id',
440 // delete_records returns an array of the results from the sql call,
441 // not a boolean, so we have to set our return variable
442 if ($results == false) {
451 * Internal function to set the time a group was modified.
453 function groups_db_set_group_modified($groupid) {
454 return set_field('groups', 'timemodified', time(), 'id', $groupid);
458 /******************************************************************************
459 * Groups SQL clauses for modules and core.
463 * Returns the table in which group members are stored, with a prefix 'gm'.
464 * @return SQL string.
466 function groups_members_from_sql() {
468 return " {$CFG->prefix}groups_members gm ";
472 * Returns a join testing user.id against member's user ID.
473 * Relies on 'user' table being included as 'user u'.
474 * Used in Quiz module reports.
475 * @param group ID, optional to include a test for this in the SQL.
476 * @return SQL string.
478 function groups_members_join_sql($groupid=false) {
479 $sql = ' JOIN '.groups_members_from_sql().' ON u.id = gm.userid ';
481 $sql = "AND gm.groupid = '$groupid' ";
484 //return ' INNER JOIN '.$CFG->prefix.'role_assignments ra ON u.id=ra.userid'.
485 // ' INNER JOIN '.$CFG->prefix.'context c ON ra.contextid=c.id AND c.contextlevel='.CONTEXT_GROUP.' AND c.instanceid='.$groupid;
489 * Returns SQL for a WHERE clause testing the group ID.
490 * Optionally test the member's ID against another table's user ID column.
492 * @param userid_sql Optional user ID column selector, example "mdl_user.id", or false.
493 * @return SQL string.
495 function groups_members_where_sql($groupid, $userid_sql=false) {
496 $sql = " gm.groupid = '$groupid' ";
498 $sql .= "AND $userid_sql = gm.userid ";