Merge branch 'MDL-66992-master' of https://github.com/tungthai/moodle
[moodle.git] / enrol / meta / classes / observer.php
blob446fca5c8f1034095ae82873cc88ed995e351f2b
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Event observer for meta enrolment plugin.
20 * @package enrol_meta
21 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->dirroot.'/enrol/meta/locallib.php');
29 /**
30 * Event observer for enrol_meta.
32 * @package enrol_meta
33 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class enrol_meta_observer extends enrol_meta_handler {
38 /**
39 * Triggered via user_enrolment_created event.
41 * @param \core\event\user_enrolment_created $event
42 * @return bool true on success.
44 public static function user_enrolment_created(\core\event\user_enrolment_created $event) {
45 if (!enrol_is_enabled('meta')) {
46 // No more enrolments for disabled plugins.
47 return true;
50 if ($event->other['enrol'] === 'meta') {
51 // Prevent circular dependencies - we can not sync meta enrolments recursively.
52 return true;
55 self::sync_course_instances($event->courseid, $event->relateduserid);
56 return true;
59 /**
60 * Triggered via user_enrolment_deleted event.
62 * @param \core\event\user_enrolment_deleted $event
63 * @return bool true on success.
65 public static function user_enrolment_deleted(\core\event\user_enrolment_deleted $event) {
66 if (!enrol_is_enabled('meta')) {
67 // This is slow, let enrol_meta_sync() deal with disabled plugin.
68 return true;
71 if ($event->other['enrol'] === 'meta') {
72 // Prevent circular dependencies - we can not sync meta enrolments recursively.
73 return true;
76 self::sync_course_instances($event->courseid, $event->relateduserid);
78 return true;
81 /**
82 * Triggered via user_enrolment_updated event.
84 * @param \core\event\user_enrolment_updated $event
85 * @return bool true on success
87 public static function user_enrolment_updated(\core\event\user_enrolment_updated $event) {
88 if (!enrol_is_enabled('meta')) {
89 // No modifications if plugin disabled.
90 return true;
93 if ($event->other['enrol'] === 'meta') {
94 // Prevent circular dependencies - we can not sync meta enrolments recursively.
95 return true;
98 self::sync_course_instances($event->courseid, $event->relateduserid);
100 return true;
104 * Triggered via role_assigned event.
106 * @param \core\event\role_assigned $event
107 * @return bool true on success.
109 public static function role_assigned(\core\event\role_assigned $event) {
110 if (!enrol_is_enabled('meta')) {
111 return true;
114 // Prevent circular dependencies - we can not sync meta roles recursively.
115 if ($event->other['component'] === 'enrol_meta') {
116 return true;
119 // Only course level roles are interesting.
120 if (!$parentcontext = context::instance_by_id($event->contextid, IGNORE_MISSING)) {
121 return true;
123 if ($parentcontext->contextlevel != CONTEXT_COURSE) {
124 return true;
127 self::sync_course_instances($parentcontext->instanceid, $event->relateduserid);
129 return true;
133 * Triggered via role_unassigned event.
135 * @param \core\event\role_unassigned $event
136 * @return bool true on success
138 public static function role_unassigned(\core\event\role_unassigned $event) {
139 if (!enrol_is_enabled('meta')) {
140 // All roles are removed via cron automatically.
141 return true;
144 // Prevent circular dependencies - we can not sync meta roles recursively.
145 if ($event->other['component'] === 'enrol_meta') {
146 return true;
149 // Only course level roles are interesting.
150 if (!$parentcontext = context::instance_by_id($event->contextid, IGNORE_MISSING)) {
151 return true;
153 if ($parentcontext->contextlevel != CONTEXT_COURSE) {
154 return true;
157 self::sync_course_instances($parentcontext->instanceid, $event->relateduserid);
159 return true;
163 * Triggered via course_deleted event.
165 * @param \core\event\course_deleted $event
166 * @return bool true on success
168 public static function course_deleted(\core\event\course_deleted $event) {
169 global $DB;
171 if (!enrol_is_enabled('meta')) {
172 // This is slow, let enrol_meta_sync() deal with disabled plugin.
173 return true;
176 // Does anything want to sync with this parent?
177 if (!$enrols = $DB->get_records('enrol', array('customint1' => $event->objectid, 'enrol' => 'meta'),
178 'courseid ASC, id ASC')) {
179 return true;
182 $plugin = enrol_get_plugin('meta');
183 $unenrolaction = $plugin->get_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
185 if ($unenrolaction == ENROL_EXT_REMOVED_UNENROL) {
186 // Simple, just delete this instance which purges all enrolments,
187 // admins were warned that this is risky setting!
188 foreach ($enrols as $enrol) {
189 $plugin->delete_instance($enrol);
191 return true;
194 foreach ($enrols as $enrol) {
195 if ($unenrolaction == ENROL_EXT_REMOVED_SUSPEND or $unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
196 // This makes all enrolments suspended very quickly.
197 $plugin->update_status($enrol, ENROL_INSTANCE_DISABLED);
199 if ($unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
200 $context = context_course::instance($enrol->courseid);
201 role_unassign_all(array('contextid'=>$context->id, 'component'=>'enrol_meta', 'itemid'=>$enrol->id));
205 return true;
209 * Triggered via enrol_instance_updated event.
211 * @param \core\event\enrol_instance_updated $event
212 * @return boolean
214 public static function enrol_instance_updated(\core\event\enrol_instance_updated $event) {
215 global $DB;
217 if (!enrol_is_enabled('meta')) {
218 // This is slow, let enrol_meta_sync() deal with disabled plugin.
219 return true;
222 // Does anything want to sync with this parent?
223 $affectedcourses = $DB->get_fieldset_sql('SELECT DISTINCT courseid FROM {enrol} '.
224 'WHERE customint1 = ? AND enrol = ?',
225 array($event->courseid, 'meta'));
227 foreach ($affectedcourses as $courseid) {
228 enrol_meta_sync($courseid);
231 return true;