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