Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 81 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
taoQtiTest_actions_Items | |
0.00% |
0 / 81 |
|
0.00% |
0 / 9 |
992 | |
0.00% |
0 / 1 |
get | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
42 | |||
getItemClasses | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getItems | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
110 | |||
getCategories | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
42 | |||
getQtiItems | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
getItemService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getCreatorItemsService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
returnSuccess | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
returnFailure | |
0.00% |
0 / 7 |
|
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) 2013-2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
19 | */ |
20 | |
21 | use oat\generis\model\OntologyRdfs; |
22 | use oat\taoQtiTest\models\creator\CreatorItems; |
23 | use oat\taoItems\model\CategoryService; |
24 | use qtism\common\utils\Format; |
25 | |
26 | /** |
27 | * Actions about Items in a Test context. |
28 | * |
29 | * @author Bertrand Chevrier <bertrand@taotesting.com> |
30 | * @author Jérôme Bogaerts <jerome@taotesting.com> |
31 | */ |
32 | class taoQtiTest_actions_Items extends tao_actions_CommonModule |
33 | { |
34 | /** |
35 | * Get ALL QTI items within the platform. |
36 | * |
37 | * The response is encoded in JSON and contains only some basic data about items (uri, label keys). |
38 | * A 'pattern' request parameter parameter is allowed to filter results at search time. |
39 | * A 'notempty' ('1', 'true', 'on' and 'yes' values available) request parameter is allowed to filter empty items. |
40 | * |
41 | * This method will be refactored (limit, filtering, etc.) with the resource widget. |
42 | * |
43 | * @deprecated use getItems instead |
44 | */ |
45 | public function get() |
46 | { |
47 | $items = []; |
48 | $propertyFilters = [ |
49 | // phpcs:disable Generic.Files.LineLength |
50 | taoItems_models_classes_ItemsService::PROPERTY_ITEM_MODEL => taoItems_models_classes_itemModel::CLASS_URI_QTI |
51 | // phpcs:enable Generic.Files.LineLength |
52 | ]; |
53 | $options = ['recursive' => true, 'like' => true, 'limit' => 50]; |
54 | $notEmpty = filter_var($this->getRequestParameter('notempty'), FILTER_VALIDATE_BOOLEAN); |
55 | |
56 | if (($pattern = $this->getRequestParameter('pattern')) !== null && $pattern !== '') { |
57 | $propertyFilters[OntologyRdfs::RDFS_LABEL] = $pattern; |
58 | } |
59 | |
60 | $itemsService = taoItems_models_classes_ItemsService::singleton(); |
61 | $itemClass = $itemsService->getRootClass(); |
62 | |
63 | $result = $itemClass->searchInstances($propertyFilters, $options); |
64 | |
65 | foreach ($result as $qtiItem) { |
66 | if (!$notEmpty || $itemsService->hasItemContent($qtiItem)) { |
67 | $items[] = [ |
68 | 'uri' => $qtiItem->getUri(), |
69 | 'label' => $qtiItem->getLabel() |
70 | ]; |
71 | } |
72 | } |
73 | |
74 | $this->returnJson($items); |
75 | } |
76 | |
77 | /** |
78 | * Get the list of items classes |
79 | */ |
80 | public function getItemClasses() |
81 | { |
82 | try { |
83 | $data = $this->getCreatorItemsService()->getItemClasses(); |
84 | } catch (\common_Exception $e) { |
85 | return $this->returnFailure($e); |
86 | } |
87 | |
88 | return $this->returnSuccess([$data]); |
89 | } |
90 | |
91 | /** |
92 | * Retrieve non empty QTI Items, using different parameters : |
93 | * - format (list or tree) |
94 | * - classUri (top class) |
95 | * - pattern (label filtering) |
96 | * - offset/limit (paginate) |
97 | */ |
98 | public function getItems() |
99 | { |
100 | try { |
101 | if (!$this->hasRequestParameter('classUri')) { |
102 | throw new \InvalidArgumentException('Missing parameter classUri'); |
103 | } |
104 | if (!$this->hasRequestParameter('format')) { |
105 | throw new \InvalidArgumentException('Missing parameter format'); |
106 | } |
107 | |
108 | $classUri = $this->getRequestParameter('classUri'); |
109 | $format = $this->getRequestParameter('format'); |
110 | $search = $this->hasRequestParameter('search') ? $this->getRawParameter('search') : ''; |
111 | $limit = $this->hasRequestParameter('limit') ? $this->getRequestParameter('limit') : 30; |
112 | $offset = $this->hasRequestParameter('offset') ? $this->getRequestParameter('offset') : 0; |
113 | |
114 | if (! empty($search)) { |
115 | $decodedSearch = json_decode($search, true); |
116 | if (is_array($decodedSearch) && count($decodedSearch) > 0) { |
117 | $search = $decodedSearch; |
118 | } |
119 | } |
120 | |
121 | $itemClass = new \core_kernel_classes_Class($classUri); |
122 | $data = $this->getCreatorItemsService()->getQtiItems($itemClass, $format, $search, $offset, $limit); |
123 | } catch (\Exception $e) { |
124 | return $this->returnFailure($e); |
125 | } |
126 | |
127 | return $this->returnSuccess($data); |
128 | } |
129 | |
130 | |
131 | /** |
132 | * Get all categories related to a list of items. |
133 | * |
134 | * The response is encoded in JSON and contains the list of items and its categories. |
135 | * parameter uris is required in order to get categories for one or more items |
136 | * @throws common_exception_MissingParameter |
137 | * |
138 | * @deprecated the categories are retrieved with the items in getItems instead |
139 | */ |
140 | public function getCategories() |
141 | { |
142 | if (!$this->hasRequestParameter('uris')) { |
143 | $this->returnJson( |
144 | __("At least one mandatory parameter was required but found missing in your request"), |
145 | 412 |
146 | ); |
147 | return; |
148 | } |
149 | |
150 | $categories = []; |
151 | $uris = $this->getRequestParameter('uris'); |
152 | $uris = (!is_array($uris)) ? [$uris] : $uris; |
153 | |
154 | $items = $this->getQtiItems($uris); |
155 | |
156 | if (count($items) > 0) { |
157 | $service = $this->getServiceManager()->get(CategoryService::SERVICE_ID); |
158 | $itemsCategories = $service->getItemsCategories($items); |
159 | |
160 | //filter all values that wouldn't be valid XML identifiers |
161 | foreach ($itemsCategories as $itemUri => $itemCategories) { |
162 | $filtered = array_filter($itemCategories, function ($categorie) { |
163 | return Format::isIdentifier($categorie); |
164 | }); |
165 | if (count($filtered) > 0) { |
166 | $categories[$itemUri] = $filtered; |
167 | } |
168 | } |
169 | } |
170 | $this->returnJson($categories); |
171 | } |
172 | |
173 | /** |
174 | * Get the qti items from a list of uris |
175 | * @param array $itemUris list of item uris to get |
176 | * @return core_kernel_classes_Resource[] $items |
177 | */ |
178 | private function getQtiItems(array $itemUris) |
179 | { |
180 | $items = []; |
181 | |
182 | foreach ($itemUris as $uri) { |
183 | $item = new \core_kernel_classes_Resource($uri); |
184 | if ($this->getItemService()->hasItemModel($item, [\oat\taoQtiItem\model\ItemModel::MODEL_URI])) { |
185 | $items[$uri] = $item; |
186 | } |
187 | } |
188 | |
189 | return $items; |
190 | } |
191 | |
192 | /** |
193 | * Get the ItemService |
194 | * @return \taoItems_models_classes_ItemsService the service |
195 | */ |
196 | private function getItemService() |
197 | { |
198 | return \taoItems_models_classes_ItemsService::singleton(); |
199 | } |
200 | |
201 | /** |
202 | * Get the CreatorItems service |
203 | * @return CreatorItems the service |
204 | */ |
205 | private function getCreatorItemsService() |
206 | { |
207 | return $this->getServiceManager()->get(CreatorItems::SERVICE_ID); |
208 | } |
209 | |
210 | /** |
211 | * Helps you to format 200 responses, |
212 | * using the usual format [success,data] |
213 | * @param mixed $data the response data to encode |
214 | * @return string the json |
215 | */ |
216 | protected function returnSuccess($data) |
217 | { |
218 | $returnArray = [ |
219 | 'success' => true, |
220 | 'data' => $data |
221 | ]; |
222 | |
223 | return $this->returnJson($returnArray); |
224 | } |
225 | |
226 | /** |
227 | * Helps you to format failures responses. |
228 | * @param \Exception $e |
229 | * @return string the json |
230 | */ |
231 | protected function returnFailure(\Exception $exception) |
232 | { |
233 | |
234 | \common_Logger::e($exception); |
235 | |
236 | $returnArray = [ |
237 | 'success' => false, |
238 | 'errorCode' => 500, |
239 | 'errorMsg' => $exception->getMessage() |
240 | ]; |
241 | |
242 | return $this->returnJson($returnArray, 500); |
243 | } |
244 | } |