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.

Differences Between: [Versions 401 and 402] [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       * Return name of the legacy event, which is replaced by this event.
 102       *
 103       * @return string legacy event name
 104       */
 105      public static function get_legacy_eventname() {
 106          return 'course_completed';
 107      }
 108  
 109      /**
 110       * Return course_completed legacy event data.
 111       *
 112       * @return \stdClass completion data.
 113       */
 114      protected function get_legacy_eventdata() {
 115          return $this->get_record_snapshot('course_completions', $this->objectid);
 116      }
 117  
 118      /**
 119       * Custom validation.
 120       *
 121       * @throws \coding_exception
 122       * @return void
 123       */
 124      protected function validate_data() {
 125          parent::validate_data();
 126  
 127          if (!isset($this->relateduserid)) {
 128              throw new \coding_exception('The \'relateduserid\' must be set.');
 129          }
 130  
 131          // Check that the 'relateduserid' value is set in other as well. This is because we introduced this in 2.6
 132          // and some observers may be relying on this value to be present.
 133          if (!isset($this->other['relateduserid'])) {
 134              throw new \coding_exception('The \'relateduserid\' value must be set in other.');
 135          }
 136      }
 137  
 138      public static function get_objectid_mapping() {
 139          // Sorry - there is no mapping available for completion records.
 140          return array('db' => 'course_completions', 'restore' => base::NOT_MAPPED);
 141      }
 142  
 143      public static function get_other_mapping() {
 144          $othermapped = array();
 145          $othermapped['relateduserid'] = array('db' => 'user', 'restore' => 'user');
 146          return $othermapped;
 147      }
 148  }