Skip to content

Commit

Permalink
MDL-52014 quiz: do not log preview attempt deletions
Browse files Browse the repository at this point in the history
This happens automatically by the system.
  • Loading branch information
danpoltawski committed Nov 2, 2015
1 parent 821ab27 commit 5294052
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
26 changes: 14 additions & 12 deletions mod/quiz/locallib.php
Expand Up @@ -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
Expand Down
24 changes: 22 additions & 2 deletions mod/quiz/tests/events_test.php
Expand Up @@ -38,7 +38,12 @@
*/
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);

Expand Down Expand Up @@ -75,7 +80,7 @@ protected function prepare_quiz_data() {
$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);

Expand Down Expand Up @@ -283,6 +288,21 @@ public function test_attempt_deleted() {
$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.
*
Expand Down

0 comments on commit 5294052

Please sign in to comment.