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.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]

   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   * Course completed event.
  19   *
  20   * @package    core
  21   * @copyright  2013 Rajesh Taneja <rajesh@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core\event;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Course completed event class.
  31   *
  32   * @property-read int $relateduserid user who completed the course
  33   * @property-read array $other {
  34   *      Extra information about event.
  35   *
  36   *      - int relateduserid: deprecated since 2.7, please use property relateduserid
  37   * }
  38   *
  39   * @package    core
  40   * @since      Moodle 2.6
  41   * @copyright  2013 Rajesh Taneja <rajesh@moodle.com>
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class course_completed extends base {
  45      /**
  46       * Create event from course_completion record.
  47       * @param \stdClass $completion
  48       * @return course_completed
  49       */
  50      public static function create_from_completion(\stdClass $completion) {
  51          $event = self::create(
  52              array(
  53                  'objectid' => $completion->id,
  54                  'relateduserid' => $completion->userid,
  55                  'context' => \context_course::instance($completion->course),
  56                  'courseid' => $completion->course,
  57                  'other' => array('relateduserid' => $completion->userid), // Deprecated since 2.7, please use property relateduserid.
  58              )
  59          );
  60          $event->add_record_snapshot('course_completions', $completion);
  61          return $event;
  62      }
  63  
  64      /**
  65       * Initialise required event data properties.
  66       */
  67      protected function init() {
  68          $this->data['objecttable'] = 'course_completions';
  69          $this->data['crud'] = 'u';
  70          $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
  71      }
  72  
  73      /**
  74       * Returns localised event name.
  75       *
  76       * @return string
  77       */
  78      public static function get_name() {
  79          return get_string('eventcoursecompleted', 'core_completion');
  80      }
  81  
  82      /**
  83       * Returns non-localised event description with id's for admin use only.
  84       *
  85       * @return string
  86       */
  87      public function get_description() {
  88          return "The user with id '$this->relateduserid' completed the course with id '$this->courseid'.";
  89      }
  90  
  91      /**
  92       * Returns relevant URL.
  93       *
  94       * @return \moodle_url
  95       */
  96      public function get_url() {
  97          return new \moodle_url('/report/completion/index.php', array('course' => $this->courseid));
  98      }
  99  
 100      /**
 101       * Custom validation.
 102       *
 103       * @throws \coding_exception
 104       * @return void
 105       */
 106      protected function validate_data() {
 107          parent::validate_data();
 108  
 109          if (!isset($this->relateduserid)) {
 110              throw new \coding_exception('The \'relateduserid\' must be set.');
 111          }
 112  
 113          // Check that the 'relateduserid' value is set in other as well. This is because we introduced this in 2.6
 114          // and some observers may be relying on this value to be present.
 115          if (!isset($this->other['relateduserid'])) {
 116              throw new \coding_exception('The \'relateduserid\' value must be set in other.');
 117          }
 118      }
 119  
 120      public static function get_objectid_mapping() {
 121          // Sorry - there is no mapping available for completion records.
 122          return array('db' => 'course_completions', 'restore' => base::NOT_MAPPED);
 123      }
 124  
 125      public static function get_other_mapping() {
 126          $othermapped = array();
 127          $othermapped['relateduserid'] = array('db' => 'user', 'restore' => 'user');
 128          return $othermapped;
 129      }
 130  }