MDL-76362 qtype_numerical: Support empty units for apply_units

This commit is contained in:
Andrew Nicols
2023-01-23 09:16:07 +08:00
parent 5f412b3301
commit 12b36d2a32
2 changed files with 9 additions and 2 deletions
+5 -1
View File
@@ -651,9 +651,13 @@ class qtype_numerical_answer_processor {
* by the unit multiplier, if any, and the unit string, for reference.
*/
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
// of writing e to e.
$response = str_replace(' ', '', $response ?? '');
$response = str_replace(' ', '', $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 ,
@@ -62,7 +62,7 @@ class answerprocessor_test extends \advanced_testcase {
$rc = new \ReflectionClass($ap);
$rcm = $rc->getMethod('parse_response');
$rcm->setAccessible(True);
$rcm->setAccessible(true);
$this->assertEquals($expected, $rcm->invoke($ap, $args));
}
@@ -200,6 +200,9 @@ class answerprocessor_test extends \advanced_testcase {
[1, 'frogs', null, '1 frogs'],
[null, null, null, '. m/s'],
[null, null, null, null],
[null, null, null, ''],
[null, null, null, ' '],
];
}