diff --git a/mod/lesson/classes/event/highscore_added.php b/mod/lesson/classes/event/highscore_added.php index b3ce792128db8..b8cd4aaad5117 100644 --- a/mod/lesson/classes/event/highscore_added.php +++ b/mod/lesson/classes/event/highscore_added.php @@ -27,6 +27,22 @@ defined('MOODLE_INTERNAL') || die(); +/** + * Event to be triggered when a highscore is added. + * + * @property-read array $other { + * Extra information about event. + * + * - int lessonid: the id of the lesson in the lesson table. + * - string nickname: the user's nickname. + * } + * + * @package mod_lesson + * @since Moodle 2.7 + * @copyright 2013 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. + */ + class highscore_added extends \core\event\base { /** @@ -62,10 +78,8 @@ public function get_url() { * @return string */ public function get_description() { - $highscore = $this->get_record_snapshot('lesson_high_scores', $this->objectid); - - return 'A new highscore was added to the lesson with the id ' . $highscore->lessonid . - ' for user with the id ' . $this->userid; + return "The user with the id '$this->userid' added a new highscore to the lesson activity with the course module " . + "id '$this->contextinstanceid'."; } /** @@ -74,9 +88,25 @@ public function get_description() { * @return array of parameters to be passed to legacy add_to_log() function. */ protected function get_legacy_logdata() { - $highscore = $this->get_record_snapshot('lesson_high_scores', $this->objectid); - return array($this->courseid, 'lesson', 'update highscores', 'highscores.php?id=' . $this->contextinstanceid, - $highscore->nickname, $this->contextinstanceid); + $this->other['nickname'], $this->contextinstanceid); + } + + /** + * Custom validations. + * + * @throws \coding_exception when validation fails. + * @return void + */ + protected function validate_data() { + parent::validate_data(); + + if (!isset($this->other['lessonid'])) { + throw new \coding_exception('The \'lessonid\' value must be set in other.'); + } + + if (!isset($this->other['nickname'])) { + throw new \coding_exception('The \'nickname\' value must be set in other.'); + } } } diff --git a/mod/lesson/highscores.php b/mod/lesson/highscores.php index d1c3596363d80..5cbedbb6f848b 100644 --- a/mod/lesson/highscores.php +++ b/mod/lesson/highscores.php @@ -149,6 +149,10 @@ 'objectid' => $newhighscore->id, 'context' => $context, 'courseid' => $course->id, + 'other' => array( + 'lessonid' => $lesson->id, + 'nickname' => $newhighscore->nickname + ) )); $event->trigger(); diff --git a/mod/lesson/tests/events_test.php b/mod/lesson/tests/events_test.php index 820ebedd485c5..dda3ee935d793 100644 --- a/mod/lesson/tests/events_test.php +++ b/mod/lesson/tests/events_test.php @@ -105,7 +105,11 @@ public function test_highscore_added() { $event = \mod_lesson\event\highscore_added::create(array( 'objectid' => $newhighscore->id, 'context' => context_module::instance($this->lesson->properties()->cmid), - 'courseid' => $this->course->id + 'courseid' => $this->course->id, + 'other' => array( + 'lessonid' => $this->lesson->id, + 'nickname' => 'noob' + ) )); // Trigger and capture the event.