Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 137 |
|
0.00% |
0 / 16 |
CRAP | |
0.00% |
0 / 1 |
taoQtiTest_actions_RestQtiTests | |
0.00% |
0 / 137 |
|
0.00% |
0 / 16 |
2162 | |
0.00% |
0 / 1 |
index | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
exportQtiPackage | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
import | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
42 | |||
getItemClassUri | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
isOverwriteTest | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
getTaskName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
importDeferred | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
20 | |||
getItems | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
30 | |||
addExtraReturnData | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
createClass | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
getUploadedPackageData | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
getOverwriteTestUri | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
getPackageLabel | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
getTestClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getQtiPackageExporter | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getQtiTestService | |
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 | */ |
19 | |
20 | use oat\tao\model\TaoOntology; |
21 | use oat\tao\model\taskQueue\TaskLog\Entity\EntityInterface; |
22 | use oat\tao\model\taskQueue\TaskLogInterface; |
23 | use oat\taoQtiTest\helpers\QtiPackageExporter; |
24 | use oat\taoQtiTest\models\tasks\ImportQtiTest; |
25 | use oat\taoQtiItem\controller\AbstractRestQti; |
26 | use core_kernel_classes_Resource as Resource; |
27 | use oat\taoTests\models\MissingTestmodelException; |
28 | use Slim\Http\StatusCode; |
29 | |
30 | /** |
31 | * |
32 | * @author Absar Gilani & Rashid - PCG Team - <absar.gilani6@gmail.com> |
33 | * @author Gyula Szucs <gyula@taotesting.com> |
34 | */ |
35 | class taoQtiTest_actions_RestQtiTests extends AbstractRestQti |
36 | { |
37 | public const PARAM_PACKAGE_NAME = 'qtiPackage'; |
38 | |
39 | private const PARAM_TEST_URI = 'testUri'; |
40 | |
41 | private const ITEM_CLASS_URI = 'itemClassUri'; |
42 | |
43 | /** |
44 | * @deprecated Use taoQtiTest_actions_RestQtiTests::OVERWRITE_TEST_URI instead with the URI of the test to be |
45 | * replaced |
46 | */ |
47 | private const OVERWRITE_TEST = 'overwriteTest'; |
48 | |
49 | private const OVERWRITE_TEST_URI = 'overwriteTestUri'; |
50 | private const PACKAGE_LABEL = 'packageLabel'; |
51 | |
52 | /** |
53 | * @throws common_exception_NotImplemented |
54 | */ |
55 | public function index() |
56 | { |
57 | $this->returnFailure(new \common_exception_NotImplemented('This API does not support this call.')); |
58 | } |
59 | |
60 | /** |
61 | * Method will return qti package encoded in base64 for delivery |
62 | * @throws common_Exception |
63 | * @throws common_exception_Error |
64 | * @throws common_exception_NotImplemented |
65 | */ |
66 | public function exportQtiPackage() |
67 | { |
68 | if ($this->getRequestMethod() !== Request::HTTP_GET) { |
69 | throw new \common_exception_NotImplemented('Only post method is accepted to import Qti package.'); |
70 | } |
71 | |
72 | $params = $this->getPsrRequest()->getQueryParams(); |
73 | |
74 | if (!isset($params[self::PARAM_TEST_URI]) || (!$testId = $params[self::PARAM_TEST_URI])) { |
75 | return $this->returnFailure(new common_exception_MissingParameter()); |
76 | } |
77 | |
78 | $test = $this->getResource($testId); |
79 | |
80 | if (!$test->exists()) { |
81 | return $this->returnFailure(new common_exception_ResourceNotFound('Resource not found')); |
82 | } |
83 | |
84 | $exportReport = $this->getQtiPackageExporter()->exportDeliveryQtiPackage($test->getUri()); |
85 | |
86 | $data[self::PARAM_PACKAGE_NAME] = base64_encode(file_get_contents($exportReport->getData()['path'])); |
87 | |
88 | return $this->returnSuccess($data); |
89 | } |
90 | |
91 | /** |
92 | * Import file entry point by using $this->service |
93 | * Check POST method & get valid uploaded file |
94 | */ |
95 | public function import() |
96 | { |
97 | try { |
98 | if ($this->getRequestMethod() != Request::HTTP_POST) { |
99 | throw new \common_exception_NotImplemented('Only post method is accepted to import Qti package.'); |
100 | } |
101 | |
102 | $report = taoQtiTest_models_classes_CrudQtiTestsService::singleton() |
103 | ->importQtiTest( |
104 | $this->getUploadedPackageData()['tmp_name'], |
105 | $this->getTestClass(), |
106 | $this->isMetadataGuardiansEnabled(), |
107 | $this->isMetadataValidatorsEnabled(), |
108 | $this->isItemMustExistEnabled(), |
109 | $this->isItemMustBeOverwrittenEnabled(), |
110 | $this->isOverwriteTest(), |
111 | $this->getItemClassUri(), |
112 | $this->getOverwriteTestUri(), |
113 | $this->getPackageLabel() |
114 | ); |
115 | |
116 | if ($report->getType() === common_report_Report::TYPE_SUCCESS) { |
117 | $data = []; |
118 | foreach ($report as $r) { |
119 | $values = $r->getData(); |
120 | $testid = $values->rdfsResource->getUri(); |
121 | foreach ($values->items as $item) { |
122 | $itemsid[] = $item->getUri(); |
123 | } |
124 | $data[] = [ |
125 | 'testId' => $testid, |
126 | 'testItems' => $itemsid |
127 | ]; |
128 | } |
129 | return $this->returnSuccess($data); |
130 | } else { |
131 | throw new common_exception_RestApi($report->getMessage()); |
132 | } |
133 | } catch (common_exception_RestApi $e) { |
134 | return $this->returnFailure($e); |
135 | } |
136 | } |
137 | |
138 | /** |
139 | * @throws common_exception_RestApi |
140 | */ |
141 | protected function getItemClassUri(): string |
142 | { |
143 | $itemClassUri = $this->getPostParameter(self::ITEM_CLASS_URI, TaoOntology::CLASS_URI_ITEM); |
144 | $itemClass = $this->getClass($itemClassUri); |
145 | |
146 | if (!$itemClass->exists()) { |
147 | throw new common_exception_RestApi('Class does not exist. Please use valid ' . self::ITEM_CLASS_URI); |
148 | } |
149 | |
150 | return $itemClassUri; |
151 | } |
152 | |
153 | /** |
154 | * @throws common_exception_RestApi |
155 | */ |
156 | protected function isOverwriteTest(): bool |
157 | { |
158 | $isOverwriteTest = $this->getPostParameter(self::OVERWRITE_TEST); |
159 | |
160 | if (is_null($isOverwriteTest)) { |
161 | return false; |
162 | } |
163 | |
164 | if (!in_array($isOverwriteTest, ['true', 'false'])) { |
165 | throw new common_exception_RestApi( |
166 | 'isOverwriteTest parameter should be boolean (true or false).' |
167 | ); |
168 | } |
169 | |
170 | return filter_var($isOverwriteTest, FILTER_VALIDATE_BOOLEAN); |
171 | } |
172 | |
173 | /** |
174 | * @inheritdoc |
175 | */ |
176 | protected function getTaskName() |
177 | { |
178 | return ImportQtiTest::class; |
179 | } |
180 | |
181 | /** |
182 | * Import test package through the task queue. |
183 | * Check POST method & get valid uploaded file |
184 | */ |
185 | public function importDeferred() |
186 | { |
187 | try { |
188 | if ($this->getRequestMethod() != Request::HTTP_POST) { |
189 | throw new \common_exception_NotImplemented('Only post method is accepted to import Qti package.'); |
190 | } |
191 | |
192 | $task = ImportQtiTest::createTask( |
193 | $this->getUploadedPackageData(), |
194 | $this->getTestClass(), |
195 | $this->isMetadataGuardiansEnabled(), |
196 | $this->isMetadataValidatorsEnabled(), |
197 | $this->isItemMustExistEnabled(), |
198 | $this->isItemMustBeOverwrittenEnabled(), |
199 | $this->isOverwriteTest(), |
200 | $this->getItemClassUri(), |
201 | $this->getOverwriteTestUri(), |
202 | $this->getPackageLabel() |
203 | ); |
204 | |
205 | $result = [ |
206 | 'reference_id' => $task->getId(), |
207 | ]; |
208 | |
209 | /** @var TaskLogInterface $taskLog */ |
210 | $taskLog = $this->getServiceManager()->get(TaskLogInterface::SERVICE_ID); |
211 | |
212 | if ($report = $taskLog->getReport($task->getId())) { |
213 | $result['report'] = $report->toArray(); |
214 | } |
215 | |
216 | return $this->returnSuccess($result); |
217 | } catch (common_exception_RestApi $e) { |
218 | return $this->returnFailure($e); |
219 | } |
220 | } |
221 | |
222 | /** |
223 | * @throws common_exception_NotImplemented |
224 | */ |
225 | public function getItems(): void |
226 | { |
227 | $request = $this->getPsrRequest(); |
228 | $testUri = $request->getQueryParams()[self::PARAM_TEST_URI] ?? null; |
229 | try { |
230 | if ($request->getMethod() !== Request::HTTP_GET || empty($testUri)) { |
231 | throw new common_exception_MissingParameter(self::PARAM_TEST_URI, $this->getRequestURI()); |
232 | } |
233 | $testResource = $this->getResource($testUri); |
234 | |
235 | $this->returnSuccess( |
236 | array_map(static function (Resource $item) { |
237 | return ['itemUri' => $item->getUri()]; |
238 | }, array_values($this->getQtiTestService()->getItems($testResource))) |
239 | ); |
240 | } catch (MissingTestmodelException $e) { |
241 | $this->returnFailure(new common_exception_NotFound( |
242 | sprintf('Test %s not found', $testUri), |
243 | StatusCode::HTTP_NOT_FOUND, |
244 | $e |
245 | )); |
246 | } catch (Exception $e) { |
247 | $this->returnFailure($e); |
248 | } |
249 | } |
250 | |
251 | /** |
252 | * Add extra values to the JSON returned. |
253 | * |
254 | * @param EntityInterface $taskLogEntity |
255 | * @return array |
256 | */ |
257 | protected function addExtraReturnData(EntityInterface $taskLogEntity) |
258 | { |
259 | $data = []; |
260 | |
261 | if ($taskLogEntity->getReport()) { |
262 | $plainReport = $this->getPlainReport($taskLogEntity->getReport()); |
263 | |
264 | //the third report is report of import test |
265 | if (isset($plainReport[2]) && isset($plainReport[2]->getData()['rdfsResource'])) { |
266 | $data['testId'] = $plainReport[2]->getData()['rdfsResource']['uriResource']; |
267 | } |
268 | } |
269 | |
270 | return $data; |
271 | } |
272 | |
273 | /** |
274 | * Create a Test Class |
275 | * |
276 | * Label parameter is mandatory |
277 | * If parent class parameter is an uri of valid test class, new class will be created under it |
278 | * If not parent class parameter is provided, class will be created under root class |
279 | * Comment parameter is not mandatory, used to describe new created class |
280 | */ |
281 | public function createClass(): void |
282 | { |
283 | try { |
284 | $class = $this->createSubClass(new core_kernel_classes_Class(TaoOntology::CLASS_URI_TEST)); |
285 | |
286 | $this->returnSuccess([ |
287 | 'message' => __('Class successfully created.'), |
288 | 'class-uri' => $class->getUri(), |
289 | ]); |
290 | } catch (common_exception_ClassAlreadyExists $e) { |
291 | $this->returnSuccess([ |
292 | 'message' => $e->getMessage(), |
293 | 'class-uri' => $e->getClass()->getUri(), |
294 | ]); |
295 | } catch (Exception $e) { |
296 | $this->returnFailure($e); |
297 | } |
298 | } |
299 | |
300 | /** |
301 | * @return array |
302 | * @throws common_exception_Error |
303 | * @throws common_exception_RestApi |
304 | */ |
305 | private function getUploadedPackageData() |
306 | { |
307 | if (!tao_helpers_Http::hasUploadedFile(self::PARAM_PACKAGE_NAME)) { |
308 | throw new common_exception_RestApi(__('Missed test package file')); |
309 | } |
310 | |
311 | $fileData = tao_helpers_Http::getUploadedFile(self::PARAM_PACKAGE_NAME); |
312 | |
313 | $mimeType = tao_helpers_File::getMimeType($fileData['tmp_name']); |
314 | |
315 | if (!in_array($mimeType, self::$accepted_types)) { |
316 | throw new common_exception_RestApi(__('Wrong file mime type')); |
317 | } |
318 | |
319 | return $fileData; |
320 | } |
321 | |
322 | /** |
323 | * @return string|null |
324 | * @throws common_exception_RestApi |
325 | */ |
326 | private function getOverwriteTestUri(): ?string |
327 | { |
328 | $overwriteTestUri = $this->getPostParameter(self::OVERWRITE_TEST_URI); |
329 | |
330 | if ($overwriteTestUri !== null && !is_string($overwriteTestUri)) { |
331 | throw new common_exception_RestApi( |
332 | sprintf('%s parameter should be string', self::OVERWRITE_TEST_URI) |
333 | ); |
334 | } |
335 | |
336 | return $overwriteTestUri; |
337 | } |
338 | |
339 | /** |
340 | * @return string|null |
341 | * @throws common_exception_RestApi |
342 | */ |
343 | private function getPackageLabel(): ?string |
344 | { |
345 | $packageLabel = $this->getPostParameter(self::PACKAGE_LABEL); |
346 | |
347 | if ($packageLabel !== null && !is_string($packageLabel)) { |
348 | throw new common_exception_RestApi( |
349 | sprintf('%s parameter should be string', self::PACKAGE_LABEL) |
350 | ); |
351 | } |
352 | |
353 | return $packageLabel; |
354 | } |
355 | |
356 | /** |
357 | * Get class instance to import test |
358 | * |
359 | * @throws common_exception_RestApi |
360 | */ |
361 | private function getTestClass(): core_kernel_classes_Class |
362 | { |
363 | return $this->getClassFromRequest(new core_kernel_classes_Class(TaoOntology::CLASS_URI_TEST)); |
364 | } |
365 | |
366 | /** |
367 | * @return QtiPackageExporter |
368 | */ |
369 | private function getQtiPackageExporter(): QtiPackageExporter |
370 | { |
371 | return $this->getServiceLocator()->get(QtiPackageExporter::SERVICE_ID); |
372 | } |
373 | |
374 | private function getQtiTestService(): taoQtiTest_models_classes_QtiTestService |
375 | { |
376 | return $this->getServiceLocator()->get(taoQtiTest_models_classes_QtiTestService::class); |
377 | } |
378 | } |