Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 193 |
|
0.00% |
0 / 22 |
CRAP | |
0.00% |
0 / 1 |
| DeliveryHelper | |
0.00% |
0 / 193 |
|
0.00% |
0 / 22 |
3906 | |
0.00% |
0 / 1 |
| getDeliveryExecutionManagerService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| createErrorMessage | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| buildDeliveryData | |
0.00% |
0 / 38 |
|
0.00% |
0 / 1 |
90 | |||
| getCurrentDeliveryExecutions | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| authoriseExecutions | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
| terminateExecutions | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
| reactivateExecution | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
| pauseExecutions | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
30 | |||
| reportExecutions | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
| getDeliveryTimer | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setExtraTime | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDeliveryExecutionById | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| buildDeliveryExecutionData | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| adjustColumnName | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| adjustDeliveryExecutions | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| _getUserExtraFields | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
42 | |||
| getExtraFields | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| getExtraFieldsProperties | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| testStateChanged | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| getHasBeenPaused | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| setHasBeenPaused | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getAllReasonsCategories | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| 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-2022 (original work) Open Assessment Technologies SA ; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoProctoring\helpers; |
| 23 | |
| 24 | use core_kernel_classes_Resource; |
| 25 | use oat\oatbox\service\ServiceManager; |
| 26 | use oat\taoDelivery\model\execution\DeliveryExecution as DeliveryExecutionInterface; |
| 27 | use oat\taoDeliveryRdf\model\DeliveryAssemblyService; |
| 28 | use oat\taoProctoring\model\DeliveryExecutionStateService; |
| 29 | use oat\taoProctoring\model\execution\DeliveryExecution; |
| 30 | use oat\taoProctoring\model\execution\DeliveryExecutionListInterface; |
| 31 | use oat\taoProctoring\model\execution\DeliveryExecutionManagerService; |
| 32 | use oat\taoProctoring\model\execution\DeliveryExecutionList; |
| 33 | use oat\taoProctoring\model\monitorCache\DeliveryMonitoringService; |
| 34 | use oat\taoProctoring\model\ReasonCategoryService; |
| 35 | use oat\taoQtiTest\models\event\QtiTestStateChangeEvent; |
| 36 | use oat\taoQtiTest\models\runner\time\QtiTimer; |
| 37 | use qtism\runtime\tests\AssessmentTestSessionState; |
| 38 | use tao_helpers_Date as DateHelper; |
| 39 | |
| 40 | /** |
| 41 | * @deprecated please use configurable service oat\taoProctoring\model\execution\DeliveryHelperService |
| 42 | * |
| 43 | * This temporary helpers is a temporary way to return data to the controller. |
| 44 | * This helps isolating the mock code from the real controller one. |
| 45 | * It will be replaced by a real service afterward. |
| 46 | */ |
| 47 | class DeliveryHelper |
| 48 | { |
| 49 | /** |
| 50 | * Cached value for prepopulated fields |
| 51 | * @var array |
| 52 | */ |
| 53 | private static $extraFields = []; |
| 54 | |
| 55 | /** |
| 56 | * Translation map to convert frontend data column into database column |
| 57 | * @var array |
| 58 | */ |
| 59 | private static $columnsMap = [ |
| 60 | 'delivery' => DeliveryMonitoringService::DELIVERY_NAME, |
| 61 | 'deliveryLabel' => DeliveryMonitoringService::DELIVERY_NAME, |
| 62 | ]; |
| 63 | |
| 64 | /** |
| 65 | * @return \oat\oatbox\service\ConfigurableService |
| 66 | */ |
| 67 | private static function getDeliveryExecutionManagerService() |
| 68 | { |
| 69 | return ServiceManager::getServiceManager()->get(DeliveryExecutionManagerService::SERVICE_ID); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Creates a standard error message with different actions |
| 74 | * @param {DeliveryExecution} $deliveryExecution |
| 75 | * @param {String} $action |
| 76 | * @return string |
| 77 | */ |
| 78 | private static function createErrorMessage($deliveryExecution, $action) |
| 79 | { |
| 80 | if ($deliveryExecution->getState()->getUri() === DeliveryExecution::STATE_FINISHED) { |
| 81 | // phpcs:disable Generic.Files.LineLength |
| 82 | $errorMsg = __('%s could not be %s because it is finished. Please refresh your data.', $deliveryExecution->getLabel(), $action); |
| 83 | // phpcs:enable Generic.Files.LineLength |
| 84 | } elseif ($deliveryExecution->getState()->getUri() === DeliveryExecution::STATE_TERMINATED) { |
| 85 | // phpcs:disable Generic.Files.LineLength |
| 86 | $errorMsg = __('%s could not be %s because it is terminated. Please refresh your data.', $deliveryExecution->getLabel(), $action); |
| 87 | // phpcs:enable Generic.Files.LineLength |
| 88 | } else { |
| 89 | $errorMsg = __('%s could not be %s.', $deliveryExecution->getLabel(), $action); |
| 90 | } |
| 91 | |
| 92 | return $errorMsg; |
| 93 | } |
| 94 | |
| 95 | public static function buildDeliveryData($delivery, $executions) |
| 96 | { |
| 97 | $inprogress = 0; |
| 98 | $paused = 0; |
| 99 | $awaiting = 0; |
| 100 | foreach ($executions as $executionData) { |
| 101 | $executionState = $executionData[DeliveryMonitoringService::STATUS]; |
| 102 | switch ($executionState) { |
| 103 | case DeliveryExecution::STATE_AWAITING: |
| 104 | $awaiting++; |
| 105 | break; |
| 106 | case DeliveryExecution::STATE_ACTIVE: |
| 107 | $inprogress++; |
| 108 | break; |
| 109 | case DeliveryExecution::STATE_PAUSED: |
| 110 | $paused++; |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | $deliveryProps = array( |
| 116 | new \core_kernel_classes_Property(DeliveryAssemblyService::PROPERTY_START), |
| 117 | new \core_kernel_classes_Property(DeliveryAssemblyService::PROPERTY_END), |
| 118 | ); |
| 119 | $deliveryProperties = $delivery->getPropertiesValues($deliveryProps); |
| 120 | $propStartExec = current($deliveryProperties[DeliveryAssemblyService::PROPERTY_START]); |
| 121 | $propEndExec = current($deliveryProperties[DeliveryAssemblyService::PROPERTY_END]); |
| 122 | |
| 123 | $properties = array(); |
| 124 | if (!is_null($propStartExec) && !empty((string)$propStartExec)) { |
| 125 | $properties['periodStart'] = DateHelper::displayeDate((string)$propStartExec); |
| 126 | } |
| 127 | if (!is_null($propStartExec) && !empty((string)$propEndExec)) { |
| 128 | $properties['periodEnd'] = DateHelper::displayeDate((string)$propEndExec); |
| 129 | } |
| 130 | |
| 131 | $entry = array( |
| 132 | 'id' => $delivery->getUri(), |
| 133 | 'url' => _url('monitoring', 'Delivery', null, array('delivery' => $delivery->getUri())), |
| 134 | 'label' => $delivery->getLabel(), |
| 135 | 'stats' => array( |
| 136 | 'awaitingApproval' => $awaiting, |
| 137 | 'inProgress' => $inprogress, |
| 138 | 'paused' => $paused |
| 139 | ), |
| 140 | 'properties' => $properties |
| 141 | ); |
| 142 | |
| 143 | return $entry; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Gets the aggregated data for a filtered set of delivery executions of a given delivery |
| 148 | * This is performance critical, would need to find a way to optimize to obtain such information |
| 149 | * |
| 150 | * @param core_kernel_classes_Resource $delivery |
| 151 | * @param core_kernel_classes_Resource $testCenter |
| 152 | * @param array $options |
| 153 | * @return array |
| 154 | */ |
| 155 | public static function getCurrentDeliveryExecutions( |
| 156 | core_kernel_classes_Resource $delivery, |
| 157 | core_kernel_classes_Resource $testCenter, |
| 158 | array $options = [] |
| 159 | ) { |
| 160 | $deliveryService = ServiceManager::getServiceManager()->get(DeliveryMonitoringService::SERVICE_ID); |
| 161 | |
| 162 | return self::adjustDeliveryExecutions( |
| 163 | $deliveryService->getCurrentDeliveryExecutions($delivery, $testCenter, $options), |
| 164 | $options |
| 165 | ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Authorises a list of delivery executions |
| 170 | * |
| 171 | * @param array $deliveryExecutions |
| 172 | * @param array $reason |
| 173 | * @param string $testCenter Test center uri |
| 174 | * @return array |
| 175 | * @throws \oat\oatbox\service\ServiceNotFoundException |
| 176 | */ |
| 177 | public static function authoriseExecutions($deliveryExecutions, $reason = null, $testCenter = null) |
| 178 | { |
| 179 | /** @var DeliveryExecutionStateService $deliveryExecutionStateService */ |
| 180 | $deliveryExecutionStateService = ServiceManager::getServiceManager()->get( |
| 181 | DeliveryExecutionStateService::SERVICE_ID |
| 182 | ); |
| 183 | |
| 184 | $result = [ 'processed' => [], 'unprocessed' => [] ]; |
| 185 | foreach ($deliveryExecutions as $deliveryExecution) { |
| 186 | if (is_string($deliveryExecution)) { |
| 187 | $deliveryExecution = self::getDeliveryExecutionById($deliveryExecution); |
| 188 | } |
| 189 | |
| 190 | if ($deliveryExecutionStateService->authoriseExecution($deliveryExecution, $reason, $testCenter)) { |
| 191 | $result['processed'][$deliveryExecution->getIdentifier()] = true; |
| 192 | } else { |
| 193 | $result['unprocessed'][$deliveryExecution->getIdentifier()] = self::createErrorMessage( |
| 194 | $deliveryExecution, |
| 195 | __('authorized') |
| 196 | ); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return $result; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Terminates a list of delivery executions |
| 205 | * |
| 206 | * @param array $deliveryExecutions |
| 207 | * @param array $reason |
| 208 | * @return array |
| 209 | * @throws \oat\oatbox\service\ServiceNotFoundException |
| 210 | */ |
| 211 | public static function terminateExecutions($deliveryExecutions, $reason = null) |
| 212 | { |
| 213 | /** @var DeliveryExecutionStateService $deliveryExecutionStateService */ |
| 214 | $deliveryExecutionStateService = ServiceManager::getServiceManager()->get( |
| 215 | DeliveryExecutionStateService::SERVICE_ID |
| 216 | ); |
| 217 | |
| 218 | $result = [ 'processed' => [], 'unprocessed' => [] ]; |
| 219 | foreach ($deliveryExecutions as $deliveryExecution) { |
| 220 | if (is_string($deliveryExecution)) { |
| 221 | $deliveryExecution = self::getDeliveryExecutionById($deliveryExecution); |
| 222 | } |
| 223 | |
| 224 | if ($deliveryExecutionStateService->terminateExecution($deliveryExecution, $reason)) { |
| 225 | $result['processed'][$deliveryExecution->getIdentifier()] = true; |
| 226 | } else { |
| 227 | $result['unprocessed'][$deliveryExecution->getIdentifier()] = self::createErrorMessage( |
| 228 | $deliveryExecution, |
| 229 | __('terminated') |
| 230 | ); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | return $result; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * @param array $deliveryExecutions |
| 239 | * @param null $reason |
| 240 | * @return array |
| 241 | */ |
| 242 | public static function reactivateExecution($deliveryExecutions, $reason = null) |
| 243 | { |
| 244 | /** @var DeliveryExecutionStateService $deliveryExecutionStateService */ |
| 245 | $deliveryExecutionStateService = ServiceManager::getServiceManager()->get( |
| 246 | DeliveryExecutionStateService::SERVICE_ID |
| 247 | ); |
| 248 | |
| 249 | $result = [ 'processed' => [], 'unprocessed' => [] ]; |
| 250 | foreach ($deliveryExecutions as $deliveryExecution) { |
| 251 | if (is_string($deliveryExecution)) { |
| 252 | $deliveryExecution = self::getDeliveryExecutionById($deliveryExecution); |
| 253 | } |
| 254 | |
| 255 | if ($deliveryExecutionStateService->reactivateExecution($deliveryExecution, $reason)) { |
| 256 | $result['processed'][$deliveryExecution->getIdentifier()] = true; |
| 257 | } else { |
| 258 | $result['unprocessed'][$deliveryExecution->getIdentifier()] = self::createErrorMessage( |
| 259 | $deliveryExecution, |
| 260 | __('terminated') |
| 261 | ); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | return $result; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Pauses a list of delivery executions |
| 270 | * |
| 271 | * @param array $deliveryExecutions |
| 272 | * @param array $reason |
| 273 | * @return array |
| 274 | * @throws \oat\oatbox\service\ServiceNotFoundException |
| 275 | */ |
| 276 | public static function pauseExecutions($deliveryExecutions, $reason = null) |
| 277 | { |
| 278 | /** @var DeliveryExecutionStateService $deliveryExecutionStateService */ |
| 279 | $deliveryExecutionStateService = ServiceManager::getServiceManager()->get( |
| 280 | DeliveryExecutionStateService::SERVICE_ID |
| 281 | ); |
| 282 | |
| 283 | $result = [ 'processed' => [], 'unprocessed' => [] ]; |
| 284 | foreach ($deliveryExecutions as $deliveryExecution) { |
| 285 | if (is_string($deliveryExecution)) { |
| 286 | $deliveryExecution = self::getDeliveryExecutionById($deliveryExecution); |
| 287 | } |
| 288 | |
| 289 | try { |
| 290 | $isPaused = $deliveryExecutionStateService->pauseExecution($deliveryExecution, $reason); |
| 291 | if ($isPaused) { |
| 292 | $result['processed'][$deliveryExecution->getIdentifier()] = true; |
| 293 | } else { |
| 294 | $result['unprocessed'][$deliveryExecution->getIdentifier()] = self::createErrorMessage( |
| 295 | $deliveryExecution, |
| 296 | __('paused') |
| 297 | ); |
| 298 | } |
| 299 | } catch (\Exception $exception) { |
| 300 | $result['unprocessed'][$deliveryExecution->getIdentifier()] = $exception->getMessage(); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | return $result; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Report irregularity to a list of delivery executions |
| 309 | * |
| 310 | * @param array $deliveryExecutions |
| 311 | * @param array $reason |
| 312 | * @return array |
| 313 | * @throws \oat\oatbox\service\ServiceNotFoundException |
| 314 | */ |
| 315 | public static function reportExecutions($deliveryExecutions, $reason = null) |
| 316 | { |
| 317 | $deliveryExecutionStateService = ServiceManager::getServiceManager()->get( |
| 318 | DeliveryExecutionStateService::SERVICE_ID |
| 319 | ); |
| 320 | |
| 321 | $result = [ 'processed' => [], 'unprocessed' => [] ]; |
| 322 | foreach ($deliveryExecutions as $deliveryExecution) { |
| 323 | if (is_string($deliveryExecution)) { |
| 324 | $deliveryExecution = self::getDeliveryExecutionById($deliveryExecution); |
| 325 | } |
| 326 | |
| 327 | if ($deliveryExecutionStateService->reportExecution($deliveryExecution, $reason)) { |
| 328 | $result['processed'][$deliveryExecution->getIdentifier()] = true; |
| 329 | } else { |
| 330 | $result['unprocessed'][$deliveryExecution->getIdentifier()] = self::createErrorMessage( |
| 331 | $deliveryExecution, |
| 332 | __('reported for irregularity') |
| 333 | ); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | return $result; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Gets the delivery time counter |
| 342 | * |
| 343 | * @param DeliveryExecutionInterface $deliveryExecution |
| 344 | * @return QtiTimer |
| 345 | * @throws \oat\oatbox\service\ServiceNotFoundException |
| 346 | */ |
| 347 | public static function getDeliveryTimer($deliveryExecution) |
| 348 | { |
| 349 | return self::getDeliveryExecutionManagerService()->getDeliveryTimer($deliveryExecution); |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Sets the extra time to a list of delivery executions |
| 354 | * |
| 355 | * @param array $deliveryExecutions |
| 356 | * @param float $extraTime |
| 357 | * @return array |
| 358 | * @throws \oat\oatbox\service\ServiceNotFoundException |
| 359 | */ |
| 360 | public static function setExtraTime($deliveryExecutions, $extraTime = null) |
| 361 | { |
| 362 | return self::getDeliveryExecutionManagerService()->setExtraTime($deliveryExecutions, $extraTime); |
| 363 | } |
| 364 | |
| 365 | public static function getDeliveryExecutionById($deliveryExecutionId) |
| 366 | { |
| 367 | return self::getDeliveryExecutionManagerService()->getDeliveryExecutionById($deliveryExecutionId); |
| 368 | } |
| 369 | |
| 370 | public static function buildDeliveryExecutionData($deliveryExecutions, $sortOptions = array()) |
| 371 | { |
| 372 | return self::adjustDeliveryExecutions($deliveryExecutions, $sortOptions); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Converts a frontend column name into database column name. |
| 377 | * Useful to translate the name of a column to sort. |
| 378 | * @param string $column |
| 379 | * @return string |
| 380 | */ |
| 381 | public static function adjustColumnName($column) |
| 382 | { |
| 383 | if (isset(self::$columnsMap[$column])) { |
| 384 | $column = self::$columnsMap[$column]; |
| 385 | } |
| 386 | return $column; |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * Adjusts a list of delivery executions: add information, format the result |
| 391 | * @param DeliveryExecution[] $deliveryExecutions |
| 392 | * @return array |
| 393 | * @internal param array $options |
| 394 | */ |
| 395 | private static function adjustDeliveryExecutions($deliveryExecutions) |
| 396 | { |
| 397 | return ServiceManager::getServiceManager()->get(DeliveryExecutionListInterface::class) |
| 398 | ->adjustDeliveryExecutions($deliveryExecutions); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Get array of user specific extra fields to be displayed in the monitoring data table |
| 403 | * |
| 404 | * @return array |
| 405 | * |
| 406 | * phpcs:disable PSR2.Methods.MethodDeclaration |
| 407 | */ |
| 408 | private static function _getUserExtraFields() |
| 409 | { |
| 410 | if (!self::$extraFields) { |
| 411 | $proctoringExtension = \common_ext_ExtensionsManager::singleton()->getExtensionById('taoProctoring'); |
| 412 | $userExtraFields = $proctoringExtension->getConfig('monitoringUserExtraFields'); |
| 413 | $userExtraFieldsSettings = $proctoringExtension->getConfig('monitoringUserExtraFieldsSettings'); |
| 414 | if (!empty($userExtraFields) && is_array($userExtraFields)) { |
| 415 | foreach ($userExtraFields as $name => $uri) { |
| 416 | $property = new \core_kernel_classes_Property($uri); |
| 417 | $settings = array_key_exists($name, $userExtraFieldsSettings) ? |
| 418 | $userExtraFieldsSettings[$name] : []; |
| 419 | self::$extraFields[] = array_merge(array( |
| 420 | 'id' => $name, |
| 421 | 'property' => $property, |
| 422 | 'label' => __($property->getLabel()), |
| 423 | ), $settings); |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | return self::$extraFields; |
| 429 | } |
| 430 | // phpcs:enable PSR2.Methods.MethodDeclaration |
| 431 | |
| 432 | /** |
| 433 | * Return array of extra fields to be displayed in the monitoring data table |
| 434 | * |
| 435 | * @return array |
| 436 | */ |
| 437 | public static function getExtraFields() |
| 438 | { |
| 439 | return array_map(function ($field) { |
| 440 | $extra = [ |
| 441 | 'id' => $field['id'], |
| 442 | 'label' => $field['label'], |
| 443 | 'filterable' => array_key_exists('filterable', $field) ? $field['filterable'] : false, |
| 444 | ]; |
| 445 | if (array_key_exists('columnPosition', $field)) { |
| 446 | $extra['columnPosition'] = $field['columnPosition']; |
| 447 | } |
| 448 | return $extra; |
| 449 | }, self::_getUserExtraFields()); |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Return array of extra fields to be saved in monitoring storage |
| 454 | * |
| 455 | * @return array |
| 456 | */ |
| 457 | public static function getExtraFieldsProperties() |
| 458 | { |
| 459 | return array_map(function ($field) { |
| 460 | return array( |
| 461 | 'id' => $field['id'], |
| 462 | 'property' => $field['property'] |
| 463 | ); |
| 464 | }, self::_getUserExtraFields()); |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Catch changing of session state |
| 469 | * @param QtiTestStateChangeEvent $event |
| 470 | */ |
| 471 | public static function testStateChanged(QtiTestStateChangeEvent $event) |
| 472 | { |
| 473 | if ( |
| 474 | $event->getPreviousState() !== AssessmentTestSessionState::INITIAL |
| 475 | && $event->getSession()->getState() === AssessmentTestSessionState::SUSPENDED |
| 476 | ) { |
| 477 | self::setHasBeenPaused($event->getSession()->getSessionId(), true); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * @param $deliveryExecution |
| 483 | * @return mixed |
| 484 | */ |
| 485 | public static function getHasBeenPaused($deliveryExecution) |
| 486 | { |
| 487 | if (is_string($deliveryExecution)) { |
| 488 | $deliveryExecution = self::getDeliveryExecutionById($deliveryExecution); |
| 489 | } |
| 490 | /** @var DeliveryMonitoringService $deliveryMonitoringService */ |
| 491 | $deliveryMonitoringService = ServiceManager::getServiceManager()->get(DeliveryMonitoringService::SERVICE_ID); |
| 492 | $data = $deliveryMonitoringService->getData($deliveryExecution); |
| 493 | $status = isset($data->get()['hasBeenPaused']) ? (bool) $data->get()['hasBeenPaused'] : false; |
| 494 | self::setHasBeenPaused($deliveryExecution, false); |
| 495 | return $status; |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * @param $deliveryExecution |
| 500 | * @param boolean $paused |
| 501 | * @throws \common_exception_NotFound |
| 502 | */ |
| 503 | public static function setHasBeenPaused($deliveryExecution, $paused) |
| 504 | { |
| 505 | if (is_string($deliveryExecution)) { |
| 506 | $deliveryExecution = self::getDeliveryExecutionById($deliveryExecution); |
| 507 | } |
| 508 | /** @var DeliveryMonitoringService $deliveryMonitoringService */ |
| 509 | $deliveryMonitoringService = ServiceManager::getServiceManager()->get(DeliveryMonitoringService::SERVICE_ID); |
| 510 | |
| 511 | $data = $deliveryMonitoringService->createMonitoringData($deliveryExecution); |
| 512 | |
| 513 | $data->update('hasBeenPaused', $paused); |
| 514 | $deliveryMonitoringService->partialSave($data); |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * Get the list of all available categories, sorted by action names |
| 519 | * |
| 520 | * @param bool $hasAccessToReactivate |
| 521 | * @return array |
| 522 | */ |
| 523 | public static function getAllReasonsCategories($hasAccessToReactivate = false) |
| 524 | { |
| 525 | /** @var ReasonCategoryService $categoryService */ |
| 526 | $categoryService = ServiceManager::getServiceManager()->get(ReasonCategoryService::SERVICE_ID); |
| 527 | |
| 528 | $response = array( |
| 529 | 'authorize' => array(), |
| 530 | 'pause' => $categoryService->getIrregularities(), |
| 531 | 'terminate' => $categoryService->getIrregularities(), |
| 532 | 'report' => $categoryService->getIrregularities(), |
| 533 | 'timeAdjustment' => $categoryService->getIrregularities(), |
| 534 | 'print' => [], |
| 535 | ); |
| 536 | if ($hasAccessToReactivate) { |
| 537 | $response['reactivate'] = $categoryService->getIrregularities(); |
| 538 | } |
| 539 | |
| 540 | return $response; |
| 541 | } |
| 542 | } |