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.
   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   * User competency viewed event.
  19   *
  20   * @package    core_competency
  21   * @copyright  2016 Issam Taboubi <issam.taboubi@umontreal.ca>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core\event;
  26  
  27  use core\event\base;
  28  use core_competency\user_competency;
  29  use context_course;
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  /**
  33   * User competency viewed in plan event class.
  34   *
  35   * @property-read array $other {
  36   *      Extra information about event.
  37   *
  38   *      - int planid: id of plan for which competency is associated.
  39   *      - int competencyid: id of competency.
  40   * }
  41   *
  42   * @package    core_competency
  43   * @since      Moodle 3.1
  44   * @copyright  2016 Issam Taboubi <issam.taboubi@umontreal.ca>
  45   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  46   */
  47  class competency_user_competency_viewed_in_plan extends base {
  48  
  49      /**
  50       * Convenience method to instantiate the event in plan.
  51       *
  52       * @param user_competency $usercompetency The user competency.
  53       * @param int $planid The pland ID
  54       * @return self
  55       */
  56      public static function create_from_user_competency_viewed_in_plan(user_competency $usercompetency, $planid) {
  57          if (!$usercompetency->get('id')) {
  58              throw new \coding_exception('The user competency ID must be set.');
  59          }
  60          $params = array(
  61              'contextid' => $usercompetency->get_context()->id,
  62              'objectid' => $usercompetency->get('id'),
  63              'relateduserid' => $usercompetency->get('userid'),
  64              'other' => array(
  65                  'competencyid' => $usercompetency->get('competencyid'),
  66                  'planid' => $planid
  67              )
  68          );
  69  
  70          $event = static::create($params);
  71          $event->add_record_snapshot(user_competency::TABLE, $usercompetency->to_record());
  72          return $event;
  73      }
  74  
  75      /**
  76       * Returns description of what happened.
  77       *
  78       * @return string
  79       */
  80      public function get_description() {
  81          return "The user with id '$this->userid' viewed the user competency with id '$this->objectid' "
  82                  . "in plan with id '" . $this->other['planid'] . "'";
  83      }
  84  
  85      /**
  86       * Return localised event name.
  87       *
  88       * @return string
  89       */
  90      public static function get_name() {
  91          return get_string('eventusercompetencyviewedinplan', 'core_competency');
  92      }
  93  
  94      /**
  95       * Get URL related to the action
  96       *
  97       * @return \moodle_url
  98       */
  99      public function get_url() {
 100          return \core_competency\url::user_competency_in_plan($this->relateduserid, $this->other['competencyid'],
 101              $this->other['planid']);
 102      }
 103  
 104      /**
 105       * Init method.
 106       *
 107       * @return void
 108       */
 109      protected function init() {
 110          $this->data['crud'] = 'r';
 111          $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
 112          $this->data['objecttable'] = user_competency::TABLE;
 113      }
 114  
 115      /**
 116       * Get_objectid_mapping method.
 117       *
 118       * @return string the name of the restore mapping the objectid links to
 119       */
 120      public static function get_objectid_mapping() {
 121          return base::NOT_MAPPED;
 122      }
 123  
 124      /**
 125       * Custom validation.
 126       *
 127       * Throw \coding_exception notice in case of any problems.
 128       */
 129      protected function validate_data() {
 130          if ($this->other === null) {
 131              throw new \coding_exception('The \'competencyid\' and \'planid\' values must be set.');
 132          }
 133  
 134          if (!isset($this->other['competencyid'])) {
 135              throw new \coding_exception('The \'competencyid\' value must be set.');
 136          }
 137  
 138          if (!isset($this->other['planid'])) {
 139              throw new \coding_exception('The \'planid\' value must be set.');
 140          }
 141      }
 142  
 143  }