Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / group / db / dbbasicgrouplib.php
blob735a531489c3215ee7c02664ee1b36fbb13b2ae0
1 <?php
2 /**
3 * Functions to make changes to groups in the database i.e. functions that
4 * access tables:
5 * groups_courses_groups, groups and groups_members.
7 * @copyright &copy; 2006 The Open University
8 * @author J.White AT open.ac.uk
9 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
10 * @package groups
12 require_once($CFG->libdir.'/datalib.php');
13 require_once($CFG->dirroot.'/group/lib.php');
16 /*******************************************************************************
17 * Utility functions
18 ******************************************************************************/
20 /**
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);
29 return $query;
33 /*******************************************************************************
34 List functions
35 ******************************************************************************/
38 /**
39 * Returns all the ids of all the groups for the specified course.
40 * @uses $CFG
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) {
46 if (! $courseid) {
47 return false;
49 $records = get_records('groups_courses_groups', 'courseid', $courseid,
50 '', $fields='id, groupid');
51 if (! $records) {
52 return false;
54 // Put the results into an array, note these are NOT 'group' objects.
55 $groupids = array();
56 foreach ($records as $record) {
57 array_push($groupids, $record->groupid);
60 return $groupids;
64 /**
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) {
71 if (!$groupid) {
72 $userids = false;
73 } else {
74 $users = get_records('groups_members', 'groupid ', $groupid, '',
75 $fields='id, userid');
76 if (!$users) {
77 $userids = false;
78 } else {
79 $userids = array();
80 foreach ($users as $user) {
81 array_push($userids, $user->userid);
85 return $userids;
89 /**
90 * Gets the groups to which a user belongs for a specified course.
91 * @uses $CFG
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) {
99 $groupids = false;
100 } else {
101 global $CFG;
102 $sql = "SELECT g.id, gm.userid
103 FROM {$CFG->prefix}groups_members gm
104 INNER JOIN {$CFG->prefix}groups g
105 ON gm.groupid = g.id
106 INNER JOIN {$CFG->prefix}groups_courses_groups cg
107 ON g.id = cg.groupid
108 WHERE cg.courseid = '$courseid' AND gm.userid = '$userid'";
110 $groups = get_records_sql($sql);
111 $groupids = groups_groups_to_groupids($groups);
114 return $groupids;
119 * Get the group settings object for a group - this contains the following
120 * properties:
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) {
127 if (!$groupid) {
128 $groupsettings = false;
129 } else {
130 global $CFG;
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) {
153 global $CFG;
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);
160 if ($commongroups) {
161 $havecommongroup = true;
164 return $havecommongroup;
169 /*******************************************************************************
170 Membership functions
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
178 * occurred.
180 function groups_db_group_exists($groupid) {
181 if (!$groupid) {
182 $exists = false;
183 } else {
184 $exists = record_exists($table = 'groups', 'id', $groupid);
187 return $exists;
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);
198 global $CFG;
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);
206 $group = false;
207 if ($records) {
208 $group = array_shift($records);
210 return $group;
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) {
218 global $CFG;
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);
225 $group = false;
226 if ($records) {
227 $group = current($records);
229 return $group;
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) {
240 $ismember = false;
241 } else {
242 $ismember = record_exists($table = 'groups_members', 'groupid',
243 $groupid, 'userid', $userid);
246 return $ismember;
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) {
258 $ismember = false;
259 } else {
260 $ismember = record_exists($table = 'groups_courses_groups',
261 'groupid', $groupid,
262 'courseid', $courseid);
265 return $ismember;
269 /*******************************************************************************
270 Creation functions
271 ******************************************************************************/
274 /**
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
281 if (!$courseid) {
282 $groupid = false;
283 } else {
284 $groupsettings = groups_set_default_group_settings($groupsettings);
286 $record = $groupsettings;
287 if (! $copytime) {
288 $now = time();
289 $record->timecreated = $now;
290 $record->timemodified = $now;
292 //print_r($record);
293 $groupid = insert_record('groups', $record);
295 if ($groupid != false) {
296 $record2 = new Object();
297 $record2->courseid = $courseid;
298 $record2->groupid = $groupid;
299 if ($copytime) {
300 $record2->timeadded = $record->timemodified;
301 } else {
302 $record2->timeadded = $now;
304 $groupadded = insert_record('groups_courses_groups', $record2);
305 if (!$groupadded) {
306 $groupid = false;
310 return $groupid;
313 /**
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) {
319 global $CFG;
320 // Check we have a valid course id
321 if (!$courseid || !$group || !isset($group->id)) {
322 return false;
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);
338 if (! $groupadded) {
339 $groupid = false;
342 return $group->id;
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)) {
355 $useradded = false;
356 // If the user is already a member of the group, just return success
357 } elseif (groups_is_member($groupid, $userid)) {
358 $useradded = true;
359 } else {
360 // Add the user to the group
361 $record = new Object();
362 $record->groupid = $groupid;
363 $record->userid = $userid;
364 if ($copytime) {
365 $record->timeadded = $copytime;
366 } else {
367 $record->timeadded = time();
369 $useradded = insert_record($table = 'groups_members', $record);
372 return $useradded;
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) {
384 $success = true;
385 if (!$groupid or !$groupsettings or !groups_db_group_exists($groupid)) {
386 $success = false;
387 } else {
388 $record = $groupsettings;
389 $record->id = $groupid;
390 $record->timemodified = time();
391 $result = update_record('groups', $record);
392 if (!$result) {
393 $success = false;
397 return $success;
400 /*******************************************************************************
401 Deletion functions
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) {
413 $success = false;
414 } else {
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) {
420 $success = false;
421 } else {
422 $success = true;
426 return $success;
430 /**
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) {
436 if (!$groupid) {
437 $success = false;
438 } else {
439 $success = true;
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);
446 if (!$userdeleted) {
447 $success = false;
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,
458 $groupingid);
459 if(!$groupremoved) {
460 $success = false;
465 // Remove links with courses.
466 $results = delete_records('groups_courses_groups', 'groupid', $groupid);
467 if ($results == false) {
468 $success = false;
471 // Delete the group itself
472 $results = delete_records($table = 'groups', $field1 = 'id',
473 $value1 = $groupid);
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) {
477 $success = false;
481 return $success;
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() {
501 global $CFG;
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 ';
514 if ($groupid) {
515 $sql = "AND gm.groupid = '$groupid' ";
517 return $sql;
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.
525 * @param groupid
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' ";
531 if ($userid_sql) {
532 $sql .= "AND $userid_sql = gm.userid ";
534 return $sql;