]> git.mjollnir.org Git - moodle.git/commitdiff
shortanswer qtype: MDL-17706 use extra_question_fields mechanism to save on code...
authortjhunt <tjhunt>
Fri, 9 Jan 2009 05:10:33 +0000 (05:10 +0000)
committertjhunt <tjhunt>
Fri, 9 Jan 2009 05:10:33 +0000 (05:10 +0000)
question/type/questiontype.php
question/type/shortanswer/questiontype.php

index 4adccba2d8939db5450e493a3f78e420aeeeb3b8..b0ead0faa3606a35ec8404a80256419df6aa0fa6 100644 (file)
@@ -127,7 +127,7 @@ class default_questiontype {
     function has_wildcards_in_responses($question, $subqid) {
         return false;
     }
-    
+
     /**
      * @return whether the question_answers.answer field needs to have
      * restore_decode_content_links_worker called on it.
@@ -148,6 +148,14 @@ class default_questiontype {
         return null;
     }
 
+    /**
+        * If you use extra_question_fields, overload this function to return question id field name
+        *  in case you table use another name for this column
+        */
+    function questionid_column_name() {
+        return 'questionid';
+    }
+
     /**
      * If your question type has a table that extends the question_answers table,
      * make this method return an array wherer the first element is the table name,
@@ -401,11 +409,12 @@ class default_questiontype {
             $question_extension_table = array_shift($extra_question_fields);
 
             $function = 'update_record';
-            $options = $DB->get_record($question_extension_table, array('questionid' => $question->id));
+            $questionidcolname = $this->questionid_column_name();
+            $options = $DB->get_record($question_extension_table, array($questionidcolname => $question->id));
             if (!$options) {
                 $function = 'insert_record';
                 $options = new stdClass;
-                $options->questionid = $question->id;
+                $options->$questionidcolname = $question->id;
             }
             foreach ($extra_question_fields as $field) {
                 if (!isset($question->$field)) {
@@ -453,7 +462,7 @@ class default_questiontype {
         $extra_question_fields = $this->extra_question_fields();
         if (is_array($extra_question_fields)) {
             $question_extension_table = array_shift($extra_question_fields);
-            $extra_data = $DB->get_record($question_extension_table, array('questionid' => $question->id), '', implode(', ', $extra_question_fields));
+            $extra_data = $DB->get_record($question_extension_table, array($this->questionid_column_name() => $question->id), implode(', ', $extra_question_fields));
             if ($extra_data) {
                 foreach ($extra_question_fields as $field) {
                     $question->options->$field = $extra_data->$field;
@@ -510,7 +519,8 @@ class default_questiontype {
         $extra_question_fields = $this->extra_question_fields();
         if (is_array($extra_question_fields)) {
             $question_extension_table = array_shift($extra_question_fields);
-            $success = $success && $DB->delete_records($question_extension_table, array('questionid' => $questionid));
+            $success = $success && $DB->delete_records($question_extension_table,
+                    array($this->questionid_column_name() => $questionid));
         }
 
         $extra_answer_fields = $this->extra_answer_fields();
index afc2d751dca5a1c001bdff2fd3ce39a0a5508bb0..74a4ea8b29edef43fd383fb3cecb65e2471ebf49 100644 (file)
@@ -26,20 +26,12 @@ class question_shortanswer_qtype extends default_questiontype {
         return true;
     }
 
-    function get_question_options(&$question) {
-        global $DB;
-        // Get additional information from database
-        // and attach it to the question object
-        if (!$question->options = $DB->get_record('question_shortanswer', array('question' => $question->id))) {
-            notify('Error: Missing question options!');
-            return false;
-        }
+    function extra_question_fields() {
+        return array('question_shortanswer','answers','usecase');
+    }
 
-        if (!$question->options->answers = $DB->get_records('question_answers', array('question' => $question->id), 'id ASC')) {
-            notify('Error: Missing question answers for shortanswer question ' . $question->id . '!');
-            return false;
-        }
-        return true;
+    function questionid_column_name() {
+        return 'question';
     }
 
     function save_question_options($question) {
@@ -87,22 +79,10 @@ class question_shortanswer_qtype extends default_questiontype {
             }
         }
 
-        if ($options = $DB->get_record("question_shortanswer", array("question" => $question->id))) {
-            $options->answers = implode(",",$answers);
-            $options->usecase = $question->usecase;
-            if (!$DB->update_record("question_shortanswer", $options)) {
-                $result->error = "Could not update quiz shortanswer options! (id=$options->id)";
-                return $result;
-            }
-        } else {
-            unset($options);
-            $options->question = $question->id;
-            $options->answers = implode(",",$answers);
-            $options->usecase = $question->usecase;
-            if (!$DB->insert_record("question_shortanswer", $options)) {
-                $result->error = "Could not insert quiz shortanswer options!";
-                return $result;
-            }
+        $question->answers = implode(',', $answers);
+        $parentresult = parent::save_question_options($question);
+        if($parentresult !== null) { // Parent function returns null if all is OK
+            return $parentresult;
         }
 
         // delete old answer records
@@ -122,18 +102,6 @@ class question_shortanswer_qtype extends default_questiontype {
         }
     }
 
-    /**
-    * Deletes question from the question-type specific tables
-    *
-    * @return boolean Success/Failure
-    * @param object $question  The question being deleted
-    */
-    function delete_question($questionid) {
-        global $DB;
-        $DB->delete_records("question_shortanswer", array("question" => $questionid));
-        return true;
-    }
-
     function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options) {
         global $CFG;
     /// This implementation is also used by question type 'numerical'