Merge branch 'MDL-57742_master' of git://github.com/markn86/moodle
[moodle.git] / lib / classes / event / course_completed.php
blobb01c626cb44ff85d8b96252f953ec8405b49a7ec
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.
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/>.
17 /**
18 * Course completed event.
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
25 namespace core\event;
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Course completed event class.
32 * @property-read int $relateduserid user who completed the course
33 * @property-read array $other {
34 * Extra information about event.
36 * - int relateduserid: deprecated since 2.7, please use property relateduserid
37 * }
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
44 class course_completed extends base {
45 /**
46 * Create event from course_completion record.
47 * @param \stdClass $completion
48 * @return course_completed
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.
60 $event->add_record_snapshot('course_completions', $completion);
61 return $event;
64 /**
65 * Initialise required event data properties.
67 protected function init() {
68 $this->data['objecttable'] = 'course_completions';
69 $this->data['crud'] = 'u';
70 $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
73 /**
74 * Returns localised event name.
76 * @return string
78 public static function get_name() {
79 return get_string('eventcoursecompleted', 'core_completion');
82 /**
83 * Returns non-localised event description with id's for admin use only.
85 * @return string
87 public function get_description() {
88 return "The user with id '$this->relateduserid' completed the course with id '$this->courseid'.";
91 /**
92 * Returns relevant URL.
94 * @return \moodle_url
96 public function get_url() {
97 return new \moodle_url('/report/completion/index.php', array('course' => $this->courseid));
101 * Return name of the legacy event, which is replaced by this event.
103 * @return string legacy event name
105 public static function get_legacy_eventname() {
106 return 'course_completed';
110 * Return course_completed legacy event data.
112 * @return \stdClass completion data.
114 protected function get_legacy_eventdata() {
115 return $this->get_record_snapshot('course_completions', $this->objectid);
119 * Custom validation.
121 * @throws \coding_exception
122 * @return void
124 protected function validate_data() {
125 parent::validate_data();
127 if (!isset($this->relateduserid)) {
128 throw new \coding_exception('The \'relateduserid\' must be set.');
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.');
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);
143 public static function get_other_mapping() {
144 $othermapped = array();
145 $othermapped['relateduserid'] = array('db' => 'user', 'restore' => 'user');
146 return $othermapped;