Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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 to be triggered when a new course module is updated.
  19   *
  20   * @package    core
  21   * @copyright  2013 Ankit Agarwal
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
  23   */
  24  
  25  namespace core\event;
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Class course_module_updated
  30   *
  31   * Class for event to be triggered when a course module is updated.
  32   *
  33   * @property-read array $other {
  34   *      Extra information about event.
  35   *
  36   *      - string modulename: name of module updated.
  37   *      - string name: title of module.
  38   *      - string instanceid: id of module instance.
  39   * }
  40   *
  41   * @package    core
  42   * @since      Moodle 2.6
  43   * @copyright  2013 Ankit Agarwal
  44   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
  45   */
  46  class course_module_updated extends base {
  47  
  48      /**
  49       * Set basic properties for the event.
  50       */
  51      protected function init() {
  52          $this->data['objecttable'] = 'course_modules';
  53          $this->data['crud'] = 'u';
  54          $this->data['edulevel'] = self::LEVEL_TEACHING;
  55      }
  56  
  57      /**
  58       * Returns localised general event name.
  59       *
  60       * @return string
  61       */
  62      public static function get_name() {
  63          return get_string('eventcoursemoduleupdated', 'core');
  64      }
  65  
  66      /**
  67       * Returns non-localised event description with id's for admin use only.
  68       *
  69       * @return string
  70       */
  71      public function get_description() {
  72          return "The user with id '$this->userid' updated the '{$this->other['modulename']}' activity with " .
  73              "course module id '$this->contextinstanceid'.";
  74      }
  75  
  76      /**
  77       * Returns relevant URL.
  78       * @return \moodle_url
  79       */
  80      public function get_url() {
  81          return new \moodle_url('/mod/' . $this->other['modulename'] . '/view.php', array('id' => $this->objectid));
  82      }
  83  
  84      /**
  85       * custom validations
  86       *
  87       * Throw \coding_exception notice in case of any problems.
  88       */
  89      protected function validate_data() {
  90          parent::validate_data();
  91          if (!isset($this->other['modulename'])) {
  92              throw new \coding_exception('The \'modulename\' value must be set in other.');
  93          }
  94          if (!isset($this->other['instanceid'])) {
  95              throw new \coding_exception('The \'instanceid\' value must be set in other.');
  96          }
  97          if (!isset($this->other['name'])) {
  98              throw new \coding_exception('The \'name\' value must be set in other.');
  99          }
 100      }
 101  
 102      /**
 103       * Set data to create new event from course module.
 104       *
 105       * @param \cm_info|\stdClass $cm course module instance, as returned by {@link get_coursemodule_from_id}
 106       *                     or {@link get_coursemodule_from_instance}.
 107       * @param \context_module $modcontext module context instance
 108       * @return \core\event\base returns instance of new event
 109       */
 110      public static final function create_from_cm($cm, $modcontext = null) {
 111          // If not set, get the module context.
 112          if (empty($modcontext)) {
 113              $modcontext = \context_module::instance($cm->id);
 114          }
 115  
 116          // Create event object for course module update action.
 117          $event = static::create(array(
 118              'context'  => $modcontext,
 119              'objectid' => $cm->id,
 120              'other'    => array(
 121                  'modulename' => $cm->modname,
 122                  'instanceid' => $cm->instance,
 123                  'name'       => $cm->name,
 124              )
 125          ));
 126          return $event;
 127      }
 128  
 129      public static function get_objectid_mapping() {
 130          return array('db' => 'course_modules', 'restore' => 'course_module');
 131      }
 132  
 133      public static function get_other_mapping() {
 134          $othermapping = array();
 135          $othermapping['instanceid'] = base::NOT_MAPPED;
 136  
 137          return $othermapping;
 138      }
 139  }
 140