Index: question/type/shortanswer/edit_shortanswer_form.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/shortanswer/edit_shortanswer_form.php,v retrieving revision 1.10.2.3 diff -u -r1.10.2.3 edit_shortanswer_form.php --- question/type/shortanswer/edit_shortanswer_form.php 28 Aug 2008 01:46:48 -0000 1.10.2.3 +++ question/type/shortanswer/edit_shortanswer_form.php 13 Feb 2009 09:50:08 -0000 @@ -26,30 +26,8 @@ $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) { Index: question/type/numerical/edit_numerical_form.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/numerical/edit_numerical_form.php,v retrieving revision 1.11.2.6 diff -u -r1.11.2.6 edit_numerical_form.php --- question/type/numerical/edit_numerical_form.php 11 Dec 2008 04:33:48 -0000 1.11.2.6 +++ question/type/numerical/edit_numerical_form.php 13 Feb 2009 09:50:08 -0000 @@ -13,6 +13,18 @@ * numerical editing form definition. */ class question_edit_numerical_form extends question_edit_form { + + function get_per_answer_fields(&$mform, $label, $gradeoptions) { + $repeated = parent::get_per_answer_fields(&$mform, $label, $gradeoptions); + + $tolerance =& $mform->createElement('text', 'tolerance', get_string('acceptederror', 'quiz')); + $mform->setType('tolerance', PARAM_NUMBER); + array_splice($repeated, 2, 0, $tolerance); + + return $repeated; + } + + /** * Add question-type specific form fields. * @@ -22,38 +34,8 @@ //------------------------------------------------------------------------------------------ $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: question/type/multichoice/edit_multichoice_form.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/multichoice/edit_multichoice_form.php,v retrieving revision 1.12.2.2 diff -u -r1.12.2.2 edit_multichoice_form.php --- question/type/multichoice/edit_multichoice_form.php 23 Nov 2007 22:12:36 -0000 1.12.2.2 +++ question/type/multichoice/edit_multichoice_form.php 13 Feb 2009 09:50:08 -0000 @@ -41,28 +41,8 @@ $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, 5); $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'qtype_multichoice')); Index: question/type/edit_question_form.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/edit_question_form.php,v retrieving revision 1.15.2.1 diff -u -r1.15.2.1 edit_question_form.php --- question/type/edit_question_form.php 8 Oct 2008 10:19:51 -0000 1.15.2.1 +++ question/type/edit_question_form.php 13 Feb 2009 09:50:08 -0000 @@ -240,6 +240,51 @@ // 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. + * @return array of form fields. + */ + function get_per_answer_fields(&$mform, $label, $gradeoptions) { + $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)); + $mform->setType('answer', PARAM_RAW); + 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. + */ + function add_per_answer_fields(&$mform, $label, $gradeoptions, $minoptions = 0) { + $repeated = $this->get_per_answer_fields($mform, $label, $gradeoptions); + + if (isset($this->question->options)){ + $countanswers = count($this->question->options->answers); + } else { + $countanswers = 0; + } + if ($this->question->formoptions->repeatelements){ + $repeatsatstart = max($minoptions, QUESTION_NUMANS_START, $countanswers + QUESTION_NUMANS_ADD); + } else { + $repeatsatstart = $countanswers; + } + + $repeatedoptions = array(); + $repeatedoptions['fraction']['default'] = 0; + $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', QUESTION_NUMANS_ADD, get_string('addmorechoiceblanks', 'qtype_multichoice')); + } + function set_data($question) { global $QTYPES; $QTYPES[$question->qtype]->set_default_options($question);