Commit | Line | Data |
---|---|---|
f3f7610c ML |
1 | <?php |
2 | /** | |
ddff2fa8 | 3 | * Extra library for groups and groupings. |
f3f7610c ML |
4 | * |
5 | * @copyright © 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 | */ | |
10 | ||
ddff2fa8 | 11 | /* |
12 | * INTERNAL FUNCTIONS - to be used by moodle core only | |
13 | * require_once $CFG->dirroot.'/group/lib.php' must be used | |
14 | */ | |
15 | ||
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 | |
21 | */ | |
22 | function groups_create_group($data, $um=false) { | |
23 | global $CFG; | |
24 | require_once("$CFG->libdir/gdlib.php"); | |
25 | ||
26 | $data->timecreated = time(); | |
27 | $data->timemodified = $data->timecreated; | |
28 | $id = insert_record('groups', $data); | |
29 | ||
30 | if ($id and $um) { | |
31 | //update image | |
32 | if (save_profile_image($id, $um, 'groups')) { | |
33 | set_field('groups', 'picture', 1, 'id', $id); | |
34 | } | |
35 | } | |
36 | ||
37 | return $id; | |
38 | } | |
39 | ||
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 | |
45 | */ | |
46 | function groups_update_group($data, $um=false) { | |
47 | global $CFG; | |
48 | require_once("$CFG->libdir/gdlib.php"); | |
49 | ||
50 | $data->timemodified = time(); | |
51 | $result = update_record('groups', $data); | |
52 | ||
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); | |
57 | } | |
58 | } | |
59 | ||
60 | return $result; | |
61 | } | |
62 | ||
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 | |
68 | */ | |
69 | function groups_delete_group($groupid) { | |
70 | global $CFG; | |
71 | require_once($CFG->libdir.'/gdlib.php'); | |
72 | ||
73 | if (empty($groupid)) { | |
74 | return false; | |
75 | } | |
76 | ||
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); | |
85 | } | |
86 | ||
87 | function groups_delete_grouping($groupingid) { | |
88 | if (empty($groupingid)) { | |
89 | return false; | |
90 | ||
91 | } | |
92 | ||
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); | |
101 | } | |
102 | ||
103 | function groups_delete_group_members($courseid, $showfeedback=false) { | |
104 | global $CFG; | |
105 | ||
106 | $sql = "DELETE FROM {$CFG->prefix}groups_members | |
107 | WHERE groupid in (SELECT id FROM {$CFG->prefix}groups g WHERE g.courseid = $courseid)"; | |
108 | ||
109 | execute_sql($sql, false); | |
110 | if ($showfeedback) { | |
111 | notify(get_string('deleted').' groups_members'); | |
112 | } | |
113 | ||
114 | return true; | |
115 | } | |
116 | ||
117 | function groups_delete_groups($courseid, $showfeedback=false) { | |
118 | global $CFG; | |
119 | require_once($CFG->libdir.'/gdlib.php'); | |
120 | ||
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); | |
125 | ||
126 | groups_delete_group_members($courseid, false); | |
127 | ||
128 | // delete group pictures | |
129 | if ($groups = get_records('groups', 'courseid', $courseid)) { | |
130 | foreach($groups as $group) { | |
131 | delete_profile_image($group->id, 'groups'); | |
132 | } | |
133 | } | |
134 | ||
135 | delete_records('groups', 'courseid', $courseid); | |
136 | if ($showfeedback) { | |
137 | notify(get_string('deleted').' groups'); | |
138 | } | |
139 | ||
140 | return true; | |
141 | } | |
142 | ||
143 | function groups_delete_groupings($courseid, $showfeedback=false) { | |
144 | global $CFG; | |
145 | ||
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); | |
150 | ||
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); | |
155 | ||
156 | delete_records('groupings', 'courseid', $courseid); | |
157 | if ($showfeedback) { | |
158 | notify(get_string('deleted').' groupings'); | |
159 | } | |
160 | ||
161 | return true; | |
162 | } | |
163 | ||
164 | /* =================================== */ | |
165 | /* various functions used by groups UI */ | |
166 | /* =================================== */ | |
167 | ||
168 | /** | |
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 | |
176 | */ | |
177 | function groups_get_users_not_in_group($courseid, $groupid, $searchtext='') { | |
178 | ||
179 | global $CFG; | |
180 | ||
181 | $context = get_context_instance(CONTEXT_COURSE, $courseid); | |
182 | ||
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 = ''; | |
189 | } | |
190 | ||
191 | $capability = 'moodle/course:view'; | |
192 | $doanything = false; | |
f3f7610c | 193 | |
ddff2fa8 | 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 | |
199 | } | |
200 | $doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext); | |
201 | } | |
f3f7610c | 202 | |
ddff2fa8 | 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; | |
208 | } | |
209 | } | |
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; | |
213 | } | |
214 | } | |
215 | } | |
216 | if (empty($validroleids)) { | |
217 | return false; | |
218 | } | |
219 | $roleids = '('.implode(',', $validroleids).')'; | |
220 | } else { | |
221 | return false; // No need to continue, since no roles have this capability set | |
222 | } | |
f3f7610c | 223 | |
ddff2fa8 | 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"; | |
f3f7610c | 236 | |
ddff2fa8 | 237 | return get_records_sql($select.$from.$where);; |
238 | } | |
f3f7610c | 239 | |
429e531e | 240 | ?> |