MDL-9628 Added title attributes to quickgrading and quickfeedback input elements...
[moodle-pu.git] / group / lib / basicgrouplib.php
blobf4308057589fbc35c3f312bf29bc9cad378f516d
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 group ID.
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);
210 * Determine if a course ID, and group name match a group in the database.
211 * @return mixed A group-like object with $group->id, or false.
213 function groups_group_name_exists($courseid, $grp_name) {
214 return groups_db_group_name_exists($courseid, $grp_name);
218 * Determines if the user is a member of the given group.
220 * @uses $USER If $userid is null, use the global object.
221 * @param int $groupid The group to check for membership.
222 * @param int $userid The user to check against the group.
223 * @return boolean True if the user is a member, false otherwise.
225 function groups_is_member($groupid, $userid = null) {
226 if (! $userid) {
227 global $USER;
228 $userid = $USER->id;
230 $ismember = groups_db_is_member($groupid, $userid);
232 return $ismember;
237 * Determines if a specified group is a group for a specified course
238 * @param int $groupid The group about which the request has been made
239 * @param int $courseid The course for which the request has been made
240 * @return boolean True if the group belongs to the course, false otherwise
242 function groups_group_belongs_to_course($groupid, $courseid) {
243 $belongstocourse = groups_db_group_belongs_to_course($groupid, $courseid);
244 return $belongstocourse;
248 * Returns an object with the default group info values - these can of course be
249 * overridden if desired.
250 * Can also be used to set the default for any values not set
251 * @return object The group info object.
253 function groups_set_default_group_settings($groupinfo = null) {
255 if (!isset($groupinfo->name)) {
256 $groupinfo->name = 'Temporary Group Name';
259 if (!isset($groupinfo->description)) {
260 $groupinfo->description = '';
263 if (!isset($groupinfo->lang)) {
264 $groupinfo->lang = current_language();
267 if (!isset($groupinfo->theme)) {
268 $groupinfo->theme = '';
271 if (!isset($groupinfo->picture)) {
272 $groupinfo->picture = 0;
275 if (!isset($groupinfo->hidepicture)) {
276 $groupinfo->hidepicture = 1;
279 if (isset($groupinfo->hidepicture)) {
280 if ($groupinfo->hidepicture != 0 and $groupinfo->hidepicture != 1) {
281 $groupinfo->hidepicture = 1;
285 return $groupinfo;
288 /*****************************
289 Creation functions
290 *****************************/
293 * Creates a group for a specified course
294 * All groups should really belong to a grouping (though there is nothing in
295 * this API that stops them not doing
296 * so, to allow plenty of flexibility) so you should be using this in
297 * conjunction with the function to add a group to
298 * a grouping.
299 * @param int $courseid The course to create the group for
300 * @return int | false The id of the group created or false if the group was
301 * not created successfully.
302 * See comment above on web service autoupdating.
304 function groups_create_group($courseid, $groupsettings = false) {
305 return groups_db_create_group($courseid, $groupsettings);
309 * Restore a group for a specified course.
310 * For backup/restorelib.php
312 function groups_restore_group($courseid, $groupsettings) {
313 return groups_db_create_group($courseid, $groupsettings, $copytime=true);
318 * Sets the information about a group
319 * Only sets the string for the picture - does not upload the picture!
320 * @param object $groupsettings An object containing some or all of the
321 * following properties: name, description, lang, theme, picture, hidepicture
322 * @return boolean True if info was added successfully, false otherwise.
324 function groups_set_group_settings($groupid, $groupsettings) {
325 return groups_db_set_group_settings($groupid, $groupsettings);
330 * Adds a specified user to a group
331 * @param int $userid The user id
332 * @param int $groupid The group id
333 * @return boolean True if user added successfully or the user is already a
334 * member of the group, false otherwise.
335 * See comment above on web service autoupdating.
337 function groups_add_member($groupid, $userid) {
338 $useradded = false;
340 $alreadymember = groups_is_member($groupid, $userid);
341 if (!groups_group_exists($groupid)) {
342 $useradded = false;
343 } elseif ($alreadymember) {
344 $useradded = true;
345 } else {
346 $useradded = groups_db_add_member($groupid, $userid);
348 if ($useradded) {
350 // MDL-9983
351 $eventdata = new object();
352 $eventdata -> groupid = $groupid;
353 $eventdata -> userid = $userid;
354 events_trigger('group_user_added', $eventdata);
355 $useradded = groups_db_set_group_modified($groupid);
357 return $useradded;
361 * Restore a user to the group specified in $member.
362 * For backup/restorelib.php
363 * @param $member object Group member object.
365 function groups_restore_member($member) {
366 $alreadymember = groups_is_member($member->groupid, $member->userid);
367 if (! groups_group_exists($member->groupid)) {
368 return false;
369 } elseif ($alreadymember) {
370 return true;
371 } else {
372 $useradded = groups_db_add_member($member->groupid, $member->userid, $member->timeadded);
374 return $useradded;
378 /*****************************
379 Deletion functions
380 *****************************/
384 * Delete a group best effort, first removing members and links with courses and groupings.
385 * @param int $groupid The group to delete
386 * @return boolean True if deletion was successful, false otherwise
387 * See comment above on web service autoupdating.
389 function groups_delete_group($groupid) {
390 $groupdeleted = groups_db_delete_group($groupid);
392 return $groupdeleted;
397 * Deletes the link between the specified user and group.
398 * @param int $groupid The group to delete the user from
399 * @param int $userid The user to delete
400 * @return boolean True if deletion was successful, false otherwise
401 * See comment above on web service autoupdating.
403 function groups_remove_member($groupid, $userid) {
404 $success = groups_db_remove_member($groupid, $userid);
405 if ($success) {
406 $success = groups_db_set_group_modified($groupid);
408 return $success;
412 * Removes all users from the specified group.
413 * @param int $groupid The ID of the group.
414 * @return boolean True for success, false otherwise.
416 function groups_remove_all_members($groupid) {
417 if (! groups_group_exists($groupid)) {
418 //Woops, delete group last!
419 return false;
421 $userids = groups_get_members($groupid);
422 if (! $userids) {
423 return false;
425 $success = true;
426 foreach ($userids as $id) {
427 $success = $success && groups_db_remove_member($groupid, $id);
429 $success = $success && groups_db_set_group_modified($groupid);
430 return $success;
434 * Update a group and return true or false
436 * @param object $data - all the data needed for an entry in the 'groups' table
438 function groups_update_group($data, $courseid) {
439 $oldgroup = get_record('groups', 'id', $data->id); // should not fail, already tested above
441 // Update with the new data
442 if (update_record('groups', $data)) {
444 $group = get_record('groups', 'id', $data->id);
446 add_to_log($group->id, "groups", "update", "edit.php?id=$courseid&amp;group=$group->id", "");
448 return true;
452 return false;