]> git.mjollnir.org Git - moodle.git/commitdiff
Deleting attempts properly now
authorgustav_delius <gustav_delius>
Wed, 22 Mar 2006 18:27:28 +0000 (18:27 +0000)
committergustav_delius <gustav_delius>
Wed, 22 Mar 2006 18:27:28 +0000 (18:27 +0000)
lib/questionlib.php
mod/quiz/attempt.php
question/questiontypes/essay/questiontype.php
question/questiontypes/questiontype.php
question/questiontypes/rqp/questiontype.php

index 1d20529911090d58a10aa37b9ef7f76d8f2237a5..e59f87cd3242c3d9a7457772c62f695539ebf189 100644 (file)
@@ -317,6 +317,29 @@ function question_category_isused($categoryid, $recursive = false) {
     return false;
 }
 
+/**
+ * Deletes all data associated to an attempt from the database
+ *
+ * @param object $question  The question being deleted
+ */
+function delete_attempt($attemptid) {
+    global $QTYPES;
+
+    $states = get_records('question_states', 'attempt', $attemptid);
+    $stateslist = implode(',', array_keys($states));
+
+    // delete questiontype-specific data
+    foreach ($QTYPES as $qtype) {
+        $qtype->delete_states($stateslist);
+    }
+
+    // delete entries from all other question tables
+    // It is important that this is done only after calling the questiontype functions
+    delete_records("question_states", "attempt", $attemptid);
+    delete_records("question_sessions", "attemptid", $attemptid);
+
+    return;
+}
 
 /**
  * Deletes question and all associated data from the database
@@ -341,6 +364,14 @@ function delete_question($questionid) {
         echo "Question with id $questionid does not exist.<br />";
     }
 
+    $states = get_records('question_states', 'question', $questionid);
+    $stateslist = implode(',', array_keys($states));
+
+    // delete questiontype-specific data
+    foreach ($QTYPES as $qtype) {
+        $qtype->delete_states($stateslist);
+    }
+
     // delete entries from all other question tables
     // It is important that this is done only after calling the questiontype functions
     delete_records("question_answers", "question", $questionid);
index 4ead6d79d2990ac658fc685336df41da0b5f7d6f..28cc1493727797bf0bc901b087ebfc1781441d5d 100644 (file)
                 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
-                    // TODO: the following should become a function in questionlib.php which
-                    // really deletes all records associated to this attempt.
-                    delete_records('question_states', 'attempt', $oldattempt->uniqueid);
-                    delete_records('question_sessions', 'attemptid', $oldattempt->uniqueid);
+                    delete_attempt($oldattempt->uniqueid);
                 }
             }
         }
index 19fe7b4e8aed013264125e4065b186bfd17ed3c1..cc8ad8a2c1acf3e6cb92502b6e3de6947d31d3fa 100644 (file)
@@ -69,9 +69,18 @@ class question_essay_qtype extends default_questiontype {
     }
 
     /**
-    * Deletes question from the question-type specific tables
+    * Deletes states from the question-type specific tables
+    *
+    * @param string $stateslist  Comma separated list of state ids to be deleted
+    */
+    function delete_states($stateslist) {
+        delete_records_select("question_essay_states", "stateid IN ($stateslist)");
+        return true;
+    }
+
+    /**
+    * Deletes a question from the question-type specific tables
     *
-    * @return boolean Success/Failure
     * @param object $question  The question being deleted
     */
     function delete_question($questionid) {
index be65d50c1ce697c4c746a868b873131e6e660323..745b3ecf2ca1de2694f19f1f96f56eeac6ccf644 100644 (file)
@@ -178,7 +178,19 @@ class default_questiontype {
     }
 
     /**
-    * Deletes question from the question-type specific tables
+    * Deletes states from the question-type specific tables
+    *
+    * @param string $stateslist  Comma separated list of state ids to be deleted
+    */
+    function delete_states($stateslist) {
+        /// The default question type does not have any tables of its own
+        // therefore there is nothing to delete
+
+        return true;
+    }
+
+    /**
+    * Deletes a question from the question-type specific tables
     *
     * @return boolean Success/Failure
     * @param object $question  The question being deleted
index f60510368bc43406d825aa89b992f4a87996d6ea..3328b97d6e3ac5ad6895d7239d0e5f3f924e502d 100644 (file)
@@ -123,6 +123,16 @@ class question_rqp_qtype extends default_questiontype {
         return true;
     }
 
+    /**
+    * Deletes states from the question-type specific tables
+    *
+    * @param string $stateslist  Comma separated list of state ids to be deleted
+    */
+    function delete_states($stateslist) {
+        delete_records_select("question_rqp_states", "stateid IN ($stateslist)");
+        return true;
+    }
+
     /**
     * Deletes question from the question-type specific tables
     *
@@ -131,11 +141,6 @@ class question_rqp_qtype extends default_questiontype {
     */
     function delete_question($questionid) {
         delete_records("question_rqp", "question", $questionid);
-        if ($states = get_records('question_states', 'question', $questionid)) {
-            foreach ($states as $state) {
-                delete_records('question_rqp_states', 'stateid', $state->id);
-            }
-        }
         return true;
     }