Skip to content

Commit

Permalink
MDL-27418 add validation to stop students intering a thosands sep.
Browse files Browse the repository at this point in the history
If it might be confused by a decimal point from another locale.
  • Loading branch information
timhunt committed Jun 28, 2011
1 parent 1a1353a commit f040d4b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions question/type/numerical/lang/en/qtype_numerical.php
Expand Up @@ -64,6 +64,7 @@
$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';
Expand Down
9 changes: 9 additions & 0 deletions question/type/numerical/question.php
Expand Up @@ -109,6 +109,10 @@ public function is_complete_response(array $response) {
return false;
}

if ($this->ap->contains_thousands_seaparator($response['answer'])) {
return false;
}

return true;
}

Expand All @@ -130,6 +134,11 @@ public function get_validation_error(array $response) {
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 '';
}

Expand Down
13 changes: 13 additions & 0 deletions question/type/numerical/questiontype.php
Expand Up @@ -524,6 +524,19 @@ public function get_separator() {
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
Expand Down

0 comments on commit f040d4b

Please sign in to comment.