| Code Coverage | ||||||||||
| Lines | Functions and Methods | Classes and Traits | ||||||||
| Total |  | 97.92% | 47 / 48 |  | 83.33% | 5 / 6 | CRAP |  | 0.00% | 0 / 1 | 
| XIncludeAdditionalAssetInjector |  | 97.92% | 47 / 48 |  | 83.33% | 5 / 6 | 15 |  | 0.00% | 0 / 1 | 
| injectNonRDFXincludeRelatedAssets |  | 100.00% | 15 / 15 |  | 100.00% | 1 / 1 | 4 | |||
| includeSharedStimulusStylesheets |  | 100.00% | 21 / 21 |  | 100.00% | 1 / 1 | 3 | |||
| findQtiXIncludeByResourceIdentifierOrHref |  | 100.00% | 4 / 4 |  | 100.00% | 1 / 1 | 4 | |||
| getPassageWrapperStyleClass |  | 83.33% | 5 / 6 |  | 0.00% | 0 / 1 | 2.02 | |||
| packedAssetTypeIsNotXinclude |  | 100.00% | 1 / 1 |  | 100.00% | 1 / 1 | 1 | |||
| getAssetStylesheetLoader |  | 100.00% | 1 / 1 |  | 100.00% | 1 / 1 | 1 | |||
| 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) 2022 (original work) Open Assessment Technologies SA; | 
| 19 | */ | 
| 20 | |
| 21 | declare(strict_types=1); | 
| 22 | |
| 23 | namespace oat\taoQtiItem\model\compile\QtiAssetCompiler; | 
| 24 | |
| 25 | use common_Exception; | 
| 26 | use oat\generis\Helper\UuidPrimaryKeyTrait; | 
| 27 | use oat\oatbox\service\ConfigurableService; | 
| 28 | use oat\oatbox\filesystem\Directory; | 
| 29 | use oat\taoQtiItem\model\Export\Stylesheet\AssetStylesheetLoader; | 
| 30 | use oat\taoQtiItem\model\pack\QtiAssetPacker\PackedAsset; | 
| 31 | use oat\taoQtiItem\model\qti\exception\QtiModelException; | 
| 32 | use oat\taoQtiItem\model\qti\Item; | 
| 33 | use oat\taoQtiItem\model\qti\Stylesheet; | 
| 34 | use oat\taoQtiItem\model\qti\XInclude; | 
| 35 | |
| 36 | class XIncludeAdditionalAssetInjector extends ConfigurableService | 
| 37 | { | 
| 38 | use UuidPrimaryKeyTrait; | 
| 39 | |
| 40 | public const XINCLUDE_ASSET_TYPE = 'xinclude'; | 
| 41 | |
| 42 | public const COMPILED_PASSAGE_STYLESHEET_FILENAME_PREFIX = 'passage'; | 
| 43 | |
| 44 | public const WRAPPER_CSS_CLASS_PREFIX = 'tao-'; | 
| 45 | |
| 46 | /** | 
| 47 | * @throws common_Exception | 
| 48 | */ | 
| 49 | public function injectNonRDFXincludeRelatedAssets( | 
| 50 | Item $qtiItem, | 
| 51 | Directory $publicDirectory, | 
| 52 | PackedAsset $packedAsset | 
| 53 | ): void { | 
| 54 | if ($this->packedAssetTypeIsNotXinclude($packedAsset)) { | 
| 55 | return; | 
| 56 | } | 
| 57 | |
| 58 | $passageStylesheetLoader = $this->getAssetStylesheetLoader(); | 
| 59 | $passageResourceIdentifier = $packedAsset->getMediaAsset()->getMediaIdentifier(); | 
| 60 | $xInclude = $this->findQtiXIncludeByResourceIdentifierOrHref($qtiItem, $passageResourceIdentifier); | 
| 61 | |
| 62 | if ($stylesheetFiles = $passageStylesheetLoader->loadAssetsFromAssetResource($passageResourceIdentifier)) { | 
| 63 | try { | 
| 64 | $this->includeSharedStimulusStylesheets($qtiItem, $publicDirectory, $stylesheetFiles, $xInclude); | 
| 65 | } catch (QtiModelException $e) { | 
| 66 | $this->logWarning( | 
| 67 | sprintf( | 
| 68 | 'Compilation: Injecting stylesheet for Passage %s failed with message %s', | 
| 69 | $packedAsset->getReplacedBy(), | 
| 70 | $e->getMessage() | 
| 71 | ) | 
| 72 | ); | 
| 73 | } | 
| 74 | } | 
| 75 | } | 
| 76 | |
| 77 | /** | 
| 78 | * @throws QtiModelException | 
| 79 | * @throws common_Exception | 
| 80 | */ | 
| 81 | private function includeSharedStimulusStylesheets( | 
| 82 | Item $qtiItem, | 
| 83 | Directory $publicDirectory, | 
| 84 | array $stylesheetFiles, | 
| 85 | ?XInclude $XInclude | 
| 86 | ): void { | 
| 87 | $prefix = self::COMPILED_PASSAGE_STYLESHEET_FILENAME_PREFIX; | 
| 88 | if ($XInclude) { | 
| 89 | $prefix = $this->getPassageWrapperStyleClass($XInclude); | 
| 90 | } | 
| 91 | $stylesheetTargetPubDirectory = implode(DIRECTORY_SEPARATOR, [ | 
| 92 | $prefix, | 
| 93 | AssetStylesheetLoader::ASSET_CSS_DIRECTORY_NAME | 
| 94 | ]); | 
| 95 | |
| 96 | foreach ($stylesheetFiles as $stylesheetFile) { | 
| 97 | $targetPath = implode(DIRECTORY_SEPARATOR, [ | 
| 98 | $stylesheetTargetPubDirectory, | 
| 99 | basename($stylesheetFile['path']) | 
| 100 | ]); | 
| 101 | |
| 102 | $publicDirectory->getFile($targetPath)->write($stylesheetFile['stream']); | 
| 103 | |
| 104 | $qtiStylesheet = new Stylesheet( | 
| 105 | [ | 
| 106 | 'href' => $targetPath, | 
| 107 | 'title' => basename($stylesheetFile['path']), | 
| 108 | 'type' => 'text/css' | 
| 109 | ] | 
| 110 | ); | 
| 111 | |
| 112 | $qtiItem->addStylesheet($qtiStylesheet); | 
| 113 | } | 
| 114 | } | 
| 115 | |
| 116 | private function findQtiXIncludeByResourceIdentifierOrHref(Item $qtiItem, string $resourceId): ?XInclude | 
| 117 | { | 
| 118 | foreach ($qtiItem->getComposingElements() as $element) { | 
| 119 | if ($element instanceof XInclude && strpos($element->getAttributeValue('href'), $resourceId) !== false) { | 
| 120 | return $element; | 
| 121 | } | 
| 122 | } | 
| 123 | |
| 124 | return null; | 
| 125 | } | 
| 126 | |
| 127 | private function getPassageWrapperStyleClass(XInclude $xInclude): ?string | 
| 128 | { | 
| 129 | $existingClassAttr = $xInclude->getAttributeValue('class'); | 
| 130 | if ($existingClassAttr) { | 
| 131 | return $existingClassAttr; | 
| 132 | } | 
| 133 | |
| 134 | |
| 135 | /* generate unique wrapper class as we do on FE */ | 
| 136 | $generatedClass = self::WRAPPER_CSS_CLASS_PREFIX . bin2hex(random_bytes(6)); | 
| 137 | $xInclude->setAttribute('class', $generatedClass); | 
| 138 | |
| 139 | return $generatedClass; | 
| 140 | } | 
| 141 | |
| 142 | private function packedAssetTypeIsNotXinclude(PackedAsset $packedAsset): bool | 
| 143 | { | 
| 144 | return $packedAsset->getType() !== self::XINCLUDE_ASSET_TYPE; | 
| 145 | } | 
| 146 | |
| 147 | private function getAssetStylesheetLoader(): AssetStylesheetLoader | 
| 148 | { | 
| 149 | return $this->getServiceLocator()->get(AssetStylesheetLoader::class); | 
| 150 | } | 
| 151 | } |