Fixes bug MDL-8234, "New groups code & AS keyword"
[moodle-pu.git] / group / lib / basicgrouplib.php
blob8cb78c83930cc5ab15a0aedcfe6d692429af0927
1 <?php
2 /**
3 * Library of basic group functions.
5 * These functions are essentially just wrappers for the equivalent database
6 * functions in db/dbgrouplib.php
7 *
8 * It is advised that you do not create groups that do not belong to a
9 * grouping, although to allow maximum flexibility, functions are
10 * provided that allow you to do this.
11 * Note that groups (and groupings - see groupinglib.php) must belong to a
12 * course. There is no reason why a group cannot belong to more than one
13 * course, although this might cause problems when group members are not
14 * users of one of the courses.
15 * At the moment, there are no checks that group members are also users of a
16 * course.
18 * @copyright &copy; 2006 The Open University
19 * @author J.White AT open.ac.uk
20 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
21 * @package groups
23 require_once($CFG->dirroot.'/group/db/dbbasicgrouplib.php');
26 /*****************************
27 List functions
28 *****************************/
30 /**
31 * Gets a list of the group IDs for a specified course.
32 * @param int $courseid The id of the course.
33 * @return array | false Returns an array of the group IDs or false if no records
34 * or an error occurred.
36 function groups_get_groups($courseid) {
37 $groupids = groups_db_get_groups($courseid);
38 return $groupids;
42 /**
43 * Returns the ids of the users in the specified group.
44 * @param int $groupid The groupid to get the users for
45 * @param string $membertype Either 'student', 'teacher' or false. The function
46 * only returns these
47 * types of group members. If set to false, returns all group members.
48 * @return array | false Returns an array of the user ids for the specified
49 * group or false if no users or an error returned.
51 function groups_get_members($groupid, $membertype = false) {
52 $userids = groups_db_get_members($groupid);
54 return $userids;
57 /**
58 * Get the user ID and time added for each member of a group, for backup4.
59 * @return array An array of member records.
61 function groups_get_member_records($groupid) {
62 if (!$groupid) {
63 return false;
65 $members = get_records('groups_members', 'groupid ', $groupid, '',
66 $fields='id, userid, timeadded');
68 return $members;
72 /**
73 * Gets the groups to which a user belongs for a specified course.
74 * @param int $userid The id of the specified user
75 * @param int $courseid The id of the course.
76 * @param boolean $usedatabase. If true, gets the information from
77 * @return array | false An array of the group ids of the groups to which the
78 * user belongs or false if there are no groups or an error occurred.
80 function groups_get_groups_for_user($userid, $courseid) {
81 $groupids = groups_db_get_groups_for_user($userid, $courseid);
82 return $groupids;
85 /**
86 * Get the groups to which a user belongs in any course on site.
87 * @return array | false An array of the group IDs, or false on error.
89 function groups_get_all_groups_for_user($userid) {
90 $groups = get_records('groups_members', 'userid', $userid);
91 if (! $groups) {
92 return false;
94 // Put the results into an array. TODO: check.
95 $groupids = array();
96 foreach ($groups as $group) {
97 array_push($groupids, $group->id);
99 return $groupids;
103 * Gets the groups for the current user and specified course
104 * @param int $courseid The id of the course
105 * @param int $usedatabase Set to true if the information is to be obtained
106 * directly
107 * from the database, false if it is to be obtained from the $USER object.
108 * @return array An array of the groupids.
110 function groups_get_groups_for_current_user($courseid) {
111 global $USER;
112 $groupids = groups_get_groups_for_user($USER->id, $courseid);
113 return $groupids;
118 * Get the group settings object for a group - this contains the following
119 * properties:
120 * name, description, lang, theme, picture, hidepicture
121 * @param int $groupid The id of the gruop
122 * @return object The group settings object
124 function groups_get_group_settings($groupid, $courseid=false, $alldata=false) {
125 return groups_db_get_group_settings($groupid, $courseid, $alldata);
129 * Gets the path where the image for a particular group can be found (if it
130 * exists)
131 * @param int $groupid The id of the group
132 * @return string The path of the image for the group
134 function groups_get_group_image_path($groupid) {
135 //TODO: groupid=1, /user/pixgroup.php/1/f1.jpg ??
136 return $CFG->wwwroot.'/pixgroup.php/'.$groupid.'/f1.jpg';
140 * Gets the name of a group with a specified id
141 * @param int $groupid The id of the group
142 * @return string The name of the group
144 function groups_get_group_name($groupid) {
145 $settings = groups_get_group_settings($groupid);
146 if ($settings) {
147 return $settings->name;
149 return false;
153 * Gets the users for a course who are not in a specified group
154 * @param int $groupid The id of the group
155 * @return array An array of the userids of the non-group members, or false if
156 * an error occurred.
158 function groups_get_users_not_in_group($courseid, $groupid) {
159 $users = get_course_users($courseid);
160 $userids = groups_users_to_userids($users);
161 $nongroupmembers = array();
163 foreach($userids as $userid) {
164 if (!groups_is_member($groupid, $userid)) {
165 array_push($nongroupmembers, $userid);
169 return $nongroupmembers;
173 * Given two users, determines if there exists a group to which they both belong
174 * @param int $userid1 The id of the first user
175 * @param int $userid2 The id of the second user
176 * @return boolean True if the users are in a common group, false otherwise or
177 * if an error occurred.
179 function groups_users_in_common_group($userid1, $userid2) {
180 return groups_db_users_in_common_group($userid1, $userid2);
184 /*****************************
185 Membership functions
186 *****************************/
190 * Determines if a group with a given groupid exists.
191 * @param int $groupid The groupid to check for
192 * @return boolean True if the group exists, false otherwise or if an error
193 * occurred.
195 function groups_group_exists($groupid) {
196 return groups_db_group_exists($groupid);
201 * Determine if a course ID, group name and description match a group in the database.
202 * For backup/restorelib.php
203 * @return mixed A group-like object with $group->id, or false.
205 function groups_group_matches($courseid, $grp_name, $grp_description) {
206 return groups_db_group_matches($courseid, $grp_name, $grp_description);
211 * Determines if a specified user is a member of a specified group
212 * @param int $groupid The group about which the request has been made
213 * @param int $userid The user about which the request has been made
214 * @return boolean True if the user is a member of the group, false otherwise
216 function groups_is_member($groupid, $userid) {
217 $ismember = groups_db_is_member($groupid, $userid);
219 return $ismember;
224 * Determines if a specified group is a group for a specified course
225 * @param int $groupid The group about which the request has been made
226 * @param int $courseid The course for which the request has been made
227 * @return boolean True if the group belongs to the course, false otherwise
229 function groups_group_belongs_to_course($groupid, $courseid) {
230 $belongstocourse = groups_db_group_belongs_to_course($groupid, $courseid);
231 return $belongstocourse;
235 * Returns an object with the default group info values - these can of course be
236 * overridden if desired.
237 * Can also be used to set the default for any values not set
238 * @return object The group info object.
240 function groups_set_default_group_settings($groupinfo = null) {
242 if (!isset($groupinfo->name)) {
243 $groupinfo->name = 'Temporary Group Name';
246 if (!isset($groupinfo->description)) {
247 $groupinfo->description = '';
250 if (!isset($groupinfo->lang)) {
251 $groupinfo->lang = current_language();
254 if (!isset($groupinfo->theme)) {
255 $groupinfo->theme = '';
258 if (!isset($groupinfo->picture)) {
259 $groupinfo->picture = 0;
262 if (!isset($groupinfo->hidepicture)) {
263 $groupinfo->hidepicture = 1;
266 if (isset($groupinfo->hidepicture)) {
267 if ($groupinfo->hidepicture != 0 and $groupinfo->hidepicture != 1) {
268 $groupinfo->hidepicture = 1;
272 return $groupinfo;
275 /*****************************
276 Creation functions
277 *****************************/
280 * Creates a group for a specified course
281 * All groups should really belong to a grouping (though there is nothing in
282 * this API that stops them not doing
283 * so, to allow plenty of flexibility) so you should be using this in
284 * conjunction with the function to add a group to
285 * a grouping.
286 * @param int $courseid The course to create the group for
287 * @return int | false The id of the group created or false if the group was
288 * not created successfully.
289 * See comment above on web service autoupdating.
291 function groups_create_group($courseid, $groupsettings = false) {
292 return groups_db_create_group($courseid, $groupsettings);
296 * Restore a group for a specified course.
297 * For backup/restorelib.php
299 function groups_restore_group($courseid, $groupsettings) {
300 return groups_db_create_group($courseid, $groupsettings, $copytime=true);
305 * Sets the information about a group
306 * Only sets the string for the picture - does not upload the picture!
307 * @param object $groupsettings An object containing some or all of the
308 * following properties: name, description, lang, theme, picture, hidepicture
309 * @return boolean True if info was added successfully, false otherwise.
311 function groups_set_group_settings($groupid, $groupsettings) {
312 return groups_db_set_group_settings($groupid, $groupsettings);
317 * Adds a specified user to a group
318 * @param int $userid The user id
319 * @param int $groupid The group id
320 * @return boolean True if user added successfully or the user is already a
321 * member of the group, false otherwise.
322 * See comment above on web service autoupdating.
324 function groups_add_member($groupid, $userid) {
325 $useradded = false;
327 $alreadymember = groups_is_member($groupid, $userid);
328 if (!groups_group_exists($groupid)) {
329 $useradded = false;
330 } elseif ($alreadymember) {
331 $useradded = true;
332 } else {
333 $useradded = groups_db_add_member($groupid, $userid);
335 if ($useradded) {
336 $useradded = groups_db_set_group_modified($groupid);
338 return $useradded;
342 * Restore a user to the group specified in $member.
343 * For backup/restorelib.php
344 * @param $member object Group member object.
346 function groups_restore_member($member) {
347 $alreadymember = groups_is_member($member->groupid, $member->userid);
348 if (! groups_group_exists($member->groupid)) {
349 return false;
350 } elseif ($alreadymember) {
351 return true;
352 } else {
353 $useradded = groups_db_add_member($member->groupid, $member->userid, $member->timeadded);
355 return $useradded;
359 /*****************************
360 Deletion functions
361 *****************************/
365 * Delete a group best effort, first removing members and links with courses and groupings.
366 * @param int $groupid The group to delete
367 * @return boolean True if deletion was successful, false otherwise
368 * See comment above on web service autoupdating.
370 function groups_delete_group($groupid) {
371 $groupdeleted = groups_db_delete_group($groupid);
373 return $groupdeleted;
378 * Deletes the link between the specified user and 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
382 * See comment above on web service autoupdating.
384 function groups_remove_member($groupid, $userid) {
385 $success = groups_db_remove_member($groupid, $userid);
386 if ($success) {
387 $success = groups_db_set_group_modified($groupid);
389 return $success;
393 * Removes all users from the specified group.
394 * @param int $groupid The ID of the group.
395 * @return boolean True for success, false otherwise.
397 function groups_remove_all_members($groupid) {
398 if (! groups_group_exists($groupid)) {
399 //Woops, delete group last!
400 return false;
402 $userids = groups_get_members($groupid);
403 if (! $userids) {
404 return false;
406 $success = true;
407 foreach ($userids as $id) {
408 $success = $success && groups_db_remove_member($groupid, $id);
410 $success = $success && groups_db_set_group_modified($groupid);
411 return $success;