MDL-76362 qtype_numerical: Support empty units for apply_units

This commit is contained in:
Andrew Nicols 2023-01-06 22:11:22 +08:00
parent 913430f144
commit b4103db72e
2 changed files with 9 additions and 2 deletions

View file

@ -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 ,

View file

@ -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, ' '],
]; ];
} }