Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 59 |
|
0.00% |
0 / 19 |
CRAP | |
0.00% |
0 / 1 |
ExtendedStateService | |
0.00% |
0 / 59 |
|
0.00% |
0 / 19 |
552 | |
0.00% |
0 / 1 |
getStorageService | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
setStorageService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getSessionUserUri | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
getExtendedState | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
persist | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
setItemFlag | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getItemFlag | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setStoreId | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getStoreId | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
addEvent | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
getEvents | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
removeEvents | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
clearEvents | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
storeItemHrefIndex | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
loadItemHrefIndex | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setCatValue | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getCatValue | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
removeCatValue | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
deleteDeliveryExecutionData | |
0.00% |
0 / 4 |
|
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) 2015-2017 (original work) Open Assessment Technologies SA; |
19 | */ |
20 | |
21 | namespace oat\taoQtiTest\models; |
22 | |
23 | use oat\oatbox\service\ConfigurableService; |
24 | use oat\tao\model\state\StateStorage; |
25 | use oat\taoDelivery\model\execution\ServiceProxy; |
26 | use oat\taoDelivery\model\execution\Delete\DeliveryExecutionDelete; |
27 | use oat\taoDelivery\model\execution\Delete\DeliveryExecutionDeleteRequest; |
28 | use oat\taoQtiTest\models\runner\ExtendedState; |
29 | use oat\taoQtiTest\models\runner\StorageManager; |
30 | |
31 | /** |
32 | * Manage the flagged items |
33 | */ |
34 | class ExtendedStateService extends ConfigurableService implements DeliveryExecutionDelete |
35 | { |
36 | public const SERVICE_ID = 'taoQtiTest/ExtendedStateService'; |
37 | |
38 | protected $cache = []; |
39 | protected $deliveryExecutions = []; |
40 | |
41 | /** |
42 | * @var StateStorage |
43 | */ |
44 | protected $storageService; |
45 | |
46 | /** |
47 | * Gets the StateStorage service |
48 | * @return StorageManager |
49 | */ |
50 | public function getStorageService() |
51 | { |
52 | if (!$this->storageService) { |
53 | $this->storageService = $this->getServiceLocator()->get(StorageManager::SERVICE_ID); |
54 | } |
55 | return $this->storageService; |
56 | } |
57 | |
58 | /** |
59 | * Sets the StateStorage service |
60 | * @param StateStorage $storageService |
61 | */ |
62 | public function setStorageService($storageService) |
63 | { |
64 | $this->storageService = $storageService; |
65 | } |
66 | |
67 | /** |
68 | * @param string $testSessionId |
69 | * @return string |
70 | * @throws \common_exception_Error |
71 | */ |
72 | protected function getSessionUserUri($testSessionId) |
73 | { |
74 | if (!isset($this->deliveryExecutions[$testSessionId])) { |
75 | $this->deliveryExecutions[$testSessionId] = ServiceProxy::singleton()->getDeliveryExecution($testSessionId); |
76 | } |
77 | if ($this->deliveryExecutions[$testSessionId]) { |
78 | return $this->deliveryExecutions[$testSessionId]->getUserIdentifier(); |
79 | } |
80 | return \common_session_SessionManager::getSession()->getUserUri(); |
81 | } |
82 | |
83 | /** |
84 | * @param $testSessionId |
85 | * @return ExtendedState |
86 | * @throws \common_exception_Error |
87 | */ |
88 | public function getExtendedState($testSessionId) |
89 | { |
90 | if (!isset($this->cache[$testSessionId])) { |
91 | $extendedState = new ExtendedState($testSessionId, $this->getSessionUserUri($testSessionId)); |
92 | $this->getServiceManager()->propagate($extendedState); |
93 | $extendedState->setStorage($this->getStorageService()); |
94 | $extendedState->load(); |
95 | $this->cache[$testSessionId] = $extendedState; |
96 | } |
97 | return $this->cache[$testSessionId]; |
98 | } |
99 | |
100 | /** |
101 | * Persists the extended state |
102 | * @param string $testSessionId |
103 | * @throws \common_exception_Error |
104 | */ |
105 | public function persist($testSessionId) |
106 | { |
107 | $extendedState = $this->getExtendedState($testSessionId); |
108 | $extendedState->save(); |
109 | $this->getStorageService()->persist($extendedState->getUserId(), $extendedState->getStorageKey()); |
110 | } |
111 | |
112 | /** |
113 | * Set the marked for review state of an item |
114 | * @param string $testSessionId |
115 | * @param string $itemRef |
116 | * @param boolean $flag |
117 | * @throws \common_Exception |
118 | */ |
119 | public function setItemFlag($testSessionId, $itemRef, $flag) |
120 | { |
121 | $extendedState = $this->getExtendedState($testSessionId); |
122 | $extendedState->setItemFlag($itemRef, $flag); |
123 | $extendedState->save(); |
124 | } |
125 | |
126 | /** |
127 | * Gets the marked for review state of an item |
128 | * @param string $testSessionId |
129 | * @param string $itemRef |
130 | * @return bool |
131 | * @throws \common_Exception |
132 | */ |
133 | public function getItemFlag($testSessionId, $itemRef) |
134 | { |
135 | $extendedState = $this->getExtendedState($testSessionId); |
136 | return $extendedState->getItemFlag($itemRef); |
137 | } |
138 | |
139 | /** |
140 | * Sets the name of the client store used for the timer |
141 | * @param string $testSessionId |
142 | * @param string $storeId |
143 | * @throws \common_Exception |
144 | */ |
145 | public function setStoreId($testSessionId, $storeId) |
146 | { |
147 | $extendedState = $this->getExtendedState($testSessionId); |
148 | $extendedState->setStoreId($storeId); |
149 | $extendedState->save(); |
150 | } |
151 | |
152 | /** |
153 | * Gets the name of the client store used for the timer |
154 | * @param string $testSessionId |
155 | * @return bool |
156 | * @throws \common_Exception |
157 | */ |
158 | public function getStoreId($testSessionId) |
159 | { |
160 | $extendedState = $this->getExtendedState($testSessionId); |
161 | return $extendedState->getStoreId(); |
162 | } |
163 | |
164 | /** |
165 | * Add an event on top of the queue |
166 | * @param string $testSessionId |
167 | * @param string $eventName |
168 | * @param mixed $data |
169 | * @return string |
170 | * @throws \common_Exception |
171 | */ |
172 | public function addEvent($testSessionId, $eventName, $data = null) |
173 | { |
174 | $extendedState = $this->getExtendedState($testSessionId); |
175 | $eventId = $extendedState->addEvent($eventName, $data); |
176 | $extendedState->save(); |
177 | return $eventId; |
178 | } |
179 | |
180 | /** |
181 | * Gets all events from the queue |
182 | * @param $testSessionId |
183 | * @return array|mixed |
184 | * @throws \common_Exception |
185 | */ |
186 | public function getEvents($testSessionId) |
187 | { |
188 | $extendedState = $this->getExtendedState($testSessionId); |
189 | return $extendedState->getEvents(); |
190 | } |
191 | |
192 | /** |
193 | * Removes particular events from the queue |
194 | * @param $testSessionId |
195 | * @param array $ids |
196 | * @throws \common_Exception |
197 | */ |
198 | public function removeEvents($testSessionId, $ids = []) |
199 | { |
200 | $extendedState = $this->getExtendedState($testSessionId); |
201 | $extendedState->removeEvents($ids); |
202 | $extendedState->save(); |
203 | } |
204 | |
205 | /** |
206 | * Removes all events from the queue |
207 | * @param $testSessionId |
208 | * @throws \common_Exception |
209 | */ |
210 | public function clearEvents($testSessionId) |
211 | { |
212 | $extendedState = $this->getExtendedState($testSessionId); |
213 | $extendedState->clearEvents(); |
214 | $extendedState->save(); |
215 | } |
216 | |
217 | /** |
218 | * Stores the table that maps the items identifiers to item reference |
219 | * Fallback index in case of the delivery was compiled without the index of item href |
220 | * @param $testSessionId |
221 | * @param array $table |
222 | * @throws \common_Exception |
223 | */ |
224 | public function storeItemHrefIndex($testSessionId, $table) |
225 | { |
226 | $extendedState = $this->getExtendedState($testSessionId); |
227 | $extendedState->setItemHrefIndex($table); |
228 | $extendedState->save(); |
229 | } |
230 | |
231 | /** |
232 | * Loads the table that maps the items identifiers to item reference |
233 | * Fallback index in case of the delivery was compiled without the index of item href |
234 | * @param $testSessionId |
235 | * @return array |
236 | * @throws \common_Exception |
237 | */ |
238 | public function loadItemHrefIndex($testSessionId) |
239 | { |
240 | $extendedState = $this->getExtendedState($testSessionId); |
241 | return $extendedState->getItemHrefIndex(); |
242 | } |
243 | |
244 | /** |
245 | * Set a CAT Value |
246 | * |
247 | * Set a CAT value in the Extended State. |
248 | * |
249 | * @param string $testSessionId |
250 | * @param string $assessmentSectionId |
251 | * @param string $key |
252 | * @param string $value |
253 | * @throws \common_Exception |
254 | */ |
255 | public function setCatValue($testSessionId, $assessmentSectionId, $key, $value) |
256 | { |
257 | $extendedState = $this->getExtendedState($testSessionId); |
258 | $extendedState->setCatValue($assessmentSectionId, $key, $value); |
259 | $extendedState->save(); |
260 | } |
261 | |
262 | /** |
263 | * Get a CAT Value |
264 | * |
265 | * Get a CAT value from the Extended State. |
266 | * |
267 | * @param string $testSessionId |
268 | * @param string $assessmentSectionId |
269 | * @param string $key |
270 | * @return string |
271 | * @throws \common_Exception |
272 | */ |
273 | public function getCatValue($testSessionId, $assessmentSectionId, $key) |
274 | { |
275 | $extendedState = $this->getExtendedState($testSessionId); |
276 | return $extendedState->getCatValue($assessmentSectionId, $key); |
277 | } |
278 | |
279 | /** |
280 | * Remove a CAT value from the ExtendedState. |
281 | * |
282 | * @param string $testSessionId |
283 | * @param string $assessmentSectionId |
284 | * @param string $key |
285 | * @throws \common_Exception |
286 | */ |
287 | public function removeCatValue($testSessionId, $assessmentSectionId, $key) |
288 | { |
289 | $extendedState = $this->getExtendedState($testSessionId); |
290 | $extendedState->removeCatValue($assessmentSectionId, $key); |
291 | $extendedState->save(); |
292 | } |
293 | |
294 | /** |
295 | * @inheritdoc |
296 | */ |
297 | public function deleteDeliveryExecutionData(DeliveryExecutionDeleteRequest $request) |
298 | { |
299 | $sessionId = $request->getDeliveryExecution()->getIdentifier(); |
300 | $extendedState = $this->getExtendedState($sessionId); |
301 | $extendedState->deleteDeliveryExecutionData($request); |
302 | |
303 | return $this->getStorageService()->persist($extendedState->getUserId(), $extendedState->getStorageKey()); |
304 | } |
305 | } |