Code Coverage  | 
      ||||||||||
Lines  | 
       Functions and Methods  | 
       Classes and Traits  | 
      ||||||||
| Total |         | 
       25.23%  | 
       28 / 111  | 
               | 
       15.79%  | 
       3 / 19  | 
       CRAP |         | 
       0.00%  | 
       0 / 1  | 
      
| DeliveryMonitoringData |         | 
       25.23%  | 
       28 / 111  | 
               | 
       15.79%  | 
       3 / 19  | 
       1011.27 |         | 
       0.00%  | 
       0 / 1  | 
      
| __construct |         | 
       100.00%  | 
       2 / 2  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| update |         | 
       100.00%  | 
       1 / 1  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| addValue |         | 
       100.00%  | 
       2 / 2  | 
               | 
       100.00%  | 
       1 / 1  | 
       3 | |||
| setDeliveryExecution |         | 
       0.00%  | 
       0 / 1  | 
               | 
       0.00%  | 
       0 / 1  | 
       2 | |||
| getDeliveryExecution |         | 
       0.00%  | 
       0 / 1  | 
               | 
       0.00%  | 
       0 / 1  | 
       2 | |||
| setDeliveryExecutionContext |         | 
       0.00%  | 
       0 / 1  | 
               | 
       0.00%  | 
       0 / 1  | 
       2 | |||
| getDeliveryExecutionContext |         | 
       0.00%  | 
       0 / 6  | 
               | 
       0.00%  | 
       0 / 1  | 
       12 | |||
| validate |         | 
       72.73%  | 
       8 / 11  | 
               | 
       0.00%  | 
       0 / 1  | 
       7.99 | |||
| getErrors |         | 
       0.00%  | 
       0 / 1  | 
               | 
       0.00%  | 
       0 / 1  | 
       2 | |||
| get |         | 
       66.67%  | 
       2 / 3  | 
               | 
       0.00%  | 
       0 / 1  | 
       2.15 | |||
| setTestSession |         | 
       0.00%  | 
       0 / 1  | 
               | 
       0.00%  | 
       0 / 1  | 
       2 | |||
| updateData |         | 
       45.45%  | 
       5 / 11  | 
               | 
       0.00%  | 
       0 / 1  | 
       6.60 | |||
| updateLastTestTakerActivity |         | 
       0.00%  | 
       0 / 1  | 
               | 
       0.00%  | 
       0 / 1  | 
       2 | |||
| updateLastConnect |         | 
       72.73%  | 
       8 / 11  | 
               | 
       0.00%  | 
       0 / 1  | 
       3.18 | |||
| updateStatus |         | 
       0.00%  | 
       0 / 4  | 
               | 
       0.00%  | 
       0 / 1  | 
       6 | |||
| updateRemainingTime |         | 
       0.00%  | 
       0 / 15  | 
               | 
       0.00%  | 
       0 / 1  | 
       42 | |||
| updateDiffTimestamp |         | 
       0.00%  | 
       0 / 13  | 
               | 
       0.00%  | 
       0 / 1  | 
       30 | |||
| updateExtraTime |         | 
       0.00%  | 
       0 / 22  | 
               | 
       0.00%  | 
       0 / 1  | 
       12 | |||
| getTestSession |         | 
       0.00%  | 
       0 / 4  | 
               | 
       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 (original work) Open Assessment Technologies SA; | 
| 19 | * | 
| 20 | * | 
| 21 | */ | 
| 22 | |
| 23 | namespace oat\taoProctoring\model\monitorCache\implementation; | 
| 24 | |
| 25 | use oat\taoDelivery\model\execution\DeliveryExecutionContext; | 
| 26 | use oat\taoDelivery\model\execution\DeliveryExecutionContextInterface; | 
| 27 | use oat\taoDelivery\model\execution\DeliveryExecutionInterface; | 
| 28 | use oat\taoProctoring\model\execution\DeliveryExecutionManagerService; | 
| 29 | use oat\taoProctoring\model\implementation\TestSessionService; | 
| 30 | use oat\taoProctoring\model\monitorCache\DeliveryMonitoringData as DeliveryMonitoringDataInterface; | 
| 31 | use oat\taoProctoring\model\execution\DeliveryExecution as ProctoredDeliveryExecution; | 
| 32 | use oat\taoProctoring\model\TestSessionConnectivityStatusService; | 
| 33 | use oat\taoQtiTest\models\runner\session\TestSession; | 
| 34 | use oat\taoQtiTest\models\runner\time\QtiTimerFactory; | 
| 35 | use oat\taoTests\models\runner\time\TimePoint; | 
| 36 | use qtism\runtime\tests\AssessmentTestSession; | 
| 37 | use oat\taoDelivery\model\execution\DeliveryExecution; | 
| 38 | use oat\taoProctoring\model\monitorCache\DeliveryMonitoringService; | 
| 39 | use Zend\ServiceManager\ServiceLocatorAwareTrait; | 
| 40 | use Zend\ServiceManager\ServiceLocatorAwareInterface; | 
| 41 | |
| 42 | /** | 
| 43 | * class DeliveryMonitoringData | 
| 44 | * | 
| 45 | * Represents data model of delivery execution. | 
| 46 | * | 
| 47 | * @package oat\taoProctoring | 
| 48 | * @author Aleh Hutnikau <hutnikau@1pt.com> | 
| 49 | */ | 
| 50 | class DeliveryMonitoringData implements DeliveryMonitoringDataInterface, ServiceLocatorAwareInterface | 
| 51 | { | 
| 52 | use ServiceLocatorAwareTrait; | 
| 53 | |
| 54 | /** | 
| 55 | * @var array | 
| 56 | */ | 
| 57 | private $data = []; | 
| 58 | |
| 59 | /** | 
| 60 | * @var DeliveryExecution | 
| 61 | */ | 
| 62 | private $deliveryExecution; | 
| 63 | |
| 64 | /** | 
| 65 | * @var AssessmentTestSession | 
| 66 | */ | 
| 67 | private $testSession; | 
| 68 | |
| 69 | /** | 
| 70 | * @var array | 
| 71 | */ | 
| 72 | private $errors = []; | 
| 73 | |
| 74 | /** | 
| 75 | * @var array | 
| 76 | */ | 
| 77 | private $requiredFields = [ | 
| 78 | DeliveryMonitoringService::DELIVERY_EXECUTION_ID, | 
| 79 | DeliveryMonitoringService::STATUS, | 
| 80 | ]; | 
| 81 | |
| 82 | /** | 
| 83 | * @param DeliveryExecutionInterface $deliveryExecution | 
| 84 | * @param $data | 
| 85 | * @throws \common_exception_NotFound | 
| 86 | */ | 
| 87 | public function __construct(DeliveryExecutionInterface $deliveryExecution, array $data) | 
| 88 | { | 
| 89 | $this->deliveryExecution = $deliveryExecution; | 
| 90 | $this->data = $data; | 
| 91 | } | 
| 92 | |
| 93 | /** | 
| 94 | * (non-PHPdoc) | 
| 95 | * @see \oat\taoProctoring\model\monitorCache\DeliveryMonitoringData::update() | 
| 96 | */ | 
| 97 | public function update($key, $value) | 
| 98 | { | 
| 99 | $this->addValue($key, $value, true); | 
| 100 | } | 
| 101 | |
| 102 | /** | 
| 103 | * Add data | 
| 104 | * @param string $key | 
| 105 | * @param string $value | 
| 106 | * @param boolean $overwrite | 
| 107 | */ | 
| 108 | public function addValue($key, $value, $overwrite = false) | 
| 109 | { | 
| 110 | if (!isset($this->data[$key]) || $overwrite) { | 
| 111 | $this->data[$key] = (string) $value; | 
| 112 | } | 
| 113 | } | 
| 114 | |
| 115 | /** | 
| 116 | * Save delivery execution | 
| 117 | * @param DeliveryExecution $deliveryExecution | 
| 118 | */ | 
| 119 | public function setDeliveryExecution(DeliveryExecution $deliveryExecution) | 
| 120 | { | 
| 121 | $this->deliveryExecution = $deliveryExecution; | 
| 122 | } | 
| 123 | |
| 124 | /** | 
| 125 | * @return DeliveryExecutionInterface | 
| 126 | */ | 
| 127 | public function getDeliveryExecution() | 
| 128 | { | 
| 129 | return $this->deliveryExecution; | 
| 130 | } | 
| 131 | |
| 132 | /** | 
| 133 | * {@inheritdoc} | 
| 134 | */ | 
| 135 | public function setDeliveryExecutionContext(DeliveryExecutionContextInterface $context) | 
| 136 | { | 
| 137 | $this->update(self::PARAM_EXECUTION_CONTEXT, json_encode($context)); | 
| 138 | } | 
| 139 | |
| 140 | /** | 
| 141 | * {@inheritdoc} | 
| 142 | */ | 
| 143 | public function getDeliveryExecutionContext() | 
| 144 | { | 
| 145 | try { | 
| 146 | if (isset($this->data[self::PARAM_EXECUTION_CONTEXT])) { | 
| 147 | return DeliveryExecutionContext::createFromArray( | 
| 148 | json_decode($this->data[self::PARAM_EXECUTION_CONTEXT], true) | 
| 149 | ); | 
| 150 | } | 
| 151 | } catch (\Exception $e) { | 
| 152 | } | 
| 153 | |
| 154 | return null; | 
| 155 | } | 
| 156 | |
| 157 | /** | 
| 158 | * Validate data | 
| 159 | * @return bool whether data is valid and can be saved. | 
| 160 | */ | 
| 161 | public function validate() | 
| 162 | { | 
| 163 | $result = true; | 
| 164 | $this->errors = []; | 
| 165 | $data = $this->get(); | 
| 166 | |
| 167 | foreach ($this->requiredFields as $requiredField) { | 
| 168 | if (!isset($data[$requiredField])) { | 
| 169 | $result = false; | 
| 170 | $this->errors[$requiredField] = 'cannot be empty'; | 
| 171 | } | 
| 172 | } | 
| 173 | |
| 174 | foreach ($data as $fieldName => $fieldValue) { | 
| 175 | if (!array_key_exists($fieldName, $this->errors) && $fieldValue !== null && !is_string($fieldValue)) { | 
| 176 | $this->errors[$fieldName] = 'should be a string'; | 
| 177 | } | 
| 178 | } | 
| 179 | |
| 180 | return $result; | 
| 181 | } | 
| 182 | |
| 183 | /** | 
| 184 | * Get list of errors. | 
| 185 | * @return array | 
| 186 | */ | 
| 187 | public function getErrors() | 
| 188 | { | 
| 189 | return $this->errors; | 
| 190 | } | 
| 191 | |
| 192 | /** | 
| 193 | * Get delivery execution data | 
| 194 | * @param bool $refresh | 
| 195 | * @return array | 
| 196 | */ | 
| 197 | public function get($refresh = false) | 
| 198 | { | 
| 199 | if ($refresh) { | 
| 200 | $this->updateData(); | 
| 201 | } | 
| 202 | return $this->data; | 
| 203 | } | 
| 204 | |
| 205 | /** | 
| 206 | * Set test session | 
| 207 | * @param AssessmentTestSession $testSession | 
| 208 | */ | 
| 209 | public function setTestSession(AssessmentTestSession $testSession) | 
| 210 | { | 
| 211 | $this->testSession = $testSession; | 
| 212 | } | 
| 213 | |
| 214 | /** | 
| 215 | * @param array $keys | 
| 216 | */ | 
| 217 | public function updateData(array $keys = null) | 
| 218 | { | 
| 219 | if ($keys === null) { | 
| 220 | $keys = [ | 
| 221 | DeliveryMonitoringService::STATUS, | 
| 222 | DeliveryMonitoringService::REMAINING_TIME, | 
| 223 | DeliveryMonitoringService::EXTRA_TIME, | 
| 224 | DeliveryMonitoringService::EXTENDED_TIME | 
| 225 | ]; | 
| 226 | } | 
| 227 | foreach ($keys as $key) { | 
| 228 | $methodName = 'update' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))); | 
| 229 | if (method_exists($this, $methodName)) { | 
| 230 | $this->{$methodName}(); | 
| 231 | } | 
| 232 | } | 
| 233 | } | 
| 234 | |
| 235 | /** | 
| 236 | * Update extra time allowed for the delivery execution | 
| 237 | */ | 
| 238 | private function updateLastTestTakerActivity() | 
| 239 | { | 
| 240 | $this->addValue(DeliveryMonitoringService::LAST_TEST_TAKER_ACTIVITY, microtime(true), true); | 
| 241 | } | 
| 242 | |
| 243 | /** | 
| 244 | * Update connectivity status (online|offline) | 
| 245 | */ | 
| 246 | private function updateLastConnect() | 
| 247 | { | 
| 248 | $status = $this->deliveryExecution->getState()->getUri(); | 
| 249 | /** @var TestSessionConnectivityStatusService $testSessionConnectivityStatusService */ | 
| 250 | $testSessionConnectivityStatusService = $this->getServiceLocator()->get( | 
| 251 | TestSessionConnectivityStatusService::SERVICE_ID | 
| 252 | ); | 
| 253 | |
| 254 | if ( | 
| 255 | $testSessionConnectivityStatusService->hasOnlineMode() | 
| 256 | && ProctoredDeliveryExecution::STATE_ACTIVE == $status | 
| 257 | ) { | 
| 258 | $lastConnectivity = $testSessionConnectivityStatusService->getLastOnline( | 
| 259 | $this->deliveryExecution->getIdentifier() | 
| 260 | ); | 
| 261 | } else { | 
| 262 | // to ensure that during sorting by connectivity all similar statuses grouped together | 
| 263 | $lastConnectivity = (~PHP_INT_MAX) + substr(abs(crc32($status)), 0, 3); | 
| 264 | } | 
| 265 | |
| 266 | $this->addValue(DeliveryMonitoringService::CONNECTIVITY, $lastConnectivity, true); | 
| 267 | } | 
| 268 | |
| 269 | /** | 
| 270 | * Update test session state | 
| 271 | */ | 
| 272 | private function updateStatus() | 
| 273 | { | 
| 274 | $status = $this->deliveryExecution->getState()->getUri(); | 
| 275 | $this->addValue(DeliveryMonitoringService::STATUS, $status, true); | 
| 276 | if ($status == ProctoredDeliveryExecution::STATE_PAUSED) { | 
| 277 | $this->addValue(DeliveryMonitoringService::LAST_PAUSE_TIMESTAMP, microtime(true), true); | 
| 278 | } | 
| 279 | } | 
| 280 | |
| 281 | /** | 
| 282 | * Update remaining time of delivery execution | 
| 283 | */ | 
| 284 | private function updateRemainingTime() | 
| 285 | { | 
| 286 | $result = null; | 
| 287 | $remaining = 0; | 
| 288 | $hasTimer = false; | 
| 289 | |
| 290 | $session = $this->getTestSession(); | 
| 291 | |
| 292 | if ($session !== null && $session->isRunning()) { | 
| 293 | $remaining = PHP_INT_MAX; | 
| 294 | $timeConstraints = $session->getTimeConstraints(); | 
| 295 | foreach ($timeConstraints as $tc) { | 
| 296 | // Only consider time constraints in force. | 
| 297 | $maximumRemainingTime = $tc->getMaximumRemainingTime(); | 
| 298 | if ($maximumRemainingTime !== false) { | 
| 299 | $hasTimer = true; | 
| 300 | $remaining = min($remaining, $maximumRemainingTime->getSeconds(true)); | 
| 301 | } | 
| 302 | } | 
| 303 | } | 
| 304 | |
| 305 | if ($hasTimer) { | 
| 306 | $result = $remaining; | 
| 307 | } | 
| 308 | |
| 309 | $this->addValue(DeliveryMonitoringService::REMAINING_TIME, $result, true); | 
| 310 | } | 
| 311 | |
| 312 | /** | 
| 313 | * Update diff between last_pause_timestamp and last_test_taker_activity | 
| 314 | */ | 
| 315 | private function updateDiffTimestamp() | 
| 316 | { | 
| 317 | $diffTimestamp = 0; | 
| 318 | $lastTimeStamp = 0; | 
| 319 | $lastActivity = 0; | 
| 320 | if (isset($this->data[DeliveryMonitoringService::LAST_PAUSE_TIMESTAMP])) { | 
| 321 | $lastTimeStamp = $this->data[DeliveryMonitoringService::LAST_PAUSE_TIMESTAMP]; | 
| 322 | } | 
| 323 | |
| 324 | if (isset($this->data[DeliveryMonitoringService::LAST_TEST_TAKER_ACTIVITY])) { | 
| 325 | $lastActivity = $this->data[DeliveryMonitoringService::LAST_TEST_TAKER_ACTIVITY]; | 
| 326 | } | 
| 327 | |
| 328 | if ($lastTimeStamp - $lastActivity > 0) { | 
| 329 | $diffTimestamp = isset($this->data[DeliveryMonitoringService::DIFF_TIMESTAMP]) | 
| 330 | ? $this->data[DeliveryMonitoringService::DIFF_TIMESTAMP] | 
| 331 | : 0; | 
| 332 | $diffTimestamp += $lastTimeStamp - $lastActivity; | 
| 333 | } | 
| 334 | |
| 335 | $this->addValue(DeliveryMonitoringService::DIFF_TIMESTAMP, $diffTimestamp, true); | 
| 336 | } | 
| 337 | |
| 338 | /** | 
| 339 | * Update extra time allowed for the delivery execution | 
| 340 | */ | 
| 341 | private function updateExtraTime() | 
| 342 | { | 
| 343 | $testSession = $this->getTestSession(); | 
| 344 | if ($testSession instanceof TestSession) { | 
| 345 | $timer = $testSession->getTimer(); | 
| 346 | $timerTarget = $testSession->getTimerTarget(); | 
| 347 | } else { | 
| 348 | $timerTarget = TimePoint::TARGET_SERVER; | 
| 349 | $qtiTimerFactory = $this->getServiceLocator()->get(QtiTimerFactory::SERVICE_ID); | 
| 350 | $timer = $qtiTimerFactory->getTimer( | 
| 351 | $this->deliveryExecution->getIdentifier(), | 
| 352 | $this->deliveryExecution->getUserIdentifier() | 
| 353 | ); | 
| 354 | } | 
| 355 | |
| 356 | /** @var DeliveryExecutionManagerService $deliveryExecutionManager */ | 
| 357 | $deliveryExecutionManager = $this->getServiceLocator()->get(DeliveryExecutionManagerService::SERVICE_ID); | 
| 358 | $maxTimeSeconds = $deliveryExecutionManager->getTimeLimits($testSession); | 
| 359 | |
| 360 | $data = $this->get(); | 
| 361 | $oldConsumedExtraTime = isset($data[DeliveryMonitoringService::CONSUMED_EXTRA_TIME]) | 
| 362 | ? $data[DeliveryMonitoringService::CONSUMED_EXTRA_TIME] | 
| 363 | : 0; | 
| 364 | $consumedExtraTime = max( | 
| 365 | $oldConsumedExtraTime, | 
| 366 | $timer->getConsumedExtraTime(null, $maxTimeSeconds, $timerTarget) | 
| 367 | ); | 
| 368 | |
| 369 | $this->addValue(DeliveryMonitoringService::EXTRA_TIME, $timer->getExtraTime(), true); | 
| 370 | $this->addValue(DeliveryMonitoringService::CONSUMED_EXTRA_TIME, $consumedExtraTime, true); | 
| 371 | } | 
| 372 | |
| 373 | /** | 
| 374 | * @return AssessmentTestSession | 
| 375 | */ | 
| 376 | private function getTestSession() | 
| 377 | { | 
| 378 | if ($this->testSession === null) { | 
| 379 | $testSessionService = $this->getServiceLocator()->get(TestSessionService::SERVICE_ID); | 
| 380 | $this->testSession = $testSessionService->getTestSession($this->deliveryExecution); | 
| 381 | } | 
| 382 | return $this->testSession; | 
| 383 | } | 
| 384 | } |