2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Event observer for meta enrolment plugin.
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');
30 * Event observer for 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
{
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.
50 if ($event->other
['enrol'] === 'meta') {
51 // Prevent circular dependencies - we can not sync meta enrolments recursively.
55 self
::sync_course_instances($event->courseid
, $event->relateduserid
);
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.
71 if ($event->other
['enrol'] === 'meta') {
72 // Prevent circular dependencies - we can not sync meta enrolments recursively.
76 self
::sync_course_instances($event->courseid
, $event->relateduserid
);
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.
93 if ($event->other
['enrol'] === 'meta') {
94 // Prevent circular dependencies - we can not sync meta enrolments recursively.
98 self
::sync_course_instances($event->courseid
, $event->relateduserid
);
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')) {
114 // Prevent circular dependencies - we can not sync meta roles recursively.
115 if ($event->other
['component'] === 'enrol_meta') {
119 // Only course level roles are interesting.
120 if (!$parentcontext = context
::instance_by_id($event->contextid
, IGNORE_MISSING
)) {
123 if ($parentcontext->contextlevel
!= CONTEXT_COURSE
) {
127 self
::sync_course_instances($parentcontext->instanceid
, $event->relateduserid
);
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.
144 // Prevent circular dependencies - we can not sync meta roles recursively.
145 if ($event->other
['component'] === 'enrol_meta') {
149 // Only course level roles are interesting.
150 if (!$parentcontext = context
::instance_by_id($event->contextid
, IGNORE_MISSING
)) {
153 if ($parentcontext->contextlevel
!= CONTEXT_COURSE
) {
157 self
::sync_course_instances($parentcontext->instanceid
, $event->relateduserid
);
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) {
171 if (!enrol_is_enabled('meta')) {
172 // This is slow, let enrol_meta_sync() deal with disabled plugin.
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')) {
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);
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
));
209 * Triggered via enrol_instance_updated event.
211 * @param \core\event\enrol_instance_updated $event
214 public static function enrol_instance_updated(\core\event\enrol_instance_updated
$event) {
217 if (!enrol_is_enabled('meta')) {
218 // This is slow, let enrol_meta_sync() deal with disabled plugin.
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);