Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 82 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
Authoring | |
0.00% |
0 / 82 |
|
0.00% |
0 / 5 |
756 | |
0.00% |
0 / 1 |
validateQtiXml | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
checkEmptyMedia | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
56 | |||
addRequiredResources | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
42 | |||
sanitizeQtiXml | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
loadQtiXml | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
90 |
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) 2013-2025 (original work) Open Assessment Technologies SA; |
19 | */ |
20 | |
21 | namespace oat\taoQtiItem\helpers; |
22 | |
23 | //use oat\taoQtiItem\helpers\Authoring; |
24 | use common_ext_ExtensionsManager; |
25 | use common_Logger; |
26 | use DOMDocument; |
27 | use DOMElement; |
28 | use DOMXpath; |
29 | use oat\oatbox\filesystem\File; |
30 | use oat\taoQtiItem\model\qti\exception\QtiModelException; |
31 | use oat\taoQtiItem\model\qti\Parser; |
32 | use core_kernel_classes_Resource; |
33 | use taoItems_models_classes_ItemsService; |
34 | use tao_helpers_File; |
35 | use common_exception_Error; |
36 | |
37 | /** |
38 | * Helper to provide methods for QTI authoring |
39 | * |
40 | * @access public |
41 | * @author Sam, <sam@taotesting.com> |
42 | * @package taoQtiItem |
43 | */ |
44 | class Authoring |
45 | { |
46 | /** |
47 | * Validate a QTI XML string. |
48 | * |
49 | * @param string $qti File path or XML string |
50 | * @throws QtiModelException |
51 | */ |
52 | public static function validateQtiXml($qti) |
53 | { |
54 | |
55 | $dom = self::loadQtiXml($qti); |
56 | $returnValue = $dom->saveXML(); |
57 | |
58 | $parserValidator = new Parser($returnValue); |
59 | $parserValidator->validate(); |
60 | if (!$parserValidator->isValid()) { |
61 | common_Logger::w('Invalid QTI output: ' . PHP_EOL . ' ' . $parserValidator->displayErrors()); |
62 | throw new QtiModelException('invalid QTI item XML ' . PHP_EOL . ' ' . $parserValidator->displayErrors()); |
63 | } |
64 | } |
65 | |
66 | /** |
67 | * Simple function to check if the item is not missing any of the required asset configuration path |
68 | * @param string $qti |
69 | * @throws QtiModelException |
70 | * @throws common_exception_Error |
71 | */ |
72 | public static function checkEmptyMedia($qti) |
73 | { |
74 | $doc = new DOMDocument(); |
75 | $doc->loadHTML(self::loadQtiXml($qti)->saveXML()); |
76 | |
77 | $imgs = $doc->getElementsByTagName('img'); |
78 | foreach ($imgs as $img) { |
79 | if (empty($img->getAttribute('src'))) { |
80 | throw new QtiModelException('image has no source'); |
81 | } |
82 | } |
83 | |
84 | $objects = $doc->getElementsByTagName('object'); |
85 | foreach ($objects as $object) { |
86 | if (empty($object->getAttribute('data'))) { |
87 | throw new QtiModelException('object has no data source'); |
88 | } |
89 | } |
90 | |
91 | $objects = $doc->getElementsByTagName('include'); |
92 | foreach ($objects as $object) { |
93 | if (empty($object->getAttribute('href'))) { |
94 | throw new QtiModelException('object has no data source'); |
95 | } |
96 | } |
97 | } |
98 | |
99 | /** |
100 | * Add a list of required resources files to an RDF item and keeps the relative path structure |
101 | * For instances, required css, js etc. |
102 | * |
103 | * @param string $sourceDirectory |
104 | * @param array $relativeSourceFiles |
105 | * @param core_kernel_classes_Resource $item |
106 | * @param string $lang |
107 | * @return array |
108 | * @throws common_exception_Error |
109 | */ |
110 | public static function addRequiredResources( |
111 | $sourceDirectory, |
112 | $relativeSourceFiles, |
113 | $prefix, |
114 | core_kernel_classes_Resource $item, |
115 | $lang |
116 | ) { |
117 | |
118 | $returnValue = []; |
119 | |
120 | $directory = taoItems_models_classes_ItemsService::singleton()->getItemDirectory($item, $lang); |
121 | |
122 | foreach ($relativeSourceFiles as $relPath) { |
123 | if (! tao_helpers_File::securityCheck($relPath, true)) { |
124 | throw new common_exception_Error('Invalid resource file path'); |
125 | } |
126 | |
127 | $relPath = preg_replace('/^\.\//', '', $relPath); |
128 | $source = $sourceDirectory . $relPath; |
129 | |
130 | $fh = fopen($source, 'r'); |
131 | if (! is_resource($fh)) { |
132 | throw new common_exception_Error('The resource "' . $source . '" cannot be copied.'); |
133 | } |
134 | |
135 | $path = tao_helpers_File::concat([ |
136 | $prefix ? $prefix : '', |
137 | $relPath |
138 | ]); |
139 | |
140 | // cannot write as PCI do not get cleaned up |
141 | if ($directory->getFile($path)->put($fh)) { |
142 | $returnValue[] = $relPath; |
143 | } |
144 | fclose($fh); |
145 | } |
146 | |
147 | return $returnValue; |
148 | } |
149 | |
150 | /** |
151 | * Remove invalid elements and attributes from QTI XML. |
152 | * @param string $qti File path or XML string |
153 | * @return string sanitized XML |
154 | */ |
155 | public static function sanitizeQtiXml($qti) |
156 | { |
157 | $doc = self::loadQtiXml($qti); |
158 | |
159 | $xpath = new DOMXpath($doc); |
160 | |
161 | $ids = []; |
162 | $elementsWithId = $xpath->query("//*[not(local-name()='lib') and not(local-name()='module') and @id]"); |
163 | |
164 | /** @var DOMElement $elementWithId */ |
165 | foreach ($elementsWithId as $elementWithId) { |
166 | $id = $elementWithId->getAttribute('id'); |
167 | if (in_array($id, $ids)) { |
168 | $elementWithId->removeAttribute('id'); |
169 | } else { |
170 | $ids[] = $id; |
171 | } |
172 | } |
173 | |
174 | return $doc->saveXML(); |
175 | } |
176 | |
177 | /** |
178 | * Load QTI xml and return DOMDocument instance. |
179 | * If string is not valid xml then QtiModelException will be thrown. |
180 | * |
181 | * @param string|File $file If it's a string it can be a file path or an XML string |
182 | * @throws QtiModelException |
183 | * @throws common_exception_Error |
184 | * @return DOMDocument |
185 | */ |
186 | public static function loadQtiXml($file) |
187 | { |
188 | if ($file instanceof File) { |
189 | $qti = $file->read(); |
190 | } elseif (preg_match("/^<\?xml(.*)?/m", trim($file))) { |
191 | $qti = $file; |
192 | } elseif (is_file($file)) { |
193 | $qti = file_get_contents($file); |
194 | } else { |
195 | throw new common_exception_Error( |
196 | "Wrong parameter. " . __CLASS__ . "::" . __METHOD__ |
197 | . " accepts either XML content or the path to a file but got " . substr($file, 0, 500) |
198 | ); |
199 | } |
200 | |
201 | $dom = new DOMDocument('1.0', 'UTF-8'); |
202 | |
203 | $domDocumentConfig = common_ext_ExtensionsManager::singleton() |
204 | ->getExtensionById('taoQtiItem') |
205 | ->getConfig('XMLParser'); |
206 | |
207 | if (is_array($domDocumentConfig) && !empty($domDocumentConfig)) { |
208 | foreach ($domDocumentConfig as $param => $value) { |
209 | if (property_exists($dom, $param)) { |
210 | $dom->$param = $value; |
211 | } |
212 | } |
213 | } else { |
214 | $dom->formatOutput = true; |
215 | $dom->preserveWhiteSpace = false; |
216 | $dom->validateOnParse = false; |
217 | } |
218 | |
219 | libxml_use_internal_errors(true); |
220 | |
221 | if (!$dom->loadXML($qti)) { |
222 | $errors = libxml_get_errors(); |
223 | |
224 | $errorsMsg = 'Wrong QTI item output format:' |
225 | . PHP_EOL |
226 | . array_reduce($errors, function ($carry, $item) { |
227 | $carry .= $item->message . PHP_EOL; |
228 | return $carry; |
229 | }); |
230 | |
231 | common_Logger::w($errorsMsg); |
232 | |
233 | throw new QtiModelException($errorsMsg); |
234 | } |
235 | |
236 | return $dom; |
237 | } |
238 | } |