public function testMarshallMatchTable() { $identifier = 'outcome3'; $cardinality = Cardinality::SINGLE; $baseType = BaseType::FLOAT; $component = new OutcomeDeclaration($identifier, $baseType, $cardinality); $entries = new MatchTableEntryCollection(); $entries[] = new MatchTableEntry(1, 1.5); $entries[] = new MatchTableEntry(2, 2.5); $matchTable = new MatchTable($entries); $component->setLookupTable($matchTable); $marshaller = $this->getMarshallerFactory()->createMarshaller($component); $element = $marshaller->marshall($component); $this->assertInstanceOf('\\DOMElement', $element); $this->assertEquals('outcomeDeclaration', $element->nodeName); $this->assertEquals($identifier, $element->getAttribute('identifier')); $this->assertEquals('float', $element->getAttribute('baseType')); $this->assertEquals('single', $element->getAttribute('cardinality')); $defaultValues = $element->getElementsByTagName('defaultValue'); $this->assertEquals(0, $defaultValues->length); $lookupTable = $element->getElementsByTagName('matchTable'); $this->assertEquals(1, $lookupTable->length); $entries = $lookupTable->item(0)->getElementsByTagName('matchTableEntry'); $this->assertEquals(2, $entries->length); $entry = $entries->item(0); $this->assertEquals('matchTableEntry', $entry->nodeName); $this->assertEquals('1', $entry->getAttribute('sourceValue')); $this->assertEquals('1.5', $entry->getAttribute('targetValue')); $entry = $entries->item(1); $this->assertEquals('matchTableEntry', $entry->nodeName); $this->assertEquals('2', $entry->getAttribute('sourceValue')); $this->assertEquals('2.5', $entry->getAttribute('targetValue')); }
private function buildOutcomeDeclarations() { // Set <outcomeDeclaration> with assumption default value is always 0 $outcomeDeclaration = new OutcomeDeclaration('SCORE', BaseType::INTEGER); $valueCollection = new ValueCollection(); $valueCollection->attach(new Value(0)); $outcomeDeclaration->setDefaultValue(new DefaultValue($valueCollection)); $outcomeDeclarationCollection = new OutcomeDeclarationCollection(); $outcomeDeclarationCollection->attach($outcomeDeclaration); return $outcomeDeclarationCollection; }
/** * Unmarshall a DOMElement object corresponding to a QTI outcomeDeclaration element. * * @param DOMElement $element A DOMElement object. * @return QtiComponent An OutcomeDeclaration object. * @throws UnmarshallingException */ protected function unmarshall(DOMElement $element) { try { $baseComponent = parent::unmarshall($element); $object = new OutcomeDeclaration($baseComponent->getIdentifier()); $object->setBaseType($baseComponent->getBaseType()); $object->setCardinality($baseComponent->getCardinality()); $object->setDefaultValue($baseComponent->getDefaultValue()); // deal with views. if (($views = static::getDOMElementAttributeAs($element, 'view')) != null) { $viewCollection = new ViewCollection(); foreach (explode(" ", $views) as $viewName) { $viewCollection[] = View::getConstantByName($viewName); } $object->setViews($viewCollection); } // deal with interpretation. if (($interpretation = static::getDOMElementAttributeAs($element, 'interpretation')) != null) { $object->setInterpretation($interpretation); } // deal with longInterpretation. if (($longInterpretation = static::getDOMElementAttributeAs($element, 'longInterpretation')) != null) { $object->setLongInterpretation($longInterpretation); } // deal with normalMaximum. if (($normalMaximum = static::getDOMElementAttributeAs($element, 'normalMaximum', 'float')) !== null) { $object->setNormalMaximum($normalMaximum); } // deal with normalMinimum. if (($normalMinimum = static::getDOMElementAttributeAs($element, 'normalMinimum', 'float')) !== null) { $object->setNormalMinimum($normalMinimum); } // deal with matseryValue. if (($masteryValue = static::getDOMElementAttributeAs($element, 'masteryValue', 'float')) !== null) { $object->setMasteryValue($masteryValue); } // deal with lookupTable. $interpolationTables = $element->getElementsByTagName('interpolationTable'); $matchTable = $element->getElementsByTagName('matchTable'); if ($interpolationTables->length == 1 || $matchTable->length == 1) { // we have a lookupTable defined. $lookupTable = null; if ($interpolationTables->length == 1) { $lookupTable = $interpolationTables->item(0); } else { $lookupTable = $matchTable->item(0); } $lookupTableMarshaller = $this->getMarshallerFactory()->createMarshaller($lookupTable, array($object->getBaseType())); $object->setLookupTable($lookupTableMarshaller->unmarshall($lookupTable)); } return $object; } catch (InvalidArgumentException $e) { $msg = "An unexpected error occured while unmarshalling the outcomeDeclaration."; throw new UnmarshallingException($msg, $element, $e); } }
/** * @depends testMarshallMatchTable21 */ public function testMarshallMatchTable20() { // This should fail because no marshaller is known for lookupTable sub classes // in a QTI 2.0 context. $identifier = 'outcome3'; $cardinality = Cardinality::SINGLE; $baseType = BaseType::FLOAT; $component = new OutcomeDeclaration($identifier, $baseType, $cardinality); $entries = new MatchTableEntryCollection(); $entries[] = new MatchTableEntry(1, 1.5); $matchTable = new MatchTable($entries); $component->setLookupTable($matchTable); $expectedMsg = "No mapping entry found for QTI class name 'matchTable'."; $this->setExpectedException('qtism\\data\\storage\\xml\\marshalling\\MarshallerNotFoundException', $expectedMsg); $marshaller = $this->getMarshallerFactory('2.0.0')->createMarshaller($component); $element = $marshaller->marshall($component); }
/** * Append an outcome declaration to a test. * * This method will append an outcome declaration with identifier $varName, single cardinality * to a given QTI-SDK AssessmentTest $test object. * * @param qtism\data\AssessmentTest $test A QTI-SDK AssessmentTest object. * @param string $varName The variable name to be used for the outcome declaration. * @param integer $baseType A QTI-SDK Base Type. * @param mixed (optional) A default value for the variable. */ public static function appendOutcomeDeclarationToTest(AssessmentTest $test, $varName, $baseType, $defaultValue = null) { $outcomeDeclarations = $test->getOutcomeDeclarations(); $outcome = new OutcomeDeclaration($varName, $baseType, Cardinality::SINGLE); if ($defaultValue !== null) { $outcome->setDefaultValue(new DefaultValue(new ValueCollection(array(new Value($defaultValue, $baseType))))); } $outcomeDeclarations->attach($outcome); }