Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Event observer for meta enrolment plugin.
  19   *
  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
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once($CFG->dirroot.'/enrol/meta/locallib.php');
  28  
  29  /**
  30   * Event observer for enrol_meta.
  31   *
  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
  35   */
  36  class enrol_meta_observer extends enrol_meta_handler {
  37  
  38      /**
  39       * Triggered via user_enrolment_created event.
  40       *
  41       * @param \core\event\user_enrolment_created $event
  42       * @return bool true on success.
  43       */
  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;
  48          }
  49  
  50          if ($event->other['enrol'] === 'meta') {
  51              // Prevent circular dependencies - we can not sync meta enrolments recursively.
  52              return true;
  53          }
  54  
  55          self::sync_course_instances($event->courseid, $event->relateduserid);
  56          return true;
  57      }
  58  
  59      /**
  60       * Triggered via user_enrolment_deleted event.
  61       *
  62       * @param \core\event\user_enrolment_deleted $event
  63       * @return bool true on success.
  64       */
  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;
  69          }
  70  
  71          if ($event->other['enrol'] === 'meta') {
  72              // Prevent circular dependencies - we can not sync meta enrolments recursively.
  73              return true;
  74          }
  75  
  76          self::sync_course_instances($event->courseid, $event->relateduserid);
  77  
  78          return true;
  79      }
  80  
  81      /**
  82       * Triggered via user_enrolment_updated event.
  83       *
  84       * @param \core\event\user_enrolment_updated $event
  85       * @return bool true on success
  86       */
  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;
  91          }
  92  
  93          if ($event->other['enrol'] === 'meta') {
  94              // Prevent circular dependencies - we can not sync meta enrolments recursively.
  95              return true;
  96          }
  97  
  98          self::sync_course_instances($event->courseid, $event->relateduserid);
  99  
 100          return true;
 101      }
 102  
 103      /**
 104       * Triggered via role_assigned event.
 105       *
 106       * @param \core\event\role_assigned $event
 107       * @return bool true on success.
 108       */
 109      public static function role_assigned(\core\event\role_assigned $event) {
 110          if (!enrol_is_enabled('meta')) {
 111              return true;
 112          }
 113  
 114          // Prevent circular dependencies - we can not sync meta roles recursively.
 115          if ($event->other['component'] === 'enrol_meta') {
 116              return true;
 117          }
 118  
 119          // Only course level roles are interesting.
 120          if (!$parentcontext = context::instance_by_id($event->contextid, IGNORE_MISSING)) {
 121              return true;
 122          }
 123          if ($parentcontext->contextlevel != CONTEXT_COURSE) {
 124              return true;
 125          }
 126  
 127          self::sync_course_instances($parentcontext->instanceid, $event->relateduserid);
 128  
 129          return true;
 130      }
 131  
 132      /**
 133       * Triggered via role_unassigned event.
 134       *
 135       * @param \core\event\role_unassigned $event
 136       * @return bool true on success
 137       */
 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;
 142          }
 143  
 144          // Prevent circular dependencies - we can not sync meta roles recursively.
 145          if ($event->other['component'] === 'enrol_meta') {
 146              return true;
 147          }
 148  
 149          // Only course level roles are interesting.
 150          if (!$parentcontext = context::instance_by_id($event->contextid, IGNORE_MISSING)) {
 151              return true;
 152          }
 153          if ($parentcontext->contextlevel != CONTEXT_COURSE) {
 154              return true;
 155          }
 156  
 157          self::sync_course_instances($parentcontext->instanceid, $event->relateduserid);
 158  
 159          return true;
 160      }
 161  
 162      /**
 163       * Triggered via course_deleted event.
 164       *
 165       * @param \core\event\course_deleted $event
 166       * @return bool true on success
 167       */
 168      public static function course_deleted(\core\event\course_deleted $event) {
 169          global $DB;
 170  
 171          if (!enrol_is_enabled('meta')) {
 172              // This is slow, let enrol_meta_sync() deal with disabled plugin.
 173              return true;
 174          }
 175  
 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;
 180          }
 181  
 182          $plugin = enrol_get_plugin('meta');
 183          $unenrolaction = $plugin->get_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
 184  
 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);
 190              }
 191              return true;
 192          }
 193  
 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);
 198              }
 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));
 202              }
 203          }
 204  
 205          return true;
 206      }
 207  
 208      /**
 209       * Triggered via enrol_instance_updated event.
 210       *
 211       * @param \core\event\enrol_instance_updated $event
 212       * @return boolean
 213       */
 214      public static function enrol_instance_updated(\core\event\enrol_instance_updated $event) {
 215          global $DB;
 216  
 217          if (!enrol_is_enabled('meta')) {
 218              // This is slow, let enrol_meta_sync() deal with disabled plugin.
 219              return true;
 220          }
 221  
 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'));
 226  
 227          foreach ($affectedcourses as $courseid) {
 228              enrol_meta_sync($courseid);
 229          }
 230  
 231          return true;
 232      }
 233  }