| Code Coverage | ||||||||||
| Lines | Functions and Methods | Classes and Traits | ||||||||
| Total |  | 66.67% | 16 / 24 |  | 66.67% | 2 / 3 | CRAP |  | 0.00% | 0 / 1 | 
| DeliveryExecutionStateListener |  | 66.67% | 16 / 24 |  | 66.67% | 2 / 3 | 7.33 |  | 0.00% | 0 / 1 | 
| updateRemainingTime |  | 61.90% | 13 / 21 |  | 0.00% | 0 / 1 | 4.88 | |||
| getTestSession |  | 100.00% | 2 / 2 |  | 100.00% | 1 / 1 | 1 | |||
| getMonitoringService |  | 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) 2020 (original work) Open Assessment Technologies SA ; | 
| 19 | */ | 
| 20 | |
| 21 | declare(strict_types=1); | 
| 22 | |
| 23 | namespace oat\taoProctoring\model\listener; | 
| 24 | |
| 25 | use oat\oatbox\service\ConfigurableService; | 
| 26 | use oat\taoDelivery\models\classes\execution\event\DeliveryExecutionState; | 
| 27 | use oat\taoProctoring\model\execution\DeliveryExecution; | 
| 28 | use oat\taoProctoring\model\monitorCache\DeliveryMonitoringService; | 
| 29 | use oat\taoQtiTest\models\runner\session\TestSession; | 
| 30 | use oat\taoQtiTest\models\TestSessionService; | 
| 31 | use oat\taoTests\models\runner\time\TimePoint; | 
| 32 | |
| 33 | class DeliveryExecutionStateListener extends ConfigurableService | 
| 34 | { | 
| 35 | /** | 
| 36 | * When test session is paused by proctor remaining time is recalculated based on server timer to show | 
| 37 | * more precise value. After receiving pause confirmation from client remaining time will be recalculated | 
| 38 | * to show correct value. | 
| 39 | * | 
| 40 | * @param DeliveryExecutionState $event | 
| 41 | * @throws \common_Exception | 
| 42 | * @throws \common_exception_NotFound | 
| 43 | */ | 
| 44 | public function updateRemainingTime(DeliveryExecutionState $event): void | 
| 45 | { | 
| 46 | if ($event->getState() != DeliveryExecution::STATE_PAUSED) { | 
| 47 | return; | 
| 48 | } | 
| 49 | $deliveryExecution = $event->getDeliveryExecution(); | 
| 50 | $executionId = $deliveryExecution->getIdentifier(); | 
| 51 | |
| 52 | $monitoringService = $this->getMonitoringService(); | 
| 53 | $data = $monitoringService->getData($deliveryExecution); | 
| 54 | $testSession = $this->getTestSession($deliveryExecution); | 
| 55 | if (empty($testSession)) { | 
| 56 | $this->logWarning( | 
| 57 | 'monitor cache for delivery ' . $executionId | 
| 58 | . ' could not be updated. Test session could not be retrieved' | 
| 59 | ); | 
| 60 | |
| 61 | return; | 
| 62 | } | 
| 63 | $testSession->setTimerTarget(TimePoint::TARGET_SERVER); | 
| 64 | $data->setTestSession($testSession); | 
| 65 | $data->updateData([DeliveryMonitoringService::REMAINING_TIME]); | 
| 66 | |
| 67 | $success = $monitoringService->save($data); | 
| 68 | if (!$success) { | 
| 69 | $this->logError( | 
| 70 | 'Monitor cache for delivery ' . $executionId . ' could not be updated. Remaining time was not updated' | 
| 71 | ); | 
| 72 | } | 
| 73 | } | 
| 74 | |
| 75 | /** | 
| 76 | * @param $deliveryExecution | 
| 77 | * @return null|TestSession | 
| 78 | * @throws \common_Exception | 
| 79 | */ | 
| 80 | private function getTestSession($deliveryExecution): ?TestSession | 
| 81 | { | 
| 82 | /** @var TestSessionService $testSessionService */ | 
| 83 | $testSessionService = $this->getServiceLocator()->get(TestSessionService::SERVICE_ID); | 
| 84 | |
| 85 | return $testSessionService->getTestSession($deliveryExecution, true); | 
| 86 | } | 
| 87 | |
| 88 | /** | 
| 89 | * @return DeliveryMonitoringService | 
| 90 | */ | 
| 91 | private function getMonitoringService(): DeliveryMonitoringService | 
| 92 | { | 
| 93 | return $this->getServiceLocator()->get(DeliveryMonitoringService::SERVICE_ID); | 
| 94 | } | 
| 95 | } |