/** * Fill the variable $variableName with a correctly transformed $clientSideValue. * * @param string $variableName The variable identifier you want to fill. * @param array $clientSideValue The value received from the client-side representing the value of the variable with identifier $variableName. * @return Variable A Variable object filled with a correctly transformed $clientSideValue. * @throws OutOfBoundsException If no variable with $variableName is described in the item. * @throws OutOfRangeException If the $clientSideValue does not fit the target variable's baseType. */ public function fill($variableName, $clientSideValue) { $variableDeclaration = $this->findVariableDeclaration($variableName); if ($variableDeclaration === false) { $itemId = $this->getItemRef()->getIdentifier(); $msg = "Variable declaration with identifier '{$variableName}' not found in item '{$itemId}'."; throw new OutOfBoundsException($msg); } // Create Runtime Variable from Data Model. $runtimeVar = $variableDeclaration instanceof ResponseDeclaration ? ResponseVariable::createFromDataModel($variableDeclaration) : OutcomeVariable::createFromDataModel($variableDeclaration); // Set the data into the runtime variable thanks to the PCI JSON Unmarshaller // from QTISM. try { $unmarshaller = new PciJsonUnmarshaller(taoQtiCommon_helpers_Utils::getFileDatatypeManager()); $value = $unmarshaller->unmarshall($clientSideValue); // Dev's note: // The PCI JSON Representation format does make the difference between multiple and ordered containers. // We then have to juggle a bit if the target variable has ordered cardinality. if ($value !== null && $value->getCardinality() === Cardinality::MULTIPLE && $variableDeclaration->getCardinality() === Cardinality::ORDERED) { $value = new OrderedContainer($value->getBaseType(), $value->getArrayCopy()); } $runtimeVar->setValue($value); } catch (PciJsonUnmarshallingException $e) { $strClientSideValue = mb_substr(var_export($clientSideValue, true), 0, 50, TAO_DEFAULT_ENCODING); $msg = "Unable to put value '{$strClientSideValue}' into variable '{$variableName}'."; throw new OutOfRangeException($msg, 0, $e); } return $runtimeVar; }