From ff51d64621b6ccf8aed95998745f8b00a4a16bd7 Mon Sep 17 00:00:00 2001 From: tjhunt Date: Mon, 18 Jun 2007 16:19:00 +0000 Subject: [PATCH] Make a library function for deleting quiz attempts, rather than having duplicated code. Merged from OU Moodle. --- mod/quiz/attempt.php | 16 +++++---------- mod/quiz/locallib.php | 31 +++++++++++++++++++++++++++++ mod/quiz/report/overview/report.php | 18 ++--------------- 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/mod/quiz/attempt.php b/mod/quiz/attempt.php index 8ad6deb962..a3247b7f91 100644 --- a/mod/quiz/attempt.php +++ b/mod/quiz/attempt.php @@ -172,17 +172,11 @@ $newattempt = false; if (!$attempt) { - // Check if this is a preview request from a teacher - // in which case the previous previews should be deleted - if ($ispreviewing) { - if ($oldattempts = get_records_select('quiz_attempts', "quiz = '$quiz->id' - AND userid = '$USER->id'")) { - delete_records('quiz_attempts', 'quiz', $quiz->id, 'userid', $USER->id); - delete_records('quiz_grades', 'quiz', $quiz->id, 'userid', $USER->id); - foreach ($oldattempts as $oldattempt) { - // there should only be one but we loop just in case - delete_attempt($oldattempt->uniqueid); - } + // Delete any previous preview attempts belonging to this user. + if ($oldattempts = get_records_select('quiz_attempts', "quiz = '$quiz->id' + AND userid = '$USER->id'")) { + foreach ($oldattempts as $oldattempt) { + quiz_delete_attempt($oldattempt, $quiz); } } $newattempt = true; diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index 85ce51df08..ff565c1b1d 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -117,6 +117,37 @@ function quiz_get_user_attempts($quizid, $userid, $status = 'finished') { } } +/** + * Delete a quiz attempt. + */ +function quiz_delete_attempt($attempt, $quiz) { + if (is_numeric($attempt)) { + if (!$attempt = get_record('quiz_attempts', 'id', $attempt)) { + return; + } + } + + if ($attempt->quiz != $quiz->id) { + debugging("Trying to delete attempt $attempt->id which belongs to quiz $attempt->quiz " . + "but was passed quiz $quiz->id."); + return; + } + + delete_records('quiz_attempts', 'id', $attempt->id); + delete_attempt($attempt->uniqueid); + + // Search quiz_attempts for other instances by this user. + // If none, then delete record for this quiz, this user from quiz_grades + // else recalculate best grade + + $userid = $attempt->userid; + if (!record_exists('quiz_attempts', 'userid', $userid, 'quiz', $quiz->id)) { + delete_records('quiz_grades', 'userid', $userid,'quiz', $quiz->id); + } else { + quiz_save_best_grade($quiz, $userid); + } +} + /// Functions to do with quiz layout and pages //////////////////////////////// /** diff --git a/mod/quiz/report/overview/report.php b/mod/quiz/report/overview/report.php index ac601b5df1..670d1ff20c 100644 --- a/mod/quiz/report/overview/report.php +++ b/mod/quiz/report/overview/report.php @@ -36,22 +36,8 @@ class quiz_report extends quiz_default_report { $attemptids = optional_param('attemptid', array(), PARAM_INT); foreach($attemptids as $attemptid) { - if ($attemptid && $todelete = get_record('quiz_attempts', 'id', $attemptid)) { - delete_records('quiz_attempts', 'id', $attemptid); - delete_attempt($todelete->uniqueid); - - // Search quiz_attempts for other instances by this user. - // If none, then delete record for this quiz, this user from quiz_grades - // else recalculate best grade - - $userid = $todelete->userid; - if (!record_exists('quiz_attempts', 'userid', $userid, 'quiz', $quiz->id)) { - delete_records('quiz_grades', 'userid', $userid,'quiz', $quiz->id); - } else { - quiz_save_best_grade($quiz, $userid); - } - quiz_update_grades($quiz, $userid); - } + quiz_delete_attempt($attemptid, $quiz); + quiz_update_grades($quiz, $USER->id); } break; } -- 2.39.5