Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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  /**
  18   * Contentbank content uploaded event.
  19   *
  20   * @package    core
  21   * @copyright  2020 Amaia Anabitarte <amaia@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core\event;
  26  
  27  /**
  28   * Content bank content uploaded class.
  29   *
  30   * @property-read array $other {
  31   *      Extra information about event.
  32   *      - string contenttype: the contenttype of the content.
  33   *      - string name: the name of the content.
  34   * }
  35   *
  36   * @package    core
  37   * @since      Moodle 3.9
  38   * @copyright  2020 Amaia Anabitarte <amaia@moodle.com>
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class contentbank_content_uploaded extends base {
  42  
  43      /**
  44       * Initialise the event data.
  45       */
  46      protected function init() {
  47          $this->data['objecttable'] = 'contentbank_content';
  48          $this->data['crud'] = 'c';
  49          $this->data['edulevel'] = self::LEVEL_OTHER;
  50      }
  51  
  52      /**
  53       * Creates an event from content bank content object
  54       *
  55       * @since Moodle 3.9
  56       * @param \stdClass $record Data to create the event
  57       * @return contentbank_content_uploaded
  58       */
  59      public static function create_from_record(\stdClass $record) {
  60          $event = self::create([
  61              'objectid' => $record->id,
  62              'relateduserid' => $record->usercreated,
  63              'context' => \context::instance_by_id($record->contextid),
  64              'other' => [
  65                  'contenttype' => $record->contenttype,
  66                  'name' => $record->name
  67              ]
  68          ]);
  69          return $event;
  70      }
  71  
  72      /**
  73       * Returns localised general event name.
  74       *
  75       * @return string
  76       */
  77      public static function get_name() {
  78          return get_string('eventcontentuploaded', 'core_contentbank');
  79      }
  80  
  81      /**
  82       * Returns non-localised description of what happened.
  83       *
  84       * @return string
  85       */
  86      public function get_description() {
  87          return "The user with id '$this->userid' uploaded the content with id '$this->objectid'.";
  88      }
  89  
  90      /**
  91       * Custom validation.
  92       *
  93       * @throws \coding_exception
  94       * @return void
  95       */
  96      protected function validate_data() {
  97          parent::validate_data();
  98  
  99          if (!isset($this->other['contenttype'])) {
 100              throw new \coding_exception('The \'contenttype\' value must be set in other.');
 101          }
 102  
 103          if (!isset($this->other['name'])) {
 104              throw new \coding_exception('The \'name\' value must be set in other.');
 105          }
 106      }
 107  
 108      /**
 109       * Returns relevant URL.
 110       *
 111       * @return \moodle_url
 112       */
 113      public function get_url() {
 114          $url = new \moodle_url('/contentbank/view.php');
 115          $url->param('id', $this->objectid);
 116          return $url;
 117      }
 118  
 119      /**
 120       * Used for mapping events on restore
 121       *
 122       * @return array
 123       */
 124      public static function get_objectid_mapping() {
 125          return array('db' => 'contentbank_content', 'restore' => 'contentbank_content');
 126      }
 127  
 128      /**
 129       * Used for mapping events on restore
 130       *
 131       * @return bool
 132       */
 133      public static function get_other_mapping() {
 134          // No mapping required.
 135          return false;
 136      }
 137  }