]> git.mjollnir.org Git - moodle.git/commitdiff
Some improvements to delete_question()
authorgustav_delius <gustav_delius>
Mon, 20 Mar 2006 23:04:22 +0000 (23:04 +0000)
committergustav_delius <gustav_delius>
Mon, 20 Mar 2006 23:04:22 +0000 (23:04 +0000)
13 files changed:
lib/questionlib.php
mod/quiz/lib.php
question/questiontypes/calculated/questiontype.php
question/questiontypes/essay/questiontype.php
question/questiontypes/match/questiontype.php
question/questiontypes/multianswer/questiontype.php
question/questiontypes/multichoice/questiontype.php
question/questiontypes/numerical/questiontype.php
question/questiontypes/questiontype.php
question/questiontypes/randomsamatch/questiontype.php
question/questiontypes/rqp/questiontype.php
question/questiontypes/shortanswer/questiontype.php
question/questiontypes/truefalse/questiontype.php

index 2966080af73ebbb78d0eca07a39309444d3969c0..ed629b1d420e026dd6eae20634f59afa0059a0d8 100644 (file)
@@ -173,39 +173,61 @@ class cmoptions {
 }
 
 
-
 /// FUNCTIONS //////////////////////////////////////////////////////
 
+/**
+ * Returns an array with all the course modules that use this question
+ *
+ * @param object $questionid 
+ */
+function question_whereused($questionid) {
+    $instances = array();
+    $modules = get_records('modules');
+    foreach ($modules as $module) {
+        $fn = $module->name.'_question_whereused';
+        if (function_exists($fn)) {
+            $instances[] = $fn($questionid);
+        }
+    }
+    return $instances;
+}
 
 /**
  * Deletes question and all associated data from the database
  *
- * TODO: remove quiz dependence
- *
+ * It will not delete a question if it is used by an activity module
  * @param object $question  The question being deleted
  */
-function delete_question($question) {
+function delete_question($questionid) {
     global $QTYPES;
+    
+    // Do not delete a question if it is used by an activity module
+    if (count(question_whereused($questionid))) {
+        return;
+    }
+
+    // delete questiontype-specific data
     if (isset($QTYPES[$question->qtype])) {
-        $QTYPES[$question->qtype]->delete_question($question);
-    } else {echo 'qtype: '.$question->qtype.'<br />';}
-    delete_records("question_answers", "question", $question->id);
-    delete_records("question_states", "question", $question->id);
-    delete_records("question_sessions", "questionid", $question->id);
-    if ($newversions = get_records('quiz_question_versions', 'oldquestion', $question->id)) {
-        foreach ($newversions as $newversion) {
-            $newquestion = get_record('question', 'id', $newversion->newquestion);
-            delete_question($newquestion);
-        }
-        delete_records("quiz_question_versions", "oldquestion", $question->id);
+        $QTYPES[$question->qtype]->delete_question($questionid);
     }
-    delete_records("quiz_question_versions", "newquestion", $question->id);
-    if ($children = get_records('question', 'parent', $question->id)) {
+
+    // 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);
+    delete_records("question_states", "question", $questionid);
+    delete_records("question_sessions", "questionid", $questionid);
+
+    // Now recursively delete all child questions
+    if ($children = get_records('question', 'parent', $questionid)) {
         foreach ($children as $child) {
-            delete_question($child);
+            delete_question($child->id);
         }
     }
-    return true;
+    
+    // Finally delete the question record itself
+    delete_records('question', 'id', $questionid);
+
+    return;
 }
 
 /**
index de7a4d221835beeb2a8b349d7b3e6ae55f48e510..d7932ef6f31c1b72b5a7ac59e66e953da28098c0 100644 (file)
@@ -11,7 +11,6 @@
 */
 
 require_once($CFG->libdir.'/pagelib.php');
-require_once($CFG->dirroot.'/mod/quiz/constants.php');
 
 /// CONSTANTS ///////////////////////////////////////////////////////////////////
 
@@ -377,7 +376,7 @@ function quiz_delete_course($course, $feedback=true) {
                 //deleting questions
                 if ($questions = get_records("question", "category", $category->id)) {
                     foreach ($questions as $question) {
-                        delete_question($question);
+                        delete_question($question->id);
                     }
                     delete_records("question", "category", $category->id);
                 }
index ca9374c00bb9342d39cbc0007b4e2d627aa8318a..bc00bd85a3a48fbb9638b87bc30088602d6a2dad 100644 (file)
@@ -203,11 +203,16 @@ class question_calculated_qtype extends question_dataset_dependent_questiontype
     * @return boolean Success/Failure
     * @param object $question  The question being deleted
     */
-    function delete_question($question) {
-        delete_records("question_calculated", "question", $question->id);
-        delete_records("question_numerical_units", "question", $question->id);
-        delete_records("question_datasets", "question", $question->id);
-        //TODO: delete entries from the question_dataset_items and question_dataset_definitions tables
+    function delete_question($questionid) {
+        delete_records("question_calculated", "question", $questionid);
+        delete_records("question_numerical_units", "question", $questionid);
+        if ($datasets = get_records('question_datasets', 'question', $questionid)) {
+            foreach ($datasets as $dataset) {
+                delete_records('question_dataset_definitions', 'id', $datasets->datasetdefinition);
+                delete_records('question_dataset_items', 'definition', $datasets->datasetdefinition);
+            }
+        }
+        delete_records("question_datasets", "question", $questionid);
         return true;
     }
 
index 8deeee2f10052c85d2f88415cae9ecd473f17d50..8b86cfe167b1d93e05b397178a047a23ad1c091a 100644 (file)
@@ -74,8 +74,8 @@ class question_essay_qtype extends default_questiontype {
     * @return boolean Success/Failure
     * @param object $question  The question being deleted
     */
-    function delete_question($question) {
-        delete_records("question_essay", "question", $question->id);
+    function delete_question($questionid) {
+        delete_records("question_essay", "question", $questionid);
         return true;
     }
 
index 8ffdfbf7888e8a4ab62efd010260d7a385236a5b..f884b1f8a79f554b76b883fae28136ab791432d2 100644 (file)
@@ -108,9 +108,9 @@ class question_match_qtype extends default_questiontype {
     * @return boolean Success/Failure
     * @param integer $question->id
     */
-    function delete_question($question) {
-        delete_records("question_match", "question", $question->id);
-        delete_records("question_match_sub", "question", $question->id);
+    function delete_question($questionid) {
+        delete_records("question_match", "question", $questionid);
+        delete_records("question_match_sub", "question", $questionid);
         return true;
     }
 
index 1dd24a9d80f5a86ca597300f094ffc1e55d08b7d..be91d5b327a1127b3a25732e2d1c37d6671bbb4d 100644 (file)
@@ -171,8 +171,8 @@ class quiz_embedded_cloze_qtype extends default_questiontype {
     * @return boolean Success/Failure
     * @param object $question  The question being deleted
     */
-    function delete_question($question) {
-        delete_records("question_multianswer", "question", $question->id);
+    function delete_question($questionid) {
+        delete_records("question_multianswer", "question", $questionid);
         return true;
     }
 
index 17b4218e7664cd5edd2e29db7875e099c6a914fc..6beed5c123976b60d2a0f45f174c77a8903c38e9 100644 (file)
@@ -144,8 +144,8 @@ class question_multichoice_qtype extends default_questiontype {
     * @return boolean Success/Failure
     * @param object $question  The question being deleted
     */
-    function delete_question($question) {
-        delete_records("question_multichoice", "question", $question->id);
+    function delete_question($questionid) {
+        delete_records("question_multichoice", "question", $questionid);
         return true;
     }
 
index 1675fac934f64c2b38360866e74ac1ba6d8d0d1e..fadd95c3c24ac7420ff0db5e5cc40c995e5bd1cf 100644 (file)
@@ -228,9 +228,9 @@ class question_numerical_qtype extends question_shortanswer_qtype {
     * @return boolean Success/Failure
     * @param object $question  The question being deleted
     */
-    function delete_question($question) {
-        delete_records("question_numerical", "question", $question->id);
-        delete_records("question_numerical_units", "question", $question->id);
+    function delete_question($questionid) {
+        delete_records("question_numerical", "question", $questionid);
+        delete_records("question_numerical_units", "question", $questionid);
         return true;
     }
 
index e833a60306a07a53c6b61b555227e785b70d1ffc..ff68a13a8a94d08aa7e8dede0cfebd5fe4f67e8e 100644 (file)
@@ -183,7 +183,7 @@ class default_questiontype {
     * @return boolean Success/Failure
     * @param object $question  The question being deleted
     */
-    function delete_question($question) {
+    function delete_question($questionid) {
         /// The default question type does not have any tables of its own
         // therefore there is nothing to delete
 
index c034cc1ea3d43a14c2c0b602f06301cdeafb74eb..2009d73568d7b0b32bae02cb730c20e44081f14f 100644 (file)
@@ -61,8 +61,8 @@ class question_randomsamatch_qtype extends question_match_qtype {
     * @return boolean Success/Failure
     * @param object $question  The question being deleted
     */
-    function delete_question($question) {
-        delete_records("question_randomsamatch", "question", $question->id);
+    function delete_question($questionid) {
+        delete_records("question_randomsamatch", "question", $questionid);
         return true;
     }
 
index 4244cd574d6a9dec2e0ab1db501ea718e25c4a08..bb55188366b32924d113cc3738ff36e56e09bec0 100644 (file)
@@ -129,9 +129,13 @@ class question_rqp_qtype extends default_questiontype {
     * @return boolean Success/Failure
     * @param object $question  The question being deleted
     */
-    function delete_question($question) {
+    function delete_question($questionid) {
         delete_records("question_rqp", "question", $questionid);
-        //TODO: delete also the states from question_rqp_states
+        if ($states = get_records('question_states', 'question', $questionid)) {
+            foreach ($states as $state) {
+                delete_records('question_rqp_states', 'stateid', $state->id);
+            }
+        }
         return true;
     }
 
index 6c9628d61bcbd45504e861a3e00fdd45c01fc321..62f39ad03938e70ca52b39272fea005c2e7ca8ce 100644 (file)
@@ -112,9 +112,8 @@ class question_shortanswer_qtype extends default_questiontype {
     * @return boolean Success/Failure
     * @param object $question  The question being deleted
     */
-    function delete_question($question) {
-        delete_records("question_shortanswer", "question", $question->id);
-        //TODO: delete also the states from question_rqp_states
+    function delete_question($questionid) {
+        delete_records("question_shortanswer", "question", $questionid);
         return true;
     }
 
index 57ffb74015b9f4aba340a0758976796619858a2a..17725f9c53791dd026e2e095a5f8bb0777a52633 100644 (file)
@@ -115,8 +115,8 @@ class question_truefalse_qtype extends default_questiontype {
     * @return boolean Success/Failure
     * @param object $question  The question being deleted
     */
-    function delete_question($question) {
-        delete_records("question_truefalse", "question", $question->id);
+    function delete_question($questionid) {
+        delete_records("question_truefalse", "question", $questionid);
         return true;
     }