Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 311 and 403]

   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       * Legacy event name.
  86       *
  87       * @return string legacy event name
  88       */
  89      public static function get_legacy_eventname() {
  90          return 'mod_updated';
  91      }
  92  
  93      /**
  94       * Legacy event data.
  95       *
  96       * @return \stdClass
  97       */
  98      protected function get_legacy_eventdata() {
  99          $eventdata = new \stdClass();
 100          $eventdata->modulename = $this->other['modulename'];
 101          $eventdata->name       = $this->other['name'];
 102          $eventdata->cmid       = $this->objectid;
 103          $eventdata->courseid   = $this->courseid;
 104          $eventdata->userid     = $this->userid;
 105          return $eventdata;
 106      }
 107  
 108      /**
 109       * replace add_to_log() statement.
 110       *
 111       * @return array of parameters to be passed to legacy add_to_log() function.
 112       */
 113      protected function get_legacy_logdata() {
 114          $log1 = array($this->courseid, "course", "update mod", "../mod/" . $this->other['modulename'] . "/view.php?id=" .
 115                  $this->objectid, $this->other['modulename'] . " " . $this->other['instanceid']);
 116          $log2 = array($this->courseid, $this->other['modulename'], "update",
 117                 "view.php?id={$this->objectid}",
 118                 "{$this->other['instanceid']}", $this->objectid);
 119          return array($log1, $log2);
 120      }
 121  
 122      /**
 123       * custom validations
 124       *
 125       * Throw \coding_exception notice in case of any problems.
 126       */
 127      protected function validate_data() {
 128          parent::validate_data();
 129          if (!isset($this->other['modulename'])) {
 130              throw new \coding_exception('The \'modulename\' value must be set in other.');
 131          }
 132          if (!isset($this->other['instanceid'])) {
 133              throw new \coding_exception('The \'instanceid\' value must be set in other.');
 134          }
 135          if (!isset($this->other['name'])) {
 136              throw new \coding_exception('The \'name\' value must be set in other.');
 137          }
 138      }
 139  
 140      /**
 141       * Set data to create new event from course module.
 142       *
 143       * @param \cm_info|\stdClass $cm course module instance, as returned by {@link get_coursemodule_from_id}
 144       *                     or {@link get_coursemodule_from_instance}.
 145       * @param \context_module $modcontext module context instance
 146       * @return \core\event\base returns instance of new event
 147       */
 148      public static final function create_from_cm($cm, $modcontext = null) {
 149          // If not set, get the module context.
 150          if (empty($modcontext)) {
 151              $modcontext = \context_module::instance($cm->id);
 152          }
 153  
 154          // Create event object for course module update action.
 155          $event = static::create(array(
 156              'context'  => $modcontext,
 157              'objectid' => $cm->id,
 158              'other'    => array(
 159                  'modulename' => $cm->modname,
 160                  'instanceid' => $cm->instance,
 161                  'name'       => $cm->name,
 162              )
 163          ));
 164          return $event;
 165      }
 166  
 167      public static function get_objectid_mapping() {
 168          return array('db' => 'course_modules', 'restore' => 'course_module');
 169      }
 170  
 171      public static function get_other_mapping() {
 172          $othermapping = array();
 173          $othermapping['instanceid'] = base::NOT_MAPPED;
 174  
 175          return $othermapping;
 176      }
 177  }
 178