]> git.mjollnir.org Git - moodle.git/commitdiff
Make a library function for deleting quiz attempts, rather than having duplicated...
authortjhunt <tjhunt>
Mon, 18 Jun 2007 16:19:00 +0000 (16:19 +0000)
committertjhunt <tjhunt>
Mon, 18 Jun 2007 16:19:00 +0000 (16:19 +0000)
mod/quiz/attempt.php
mod/quiz/locallib.php
mod/quiz/report/overview/report.php

index 8ad6deb9623eb432e57bb8126c4e314f496a207e..a3247b7f91b72f606e575dcc2e84afb41d033c4f 100644 (file)
 
     $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;
index 85ce51df08c264989bd5f39f0f17d6209d425fee..ff565c1b1d4072a95438a43bf1a5f817ed95b3c0 100644 (file)
@@ -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 ////////////////////////////////
 
 /**
index ac601b5df150c1850460abb5c4f646b6f73ab903..670d1ff20cb63fd77384e259733a5d321d0c1f7b 100644 (file)
@@ -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;
         }