MDL-52014 quiz: do not log preview attempt deletions
authorDan Poltawski <dan@moodle.com>
Mon, 2 Nov 2015 16:15:47 +0000 (16:15 +0000)
committerDan Poltawski <dan@moodle.com>
Mon, 2 Nov 2015 16:17:22 +0000 (16:17 +0000)
This happens automatically by the system.

mod/quiz/locallib.php
mod/quiz/tests/events_test.php

index aa13741..424d689 100644 (file)
@@ -420,18 +420,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
index 59d8139..ca6bfe7 100644 (file)
@@ -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.
      *