Fixes bug MDL-8454, "students can't post to forums"
[moodle-pu.git] / group / lib / basicgrouplib.php
blob3a8687a878e3365fad096b6016efb8261a43f43a
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 the user is a member of the given group.
213 * @uses $USER If $userid is null, use the global object.
214 * @param int $groupid The group to check for membership.
215 * @param int $userid The user to check against the group.
216 * @return boolean True if the user is a member, false otherwise.
218 function groups_is_member($groupid, $userid = null) {
219 if (! $userid) {
220 global $USER;
221 $userid = $USER->id;
223 $ismember = groups_db_is_member($groupid, $userid);
225 return $ismember;
230 * Determines if a specified group is a group for a specified course
231 * @param int $groupid The group about which the request has been made
232 * @param int $courseid The course for which the request has been made
233 * @return boolean True if the group belongs to the course, false otherwise
235 function groups_group_belongs_to_course($groupid, $courseid) {
236 $belongstocourse = groups_db_group_belongs_to_course($groupid, $courseid);
237 return $belongstocourse;
241 * Returns an object with the default group info values - these can of course be
242 * overridden if desired.
243 * Can also be used to set the default for any values not set
244 * @return object The group info object.
246 function groups_set_default_group_settings($groupinfo = null) {
248 if (!isset($groupinfo->name)) {
249 $groupinfo->name = 'Temporary Group Name';
252 if (!isset($groupinfo->description)) {
253 $groupinfo->description = '';
256 if (!isset($groupinfo->lang)) {
257 $groupinfo->lang = current_language();
260 if (!isset($groupinfo->theme)) {
261 $groupinfo->theme = '';
264 if (!isset($groupinfo->picture)) {
265 $groupinfo->picture = 0;
268 if (!isset($groupinfo->hidepicture)) {
269 $groupinfo->hidepicture = 1;
272 if (isset($groupinfo->hidepicture)) {
273 if ($groupinfo->hidepicture != 0 and $groupinfo->hidepicture != 1) {
274 $groupinfo->hidepicture = 1;
278 return $groupinfo;
281 /*****************************
282 Creation functions
283 *****************************/
286 * Creates a group for a specified course
287 * All groups should really belong to a grouping (though there is nothing in
288 * this API that stops them not doing
289 * so, to allow plenty of flexibility) so you should be using this in
290 * conjunction with the function to add a group to
291 * a grouping.
292 * @param int $courseid The course to create the group for
293 * @return int | false The id of the group created or false if the group was
294 * not created successfully.
295 * See comment above on web service autoupdating.
297 function groups_create_group($courseid, $groupsettings = false) {
298 return groups_db_create_group($courseid, $groupsettings);
302 * Restore a group for a specified course.
303 * For backup/restorelib.php
305 function groups_restore_group($courseid, $groupsettings) {
306 return groups_db_create_group($courseid, $groupsettings, $copytime=true);
311 * Sets the information about a group
312 * Only sets the string for the picture - does not upload the picture!
313 * @param object $groupsettings An object containing some or all of the
314 * following properties: name, description, lang, theme, picture, hidepicture
315 * @return boolean True if info was added successfully, false otherwise.
317 function groups_set_group_settings($groupid, $groupsettings) {
318 return groups_db_set_group_settings($groupid, $groupsettings);
323 * Adds a specified user to a group
324 * @param int $userid The user id
325 * @param int $groupid The group id
326 * @return boolean True if user added successfully or the user is already a
327 * member of the group, false otherwise.
328 * See comment above on web service autoupdating.
330 function groups_add_member($groupid, $userid) {
331 $useradded = false;
333 $alreadymember = groups_is_member($groupid, $userid);
334 if (!groups_group_exists($groupid)) {
335 $useradded = false;
336 } elseif ($alreadymember) {
337 $useradded = true;
338 } else {
339 $useradded = groups_db_add_member($groupid, $userid);
341 if ($useradded) {
342 $useradded = groups_db_set_group_modified($groupid);
344 return $useradded;
348 * Restore a user to the group specified in $member.
349 * For backup/restorelib.php
350 * @param $member object Group member object.
352 function groups_restore_member($member) {
353 $alreadymember = groups_is_member($member->groupid, $member->userid);
354 if (! groups_group_exists($member->groupid)) {
355 return false;
356 } elseif ($alreadymember) {
357 return true;
358 } else {
359 $useradded = groups_db_add_member($member->groupid, $member->userid, $member->timeadded);
361 return $useradded;
365 /*****************************
366 Deletion functions
367 *****************************/
371 * Delete a group best effort, first removing members and links with courses and groupings.
372 * @param int $groupid The group to delete
373 * @return boolean True if deletion was successful, false otherwise
374 * See comment above on web service autoupdating.
376 function groups_delete_group($groupid) {
377 $groupdeleted = groups_db_delete_group($groupid);
379 return $groupdeleted;
384 * Deletes the link between the specified user and group.
385 * @param int $groupid The group to delete the user from
386 * @param int $userid The user to delete
387 * @return boolean True if deletion was successful, false otherwise
388 * See comment above on web service autoupdating.
390 function groups_remove_member($groupid, $userid) {
391 $success = groups_db_remove_member($groupid, $userid);
392 if ($success) {
393 $success = groups_db_set_group_modified($groupid);
395 return $success;
399 * Removes all users from the specified group.
400 * @param int $groupid The ID of the group.
401 * @return boolean True for success, false otherwise.
403 function groups_remove_all_members($groupid) {
404 if (! groups_group_exists($groupid)) {
405 //Woops, delete group last!
406 return false;
408 $userids = groups_get_members($groupid);
409 if (! $userids) {
410 return false;
412 $success = true;
413 foreach ($userids as $id) {
414 $success = $success && groups_db_remove_member($groupid, $id);
416 $success = $success && groups_db_set_group_modified($groupid);
417 return $success;