mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 16:36:37 +02:00
MDL-76362 qtype_numerical: Support empty units for apply_units
This commit is contained in:
parent
913430f144
commit
b4103db72e
2 changed files with 9 additions and 2 deletions
|
@ -651,9 +651,13 @@ class qtype_numerical_answer_processor {
|
||||||
* by the unit multiplier, if any, and the unit string, for reference.
|
* by the unit multiplier, if any, and the unit string, for reference.
|
||||||
*/
|
*/
|
||||||
public function apply_units($response, $separateunit = null) {
|
public function apply_units($response, $separateunit = null) {
|
||||||
|
if ($response === null || trim($response) === '') {
|
||||||
|
return [null, null, null];
|
||||||
|
}
|
||||||
|
|
||||||
// Strip spaces (which may be thousands separators) and change other forms
|
// Strip spaces (which may be thousands separators) and change other forms
|
||||||
// of writing e to e.
|
// of writing e to e.
|
||||||
$response = str_replace(' ', '', $response ?? '');
|
$response = str_replace(' ', '', $response);
|
||||||
$response = preg_replace('~(?:e|E|(?:x|\*|×)10(?:\^|\*\*))([+-]?\d+)~', 'e$1', $response);
|
$response = preg_replace('~(?:e|E|(?:x|\*|×)10(?:\^|\*\*))([+-]?\d+)~', 'e$1', $response);
|
||||||
|
|
||||||
// If a . is present or there are multiple , (i.e. 2,456,789 ) assume ,
|
// If a . is present or there are multiple , (i.e. 2,456,789 ) assume ,
|
||||||
|
|
|
@ -62,7 +62,7 @@ class answerprocessor_test extends \advanced_testcase {
|
||||||
|
|
||||||
$rc = new \ReflectionClass($ap);
|
$rc = new \ReflectionClass($ap);
|
||||||
$rcm = $rc->getMethod('parse_response');
|
$rcm = $rc->getMethod('parse_response');
|
||||||
$rcm->setAccessible(True);
|
$rcm->setAccessible(true);
|
||||||
|
|
||||||
$this->assertEquals($expected, $rcm->invoke($ap, $args));
|
$this->assertEquals($expected, $rcm->invoke($ap, $args));
|
||||||
}
|
}
|
||||||
|
@ -200,6 +200,9 @@ class answerprocessor_test extends \advanced_testcase {
|
||||||
|
|
||||||
[1, 'frogs', null, '1 frogs'],
|
[1, 'frogs', null, '1 frogs'],
|
||||||
[null, null, null, '. m/s'],
|
[null, null, null, '. m/s'],
|
||||||
|
[null, null, null, null],
|
||||||
|
[null, null, null, ''],
|
||||||
|
[null, null, null, ' '],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue