From f040d4b0462844b2d47387eb48d73c3b7dc84825 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Tue, 28 Jun 2011 18:26:34 +0100 Subject: [PATCH] MDL-27418 add validation to stop students intering a thosands sep. If it might be confused by a decimal point from another locale. --- question/type/numerical/lang/en/qtype_numerical.php | 1 + question/type/numerical/question.php | 9 +++++++++ question/type/numerical/questiontype.php | 13 +++++++++++++ 3 files changed, 23 insertions(+) diff --git a/question/type/numerical/lang/en/qtype_numerical.php b/question/type/numerical/lang/en/qtype_numerical.php index a8d65fb7f47..a2c42c3d7be 100644 --- a/question/type/numerical/lang/en/qtype_numerical.php +++ b/question/type/numerical/lang/en/qtype_numerical.php @@ -64,6 +64,7 @@ $string['nominal'] = 'Nominal'; $string['onlynumerical'] = 'Units are not used at all. Only the numerical value is graded.'; $string['oneunitshown'] = 'Unit 1 is automatically displayed beside the answer box.'; $string['pleaseenterananswer'] = 'Please enter an answer.'; +$string['pleaseenteranswerwithoutthousandssep'] = 'Please enter your answer without using the thousand separator ({$a}).'; $string['relative'] = 'Relative'; $string['rightexample'] = 'on the right, for example 1.00cm or 1.00km'; $string['selectunits'] = 'Select units'; diff --git a/question/type/numerical/question.php b/question/type/numerical/question.php index 7393dd11664..990d11b5438 100644 --- a/question/type/numerical/question.php +++ b/question/type/numerical/question.php @@ -109,6 +109,10 @@ class qtype_numerical_question extends question_graded_automatically { return false; } + if ($this->ap->contains_thousands_seaparator($response['answer'])) { + return false; + } + return true; } @@ -130,6 +134,11 @@ class qtype_numerical_question extends question_graded_automatically { return get_string('unitnotselected', 'qtype_numerical'); } + if ($this->ap->contains_thousands_seaparator($response['answer'])) { + return get_string('pleaseenteranswerwithoutthousandssep', 'qtype_numerical', + $this->ap->get_separator()); + } + return ''; } diff --git a/question/type/numerical/questiontype.php b/question/type/numerical/questiontype.php index a62dfa6ec4e..90bd215b7f7 100644 --- a/question/type/numerical/questiontype.php +++ b/question/type/numerical/questiontype.php @@ -524,6 +524,19 @@ class qtype_numerical_answer_processor { return $this->thousandssep; } + /** + * @return book 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. + */ + public function contains_thousands_seaparator($value) { + if (!in_array($this->thousandssep, array('.', ','))) { + return false; + } + + return strpos($value, $this->thousandssep) !== false; + } + /** * Create the regular expression that {@link parse_response()} requires. * @return string