From a2caf56dff2d5f80550778b32450caa6dbf1df3f Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 21 Nov 2013 17:08:58 -0800 Subject: [PATCH] MDL-40063 mod_quiz: replaced 'attempt' add_to_log call with an event --- mod/quiz/classes/event/attempt_started.php | 12 ++++++++++++ mod/quiz/locallib.php | 31 +++++++++++++++++------------- mod/quiz/tests/events_test.php | 16 +++++++++++++++ 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/mod/quiz/classes/event/attempt_started.php b/mod/quiz/classes/event/attempt_started.php index 69ba87d1668..fa641c9ea90 100644 --- a/mod/quiz/classes/event/attempt_started.php +++ b/mod/quiz/classes/event/attempt_started.php @@ -101,6 +101,18 @@ class attempt_started extends \core\event\base { } /** + * Return the legacy event log data. + * + * @return array + */ + protected function get_legacy_logdata() { + $attempt = $this->get_record_snapshot('quiz_attempts', $this->objectid); + + return array($this->courseid, 'quiz', 'attempt', 'review.php?attempt=' . $this->objectid, + $attempt->quiz, $this->contextinstanceid); + } + + /** * Custom validation. * * @throws \coding_exception diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index a02b04b3f00..47eb0bc6f4e 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -289,24 +289,29 @@ function quiz_attempt_save_started($quizobj, $quba, $attempt) { question_engine::save_questions_usage_by_activity($quba); $attempt->uniqueid = $quba->get_id(); $attempt->id = $DB->insert_record('quiz_attempts', $attempt); - // Log the new attempt. + + // Params used by the events below. + $params = array( + 'objectid' => $attempt->id, + 'relateduserid' => $attempt->userid, + 'courseid' => $quizobj->get_courseid(), + 'context' => $quizobj->get_context() + ); + // Decide which event we are using. if ($attempt->preview) { - $params = array( - 'objectid' => $attempt->id, - 'relateduserid' => $attempt->userid, - 'courseid' => $quizobj->get_courseid(), - 'context' => $quizobj->get_context(), - 'other' => array( - 'quizid' => $quizobj->get_quizid() - ) + $params['other'] = array( + 'quizid' => $quizobj->get_quizid() ); $event = \mod_quiz\event\attempt_preview_started::create($params); - $event->add_record_snapshot('quiz', $quizobj->get_quiz()); - $event->trigger(); } else { - add_to_log($quizobj->get_courseid(), 'quiz', 'attempt', 'review.php?attempt='.$attempt->id, - $quizobj->get_quizid(), $quizobj->get_cmid()); + $event = \mod_quiz\event\attempt_started::create($params); + } + + // Trigger the event. + $event->add_record_snapshot('quiz', $quizobj->get_quiz()); + $event->trigger(); + return $attempt; } diff --git a/mod/quiz/tests/events_test.php b/mod/quiz/tests/events_test.php index 0479b0b0120..c1138e2ae27 100644 --- a/mod/quiz/tests/events_test.php +++ b/mod/quiz/tests/events_test.php @@ -222,6 +222,22 @@ class mod_quiz_events_testcase extends advanced_testcase { $this->assertEquals('quiz_attempt_started', $event->get_legacy_eventname()); $this->assertEventLegacyData($legacydata, $event); $this->assertEventContextNotUsed($event); + + // Create another attempt. + $attempt = quiz_create_attempt($quizobj, 1, false, time(), false, 2); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + quiz_attempt_save_started($quizobj, $quba, $attempt); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the event data is valid. + $this->assertInstanceOf('\mod_quiz\event\attempt_started', $event); + $this->assertEquals(context_module::instance($quizobj->get_cmid()), $event->get_context()); + $expected = array($quizobj->get_courseid(), 'quiz', 'attempt', 'review.php?attempt=' . $attempt->id, + $quizobj->get_quizid(), $quizobj->get_cmid()); + $this->assertEventLegacyLogData($expected, $event); } /** -- 2.11.4.GIT