MDL-36534 Quiz: Group question type unit fields
authorColin Chambers <c.chambers@open.ac.uk>
Mon, 12 Nov 2012 16:10:50 +0000 (16:10 +0000)
committerColin Chambers <c.chambers@open.ac.uk>
Mon, 14 Jan 2013 14:37:37 +0000 (14:37 +0000)
question/type/calculated/lang/en/qtype_calculated.php
question/type/calculatedmulti/edit_calculatedmulti_form.php
question/type/numerical/edit_numerical_form.php
question/type/numerical/lang/en/qtype_numerical.php

index a27d9be..034cf96 100644 (file)
@@ -91,9 +91,9 @@ $string['minmax'] = 'Range of Values';
 $string['missingformula'] = 'Missing formula';
 $string['missingname'] = 'Missing question name';
 $string['missingquestiontext'] = 'Missing question text';
-$string['mustbenumeric'] = 'You must enter a number here.';
+$string['mustbenumeric'] = '{$a} must be a number.';
 $string['mustenteraformulaorstar'] = 'You must enter a formula or \'*\'.';
-$string['mustnotbenumeric'] = 'This can\'t be a number.';
+$string['mustnotbenumeric'] = '{$a} can\'t be a number.';
 $string['newcategory1'] = 'will use a new shared dataset';
 $string['newcategory2'] = 'a file from a new set of files that may also be used by other questions in this category';
 $string['newcategory3'] = 'a link from a new set of links that may also be used by other questions in this category';
index 7a802ee..24cbaa2 100644 (file)
@@ -298,7 +298,8 @@ class qtype_calculatedmulti_edit_form extends question_edit_form {
                 }
                 if (!is_numeric($data['tolerance'][$key])) {
                     $errors['tolerance['.$key.']'] =
-                            get_string('mustbenumeric', 'qtype_calculated');
+                            get_string('mustbenumeric', 'qtype_calculated',
+                                get_string('acceptederror', 'qtype_numerical'));
                 }
                 if ($data['fraction'][$key] > 0) {
                     $totalfraction += $data['fraction'][$key];
index 5328c9c..d372b9a 100644 (file)
@@ -129,19 +129,18 @@ class qtype_numerical_edit_form extends question_edit_form {
      * @param object $mform the form being built.
      */
     protected function add_unit_fields($mform) {
-        $repeated = array(
-            $mform->createElement('header', 'unithdr',
-                    get_string('unithdr', 'qtype_numerical', '{no}')),
-            $mform->createElement('text', 'unit', get_string('unit', 'quiz')),
-            $mform->createElement('text', 'multiplier', get_string('multiplier', 'quiz')),
-        );
+        $mform->addElement('header', 'unithdr',
+                    get_string('units', 'qtype_numerical'), '');
+
+        $textboxgroup = array();
+        $textboxgroup[] = $mform->createElement('group', 'units',
+                 get_string('unitx', 'qtype_numerical'), $this->unit_group($mform), null, false);
 
-        $repeatedoptions['unit']['type'] = PARAM_NOTAGS;
-        $repeatedoptions['multiplier']['type'] = PARAM_FLOAT;
         $repeatedoptions['unit']['disabledif'] =
-                array('unitrole', 'eq', qtype_numerical::UNITNONE);
+                 array('unitrole', 'eq', qtype_numerical::UNITNONE);
         $repeatedoptions['multiplier']['disabledif'] =
-                array('unitrole', 'eq', qtype_numerical::UNITNONE);
+                 array('unitrole', 'eq', qtype_numerical::UNITNONE);
+        $mform->disabledIf('addunits', 'unitrole', 'eq', qtype_numerical::UNITNONE);
 
         if (isset($this->question->options->units)) {
             $countunits = count($this->question->options->units);
@@ -153,17 +152,39 @@ class qtype_numerical_edit_form extends question_edit_form {
         } else {
             $repeatsatstart = $countunits;
         }
-        $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'nounits',
-                'addunits', 2, get_string('addmoreunitblanks', 'qtype_calculated', '{no}'));
-
-        if ($mform->elementExists('multiplier[0]')) {
-            $firstunit = $mform->getElement('multiplier[0]');
-            $firstunit->freeze();
-            $mform->setDefault('multiplier[0]', '1.0');
-            $mform->addHelpButton('multiplier[0]', 'numericalmultiplier', 'qtype_numerical');
+
+        $this->repeat_elements($textboxgroup, $repeatsatstart, $repeatedoptions, 'nounits',
+                'addunits', 2, get_string('addmoreunitblanks', 'qtype_calculated', '{no}'), true);
+
+        // The following strange-looking if statement is to do with when the
+        // form is used to move questions between categories. See MDL-15159.
+        if ($mform->elementExists('units[0]')) {
+            $firstunit = $mform->getElement('units[0]');
+            $elements = $firstunit->getElements();
+            foreach ($elements as $element) {
+                if ($element->getName() != 'multiplier[0]') {
+                    continue;
+                }
+                $element->freeze();
+                $element->setValue('1.0');
+                $element->setPersistantFreeze(true);
+            }
+            $mform->addHelpButton('units[0]', 'numericalmultiplier', 'qtype_numerical');
         }
     }
 
+    protected function unit_group($mform) {
+        $grouparray = array();
+        $grouparray[] = $mform->createElement('text', 'unit', get_string('unit', 'quiz'),
+                array('type'=>PARAM_NOTAGS, 'size'=>10));
+        $grouparray[] = $mform->createElement('static', '', '', ' ' .
+                get_string('multiplier', 'quiz').' ');
+        $grouparray[] = $mform->createElement('text', 'multiplier',
+                get_string('multiplier', 'quiz'), array('type'=>PARAM_NUMBER, 'size'=>10));
+
+        return $grouparray;
+    }
+
     protected function data_preprocessing($question) {
         $question = parent::data_preprocessing($question);
         $question = $this->data_preprocessing_answers($question);
@@ -265,7 +286,8 @@ class qtype_numerical_edit_form extends question_edit_form {
                 }
                 if ($answer !== '*' && !is_numeric($data['tolerance'][$key])) {
                     $errors['tolerance['.$key.']'] =
-                            get_string('mustbenumeric', 'qtype_calculated');
+                            get_string('mustbenumeric', 'qtype_calculated',
+                                get_string('acceptederror', 'qtype_numerical'));
                 }
             } else if ($data['fraction'][$key] != 0 ||
                     !html_is_blank($data['feedback'][$key]['text'])) {
@@ -323,18 +345,19 @@ class qtype_numerical_edit_form extends question_edit_form {
      */
     protected function validate_numerical_options($data, $errors) {
         if ($data['unitrole'] != qtype_numerical::UNITNONE && trim($data['unit'][0]) == '') {
-            $errors['unit[0]'] = get_string('unitonerequired', 'qtype_numerical');
+            $errors['units[0]'] = get_string('unitonerequired', 'qtype_numerical');
         }
 
-        if (empty($data['unit'])) {
+        if (empty($data['unit']) || $data['unitrole'] == qtype_numerical::UNITNONE) {
             return $errors;
         }
 
         // Basic unit validation.
         foreach ($data['unit'] as $key => $unit) {
             if (is_numeric($unit)) {
-                $errors['unit[' . $key . ']'] =
-                        get_string('mustnotbenumeric', 'qtype_calculated');
+                $errors['units[' . $key . ']'] =
+                        get_string('mustnotbenumeric', 'qtype_calculated',
+                            get_string('unit', 'quiz'));
             }
 
             $trimmedunit = trim($unit);
@@ -344,11 +367,12 @@ class qtype_numerical_edit_form extends question_edit_form {
 
             $trimmedmultiplier = trim($data['multiplier'][$key]);
             if (empty($trimmedmultiplier)) {
-                $errors['multiplier[' . $key . ']'] =
+                $errors['units[' . $key . ']'] =
                         get_string('youmustenteramultiplierhere', 'qtype_calculated');
             } else if (!is_numeric($trimmedmultiplier)) {
-                $errors['multiplier[' . $key . ']'] =
-                        get_string('mustbenumeric', 'qtype_calculated');
+                $errors['units[' . $key . ']'] =
+                        get_string('mustbenumeric', 'qtype_calculated',
+                            get_string('multiplier', 'quiz'));
             }
         }
 
@@ -361,7 +385,7 @@ class qtype_numerical_edit_form extends question_edit_form {
             }
 
             if (in_array($trimmedunit, $alreadyseenunits)) {
-                $errors['unit[' . $key . ']'] =
+                $errors['units[' . $key . ']'] =
                         get_string('errorrepeatedunit', 'qtype_numerical');
             } else {
                 $alreadyseenunits[] = $trimmedunit;
index fb4784b..ade8ff4 100644 (file)
@@ -78,7 +78,6 @@ $string['unitchoice'] = 'a multiple choice selection';
 $string['unitedit'] = 'Edit unit';
 $string['unitgraded'] = 'The unit must be given, and will be graded.';
 $string['unithandling'] = 'Unit handling';
-$string['unithdr'] = 'Unit {$a}';
 $string['unitincorrect'] = 'You did not give the correct unit.';
 $string['unitmandatory'] = 'Mandatory';
 $string['unitmandatory_help'] = '
@@ -102,7 +101,9 @@ $string['unitpenalty_help'] = 'The penalty is applied if
 * the wrong unit name is entered into the unit input, or
 * a unit is entered into the value input box';
 $string['unitposition'] = 'Units go';
+$string['units'] = 'Units';
 $string['unitselect'] = 'a drop-down menu';
+$string['unitx'] = 'Unit {no} ';
 $string['validnumberformats'] = 'Valid number formats';
 $string['validnumberformats_help'] = '
 * regular numbers 13500.67, 13 500.67, 13500,67 or 13 500,67