Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
46.15% |
36 / 78 |
|
21.43% |
3 / 14 |
CRAP | |
0.00% |
0 / 1 |
| QtiRunnerNavigation | |
46.15% |
36 / 78 |
|
21.43% |
3 / 14 |
289.80 | |
0.00% |
0 / 1 |
| setDeliveryExecutionServiceProxy | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setLogger | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getNavigator | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
3.21 | |||
| move | |
36.84% |
7 / 19 |
|
0.00% |
0 / 1 |
19.34 | |||
| checkTimedSectionExit | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| endItemSessions | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| jumpsOutOfSection | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| isTimeLimited | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| isAdaptive | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| isSessionSuspended | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| isExecutionPaused | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
3.01 | |||
| getDeliveryExecutionService | |
40.00% |
2 / 5 |
|
0.00% |
0 / 1 |
4.94 | |||
| getLogger | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| getEventManager | |
0.00% |
0 / 1 |
|
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) 2016-2023 (original work) Open Assessment Technologies SA. |
| 19 | */ |
| 20 | |
| 21 | namespace oat\taoQtiTest\models\runner\navigation; |
| 22 | |
| 23 | use common_Exception; |
| 24 | use common_exception_InconsistentData; |
| 25 | use common_exception_InvalidArgumentType; |
| 26 | use common_exception_NotImplemented; |
| 27 | use common_Logger; |
| 28 | use oat\taoDelivery\model\execution\DeliveryExecutionInterface; |
| 29 | use oat\taoDelivery\model\execution\ServiceProxy as TaoDeliveryServiceProxy; |
| 30 | use oat\taoQtiTest\models\event\QtiMoveEvent; |
| 31 | use oat\taoQtiTest\models\runner\QtiRunnerPausedException; |
| 32 | use oat\taoQtiTest\models\runner\RunnerServiceContext; |
| 33 | use oat\taoQtiTest\models\runner\QtiRunnerServiceContext; |
| 34 | use qtism\data\AssessmentSection; |
| 35 | use oat\oatbox\service\ServiceManager; |
| 36 | use oat\oatbox\event\EventManager; |
| 37 | use qtism\runtime\tests\AssessmentTestSession; |
| 38 | use qtism\runtime\tests\AssessmentTestSessionState; |
| 39 | use qtism\runtime\tests\Route; |
| 40 | |
| 41 | /** |
| 42 | * Class QtiRunnerNavigation |
| 43 | * @package oat\taoQtiTest\models\runner\navigation |
| 44 | * |
| 45 | * @author Jean-Sébastien Conan <jean-sebastien.conan@vesperiagroup.com> |
| 46 | */ |
| 47 | class QtiRunnerNavigation |
| 48 | { |
| 49 | private static ?TaoDeliveryServiceProxy $deliveryExecutionServiceProxy; |
| 50 | |
| 51 | private static ?common_Logger $logger; |
| 52 | |
| 53 | public static function setDeliveryExecutionServiceProxy( |
| 54 | ?TaoDeliveryServiceProxy $serviceProxy |
| 55 | ): void { |
| 56 | self::$deliveryExecutionServiceProxy = $serviceProxy; |
| 57 | } |
| 58 | |
| 59 | public static function setLogger(?common_Logger $logger) |
| 60 | { |
| 61 | self::$logger = $logger; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Gets a QTI runner navigator. |
| 66 | * |
| 67 | * @param string $direction |
| 68 | * @param string $scope |
| 69 | * @return RunnerNavigation |
| 70 | * @throws common_exception_InconsistentData |
| 71 | * @throws common_exception_NotImplemented |
| 72 | */ |
| 73 | public static function getNavigator($direction, $scope) |
| 74 | { |
| 75 | $className = __NAMESPACE__ . '\QtiRunnerNavigation' . ucfirst($direction) . ucfirst($scope); |
| 76 | if (class_exists($className)) { |
| 77 | $navigator = new $className(); |
| 78 | if ($navigator instanceof RunnerNavigation) { |
| 79 | return $navigator; |
| 80 | } |
| 81 | |
| 82 | throw new common_exception_InconsistentData('Navigator must be an instance of RunnerNavigation'); |
| 83 | } |
| 84 | |
| 85 | throw new common_exception_NotImplemented('The action is invalid!'); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @param string $direction |
| 90 | * @param string $scope |
| 91 | * @param RunnerServiceContext $context |
| 92 | * @param integer $ref |
| 93 | * @return boolean |
| 94 | * @throws QtiRunnerPausedException |
| 95 | * @throws common_Exception |
| 96 | * @throws common_exception_InvalidArgumentType |
| 97 | * @throws common_exception_InconsistentData |
| 98 | * @throws common_exception_NotImplemented |
| 99 | */ |
| 100 | public static function move($direction, $scope, RunnerServiceContext $context, $ref) |
| 101 | { |
| 102 | /* @var ?AssessmentTestSession $session */ |
| 103 | $session = $context->getTestSession(); |
| 104 | $navigator = self::getNavigator($direction, $scope); |
| 105 | |
| 106 | if (self::isSessionSuspended($context) || self::isExecutionPaused($context)) { |
| 107 | self::getLogger()->logDebug( |
| 108 | self::class . '::move session is suspended or execution paused' |
| 109 | ); |
| 110 | |
| 111 | throw new QtiRunnerPausedException(); |
| 112 | } |
| 113 | |
| 114 | if ($context instanceof QtiRunnerServiceContext) { |
| 115 | $from = $session->isRunning() ? $session->getRoute()->current() : null; |
| 116 | |
| 117 | self::getEventManager()->trigger( |
| 118 | new QtiMoveEvent(QtiMoveEvent::CONTEXT_BEFORE, $session, $from) |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | $result = $navigator->move($context, $ref); |
| 123 | |
| 124 | if ($context instanceof QtiRunnerServiceContext) { |
| 125 | $to = $session->isRunning() ? $session->getRoute()->current() : null; |
| 126 | |
| 127 | self::getEventManager()->trigger( |
| 128 | new QtiMoveEvent(QtiMoveEvent::CONTEXT_AFTER, $session, $from, $to) |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | return $result; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Check if a timed section is exited |
| 137 | * @param RunnerServiceContext $context |
| 138 | * @param int $nextPosition |
| 139 | */ |
| 140 | public static function checkTimedSectionExit(RunnerServiceContext $context, $nextPosition): void |
| 141 | { |
| 142 | $timerConfig = $context->getTestConfig()->getConfigValue('timer'); |
| 143 | |
| 144 | if (empty($timerConfig['keepUpToTimeout'])) { |
| 145 | /* @var AssessmentTestSession $session */ |
| 146 | $session = $context->getTestSession(); |
| 147 | $route = $session->getRoute(); |
| 148 | $section = $session->getCurrentAssessmentSection(); |
| 149 | |
| 150 | // As we have only one identifier for the whole adaptive section |
| 151 | // it will consider a jump of section on the first item |
| 152 | if ( |
| 153 | !self::isAdaptive($context) |
| 154 | && self::jumpsOutOfSection($route, $section, $nextPosition) |
| 155 | && self::isTimeLimited($section) |
| 156 | ) { |
| 157 | self::endItemSessions($session, $section); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | private static function endItemSessions( |
| 163 | AssessmentTestSession $session, |
| 164 | AssessmentSection $section |
| 165 | ): void { |
| 166 | foreach ($section->getComponentsByClassName('assessmentItemRef') as $assessmentItemRef) { |
| 167 | $itemSessions = $session->getAssessmentItemSessions($assessmentItemRef->getIdentifier()); |
| 168 | |
| 169 | if ($itemSessions !== false) { |
| 170 | foreach ($itemSessions as $itemSession) { |
| 171 | $itemSession->endItemSession(); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | private static function jumpsOutOfSection( |
| 178 | Route $route, |
| 179 | AssessmentSection $section, |
| 180 | $nextPosition |
| 181 | ): bool { |
| 182 | if ($nextPosition >= 0 && $nextPosition < $route->count()) { |
| 183 | $nextSection = $route->getRouteItemAt($nextPosition); |
| 184 | $nextSectionId = $nextSection->getAssessmentSection()->getIdentifier(); |
| 185 | |
| 186 | return $section->getIdentifier() !== $nextSectionId; |
| 187 | } |
| 188 | |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | private static function isTimeLimited(AssessmentSection $section): bool |
| 193 | { |
| 194 | $limits = $section->getTimeLimits(); |
| 195 | |
| 196 | return $limits != null && $limits->hasMaxTime(); |
| 197 | } |
| 198 | |
| 199 | private static function isAdaptive(RunnerServiceContext $context): bool |
| 200 | { |
| 201 | return $context instanceof QtiRunnerServiceContext && $context->isAdaptive(); |
| 202 | } |
| 203 | |
| 204 | private static function isSessionSuspended(RunnerServiceContext $context): bool |
| 205 | { |
| 206 | $session = $context->getTestSession(); |
| 207 | |
| 208 | if ($session instanceof AssessmentTestSession) { |
| 209 | if ($session->getState() === AssessmentTestSessionState::SUSPENDED) { |
| 210 | self::getLogger()->logDebug( |
| 211 | sprintf('%s DeliveryExecution is suspended', self::class) |
| 212 | ); |
| 213 | |
| 214 | return true; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | private static function isExecutionPaused(RunnerServiceContext $context): bool |
| 222 | { |
| 223 | $executionService = self::getDeliveryExecutionService(); |
| 224 | |
| 225 | if ($executionService !== null) { |
| 226 | $execution = $executionService->getDeliveryExecution( |
| 227 | $context->getTestExecutionUri() |
| 228 | ); |
| 229 | |
| 230 | if ($execution->getState()->getUri() === DeliveryExecutionInterface::STATE_PAUSED) { |
| 231 | self::getLogger()->logDebug( |
| 232 | sprintf('%s DeliveryExecution is Paused', self::class) |
| 233 | ); |
| 234 | |
| 235 | return true; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | private static function getDeliveryExecutionService(): ?TaoDeliveryServiceProxy |
| 243 | { |
| 244 | if (isset(self::$deliveryExecutionServiceProxy)) { |
| 245 | return self::$deliveryExecutionServiceProxy; |
| 246 | } |
| 247 | |
| 248 | return class_exists(TaoDeliveryServiceProxy::class) |
| 249 | ? TaoDeliveryServiceProxy::singleton() |
| 250 | : null; |
| 251 | } |
| 252 | |
| 253 | private static function getLogger(): common_Logger |
| 254 | { |
| 255 | if (!self::$logger instanceof common_Logger) { |
| 256 | self::$logger = common_Logger::singleton(); |
| 257 | } |
| 258 | |
| 259 | return self::$logger; |
| 260 | } |
| 261 | |
| 262 | private static function getEventManager(): EventManager |
| 263 | { |
| 264 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 265 | return ServiceManager::getServiceManager()->get(EventManager::SERVICE_ID); |
| 266 | } |
| 267 | } |