MDL-40063 mod_quiz: replaced 'attempt' add_to_log call with an event
This commit is contained in:
@@ -100,6 +100,18 @@ class attempt_started extends \core\event\base {
|
||||
return $legacyeventdata;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the legacy event log data.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_legacy_logdata() {
|
||||
$attempt = $this->get_record_snapshot('quiz_attempts', $this->objectid);
|
||||
|
||||
return array($this->courseid, 'quiz', 'attempt', 'review.php?attempt=' . $this->objectid,
|
||||
$attempt->quiz, $this->contextinstanceid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom validation.
|
||||
*
|
||||
|
||||
+18
-13
@@ -289,24 +289,29 @@ function quiz_attempt_save_started($quizobj, $quba, $attempt) {
|
||||
question_engine::save_questions_usage_by_activity($quba);
|
||||
$attempt->uniqueid = $quba->get_id();
|
||||
$attempt->id = $DB->insert_record('quiz_attempts', $attempt);
|
||||
// Log the new attempt.
|
||||
|
||||
// Params used by the events below.
|
||||
$params = array(
|
||||
'objectid' => $attempt->id,
|
||||
'relateduserid' => $attempt->userid,
|
||||
'courseid' => $quizobj->get_courseid(),
|
||||
'context' => $quizobj->get_context()
|
||||
);
|
||||
// Decide which event we are using.
|
||||
if ($attempt->preview) {
|
||||
$params = array(
|
||||
'objectid' => $attempt->id,
|
||||
'relateduserid' => $attempt->userid,
|
||||
'courseid' => $quizobj->get_courseid(),
|
||||
'context' => $quizobj->get_context(),
|
||||
'other' => array(
|
||||
'quizid' => $quizobj->get_quizid()
|
||||
)
|
||||
$params['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());
|
||||
$event = \mod_quiz\event\attempt_started::create($params);
|
||||
|
||||
}
|
||||
|
||||
// Trigger the event.
|
||||
$event->add_record_snapshot('quiz', $quizobj->get_quiz());
|
||||
$event->trigger();
|
||||
|
||||
return $attempt;
|
||||
}
|
||||
|
||||
|
||||
@@ -222,6 +222,22 @@ class mod_quiz_events_testcase extends advanced_testcase {
|
||||
$this->assertEquals('quiz_attempt_started', $event->get_legacy_eventname());
|
||||
$this->assertEventLegacyData($legacydata, $event);
|
||||
$this->assertEventContextNotUsed($event);
|
||||
|
||||
// Create another attempt.
|
||||
$attempt = quiz_create_attempt($quizobj, 1, false, time(), false, 2);
|
||||
|
||||
// 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_started', $event);
|
||||
$this->assertEquals(context_module::instance($quizobj->get_cmid()), $event->get_context());
|
||||
$expected = array($quizobj->get_courseid(), 'quiz', 'attempt', 'review.php?attempt=' . $attempt->id,
|
||||
$quizobj->get_quizid(), $quizobj->get_cmid());
|
||||
$this->assertEventLegacyLogData($expected, $event);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user