Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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 mod_forum\event;
  18  
  19  use coding_exception;
  20  use moodle_url;
  21  
  22  /**
  23   * The mod_forum discussion lock updated event.
  24   *
  25   * @package    mod_forum
  26   * @copyright  2022 Université Rennes 2 {@link https://www.univ-rennes2.fr}
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  class discussion_lock_updated extends \core\event\base {
  30      /**
  31       * Init method.
  32       *
  33       * @return void
  34       */
  35      protected function init(): void {
  36          $this->data['crud'] = 'u';
  37          $this->data['edulevel'] = self::LEVEL_OTHER;
  38          $this->data['objecttable'] = 'forum_discussions';
  39      }
  40  
  41      /**
  42       * Returns description of what happened.
  43       *
  44       * @return string
  45       */
  46      public function get_description(): string {
  47          return "The user with id '$this->userid' {$this->other['status']} the discussion: $this->objectid".
  48              " in the forum: {$this->other['forumid']}";
  49      }
  50  
  51      /**
  52       * Return localised event name.
  53       *
  54       * @return string
  55       */
  56      public static function get_name(): string {
  57          return get_string('eventdiscussionlockupdated', 'mod_forum');
  58      }
  59  
  60      /**
  61       * Get URL related to the action
  62       *
  63       * @return moodle_url
  64       */
  65      public function get_url(): moodle_url {
  66          return new moodle_url('/mod/forum/discuss.php', ['d' => $this->objectid]);
  67      }
  68  
  69      /**
  70       * Custom validation.
  71       *
  72       * @throws coding_exception
  73       * @return void
  74       */
  75      protected function validate_data(): void {
  76          parent::validate_data();
  77          if (!isset($this->other['forumid'])) {
  78              throw new coding_exception('forumid must be set in $other.');
  79          }
  80          if (!isset($this->other['status'])) {
  81              throw new \coding_exception('The \'status\' value must be set in other.');
  82          }
  83          if (!in_array($this->other['status'], ['locked', 'unlocked'], true)) {
  84              throw new \coding_exception('The \'status\' value must be \'locked\' or \'unlocked\'.');
  85          }
  86          if ($this->contextlevel != CONTEXT_MODULE) {
  87              throw new coding_exception('Context passed must be module context.');
  88          }
  89          if (!isset($this->objectid)) {
  90              throw new coding_exception('objectid must be set to the discussionid.');
  91          }
  92      }
  93  
  94      /**
  95       * Forum discussion object id mappings.
  96       *
  97       * @return array
  98       */
  99      public static function get_objectid_mapping(): array {
 100          return ['db' => 'forum_discussions', 'restore' => 'forum_discussion'];
 101      }
 102  
 103      /**
 104       * Forum id mappings.
 105       *
 106       * @return array
 107       */
 108      public static function get_other_mapping(): array {
 109          return ['forumid' => ['db' => 'forum', 'restore' => 'forum']];
 110      }
 111  }