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 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       * Custom validation.
 115       *
 116       * @throw \coding_exception
 117       */
 118      protected function validate_data() {
 119          parent::validate_data();
 120          if (!isset($this->other['modulename'])) {
 121              throw new \coding_exception('The \'modulename\' value must be set in other.');
 122          }
 123          if (!isset($this->other['instanceid'])) {
 124              throw new \coding_exception('The \'instanceid\' value must be set in other.');
 125          }
 126          if (!isset($this->other['name'])) {
 127              throw new \coding_exception('The \'name\' value must be set in other.');
 128          }
 129      }
 130  
 131      public static function get_objectid_mapping() {
 132          return array('db' => 'course_modules', 'restore' => 'course_module');
 133      }
 134  
 135      public static function get_other_mapping() {
 136          $othermapped = array();
 137          $othermapped['instanceid'] = base::NOT_MAPPED;
 138  
 139          return $othermapped;
 140      }
 141  }
 142