Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 42 |
|
0.00% |
0 / 15 |
CRAP | |
0.00% |
0 / 1 |
| SharedStimulusAssetHandler | |
0.00% |
0 / 42 |
|
0.00% |
0 / 15 |
342 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| isApplicable | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| handle | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
2 | |||
| getQtiModel | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| setQtiModel | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getSharedFiles | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setSharedFiles | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| addSharedFile | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getItemSource | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setItemSource | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getTargetClassPath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setTargetClassPath | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| finalize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSharedStimulusMediaEncoderService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSharedStimulusFactory | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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) 2016 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoQtiItem\model\qti\asset\handler; |
| 23 | |
| 24 | use core_kernel_classes_Class; |
| 25 | use Laminas\ServiceManager\ServiceLocatorAwareTrait; |
| 26 | use oat\generis\model\OntologyAwareTrait; |
| 27 | use oat\tao\helpers\FileUploadException; |
| 28 | use oat\tao\model\media\MediaManagement; |
| 29 | use oat\tao\model\media\MediaService; |
| 30 | use oat\taoItems\model\media\ItemMediaResolver; |
| 31 | use oat\taoMediaManager\model\sharedStimulus\encoder\SharedStimulusMediaEncoder; |
| 32 | use oat\taoMediaManager\model\MediaSource; |
| 33 | use oat\taoMediaManager\model\SharedStimulusImporter; |
| 34 | use oat\taoQtiItem\model\qti\asset\factory\SharedStimulusFactory; |
| 35 | use oat\taoQtiItem\model\qti\Element; |
| 36 | use oat\taoQtiItem\model\qti\Item; |
| 37 | use tao_helpers_Uri; |
| 38 | |
| 39 | class SharedStimulusAssetHandler implements AssetHandler |
| 40 | { |
| 41 | use OntologyAwareTrait; |
| 42 | use ServiceLocatorAwareTrait; |
| 43 | |
| 44 | /** @var ItemMediaResolver */ |
| 45 | protected $itemSource; |
| 46 | |
| 47 | /** @var MediaManagement */ |
| 48 | protected $sharedStorage; |
| 49 | |
| 50 | protected $qtiModel; |
| 51 | protected $sharedFiles = []; |
| 52 | private array $targetClassPath; |
| 53 | |
| 54 | /** |
| 55 | * MediaAssetHandler constructor. |
| 56 | * Retrieve shared storage |
| 57 | * |
| 58 | */ |
| 59 | public function __construct() |
| 60 | { |
| 61 | $sources = MediaService::singleton()->getWritableSources(); |
| 62 | $this->sharedStorage = array_shift($sources); |
| 63 | return $this; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Check if current file.xml is shared stimulus |
| 68 | * |
| 69 | * @param $relativePath |
| 70 | * @return bool |
| 71 | * @throws FileUploadException |
| 72 | */ |
| 73 | public function isApplicable($relativePath) |
| 74 | { |
| 75 | $xincluded = []; |
| 76 | /** @var Element $xincludeElement */ |
| 77 | foreach ($this->getQtiModel()->getComposingElements('oat\taoQtiItem\model\qti\Xinclude') as $xincludeElement) { |
| 78 | $xincluded[] = $xincludeElement->attr('href'); |
| 79 | \common_Logger::i("Xinclude component found in resource '" . |
| 80 | $this->getQtiModel()->getIdentifier() . "' with href '" . $xincludeElement->attr('href') . "'."); |
| 81 | } |
| 82 | |
| 83 | return (!empty($this->sharedStorage) && in_array($relativePath, $xincluded)); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Handle the process to manage shared stimulus files |
| 88 | * |
| 89 | * @param $absolutePath |
| 90 | * @param $relativePath |
| 91 | * @return array |
| 92 | * @throws \qtism\data\storage\xml\XmlStorageException |
| 93 | */ |
| 94 | public function handle($absolutePath, $relativePath) |
| 95 | { |
| 96 | SharedStimulusImporter::isValidSharedStimulus($absolutePath); |
| 97 | $newXmlFile = $this->getSharedStimulusMediaEncoderService()->encodeAssets($absolutePath); |
| 98 | $mediaResourceUri = $this->getSharedStimulusFactory()->createShardedStimulusFromSourceFiles( |
| 99 | $newXmlFile, |
| 100 | $relativePath, |
| 101 | $absolutePath, |
| 102 | $this->getTargetClassPath() |
| 103 | ); |
| 104 | |
| 105 | \common_Logger::i('Auxiliary file \'' . $absolutePath . '\' added to shared storage.'); |
| 106 | |
| 107 | return [ |
| 108 | 'uri' => MediaSource::SCHEME_NAME . tao_helpers_Uri::encode($mediaResourceUri) |
| 109 | ]; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Return item of import e.q. "qti.xml" |
| 114 | * |
| 115 | * @return Item |
| 116 | * @throws FileUploadException |
| 117 | */ |
| 118 | protected function getQtiModel() |
| 119 | { |
| 120 | if ($this->qtiModel) { |
| 121 | return $this->qtiModel; |
| 122 | } |
| 123 | throw new FileUploadException('Unable to found a valid Qti File.'); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Setter of qtiModel |
| 128 | * |
| 129 | * @param $qtiModel |
| 130 | * @return $this |
| 131 | */ |
| 132 | public function setQtiModel($qtiModel) |
| 133 | { |
| 134 | $this->qtiModel = $qtiModel; |
| 135 | return $this; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Return shared files |
| 140 | * |
| 141 | * @return array |
| 142 | */ |
| 143 | public function getSharedFiles() |
| 144 | { |
| 145 | return $this->sharedFiles; |
| 146 | } |
| 147 | |
| 148 | public function setSharedFiles($sharedFiles) |
| 149 | { |
| 150 | $this->sharedFiles = $sharedFiles; |
| 151 | return $this; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Add a shared file |
| 156 | * |
| 157 | * @param $key |
| 158 | * @param $value |
| 159 | * @return $this |
| 160 | */ |
| 161 | public function addSharedFile($key, $value) |
| 162 | { |
| 163 | $sharedFiles = $this->getSharedFiles(); |
| 164 | $sharedFiles[$key] = $value; |
| 165 | $this->setSharedFiles($sharedFiles); |
| 166 | return $this; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * @return ItemMediaResolver |
| 171 | */ |
| 172 | public function getItemSource() |
| 173 | { |
| 174 | return $this->itemSource; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * @param ItemMediaResolver $itemSource |
| 179 | * @return $this |
| 180 | */ |
| 181 | public function setItemSource($itemSource) |
| 182 | { |
| 183 | $this->itemSource = $itemSource; |
| 184 | return $this; |
| 185 | } |
| 186 | |
| 187 | public function getTargetClassPath(): array |
| 188 | { |
| 189 | return $this->targetClassPath; |
| 190 | } |
| 191 | |
| 192 | public function setTargetClassPath(array $targetClassPath): self |
| 193 | { |
| 194 | $this->targetClassPath = $targetClassPath; |
| 195 | return $this; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * @inherit |
| 200 | */ |
| 201 | public function finalize() |
| 202 | { |
| 203 | // Nothing to do |
| 204 | } |
| 205 | |
| 206 | private function getSharedStimulusMediaEncoderService(): SharedStimulusMediaEncoder |
| 207 | { |
| 208 | return $this->getServiceLocator()->get(SharedStimulusMediaEncoder::SERVICE_ID); |
| 209 | } |
| 210 | |
| 211 | private function getSharedStimulusFactory(): SharedStimulusFactory |
| 212 | { |
| 213 | return $this->getServiceLocator()->get(SharedStimulusFactory::class); |
| 214 | } |
| 215 | } |