MDL-53140 qtype_numerical: Localised decimal separator bug fix
authorShamim Rezaie <shamim@moodle.com>
Thu, 4 Apr 2019 23:03:13 +0000 (10:03 +1100)
committerShamim Rezaie <shamim@moodle.com>
Wed, 1 May 2019 02:10:32 +0000 (12:10 +1000)
- Support decimal separator when it is not . or ,
- Also make sure that we don't remove . when thousandsseparator is .
- Display the answer fields in the editing form in the localised format

question/type/numerical/edit_numerical_form.php
question/type/numerical/questiontype.php

index ef71376..6788058 100644 (file)
@@ -210,6 +210,11 @@ class qtype_numerical_edit_form extends question_edit_form {
             unset($this->_form->_defaultValues["tolerance[{$key}]"]);
 
             $question->tolerance[$key] = $answer->tolerance;
+
+            if (is_numeric($question->answer[$key])) {
+                $question->answer[$key] = format_float($question->answer[$key], strlen($question->answer[$key]), true, true);
+            }
+
             $key++;
         }
 
index 82f6898..6f39b42 100644 (file)
@@ -553,7 +553,7 @@ class qtype_numerical_answer_processor {
     }
 
     /**
-     * @return book If the student's response contains a '.' or a ',' that
+     * @return bool If the student's response contains a '.' or a ',' that
      * matches the thousands separator in the current locale. In this case, the
      * parsing in apply_unit can give a result that the student did not expect.
      */
@@ -651,7 +651,7 @@ class qtype_numerical_answer_processor {
         if (strpos($response, '.') !== false || substr_count($response, ',') > 1) {
             $response = str_replace(',', '', $response);
         } else {
-            $response = str_replace(',', '.', $response);
+            $response = str_replace([$this->thousandssep, $this->decsep, ','], ['', '.', '.'], $response);
         }
 
         $regex = '[+-]?(?:\d+(?:\\.\d*)?|\\.\d+)(?:e[-+]?\d+)?';