MDL-24408 extra_question_fields: separate answer data creation code
authorOleg Sychev <oasychev@gmail.com>
Fri, 24 Jan 2014 21:23:34 +0000 (00:23 +0300)
committerOleg Sychev <oasychev@gmail.com>
Thu, 27 Mar 2014 21:27:05 +0000 (00:27 +0300)
Now any question using extra_question_fields can overload make_answer
(just as make_hint), instead of overloading whole initialise_question_answers.

question/type/questiontypebase.php

index 09fd22d..3b02bb5 100644 (file)
@@ -950,14 +950,24 @@ class question_type {
             return;
         }
         foreach ($questiondata->options->answers as $a) {
-            $question->answers[$a->id] = new question_answer($a->id, $a->answer,
-                    $a->fraction, $a->feedback, $a->feedbackformat);
+            $question->answers[$a->id] = $this->make_answer($a);
             if (!$forceplaintextanswers) {
                 $question->answers[$a->id]->answerformat = $a->answerformat;
             }
         }
     }
 
+    /**
+     * Create a question_answer, or an appropriate subclass for this question,
+     * from a row loaded from the database.
+     * @param object $answer the DB row from the question_answers table plus extra answer fields.
+     * @return question_answer
+     */
+    protected function make_answer($answer) {
+        return new question_answer($answer->id, $answer->answer,
+                    $answer->fraction, $answer->feedback, $answer->feedbackformat);
+    }
+
     /**
      * Deletes the question-type specific data when a question is deleted.
      * @param int $question the question being deleted.