Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
93.37% |
155 / 166 |
|
77.27% |
17 / 22 |
CRAP | |
0.00% |
0 / 1 |
DeliveryExecutionList | |
93.37% |
155 / 166 |
|
77.27% |
17 / 22 |
52.79 | |
0.00% |
0 / 1 |
adjustDeliveryExecutions | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
createTestTaker | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
createExecution | |
97.96% |
48 / 49 |
|
0.00% |
0 / 1 |
9 | |||
isPausedByProctor | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
getLastProctorPauseReason | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
getDeliveryLogService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSingleExecution | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getFieldId | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
getApproximatedRemainingTime | |
57.14% |
8 / 14 |
|
0.00% |
0 / 1 |
6.97 | |||
getRemainingTime | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getUserExtraFields | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
mergeExtraFieldsSettings | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
2.01 | |||
getProgressString | |
89.47% |
17 / 19 |
|
0.00% |
0 / 1 |
4.02 | |||
isOnline | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
getLastActivity | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
sanitizeUserInput | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getApplicationService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getTestSessionConnectivityStatusService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSessionStateService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getExtensionManagerService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDeliveryExecutionManagerService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createState | |
100.00% |
6 / 6 |
|
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-2022 (original work) Open Assessment Technologies SA; |
19 | */ |
20 | |
21 | namespace oat\taoProctoring\model\execution; |
22 | |
23 | use common_Exception; |
24 | use common_exception_Error; |
25 | use common_exception_NotFound; |
26 | use common_ext_ExtensionException; |
27 | use common_ext_ExtensionsManager; |
28 | use oat\generis\model\OntologyAwareTrait; |
29 | use oat\oatbox\service\ConfigurableService; |
30 | use oat\oatbox\service\exception\InvalidServiceManagerException; |
31 | use oat\oatbox\user\User; |
32 | use oat\tao\model\service\ApplicationService; |
33 | use oat\taoDelivery\model\execution\DeliveryExecutionInterface; |
34 | use oat\taoProctoring\model\deliveryLog\DeliveryLog; |
35 | use oat\taoProctoring\model\deliveryLog\event\DeliveryLogEvent; |
36 | use oat\taoProctoring\model\monitorCache\DeliveryMonitoringService; |
37 | use oat\taoProctoring\model\TestSessionConnectivityStatusService; |
38 | use oat\taoQtiTest\models\QtiTestExtractionFailedException; |
39 | use oat\taoQtiTest\models\SessionStateService; |
40 | use tao_helpers_Uri; |
41 | |
42 | /** |
43 | * Class DeliveryExecutionList |
44 | * @author Bartlomiej Marszal |
45 | */ |
46 | class DeliveryExecutionList extends ConfigurableService implements DeliveryExecutionListInterface |
47 | { |
48 | use OntologyAwareTrait; |
49 | |
50 | /** |
51 | * Adjusts a list of delivery executions: add information, format the result |
52 | * @param DeliveryExecution[] $deliveryExecutions |
53 | * @return array |
54 | * @throws common_ext_ExtensionException |
55 | * @throws common_exception_Error |
56 | * @internal param array $options |
57 | */ |
58 | public function adjustDeliveryExecutions($deliveryExecutions) |
59 | { |
60 | $executions = []; |
61 | $userExtraFields = $this->getUserExtraFields(); |
62 | |
63 | /** @var array $cachedData */ |
64 | foreach ($deliveryExecutions as $cachedData) { |
65 | $executions[] = $this->getSingleExecution($cachedData, $userExtraFields); |
66 | } |
67 | |
68 | return $executions; |
69 | } |
70 | |
71 | /** |
72 | * @param $cachedData |
73 | * @return array |
74 | * @throws common_exception_Error |
75 | * @throws common_ext_ExtensionException |
76 | */ |
77 | private function createTestTaker($cachedData) |
78 | { |
79 | $testTaker = []; |
80 | |
81 | /* @var $user User */ |
82 | $testTaker['id'] = $cachedData[DeliveryMonitoringService::TEST_TAKER]; |
83 | $testTaker['test_taker_last_name'] = isset($cachedData[DeliveryMonitoringService::TEST_TAKER_LAST_NAME]) |
84 | ? $this->sanitizeUserInput($cachedData[DeliveryMonitoringService::TEST_TAKER_LAST_NAME]) |
85 | : ''; |
86 | $testTaker['test_taker_first_name'] = isset($cachedData[DeliveryMonitoringService::TEST_TAKER_FIRST_NAME]) |
87 | ? $this->sanitizeUserInput($cachedData[DeliveryMonitoringService::TEST_TAKER_FIRST_NAME]) |
88 | : ''; |
89 | |
90 | return $testTaker; |
91 | } |
92 | |
93 | /** |
94 | * @param $cachedData |
95 | * @param $extraFields |
96 | * @return array |
97 | * @throws common_Exception |
98 | * @throws common_exception_Error |
99 | * @throws common_ext_ExtensionException |
100 | * @throws QtiTestExtractionFailedException |
101 | */ |
102 | protected function createExecution($cachedData, $extraFields): array |
103 | { |
104 | $online = $this->isOnline($cachedData); |
105 | |
106 | $isTimerAdjustmentAllowed = $this->getDeliveryExecutionManagerService()->isTimerAdjustmentAllowed( |
107 | $cachedData[DeliveryMonitoringService::DELIVERY_EXECUTION_ID] |
108 | ); |
109 | |
110 | $executionState = $cachedData[DeliveryMonitoringService::STATUS]; |
111 | |
112 | $adjustedTime = $isTimerAdjustmentAllowed ? $this->getDeliveryExecutionManagerService() |
113 | ->getAdjustedTime($cachedData[DeliveryMonitoringService::DELIVERY_EXECUTION_ID]) : 0; |
114 | |
115 | $execution = array( |
116 | 'id' => $cachedData[DeliveryMonitoringService::DELIVERY_EXECUTION_ID], |
117 | 'delivery' => array( |
118 | 'uri' => $cachedData[DeliveryMonitoringService::DELIVERY_ID], |
119 | 'label' => $this->sanitizeUserInput($cachedData[DeliveryMonitoringService::DELIVERY_NAME]), |
120 | ), |
121 | 'start_time' => $cachedData[DeliveryMonitoringService::START_TIME], |
122 | 'allowExtraTime' => isset($cachedData[DeliveryMonitoringService::ALLOW_EXTRA_TIME]) |
123 | ? (bool)$cachedData[DeliveryMonitoringService::ALLOW_EXTRA_TIME] |
124 | : null, |
125 | 'allowTimerAdjustment' => $isTimerAdjustmentAllowed, |
126 | 'timer' => [ |
127 | 'lastActivity' => $this->getLastActivity($cachedData, $online), |
128 | 'countDown' => DeliveryExecution::STATE_ACTIVE === $executionState && $online, |
129 | 'approximatedRemaining' => $this->getApproximatedRemainingTime($cachedData, $online), |
130 | 'remaining_time' => $this->getRemainingTime($cachedData), |
131 | 'extraTime' => (float) ($cachedData[DeliveryMonitoringService::EXTRA_TIME] ?? 0), |
132 | 'extendedTime' => (isset($cachedData[DeliveryMonitoringService::EXTENDED_TIME]) |
133 | && $cachedData[DeliveryMonitoringService::EXTENDED_TIME] > 1) |
134 | ? (float)$cachedData[DeliveryMonitoringService::EXTENDED_TIME] |
135 | : '', |
136 | 'consumedExtraTime' => (float) ($cachedData[DeliveryMonitoringService::CONSUMED_EXTRA_TIME] ?? 0), |
137 | 'adjustedTime' => $adjustedTime |
138 | ], |
139 | 'testTaker' => $this->createTestTaker($cachedData), |
140 | 'extraFields' => $extraFields, |
141 | 'state' => $this->createState($cachedData), |
142 | ); |
143 | |
144 | if ($this->isOnline($cachedData)) { |
145 | $execution['online'] = $online; |
146 | } |
147 | |
148 | if ($isTimerAdjustmentAllowed) { |
149 | $reason = $this->getLastProctorPauseReason($cachedData[DeliveryMonitoringService::DELIVERY_EXECUTION_ID]); |
150 | if ($reason) { |
151 | $execution['lastPauseReason'] = $reason; |
152 | } |
153 | |
154 | $execution['timer']['timeAdjustmentLimits'] = [ |
155 | 'decrease' => $this->getDeliveryExecutionManagerService()->getTimerAdjustmentDecreaseLimit( |
156 | $cachedData[DeliveryMonitoringService::DELIVERY_EXECUTION_ID] |
157 | ), |
158 | 'increase' => $this->getDeliveryExecutionManagerService()->getTimerAdjustmentIncreaseLimit( |
159 | $cachedData[DeliveryMonitoringService::DELIVERY_EXECUTION_ID] |
160 | ), |
161 | ]; |
162 | } |
163 | |
164 | return $execution; |
165 | } |
166 | |
167 | private function isPausedByProctor($lastPause): bool |
168 | { |
169 | $url = tao_helpers_Uri::getPath( |
170 | _url('pauseExecutions', 'Monitor', 'taoProctoring') |
171 | ); |
172 | return isset( |
173 | $lastPause[0][DeliveryLog::DATA]['reason'], |
174 | $lastPause[0][DeliveryLog::DATA]['context'] |
175 | ) |
176 | && mb_strpos($lastPause[0][DeliveryLog::DATA]['context'], $url) !== false; |
177 | } |
178 | |
179 | private function getLastProctorPauseReason(string $deliveryExecutionId): ?array |
180 | { |
181 | $reason = null; |
182 | $lastPause = $this->getDeliveryLogService()->search([ |
183 | DeliveryLog::DELIVERY_EXECUTION_ID => $deliveryExecutionId, |
184 | DeliveryLog::EVENT_ID => DeliveryLogEvent::EVENT_ID_TEST_PAUSE, |
185 | ], [ |
186 | 'order' => 'created_at', |
187 | 'dir' => 'desc', |
188 | 'limit' => 1, |
189 | ]); |
190 | |
191 | if ($this->isPausedByProctor($lastPause)) { |
192 | $reason = $lastPause[0][DeliveryLog::DATA]['reason']; |
193 | } |
194 | |
195 | return $reason; |
196 | } |
197 | |
198 | /** |
199 | * @return DeliveryLog|object |
200 | */ |
201 | private function getDeliveryLogService(): DeliveryLog |
202 | { |
203 | return $this->getServiceLocator()->get(DeliveryLog::SERVICE_ID); |
204 | } |
205 | |
206 | /** |
207 | * @param $cachedData |
208 | * @param $userExtraFields |
209 | * @return array |
210 | * @throws InvalidServiceManagerException |
211 | * @throws QtiTestExtractionFailedException |
212 | * @throws common_Exception |
213 | * @throws common_exception_Error |
214 | * @throws common_exception_NotFound |
215 | * @throws common_ext_ExtensionException |
216 | */ |
217 | private function getSingleExecution($cachedData, $userExtraFields) |
218 | { |
219 | $extraFields = []; |
220 | foreach ($userExtraFields as $field) { |
221 | $extraFields[$field['id']] = $this->getFieldId($cachedData, $field); |
222 | } |
223 | |
224 | return $this->createExecution($cachedData, $extraFields); |
225 | } |
226 | |
227 | /** |
228 | * @param $cachedData |
229 | * @param $field |
230 | * @return string |
231 | * @throws common_exception_Error |
232 | * @throws common_ext_ExtensionException |
233 | */ |
234 | private function getFieldId($cachedData, $field) |
235 | { |
236 | $value = isset($cachedData[$field['id']]) |
237 | ? $this->sanitizeUserInput($cachedData[$field['id']]) |
238 | : ''; |
239 | if (\common_Utils::isUri($value)) { |
240 | $value = $this->getResource($value)->getLabel(); |
241 | } |
242 | return $value; |
243 | } |
244 | |
245 | /** |
246 | * @param array $cachedData |
247 | * @param $online |
248 | * @return float |
249 | */ |
250 | private function getApproximatedRemainingTime(array $cachedData, $online) |
251 | { |
252 | $now = microtime(true); |
253 | $remaining = $this->getRemainingTime($cachedData); |
254 | $elapsedApprox = 0; |
255 | $executionState = $cachedData[DeliveryMonitoringService::STATUS]; |
256 | |
257 | if ( |
258 | $executionState === DeliveryExecution::STATE_ACTIVE |
259 | && isset($cachedData[DeliveryMonitoringService::LAST_TEST_TAKER_ACTIVITY]) |
260 | ) { |
261 | $lastActivity = (float)$cachedData[DeliveryMonitoringService::LAST_TEST_TAKER_ACTIVITY]; |
262 | $elapsedApprox = $now - $lastActivity; |
263 | $duration = (float)($cachedData[DeliveryMonitoringService::ITEM_DURATION] ?? 0); |
264 | $duration -= (float)($cachedData[DeliveryMonitoringService::STORED_ITEM_DURATION] ?? 0); |
265 | $elapsedApprox += $duration; |
266 | } |
267 | |
268 | if (is_bool($online) && $online === false) { |
269 | $elapsedApprox = 0; |
270 | } |
271 | |
272 | return round((float)$remaining - $elapsedApprox); |
273 | } |
274 | |
275 | /** |
276 | * @param array $cachedData |
277 | * @return int |
278 | */ |
279 | private function getRemainingTime(array $cachedData) |
280 | { |
281 | return (int) ($cachedData[DeliveryMonitoringService::REMAINING_TIME] ?? 0); |
282 | } |
283 | |
284 | |
285 | /** |
286 | * Get array of user specific extra fields to be displayed in the monitoring data table |
287 | * |
288 | * @return array |
289 | * @throws common_ext_ExtensionException |
290 | */ |
291 | private function getUserExtraFields() |
292 | { |
293 | $proctoringExtension = $this->getExtensionManagerService()->getExtensionById('taoProctoring'); |
294 | $userExtraFields = $proctoringExtension->getConfig('monitoringUserExtraFields'); |
295 | if (empty($userExtraFields) || !is_array($userExtraFields)) { |
296 | return []; |
297 | } |
298 | |
299 | $userExtraFieldsSettings = $proctoringExtension->getConfig('monitoringUserExtraFieldsSettings'); |
300 | |
301 | $extraFields = []; |
302 | foreach ($userExtraFields as $name => $uri) { |
303 | $extraFields[] = $this->mergeExtraFieldsSettings($uri, $name, $userExtraFieldsSettings); |
304 | } |
305 | |
306 | return $extraFields; |
307 | } |
308 | |
309 | private function mergeExtraFieldsSettings($uri, $name, $userExtraFieldsSettings) |
310 | { |
311 | $property = $this->getProperty($uri); |
312 | $settings = array_key_exists($name, $userExtraFieldsSettings) |
313 | ? $userExtraFieldsSettings[$name] |
314 | : []; |
315 | |
316 | return array_merge([ |
317 | 'id' => $name, |
318 | 'property' => $property, |
319 | 'label' => __($property->getLabel()), |
320 | ], $settings); |
321 | } |
322 | |
323 | /** |
324 | * @param array $cachedData |
325 | * @return mixed|string |
326 | */ |
327 | private function getProgressString(array $cachedData) |
328 | { |
329 | $progressStr = $cachedData[DeliveryMonitoringService::CURRENT_ASSESSMENT_ITEM]; |
330 | $progress = json_decode($progressStr, true); |
331 | if ($progress === null) { |
332 | return $progressStr; |
333 | } |
334 | |
335 | if ( |
336 | in_array( |
337 | $cachedData[DeliveryMonitoringService::STATUS], |
338 | [DeliveryExecutionInterface::STATE_TERMINATED, DeliveryExecutionInterface::STATE_FINISHED], |
339 | true |
340 | ) |
341 | ) { |
342 | return $progress['title']; |
343 | } |
344 | $format = $this->getSessionStateService()->hasOption(SessionStateService::OPTION_STATE_FORMAT) |
345 | ? $this->getSessionStateService()->getOption(SessionStateService::OPTION_STATE_FORMAT) |
346 | : __('%s - item %p/%c'); |
347 | $map = array( |
348 | '%s' => $progress['title'] ?? '', |
349 | '%p' => $progress['itemPosition'] ?? '', |
350 | '%c' => $progress['itemCount'] ?? '' |
351 | ); |
352 | |
353 | return strtr($format, $map); |
354 | } |
355 | |
356 | /** |
357 | * @param array $cachedData |
358 | * @return bool|null |
359 | */ |
360 | private function isOnline(array $cachedData) |
361 | { |
362 | if ($this->getTestSessionConnectivityStatusService()->hasOnlineMode()) { |
363 | return $this |
364 | ->getTestSessionConnectivityStatusService() |
365 | ->isOnline($cachedData[DeliveryMonitoringService::DELIVERY_EXECUTION_ID]); |
366 | } |
367 | return null; |
368 | } |
369 | |
370 | /** |
371 | * @param array $cachedData |
372 | * @param bool|null $online |
373 | * @return float|null |
374 | */ |
375 | private function getLastActivity(array $cachedData, ?bool $online) |
376 | { |
377 | if ($online && isset($cachedData[DeliveryMonitoringService::LAST_TEST_TAKER_ACTIVITY])) { |
378 | return $cachedData[DeliveryMonitoringService::LAST_TEST_TAKER_ACTIVITY]; |
379 | } |
380 | |
381 | return null; |
382 | } |
383 | |
384 | /** |
385 | * @param $input |
386 | * @return string |
387 | * @throws common_exception_Error |
388 | * @throws common_ext_ExtensionException |
389 | */ |
390 | private function sanitizeUserInput($input) |
391 | { |
392 | return htmlentities($input, ENT_COMPAT, $this->getApplicationService()->getDefaultEncoding()); |
393 | } |
394 | |
395 | /** |
396 | * @return ApplicationService |
397 | */ |
398 | private function getApplicationService() |
399 | { |
400 | return $this->getServiceLocator()->get(ApplicationService::SERVICE_ID); |
401 | } |
402 | |
403 | /** |
404 | * @return TestSessionConnectivityStatusService |
405 | */ |
406 | private function getTestSessionConnectivityStatusService() |
407 | { |
408 | return $this->getServiceLocator()->get(TestSessionConnectivityStatusService::SERVICE_ID); |
409 | } |
410 | |
411 | /** |
412 | * @return SessionStateService |
413 | */ |
414 | private function getSessionStateService() |
415 | { |
416 | return $this->getServiceLocator()->get(SessionStateService::SERVICE_ID); |
417 | } |
418 | |
419 | /** |
420 | * @return common_ext_ExtensionsManager |
421 | */ |
422 | private function getExtensionManagerService() |
423 | { |
424 | return $this->getServiceLocator()->get(common_ext_ExtensionsManager::SERVICE_ID); |
425 | } |
426 | |
427 | /** |
428 | * @return DeliveryExecutionManagerService |
429 | */ |
430 | private function getDeliveryExecutionManagerService() |
431 | { |
432 | return $this->getServiceLocator()->get(DeliveryExecutionManagerService::SERVICE_ID); |
433 | } |
434 | |
435 | /** |
436 | * @param $cachedData |
437 | * @return array |
438 | */ |
439 | private function createState($cachedData): array |
440 | { |
441 | $progressStr = $this->getProgressString($cachedData); |
442 | |
443 | $state = [ |
444 | 'status' => $cachedData[DeliveryMonitoringService::STATUS], |
445 | 'progress' => __($progressStr) |
446 | ]; |
447 | return $state; |
448 | } |
449 | } |