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