Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 114 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| SessionStateService | |
0.00% |
0 / 114 |
|
0.00% |
0 / 9 |
650 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| pauseSession | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| resumeSession | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getSessionState | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| updateTimeReference | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getDeliveryExecution | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getClientImplementation | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getSessionDescription | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| getSessionProgress | |
0.00% |
0 / 78 |
|
0.00% |
0 / 1 |
132 | |||
| 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 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoQtiTest\models; |
| 23 | |
| 24 | use oat\oatbox\service\ConfigurableService; |
| 25 | use oat\taoDelivery\model\execution\ServiceProxy; |
| 26 | use oat\taoQtiTest\models\cat\CatService; |
| 27 | use qtism\data\AssessmentItemRef; |
| 28 | use qtism\runtime\tests\AssessmentTestSession; |
| 29 | use oat\taoDelivery\model\execution\DeliveryExecution; |
| 30 | use qtism\runtime\tests\RouteItem; |
| 31 | |
| 32 | /** |
| 33 | * The SessionStateService |
| 34 | * |
| 35 | * Service used for pausing and resuming the delivery execution. |
| 36 | * All timers in paused session will be paused. |
| 37 | * |
| 38 | * Usage example: |
| 39 | * <pre> |
| 40 | * //Pause session: |
| 41 | * $sessionStateService = ServiceManager::getServiceManager()->get('taoQtiTest/SessionStateService'); |
| 42 | * $sessionStateService->pauseSession($session); |
| 43 | * |
| 44 | * //resume session: |
| 45 | * $sessionStateService = ServiceManager::getServiceManager()->get('taoQtiTest/SessionStateService'); |
| 46 | * $sessionStateService->resumeSession($session); |
| 47 | * </pre> |
| 48 | * @author Aleh Hutnikau <hutnikau@1pt.com> |
| 49 | */ |
| 50 | class SessionStateService extends ConfigurableService |
| 51 | { |
| 52 | public const SERVICE_ID = 'taoQtiTest/SessionStateService'; |
| 53 | |
| 54 | public const OPTION_STATE_FORMAT = 'stateFormat'; |
| 55 | |
| 56 | /** |
| 57 | * @var ServiceProxy |
| 58 | */ |
| 59 | private $deliveryExecutionService; |
| 60 | |
| 61 | public function __construct(array $options = []) |
| 62 | { |
| 63 | $this->deliveryExecutionService = ServiceProxy::singleton(); |
| 64 | parent::__construct($options); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Pause delivery execution. |
| 69 | * @param AssessmentTestSession $session |
| 70 | * @return boolean success |
| 71 | */ |
| 72 | public function pauseSession(AssessmentTestSession $session) |
| 73 | { |
| 74 | $session->updateDuration(); |
| 75 | return $this->getDeliveryExecution($session)->setState(DeliveryExecution::STATE_PAUSED); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Resume delivery execution |
| 80 | * @param AssessmentTestSession $session |
| 81 | */ |
| 82 | public function resumeSession(AssessmentTestSession $session) |
| 83 | { |
| 84 | $deliveryExecutionState = $this->getSessionState($session); |
| 85 | if ($deliveryExecutionState === DeliveryExecution::STATE_PAUSED) { |
| 86 | $this->updateTimeReference($session); |
| 87 | $this->getDeliveryExecution($session)->setState(DeliveryExecution::STATE_ACTIVE); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get delivery execution state |
| 93 | * @param AssessmentTestSession $session |
| 94 | * @return string |
| 95 | */ |
| 96 | public function getSessionState(AssessmentTestSession $session) |
| 97 | { |
| 98 | $deliveryExecution = $this->getDeliveryExecution($session); |
| 99 | return $deliveryExecution->getState()->getUri(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Set time reference of current assessment item session to <i>now</i> instead of time of last update. |
| 104 | * This ensures that time when delivery execution was paused will not be taken in account. |
| 105 | * Make sure that method invoked right after retrieving assessment test session |
| 106 | * and before the first AssessmentTestSession::updateDuration method call |
| 107 | * @param AssessmentTestSession $session |
| 108 | * @param \DateTime|null $time Time to be specified. Current time by default. Make sure that $time has UTC timezone. |
| 109 | */ |
| 110 | public function updateTimeReference(AssessmentTestSession $session, \DateTime $time = null) |
| 111 | { |
| 112 | if ($time === null) { |
| 113 | $time = new \DateTime('now', new \DateTimeZone('UTC')); |
| 114 | } |
| 115 | |
| 116 | $itemSession = $session->getCurrentAssessmentItemSession(); |
| 117 | |
| 118 | if ($itemSession) { |
| 119 | $itemSession->setTimeReference($time); |
| 120 | $session->updateDuration(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @param AssessmentTestSession $session |
| 126 | * @return DeliveryExecution |
| 127 | */ |
| 128 | private function getDeliveryExecution(AssessmentTestSession $session) |
| 129 | { |
| 130 | return $this->deliveryExecutionService->getDeliveryExecution($session->getSessionId()); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Returns appropriate JS service implementation for testRunner |
| 135 | * |
| 136 | * @param boolean $resetTimerAfterResume |
| 137 | * |
| 138 | * @return string |
| 139 | */ |
| 140 | public function getClientImplementation($resetTimerAfterResume = false) |
| 141 | { |
| 142 | if ($resetTimerAfterResume) { |
| 143 | return 'taoQtiTest/testRunner/resumingStrategy/keepAfterResume'; |
| 144 | } |
| 145 | return 'taoQtiTest/testRunner/resumingStrategy/resetAfterResume'; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Return a description of the test session |
| 150 | * |
| 151 | * @return string |
| 152 | */ |
| 153 | public function getSessionDescription(\taoQtiTest_helpers_TestSession $session) |
| 154 | { |
| 155 | if ($session->isRunning()) { |
| 156 | $config = \common_ext_ExtensionsManager::singleton() |
| 157 | ->getExtensionById('taoQtiTest') |
| 158 | ->getConfig('testRunner'); |
| 159 | $progressScope = isset($config['progress-indicator-scope']) ? $config['progress-indicator-scope'] : 'test'; |
| 160 | $progress = $this->getSessionProgress($session); |
| 161 | $itemPosition = $progress[$progressScope]; |
| 162 | $itemCount = $progress[$progressScope . 'Length']; |
| 163 | |
| 164 | $map = [ |
| 165 | 'title' => $session->getCurrentAssessmentSection()->getTitle(), |
| 166 | 'itemPosition' => $itemPosition, |
| 167 | 'itemCount' => $itemCount |
| 168 | ]; |
| 169 | return json_encode($map); |
| 170 | } |
| 171 | return json_encode(['title' => 'finished']); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Gets the current progress inside a delivery execution |
| 176 | * @param \taoQtiTest_helpers_TestSession $session |
| 177 | * @return array|bool |
| 178 | */ |
| 179 | protected function getSessionProgress(\taoQtiTest_helpers_TestSession $session) |
| 180 | { |
| 181 | if ($session->isRunning() !== false) { |
| 182 | $config = \common_ext_ExtensionsManager::singleton() |
| 183 | ->getExtensionById('taoQtiTest') |
| 184 | ->getConfig('testRunner'); |
| 185 | $categories = []; |
| 186 | if (isset($config['progress-indicator']) && $config['progress-indicator'] == 'categories') { |
| 187 | $categories = $config['progress-categories']; |
| 188 | } |
| 189 | $route = $session->getRoute(); |
| 190 | $routeItems = $route->getAllRouteItems(); |
| 191 | $offset = $route->getRouteItemPosition($routeItems[0]); |
| 192 | $offsetPart = 0; |
| 193 | $offsetSection = 0; |
| 194 | $lastPart = null; |
| 195 | $lastSection = null; |
| 196 | |
| 197 | $positions = []; |
| 198 | $lengthParts = []; |
| 199 | $lengthSections = []; |
| 200 | $sectionIndex = 0; |
| 201 | $partIndex = 0; |
| 202 | |
| 203 | // compute positions from the test map |
| 204 | /** @var RouteItem $routeItem */ |
| 205 | foreach ($routeItems as $routeItem) { |
| 206 | $testPart = $routeItem->getTestPart(); |
| 207 | $partId = $testPart->getIdentifier(); |
| 208 | if ($lastPart != $partId) { |
| 209 | $offsetPart = 0; |
| 210 | $lastPart = $partId; |
| 211 | $partIndex++; |
| 212 | } |
| 213 | |
| 214 | $section = $routeItem->getAssessmentSection(); |
| 215 | $sectionId = $section->getIdentifier(); |
| 216 | if ($lastSection != $sectionId) { |
| 217 | $offsetSection = 0; |
| 218 | $lastSection = $sectionId; |
| 219 | $sectionIndex++; |
| 220 | } |
| 221 | |
| 222 | $offset++; |
| 223 | $offsetSection++; |
| 224 | |
| 225 | if ($categories && $config['progress-indicator-scope'] == 'testPart') { |
| 226 | /** @var AssessmentItemRef $assessmentItemRef */ |
| 227 | $assessmentItemRef = $routeItem->getAssessmentItemRef(); |
| 228 | $assessmenCategories = $assessmentItemRef->getCategories()->getArrayCopy(); |
| 229 | if (array_intersect($categories, $assessmenCategories)) { |
| 230 | $offsetPart++; |
| 231 | } |
| 232 | } |
| 233 | $lengthParts[$partIndex] = $offsetPart; |
| 234 | $lengthSections[$sectionIndex] = $offsetSection; |
| 235 | |
| 236 | $positions[] = [ |
| 237 | 'test' => $offset, |
| 238 | 'part' => $offsetPart, |
| 239 | 'partId' => $partIndex, |
| 240 | 'section' => $offsetSection, |
| 241 | 'sectionId' => $sectionIndex, |
| 242 | ]; |
| 243 | } |
| 244 | |
| 245 | $progress = $positions[$route->getPosition()]; |
| 246 | |
| 247 | $catService = $this->getServiceManager()->get(CatService::SERVICE_ID); |
| 248 | $currentItem = $route->current(); |
| 249 | if ($catService->isAdaptive($session, $currentItem->getAssessmentItemRef())) { |
| 250 | $testSessionService = $this->getServiceManager()->get(TestSessionService::SERVICE_ID); |
| 251 | $testSessionData = $testSessionService->getTestSessionDataById($session->getSessionId()); |
| 252 | $sectionItems = $catService->getShadowTest( |
| 253 | $session, |
| 254 | $testSessionData['compilation']['private'], |
| 255 | $currentItem |
| 256 | ); |
| 257 | $currentItem = $catService->getCurrentCatItemId( |
| 258 | $session, |
| 259 | $testSessionData['compilation']['private'], |
| 260 | $currentItem |
| 261 | ); |
| 262 | $positionInSection = array_search($currentItem, $sectionItems); |
| 263 | |
| 264 | // When in an adaptive section, the actual section is just a placeholder that is dynamically |
| 265 | // replaced by the adaptive content. To set the right position, just grab the offset within |
| 266 | // this dynamic content and add it to the placeholder position. |
| 267 | $progress['test'] += $positionInSection; |
| 268 | $progress['part'] += $positionInSection; |
| 269 | $progress['section'] += $positionInSection; |
| 270 | $lengthSections[$progress['sectionId']] = count($sectionItems); |
| 271 | } |
| 272 | |
| 273 | return [ |
| 274 | 'test' => $progress['test'], |
| 275 | 'testPart' => $progress['part'], |
| 276 | 'testSection' => $progress['section'], |
| 277 | 'testLength' => $session->getRouteCount(), |
| 278 | 'testPartLength' => $lengthParts[$progress['partId']], |
| 279 | 'testSectionLength' => $lengthSections[$progress['sectionId']], |
| 280 | ]; |
| 281 | } |
| 282 | return false; |
| 283 | } |
| 284 | } |