Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is 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  namespace tool_brickfield;
  18  
  19  use core\event\base;
  20  use stdClass;
  21  
  22  /**
  23   * Observer class containing methods monitoring various events.
  24   *
  25   * @package    tool_brickfield
  26   * @copyright  2020 Brickfield Education Labs https://www.brickfield.ie
  27   * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   *
  29   */
  30  class eventobservers {
  31  
  32      /**
  33       * For course events to be handled, the accessibility tool needs to be enabled, and if a courseid is specified, the course
  34       * has to have been added to the scheduler.
  35       * @param int $courseid
  36       * @return bool
  37       * @throws \dml_exception
  38       */
  39      private static function course_event_should_be_handled(int $courseid): bool {
  40          return accessibility::is_accessibility_enabled() && analysis::is_enabled() &&
  41              (empty($courseid) || (isset($courseid) && scheduler::is_course_in_schedule($courseid)));
  42      }
  43  
  44      /**
  45       * Content area altered event observer.
  46       * @param base $event The area altered event.
  47       * @throws \ReflectionException
  48       * @throws \dml_exception
  49       */
  50      private static function area_altered(base $event) {
  51          // Handle if this feature is enabled and this course is in the schedule.
  52          if (static::course_event_should_be_handled($event->courseid)) {
  53              manager::find_new_or_updated_areas($event);
  54          }
  55      }
  56  
  57      /**
  58       * Course event observers.
  59       * These observer monitors course created / restored / updated events,
  60       * then its HTML content is processed with accessibility checking.
  61       */
  62  
  63      /**
  64       * Course created event observer.
  65       * @param \core\event\course_created $event The course created event.
  66       * @throws \ReflectionException
  67       * @throws \dml_exception
  68       */
  69      public static function course_created(\core\event\course_created $event) {
  70          // Handle if this feature is enabled and this course is in the schedule.
  71          if (static::course_event_should_be_handled($event->courseid)) {
  72              // Need to trigger rerun check for automatically created forum...
  73              $data = new stdClass();
  74              $data->courseid = $event->courseid;
  75              $data->item = 'coursererun';
  76              static::observer_insert($data);
  77              static::course_altered($event);
  78          }
  79      }
  80  
  81      /**
  82       * Course deleted event observer.
  83       * @param \core\event\course_deleted $event The course deleted event.
  84       * @throws \dml_exception
  85       */
  86      public static function course_deleted(\core\event\course_deleted $event) {
  87          // Handle if this feature is enabled and this course is in the schedule.
  88          if (static::course_event_should_be_handled($event->courseid)) {
  89              $data = new stdClass();
  90              $data->courseid = $event->courseid;
  91              $data->contextid = -1;
  92              $data->item = 'core_course';
  93              static::observer_insert($data);
  94          }
  95      }
  96  
  97      /**
  98       * Course restored event observer.
  99       * @param \core\event\course_restored $event The course restored event.
 100       * @throws \ReflectionException
 101       * @throws \dml_exception
 102       */
 103      public static function course_restored(\core\event\course_restored $event) {
 104          // Handle if this feature is enabled and this course is in the schedule.
 105          if (static::course_event_should_be_handled($event->courseid)) {
 106              $data = new stdClass();
 107              $data->courseid = $event->courseid;
 108              $data->item = 'coursererun';
 109              static::observer_insert($data);
 110              static::course_altered($event);
 111          }
 112      }
 113  
 114      /**
 115       * Course update event observer.
 116       * @param \core\event\course_updated $event The course updated event.
 117       * @throws \ReflectionException
 118       * @throws \dml_exception
 119       */
 120      public static function course_updated(\core\event\course_updated $event) {
 121          // Handle if this feature is enabled and this course is in the schedule.
 122          if (static::course_event_should_be_handled($event->courseid)) {
 123              static::course_altered($event);
 124          }
 125      }
 126  
 127      /**
 128       * Course update event observer. This is called on both course_created and course_updated, so use the base class as a type hint.
 129       * @param base $event The course updated event.
 130       * @throws \ReflectionException
 131       * @throws \dml_exception
 132       */
 133      public static function course_altered(base $event) {
 134          // Handle if this feature is enabled and this course is in the schedule.
 135          if (static::course_event_should_be_handled($event->courseid)) {
 136              // Need to rerun caching of allowed courseid, from category white/blacklist.
 137              $data = new stdClass();
 138              $data->courseid = $data->contextid = $event->courseid;
 139              $data->contextid = -1;
 140              $data->item = 'category';
 141              static::observer_insert($data);
 142              static::area_altered($event);
 143          }
 144      }
 145  
 146      /**
 147       * Course section created event observer.
 148       * @param \core\event\course_section_created $event The course section created event.
 149       * @throws \ReflectionException
 150       * @throws \dml_exception
 151       */
 152      public static function course_section_created(\core\event\course_section_created $event) {
 153          // Handle if this feature is enabled and this course is in the schedule.
 154          if (static::course_event_should_be_handled($event->courseid)) {
 155              static::area_altered($event);
 156          }
 157      }
 158  
 159      /**
 160       * Course section deleted event observer.
 161       * @param \core\event\course_section_deleted $event The course section deleted event.
 162       * @throws \dml_exception
 163       */
 164      public static function course_section_deleted(\core\event\course_section_deleted $event) {
 165          // Handle if this feature is enabled and this course is in the schedule.
 166          if (static::course_event_should_be_handled($event->courseid)) {
 167              $data = new stdClass();
 168              $data->courseid = $event->courseid;
 169              $data->contextid = -1;
 170              $data->innercontextid = $event->objectid;
 171              $data->item = 'course_sections';
 172              static::observer_insert($data);
 173          }
 174      }
 175  
 176      /**
 177       * Course section update event observer.
 178       * @param \core\event\course_section_updated $event The course section updated event.
 179       * @throws \ReflectionException
 180       * @throws \dml_exception
 181       */
 182      public static function course_section_updated(\core\event\course_section_updated $event) {
 183          // Handle if this feature is enabled and this course is in the schedule.
 184          if (static::course_event_should_be_handled($event->courseid)) {
 185              static::area_altered($event);
 186          }
 187      }
 188  
 189      /**
 190       * Course module event observers.
 191       * These observer monitors course module created / restored / updated events,
 192       * then its HTML content is processed with accessibility checking.
 193       */
 194  
 195      /**
 196       * Course module created event observer.
 197       * @param \core\event\course_module_created $event The course module created event.
 198       * @throws \ReflectionException
 199       * @throws \dml_exception
 200       */
 201      public static function course_module_created(\core\event\course_module_created $event) {
 202          // Handle if this feature is enabled and this course is in the schedule.
 203          if (static::course_event_should_be_handled($event->courseid)) {
 204              static::area_altered($event);
 205          }
 206      }
 207  
 208      /**
 209       * Course module deleted event observer.
 210       * @param \core\event\course_module_deleted $event The course module deleted event.
 211       * @throws \dml_exception
 212       */
 213      public static function course_module_deleted(\core\event\course_module_deleted $event) {
 214          // Handle if this feature is enabled and this course is in the schedule.
 215          if (static::course_event_should_be_handled($event->courseid)) {
 216              $data = new stdClass();
 217              $data->courseid = $event->courseid;
 218              $data->contextid = -1;
 219              $data->innercontextid = $event->other['instanceid'];
 220              $data->item = 'mod_' . $event->other['modulename'];
 221              static::observer_insert($data);
 222          }
 223      }
 224  
 225      /**
 226       * Course module restored event observer.
 227       * @param \core\event\course_module_restored $event The course module restored event.
 228       * @throws \ReflectionException
 229       * @throws \dml_exception
 230       */
 231      public static function course_module_restored(\core\event\course_module_restored $event) {
 232          // Handle if this feature is enabled and this course is in the schedule.
 233          if (static::course_event_should_be_handled($event->courseid)) {
 234              static::area_altered($event);
 235          }
 236      }
 237  
 238      /**
 239       * Course module updated event observer.
 240       * @param \core\event\course_module_updated $event The course module updated event.
 241       * @throws \ReflectionException
 242       * @throws \dml_exception
 243       */
 244      public static function course_module_updated(\core\event\course_module_updated $event) {
 245          // Handle if this feature is enabled and this course is in the schedule.
 246          if (static::course_event_should_be_handled($event->courseid)) {
 247              static::area_altered($event);
 248          }
 249      }
 250  
 251      /**
 252       * course_category created event observer.
 253       * @param \core\event\course_category_created $event The course_category created event.
 254       * @throws \ReflectionException
 255       * @throws \dml_exception
 256       */
 257      public static function course_category_created(\core\event\course_category_created $event) {
 258          // Handle if this feature is enabled and this course is in the schedule.
 259          if (static::course_event_should_be_handled($event->courseid)) {
 260              static::area_altered($event);
 261          }
 262      }
 263  
 264      /**
 265       * course_category deleted event observer.
 266       * @param \core\event\course_category_deleted $event The course_category deleted event.
 267       * @throws \dml_exception
 268       */
 269      public static function course_category_deleted(\core\event\course_category_deleted $event) {
 270          // Handle if this feature is enabled and this course is in the schedule.
 271          if (static::course_event_should_be_handled($event->courseid)) {
 272              $data = new stdClass();
 273              $data->contextid = $data->courseid = -1;
 274              $data->innercontextid = $event->objectid;
 275              $data->item = 'course_categories';
 276              static::observer_insert($data);
 277          }
 278      }
 279  
 280      /**
 281       * course_category update event observer.
 282       * @param \core\event\course_category_updated $event The course_category updated event.
 283       * @throws \ReflectionException
 284       * @throws \dml_exception
 285       */
 286      public static function course_category_updated(\core\event\course_category_updated $event) {
 287          // Handle if this feature is enabled and this course is in the schedule.
 288          if (static::course_event_should_be_handled($event->courseid)) {
 289              static::area_altered($event);
 290          }
 291      }
 292  
 293      /**
 294       * mod_lesson_page created event observer.
 295       * @param \mod_lesson\event\page_created $event The mod_lesson page created event.
 296       * @throws \ReflectionException
 297       * @throws \dml_exception
 298       */
 299      public static function mod_lesson_page_created(\mod_lesson\event\page_created $event) {
 300          // Handle if this feature is enabled and this course is in the schedule.
 301          if (static::course_event_should_be_handled($event->courseid)) {
 302              static::area_altered($event);
 303          }
 304      }
 305  
 306      /**
 307       * mod_lesson_page deleted event observer.
 308       * @param \mod_lesson\event\page_deleted $event The mod_lesson page deleted event.
 309       * @throws \dml_exception
 310       */
 311      public static function mod_lesson_page_deleted(\mod_lesson\event\page_deleted $event) {
 312          // Handle if this feature is enabled and this course is in the schedule.
 313          if (static::course_event_should_be_handled($event->courseid)) {
 314              $data = new stdClass();
 315              $data->courseid = $event->courseid;
 316              $data->contextid = -1;
 317              $data->innercontextid = $event->objectid;
 318              $data->item = 'lesson_pages';
 319              static::observer_insert($data);
 320          }
 321      }
 322  
 323      /**
 324       * mod_lesson_page updated event observer.
 325       * @param \mod_lesson\event\page_updated $event The mod_lesson page updated event.
 326       * @throws \ReflectionException
 327       * @throws \dml_exception
 328       */
 329      public static function mod_lesson_page_updated(\mod_lesson\event\page_updated $event) {
 330          // Handle if this feature is enabled and this course is in the schedule.
 331          if (static::course_event_should_be_handled($event->courseid)) {
 332              static::area_altered($event);
 333          }
 334      }
 335  
 336      /**
 337       * core_question created observer
 338       * @param \core\event\question_created $event The core_question created event.
 339       * @throws \ReflectionException
 340       * @throws \dml_exception
 341       */
 342      public static function core_question_created(\core\event\question_created $event) {
 343          // Handle if this feature is enabled and this course is in the schedule.
 344          if (static::course_event_should_be_handled($event->courseid)) {
 345              manager::find_new_or_updated_areas($event);
 346          }
 347      }
 348  
 349      /**
 350       * core_question updated observer
 351       * @param \core\event\question_updated $event The core_question created event.
 352       * @throws \ReflectionException
 353       * @throws \dml_exception
 354       */
 355      public static function core_question_updated(\core\event\question_updated $event) {
 356          // Handle if this feature is enabled and this course is in the schedule.
 357          if (static::course_event_should_be_handled($event->courseid)) {
 358              manager::find_new_or_updated_areas($event);
 359          }
 360      }
 361  
 362      /**
 363       * core_question deleted observer
 364       * @param \core\event\question_deleted $event The core_question deleted event.
 365       * @throws \dml_exception
 366       */
 367      public static function core_question_deleted(\core\event\question_deleted $event) {
 368          // Handle if this feature is enabled and this course is in the schedule.
 369          if (static::course_event_should_be_handled($event->courseid)) {
 370              $data = new stdClass();
 371              $data->courseid = $event->courseid;
 372              $data->contextid = -1;
 373              $data->innercontextid = $event->objectid;
 374              $data->item = $event->objecttable;
 375              static::observer_insert($data);
 376          }
 377      }
 378  
 379      /**
 380       * Book chapter created event observer.
 381       * @param \mod_book\event\chapter_created $event The book chapter created event.
 382       * @throws \ReflectionException
 383       * @throws \dml_exception
 384       */
 385      public static function book_chapter_created(\mod_book\event\chapter_created $event) {
 386          // If this feature has been disabled, do nothing.
 387          if (accessibility::is_accessibility_enabled()) {
 388              static::area_altered($event);
 389          }
 390      }
 391  
 392      /**
 393       * Book chapter deleted event observer.
 394       * @param \mod_book\event\chapter_deleted $event The book chapter deleted event.
 395       * @throws \dml_exception
 396       */
 397      public static function book_chapter_deleted(\mod_book\event\chapter_deleted $event) {
 398          // If this feature has been disabled, do nothing.
 399          if (accessibility::is_accessibility_enabled()) {
 400              $data = new stdClass();
 401              $data->courseid = $event->courseid;
 402              $data->contextid = -1;
 403              $data->innercontextid = $event->objectid;
 404              $data->item = 'book_chapters';
 405              static::observer_insert($data);
 406          }
 407      }
 408  
 409      /**
 410       * Book chapter update event observer.
 411       * @param \mod_book\event\chapter_updated $event The book chapter updated event.
 412       * @throws \ReflectionException
 413       * @throws \dml_exception
 414       */
 415      public static function book_chapter_updated(\mod_book\event\chapter_updated $event) {
 416          // If this feature has been disabled, do nothing.
 417          if (accessibility::is_accessibility_enabled()) {
 418              static::area_altered($event);
 419          }
 420      }
 421  
 422      /**
 423       * Add an observer record if appropriate.
 424       * @param stdClass $data
 425       * @throws \dml_exception
 426       */
 427      private static function observer_insert(stdClass $data) {
 428          global $DB;
 429  
 430          // Handle if this feature is enabled and this course is in the schedule.
 431          if (static::course_event_should_be_handled($data->courseid)) {
 432              $data->timecreated = time();
 433              $data->timecompleted = 0;
 434  
 435              $DB->insert_record(manager::DB_PROCESS, $data);
 436          }
 437      }
 438  }