MDL-40063 mod_quiz: replaced 'preview' add_to_log call with an event
authorMark Nelson <markn@moodle.com>
Fri, 22 Nov 2013 01:03:17 +0000 (17:03 -0800)
committerMark Nelson <markn@moodle.com>
Fri, 11 Apr 2014 03:41:54 +0000 (20:41 -0700)
mod/quiz/classes/event/attempt_preview_started.php [new file with mode: 0644]
mod/quiz/lang/en/quiz.php
mod/quiz/locallib.php
mod/quiz/tests/events_test.php

diff --git a/mod/quiz/classes/event/attempt_preview_started.php b/mod/quiz/classes/event/attempt_preview_started.php
new file mode 100644 (file)
index 0000000..fc352a2
--- /dev/null
@@ -0,0 +1,101 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * 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 <markn@moodle.com>
+ * @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.');
+        }
+    }
+}
index 035affe..001ae8c 100644 (file)
@@ -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';
index 14182b0..a02b04b 100644 (file)
@@ -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());
index 6ac30af..0479b0b 100644 (file)
@@ -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);
+    }
 }