Skip to content

Commit

Permalink
MDL-40063 mod_quiz: replaced 'preview' add_to_log call with an event
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Apr 11, 2014
1 parent e011470 commit 45e1e3b
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 2 deletions.
101 changes: 101 additions & 0 deletions mod/quiz/classes/event/attempt_preview_started.php
@@ -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.');
}
}
}
1 change: 1 addition & 0 deletions mod/quiz/lang/en/quiz.php
Expand Up @@ -306,6 +306,7 @@
$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';
Expand Down
14 changes: 12 additions & 2 deletions mod/quiz/locallib.php
Expand Up @@ -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());
Expand Down
25 changes: 25 additions & 0 deletions mod/quiz/tests/events_test.php
Expand Up @@ -569,4 +569,29 @@ public function test_attempt_viewed() {
$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);
}
}

0 comments on commit 45e1e3b

Please sign in to comment.