Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 197 |
|
0.00% |
0 / 32 |
CRAP | |
0.00% |
0 / 1 |
| QtiTimer | |
0.00% |
0 / 197 |
|
0.00% |
0 / 32 |
5550 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| start | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
20 | |||
| end | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| getFirstTimestamp | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getLastTimestamp | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getLastRegisteredTimestamp | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| adjust | |
0.00% |
0 / 57 |
|
0.00% |
0 / 1 |
182 | |||
| compute | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| timeout | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setStorage | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setStrategy | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getStorage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| toArray | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| jsonSerialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| save | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| unserializeTimeLine | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| unserializeAdjustmentMap | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| load | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
132 | |||
| getExtraTime | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getExtendedTime | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setExtendedTime | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setExtraTime | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setConsumedExtraTime | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getConsumedExtraTime | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getRemainingExtraTime | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAdjustmentMap | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| delete | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| checkTimestampCoherence | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| isRangeOpen | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
12 | |||
| getRange | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| onlyOneFlag | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| binaryPopCount | |
0.00% |
0 / 6 |
|
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 (original work) Open Assessment Technologies SA ; |
| 19 | */ |
| 20 | |
| 21 | /** |
| 22 | * @author Jean-Sébastien Conan <jean-sebastien.conan@vesperiagroup.com> |
| 23 | */ |
| 24 | |
| 25 | namespace oat\taoQtiTest\models\runner\time; |
| 26 | |
| 27 | use oat\taoTests\models\runner\time\ExtraTime; |
| 28 | use oat\taoTests\models\runner\time\InconsistentCriteriaException; |
| 29 | use oat\taoTests\models\runner\time\InconsistentRangeException; |
| 30 | use oat\taoTests\models\runner\time\InvalidDataException; |
| 31 | use oat\taoTests\models\runner\time\InvalidStorageException; |
| 32 | use oat\taoTests\models\runner\time\InvalidTimerStrategyException; |
| 33 | use oat\taoTests\models\runner\time\TimeException; |
| 34 | use oat\taoTests\models\runner\time\TimeLine; |
| 35 | use oat\taoTests\models\runner\time\TimePoint; |
| 36 | use oat\taoTests\models\runner\time\TimerAdjustmentMapInterface; |
| 37 | use oat\taoTests\models\runner\time\TimerStrategyInterface; |
| 38 | use oat\taoTests\models\runner\time\TimeStorage; |
| 39 | use oat\taoTests\models\runner\time\Timer; |
| 40 | |
| 41 | /** |
| 42 | * Class QtiTimer |
| 43 | * @package oat\taoQtiTest\models\runner\time |
| 44 | */ |
| 45 | class QtiTimer implements Timer, ExtraTime, \JsonSerializable |
| 46 | { |
| 47 | /** |
| 48 | * The TimeLine used to compute the duration |
| 49 | * @var TimeLine |
| 50 | */ |
| 51 | protected $timeLine; |
| 52 | |
| 53 | /** |
| 54 | * The storage used to maintain the data |
| 55 | * @var TimeStorage |
| 56 | */ |
| 57 | protected $storage; |
| 58 | |
| 59 | /** |
| 60 | * @var TimerStrategyInterface |
| 61 | */ |
| 62 | protected $timerStrategy; |
| 63 | |
| 64 | /** |
| 65 | * The total added extra time |
| 66 | * @var float |
| 67 | */ |
| 68 | protected $extraTime = 0.0; |
| 69 | |
| 70 | /** |
| 71 | * The extended time |
| 72 | * @var float |
| 73 | */ |
| 74 | protected $extendedTime = 0.0; |
| 75 | |
| 76 | /** |
| 77 | * The already consumed extra time |
| 78 | * @var float |
| 79 | */ |
| 80 | protected $consumedExtraTime = 0.0; |
| 81 | |
| 82 | /** |
| 83 | * @var AdjustmentMap |
| 84 | */ |
| 85 | protected $adjustmentMap; |
| 86 | |
| 87 | /** |
| 88 | * QtiTimer constructor. |
| 89 | */ |
| 90 | public function __construct() |
| 91 | { |
| 92 | $this->timeLine = new QtiTimeLine(); |
| 93 | $this->adjustmentMap = new AdjustmentMap(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Adds a "server start" TimePoint at a particular timestamp for the provided ItemRef |
| 98 | * @param string|array $tags |
| 99 | * @param float $timestamp |
| 100 | * @return Timer |
| 101 | * @throws TimeException |
| 102 | */ |
| 103 | public function start($tags, $timestamp) |
| 104 | { |
| 105 | // check the provided arguments |
| 106 | if (!is_numeric($timestamp) || $timestamp < 0) { |
| 107 | throw new InvalidDataException('start() needs a valid timestamp!'); |
| 108 | } |
| 109 | |
| 110 | // extract the TimePoint identification from the provided item, and find existing range |
| 111 | $range = $this->getRange($tags); |
| 112 | |
| 113 | // validate the data consistence |
| 114 | if ($this->isRangeOpen($range)) { |
| 115 | // unclosed range found, auto closing |
| 116 | // auto generate the timestamp for the missing END point, one microsecond earlier |
| 117 | \common_Logger::t('Missing END TimePoint in QtiTimer, auto add an arbitrary value'); |
| 118 | $point = new TimePoint( |
| 119 | $tags, |
| 120 | $timestamp - (1 / TimePoint::PRECISION), |
| 121 | TimePoint::TYPE_END, |
| 122 | TimePoint::TARGET_SERVER |
| 123 | ); |
| 124 | $this->timeLine->add($point); |
| 125 | $range[] = $point; |
| 126 | } |
| 127 | $this->checkTimestampCoherence($range, $timestamp); |
| 128 | |
| 129 | // append the new START TimePoint |
| 130 | $point = new TimePoint($tags, $timestamp, TimePoint::TYPE_START, TimePoint::TARGET_SERVER); |
| 131 | $this->timeLine->add($point); |
| 132 | |
| 133 | return $this; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Adds a "server end" TimePoint at a particular timestamp for the provided ItemRef |
| 138 | * @param string|array $tags |
| 139 | * @param float $timestamp |
| 140 | * @return Timer |
| 141 | * @throws TimeException |
| 142 | */ |
| 143 | public function end($tags, $timestamp) |
| 144 | { |
| 145 | // check the provided arguments |
| 146 | if (!is_numeric($timestamp) || $timestamp < 0) { |
| 147 | throw new InvalidDataException('end() needs a valid timestamp!'); |
| 148 | } |
| 149 | |
| 150 | // extract the TimePoint identification from the provided item, and find existing range |
| 151 | $range = $this->getRange($tags); |
| 152 | |
| 153 | // validate the data consistence |
| 154 | if ($this->isRangeOpen($range)) { |
| 155 | $this->checkTimestampCoherence($range, $timestamp); |
| 156 | |
| 157 | // append the new END TimePoint |
| 158 | $point = new TimePoint($tags, $timestamp, TimePoint::TYPE_END, TimePoint::TARGET_SERVER); |
| 159 | $this->timeLine->add($point); |
| 160 | } else { |
| 161 | // already closed range found, just log the info |
| 162 | \common_Logger::t('Range already closed, or missing START TimePoint in QtiTimer, continue anyway'); |
| 163 | } |
| 164 | |
| 165 | return $this; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Gets the first timestamp of the range for the provided tags |
| 170 | * @param string|array $tags |
| 171 | * @return float $timestamp |
| 172 | */ |
| 173 | public function getFirstTimestamp($tags) |
| 174 | { |
| 175 | // extract the TimePoint identification from the provided item, and find existing range |
| 176 | $range = $this->getRange($tags); |
| 177 | $last = false; |
| 178 | |
| 179 | if (count($range)) { |
| 180 | $last = $range[0]->getTimestamp(); |
| 181 | } |
| 182 | |
| 183 | return $last; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Gets the last timestamp of the range for the provided tags |
| 188 | * @param string|array $tags |
| 189 | * @return bool|float $timestamp Returns the last timestamp of the range or false if none |
| 190 | */ |
| 191 | public function getLastTimestamp($tags) |
| 192 | { |
| 193 | // extract the TimePoint identification from the provided item, and find existing range |
| 194 | $range = $this->getRange($tags); |
| 195 | $length = count($range); |
| 196 | $last = false; |
| 197 | |
| 198 | if ($length) { |
| 199 | $last = $range[$length - 1]->getTimestamp(); |
| 200 | } |
| 201 | |
| 202 | return $last; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Gets the last registered timestamp |
| 207 | * @return bool|float $timestamp Returns the last timestamp or false if none |
| 208 | */ |
| 209 | public function getLastRegisteredTimestamp() |
| 210 | { |
| 211 | $points = $this->timeLine->getPoints(); |
| 212 | $length = count($points); |
| 213 | $last = false; |
| 214 | |
| 215 | if ($length) { |
| 216 | $last = end($points)->getTimestamp(); |
| 217 | } |
| 218 | |
| 219 | return $last; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Adds "client start" and "client end" TimePoint based on the provided duration for a particular ItemRef |
| 224 | * @param string|array $tags |
| 225 | * @param float $duration |
| 226 | * @return Timer |
| 227 | * @throws TimeException |
| 228 | */ |
| 229 | public function adjust($tags, $duration) |
| 230 | { |
| 231 | // check the provided arguments |
| 232 | if (!is_null($duration) && (!is_numeric($duration) || $duration < 0)) { |
| 233 | throw new InvalidDataException('adjust() needs a valid duration!'); |
| 234 | } |
| 235 | |
| 236 | // extract the TimePoint identification from the provided item, and find existing range |
| 237 | $itemTimeLine = $this->timeLine->filter($tags, TimePoint::TARGET_SERVER); |
| 238 | $range = $itemTimeLine->getPoints(); |
| 239 | |
| 240 | // validate the data consistence |
| 241 | $rangeLength = count($range); |
| 242 | if (!$rangeLength || ($rangeLength % 2)) { |
| 243 | throw new InconsistentRangeException( |
| 244 | 'The time range does not seem to be consistent, the range is not complete!' |
| 245 | ); |
| 246 | } |
| 247 | |
| 248 | $serverDuration = $itemTimeLine->compute(); |
| 249 | |
| 250 | // take care of existing client range |
| 251 | $clientTimeLine = $this->timeLine->filter($tags, TimePoint::TARGET_CLIENT); |
| 252 | $clientRange = $clientTimeLine->getPoints(); |
| 253 | $clientRangeLength = count($clientRange); |
| 254 | if ($clientRangeLength) { |
| 255 | $clientDuration = 0; |
| 256 | try { |
| 257 | $clientDuration = $clientTimeLine->compute(); |
| 258 | } catch (TimeException $e) { |
| 259 | \common_Logger::t('Handled client range error'); |
| 260 | } |
| 261 | |
| 262 | if (is_null($duration)) { |
| 263 | if ($clientDuration) { |
| 264 | $duration = $clientDuration; |
| 265 | \common_Logger::t( |
| 266 | "No client duration provided to adjust the timer, but a range already exist: ${duration}" |
| 267 | ); |
| 268 | } else { |
| 269 | $duration = $serverDuration; |
| 270 | \common_Logger::t( |
| 271 | "No client duration provided to adjust the timer, fallback to server duration: ${duration}" |
| 272 | ); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | $removed = $this->timeLine->remove($tags, TimePoint::TARGET_CLIENT); |
| 277 | if ($removed == $clientRangeLength) { |
| 278 | \common_Logger::t("Replace client duration in timer: ${clientDuration} to ${duration}"); |
| 279 | } else { |
| 280 | \common_Logger::w("Unable to replace client duration in timer: ${clientDuration} to ${duration}"); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // check if the client side duration is bound by the server side duration |
| 285 | if (is_null($duration)) { |
| 286 | $duration = $serverDuration; |
| 287 | \common_Logger::t( |
| 288 | "No client duration provided to adjust the timer, fallback to server duration: ${duration}" |
| 289 | ); |
| 290 | } elseif ($duration > $serverDuration) { |
| 291 | \common_Logger::w( |
| 292 | "A client duration must not be larger than the server time range! (${duration} > ${serverDuration})" |
| 293 | ); |
| 294 | $duration = $serverDuration; |
| 295 | } |
| 296 | |
| 297 | // extract range boundaries |
| 298 | TimePoint::sort($range); |
| 299 | $serverStart = $range[0]; |
| 300 | $serverEnd = $range[$rangeLength - 1]; |
| 301 | |
| 302 | // adjust the range by inserting the client duration between the server overall time range boundaries |
| 303 | $overallDuration = $serverEnd->getTimestamp() - $serverStart->getTimestamp(); |
| 304 | $delay = ($overallDuration - $duration) / 2; |
| 305 | |
| 306 | $start = new TimePoint( |
| 307 | $tags, |
| 308 | $serverStart->getTimestamp() + $delay, |
| 309 | TimePoint::TYPE_START, |
| 310 | TimePoint::TARGET_CLIENT |
| 311 | ); |
| 312 | $this->timeLine->add($start); |
| 313 | |
| 314 | $end = new TimePoint($tags, $serverEnd->getTimestamp() - $delay, TimePoint::TYPE_END, TimePoint::TARGET_CLIENT); |
| 315 | $this->timeLine->add($end); |
| 316 | |
| 317 | return $this; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Computes the total duration represented by the filtered TimePoints |
| 322 | * @param string|array $tags A tag or a list of tags to filter |
| 323 | * @param int $target The type of target TimePoint to filter |
| 324 | * @return float Returns the total computed duration |
| 325 | * @throws TimeException |
| 326 | */ |
| 327 | public function compute($tags, $target) |
| 328 | { |
| 329 | // cannot compute a duration across different targets |
| 330 | if (!$this->onlyOneFlag($target)) { |
| 331 | throw new InconsistentCriteriaException('Cannot compute a duration across different targets!'); |
| 332 | } |
| 333 | |
| 334 | return $this->timeLine->compute($tags, $target); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Checks if the duration of a TimeLine subset reached the timeout |
| 339 | * @param float $timeLimit The time limit against which compare the duration |
| 340 | * @param string|array $tags A tag or a list of tags to filter |
| 341 | * @param int $target The type of target TimePoint to filter |
| 342 | * @return bool Returns true if the timeout is reached |
| 343 | * @throws TimeException |
| 344 | */ |
| 345 | public function timeout($timeLimit, $tags, $target) |
| 346 | { |
| 347 | $duration = $this->compute($tags, $target); |
| 348 | return $duration >= $timeLimit; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Sets the storage used to maintain the data |
| 353 | * @param TimeStorage $storage |
| 354 | * @return Timer |
| 355 | */ |
| 356 | public function setStorage(TimeStorage $storage) |
| 357 | { |
| 358 | $this->storage = $storage; |
| 359 | return $this; |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * @inheritDoc |
| 364 | */ |
| 365 | public function setStrategy(TimerStrategyInterface $strategy) |
| 366 | { |
| 367 | $this->timerStrategy = $strategy; |
| 368 | return $this; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Gets the storage used to maintain the data |
| 373 | * @return TimeStorage |
| 374 | */ |
| 375 | public function getStorage() |
| 376 | { |
| 377 | return $this->storage; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Exports the internal state to an array |
| 382 | * @return array |
| 383 | */ |
| 384 | public function toArray() |
| 385 | { |
| 386 | return [ |
| 387 | QtiTimeStorageFormat::STORAGE_KEY_TIME_LINE => $this->timeLine, |
| 388 | QtiTimeStorageFormat::STORAGE_KEY_EXTRA_TIME => $this->extraTime, |
| 389 | QtiTimeStorageFormat::STORAGE_KEY_EXTENDED_TIME => $this->extendedTime, |
| 390 | QtiTimeStorageFormat::STORAGE_KEY_CONSUMED_EXTRA_TIME => $this->consumedExtraTime, |
| 391 | QtiTimeStorageFormat::STORAGE_KEY_TIMER_ADJUSTMENT_MAP => $this->adjustmentMap, |
| 392 | ]; |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Specify data which should be serialized to JSON |
| 397 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
| 398 | * @return mixed data which can be serialized by <b>json_encode</b>, |
| 399 | * which is a value of any type other than a resource. |
| 400 | * @since 5.4.0 |
| 401 | */ |
| 402 | public function jsonSerialize() |
| 403 | { |
| 404 | return $this->toArray(); |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Saves the data to the storage |
| 409 | * @return Timer |
| 410 | * @throws InvalidStorageException |
| 411 | */ |
| 412 | public function save() |
| 413 | { |
| 414 | if (!$this->storage) { |
| 415 | throw new InvalidStorageException('A storage must be defined in order to store the data!'); |
| 416 | } |
| 417 | |
| 418 | $this->storage->store($this->toArray()); |
| 419 | return $this; |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * @param mixed $data |
| 424 | * @return QtiTimeLine |
| 425 | */ |
| 426 | protected function unserializeTimeLine($data) |
| 427 | { |
| 428 | if (is_array($data)) { |
| 429 | $timeLine = new QtiTimeLine(); |
| 430 | $timeLine->fromArray($data); |
| 431 | } else { |
| 432 | $timeLine = $data; |
| 433 | } |
| 434 | |
| 435 | return $timeLine; |
| 436 | } |
| 437 | |
| 438 | protected function unserializeAdjustmentMap($data) |
| 439 | { |
| 440 | $map = new AdjustmentMap(); |
| 441 | if (is_array($data)) { |
| 442 | $map->fromArray($data); |
| 443 | } elseif ($data instanceof TimerAdjustmentMapInterface) { |
| 444 | $map = $data; |
| 445 | } |
| 446 | |
| 447 | return $map; |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Loads the data from the storage |
| 452 | * @return Timer |
| 453 | * @throws InvalidStorageException |
| 454 | * @throws InvalidDataException |
| 455 | */ |
| 456 | public function load() |
| 457 | { |
| 458 | if (!$this->storage) { |
| 459 | throw new InvalidStorageException('A storage must be defined in order to store the data!'); |
| 460 | } |
| 461 | |
| 462 | $data = $this->storage->load(); |
| 463 | |
| 464 | if (isset($data)) { |
| 465 | if (!is_array($data)) { |
| 466 | $data = [ |
| 467 | QtiTimeStorageFormat::STORAGE_KEY_TIME_LINE => $data, |
| 468 | ]; |
| 469 | } |
| 470 | |
| 471 | if (isset($data[QtiTimeStorageFormat::STORAGE_KEY_TIME_LINE])) { |
| 472 | $this->timeLine = $this->unserializeTimeLine($data[QtiTimeStorageFormat::STORAGE_KEY_TIME_LINE]); |
| 473 | } else { |
| 474 | $this->timeLine = new QtiTimeLine(); |
| 475 | } |
| 476 | |
| 477 | if (isset($data[QtiTimeStorageFormat::STORAGE_KEY_EXTRA_TIME])) { |
| 478 | $this->extraTime = $data[QtiTimeStorageFormat::STORAGE_KEY_EXTRA_TIME]; |
| 479 | } else { |
| 480 | $this->extraTime = 0; |
| 481 | } |
| 482 | |
| 483 | if (isset($data[QtiTimeStorageFormat::STORAGE_KEY_EXTENDED_TIME])) { |
| 484 | $this->extendedTime = $data[QtiTimeStorageFormat::STORAGE_KEY_EXTENDED_TIME]; |
| 485 | } else { |
| 486 | $this->extendedTime = 0; |
| 487 | } |
| 488 | if (isset($data[QtiTimeStorageFormat::STORAGE_KEY_CONSUMED_EXTRA_TIME])) { |
| 489 | $this->consumedExtraTime = $data[QtiTimeStorageFormat::STORAGE_KEY_CONSUMED_EXTRA_TIME]; |
| 490 | } else { |
| 491 | $this->consumedExtraTime = 0; |
| 492 | } |
| 493 | |
| 494 | if (isset($data[QtiTimeStorageFormat::STORAGE_KEY_TIMER_ADJUSTMENT_MAP])) { |
| 495 | $this->adjustmentMap = $this->unserializeAdjustmentMap( |
| 496 | $data[QtiTimeStorageFormat::STORAGE_KEY_TIMER_ADJUSTMENT_MAP] |
| 497 | ); |
| 498 | } else { |
| 499 | $this->adjustmentMap = new AdjustmentMap(); |
| 500 | } |
| 501 | |
| 502 | if (!$this->timeLine instanceof TimeLine) { |
| 503 | throw new InvalidDataException('The storage did not provide acceptable data when loading!'); |
| 504 | } |
| 505 | |
| 506 | if (!$this->timerStrategy) { |
| 507 | throw new InvalidTimerStrategyException('A timer strategy must be defined!'); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | return $this; |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * Gets the added extra time |
| 516 | * @return float |
| 517 | */ |
| 518 | public function getExtraTime() |
| 519 | { |
| 520 | return $this->extraTime; |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * @return float |
| 525 | */ |
| 526 | public function getExtendedTime() |
| 527 | { |
| 528 | return $this->extendedTime; |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * @param $extendedTime |
| 533 | * @return $this |
| 534 | */ |
| 535 | public function setExtendedTime($extendedTime) |
| 536 | { |
| 537 | $this->extendedTime = $extendedTime; |
| 538 | return $this; |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * Sets the added extra time |
| 543 | * @param float $time |
| 544 | * @return $this |
| 545 | */ |
| 546 | public function setExtraTime($time) |
| 547 | { |
| 548 | $this->extraTime = max(0, floatval($time)); |
| 549 | return $this; |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * Sets the added extra time |
| 554 | * @param float $time |
| 555 | * @return $this |
| 556 | */ |
| 557 | public function setConsumedExtraTime($time) |
| 558 | { |
| 559 | $this->consumedExtraTime = max($this->consumedExtraTime, floatval($time)); |
| 560 | return $this; |
| 561 | } |
| 562 | |
| 563 | /** |
| 564 | * Gets the amount of already consumed extra time. If tags are provided, only take care of the related time. |
| 565 | * @param string|array $tags A tag or a list of tags to filter |
| 566 | * @param integer $maxTime initial (total) timer value without extra time |
| 567 | * @param integer $target (server/client) |
| 568 | * @return float |
| 569 | * @throws |
| 570 | */ |
| 571 | public function getConsumedExtraTime($tags = null, $maxTime = 0, $target = TimePoint::TARGET_SERVER) |
| 572 | { |
| 573 | if ($maxTime) { |
| 574 | $totalConsumed = $this->compute($tags, $target); |
| 575 | $consumedExtraTime = $totalConsumed - $maxTime < 0 ? 0 : $totalConsumed - $maxTime; |
| 576 | $this->setConsumedExtraTime($consumedExtraTime)->save(); |
| 577 | } |
| 578 | return $this->consumedExtraTime; |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * Gets the amount of remaining extra time |
| 583 | * @param string|array $tags A tag or a list of tags to filter |
| 584 | * @param integer $maxTime initial (total) timer value without extra time |
| 585 | * @param integer $target (server/client) |
| 586 | * @return float |
| 587 | */ |
| 588 | public function getRemainingExtraTime($tags = null, $maxTime = 0, $target = TimePoint::TARGET_SERVER) |
| 589 | { |
| 590 | return max(0, $this->getExtraTime() - $this->getConsumedExtraTime($tags, $maxTime, $target)); |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * @return AdjustmentMap |
| 595 | */ |
| 596 | public function getAdjustmentMap() |
| 597 | { |
| 598 | return $this->adjustmentMap; |
| 599 | } |
| 600 | |
| 601 | /** |
| 602 | * @inheritdoc |
| 603 | */ |
| 604 | public function delete() |
| 605 | { |
| 606 | $storage = $this->getStorage(); |
| 607 | return $storage->delete(); |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * Checks if a timestamp is consistent with existing TimePoint within a range |
| 612 | * @param array $points |
| 613 | * @param float $timestamp |
| 614 | * @throws InconsistentRangeException |
| 615 | */ |
| 616 | protected function checkTimestampCoherence($points, $timestamp) |
| 617 | { |
| 618 | foreach ($points as $point) { |
| 619 | if ($point->getTimestamp() > $timestamp) { |
| 620 | throw new InconsistentRangeException('A new TimePoint cannot be set before an existing one!'); |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * Check if the provided range is open (START TimePoint and no related END) |
| 627 | * @param array $range |
| 628 | * @return bool |
| 629 | */ |
| 630 | protected function isRangeOpen($range) |
| 631 | { |
| 632 | $nb = count($range); |
| 633 | return $nb && ($nb % 2) && ($range[$nb - 1]->getType() == TimePoint::TYPE_START); |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * Extracts a sorted range of TimePoint |
| 638 | * |
| 639 | * @param array $tags |
| 640 | * @return array |
| 641 | */ |
| 642 | protected function getRange($tags) |
| 643 | { |
| 644 | $range = $this->timeLine->find($tags, TimePoint::TARGET_SERVER); |
| 645 | |
| 646 | TimePoint::sort($range); |
| 647 | |
| 648 | return $range; |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * Checks if a binary flag contains exactly one flag set |
| 653 | * @param $value |
| 654 | * @return bool |
| 655 | */ |
| 656 | protected function onlyOneFlag($value) |
| 657 | { |
| 658 | return $this->binaryPopCount($value) == 1; |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * Count the number of bits set in a 32bits integer |
| 663 | * @param int $value |
| 664 | * @return int |
| 665 | */ |
| 666 | protected function binaryPopCount($value) |
| 667 | { |
| 668 | $value -= (($value >> 1) & 0x55555555); |
| 669 | $value = ((($value >> 2) & 0x33333333) + ($value & 0x33333333)); |
| 670 | $value = ((($value >> 4) + $value) & 0x0f0f0f0f); |
| 671 | $value += ($value >> 8); |
| 672 | $value += ($value >> 16); |
| 673 | return $value & 0x0000003f; |
| 674 | } |
| 675 | } |