Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
23 / 23 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
TestDefinitionSerializerService | |
100.00% |
23 / 23 |
|
100.00% |
5 / 5 |
8 | |
100.00% |
1 / 1 |
getSerializedTestDefinition | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
parseXmlToArray | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
setSubObjectToArray | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
getTestDefinitionXml | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getFileStorageService | |
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) 2019 (original work) Open Assessment Technologies SA ; |
19 | */ |
20 | |
21 | /** |
22 | * @author Péter Halász <peter@taotesting.com> |
23 | */ |
24 | |
25 | namespace oat\taoQtiTest\models\runner; |
26 | |
27 | use oat\oatbox\service\ConfigurableService; |
28 | use tao_models_classes_service_FileStorage; |
29 | use taoQtiTest_models_classes_QtiTestService; |
30 | |
31 | class TestDefinitionSerializerService extends ConfigurableService |
32 | { |
33 | public const SERVICE_ID = 'taoQtiTest/TestDefinitionSerializerService'; |
34 | |
35 | /** |
36 | * @param QtiRunnerServiceContext $serviceContext |
37 | * @return array |
38 | */ |
39 | public function getSerializedTestDefinition(QtiRunnerServiceContext $serviceContext) |
40 | { |
41 | return $this->parseXmlToArray($this->getTestDefinitionXml($serviceContext)); |
42 | } |
43 | |
44 | /** |
45 | * @param string $xml |
46 | * @return array |
47 | */ |
48 | private function parseXmlToArray($xml) |
49 | { |
50 | $xml = simplexml_load_string($xml); |
51 | $parsedXml = json_decode(json_encode($xml), true); |
52 | |
53 | return $this->setSubObjectToArray( |
54 | $parsedXml, |
55 | [ |
56 | taoQtiTest_models_classes_QtiTestService::XML_TEST_PART, |
57 | taoQtiTest_models_classes_QtiTestService::XML_ASSESSMENT_SECTION, |
58 | taoQtiTest_models_classes_QtiTestService::XML_ASSESSMENT_ITEM_REF, |
59 | ] |
60 | ); |
61 | } |
62 | |
63 | /** |
64 | * Changes recursively the value of the given sub-keys into array in the given object |
65 | * |
66 | * @param array $object |
67 | * @param array $keys |
68 | * @return array |
69 | */ |
70 | private function setSubObjectToArray($object, $keys) |
71 | { |
72 | $key = array_shift($keys); |
73 | |
74 | // if the object is an associative array, turn it into an indexed array |
75 | if ($object[$key] !== array_values($object[$key])) { |
76 | $object[$key] = [$object[$key]]; |
77 | } |
78 | |
79 | if (count($keys) > 0) { |
80 | foreach ($object[$key] as $index => $subObject) { |
81 | $object[$key][$index] = $this->setSubObjectToArray($subObject, $keys); |
82 | } |
83 | } |
84 | |
85 | return $object; |
86 | } |
87 | |
88 | /** |
89 | * @param QtiRunnerServiceContext $serviceContext |
90 | * @return string |
91 | */ |
92 | private function getTestDefinitionXml(QtiRunnerServiceContext $serviceContext) |
93 | { |
94 | $privateDirectoryId = explode('|', $serviceContext->getTestCompilationUri())[0]; |
95 | $directory = $this->getFileStorageService()->getDirectoryById($privateDirectoryId); |
96 | |
97 | $path = $directory->getFile(taoQtiTest_models_classes_QtiTestService::QTI_TEST_DEFINITION_INDEX)->read(); |
98 | |
99 | return $directory->getFile($path)->read(); |
100 | } |
101 | |
102 | /** |
103 | * @return tao_models_classes_service_FileStorage |
104 | */ |
105 | private function getFileStorageService() |
106 | { |
107 | return $this->getServiceLocator()->get(\tao_models_classes_service_FileStorage::SERVICE_ID); |
108 | } |
109 | } |