]> git.mjollnir.org Git - moodle.git/commitdiff
question editing: MDL-18035 Nice refactoring from Oleg Sychev that saves 50+ lines...
authortjhunt <tjhunt>
Wed, 18 Feb 2009 06:52:11 +0000 (06:52 +0000)
committertjhunt <tjhunt>
Wed, 18 Feb 2009 06:52:11 +0000 (06:52 +0000)
question/type/calculated/edit_calculated_form.php
question/type/calculated/questiontype.php
question/type/edit_question_form.php
question/type/match/edit_match_form.php
question/type/missingtype/edit_missingtype_form.php
question/type/multichoice/edit_multichoice_form.php
question/type/numerical/edit_numerical_form.php
question/type/shortanswer/edit_shortanswer_form.php

index 8840a7fb12bb30513d70ae974d2a6d7e858c5ce2..2fa1d7f384c02bf9d63f625633798e601f95831f 100644 (file)
@@ -19,6 +19,28 @@ class question_edit_calculated_form extends question_edit_form {
      * @var question_calculated_qtype
      */
     var $qtypeobj;
+
+    function get_per_answer_fields(&$mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
+        $repeated = parent::get_per_answer_fields(&$mform, $label, $gradeoptions, $repeatedoptions, $answersoption);
+        $mform->setType('answer', PARAM_NOTAGS);
+
+        $addrepeated = array();
+        $addrepeated[] =& $mform->createElement('text', 'tolerance', get_string('tolerance', 'qtype_calculated'));
+        $repeatedoptions['tolerance']['type'] = PARAM_NUMBER;
+        $repeatedoptions['tolerance']['default'] = 0.01;
+        $addrepeated[] =& $mform->createElement('select', 'tolerancetype', get_string('tolerancetype', 'quiz'), $this->qtypeobj->tolerance_types());
+
+        $addrepeated[] =&  $mform->createElement('select', 'correctanswerlength', get_string('correctanswershows', 'qtype_calculated'), range(0, 9));
+        $repeatedoptions['correctanswerlength']['default'] = 2;
+
+        $answerlengthformats = array('1' => get_string('decimalformat', 'quiz'), '2' => get_string('significantfiguresformat', 'quiz'));
+        $addrepeated[] =&  $mform->createElement('select', 'correctanswerformat', get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
+        array_splice($repeated, 3, 0, $addrepeated);
+        $repeated[1]->setLabel(get_string('correctanswerformula', 'quiz').'=');
+
+        return $repeated;
+    }
+
     /**
      * Add question-type specific form fields.
      *
@@ -37,43 +59,9 @@ class question_edit_calculated_form extends question_edit_form {
 
         $mform->insertElementBefore(    $mform->createElement('submit', $addfieldsname, $addstring),'listcategory');
 
-        $repeated = array();
-        $repeated[] =& $mform->createElement('header', 'answerhdr', get_string('answerhdr', 'qtype_calculated', '{no}'));
-
-        $repeated[] =& $mform->createElement('text', 'answer', get_string('correctanswerformula', 'quiz').'=', array('size' => 50));
-        $repeatedoptions['answer']['type'] = PARAM_NOTAGS;
-
         $creategrades = get_grade_options();
-        $gradeoptions = $creategrades->gradeoptions;
-        $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
-        $repeatedoptions['fraction']['default'] = 0;
-
-        $repeated[] =& $mform->createElement('text', 'tolerance', get_string('tolerance', 'qtype_calculated'));
-        $repeatedoptions['tolerance']['type'] = PARAM_NUMBER;
-        $repeatedoptions['tolerance']['default'] = 0.01;
-        $repeated[] =& $mform->createElement('select', 'tolerancetype', get_string('tolerancetype', 'quiz'), $this->qtypeobj->tolerance_types());
-
-        $repeated[] =&  $mform->createElement('select', 'correctanswerlength', get_string('correctanswershows', 'qtype_calculated'), range(0, 9));
-        $repeatedoptions['correctanswerlength']['default'] = 2;
-
-        $answerlengthformats = array('1' => get_string('decimalformat', 'quiz'), '2' => get_string('significantfiguresformat', 'quiz'));
-        $repeated[] =&  $mform->createElement('select', 'correctanswerformat', get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
-
-        $repeated[] =&  $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'),
-                                array('course' => $this->coursefilesid));
-        $repeatedoptions['feedback']['type'] = PARAM_RAW;
-
-        if (isset($this->question->options)){
-            $count = count($this->question->options->answers);
-        } else {
-            $count = 0;
-        }
-        if ($this->question->formoptions->repeatelements){
-            $repeatsatstart = $count + 1;
-        } else {
-            $repeatsatstart = $count;
-        }
-        $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', 1, get_string('addmoreanswerblanks', 'qtype_calculated'));
+        $this->add_per_answer_fields($mform, get_string('answerhdr', 'qtype_calculated', '{no}'),
+                $creategrades->gradeoptions, 1, 1);
 
         $repeated = array();
         $repeated[] =& $mform->createElement('header', 'unithdr', get_string('unithdr', 'qtype_numerical', '{no}'));
index b7c20d669a36c32a1b760407220c386103c5dd8a..26b604c6b7ad159e8e58385d7ff9151c3fbca480 100644 (file)
@@ -131,6 +131,10 @@ class question_calculated_qtype extends default_questiontype {
         // Get old answers:
         global $CFG, $DB;
 
+        if (isset($question->answer) && !isset($question->asnwers)) {
+            $question->answers = $question->answer;
+        }
+
         // Get old versions of the objects
         if (!$oldanswers = $DB->get_records('question_answers', array('question' => $question->id), 'id ASC')) {
             $oldanswers = array();
@@ -139,8 +143,8 @@ class question_calculated_qtype extends default_questiontype {
         if (!$oldoptions = $DB->get_records('question_calculated', array('question' => $question->id), 'answer ASC')) {
             $oldoptions = array();
         }
-                // Save the units.
-        // Save units
+
+        // Save the units.
         $virtualqtype = $this->get_virtual_qtype();
         $result = $virtualqtype->save_numerical_units($question);
         if (isset($result->error)) {
index 31c4579b28f25911f789944a66ba13095f388d82..4e7b155b91affbb9a69d955669d4d2cfaa7c452e 100644 (file)
@@ -248,6 +248,56 @@ class question_edit_form extends moodleform {
         // By default, do nothing.
     }
 
+    /**
+     * Get the list of form elements to repeat, one for each answer.
+     * @param object $mform the form being built.
+     * @param $label the label to use for each option.
+     * @param $gradeoptions the possible grades for each answer.
+     * @param $repeatedoptions reference to array of repeated options to fill
+     * @param $answersoption reference to return the name of $question->options field holding an array of answers
+     * @return array of form fields.
+     */
+    function get_per_answer_fields(&$mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
+        $repeated = array();
+        $repeated[] =& $mform->createElement('header', 'answerhdr', $label);
+        $repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'), array('size' => 50));
+        $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
+        $repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'),
+                                array('course' => $this->coursefilesid));
+        $repeatedoptions['answer']['type'] = PARAM_RAW;
+        $repeatedoptions['fraction']['default'] = 0;
+        $answersoption = 'answers';
+        return $repeated;
+    }
+
+    /**
+     * Add a set of form fields, obtained from get_per_answer_fields, to the form,
+     * one for each existing answer, with some blanks for some new ones.
+     * @param object $mform the form being built.
+     * @param $label the label to use for each option.
+     * @param $gradeoptions the possible grades for each answer.
+     * @param $minoptions the minimum number of answer blanks to display. Default QUESTION_NUMANS_START.
+     * @param $addoptions the number of answer blanks to add. Default QUESTION_NUMANS_ADD.
+     */
+    function add_per_answer_fields(&$mform, $label, $gradeoptions, $minoptions = QUESTION_NUMANS_START, $addoptions = QUESTION_NUMANS_ADD) {
+        $answersoption = '';
+        $repeatedoptions = array();
+        $repeated = $this->get_per_answer_fields($mform, $label, $gradeoptions, $repeatedoptions, $answersoption);
+
+        if (isset($this->question->options)){
+            $countanswers = count($this->question->options->$answersoption);
+        } else {
+            $countanswers = 0;
+        }
+        if ($this->question->formoptions->repeatelements){
+            $repeatsatstart = max($minoptions, $countanswers + $addoptions);
+        } else {
+            $repeatsatstart = $countanswers;
+        }
+
+        $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', $addoptions, get_string('addmorechoiceblanks', 'qtype_multichoice'));
+    }
+
     function set_data($question) {
         global $QTYPES;
         if (empty($question->image)){
index 629bd63234783ca86f2b01fe6a17d3e8554ffad5..719a9cbf5131c666995444f9c1cf288c273208cf 100644 (file)
  * match editing form definition.
  */
 class question_edit_match_form extends question_edit_form {
+
+    function get_per_answer_fields(&$mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
+        $repeated = array();
+        $repeated[] =& $mform->createElement('header', 'answerhdr', $label);
+        $repeated[] =& $mform->createElement('textarea', 'subquestions', get_string('question', 'quiz'), array('cols'=>40, 'rows'=>3));
+        $repeated[] =& $mform->createElement('text', 'subanswers', get_string('answer', 'quiz'), array('size'=>50));
+        $repeatedoptions['subquestions']['type'] = PARAM_RAW;
+        $repeatedoptions['subanswers']['type'] = PARAM_TEXT;
+        $answersoption = 'subquestions';
+        return $repeated;
+    }
+
     /**
      * Add question-type specific form fields.
      *
@@ -26,27 +38,7 @@ class question_edit_match_form extends question_edit_form {
         $mform->addElement('static', 'answersinstruct', get_string('choices', 'quiz'), get_string('filloutthreeqsandtwoas', 'qtype_match'));
         $mform->closeHeaderBefore('answersinstruct');
 
-        $repeated = array();
-        $repeated[] =& $mform->createElement('header', 'choicehdr', get_string('questionno', 'quiz', '{no}'));
-        $repeated[] =& $mform->createElement('textarea', 'subquestions', get_string('question', 'quiz'), array('cols'=>40, 'rows'=>3));
-        $repeated[] =& $mform->createElement('text', 'subanswers', get_string('answer', 'quiz'), array('size'=>50));
-
-        if (isset($this->question->options)){
-            $countsubquestions = count($this->question->options->subquestions);
-        } else {
-            $countsubquestions = 0;
-        }
-        if ($this->question->formoptions->repeatelements){
-            $repeatsatstart = (QUESTION_NUMANS_START > ($countsubquestions + QUESTION_NUMANS_ADD))?
-                                QUESTION_NUMANS_START : ($countsubquestions + QUESTION_NUMANS_ADD);
-        } else {
-            $repeatsatstart = $countsubquestions;
-        }
-        $mform->setType('subanswer', PARAM_TEXT);
-        $mform->setType('subquestion', PARAM_TEXT);
-
-        $this->repeat_elements($repeated, $repeatsatstart, array(), 'noanswers', 'addanswers', QUESTION_NUMANS_ADD, get_string('addmoreqblanks', 'qtype_match'));
-
+        $this->add_per_answer_fields($mform, get_string('questionno', 'quiz', '{no}'), 0);
     }
 
     function set_data($question) {
index e390a73e002034718b71fb64cc9e92d6b8f0e94a..46b2842aaf8031233969b6ba77aef7ab4f56e7ee 100644 (file)
@@ -20,29 +20,8 @@ class question_edit_missingtype_form extends question_edit_form {
      */
     function definition_inner(&$mform) {
         $creategrades = get_grade_options();
-        $gradeoptions = $creategrades->gradeoptionsfull;
-        $repeated = array();
-        $repeated[] =& $mform->createElement('header', 'choicehdr', get_string('choiceno', 'qtype_multichoice', '{no}'));
-        $repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'));
-        $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
-        $repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'),
-                                array('course' => $this->coursefilesid));
-
-        if (isset($this->question->options)){
-            $countanswers = count($this->question->options->answers);
-        } else {
-            $countanswers = 0;
-        }
-        if ($this->question->formoptions->repeatelements){
-            $repeatsatstart = (QUESTION_NUMANS_START > ($countanswers + QUESTION_NUMANS_ADD))?
-                                QUESTION_NUMANS_START : ($countanswers + QUESTION_NUMANS_ADD);
-        } else {
-            $repeatsatstart = $countanswers;
-        }
-        $repeatedoptions = array();
-        $repeatedoptions['fraction']['default'] = 0;
-        $mform->setType('answer', PARAM_NOTAGS);
-        $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', QUESTION_NUMANS_ADD, get_string('addmorechoiceblanks', 'qtype_multichoice'));
+        $this->add_per_answer_fields($mform, get_string('choiceno', 'qtype_multichoice', '{no}'),
+                $creategrades->gradeoptionsfull);
     }
 
     function set_data($question) {
index a9feae700ad88142110e98c2d113d6e22a574818..9b81258350ddf0898ef1ea52fb51b5a9226a43af 100644 (file)
@@ -41,42 +41,16 @@ class question_edit_multichoice_form extends question_edit_form {
         $mform->closeHeaderBefore('answersinstruct');
 */
         $creategrades = get_grade_options();
-        $gradeoptions = $creategrades->gradeoptionsfull;
-        $repeated = array();
-        $repeated[] =& $mform->createElement('header', 'choicehdr', get_string('choiceno', 'qtype_multichoice', '{no}'));
-        $repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'), array('size' => 50));
-        $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
-        $repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'),
-                                array('course' => $this->coursefilesid));
-
-        if (isset($this->question->options)){
-            $countanswers = count($this->question->options->answers);
-        } else {
-            $countanswers = 0;
-        }
-        if ($this->question->formoptions->repeatelements){
-            $repeatsatstart = max(5, QUESTION_NUMANS_START, $countanswers + QUESTION_NUMANS_ADD);
-        } else {
-            $repeatsatstart = $countanswers;
-        }
-        $repeatedoptions = array();
-        $repeatedoptions['fraction']['default'] = 0;
-        $mform->setType('answer', PARAM_RAW);
-        $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', QUESTION_NUMANS_ADD, get_string('addmorechoiceblanks', 'qtype_multichoice'));
+        $this->add_per_answer_fields($mform, get_string('choiceno', 'qtype_multichoice', '{no}'),
+                $creategrades->gradeoptionsfull, max(5, QUESTION_NUMANS_START));
 
         $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'qtype_multichoice'));
 
-        $mform->addElement('htmleditor', 'correctfeedback', get_string('correctfeedback', 'qtype_multichoice'),
+        foreach (array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback') as $feedbackname) {
+            $mform->addElement('htmleditor', $feedbackname, get_string($feedbackname, 'qtype_multichoice'),
                                 array('course' => $this->coursefilesid));
-        $mform->setType('correctfeedback', PARAM_RAW);
-
-        $mform->addElement('htmleditor', 'partiallycorrectfeedback', get_string('partiallycorrectfeedback', 'qtype_multichoice'),
-                                array('course' => $this->coursefilesid));
-        $mform->setType('partiallycorrectfeedback', PARAM_RAW);
-
-        $mform->addElement('htmleditor', 'incorrectfeedback', get_string('incorrectfeedback', 'qtype_multichoice'),
-                                array('course' => $this->coursefilesid));
-        $mform->setType('incorrectfeedback', PARAM_RAW);
+            $mform->setType($feedbackname, PARAM_RAW);
+        }
 
     }
 
index 2d91d9890ec4f3259811e4264a4378b33eee990f..a60235761310ea0f1d088136745366120b5503d9 100644 (file)
  * numerical editing form definition.
  */
 class question_edit_numerical_form extends question_edit_form {
+
+    function get_per_answer_fields(&$mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
+        $repeated = parent::get_per_answer_fields($mform, $label, $gradeoptions, $repeatedoptions, $answersoption);
+
+        $tolerance =& $mform->createElement('text', 'tolerance', get_string('acceptederror', 'quiz'));
+        $repeatedoptions['tolerance']['type'] = PARAM_NUMBER;
+        array_splice($repeated, 3, 0, array($tolerance));
+        $repeated[1]->setSize(10);
+
+        return $repeated;
+    }
+
     /**
      * Add question-type specific form fields.
      *
@@ -22,38 +34,8 @@ class question_edit_numerical_form extends question_edit_form {
 
 //------------------------------------------------------------------------------------------
         $creategrades = get_grade_options();
-        $gradeoptions = $creategrades->gradeoptions;
-        $repeated = array();
-        $repeatedoptions = array();
-        $repeated[] =& $mform->createElement('header', 'answerhdr', get_string('answerno', 'qtype_numerical', '{no}'));
-
-        $repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'));
-        $mform->setType('answer', PARAM_RAW);
-
-        $repeated[] =& $mform->createElement('text', 'tolerance', get_string('acceptederror', 'quiz'));
-        $mform->setType('tolerance', PARAM_NUMBER);
-
-        $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
-        $repeatedoptions['fraction']['default'] = 0;
-
-        $repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'),
-                                array('course' => $this->coursefilesid));
-        $mform->setType('feedback', PARAM_RAW);
-
-
-        if (isset($this->question->options)){
-            $countanswers = count($this->question->options->answers);
-        } else {
-            $countanswers = 0;
-        }
-        if ($this->question->formoptions->repeatelements){
-            $repeatsatstart = (QUESTION_NUMANS_START > ($countanswers + 1))?
-                                QUESTION_NUMANS_START : ($countanswers + 1);
-        } else {
-            $repeatsatstart = $countanswers;
-        }
-        $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', 2, get_string('addmoreanswerblanks', 'qtype_numerical'));
-
+        $this->add_per_answer_fields($mform, get_string('answerno', 'qtype_numerical', '{no}'),
+                $creategrades->gradeoptions);
 //------------------------------------------------------------------------------------------
         $repeated = array();
         $repeated[] =& $mform->createElement('header', 'unithdr', get_string('unithdr', 'qtype_numerical', '{no}'));
index f40ee47863c4f2b32b59c5ed620b1d799b7d2fed..19d882a593d062dfdc5ce1097ec10eb4c3bee015 100644 (file)
@@ -26,30 +26,8 @@ class question_edit_shortanswer_form extends question_edit_form {
         $mform->closeHeaderBefore('answersinstruct');
 
         $creategrades = get_grade_options();
-        $gradeoptions = $creategrades->gradeoptions;
-        $repeated = array();
-        $repeated[] =& $mform->createElement('header', 'answerhdr', get_string('answerno', 'qtype_shortanswer', '{no}'));
-        $repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'), array('size' => 54));
-        $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
-        $repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'),
-                                array('course' => $this->coursefilesid));
-
-        if (isset($this->question->options)){
-            $countanswers = count($this->question->options->answers);
-        } else {
-            $countanswers = 0;
-        }
-        if ($this->question->formoptions->repeatelements){
-            $repeatsatstart = (QUESTION_NUMANS_START > ($countanswers + QUESTION_NUMANS_ADD))?
-                                QUESTION_NUMANS_START : ($countanswers + QUESTION_NUMANS_ADD);
-        } else {
-            $repeatsatstart = $countanswers;
-        }
-        $repeatedoptions = array();
-        $mform->setType('answer', PARAM_RAW);
-        $repeatedoptions['fraction']['default'] = 0;
-        $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', QUESTION_NUMANS_ADD, get_string('addmoreanswerblanks', 'qtype_shortanswer'));
-
+        $this->add_per_answer_fields($mform, get_string('answerno', 'qtype_shortanswer', '{no}'),
+                $creategrades->gradeoptions);
     }
 
     function set_data($question) {