public function testMarshallMapping() { $identifier = 'response3'; $cardinality = Cardinality::SINGLE; $baseType = BaseType::FLOAT; $component = new ResponseDeclaration($identifier, $baseType, $cardinality); $entries = new MapEntryCollection(); $entries[] = new MapEntry(1.0, 1.1, true); $entries[] = new MapEntry(1.1, 1.2, false); $mapping = new Mapping($entries, 0.0); $component->setMapping($mapping); $marshaller = $this->getMarshallerFactory('2.1.0')->createMarshaller($component); $element = $marshaller->marshall($component); $this->assertInstanceOf('\\DOMElement', $element); $this->assertEquals('responseDeclaration', $element->nodeName); $this->assertEquals($identifier, $element->getAttribute('identifier')); $this->assertEquals('float', $element->getAttribute('baseType')); $this->assertEquals('single', $element->getAttribute('cardinality')); $correctResponses = $element->getElementsByTagName('defaultValue'); $this->assertEquals(0, $correctResponses->length); $mapping = $element->getElementsByTagName('mapping'); $this->assertEquals(1, $mapping->length); $entries = $mapping->item(0)->getElementsByTagName('mapEntry'); $this->assertEquals(2, $entries->length); $entry = $entries->item(0); $this->assertEquals('mapEntry', $entry->nodeName); $this->assertEquals('1.0', $entry->getAttribute('mapKey')); $this->assertEquals('1.1', $entry->getAttribute('mappedValue')); $entry = $entries->item(1); $this->assertEquals('mapEntry', $entry->nodeName); $this->assertEquals('1.1', $entry->getAttribute('mapKey')); $this->assertEquals('1.2', $entry->getAttribute('mappedValue')); }
protected function buildResponseDeclaration($responseIdentifier, $validation) { /** @var clozetext_validation $validation */ // Since we split {{response}} to multiple interactions, so we would have multiple <responseDeclaration> as needed as well $responseDeclarationCollection = new ResponseDeclarationCollection(); // Process `valid_response` foreach ($validation->get_valid_response()->get_value() as $index => $value) { // We make assumption about interaction identifier shall always be the appended with index, ie. `_0` $responseDeclaration = new ResponseDeclaration($responseIdentifier . '_' . $index); $responseDeclaration->setCardinality(Cardinality::SINGLE); $responseDeclaration->setBaseType(BaseType::STRING); $valueCollection = new ValueCollection(); $valueCollection->attach(new Value($value)); $validResponseScore = floatval($validation->get_valid_response()->get_score()); $mapEntriesCollection = new MapEntryCollection(); $mapEntriesCollection->attach(new MapEntry($value, $validResponseScore, $this->isCaseSensitive)); if (count($validation->get_alt_responses()) > 0) { /** @var clozetext_validation_alt_responses_item $alt */ foreach ($validation->get_alt_responses() as $alt) { // Assuming if (!is_null($alt->get_value()) && isset($alt->get_value()[$index])) { $alternativeValue = $alt->get_value()[$index]; $alternativeScore = floatval($alt->get_score()); $valueCollection->attach(new Value($alternativeValue)); $mapEntriesCollection->attach(new MapEntry($alternativeValue, $alternativeScore, $this->isCaseSensitive)); } } } $responseDeclaration->setCorrectResponse(new CorrectResponse($valueCollection)); $responseDeclaration->setMapping(new Mapping($mapEntriesCollection)); $responseDeclarationCollection->attach($responseDeclaration); } return $responseDeclarationCollection; }
/** * Unmarshall a DOMElement object corresponding to a QTI responseDeclaration element. * * @param \DOMElement $element A DOMElement object. * @return \qtism\data\QtiComponent A ResponseDeclaration object. * @throws \qtism\data\storage\xml\marshalling\UnmarshallingException */ protected function unmarshall(DOMElement $element) { try { $baseComponent = parent::unmarshall($element); $object = new ResponseDeclaration($baseComponent->getIdentifier()); $object->setBaseType($baseComponent->getBaseType()); $object->setCardinality($baseComponent->getCardinality()); $object->setDefaultValue($baseComponent->getDefaultValue()); $correctResponseElts = self::getChildElementsByTagName($element, 'correctResponse'); if (count($correctResponseElts) === 1) { $correctResponseElt = $correctResponseElts[0]; $marshaller = $this->getMarshallerFactory()->createMarshaller($correctResponseElt, array($baseComponent->getBaseType())); $object->setCorrectResponse($marshaller->unmarshall($correctResponseElt)); } $mappingElts = self::getChildElementsByTagName($element, 'mapping'); if (count($mappingElts) === 1) { $mappingElt = $mappingElts[0]; $marshaller = $this->getMarshallerFactory()->createMarshaller($mappingElt, array($baseComponent->getBaseType())); $object->setMapping($marshaller->unmarshall($mappingElt)); } $areaMappingElts = self::getChildElementsByTagName($element, 'areaMapping'); if (count($areaMappingElts) === 1) { $areaMappingElt = $areaMappingElts[0]; $marshaller = $this->getMarshallerFactory()->createMarshaller($areaMappingElt); $object->setAreaMapping($marshaller->unmarshall($areaMappingElt)); } return $object; } catch (InvalidArgumentException $e) { $msg = "An unexpected error occured while unmarshalling the responseDeclaration."; throw new UnmarshallingException($msg, $element, $e); } }
/** * @param string $identifier * @param array $mapping Mapping of `mapKey` to [`mappedValue`, `caseSensitive`] * ie. { * "York" => [1, false] * "york" => [1, false] * "Manhattan" => [1, true] * } * * @return \qtism\data\state\ResponseDeclaration */ public static function buildWithMapping($identifier, array $mapping, $mapEntryKeyType = null) { $responseDeclaration = new ResponseDeclaration($identifier); $mapEntryCollection = new MapEntryCollection(); foreach ($mapping as $mapKey => $values) { $mappedValue = $values[0]; $caseSensitive = isset($values[1]) ? $values[1] : false; if ($mapEntryKeyType) { $keyParts = explode(' ', $mapKey); $mapKey = new QtiDirectedPair($keyParts[0], $keyParts[1]); } $mapEntryCollection->attach(new MapEntry($mapKey, floatval($mappedValue), $caseSensitive)); } $responseDeclaration->setMapping(new Mapping($mapEntryCollection)); return $responseDeclaration; }
protected function buildResponseDeclaration($responseIdentifier, $validation) { $responseDeclaration = new ResponseDeclaration($responseIdentifier); $responseDeclaration->setCardinality(Cardinality::SINGLE); $responseDeclaration->setBaseType(BaseType::STRING); $correctResponseBuilder = new QtiCorrectResponseBuilder(); $responseDeclaration->setCorrectResponse($correctResponseBuilder->build($validation)); $mappingResponseBuilder = new QtiMappingBuilder(); $mapping = $mappingResponseBuilder->build($validation); $responseDeclaration->setMapping($mapping); foreach ($mapping->getMapEntries() as $mapEntry) { /** @var MapEntry $mapEntry */ $mapEntry->setCaseSensitive($this->isCaseSensitive); } return $responseDeclaration; }