MDL-27418 add validation to stop students intering a thosands sep.

If it might be confused by a decimal point from another locale.
This commit is contained in:
Tim Hunt 2011-06-28 18:26:34 +01:00
parent 1a1353a5ce
commit f040d4b046
3 changed files with 23 additions and 0 deletions

View file

@ -64,6 +64,7 @@ $string['nominal'] = 'Nominal';
$string['onlynumerical'] = 'Units are not used at all. Only the numerical value is graded.'; $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['oneunitshown'] = 'Unit 1 is automatically displayed beside the answer box.';
$string['pleaseenterananswer'] = 'Please enter an answer.'; $string['pleaseenterananswer'] = 'Please enter an answer.';
$string['pleaseenteranswerwithoutthousandssep'] = 'Please enter your answer without using the thousand separator ({$a}).';
$string['relative'] = 'Relative'; $string['relative'] = 'Relative';
$string['rightexample'] = 'on the right, for example 1.00cm or 1.00km'; $string['rightexample'] = 'on the right, for example 1.00cm or 1.00km';
$string['selectunits'] = 'Select units'; $string['selectunits'] = 'Select units';

View file

@ -109,6 +109,10 @@ class qtype_numerical_question extends question_graded_automatically {
return false; return false;
} }
if ($this->ap->contains_thousands_seaparator($response['answer'])) {
return false;
}
return true; return true;
} }
@ -130,6 +134,11 @@ class qtype_numerical_question extends question_graded_automatically {
return get_string('unitnotselected', 'qtype_numerical'); 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 ''; return '';
} }

View file

@ -524,6 +524,19 @@ class qtype_numerical_answer_processor {
return $this->thousandssep; 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. * Create the regular expression that {@link parse_response()} requires.
* @return string * @return string