Skip to content

Commit

Permalink
MDL-40063 mod_quiz: replaced 'attempt' 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 45e1e3b commit a2caf56
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
12 changes: 12 additions & 0 deletions mod/quiz/classes/event/attempt_started.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ protected function get_legacy_eventdata() {
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.
*
Expand Down
31 changes: 18 additions & 13 deletions mod/quiz/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
16 changes: 16 additions & 0 deletions mod/quiz/tests/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,22 @@ public function test_attempt_started() {
$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);
}

/**
Expand Down

0 comments on commit a2caf56

Please sign in to comment.