/** * Add a variable to be transformed into its IMS PCI JSON Representation * for a later output. * * @param Variable $variable */ public function addVariable(Variable $variable) { $output =& $this->getOutput(); $varName = $variable->getIdentifier(); $marshaller = new taoQtiCommon_helpers_PciJsonMarshaller(); $output[$varName] = $marshaller->marshall($variable->getValue(), taoQtiCommon_helpers_PciJsonMarshaller::MARSHALL_ARRAY); }
/** * Add a variable (Variable object) to the current context. Variables that can be set using this method * must have simple variable identifiers, in order to target the global AssessmentTestSession scope only. * * @param Variable $variable A Variable object to add to the current context. * @throws OutOfRangeException If the identifier of the given $variable is not a simple variable identifier (no prefix, no sequence number). */ public function setVariable(Variable $variable) { try { $v = new VariableIdentifier($variable->getIdentifier()); if ($v->hasPrefix() === true) { $msg = "The variables set to the AssessmentTestSession global scope must have simple variable identifiers. "; $msg .= "'" . $v->__toString() . "' given."; throw new OutOfRangeException($msg); } } catch (InvalidArgumentException $e) { $variableIdentifier = $variable->getIdentifier(); $msg = "The identifier '{$variableIdentifier}' of the variable to set is invalid."; throw new OutOfRangeException($msg, 0, $e); } $data =& $this->getDataPlaceHolder(); $data[$v->__toString()] = $variable; }
/** * @dataProvider fillVariableProvider * * @param IAssessmentItem $itemRef The reference item. * @param string $variableIdentifier The identifier of the variable. * @param array $data Client-side data. * @param Variable $expectedVariable */ public function testFillVariable(array $data, Variable $expectedVariable) { // Non-time dependent basic item in 'Yoda English'. $itemRef = new AssessmentItem('Q01', 'Question 01', false, 'en-YO'); $outcomeDeclarations = new OutcomeDeclarationCollection(); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME1', BaseType::FLOAT, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME2', BaseType::INTEGER, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME3', BaseType::BOOLEAN, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME4', BaseType::STRING, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME5', BaseType::POINT, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME6', BaseType::PAIR, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME7', BaseType::DIRECTED_PAIR, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME8', BaseType::DURATION, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME9', BaseType::URI, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME10', BaseType::IDENTIFIER, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME11', BaseType::INT_OR_IDENTIFIER, Cardinality::SINGLE); $outcomeDeclarations[] = new OutcomeDeclaration('OUTCOME12', BaseType::INTEGER, Cardinality::SINGLE); $responseDeclarations = new ResponseDeclarationCollection(); $responseDeclarations[] = new ResponseDeclaration('RESPONSE1', BaseType::IDENTIFIER, Cardinality::SINGLE); $responseDeclarations[] = new ResponseDeclaration('RESPONSE2', BaseType::IDENTIFIER, Cardinality::MULTIPLE); $responseDeclarations[] = new ResponseDeclaration('RESPONSE3', BaseType::DIRECTED_PAIR, Cardinality::ORDERED); $responseDeclarations[] = new ResponseDeclaration('RESPONSE4', -1, Cardinality::RECORD); $responseDeclarations[] = new ResponseDeclaration('RESPONSE5', -1, Cardinality::RECORD); $responseDeclarations[] = new ResponseDeclaration('RESPONSE6', -1, Cardinality::RECORD); $responseDeclarations[] = new ResponseDeclaration('RESPONSE7', -1, Cardinality::RECORD); $responseDeclarations[] = new ResponseDeclaration('RESPONSE8', BaseType::BOOLEAN, Cardinality::MULTIPLE); $itemRef->setOutcomeDeclarations($outcomeDeclarations); $itemRef->setResponseDeclarations($responseDeclarations); $filler = new taoQtiCommon_helpers_PciVariableFiller($itemRef); $variable = $filler->fill($expectedVariable->getIdentifier(), $data); $this->assertEquals($expectedVariable->getIdentifier(), $variable->getIdentifier()); $this->assertEquals($expectedVariable->getBaseType(), $variable->getBaseType()); $this->assertEquals($expectedVariable->getCardinality(), $variable->getCardinality()); $this->assertEquals(get_class($expectedVariable), get_class($variable)); if ($expectedVariable->getValue() === null) { $this->assertSame($expectedVariable->getValue(), $variable->getValue()); } else { $this->assertTrue($expectedVariable->getValue()->equals($variable->getValue())); } }
/** * Add a variable to the State that has to be built for the client-side. The final output * array is available through the tao_helpers_StateOutput::getOutput() method. * * @param Variable $variable A QTI Runtime Variable */ public function addVariable(Variable $variable) { $output =& $this->getOutput(); $baseType = $variable->getBaseType(); $cardinality = $variable->getCardinality(); $varName = $variable->getIdentifier(); if ($cardinality === Cardinality::SINGLE) { $singleValue = $variable->getValue(); if (is_null($singleValue) === true) { $output[$varName] = array(''); return; } $values = array($variable->getValue()); } else { $containerValue = $variable->getValue(); if (is_null($containerValue) === true || $containerValue->isNull() === true) { $output[$varName] = array(); return; } $values = $containerValue->getArrayCopy(true); } $output[$varName] = array(); foreach ($values as $val) { if (is_null($val) === true) { $output[$varName][] = ''; } else { if (gettype($val) === 'boolean') { $output[$varName][] = $val === true ? 'true' : 'false'; } else { if ($val instanceof QtiPoint) { $output[$varName][] = array('' . $val->getX(), '' . $val->getY()); } else { if ($val instanceof QtiPair) { $output[$varName][] = array($val->getFirst(), $val->getSecond()); } else { if (gettype($val) === 'object') { $output[$varName][] = $val->__toString(); } else { $output[$varName][] = '' . $val; } } } } } } }