getElementsByTagName('include'); $elements = []; /** @var DOMElement $xincludeNode */ foreach ($domElementsCollection as $xincludeNode) { $elements[] = $xincludeNode; } foreach ($elements as $xincludeNode) { $sharedStimulusElement = $this->createSharedStimulusContent($xincludeNode, $packedAssets); $xincludeNode->parentNode->replaceChild( $domDocument->importNode($sharedStimulusElement, true), $xincludeNode ); } } /** * @param DOMElement $xincludeNode * @param PackedAsset[] $packedAssets * @return DOMElement * @throws tao_models_classes_FileNotFoundException */ private function createSharedStimulusContent(DOMElement $xincludeNode, array $packedAssets): DOMElement { $href = $xincludeNode->getAttribute('href'); if (!isset($packedAssets[$href])) { throw new InvalidArgumentException(sprintf('SharedStimulus %s cannot be found', $href)); } /** @var MediaAsset $asset */ $asset = $packedAssets[$href]->getMediaAsset(); $this->logInfo(sprintf('Injecting shared stimulus %s to QTI map', $asset->getMediaIdentifier())); $sharedStimulusContent = $asset->getMediaSource()->getFileStream($asset->getMediaIdentifier())->getContents(); return $this->createSharedStimulusNode($sharedStimulusContent); } private function createSharedStimulusNode(string $sharedStimulusContent): DOMElement { $sharedStimulusDocument = new DOMDocument('1.0', 'UTF-8'); if ($sharedStimulusDocument->loadXML($sharedStimulusContent) === false) { throw new InvalidArgumentException('SharedStimulus content is not parsable.'); } $sharedStimulusXpath = new DOMXPath($sharedStimulusDocument); $mainElement = $sharedStimulusXpath->query('//row[@class="grid-row"]'); if ($mainElement->count() == 0) { $sharedStimulusElement = $sharedStimulusDocument->firstChild; $sharedStimulusElement->removeAttribute('xml:lang'); } else { $sharedStimulusElement = $mainElement->item(0); } return $sharedStimulusElement; } }