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.

Differences Between: [Versions 401 and 402] [Versions 401 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 created.
  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_created
  30   *
  31   * Class for event to be triggered when a new course module is created.
  32   *
  33   * @property-read array $other {
  34   *      Extra information about event.
  35   *
  36   *      - string modulename: name of module created.
  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_created 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'] = 'c';
  54          $this->data['edulevel'] = self::LEVEL_TEACHING;
  55      }
  56  
  57      /**
  58       * Api to Create new event from course module.
  59       *
  60       * @since Moodle 2.6.4, 2.7.1
  61       * @param \cm_info|\stdClass $cm course module instance, as returned by {@link get_coursemodule_from_id}
  62       *                               or {@link get_coursemodule_from_instance}.
  63       * @param \context_module $modcontext module context instance
  64       *
  65       * @return \core\event\base returns instance of new event
  66       */
  67      public static final function create_from_cm($cm, $modcontext = null) {
  68          // If not set, get the module context.
  69          if (empty($modcontext)) {
  70              $modcontext = \context_module::instance($cm->id);
  71          }
  72  
  73          // Create event object for course module update action.
  74          $event = static::create(array(
  75              'context'  => $modcontext,
  76              'objectid' => $cm->id,
  77              'other'    => array(
  78                  'modulename' => $cm->modname,
  79                  'instanceid' => $cm->instance,
  80                  'name'       => $cm->name,
  81              )
  82          ));
  83          return $event;
  84      }
  85  
  86      /**
  87       * Returns localised general event name.
  88       *
  89       * @return string
  90       */
  91      public static function get_name() {
  92          return get_string('eventcoursemodulecreated', 'core');
  93      }
  94  
  95      /**
  96       * Returns non-localised event description with id's for admin use only.
  97       *
  98       * @return string
  99       */
 100      public function get_description() {
 101          return "The user with id '$this->userid' created the '{$this->other['modulename']}' activity with " .
 102              "course module id '$this->contextinstanceid'.";
 103      }
 104  
 105      /**
 106       * Returns relevant URL.
 107       * @return \moodle_url
 108       */
 109      public function get_url() {
 110          return new \moodle_url('/mod/' . $this->other['modulename'] . '/view.php', array('id' => $this->objectid));
 111      }
 112  
 113      /**
 114       * Legacy event name.
 115       *
 116       * @return string legacy event name
 117       */
 118      public static function get_legacy_eventname() {
 119          return 'mod_created';
 120      }
 121  
 122      /**
 123       * Legacy event data.
 124       *
 125       * @return \stdClass
 126       */
 127      protected function get_legacy_eventdata() {
 128          $eventdata = new \stdClass();
 129          $eventdata->modulename = $this->other['modulename'];
 130          $eventdata->name       = $this->other['name'];
 131          $eventdata->cmid       = $this->objectid;
 132          $eventdata->courseid   = $this->courseid;
 133          $eventdata->userid     = $this->userid;
 134          return $eventdata;
 135      }
 136  
 137      /**
 138       * replace add_to_log() statement.
 139       *
 140       * @return array of parameters to be passed to legacy add_to_log() function.
 141       */
 142      protected function get_legacy_logdata() {
 143          $log1 = array($this->courseid, "course", "add mod", "../mod/" . $this->other['modulename'] . "/view.php?id=" .
 144                  $this->objectid, $this->other['modulename'] . " " . $this->other['instanceid']);
 145          $log2 = array($this->courseid, $this->other['modulename'], "add",
 146              "view.php?id={$this->objectid}",
 147                  "{$this->other['instanceid']}", $this->objectid);
 148          return array($log1, $log2);
 149      }
 150  
 151      /**
 152       * Custom validation.
 153       *
 154       * @throw \coding_exception
 155       */
 156      protected function validate_data() {
 157          parent::validate_data();
 158          if (!isset($this->other['modulename'])) {
 159              throw new \coding_exception('The \'modulename\' value must be set in other.');
 160          }
 161          if (!isset($this->other['instanceid'])) {
 162              throw new \coding_exception('The \'instanceid\' value must be set in other.');
 163          }
 164          if (!isset($this->other['name'])) {
 165              throw new \coding_exception('The \'name\' value must be set in other.');
 166          }
 167      }
 168  
 169      public static function get_objectid_mapping() {
 170          return array('db' => 'course_modules', 'restore' => 'course_module');
 171      }
 172  
 173      public static function get_other_mapping() {
 174          $othermapped = array();
 175          $othermapped['instanceid'] = base::NOT_MAPPED;
 176  
 177          return $othermapped;
 178      }
 179  }
 180