From 8b233bdacd315922fba5589e4729d8cb24347a6b Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Wed, 30 Apr 2014 13:47:38 +0800 Subject: [PATCH] MDL-43557 fix BC issue in \core\event\course_completed and incorrect description --- completion/completion_completion.php | 12 +------- lib/classes/event/course_completed.php | 39 +++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/completion/completion_completion.php b/completion/completion_completion.php index 18c78dceb8a4d..4e0a33c443691 100644 --- a/completion/completion_completion.php +++ b/completion/completion_completion.php @@ -161,17 +161,7 @@ public function mark_complete($timecomplete = null) { // Save record. if ($result = $this->_save()) { $data = $this->get_record_data(); - $event = \core\event\course_completed::create( - array( - 'objectid' => $data->id, - 'userid' => $USER->id, - 'relateduserid' => $data->userid, - 'context' => context_course::instance($data->course), - 'courseid' => $data->course - ) - ); - $event->add_record_snapshot('course_completions', $data); - $event->trigger(); + \core\event\course_completed::create_from_completion($data)->trigger(); } return $result; diff --git a/lib/classes/event/course_completed.php b/lib/classes/event/course_completed.php index 32e129ef618b6..705251d6d0ed0 100644 --- a/lib/classes/event/course_completed.php +++ b/lib/classes/event/course_completed.php @@ -29,12 +29,37 @@ /** * Course completed event class. * + * @property-read int $relateduserid user who completed the course + * @property-read array $other { + * Extra information about event. + * + * - int relateduserid: deprecated since 2.7, please use property relateduserid + * } + * * @package core * @since Moodle 2.6 * @copyright 2013 Rajesh Taneja * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class course_completed extends base { + /** + * Create event from course_completion record. + * @param \stdClass $completion + * @return course_completed + */ + public static function create_from_completion(\stdClass $completion) { + $event = self::create( + array( + 'objectid' => $completion->id, + 'relateduserid' => $completion->userid, + 'context' => \context_course::instance($completion->course), + 'courseid' => $completion->course, + 'other' => array('relateduserid' => $completion->userid), // Deprecated since 2.7, please use property relateduserid. + ) + ); + $event->add_record_snapshot('course_completions', $completion); + return $event; + } /** * Initialise required event data properties. @@ -60,7 +85,7 @@ public static function get_name() { * @return string */ public function get_description() { - return "The course with the id '$this->courseid' was completed by the user with the id '$this->userid'."; + return "The course with the id '$this->courseid' was completed for the user with the id '$this->relateduserid'."; } /** @@ -89,4 +114,16 @@ public static function get_legacy_eventname() { protected function get_legacy_eventdata() { return $this->get_record_snapshot('course_completions', $this->objectid); } + + /** + * Custom validation. + * + * @throws \coding_exception + * @return void + */ + protected function validate_data() { + parent::validate_data(); + + // TODO: MDL-45319 add validation of relateduserid and other['relateduserid']. + } }