Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
7.64% |
11 / 144 |
|
14.29% |
1 / 7 |
CRAP | |
0.00% |
0 / 1 |
| TestSessionService | |
7.64% |
11 / 144 |
|
14.29% |
1 / 7 |
1115.63 | |
0.00% |
0 / 1 |
| singleton | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isExpired | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
132 | |||
| getProgress | |
0.00% |
0 / 46 |
|
0.00% |
0 / 1 |
132 | |||
| getProgressText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getMappedItems | |
0.00% |
0 / 41 |
|
0.00% |
0 / 1 |
42 | |||
| getLastTestTakersEvent | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| getSmallestMaxTimeConstraint | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
| 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) 2016 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoProctoring\model\implementation; |
| 23 | |
| 24 | use DateInterval; |
| 25 | use DateTimeImmutable; |
| 26 | use oat\oatbox\service\ServiceManager; |
| 27 | use oat\taoDelivery\model\execution\DeliveryExecution; |
| 28 | use oat\taoProctoring\model\deliveryLog\DeliveryLog; |
| 29 | use oat\taoProctoring\model\execution\DeliveryExecution as DeliveryExecutionState; |
| 30 | use oat\taoQtiTest\models\cat\CatService; |
| 31 | use oat\taoQtiTest\models\runner\config\QtiRunnerConfig; |
| 32 | use oat\taoQtiTest\models\runner\session\TestSession; |
| 33 | use oat\taoQtiTest\models\runner\time\QtiTimeConstraint; |
| 34 | use oat\taoQtiTest\models\TestSessionService as QtiTestSessionService; |
| 35 | use qtism\common\datatypes\QtiDuration; |
| 36 | use qtism\runtime\tests\AssessmentTestSession; |
| 37 | |
| 38 | /** |
| 39 | * Interface TestSessionService |
| 40 | * @package oat\taoProctoring\model |
| 41 | * @author Aleh Hutnikau <hutnikau@1pt.com> |
| 42 | */ |
| 43 | class TestSessionService extends QtiTestSessionService |
| 44 | { |
| 45 | public const SERVICE_ID = 'taoProctoring/TestSessionService'; |
| 46 | |
| 47 | public static function singleton() |
| 48 | { |
| 49 | return ServiceManager::getServiceManager()->get(TestSessionService::SERVICE_ID); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Checks if delivery execution was expired after pausing or abandoned after authorization |
| 54 | * |
| 55 | * @param DeliveryExecution $deliveryExecution |
| 56 | * @return bool |
| 57 | */ |
| 58 | public function isExpired(DeliveryExecution $deliveryExecution) |
| 59 | { |
| 60 | if (!isset(self::$cache[$deliveryExecution->getIdentifier()]['expired'])) { |
| 61 | $executionState = $deliveryExecution->getState()->getUri(); |
| 62 | if ( |
| 63 | !in_array($executionState, [ |
| 64 | DeliveryExecutionState::STATE_PAUSED, |
| 65 | DeliveryExecutionState::STATE_ACTIVE, |
| 66 | DeliveryExecutionState::STATE_AWAITING, |
| 67 | DeliveryExecutionState::STATE_AUTHORIZED, |
| 68 | ]) || |
| 69 | !$lastTestTakersEvent = $this->getLastTestTakersEvent($deliveryExecution) |
| 70 | ) { |
| 71 | return self::$cache[$deliveryExecution->getIdentifier()]['expired'] = false; |
| 72 | } |
| 73 | |
| 74 | /** @var \oat\taoProctoring\model\implementation\DeliveryExecutionStateService $deliveryExecutionStateService */ |
| 75 | $deliveryExecutionStateService = $this->getServiceLocator()->get(DeliveryExecutionStateService::SERVICE_ID); |
| 76 | |
| 77 | if ( |
| 78 | ( |
| 79 | $executionState === DeliveryExecutionState::STATE_AUTHORIZED |
| 80 | || $executionState === DeliveryExecutionState::STATE_AWAITING |
| 81 | ) |
| 82 | && $deliveryExecutionStateService->isCancelable($deliveryExecution) |
| 83 | ) { |
| 84 | $delay = $deliveryExecutionStateService->getOption( |
| 85 | DeliveryExecutionStateService::OPTION_CANCELLATION_DELAY |
| 86 | ); |
| 87 | $startedTimestamp = \tao_helpers_Date::getTimeStamp($deliveryExecution->getStartTime(), true); |
| 88 | $started = (new DateTimeImmutable())->setTimestamp($startedTimestamp); |
| 89 | if ($started->add(new DateInterval($delay)) < (new DateTimeImmutable())) { |
| 90 | self::$cache[$deliveryExecution->getIdentifier()]['expired'] = true; |
| 91 | return self::$cache[$deliveryExecution->getIdentifier()]['expired']; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | $wasPausedAt = (new DateTimeImmutable())->setTimestamp($lastTestTakersEvent['created_at']); |
| 96 | if ( |
| 97 | $wasPausedAt |
| 98 | && $deliveryExecutionStateService->hasOption( |
| 99 | DeliveryExecutionStateService::OPTION_TERMINATION_DELAY_AFTER_PAUSE |
| 100 | ) |
| 101 | ) { |
| 102 | $delay = $deliveryExecutionStateService->getOption( |
| 103 | DeliveryExecutionStateService::OPTION_TERMINATION_DELAY_AFTER_PAUSE |
| 104 | ); |
| 105 | |
| 106 | if ($wasPausedAt->add(new DateInterval($delay)) < (new DateTimeImmutable())) { |
| 107 | self::$cache[$deliveryExecution->getIdentifier()]['expired'] = true; |
| 108 | |
| 109 | return self::$cache[$deliveryExecution->getIdentifier()]['expired']; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | self::$cache[$deliveryExecution->getIdentifier()]['expired'] = false; |
| 114 | } |
| 115 | |
| 116 | return self::$cache[$deliveryExecution->getIdentifier()]['expired']; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @param AssessmentTestSession $session |
| 121 | * @return null|string |
| 122 | */ |
| 123 | public function getProgress(AssessmentTestSession $session = null) |
| 124 | { |
| 125 | $result = null; |
| 126 | |
| 127 | $testConfig = $this->getServiceManager()->get(QtiRunnerConfig::SERVICE_ID); |
| 128 | $reviewConfig = $testConfig->getConfigValue('review'); |
| 129 | $displaySubsectionTitle = isset($reviewConfig['displaySubsectionTitle']) |
| 130 | ? (bool) $reviewConfig['displaySubsectionTitle'] |
| 131 | : true; |
| 132 | |
| 133 | if ($session !== null) { |
| 134 | if ($session->isRunning()) { |
| 135 | $route = $session->getRoute(); |
| 136 | $currentItem = $route->current(); |
| 137 | |
| 138 | $catService = $this->getServiceManager()->get(CatService::SERVICE_ID); |
| 139 | $isAdaptive = $catService->isAdaptive($session, $currentItem->getAssessmentItemRef()); |
| 140 | |
| 141 | if ($displaySubsectionTitle || $isAdaptive) { |
| 142 | $currentSection = $session->getCurrentAssessmentSection(); |
| 143 | if ($isAdaptive) { |
| 144 | $testSessionData = $this->getTestSessionDataById($session->getSessionId()); |
| 145 | $sectionItems = $catService->getShadowTest( |
| 146 | $session, |
| 147 | $testSessionData['compilation']['private'], |
| 148 | $currentItem |
| 149 | ); |
| 150 | $currentItem = $catService->getCurrentCatItemId( |
| 151 | $session, |
| 152 | $testSessionData['compilation']['private'], |
| 153 | $currentItem |
| 154 | ); |
| 155 | } else { |
| 156 | $sectionItems = $route->getRouteItemsByAssessmentSection($currentSection)->getArrayCopy(true); |
| 157 | } |
| 158 | $positionInSection = array_search($currentItem, $sectionItems); |
| 159 | |
| 160 | $result = $this->getProgressText( |
| 161 | $currentSection->getTitle(), |
| 162 | $positionInSection, |
| 163 | count($sectionItems) |
| 164 | ); |
| 165 | } else { |
| 166 | // we need only top section and items from there |
| 167 | $parts = $this->getMappedItems($session); |
| 168 | foreach ($parts as $part) { |
| 169 | foreach ($part['sections'] as $section) { |
| 170 | foreach ($section['items'] as $key => $item) { |
| 171 | if ($currentItem->getAssessmentItemRef()->getIdentifier() == $key) { |
| 172 | $result = $this->getProgressText( |
| 173 | $section['label'], |
| 174 | $item['positionInSection'], |
| 175 | count($section['items']) |
| 176 | ); |
| 177 | |
| 178 | break 3; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } else { |
| 185 | $result = __('finished'); |
| 186 | } |
| 187 | } |
| 188 | return $result; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * @param string $sectionTitle |
| 193 | * @param int $positionInSection |
| 194 | * @param int $sectionCount |
| 195 | * @return string |
| 196 | */ |
| 197 | private function getProgressText($sectionTitle, $positionInSection, $sectionCount) |
| 198 | { |
| 199 | return __('%1$s - Item %2$s/%3$s', $sectionTitle, $positionInSection + 1, $sectionCount); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Load all items as there should be viewed |
| 204 | * @param $session |
| 205 | * @return array |
| 206 | */ |
| 207 | private function getMappedItems($session) |
| 208 | { |
| 209 | $parts = []; |
| 210 | $route = $session->getRoute(); |
| 211 | $routeItems = $route->getAllRouteItems(); |
| 212 | $offset = $route->getRouteItemPosition($routeItems[0]); |
| 213 | $offsetPart = 0; |
| 214 | $offsetSection = 0; |
| 215 | $lastPart = null; |
| 216 | $lastSection = null; |
| 217 | foreach ($routeItems as $routeItem) { |
| 218 | $sections = $routeItem->getAssessmentSections()->getArrayCopy(); |
| 219 | $section = $sections[0]; |
| 220 | $sectionId = $section->getIdentifier(); |
| 221 | $testPart = $routeItem->getTestPart(); |
| 222 | $partId = $testPart->getIdentifier(); |
| 223 | $itemRef = $routeItem->getAssessmentItemRef(); |
| 224 | $itemId = $itemRef->getIdentifier(); |
| 225 | |
| 226 | if ($lastPart != $partId) { |
| 227 | $offsetPart = 0; |
| 228 | $lastPart = $partId; |
| 229 | } |
| 230 | if ($lastSection != $sectionId) { |
| 231 | $offsetSection = 0; |
| 232 | $lastSection = $sectionId; |
| 233 | } |
| 234 | |
| 235 | if (!isset($parts[$partId])) { |
| 236 | $parts[$partId] = [ |
| 237 | 'label' => $partId, |
| 238 | 'sections' => [] |
| 239 | ]; |
| 240 | } |
| 241 | if (!isset($parts[$partId]['sections'][$sectionId])) { |
| 242 | $parts[$partId]['sections'][$sectionId] = [ |
| 243 | 'label' => $section->getTitle(), |
| 244 | 'items' => [] |
| 245 | ]; |
| 246 | } |
| 247 | |
| 248 | $parts[$partId]['sections'][$sectionId]['items'][$itemId] = [ |
| 249 | 'positionInSection' => $offsetSection, |
| 250 | 'sectionLabel' => $section->getTitle(), |
| 251 | 'partLabel' => $partId |
| 252 | ]; |
| 253 | |
| 254 | $offset++; |
| 255 | $offsetSection++; |
| 256 | $offsetPart++; |
| 257 | } |
| 258 | |
| 259 | return $parts; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Get last test takers event from delivery log |
| 264 | * @param DeliveryExecution $deliveryExecution |
| 265 | * @return array|null |
| 266 | */ |
| 267 | protected function getLastTestTakersEvent(DeliveryExecution $deliveryExecution) |
| 268 | { |
| 269 | $deliveryLogService = $this->getServiceLocator()->get(DeliveryLog::SERVICE_ID); |
| 270 | $testTakerIdentifier = $deliveryExecution->getUserIdentifier(); |
| 271 | $events = array_reverse($deliveryLogService->get($deliveryExecution->getIdentifier())); |
| 272 | |
| 273 | $lastTestTakersEvent = null; |
| 274 | foreach ($events as $event) { |
| 275 | if ($event[DeliveryLog::CREATED_BY] === $testTakerIdentifier) { |
| 276 | $lastTestTakersEvent = $event; |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | return $lastTestTakersEvent; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Returns current/smallest max time constraint. |
| 286 | * |
| 287 | * @param TestSession $testSession |
| 288 | * @return QtiTimeConstraint|null |
| 289 | */ |
| 290 | public function getSmallestMaxTimeConstraint(TestSession $testSession): ?QtiTimeConstraint |
| 291 | { |
| 292 | $constraints = $testSession->getTimeConstraints(); |
| 293 | $smallestTimeConstraint = null; |
| 294 | $remainingTime = PHP_INT_MAX; |
| 295 | /** @var QtiTimeConstraint $constraint */ |
| 296 | foreach ($constraints as $constraint) { |
| 297 | /** @var QtiDuration $constraintRemainingDuration */ |
| 298 | $constraintRemainingDuration = $constraint->getMaximumRemainingTime(); |
| 299 | if ($constraintRemainingDuration === false) { |
| 300 | continue; |
| 301 | } |
| 302 | |
| 303 | if (($constraintRemainingTime = $constraintRemainingDuration->getSeconds(true)) < $remainingTime) { |
| 304 | $smallestTimeConstraint = $constraint; |
| 305 | $remainingTime = $constraintRemainingTime; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | return $smallestTimeConstraint; |
| 310 | } |
| 311 | } |