Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
4.04% |
4 / 99 |
|
20.00% |
3 / 15 |
CRAP | |
0.00% |
0 / 1 |
QTIPackedItemExporter | |
4.04% |
4 / 99 |
|
20.00% |
3 / 15 |
1313.95 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getManifest | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setManifest | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasManifest | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
export | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
42 | |||
containsItem | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
buildBasePath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
buildIdentifier | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
exportManifest | |
0.00% |
0 / 51 |
|
0.00% |
0 / 1 |
210 | |||
renderManifest | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
itemContentPostProcessing | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getQTIVersion | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
injectMetadataToManifest | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
genericLomOntologyExtractor | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFeatureFlagChecker | |
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) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
19 | * (under the project TAO-TRANSFER); |
20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
22 | * 2013-2016 (update and modification) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
23 | */ |
24 | |
25 | namespace oat\taoQtiItem\model\Export; |
26 | |
27 | use oat\tao\model\featureFlag\FeatureFlagChecker; |
28 | use oat\taoQtiItem\model\qti\exception\ExportException; |
29 | use oat\taoQtiItem\model\qti\Service; |
30 | use core_kernel_classes_Resource; |
31 | use oat\taoQtiTest\models\classes\metadata\GenericLomOntologyExtractor; |
32 | use ZipArchive; |
33 | use DOMDocument; |
34 | use oat\oatbox\reporting\Report; |
35 | use tao_helpers_Uri; |
36 | use taoItems_models_classes_TemplateRenderer; |
37 | use tao_helpers_Display; |
38 | |
39 | class QTIPackedItemExporter extends AbstractQTIItemExporter |
40 | { |
41 | private $manifest; |
42 | |
43 | /** |
44 | * Creates a new instance of QtiPackedItemExporter for a particular item. |
45 | * |
46 | * @param core_kernel_classes_Resource $item The item to be exported. |
47 | * @param ZipArchive $zip The ZIP archive were the files have to be exported. |
48 | * @param DOMDocument $manifest A Manifest to be reused to reference item components (e.g. auxilliary files). |
49 | */ |
50 | public function __construct(core_kernel_classes_Resource $item, ZipArchive $zip, DOMDocument $manifest = null) |
51 | { |
52 | parent::__construct($item, $zip); |
53 | $this->setManifest($manifest); |
54 | } |
55 | |
56 | public function getManifest() |
57 | { |
58 | return $this->manifest; |
59 | } |
60 | |
61 | public function setManifest(DOMDocument $manifest = null) |
62 | { |
63 | $this->manifest = $manifest; |
64 | } |
65 | |
66 | public function hasManifest() |
67 | { |
68 | return $this->getManifest() !== null; |
69 | } |
70 | |
71 | public function export($options = []) |
72 | { |
73 | if (!$this->containsItem()) { |
74 | $report = parent::export($options); |
75 | if ($report->getType() === Report::TYPE_ERROR || $report->containsError()) { |
76 | return $report; |
77 | } |
78 | try { |
79 | $exportResult = []; |
80 | if (is_array($report->getData())) { |
81 | $exportResult = $report->getData(); |
82 | } |
83 | $this->exportManifest($options, $exportResult); |
84 | } catch (ExportException $e) { |
85 | $report->setType(Report::TYPE_ERROR); |
86 | $report->setMessage($e->getUserMessage()); |
87 | } |
88 | return $report; |
89 | } |
90 | return Report::createSuccess(); |
91 | } |
92 | |
93 | /** |
94 | * Whenever the item is already in the manifest |
95 | * @return boolean |
96 | */ |
97 | protected function containsItem() |
98 | { |
99 | $found = false; |
100 | if ($this->hasManifest()) { |
101 | foreach ($this->getManifest()->getElementsByTagName('resource') as $resourceNode) { |
102 | /** @var \DOMElement $resourceNode */ |
103 | if ($resourceNode->getAttribute('identifier') == $this->buildIdentifier()) { |
104 | $found = true; |
105 | break; |
106 | } |
107 | } |
108 | } |
109 | return $found; |
110 | } |
111 | |
112 | public function buildBasePath() |
113 | { |
114 | return tao_helpers_Uri::getUniqueId($this->getItem()->getUri()); |
115 | } |
116 | |
117 | public function buildIdentifier() |
118 | { |
119 | return tao_helpers_Uri::getUniqueId($this->getItem()->getUri()); |
120 | } |
121 | |
122 | /** |
123 | * Build, merge and export the IMS Manifest into the target ZIP archive. |
124 | * |
125 | * @throws |
126 | */ |
127 | public function exportManifest($options = [], $exportResult = []) |
128 | { |
129 | |
130 | $base = $this->buildBasePath(); |
131 | $zipArchive = $this->getZip(); |
132 | $qtiFile = ''; |
133 | $qtiResources = []; |
134 | $sharedAssets = isset($exportResult['portableAssets']) ? $exportResult['portableAssets'] : []; |
135 | |
136 | for ($i = 0; $i < $zipArchive->numFiles; $i++) { |
137 | $fileName = $zipArchive->getNameIndex($i); |
138 | |
139 | //shared assets are authorized to be added at the root of the package |
140 | if (preg_match("@^" . preg_quote($base) . "@", $fileName) || in_array($fileName, $sharedAssets)) { |
141 | if (basename($fileName) == 'qti.xml') { |
142 | $qtiFile = $fileName; |
143 | } else { |
144 | if (!empty($fileName)) { |
145 | $qtiResources[] = htmlspecialchars($fileName, ENT_QUOTES | ENT_XML1); |
146 | } |
147 | } |
148 | } |
149 | } |
150 | |
151 | $qtiItemService = Service::singleton(); |
152 | |
153 | //@todo add support of multi language packages |
154 | $rdfItem = $this->getItem(); |
155 | $qtiItem = $qtiItemService->getDataItemByRdfItem($rdfItem); |
156 | $qtiItem->validateOutcomes(); |
157 | if (!is_null($qtiItem)) { |
158 | // -- Prepare data transfer to the imsmanifest.tpl template. |
159 | $qtiItemData = []; |
160 | |
161 | // alter identifier for export to avoid any "clash". |
162 | $qtiItemData['identifier'] = $this->buildIdentifier(); |
163 | $qtiItemData['filePath'] = $qtiFile; |
164 | $qtiItemData['medias'] = $qtiResources; |
165 | $qtiItemData['adaptive'] = ($qtiItem->getAttributeValue('adaptive') === 'adaptive') ? true : false; |
166 | $qtiItemData['timeDependent'] = $qtiItem->getAttributeValue('timeDependent') === 'timeDependent'; |
167 | $qtiItemData['toolName'] = $qtiItem->getAttributeValue('toolVendor'); |
168 | $qtiItemData['toolVersion'] = $qtiItem->getAttributeValue('toolVersion'); |
169 | $qtiItemData['interactions'] = []; |
170 | |
171 | foreach ($qtiItem->getInteractions() as $interaction) { |
172 | $interactionData = []; |
173 | $interactionData['type'] = $interaction->getQtiTag(); |
174 | $qtiItemData['interactions'][] = $interactionData; |
175 | } |
176 | |
177 | // -- Build a brand new IMS Manifest. |
178 | $newManifest = $this->renderManifest($options, $qtiItemData); |
179 | |
180 | if ($this->hasManifest()) { |
181 | // Merge old manifest and new one. |
182 | $dom1 = $this->getManifest(); |
183 | $dom2 = $newManifest; |
184 | $resourceNodes = $dom2->getElementsByTagName('resource'); |
185 | $resourcesNodes = $dom1->getElementsByTagName('resources'); |
186 | |
187 | foreach ($resourcesNodes as $resourcesNode) { |
188 | foreach ($resourceNodes as $resourceNode) { |
189 | $newResourceNode = $dom1->importNode($resourceNode, true); |
190 | $resourcesNode->appendChild($newResourceNode); |
191 | } |
192 | } |
193 | |
194 | |
195 | // rendered manifest is now useless. |
196 | unset($dom2); |
197 | } else { |
198 | // Brand new manifest. |
199 | $this->setManifest($newManifest); |
200 | } |
201 | |
202 | $manifest = $this->getManifest(); |
203 | $this->getMetadataExporter()->export($this->getItem(), $manifest); |
204 | $this->injectMetadataToManifest($manifest, $this->getItem()); |
205 | $this->setManifest($manifest); |
206 | |
207 | |
208 | // -- Overwrite manifest in the current ZIP archive. |
209 | $zipArchive->addFromString('imsmanifest.xml', $this->getManifest()->saveXML()); |
210 | } else { |
211 | //the item has no item content, there are 2 possibilities: |
212 | $itemLabel = $this->getItem()->getLabel(); |
213 | if (empty($itemLabel)) { |
214 | //it has no label at all: the item does not exist anymore |
215 | throw new ExportException($this->getItem()->getUri(), 'item not found'); |
216 | } else { |
217 | //there is one, so the item does exist but might not have any content |
218 | throw new ExportException($itemLabel, 'no item content'); |
219 | } |
220 | } |
221 | } |
222 | |
223 | protected function renderManifest(array $options, array $qtiItemData) |
224 | { |
225 | $asApip = isset($options['apip']) && $options['apip'] === true; |
226 | $dir = \common_ext_ExtensionsManager::singleton()->getExtensionById('taoQtiItem')->getDir(); |
227 | $tpl = ($asApip === false) |
228 | ? $dir . 'model/qti/templates/imsmanifest.tpl.php' |
229 | : $dir . 'model/qti/templates/imsmanifestApip.tpl.php'; |
230 | |
231 | $templateRenderer = new taoItems_models_classes_TemplateRenderer($tpl, [ |
232 | 'qtiItems' => [$qtiItemData], |
233 | 'manifestIdentifier' => 'MANIFEST-' . tao_helpers_Display::textCleaner(uniqid('tao', true), '-') |
234 | ]); |
235 | |
236 | $renderedManifest = $templateRenderer->render(); |
237 | $newManifest = new DOMDocument('1.0', TAO_DEFAULT_ENCODING); |
238 | $newManifest->loadXML($renderedManifest); |
239 | |
240 | return $newManifest; |
241 | } |
242 | |
243 | |
244 | protected function itemContentPostProcessing($content) |
245 | { |
246 | return $content; |
247 | } |
248 | |
249 | protected function getQTIVersion(): string |
250 | { |
251 | return '2p1'; |
252 | } |
253 | |
254 | private function injectMetadataToManifest($manifest, core_kernel_classes_Resource $item) |
255 | { |
256 | $this->genericLomOntologyExtractor()->extract( |
257 | [$item], |
258 | $manifest |
259 | ); |
260 | } |
261 | private function genericLomOntologyExtractor(): GenericLomOntologyExtractor |
262 | { |
263 | return $this->getServiceManager()->getContainer()->get(GenericLomOntologyExtractor::class); |
264 | } |
265 | private function getFeatureFlagChecker(): FeatureFlagChecker |
266 | { |
267 | return $this->getServiceManager()->getContainer()->get(FeatureFlagChecker::class); |
268 | } |
269 | } |