Index: calculated/edit_calculated_form.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/calculated/edit_calculated_form.php,v retrieving revision 1.19.2.2 diff -u -r1.19.2.2 edit_calculated_form.php --- calculated/edit_calculated_form.php 23 Nov 2007 22:12:39 -0000 1.19.2.2 +++ calculated/edit_calculated_form.php 16 Feb 2009 13:36:46 -0000 @@ -19,6 +19,28 @@ * @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); + repeatedoptions['answer']['type'] = 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 @@ $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', 'answers', get_string('correctanswerformula', 'quiz').'=', array('size' => 50)); - $repeatedoptions['answers']['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}')); @@ -115,7 +103,7 @@ if (count($answers)) { $key = 0; foreach ($answers as $answer){ - $default_values['answers['.$key.']'] = $answer->answer; + $default_values['answer['.$key.']'] = $answer->answer; $default_values['fraction['.$key.']'] = $answer->fraction; $default_values['tolerance['.$key.']'] = $answer->tolerance; $default_values['tolerancetype['.$key.']'] = $answer->tolerancetype; @@ -193,7 +181,7 @@ } } } - $answers = $data['answers']; + $answers = $data['answer']; $answercount = 0; $maxgrade = false; $possibledatasets = $this->qtypeobj->find_dataset_names($data['questiontext']); @@ -204,7 +192,7 @@ if ( count($mandatorydatasets )==0){ // $errors['questiontext']=get_string('atleastonewildcard', 'qtype_datasetdependent'); foreach ($answers as $key => $answer){ - $errors['answers['.$key.']'] = get_string('atleastonewildcard', 'qtype_datasetdependent'); + $errors['answer['.$key.']'] = get_string('atleastonewildcard', 'qtype_datasetdependent'); } } foreach ($answers as $key => $answer){ @@ -214,7 +202,7 @@ if (($trimmedanswer!='')||$answercount==0){ $eqerror = qtype_calculated_find_formula_errors($trimmedanswer); if (FALSE !== $eqerror){ - $errors['answers['.$key.']'] = $eqerror; + $errors['answer['.$key.']'] = $eqerror; } } if ($trimmedanswer!=''){ @@ -277,7 +265,7 @@ } } if ($answercount==0){ - $errors['answers[0]'] = get_string('atleastoneanswer', 'qtype_calculated'); + $errors['answer[0]'] = get_string('atleastoneanswer', 'qtype_calculated'); } if ($maxgrade == false) { $errors['fraction[0]'] = get_string('fractionsnomax', 'question'); Index: calculated/questiontype.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/calculated/questiontype.php,v retrieving revision 1.26.2.12 diff -u -r1.26.2.12 questiontype.php --- calculated/questiontype.php 4 Dec 2008 16:32:16 -0000 1.26.2.12 +++ calculated/questiontype.php 1 Feb 2009 23:35:26 -0000 @@ -122,6 +122,11 @@ // Get old answers: global $CFG; + if (isset($question->answer) && !isset($question->asnwers)) { + $question->answers = $question->answer; + } + + // Get old versions of the objects if (!$oldanswers = get_records('question_answers', 'question', $question->id, 'id ASC')) { $oldanswers = array(); Index: 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 --- edit_question_form.php 8 Oct 2008 10:19:51 -0000 1.15.2.1 +++ edit_question_form.php 15 Feb 2009 23:37:08 -0000 @@ -240,6 +240,56 @@ // 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; $QTYPES[$question->qtype]->set_default_options($question); Index: match/edit_match_form.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/match/edit_match_form.php,v retrieving revision 1.8.2.4 diff -u -r1.8.2.4 edit_match_form.php --- match/edit_match_form.php 27 Oct 2008 02:33:28 -0000 1.8.2.4 +++ match/edit_match_form.php 15 Feb 2009 23:18:18 -0000 @@ -13,6 +13,18 @@ * 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_TEXT; + $repeatedoptions['subanswers']['type'] = PARAM_TEXT; + $answersoption = 'subquestions'; + return $repeated; + } + /** * Add question-type specific form fields. * @@ -26,27 +38,7 @@ $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: missingtype/edit_missingtype_form.php =================================================================== RCS file: /cvsroot/moodle/moodle/question/type/missingtype/edit_missingtype_form.php,v retrieving revision 1.4.2.2 diff -u -r1.4.2.2 edit_missingtype_form.php --- missingtype/edit_missingtype_form.php 23 Nov 2007 22:12:37 -0000 1.4.2.2 +++ missingtype/edit_missingtype_form.php 15 Feb 2009 18:48:24 -0000 @@ -20,29 +20,8 @@ */ 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: 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 --- multichoice/edit_multichoice_form.php 23 Nov 2007 22:12:36 -0000 1.12.2.2 +++ multichoice/edit_multichoice_form.php 15 Feb 2009 22:36:18 -0000 @@ -41,42 +41,16 @@ $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: 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 --- numerical/edit_numerical_form.php 11 Dec 2008 04:33:48 -0000 1.11.2.6 +++ numerical/edit_numerical_form.php 15 Feb 2009 23:42:36 -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, &$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 @@ //------------------------------------------------------------------------------------------ $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: 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 --- shortanswer/edit_shortanswer_form.php 28 Aug 2008 01:46:48 -0000 1.10.2.3 +++ shortanswer/edit_shortanswer_form.php 15 Feb 2009 17:54:24 -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) {