Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.89% |
24 / 27 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| XIncludeXmlInjector | |
88.89% |
24 / 27 |
|
33.33% |
1 / 3 |
8.09 | |
0.00% |
0 / 1 |
| injectSharedStimulus | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
| createSharedStimulusContent | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
2.01 | |||
| createSharedStimulusNode | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
3.07 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2020 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | declare(strict_types=1); |
| 23 | |
| 24 | namespace oat\taoQtiItem\model\compile\QtiAssetCompiler; |
| 25 | |
| 26 | use DOMDocument; |
| 27 | use DOMElement; |
| 28 | use DOMXPath; |
| 29 | use InvalidArgumentException; |
| 30 | use oat\oatbox\config\ConfigurationService; |
| 31 | use oat\tao\model\media\MediaAsset; |
| 32 | use oat\taoQtiItem\model\pack\QtiAssetPacker\PackedAsset; |
| 33 | use tao_models_classes_FileNotFoundException; |
| 34 | |
| 35 | class XIncludeXmlInjector extends ConfigurationService |
| 36 | { |
| 37 | /** |
| 38 | * @param DOMDocument $domDocument |
| 39 | * @param PackedAsset[] $packedAssets |
| 40 | */ |
| 41 | public function injectSharedStimulus(DOMDocument $domDocument, array $packedAssets): void |
| 42 | { |
| 43 | $domElementsCollection = $domDocument->getElementsByTagName('include'); |
| 44 | $elements = []; |
| 45 | |
| 46 | /** @var DOMElement $xincludeNode */ |
| 47 | foreach ($domElementsCollection as $xincludeNode) { |
| 48 | $elements[] = $xincludeNode; |
| 49 | } |
| 50 | |
| 51 | foreach ($elements as $xincludeNode) { |
| 52 | $sharedStimulusElement = $this->createSharedStimulusContent($xincludeNode, $packedAssets); |
| 53 | $xincludeNode->parentNode->replaceChild( |
| 54 | $domDocument->importNode($sharedStimulusElement, true), |
| 55 | $xincludeNode |
| 56 | ); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param DOMElement $xincludeNode |
| 62 | * @param PackedAsset[] $packedAssets |
| 63 | * @return DOMElement |
| 64 | * @throws tao_models_classes_FileNotFoundException |
| 65 | */ |
| 66 | private function createSharedStimulusContent(DOMElement $xincludeNode, array $packedAssets): DOMElement |
| 67 | { |
| 68 | $href = $xincludeNode->getAttribute('href'); |
| 69 | |
| 70 | if (!isset($packedAssets[$href])) { |
| 71 | throw new InvalidArgumentException(sprintf('SharedStimulus %s cannot be found', $href)); |
| 72 | } |
| 73 | |
| 74 | /** @var MediaAsset $asset */ |
| 75 | $asset = $packedAssets[$href]->getMediaAsset(); |
| 76 | |
| 77 | $this->logInfo(sprintf('Injecting shared stimulus %s to QTI map', $asset->getMediaIdentifier())); |
| 78 | |
| 79 | $sharedStimulusContent = $asset->getMediaSource()->getFileStream($asset->getMediaIdentifier())->getContents(); |
| 80 | |
| 81 | return $this->createSharedStimulusNode($sharedStimulusContent); |
| 82 | } |
| 83 | |
| 84 | private function createSharedStimulusNode(string $sharedStimulusContent): DOMElement |
| 85 | { |
| 86 | $sharedStimulusDocument = new DOMDocument('1.0', 'UTF-8'); |
| 87 | |
| 88 | if ($sharedStimulusDocument->loadXML($sharedStimulusContent) === false) { |
| 89 | throw new InvalidArgumentException('SharedStimulus content is not parsable.'); |
| 90 | } |
| 91 | |
| 92 | $sharedStimulusXpath = new DOMXPath($sharedStimulusDocument); |
| 93 | |
| 94 | $mainElement = $sharedStimulusXpath->query('//row[@class="grid-row"]'); |
| 95 | if ($mainElement->count() == 0) { |
| 96 | $sharedStimulusElement = $sharedStimulusDocument->firstChild; |
| 97 | $sharedStimulusElement->removeAttribute('xml:lang'); |
| 98 | } else { |
| 99 | $sharedStimulusElement = $mainElement->item(0); |
| 100 | } |
| 101 | |
| 102 | return $sharedStimulusElement; |
| 103 | } |
| 104 | } |