From 8539411823562be05ec0496ff78967c3a0937264 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 17 Oct 2013 18:18:02 +0800 Subject: [PATCH] MDL-40054 mod_lesson: replaced 'update highscores' add_to_log call with an event --- mod/lesson/classes/event/highscore_added.php | 81 ++++++++++++++++++++++++++++ mod/lesson/highscores.php | 13 +++-- mod/lesson/lang/en/lesson.php | 1 + mod/lesson/tests/events_test.php | 39 ++++++++++++++ 4 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 mod/lesson/classes/event/highscore_added.php diff --git a/mod/lesson/classes/event/highscore_added.php b/mod/lesson/classes/event/highscore_added.php new file mode 100644 index 00000000000..b2204dbe191 --- /dev/null +++ b/mod/lesson/classes/event/highscore_added.php @@ -0,0 +1,81 @@ +. + +/** + * Event to be triggered when a highscore is added. + * + * @package mod_lesson + * @copyright 2013 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. + */ + +namespace mod_lesson\event; + +defined('MOODLE_INTERNAL') || die(); + +class highscore_added extends \core\event\base { + + /** + * Set basic properties for the event. + */ + protected function init() { + $this->data['objecttable'] = 'lesson_high_scores'; + $this->data['crud'] = 'c'; + $this->data['level'] = self::LEVEL_PARTICIPATING; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventhighscoreadded', 'mod_lesson'); + } + + /** + * Get URL related to the action. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/mod/lesson/highscores.php', array('id' => $this->context->instanceid)); + } + + /** + * Returns non-localised event description with id's for admin use only. + * + * @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; + } + + /** + * Replace add_to_log() statement. + * + * @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->context->instanceid, + $highscore->nickname, $this->context->instanceid); + } +} diff --git a/mod/lesson/highscores.php b/mod/lesson/highscores.php index c75dbbff47f..563996a5212 100644 --- a/mod/lesson/highscores.php +++ b/mod/lesson/highscores.php @@ -143,10 +143,15 @@ switch ($mode) { $newhighscore->gradeid = $newgrade->id; $newhighscore->nickname = $name; - $DB->insert_record('lesson_high_scores', $newhighscore); - - // Log it - add_to_log($course->id, 'lesson', 'update highscores', "highscores.php?id=$cm->id", $name, $cm->id); + $newhighscore->id = $DB->insert_record('lesson_high_scores', $newhighscore); + + // Trigger highscore updated event. + $event = \mod_lesson\event\highscore_added::create(array( + 'objectid' => $newhighscore->id, + 'context' => $context, + 'courseid' => $course->id, + )); + $event->trigger(); $lesson->add_message(get_string('postsuccess', 'lesson'), 'notifysuccess'); redirect("$CFG->wwwroot/mod/lesson/highscores.php?id=$cm->id&link=1"); diff --git a/mod/lesson/lang/en/lesson.php b/mod/lesson/lang/en/lesson.php index 2f52cb705e1..33eaec3d05f 100644 --- a/mod/lesson/lang/en/lesson.php +++ b/mod/lesson/lang/en/lesson.php @@ -170,6 +170,7 @@ $string['essayemailsubject'] = 'Your grade for {$a} question'; $string['essays'] = 'Essays'; $string['essayscore'] = 'Essay score'; $string['eventessayattemptviewed'] = 'Essay attempt viewed'; +$string['eventhighscoreadded'] = 'Highscore added'; $string['fileformat'] = 'File format'; $string['finish'] = 'Finish'; $string['firstanswershould'] = 'First answer should jump to the "Correct" page'; diff --git a/mod/lesson/tests/events_test.php b/mod/lesson/tests/events_test.php index d787d20ffb8..51d4305d14a 100644 --- a/mod/lesson/tests/events_test.php +++ b/mod/lesson/tests/events_test.php @@ -81,4 +81,43 @@ class mod_lesson_events_testcase extends advanced_testcase { '&mode=grade&attemptid=1', get_string('manualgrading', 'lesson'), $this->lesson->properties()->cmid); $this->assertEventLegacyLogData($expected, $event); } + + /** + * Test the highscore added event. + * + * There is no external API for adding a highscore, so the unit test will simply create + * and trigger the event and ensure the legacy log data is returned as expected. + */ + public function test_highscore_added() { + global $DB; + + // Create a highscore. + $newhighscore = new stdClass; + $newhighscore->lessonid = $this->lesson->id; + $newhighscore->userid = 3; + $newhighscore->gradeid = 70; + $newhighscore->nickname = 'noob'; + + $newhighscore->id = $DB->insert_record('lesson_high_scores', $newhighscore); + + // Create a highscore added event. + $event = \mod_lesson\event\highscore_added::create(array( + 'objectid' => $newhighscore->id, + 'context' => context_module::instance($this->lesson->properties()->cmid), + 'courseid' => $this->course->id + )); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $event->trigger(); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the event data is valid. + $this->assertInstanceOf('\mod_lesson\event\highscore_added', $event); + $this->assertEquals(context_module::instance($this->lesson->properties()->cmid), $event->get_context()); + $expected = array($this->course->id, 'lesson', 'update highscores', 'highscores.php?id=' . $this->lesson->properties()->cmid, + 'noob', $this->lesson->properties()->cmid); + $this->assertEventLegacyLogData($expected, $event); + } } -- 2.11.4.GIT