2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Course completed event.
21 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
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
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
{
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(
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);
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
;
74 * Returns localised event name.
78 public static function get_name() {
79 return get_string('eventcoursecompleted', 'core_completion');
83 * Returns non-localised event description with id's for admin use only.
87 public function get_description() {
88 return "The user with id '$this->relateduserid' completed the course with id '$this->courseid'.";
92 * Returns relevant 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
);
121 * @throws \coding_exception
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');