MDL-10873 Now deleting the preference from user_preferences if the value submitted...
[moodle-pu.git] / group / lib.php
blobeb0234472d6abea60f362a850c660b112dee3e95
1 <?php
2 /**
3 * Extra library for groups and groupings.
5 * @copyright &copy; 2006 The Open University
6 * @author J.White AT open.ac.uk
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package groups
9 */
12 * INTERNAL FUNCTIONS - to be used by moodle core only
13 * require_once $CFG->dirroot.'/group/lib.php' must be used
16 /**
17 * Add a new group
18 * @param object $data group properties (with magic quotes);
19 * @param object $um upload manager with group picture
20 * @return id of group or false if error
22 function groups_create_group($data, $um=false) {
23 global $CFG;
24 require_once("$CFG->libdir/gdlib.php");
26 $data->timecreated = time();
27 $data->timemodified = $data->timecreated;
28 $id = insert_record('groups', $data);
30 if ($id and $um) {
31 //update image
32 if (save_profile_image($id, $um, 'groups')) {
33 set_field('groups', 'picture', 1, 'id', $id);
37 return $id;
40 /**
41 * Add a new group
42 * @param object $data group properties (with magic quotes);
43 * @param object $um upload manager with group picture
44 * @return boolean success
46 function groups_update_group($data, $um=false) {
47 global $CFG;
48 require_once("$CFG->libdir/gdlib.php");
50 $data->timemodified = time();
51 $result = update_record('groups', $data);
53 if ($result and $um) {
54 //update image
55 if (save_profile_image($data->id, $um, 'groups')) {
56 set_field('groups', 'picture', 1, 'id', $data->id);
60 return $result;
63 /**
64 * Delete a group best effort, first removing members and links with courses and groupings.
65 * Removes group avatar too.
66 * @param int $groupid The group to delete
67 * @return boolean True if deletion was successful, false otherwise
69 function groups_delete_group($groupid) {
70 global $CFG;
71 require_once($CFG->libdir.'/gdlib.php');
73 if (empty($groupid)) {
74 return false;
77 //first delete usage in groupings_groups
78 delete_records('groupings_groups', 'groupid', $groupid);
79 //delete members
80 delete_records('groups_members', 'groupid', $groupid);
81 //then imge
82 delete_profile_image($groupid, 'groups');
83 //group itself last
84 return delete_records('groups', 'id', $groupid);
87 function groups_delete_grouping($groupingid) {
88 if (empty($groupingid)) {
89 return false;
93 //first delete usage in groupings_groups
94 delete_records('groupings_groups', 'groupingid', $groupingid);
95 // remove the default groupingid from course
96 set_field('course', 'defaultgroupingid', 0, 'defaultgroupingid', $groupingid);
97 // remove the groupingid from all course modules
98 set_field('course_modules', 'groupingid', 0, 'groupingid', $groupingid);
99 //group itself last
100 return delete_records('groupings', 'id', $groupingid);
103 function groups_delete_group_members($courseid, $showfeedback=false) {
104 global $CFG;
106 $sql = "DELETE FROM {$CFG->prefix}groups_members
107 WHERE groupid in (SELECT id FROM {$CFG->prefix}groups g WHERE g.courseid = $courseid)";
109 execute_sql($sql, false);
110 if ($showfeedback) {
111 notify(get_string('deleted').' groups_members');
114 return true;
117 function groups_delete_groups($courseid, $showfeedback=false) {
118 global $CFG;
119 require_once($CFG->libdir.'/gdlib.php');
121 // delete any uses of groups
122 $sql = "DELETE FROM {$CFG->prefix}groupings_groups
123 WHERE groupid in (SELECT id FROM {$CFG->prefix}groups g WHERE g.courseid = $courseid)";
124 execute_sql($sql, false);
126 groups_delete_group_members($courseid, false);
128 // delete group pictures
129 if ($groups = get_records('groups', 'courseid', $courseid)) {
130 foreach($groups as $group) {
131 delete_profile_image($group->id, 'groups');
135 delete_records('groups', 'courseid', $courseid);
136 if ($showfeedback) {
137 notify(get_string('deleted').' groups');
140 return true;
143 function groups_delete_groupings($courseid, $showfeedback=false) {
144 global $CFG;
146 // delete any uses of groupings
147 $sql = "DELETE FROM {$CFG->prefix}groupings_groups
148 WHERE groupingid in (SELECT id FROM {$CFG->prefix}groupings g WHERE g.courseid = $courseid)";
149 execute_sql($sql, false);
151 // remove the default groupingid from course
152 set_field('course', 'defaultgroupingid', 0, 'id', $courseid);
153 // remove the groupingid from all course modules
154 set_field('course_modules', 'groupingid', 0, 'courseid', $courseid);
156 delete_records('groupings', 'courseid', $courseid);
157 if ($showfeedback) {
158 notify(get_string('deleted').' groupings');
161 return true;
164 /* =================================== */
165 /* various functions used by groups UI */
166 /* =================================== */
169 * Gets the users for a course who are not in a specified group
170 * @param int $groupid The id of the group
171 * @param string searchtext similar to searchtext in role assign, search
172 * @return array An array of the userids of the non-group members, or false if
173 * an error occurred.
174 * This function was changed to get_users_by_capability style
175 * mostly because of the searchtext requirement
177 function groups_get_users_not_in_group($courseid, $groupid, $searchtext='') {
179 global $CFG;
181 $context = get_context_instance(CONTEXT_COURSE, $courseid);
183 if ($searchtext !== '') { // Search for a subset of remaining users
184 $LIKE = sql_ilike();
185 $FULLNAME = sql_fullname();
186 $wheresearch = " AND u.id IN (SELECT id FROM {$CFG->prefix}user WHERE $FULLNAME $LIKE '%$searchtext%' OR email $LIKE '%$searchtext%' )";
187 } else {
188 $wheresearch = '';
191 $capability = 'moodle/course:view';
192 $doanything = false;
194 // find all possible "student" roles
195 if ($possibleroles = get_roles_with_capability($capability, CAP_ALLOW, $context)) {
196 if (!$doanything) {
197 if (!$sitecontext = get_context_instance(CONTEXT_SYSTEM)) {
198 return false; // Something is seriously wrong
200 $doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext);
203 $validroleids = array();
204 foreach ($possibleroles as $possiblerole) {
205 if (!$doanything) {
206 if (isset($doanythingroles[$possiblerole->id])) { // We don't want these included
207 continue;
210 if ($caps = role_context_capabilities($possiblerole->id, $context, $capability)) { // resolved list
211 if (isset($caps[$capability]) && $caps[$capability] > 0) { // resolved capability > 0
212 $validroleids[] = $possiblerole->id;
216 if (empty($validroleids)) {
217 return false;
219 $roleids = '('.implode(',', $validroleids).')';
220 } else {
221 return false; // No need to continue, since no roles have this capability set
224 /// Construct the main SQL
225 $select = " SELECT u.id, u.firstname, u.lastname";
226 $from = " FROM {$CFG->prefix}user u
227 INNER JOIN {$CFG->prefix}role_assignments ra ON ra.userid = u.id
228 INNER JOIN {$CFG->prefix}role r ON r.id = ra.roleid";
229 $where = " WHERE ra.contextid ".get_related_contexts_string($context)."
230 AND u.deleted = 0
231 AND ra.roleid in $roleids
232 AND u.id NOT IN (SELECT userid
233 FROM {$CFG->prefix}groups_members
234 WHERE groupid = $groupid)
235 $wheresearch";
237 return get_records_sql($select.$from.$where);;