MDL-28348 fix fatal error caused by unenrolling of incorrect enrol instance
[moodle.git] / enrol / meta / locallib.php
CommitLineData
df997f84
PS
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Local stuff for meta course enrolment plugin.
20 *
e2382027
PS
21 * @package enrol
22 * @subpackage meta
23 * @copyright 2010 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
df997f84
PS
25 */
26
e2382027 27defined('MOODLE_INTERNAL') || die();
df997f84
PS
28
29/**
30 * Event handler for meta enrolment plugin.
31 *
32 * We try to keep everything in sync via listening to events,
33 * it may fail sometimes, so we always do a full sync in cron too.
34 */
35class enrol_meta_handler {
36 public function role_assigned($ra) {
37 global $DB;
38
39 if (!enrol_is_enabled('meta')) {
40 return true;
41 }
42
43 // prevent circular dependencies - we can not sync meta roles recursively
44 if ($ra->component === 'enrol_meta') {
45 return true;
46 }
47
48 //only course level roles are interesting
49 $parentcontext = get_context_instance_by_id($ra->contextid);
50 if ($parentcontext->contextlevel != CONTEXT_COURSE) {
51 return true;
52 }
53
54 // does anything want to sync with this parent?
55 if (!$enrols = $DB->get_records('enrol', array('customint1'=>$parentcontext->instanceid, 'enrol'=>'meta'), 'id ASC')) {
56 return true;
57 }
58
59 // make sure the role sync is not prevented
60 $plugin = enrol_get_plugin('meta');
61 if ($disabled = $plugin->get_config('nosyncroleids')) {
62 if (in_array($ra->roleid, explode(',', $disabled))) {
76ce0f34 63 return true;
df997f84
PS
64 }
65 }
66
67 foreach ($enrols as $enrol) {
68 // Is the user enrolled? We want to sync only really enrolled users
69 if (!$DB->record_exists('user_enrolments', array('userid'=>$ra->userid, 'enrolid'=>$enrol->id))) {
70 continue;
71 }
72 $context = get_context_instance(CONTEXT_COURSE, $enrol->courseid);
73
74 // just try to assign role, no problem if role assignment already exists
75 role_assign($ra->roleid, $ra->userid, $context->id, 'enrol_meta', $enrol->id);
76 }
77
78 return true;
79 }
80
81 public function role_unassigned($ra) {
82 global $DB;
83
84 //note: do not test if plugin enabled, we want to keep removing previous roles
85
86 // prevent circular dependencies - we can not sync meta roles recursively
87 if ($ra->component === 'enrol_meta') {
88 return true;
89 }
90
91 // only course level roles are interesting
92 $parentcontext = get_context_instance_by_id($ra->contextid);
93 if ($parentcontext->contextlevel != CONTEXT_COURSE) {
94 return true;
95 }
96
97 // does anything want to sync with this parent?
98 if (!$enrols = $DB->get_records('enrol', array('customint1'=>$parentcontext->instanceid, 'enrol'=>'meta'), 'id ASC')) {
99 return true;
100 }
101
102 //note: do not check 'nosyncroleids', somebody might have just enabled it, we want to get rid of nosync roles gradually
103
104 foreach ($enrols as $enrol) {
105 // Is the user enrolled? We want to sync only really enrolled users
106 if (!$DB->record_exists('user_enrolments', array('userid'=>$ra->userid, 'enrolid'=>$enrol->id))) {
107 continue;
108 }
109 $context = get_context_instance(CONTEXT_COURSE, $enrol->courseid);
110
111 // now make sure the user does not have the role through some other enrol plugin
112 $params = array('contextid'=>$ra->contextid, 'roleid'=>$ra->roleid, 'userid'=>$ra->userid);
113 if ($DB->record_exists_select('role_assignments', "contextid = :contextid AND roleid = :roleid AND userid = :userid AND component <> 'enrol_meta'", $params)) {
114 continue;
115 }
116
117 // unassing role, there is no other role assignment in parent course
5b2904c8 118 role_unassign($ra->roleid, $ra->userid, $context->id, 'enrol_meta', $enrol->id);
df997f84
PS
119 }
120
121 return true;
122 }
123
124 public function user_enrolled($ue) {
125 global $DB;
126
127 if (!enrol_is_enabled('meta')) {
128 return true;
129 }
130
fc7cd35c
PS
131 if ($ue->enrol === 'meta') {
132 // prevent circular dependencies - we can not sync meta enrolments recursively
133 return true;
134 }
135
df997f84
PS
136 // does anything want to sync with this parent?
137 if (!$enrols = $DB->get_records('enrol', array('customint1'=>$ue->courseid, 'enrol'=>'meta'), 'id ASC')) {
138 return true;
139 }
140
141 $plugin = enrol_get_plugin('meta');
142 foreach ($enrols as $enrol) {
143 // no problem if already enrolled
144 $plugin->enrol_user($enrol, $ue->userid);
145 }
146
147 return true;
148 }
149
150 public function user_unenrolled($ue) {
151 global $DB;
152
153 //note: do not test if plugin enabled, we want to keep removing previously linked courses
154
155 // look for unenrolment candidates - it may be possible that user has multiple enrolments...
424d60d9 156 $sql = "SELECT e.*
df997f84
PS
157 FROM {enrol} e
158 JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = :userid)
159 JOIN {enrol} pe ON (pe.courseid = e.customint1 AND pe.enrol <> 'meta' AND pe.courseid = :courseid)
160 LEFT JOIN {user_enrolments} pue ON (pue.enrolid = pe.id AND pue.userid = ue.userid)
c2f53f01 161 WHERE pue.id IS NULL AND e.enrol = 'meta'";
df997f84
PS
162 $params = array('courseid'=>$ue->courseid, 'userid'=>$ue->userid);
163
424d60d9 164 $rs = $DB->get_recordset_sql($sql, $params);
df997f84
PS
165
166 $plugin = enrol_get_plugin('meta');
424d60d9 167 foreach ($rs as $enrol) {
df997f84
PS
168 $plugin->unenrol_user($enrol, $ue->userid);
169 }
424d60d9 170 $rs->close();
df997f84
PS
171
172 return true;
173 }
174
175 public function course_deleted($course) {
176 global $DB;
177
178 //note: do not test if plugin enabled, we want to keep removing previously linked courses
179
180 // does anything want to sync with this parent?
869483b6 181 if (!$enrols = $DB->get_records('enrol', array('customint1'=>$course->id, 'enrol'=>'meta'), 'id ASC')) {
df997f84
PS
182 return true;
183 }
184
185 $plugin = enrol_get_plugin('meta');
186 foreach ($enrols as $enrol) {
187 //unenrol all users
85fd37d3 188 $ues = $DB->get_recordset('user_enrolments', array('enrolid'=>$enrol->id));
df997f84
PS
189 foreach ($ues as $ue) {
190 $plugin->unenrol_user($enrol, $ue->userid);
191 }
192 $ues->close();
193 }
194
195 return true;
196 }
197}
198
199/**
200 * Sync all meta course links.
201 * @param int $courseid one course, empty mean all
202 * @return void
203 */
204function enrol_meta_sync($courseid = NULL) {
205 global $CFG, $DB;
206
207 // unfortunately this may take a loooong time
208 @set_time_limit(0); //if this fails during upgrade we can continue from cron, no big deal
209
210 $meta = enrol_get_plugin('meta');
211
212 $onecourse = $courseid ? "AND e.courseid = :courseid" : "";
213
214 // iterate through all not enrolled yet users
215 if (enrol_is_enabled('meta')) {
cf717dc2 216 list($enabled, $params) = $DB->get_in_or_equal(explode(',', $CFG->enrol_plugins_enabled), SQL_PARAMS_NAMED, 'e');
df997f84
PS
217 $onecourse = "";
218 if ($courseid) {
219 $params['courseid'] = $courseid;
220 $onecourse = "AND e.courseid = :courseid";
221 }
222 $sql = "SELECT pue.userid, e.id AS enrolid
223 FROM {user_enrolments} pue
224 JOIN {enrol} pe ON (pe.id = pue.enrolid AND pe.enrol <> 'meta' AND pe.enrol $enabled )
225 JOIN {enrol} e ON (e.customint1 = pe.courseid AND e.enrol = 'meta' AND e.status = :statusenabled $onecourse)
226 LEFT JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = pue.userid)
227 WHERE ue.id IS NULL";
228 $params['statusenabled'] = ENROL_INSTANCE_ENABLED;
229 $params['courseid'] = $courseid;
230
231 $rs = $DB->get_recordset_sql($sql, $params);
232 $instances = array(); //cache
233 foreach($rs as $ue) {
234 if (!isset($instances[$ue->enrolid])) {
235 $instances[$ue->enrolid] = $DB->get_record('enrol', array('id'=>$ue->enrolid));
236 }
237 $meta->enrol_user($instances[$ue->enrolid], $ue->userid);
238 }
239 $rs->close();
240 unset($instances);
241 }
242
243 // unenrol as necessary - ignore enabled flag, we want to get rid of all
244 $sql = "SELECT ue.userid, e.id AS enrolid
245 FROM {user_enrolments} ue
246 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'meta' $onecourse)
247 LEFT JOIN (SELECT xue.userid, xe.courseid
248 FROM {enrol} xe
249 JOIN {user_enrolments} xue ON (xue.enrolid = xe.id)
250 ) pue ON (pue.courseid = e.customint1 AND pue.userid = ue.userid)
251 WHERE pue.courseid IS NULL";
252 //TODO: this may use a bit of SQL optimisation
253 $rs = $DB->get_recordset_sql($sql, array('courseid'=>$courseid));
254 $instances = array(); //cache
255 foreach($rs as $ue) {
256 if (!isset($instances[$ue->enrolid])) {
257 $instances[$ue->enrolid] = $DB->get_record('enrol', array('id'=>$ue->enrolid));
258 }
259 $meta->unenrol_user($instances[$ue->enrolid], $ue->userid);
260 }
261 $rs->close();
262 unset($instances);
263
264 // now assign all necessary roles
265 if (enrol_is_enabled('meta')) {
266 $enabled = explode(',', $CFG->enrol_plugins_enabled);
267 foreach($enabled as $k=>$v) {
268 if ($v === 'meta') {
269 continue; // no meta sync of meta roles
270 }
271 $enabled[$k] = 'enrol_'.$v;
272 }
273 $enabled[] = $DB->sql_empty(); // manual assignments are replicated too
274
cf717dc2 275 list($enabled, $params) = $DB->get_in_or_equal($enabled, SQL_PARAMS_NAMED, 'e');
df997f84
PS
276 $sql = "SELECT DISTINCT pra.roleid, pra.userid, c.id AS contextid, e.id AS enrolid
277 FROM {role_assignments} pra
ba572e34 278 JOIN {user} u ON (u.id = pra.userid AND u.deleted = 0)
df997f84
PS
279 JOIN {context} pc ON (pc.id = pra.contextid AND pc.contextlevel = :coursecontext AND pra.component $enabled)
280 JOIN {enrol} e ON (e.customint1 = pc.instanceid AND e.enrol = 'meta' AND e.status = :statusenabled $onecourse)
281 JOIN {context} c ON (c.contextlevel = pc.contextlevel AND c.instanceid = e.courseid)
282 LEFT JOIN {role_assignments} ra ON (ra.contextid = c.id AND ra.userid = pra.userid AND ra.roleid = pra.itemid AND ra.itemid = e.id AND ra.component = 'enrol_meta')
283 WHERE ra.id IS NULL";
284 $params['statusenabled'] = ENROL_INSTANCE_ENABLED;
285 $params['coursecontext'] = CONTEXT_COURSE;
286 $params['courseid'] = $courseid;
287
288 if ($ignored = $meta->get_config('nosyncroleids')) {
cf717dc2 289 list($notignored, $xparams) = $DB->get_in_or_equal(explode(',', $ignored), SQL_PARAMS_NAMED, 'ig', false);
df997f84
PS
290 $params = array_merge($params, $xparams);
291 $sql = "$sql AND pra.roleid $notignored";
292 }
293
294 $rs = $DB->get_recordset_sql($sql, $params);
295 foreach($rs as $ra) {
296 role_assign($ra->roleid, $ra->userid, $ra->contextid, 'enrol_meta', $ra->enrolid);
297 }
298 $rs->close();
299 }
300
301 // remove unwanted roles - include ignored roles and disabled plugins too
302 $params = array('coursecontext' => CONTEXT_COURSE, 'courseid' => $courseid);
303 if ($ignored = $meta->get_config('nosyncroleids')) {
cf717dc2 304 list($notignored, $xparams) = $DB->get_in_or_equal(explode(',', $ignored), SQL_PARAMS_NAMED, 'ig', false);
df997f84
PS
305 $params = array_merge($params, $xparams);
306 $notignored = "AND pra.roleid $notignored";
307 } else {
308 $notignored = "";
309 }
310 $sql = "SELECT ra.roleid, ra.userid, ra.contextid, ra.itemid
311 FROM {role_assignments} ra
312 JOIN {enrol} e ON (e.id = ra.itemid AND ra.component = 'enrol_meta' AND e.enrol = 'meta' $onecourse)
313 JOIN {context} pc ON (pc.instanceid = e.customint1 AND pc.contextlevel = :coursecontext)
314 LEFT JOIN {role_assignments} pra ON (pra.contextid = pc.id AND pra.userid = ra.userid AND pra.roleid = ra.roleid AND pra.component <> 'enrol_meta' $notignored)
315 WHERE pra.id IS NULL";
316
317 $rs = $DB->get_recordset_sql($sql, $params);
318 foreach($rs as $ra) {
319 role_unassign($ra->roleid, $ra->userid, $ra->contextid, 'enrol_meta', $ra->itemid);
320 }
321 $rs->close();
322
323}