Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is 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   * Evidence created event.
  19   *
  20   * @package    core_competency
  21   * @copyright  2016 Jun Pataleta <jun@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core\event;
  25  
  26  use core\event\base;
  27  use core_competency\evidence;
  28  use core_competency\user_competency;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  /**
  33   * Evidence created event class.
  34   *
  35   * @property-read array $other {
  36   *      Extra information about event.
  37   *
  38   *      - int usercompetencyid: The user_competency ID linked to the evidence.
  39   *      - int competencyid: The competency ID linked to the evidence from user_competency.
  40   *      - int action: The action constant.
  41   *      - bool recommend: The recommend flag.
  42   * }
  43   *
  44   * @package    core_competency
  45   * @since      Moodle 3.1
  46   * @copyright  2016 Jun Pataleta <jun@moodle.com>
  47   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  48   */
  49  class competency_evidence_created extends base {
  50  
  51      /**
  52       * Convenience method to instantiate the event.
  53       *
  54       * @param evidence $evidence The evidence.
  55       * @param user_competency $usercompetency The user competency object linked to the evidence.
  56       * @param bool $recommend The recommend flag.
  57       * @return evidence_created
  58       * @throws \coding_exception
  59       */
  60      public static final function create_from_evidence(evidence $evidence, user_competency $usercompetency, $recommend) {
  61          // Make sure we have a valid evidence.
  62          if (!$evidence->get('id')) {
  63              throw new \coding_exception('The evidence ID must be set.');
  64          }
  65  
  66          // Make sure we have a valid user competency.
  67          if (!$usercompetency->get('id')) {
  68              throw new \coding_exception('The user competency ID must be set.');
  69          }
  70  
  71          // Make sure that the a proper user competecy is linked to the evidence.
  72          if ($evidence->get('usercompetencyid') != $usercompetency->get('id')) {
  73              throw new \coding_exception('The user competency linked with this evidence is invalid.');
  74          }
  75  
  76          $event = static::create([
  77              'contextid'  => $evidence->get('contextid'),
  78              'objectid' => $evidence->get('id'),
  79              'userid' => $evidence->get('actionuserid'),
  80              'relateduserid' => $usercompetency->get('userid'),
  81              'other' => [
  82                  'usercompetencyid' => $usercompetency->get('id'),
  83                  'competencyid' => $usercompetency->get('competencyid'),
  84                  'action' => $evidence->get('action'),
  85                  'recommend' => $recommend
  86              ]
  87          ]);
  88  
  89          // Add record snapshot for the evidence.
  90          $event->add_record_snapshot(evidence::TABLE, $evidence->to_record());
  91  
  92          // Add record snapshot for the user competency.
  93          $event->add_record_snapshot(user_competency::TABLE, $usercompetency->to_record());
  94  
  95          return $event;
  96      }
  97  
  98      /**
  99       * Returns localised general event name.
 100       *
 101       * @return string
 102       */
 103      public static function get_name() {
 104          return get_string('eventevidencecreated', 'core_competency');
 105      }
 106  
 107      /**
 108       * Returns non-localised description of what happened.
 109       *
 110       * @return string
 111       */
 112      public function get_description() {
 113          return "The user with id '$this->userid' created an evidence with id '$this->objectid'.";
 114      }
 115  
 116      /**
 117       * Returns relevant URL.
 118       *
 119       * @return \moodle_url
 120       */
 121      public function get_url() {
 122          return \core_competency\url::user_competency($this->other['usercompetencyid']);
 123      }
 124  
 125      /**
 126       * Initialise the event data.
 127       */
 128      protected function init() {
 129          $this->data['objecttable'] = evidence::TABLE;
 130          $this->data['crud'] = 'c';
 131          $this->data['edulevel'] = self::LEVEL_TEACHING;
 132      }
 133  
 134      /**
 135       * Get_objectid_mapping method.
 136       *
 137       * @return string the name of the restore mapping the objectid links to
 138       */
 139      public static function get_objectid_mapping() {
 140          return base::NOT_MAPPED;
 141      }
 142  
 143      /**
 144       * Validate the data.
 145       *
 146       * @throws \coding_exception
 147       */
 148      protected function validate_data() {
 149          parent::validate_data();
 150  
 151          if (!isset($this->relateduserid)) {
 152              throw new \coding_exception('The \'relateduserid\' must be set.');
 153          }
 154  
 155          if (!isset($this->other['usercompetencyid'])) {
 156              throw new \coding_exception('The \'usercompetencyid\' data in \'other\' must be set.');
 157          }
 158  
 159          if (!isset($this->other['competencyid'])) {
 160              throw new \coding_exception('The \'competencyid\' data in \'other\' must be set.');
 161          }
 162  
 163          if (!isset($this->other['action'])) {
 164              throw new \coding_exception('The \'action\' data in \'other\' must be set.');
 165          }
 166  
 167          if (!isset($this->other['recommend'])) {
 168              throw new \coding_exception('The \'recommend\' data in \'other\' must be set.');
 169          }
 170      }
 171  }