diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index 7ad3365cf79..ba54a2e0862 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -362,18 +362,20 @@ function quiz_delete_attempt($attempt, $quiz) { question_engine::delete_questions_usage_by_activity($attempt->uniqueid); $DB->delete_records('quiz_attempts', array('id' => $attempt->id)); - // Log the deletion of the attempt. - $params = array( - 'objectid' => $attempt->id, - 'relateduserid' => $attempt->userid, - 'context' => context_module::instance($quiz->cmid), - 'other' => array( - 'quizid' => $quiz->id - ) - ); - $event = \mod_quiz\event\attempt_deleted::create($params); - $event->add_record_snapshot('quiz_attempts', $attempt); - $event->trigger(); + // Log the deletion of the attempt if not a preview. + if (!$attempt->preview) { + $params = array( + 'objectid' => $attempt->id, + 'relateduserid' => $attempt->userid, + 'context' => context_module::instance($quiz->cmid), + 'other' => array( + 'quizid' => $quiz->id + ) + ); + $event = \mod_quiz\event\attempt_deleted::create($params); + $event->add_record_snapshot('quiz_attempts', $attempt); + $event->trigger(); + } // Search quiz_attempts for other instances by this user. // If none, then delete record for this quiz, this user from quiz_grades diff --git a/mod/quiz/tests/events_test.php b/mod/quiz/tests/events_test.php index 59d8139042f..ca6bfe70bde 100644 --- a/mod/quiz/tests/events_test.php +++ b/mod/quiz/tests/events_test.php @@ -38,7 +38,12 @@ require_once($CFG->dirroot . '/mod/quiz/attemptlib.php'); */ class mod_quiz_events_testcase extends advanced_testcase { - protected function prepare_quiz_data() { + /** + * Setup some convenience test data with a single attempt. + * + * @param bool $ispreview Make the attempt a preview attempt when true. + */ + protected function prepare_quiz_data($ispreview = false) { $this->resetAfterTest(true); @@ -75,7 +80,7 @@ class mod_quiz_events_testcase extends advanced_testcase { $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour); $timenow = time(); - $attempt = quiz_create_attempt($quizobj, 1, false, $timenow); + $attempt = quiz_create_attempt($quizobj, 1, false, $timenow, $ispreview); quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow); quiz_attempt_save_started($quizobj, $quba, $attempt); @@ -283,6 +288,21 @@ class mod_quiz_events_testcase extends advanced_testcase { $this->assertEventContextNotUsed($event); } + /** + * Test that preview attempt deletions are not logged. + */ + public function test_preview_attempt_deleted() { + // Create quiz with preview attempt. + list($quizobj, $quba, $previewattempt) = $this->prepare_quiz_data(true); + + // Delete a preview attempt, capturing events. + $sink = $this->redirectEvents(); + quiz_delete_attempt($previewattempt, $quizobj->get_quiz()); + + // Verify that no events were generated. + $this->assertEmpty($sink->get_events()); + } + /** * Test the report viewed event. *