/** * Parse an array of DOMElement to array of Resource models * * @param [DOMElement] $resourcesListElements * @return array */ public function map(array $resourcesListElements) { $resources = []; foreach ($resourcesListElements as $resourceElement) { $resource = new Resource(); $resource->setHref(Marshaller::getDOMElementAttributeAs($resourceElement, 'href')); $resource->setIdentifier(Marshaller::getDOMElementAttributeAs($resourceElement, 'identifier')); $resource->setType(Marshaller::getDOMElementAttributeAs($resourceElement, 'type')); // Mapping to File models $fileListElements = Marshaller::getChildElementsByTagName($resourceElement, 'file'); $resource->setFiles($this->mapFileElements($fileListElements)); // Mapping to Dependency models $dependencyListElements = Marshaller::getChildElementsByTagName($resourceElement, 'dependency'); $resource->setDependencies($this->mapDependencyElements($dependencyListElements)); // Mapping its metadata $metadataListElements = Marshaller::getChildElementsByTagName($resourceElement, 'metadata'); if (count($metadataListElements) > 0) { $metadataMapper = new MetadataMapper(); $flattenedMetadatas = $metadataMapper->map($metadataListElements[0]); $resource->setMetadata($flattenedMetadatas); } // Resource must have unique id ?? $resources[$resource->getIdentifier()] = $resource; } return $resources; }
public function map(array $organisationElements) { $organisations = []; foreach ($organisationElements as $organisationElement) { $organisation = new Organization(); $organisation->setIdentifier(Marshaller::getDOMElementAttributeAs($organisationElement, 'identifier')); $organisation->setStructure(Marshaller::getDOMElementAttributeAs($organisationElement, 'structure')); $titleElements = Marshaller::getChildElementsByTagName($organisationElement, 'title'); if (count($titleElements) >= 1) { $organisation->setTitle($titleElements[0]->nodeValue); } $itemElements = Marshaller::getChildElementsByTagName($organisationElement, 'item'); $items = []; foreach ($itemElements as $itemElement) { $item = new Item(); $item->setTitle(Marshaller::getChildElementsByTagName($itemElement, 'title')); $item->setIdentifier(Marshaller::getDOMElementAttributeAs($itemElement, 'identifier')); $item->setIdentifierref(Marshaller::getDOMElementAttributeAs($itemElement, 'identifierref')); $item->setIsvisible(Marshaller::getDOMElementAttributeAs($itemElement, 'isvisible')); $items[] = $item; } $organisation->setItems($items); $organisations[] = $organisation; } return $organisations; }
public function testGetChildElementsByTagNameEmpty() { $dom = new DOMDocument('1.0', 'UTF-8'); // There is only 1 child but at the second level. Nothing // should be found. $dom->loadXML('<parent><parent><child/></parent></parent>'); $element = $dom->documentElement; $this->assertEquals(0, count(Marshaller::getChildElementsByTagName($element, 'child'))); }
protected function appendChildren(DOMDocumentFragment $fragment, QtiComponent $component, $base = '') { parent::appendChildren($fragment, $component, $base); // Retrieve the two rendered simpleMatchSets and shuffle if needed. $currentSet = 0; $choiceElts = array(); $simpleMatchSetElts = array(); for ($i = 0; $i < $fragment->firstChild->childNodes->length; $i++) { $n = $fragment->firstChild->childNodes->item($i); if (Utils::hasClass($n, 'qti-simpleMatchSet') === true) { $simpleMatchSetElts[] = $n; $sets = $component->getSimpleMatchSets(); if ($this->getRenderingEngine()->mustShuffle() === true) { Utils::shuffle($n, new ShufflableCollection($sets[$currentSet]->getSimpleAssociableChoices()->getArrayCopy())); } // Retrieve the two content of the two simpleMatchSets, separately. $choiceElts[] = Marshaller::getChildElementsByTagName($n, 'div'); $currentSet++; } } // simpleMatchSet class cannot be rendered into a table :/ foreach ($simpleMatchSetElts as $sms) { $fragment->firstChild->removeChild($sms); } $table = $fragment->ownerDocument->createElement('table'); $fragment->firstChild->appendChild($table); // Build the table header. $tr = $fragment->ownerDocument->createElement('tr'); $table->appendChild($tr); // Empty upper left cell. $tr->appendChild($fragment->ownerDocument->createElement('th')); for ($i = 0; $i < count($choiceElts[1]); $i++) { $tr->appendChild(XmlUtils::changeElementName($choiceElts[1][$i], 'th')); } // Build all remaining rows. for ($i = 0; $i < count($choiceElts[0]); $i++) { $tr = $fragment->ownerDocument->createElement('tr'); $tr->appendChild(XmlUtils::changeElementName($choiceElts[0][$i], 'th')); $table->appendChild($tr); for ($j = 0; $j < count($choiceElts[1]); $j++) { $input = $fragment->ownerDocument->createElement('input'); $input->setAttribute('type', 'checkbox'); $td = $fragment->ownerDocument->createElement('td'); $td->appendChild($input); $tr->appendChild($td); } } }
public function parseManifestElement(\DOMElement $rootElement) { // Manifest mapping start! $manifest = new Manifest(); $manifest->setIdentifier(Marshaller::getDOMElementAttributeAs($rootElement, 'identifier')); // Mapping <resource>(s) to Resource model $resourcesElement = Marshaller::getChildElementsByTagName($rootElement, 'resources'); if (!empty($resourcesElement)) { if (count($resourcesElement) !== 1) { throw new MappingException('Resources tag must occur once'); } $resourceMapper = new ResourcesMapper(); $resourcesListElements = Marshaller::getChildElementsByTagName($resourcesElement[0], 'resource'); $resources = $resourceMapper->map($resourcesListElements); $manifest->setResources($resources); } // Mapping <organisation>(s) to Organisation model $organizationElements = Marshaller::getChildElementsByTagName($rootElement, 'organizations'); if (!empty($organizationElements)) { if (count($organizationElements) !== 1) { throw new MappingException('Organisations tag must occur once'); } $organisationsMapper = new OrganizationsMapper(); $organisationListElements = Marshaller::getChildElementsByTagName($organizationElements[0], 'organization'); $organisations = $organisationsMapper->map($organisationListElements); $manifest->setOrganizations($organisations); } // Mapping package Metadata $metadataElement = Marshaller::getChildElementsByTagName($rootElement, 'metadata'); if (!empty($metadataElement)) { if (count($metadataElement) !== 1) { throw new MappingException('Metadata tag must occur once'); } $metadataMapper = new MetadataMapper(); $manifest->setMetadata($metadataMapper->map($metadataElement[0])); } // Mapping sub-manifest $subManifestElement = Marshaller::getChildElementsByTagName($rootElement, 'manifest'); if (!empty($subManifestElement)) { if (count($subManifestElement) !== 1) { throw new MappingException('Manifest tag must occur once'); } $manifest->setManifest($this->parseManifestElement($subManifestElement[0])); } return $manifest; }
/** * @see \qtism\runtime\rendering\markup\xhtml\AbstractXhtmlRenderer::appendChildren() */ protected function appendChildren(DOMDocumentFragment $fragment, QtiComponent $component, $base = '') { parent::appendChildren($fragment, $component, $base); // Retrieve the two rendered simpleMatchSets and shuffle if needed. $currentSet = 0; $choiceElts = array(); $simpleMatchSetElts = array(); for ($i = 0; $i < $fragment->firstChild->childNodes->length; $i++) { $n = $fragment->firstChild->childNodes->item($i); if (Utils::hasClass($n, 'qti-simpleMatchSet') === true) { $simpleMatchSetElts[] = $n; $sets = $component->getSimpleMatchSets(); if ($this->getRenderingEngine()->getShufflingPolicy() === AbstractMarkupRenderingEngine::CONTEXT_AWARE && $component->mustShuffle()) { Utils::shuffle($n, new ShufflableCollection($sets[$currentSet]->getSimpleAssociableChoices()->getArrayCopy())); } // Retrieve the two content of the two simpleMatchSets, separately. $choiceElts[] = Marshaller::getChildElementsByTagName($n, 'li'); $currentSet++; } } // simpleMatchSet class cannot be rendered into a table :/ foreach ($simpleMatchSetElts as $sms) { $fragment->firstChild->removeChild($sms); } $table = $fragment->ownerDocument->createElement('table'); $fragment->firstChild->appendChild($table); // Build the table header. $tr = $fragment->ownerDocument->createElement('tr'); $table->appendChild($tr); // Empty upper left cell. $tr->appendChild($fragment->ownerDocument->createElement('th')); $ifVerticalStatementsStorage = array(); for ($i = 0; $i < count($choiceElts[1]); $i++) { $ifStatements = Utils::extractStatements($choiceElts[1][$i], Utils::EXTRACT_IF); $incStatements = Utils::extractStatements($choiceElts[1][$i], Utils::EXTRACT_INCLUDE); if (empty($incStatements) === false) { $tr->appendChild($incStatements[0]); } if (empty($ifStatements) === false) { $ifVerticalStatementsStorage[$i] = $ifStatements; $tr->appendChild($ifStatements[0]); } $th = XmlUtils::changeElementName($choiceElts[1][$i], 'th'); $tr->appendChild($th); if (empty($incStatements) === false) { $th->parentNode->insertBefore($incStatements[1], $th->nextSibling); } if (empty($ifStatements) === false) { $th->parentNode->insertBefore($ifStatements[1], $th->nextSibling); } } // Build all remaining rows. for ($i = 0; $i < count($choiceElts[0]); $i++) { $tr = $fragment->ownerDocument->createElement('tr'); $ifStatements = Utils::extractStatements($choiceElts[0][$i], Utils::EXTRACT_IF); $incStatements = Utils::extractStatements($choiceElts[0][$i], Utils::EXTRACT_INCLUDE); $th = XmlUtils::changeElementName($choiceElts[0][$i], 'th'); $tr->appendChild($th); $table->appendChild($tr); if (empty($incStatements) === false) { $tr->parentNode->insertBefore($incStatements[0], $tr); } if (empty($ifStatements) === false) { $tr->parentNode->insertBefore($ifStatements[0], $tr); } for ($j = 0; $j < count($choiceElts[1]); $j++) { $input = $fragment->ownerDocument->createElement('input'); $input->setAttribute('type', 'checkbox'); $td = $fragment->ownerDocument->createElement('td'); $td->appendChild($input); $tr->appendChild($td); if (isset($ifVerticalStatementsStorage[$j])) { $td->parentNode->insertBefore($ifVerticalStatementsStorage[$j][0]->cloneNode(), $td); $td->parentNode->insertBefore($ifVerticalStatementsStorage[$j][1]->cloneNode(), $td->nextSibling); } } if (empty($incStatements) === false && isset($td)) { $tr->parentNode->insertBefore($incStatements[1], $tr->nextSibling); } if (empty($ifStatements) === false && isset($td)) { $tr->parentNode->insertBefore($ifStatements[1], $tr->nextSibling); } } }