From 12e83a3c66bf729cae1909136ea148e9ce9b25ed Mon Sep 17 00:00:00 2001 From: Sergei Mikhailov Date: Thu, 7 Nov 2024 17:35:24 +0100 Subject: [PATCH] fix: add `fieldIdentifier` and `baseType` properties to the `Value` class when the value forms part of a record --- src/qtism/runtime/common/Variable.php | 23 +++++++------- .../runtime/common/ResponseVariableTest.php | 30 +++++++++++++++++++ 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/qtism/runtime/common/Variable.php b/src/qtism/runtime/common/Variable.php index f8a3c137f..8923259ff 100644 --- a/src/qtism/runtime/common/Variable.php +++ b/src/qtism/runtime/common/Variable.php @@ -15,7 +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) 2013-2020 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); + * Copyright (c) 2013-2024 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); * * @author Jérôme Bogaerts * @license GPLv2 @@ -563,10 +563,10 @@ public function getDataModelValues(): ValueCollection break; case Cardinality::RECORD: - foreach ($this->getValue() as $v) { + foreach ($this->getValue() as $k => $v) { $values[] = $v === null - ? $this->createRecordNullValue() - : $this->createRecordValue($v); + ? $this->createRecordNullValue($k) + : $this->createRecordValue($v, $k); } break; } @@ -583,26 +583,23 @@ private function createValue(QtiDatatype $value): Value return new Value($value instanceof QtiScalar ? $value->getValue() : $value); } - /** - * @param QtiDatatype $value - * @return Value - */ - private function createRecordValue(QtiDatatype $value): Value + private function createRecordValue(QtiDatatype $qtiValue, string $fieldIdentifier): Value { - $value = $this->createValue($value); + $value = $this->createValue($qtiValue); $value->setPartOfRecord(true); + $value->setFieldIdentifier($fieldIdentifier); + $value->setBaseType($qtiValue->getBaseType()); return $value; } /** * Creates a null value to fill a gap in a record set. - * - * @return Value */ - private function createRecordNullValue(): Value + private function createRecordNullValue(string $fieldIdentifier): Value { $value = new Value(null); $value->setPartOfRecord(true); + $value->setFieldIdentifier($fieldIdentifier); return $value; } diff --git a/test/qtismtest/runtime/common/ResponseVariableTest.php b/test/qtismtest/runtime/common/ResponseVariableTest.php index 119ce9f91..e0aef9ee2 100644 --- a/test/qtismtest/runtime/common/ResponseVariableTest.php +++ b/test/qtismtest/runtime/common/ResponseVariableTest.php @@ -14,6 +14,7 @@ use qtism\common\enums\Cardinality; use qtism\data\state\AreaMapping; use qtism\data\state\Mapping; +use qtism\data\state\ResponseDeclaration; use qtism\runtime\common\MultipleContainer; use qtism\runtime\common\OrderedContainer; use qtism\runtime\common\RecordContainer; @@ -54,6 +55,7 @@ public function testCreateFromVariableDeclarationExtended(): void '); + /** @var ResponseDeclaration $responseDeclaration */ $responseDeclaration = $factory->createMarshaller($element)->unmarshall($element); $responseVariable = ResponseVariable::createFromDataModel($responseDeclaration); $this::assertInstanceOf(ResponseVariable::class, $responseVariable); @@ -126,12 +128,16 @@ public function testGetScalarDataModelValuesSingleCardinality(): void $this::assertCount(1, $values); $this::assertSame(10, $values[0]->getValue()); + $this::assertSame('', $values[0]->getFieldIdentifier()); + $this::assertSame(-1, $values[0]->getBaseType()); $responseVariable = new ResponseVariable('MYVAR', Cardinality::SINGLE, BaseType::FLOAT, new QtiFloat(10.1)); $values = $responseVariable->getDataModelValues(); $this::assertCount(1, $values); $this::assertSame(10.1, $values[0]->getValue()); + $this::assertSame('', $values[0]->getFieldIdentifier()); + $this::assertSame(-1, $values[0]->getBaseType()); } public function testGetNonScalarDataModelValuesSingleCardinality(): void @@ -143,6 +149,8 @@ public function testGetNonScalarDataModelValuesSingleCardinality(): void $this::assertCount(1, $values); $this::assertTrue($qtiPair->equals($values[0]->getValue())); + $this::assertSame('', $values[0]->getFieldIdentifier()); + $this::assertSame(-1, $values[0]->getBaseType()); // QtiPoint $qtiPoint = new QtiPoint(1, 1); @@ -151,6 +159,8 @@ public function testGetNonScalarDataModelValuesSingleCardinality(): void $this::assertCount(1, $values); $this::assertTrue($qtiPoint->equals($values[0]->getValue())); + $this::assertSame('', $values[0]->getFieldIdentifier()); + $this::assertSame(-1, $values[0]->getBaseType()); // QtiFile $fileManager = new FileSystemFileManager(); @@ -186,7 +196,11 @@ public function testGetScalarDataModelValuesMultipleCardinality(): void $this::assertCount(2, $values); $this::assertEquals(10, $values[0]->getValue()); + $this::assertSame('', $values[0]->getFieldIdentifier()); + $this::assertSame(-1, $values[0]->getBaseType()); $this::assertEquals(12, $values[1]->getValue()); + $this::assertSame('', $values[1]->getFieldIdentifier()); + $this::assertSame(-1, $values[1]->getBaseType()); } public function testGetNonScalarDataModelValuesMultipleCardinality(): void @@ -208,7 +222,11 @@ public function testGetNonScalarDataModelValuesMultipleCardinality(): void $this::assertCount(2, $values); $this::assertTrue($qtiPair1->equals($values[0]->getValue())); + $this::assertSame('', $values[0]->getFieldIdentifier()); + $this::assertSame(-1, $values[0]->getBaseType()); $this::assertTrue($qtiPair2->equals($values[1]->getValue())); + $this::assertSame('', $values[1]->getFieldIdentifier()); + $this::assertSame(-1, $values[1]->getBaseType()); // QtiPoint $qtiPoint1 = new QtiPoint(0, 0); @@ -227,7 +245,11 @@ public function testGetNonScalarDataModelValuesMultipleCardinality(): void $this::assertCount(2, $values); $this::assertTrue($qtiPoint1->equals($values[0]->getValue())); + $this::assertSame('', $values[0]->getFieldIdentifier()); + $this::assertSame(-1, $values[0]->getBaseType()); $this::assertTrue($qtiPoint2->equals($values[1]->getValue())); + $this::assertSame('', $values[1]->getFieldIdentifier()); + $this::assertSame(-1, $values[1]->getBaseType()); } public function testGetDataModelValuesRecordCardinality(): void @@ -251,9 +273,17 @@ public function testGetDataModelValuesRecordCardinality(): void $this::assertCount(4, $values); $this::assertSame(12, $values[0]->getValue()); + $this::assertSame('twelve', $values[0]->getFieldIdentifier()); + $this::assertSame(BaseType::INTEGER, $values[0]->getBaseType()); $this::assertSame('bar', $values[1]->getValue()); + $this::assertSame('foo', $values[1]->getFieldIdentifier()); + $this::assertSame(BaseType::STRING, $values[1]->getBaseType()); $this::assertNull($values[2]->getValue()); + $this::assertSame('null', $values[2]->getFieldIdentifier()); + $this::assertSame(-1, $values[2]->getBaseType()); $this::assertTrue($qtiPair->equals($values[3]->getValue())); + $this::assertSame('pair', $values[3]->getFieldIdentifier()); + $this::assertSame(BaseType::PAIR, $values[3]->getBaseType()); } public function testClone(): void