Skip to content

Commit

Permalink
Merge pull request #406 from oat-sa/fix/TR-5267/record-value-field-id…
Browse files Browse the repository at this point in the history
…-and-base-type

fix: add `fieldIdentifier` and `baseType` properties to the `Value` class when the value forms part of a record
  • Loading branch information
wazelin authored Nov 8, 2024
2 parents e3ac149 + 12e83a3 commit 6285144
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/qtism/runtime/common/Variable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <jerome@taotesting.com>
* @license GPLv2
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand Down
30 changes: 30 additions & 0 deletions test/qtismtest/runtime/common/ResponseVariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,6 +55,7 @@ public function testCreateFromVariableDeclarationExtended(): void
</areaMapping>
</responseDeclaration>
');
/** @var ResponseDeclaration $responseDeclaration */
$responseDeclaration = $factory->createMarshaller($element)->unmarshall($element);
$responseVariable = ResponseVariable::createFromDataModel($responseDeclaration);
$this::assertInstanceOf(ResponseVariable::class, $responseVariable);
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 6285144

Please sign in to comment.