diff --git a/mod/quiz/classes/event/attempt_preview_started.php b/mod/quiz/classes/event/attempt_preview_started.php new file mode 100644 index 00000000000..fc352a2f61f --- /dev/null +++ b/mod/quiz/classes/event/attempt_preview_started.php @@ -0,0 +1,101 @@ +. + +/** + * Attempt preview started event class. + * + * @property-read array $other { + * Extra information about event. + * + * - int quizid: the id of the quiz. + * } + * + * @package mod_quiz + * @since Moodle 2.7 + * @copyright 2014 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace mod_quiz\event; + +defined('MOODLE_INTERNAL') || die(); + +class attempt_preview_started extends \core\event\base { + + /** + * Init method. + */ + protected function init() { + $this->data['objecttable'] = 'quiz_attempts'; + $this->data['crud'] = 'r'; + $this->data['edulevel'] = self::LEVEL_TEACHING; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventattemptpreviewstarted', 'mod_quiz'); + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + return 'A quiz attempt with the id of ' . $this->objectid . ' belonging to the quiz with the id ' . $this->other['quizid'] . + ' was previewed by the user with the id ' . $this->relateduserid; + } + + /** + * Returns relevant URL. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/mod/quiz/view.php', array('id' => $this->contextinstanceid)); + } + + /** + * Return the legacy event log data. + * + * @return array + */ + protected function get_legacy_logdata() { + return array($this->courseid, 'quiz', 'preview', 'view.php?id=' . $this->contextinstanceid, + $this->other['quizid'], $this->contextinstanceid); + } + + /** + * Custom validation. + * + * @throws \coding_exception + * @return void + */ + protected function validate_data() { + parent::validate_data(); + + if (!isset($this->relateduserid)) { + throw new \coding_exception('The \'relateduserid\' must be set.'); + } + + if (!isset($this->other['quizid'])) { + throw new \coding_exception('The \'quizid\' must be set in other.'); + } + } +} diff --git a/mod/quiz/lang/en/quiz.php b/mod/quiz/lang/en/quiz.php index 035affeba47..001ae8c82f5 100644 --- a/mod/quiz/lang/en/quiz.php +++ b/mod/quiz/lang/en/quiz.php @@ -306,6 +306,7 @@ $string['errorunexpectedevent'] = 'Unexpected event code {$a->event} found for q $string['essay'] = 'Essay'; $string['essayquestions'] = 'Questions'; $string['eventattemptdeleted'] = 'Quiz attempt deleted'; +$string['eventattemptpreviewstarted'] = 'Quiz attempt preview started'; $string['eventattemptreviewed'] = 'Quiz attempt reviewed'; $string['eventattemptsummaryviewed'] = 'Quiz attempt summary viewed'; $string['eventattemptviewed'] = 'Quiz attempt viewed'; diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index 14182b010cd..a02b04b3f00 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -291,8 +291,18 @@ function quiz_attempt_save_started($quizobj, $quba, $attempt) { $attempt->id = $DB->insert_record('quiz_attempts', $attempt); // Log the new attempt. if ($attempt->preview) { - add_to_log($quizobj->get_courseid(), 'quiz', 'preview', 'view.php?id='.$quizobj->get_cmid(), - $quizobj->get_quizid(), $quizobj->get_cmid()); + $params = array( + 'objectid' => $attempt->id, + 'relateduserid' => $attempt->userid, + 'courseid' => $quizobj->get_courseid(), + 'context' => $quizobj->get_context(), + '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()); diff --git a/mod/quiz/tests/events_test.php b/mod/quiz/tests/events_test.php index 6ac30aff680..0479b0b0120 100644 --- a/mod/quiz/tests/events_test.php +++ b/mod/quiz/tests/events_test.php @@ -569,4 +569,29 @@ class mod_quiz_events_testcase extends advanced_testcase { $this->assertEventLegacyLogData($expected, $event); $this->assertEventContextNotUsed($event); } + + /** + * Test the attempt previewed event. + */ + public function test_attempt_preview_started() { + list($quizobj, $quba, $attempt) = $this->prepare_quiz_data(); + + // We want to preview this attempt. + $attempt = quiz_create_attempt($quizobj, 1, false, time(), false, 2); + $attempt->preview = 1; + + // 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_preview_started', $event); + $this->assertEquals(context_module::instance($quizobj->get_cmid()), $event->get_context()); + $expected = array($quizobj->get_courseid(), 'quiz', 'preview', 'view.php?id=' . $quizobj->get_cmid(), + $quizobj->get_quizid(), $quizobj->get_cmid()); + $this->assertEventLegacyLogData($expected, $event); + $this->assertEventContextNotUsed($event); + } }