From b5b02000c8913dddba59c5ae274b3994b977a72b Mon Sep 17 00:00:00 2001 From: Sergei Mikhailov Date: Fri, 8 Nov 2024 12:23:39 +0100 Subject: [PATCH] fix: parse record-typed candidate response values when reading the remote results.xml --- models/Mapper/ResultMapper.php | 29 ++++++---- test/Unit/models/Mapper/ResultMapperTest.php | 53 +++++++++++++++++-- .../result/simple-assessment-result.xml | 31 ++++++++++- 3 files changed, 100 insertions(+), 13 deletions(-) diff --git a/models/Mapper/ResultMapper.php b/models/Mapper/ResultMapper.php index ae2ac552..4dd33a48 100644 --- a/models/Mapper/ResultMapper.php +++ b/models/Mapper/ResultMapper.php @@ -15,8 +15,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * - * Copyright (c) 2019 (original work) Open Assessment Technologies SA; - * + * Copyright (c) 2019-2024 (original work) Open Assessment Technologies SA; */ namespace oat\taoResultServer\models\Mapper; @@ -325,14 +324,26 @@ protected function createResponseVariable(ResultResponseVariable $itemVariable) */ protected function serializeValueCollection(ValueCollection $valueCollection) { - $values = array_map( - function (Value $value) { - return $value->getValue(); - }, - iterator_to_array($valueCollection) - ); + $isRecord = false; + $values = []; + /** @var Value $value */ + foreach ($valueCollection as $value) { + $fieldIdentifier = $value->getFieldIdentifier(); + $baseType = $value->getBaseType(); + $isRecord = $fieldIdentifier && $baseType !== -1; + $values[] = $isRecord + ? [ + 'name' => $fieldIdentifier, + 'base' => [ + BaseType::getNameByConstant($baseType) => $value->getValue(), + ] + ] + : $value->getValue(); + } - return implode(';', $values); + return $isRecord + ? json_encode(['record' => $values]) + : implode(';', $values); } /** diff --git a/test/Unit/models/Mapper/ResultMapperTest.php b/test/Unit/models/Mapper/ResultMapperTest.php index 9a70a95d..73bae2f1 100644 --- a/test/Unit/models/Mapper/ResultMapperTest.php +++ b/test/Unit/models/Mapper/ResultMapperTest.php @@ -15,8 +15,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * - * Copyright (c) 2019 (original work) Open Assessment Technologies SA; - * + * Copyright (c) 2019-2024 (original work) Open Assessment Technologies SA; */ namespace oat\taoResultServer\test\Unit\models\Mapper; @@ -124,7 +123,7 @@ public function testGetTestVariablesWithTemplateVariables() public function testGetItemVariables() { $variablesByItemResult = $this->load()->getItemVariables(); - $this->assertCount(3, $variablesByItemResult); + $this->assertCount(4, $variablesByItemResult); $this->assertArrayHasKey('fixture-identifier-itemResult1', $variablesByItemResult); $this->assertArrayHasKey('fixture-identifier-itemResult2', $variablesByItemResult); @@ -186,6 +185,54 @@ public function testGetItemVariables() $this->assertEquals('fixture-value20', $variable4->getCorrectResponse()); $this->assertEquals('single', $variable4->getCardinality()); $this->assertEquals('identifier', $variable4->getBaseType()); + + /** @var taoResultServer_models_classes_ResponseVariable $variable5 */ + $variable5 = $variablesByItemResult['fixture-identifier-itemResult4'][0]; + $this->assertInstanceOf(\taoResultServer_models_classes_ResponseVariable::class, $variable5); + $this->assertEquals('fixture-identifier6', $variable5->getIdentifier()); + $this->assertEquals( + json_encode( + [ + 'record' => [ + [ + 'name' => 'correct', + 'base' => [ + 'integer' => '1' + ] + ], + [ + 'name' => 'candidateResponse', + 'base' => [ + 'string' => '' + ] + ], + [ + 'name' => 'score', + 'base' => [ + 'integer' => '1' + ] + ], + [ + 'name' => 'maxscore', + 'base' => [ + 'integer' => '1' + ] + ], + [ + 'name' => 'applet', + 'base' => [ + 'string' => 'payload' + ] + ] + ] + ] + ), + $variable5->getCandidateResponse() + ); + $this->assertEquals('record', $variable5->getCardinality()); + $this->assertNull($variable5->getBaseType()); + $epochDateTime = (new DateTime())->setTimestamp(explode(' ', $variable5->getEpoch())[1]); + $this->assertSame('2018-06-27T09:41:45', $epochDateTime->format('Y-m-d\TH:i:s')); } public function testGetItemVariablesWithTemplateVariables() diff --git a/test/resources/result/simple-assessment-result.xml b/test/resources/result/simple-assessment-result.xml index c107433c..0b2ba3c2 100644 --- a/test/resources/result/simple-assessment-result.xml +++ b/test/resources/result/simple-assessment-result.xml @@ -1,4 +1,22 @@ + + @@ -60,4 +78,15 @@ fixture-value19 - \ No newline at end of file + + + + 1 + + 1 + 1 + payload + + + +