MDL-20636 Cannot really delete questions that were only used in previews #196
authorTim Hunt <T.J.Hunt@open.ac.uk>
Mon, 7 Mar 2011 16:23:11 +0000 (16:23 +0000)
committerTim Hunt <T.J.Hunt@open.ac.uk>
Mon, 7 Mar 2011 16:23:11 +0000 (16:23 +0000)
mod/quiz/lib.php
question/engine/datalib.php
question/engine/lib.php

index 04252df..6ee65e8 100644 (file)
@@ -1162,10 +1162,13 @@ function quiz_get_post_actions() {
  * @return bool whether any of these questions are used by any instance of this module.
  */
 function quiz_questions_in_use($questionids) {
  * @return bool whether any of these questions are used by any instance of this module.
  */
 function quiz_questions_in_use($questionids) {
-    global $DB;
+    global $DB, $CFG;
+    require_once($CFG->libdir . '/questionlib.php');
     list($test, $params) = $DB->get_in_or_equal($questionids);
     return $DB->record_exists_select('quiz_question_instances',
     list($test, $params) = $DB->get_in_or_equal($questionids);
     return $DB->record_exists_select('quiz_question_instances',
-            'question ' . $test, $params);
+            'question ' . $test, $params) || question_engine::questions_in_use(
+            $questionids, new qubaid_join($CFG->prefix . 'quiz_attempts quiza',
+            'quiza.uniqueid', 'quiza.preview = 0'));
 }
 
 /**
 }
 
 /**
index a4af365..29a7052 100644 (file)
@@ -804,12 +804,15 @@ ORDER BY
 
     /**
      * @param array $questionids of question ids.
 
     /**
      * @param array $questionids of question ids.
-     * @return bool whether any of these questions are being used by the question engine.
+     * @param qubaid_condition $qubaids ids of the usages to consider.
+     * @return boolean whether any of these questions are being used by any of
+     *      those usages.
      */
      */
-    public function questions_in_use(array $questionids) {
+    public function questions_in_use(array $questionids, qubaid_condition $qubaids) {
         list($test, $params) = $this->db->get_in_or_equal($questionids);
         return $this->db->record_exists_select('question_attempts',
         list($test, $params) = $this->db->get_in_or_equal($questionids);
         return $this->db->record_exists_select('question_attempts',
-                'questionid ' . $test, $params);
+                'questionid ' . $test . ' AND questionusageid ' .
+                $qubaids->usage_id_in(), $params + $qubaids->usage_id_in_params());
     }
 }
 
     }
 }
 
index 39bc76d..c70d39d 100644 (file)
@@ -128,11 +128,16 @@ abstract class question_engine {
 
     /**
      * @param array $questionids of question ids.
 
     /**
      * @param array $questionids of question ids.
-     * @return bool whether any of these questions are being used by the question engine.
+     * @param qubaid_condition $qubaids ids of the usages to consider.
+     * @return boolean whether any of these questions are being used by any of
+     *      those usages.
      */
      */
-    public static function questions_in_use(array $questionids) {
+    public static function questions_in_use(array $questionids, qubaid_condition $qubaids = null) {
+        if (is_null($qubaids)) {
+            return false;
+        }
         $dm = new question_engine_data_mapper();
         $dm = new question_engine_data_mapper();
-        return $dm->questions_in_use($questionids);
+        return $dm->questions_in_use($questionids, $qubaids);
     }
 
     /**
     }
 
     /**