* */ class taoQtiCommon_helpers_LegacyVariableFiller extends taoQtiCommon_helpers_AbstractVariableFiller { /** * Create a new LegacyVariableFiller object. * * @param IAssessmentItem $itemRef The item the variables you want to fill belong to. */ public function __construct(IAssessmentItem $itemRef) { parent::__construct($itemRef); } /** * Fille the variable $variableName with a correctly transformed * $clientSideValue. * * @param string $variableName The variable identifier you want to fill. * @param string $clientSideValue The value received from the client-side. * @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) { $variable = null; $outcomeDeclarations = $this->getItemRef()->getOutcomeDeclarations(); $responseDeclarations = $this->getItemRef()->getResponseDeclarations(); if (isset($responseDeclarations[$variableName]) === true) { $variable = ResponseVariable::createFromDataModel($responseDeclarations[$variableName]); } else if (isset($outcomeDeclarations[$variableName]) === true) { $variable = OutcomeVariable::createFromDataModel($outcomeDeclarations[$variableName]); } if (empty($variable) === true) { $itemId = $this->getItemRef()->getIdentifier(); $msg = "No variable declaration '${variableName}' found in '${itemId}'."; throw new OutOfBoundsException($msg); } common_Logger::d("Filling variable '" . $variable->getIdentifier() . "'."); if (is_array($clientSideValue) === false) { $clientSideValue = array($clientSideValue); } try { $finalValues = array(); foreach ($clientSideValue as $val) { $finalValues[] = self::transform($variable->getBaseType(), $val); } if ($variable->getCardinality() === Cardinality::SINGLE) { $variable->setValue($finalValues[0]); } else if ($variable->getCardinality() === Cardinality::MULTIPLE) { $variable->setValue(new MultipleContainer($variable->getBaseType(), $finalValues)); } else { // Cardinality is ORDERED. $variable->setValue(new OrderedContainer($variable->getBaseType(), $finalValues)); } return $variable; } catch (InvalidArgumentException $e) { if (is_array($clientSideValue)) { $clientSideValue = implode(',', $clientSideValue); } $msg = "An error occured while filling variable '${variableName}' with client-side value '${clientSideValue}':" . $e->getMessage(); throw new InvalidArgumentException($msg, 0, $e); } } /** * Transform $value in a $baseType datatype. * * @param integer $baseType * @param mixed $value * @return mixed * @throws InvalidArgumentException If $baseType is unknown. * @throws OutOfRangeException If $value cannot be transformed into $baseType datatype. */ protected static function transform($baseType, $value) { switch ($baseType) { case BaseType::BOOLEAN: if ($value === '') { return null; } else { return ($value === 'true') ? true : false; } break; case BaseType::DIRECTED_PAIR: if (is_array($value) === false) { $msg = "Cannot transform a value into a QTI directedPair, it is not an array, '" . gettype($value) . "' given"; throw new OutOfRangeException($msg); } else if (empty($value[0])) { $msg = "Cannot transform a value into a QTI directedPair, the first identifier was not given."; throw new OutOfRangeException($msg); } else if (empty($value[1])) { $msg = "Cannot transform a value into a QTI directedPair, the second identifier was not given."; throw new OutOfRangeException($msg); } else { return new QtiDirectedPair($value[0], $value[1]); } break; case BaseType::PAIR: if (is_array($value) === false) { $msg = "Cannot transform a value into a QTI pair, it is not an array, '" . gettype($value) . "' given"; throw new OutOfRangeException($msg); } else if (empty($value[0])) { $msg = "Cannot transform a value into a QTI pair, the first identifier was not given."; throw new OutOfRangeException($msg); } else if (empty($value[1])) { $msg = "Cannot transform a value into a QTI pair, the second identifier was not given."; throw new OutOfRangeException($msg); } else { return new QtiPair($value[0], $value[1]); } break; case BaseType::STRING: return new QtiString($value); break; case BaseType::IDENTIFIER: return ($value !== '') ? new QtiIdentifier($value) : null; break; case BaseType::INTEGER: return ($value !== '') ? new QtiInteger(intval($value)) : null; break; case BaseType::FLOAT: return ($value !== '') ? new QtiFloat(floatval($value)) : null; break; case BaseType::POINT: if (is_array($value) === false) { $msg = "Cannot transform a value into a QTI point, it is not an array, '" . gettype($value) . "' given"; throw new OutOfRangeException($msg); } else if (empty($value[0])) { $msg = "Cannot transform a value into a QTI point, X was not given."; throw new OutOfRangeException($msg); } else if (empty($value[1])) { $msg = "Cannot transform a value into a QTI point, Y was not given."; throw new OutOfRangeException($msg); } else { return new QtiPoint(intval($value[0]), intval($value[1])); } break; default: $msg = "Not supported baseType constant '${baseType}'."; throw new InvalidArgumentException($msg); break; } } }