Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 129 |
|
0.00% |
0 / 20 |
CRAP | |
0.00% |
0 / 1 |
| QtiTimeLine | |
0.00% |
0 / 129 |
|
0.00% |
0 / 20 |
4422 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| toArray | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| fromArray | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| jsonSerialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| serialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| unserialize | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getPoints | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| add | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| remove | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| clear | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| filter | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| find | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| compute | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
56 | |||
| computeRange | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
90 | |||
| fixRange | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
132 | |||
| cloneTimePoint | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| isStartPoint | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isEndPoint | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getRangeDuration | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| sortRanges | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| 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\ArraySerializable; |
| 28 | use oat\taoTests\models\runner\time\IncompleteRangeException; |
| 29 | use oat\taoTests\models\runner\time\InconsistentRangeException; |
| 30 | use oat\taoTests\models\runner\time\InvalidDataException; |
| 31 | use oat\taoTests\models\runner\time\MalformedRangeException; |
| 32 | use oat\taoTests\models\runner\time\TimeException; |
| 33 | use oat\taoTests\models\runner\time\TimeLine; |
| 34 | use oat\taoTests\models\runner\time\TimePoint; |
| 35 | |
| 36 | /** |
| 37 | * Class QtiTimeLine |
| 38 | * @package oat\taoQtiTest\models\runner\time |
| 39 | */ |
| 40 | class QtiTimeLine implements TimeLine, ArraySerializable, \Serializable, \JsonSerializable |
| 41 | { |
| 42 | /** |
| 43 | * The list of TimePoint representing the TimeLine |
| 44 | * @var array |
| 45 | */ |
| 46 | protected $points = []; |
| 47 | |
| 48 | /** |
| 49 | * QtiTimeLine constructor. |
| 50 | * @param array $points |
| 51 | */ |
| 52 | public function __construct($points = null) |
| 53 | { |
| 54 | if (isset($points)) { |
| 55 | foreach ($points as $point) { |
| 56 | $this->add($point); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Exports the internal state to an array |
| 63 | * @return array |
| 64 | */ |
| 65 | public function toArray() |
| 66 | { |
| 67 | $data = []; |
| 68 | foreach ($this->points as $point) { |
| 69 | $data[] = $point->toArray(); |
| 70 | } |
| 71 | return $data; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Imports the internal state from an array |
| 76 | * @param array $data |
| 77 | */ |
| 78 | public function fromArray($data) |
| 79 | { |
| 80 | $this->points = []; |
| 81 | if (is_array($data)) { |
| 82 | foreach ($data as $dataPoint) { |
| 83 | $point = new TimePoint(); |
| 84 | $point->fromArray($dataPoint); |
| 85 | $this->points[] = $point; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Specify data which should be serialized to JSON |
| 92 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
| 93 | * @return mixed data which can be serialized by <b>json_encode</b>, |
| 94 | * which is a value of any type other than a resource. |
| 95 | * @since 5.4.0 |
| 96 | */ |
| 97 | public function jsonSerialize() |
| 98 | { |
| 99 | return $this->toArray(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * String representation of object |
| 104 | * @link http://php.net/manual/en/serializable.serialize.php |
| 105 | * @return string the string representation of the object or null |
| 106 | * @since 5.1.0 |
| 107 | */ |
| 108 | public function serialize() |
| 109 | { |
| 110 | return serialize($this->points); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Constructs the object |
| 115 | * @link http://php.net/manual/en/serializable.unserialize.php |
| 116 | * @param string $serialized <p> |
| 117 | * The string representation of the object. |
| 118 | * </p> |
| 119 | * @return void |
| 120 | * @since 5.1.0 |
| 121 | * @throws InvalidDataException |
| 122 | */ |
| 123 | public function unserialize($serialized) |
| 124 | { |
| 125 | $this->points = unserialize($serialized); |
| 126 | if (!is_array($this->points)) { |
| 127 | throw new InvalidDataException('The provided serialized data are invalid!'); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Gets the list of TimePoint present in the TimeLine |
| 133 | * @return array |
| 134 | */ |
| 135 | public function getPoints() |
| 136 | { |
| 137 | return $this->points; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | /** |
| 142 | * Adds another TimePoint inside the TimeLine |
| 143 | * @param TimePoint $point |
| 144 | * @return TimeLine |
| 145 | */ |
| 146 | public function add(TimePoint $point) |
| 147 | { |
| 148 | $this->points[] = $point; |
| 149 | return $this; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Removes all TimePoint corresponding to the provided criteria |
| 154 | * @param string|array $tag A tag or a list of tags to filter |
| 155 | * @param int $target The type of target TimePoint to filter |
| 156 | * @param int $type The tyoe of TimePoint to filter |
| 157 | * @return int Returns the number of removed TimePoints |
| 158 | */ |
| 159 | public function remove($tag, $target = TimePoint::TARGET_ALL, $type = TimePoint::TYPE_ALL) |
| 160 | { |
| 161 | $tags = is_array($tag) ? $tag : [$tag]; |
| 162 | $removed = 0; |
| 163 | foreach ($this->points as $idx => $point) { |
| 164 | if ($point->match($tags, $target, $type)) { |
| 165 | unset($this->points[$idx]); |
| 166 | $removed++; |
| 167 | } |
| 168 | } |
| 169 | return $removed; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Clears the TimeLine from all its TimePoint |
| 174 | * @return TimeLine |
| 175 | */ |
| 176 | public function clear() |
| 177 | { |
| 178 | $this->points = []; |
| 179 | return $this; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Gets a filtered TimeLine, containing the TimePoint corresponding to the provided criteria |
| 184 | * @param string|array $tag A tag or a list of tags to filter |
| 185 | * @param int $target The type of target TimePoint to filter |
| 186 | * @param int $type The type of TimePoint to filter |
| 187 | * @return TimeLine Returns a subset corresponding to the found TimePoints |
| 188 | */ |
| 189 | public function filter($tag = null, $target = TimePoint::TARGET_ALL, $type = TimePoint::TYPE_ALL) |
| 190 | { |
| 191 | // the tag criteria can be omitted |
| 192 | $tags = null; |
| 193 | if (isset($tag)) { |
| 194 | $tags = is_array($tag) ? $tag : [$tag]; |
| 195 | } |
| 196 | |
| 197 | // create a another instance of the same class |
| 198 | $subset = new static(); |
| 199 | |
| 200 | // fill the new instance with filtered TimePoint |
| 201 | foreach ($this->points as $idx => $point) { |
| 202 | if ($point->match($tags, $target, $type)) { |
| 203 | $subset->add($point); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | return $subset; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Finds all TimePoint corresponding to the provided criteria |
| 212 | * @param string|array $tag A tag or a list of tags to filter |
| 213 | * @param int $target The type of target TimePoint to filter |
| 214 | * @param int $type The type of TimePoint to filter |
| 215 | * @return array Returns a list of the found TimePoints |
| 216 | */ |
| 217 | public function find($tag = null, $target = TimePoint::TARGET_ALL, $type = TimePoint::TYPE_ALL) |
| 218 | { |
| 219 | // the tag criteria can be omitted |
| 220 | $tags = null; |
| 221 | if (isset($tag)) { |
| 222 | $tags = is_array($tag) ? $tag : [$tag]; |
| 223 | } |
| 224 | |
| 225 | // gather filterer TimePoint |
| 226 | $points = []; |
| 227 | foreach ($this->points as $point) { |
| 228 | if ($point->match($tags, $target, $type)) { |
| 229 | $points [] = $point; |
| 230 | } |
| 231 | } |
| 232 | return $points; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Computes the total duration represented by the filtered TimePoints |
| 237 | * @param string|array $tag A tag or a list of tags to filter |
| 238 | * @param int $target The type of target TimePoint to filter |
| 239 | * @param int $lastTimestamp An optional timestamp that will be utilized to close the last open range, if any |
| 240 | * @return float Returns the total computed duration |
| 241 | * @throws TimeException |
| 242 | */ |
| 243 | public function compute($tag = null, $target = TimePoint::TARGET_ALL, $lastTimestamp = 0) |
| 244 | { |
| 245 | // default value for the last timestamp |
| 246 | if (!$lastTimestamp) { |
| 247 | $lastTimestamp = microtime(true); |
| 248 | } |
| 249 | |
| 250 | // either get all points or only a subset according to the provided criteria |
| 251 | if (!$tag && $target == TimePoint::TARGET_ALL) { |
| 252 | $points = $this->getPoints(); |
| 253 | } else { |
| 254 | $points = $this->find($tag, $target, TimePoint::TYPE_ALL); |
| 255 | } |
| 256 | |
| 257 | // we need a ordered list of points |
| 258 | TimePoint::sort($points); |
| 259 | |
| 260 | // gather points by ranges, relying on the points references |
| 261 | $ranges = []; |
| 262 | foreach ($points as $point) { |
| 263 | $ranges[$point->getRef()][] = $point; |
| 264 | } |
| 265 | |
| 266 | $this->sortRanges($ranges); |
| 267 | |
| 268 | // compute the total duration by summing all gathered ranges |
| 269 | // this loop can throw exceptions |
| 270 | $duration = 0; |
| 271 | foreach ($ranges as $rangeKey => $range) { |
| 272 | $nextTimestamp = $lastTimestamp; |
| 273 | if (isset($ranges[$rangeKey + 1])) { |
| 274 | $nextTimestamp = $ranges[$rangeKey + 1][0]->getTimestamp(); |
| 275 | } |
| 276 | // the last range could be still open, or some range could be malformed due to connection issues... |
| 277 | $range = $this->fixRange($range, $nextTimestamp); |
| 278 | |
| 279 | // compute the duration of the range, an exception may be thrown if the range is malformed |
| 280 | // possible errors are (but should be avoided by the `fixRange()` method): |
| 281 | // - unclosed range: should be autoclosed by fixRange |
| 282 | // - unsorted points or nested/blended ranges: should be corrected by fixRange |
| 283 | $duration += $this->computeRange($range); |
| 284 | } |
| 285 | |
| 286 | return $duration; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Compute the duration of a range of TimePoint |
| 291 | * @param array $range |
| 292 | * @return float |
| 293 | * @throws IncompleteRangeException |
| 294 | * @throws InconsistentRangeException |
| 295 | * @throws MalformedRangeException |
| 296 | */ |
| 297 | protected function computeRange($range) |
| 298 | { |
| 299 | // a range must be built from pairs of TimePoint |
| 300 | if (count($range) % 2) { |
| 301 | throw new IncompleteRangeException(); |
| 302 | } |
| 303 | |
| 304 | $duration = 0; |
| 305 | $start = null; |
| 306 | $end = null; |
| 307 | foreach ($range as $point) { |
| 308 | // grab the START TimePoint |
| 309 | if ($this->isStartPoint($point)) { |
| 310 | // we cannot have the START TimePoint twice |
| 311 | if ($start) { |
| 312 | throw new MalformedRangeException( |
| 313 | 'A time range must be defined by a START and a END TimePoint! Twice START found.' |
| 314 | ); |
| 315 | } |
| 316 | $start = $point; |
| 317 | } |
| 318 | |
| 319 | // grab the END TimePoint |
| 320 | if ($this->isEndPoint($point)) { |
| 321 | // we cannot have the END TimePoint twice |
| 322 | if ($end) { |
| 323 | throw new MalformedRangeException( |
| 324 | 'A time range must be defined by a START and a END TimePoint! Twice END found.' |
| 325 | ); |
| 326 | } |
| 327 | $end = $point; |
| 328 | } |
| 329 | |
| 330 | // when we have got START and END TimePoint, compute the duration |
| 331 | if ($start && $end) { |
| 332 | $duration += $this->getRangeDuration($start, $end); |
| 333 | $start = null; |
| 334 | $end = null; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | return $duration; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Ensures the ranges are well formed. They should have been sorted before, otherwise the process won't work. |
| 343 | * Tries to fix a range by adding missing points |
| 344 | * @param array $range |
| 345 | * @param float $lastTimestamp - An optional timestamp to apply on the last TimePoint if missing |
| 346 | * @return array |
| 347 | */ |
| 348 | protected function fixRange($range, $lastTimestamp = null) |
| 349 | { |
| 350 | $fixedRange = []; |
| 351 | $last = null; |
| 352 | $open = false; |
| 353 | |
| 354 | foreach ($range as $point) { |
| 355 | if ($this->isStartPoint($point)) { // start of range |
| 356 | // the last range could be still open... |
| 357 | if ($last && $open) { |
| 358 | $fixedRange[] = $this->cloneTimePoint($point, TimePoint::TYPE_END); |
| 359 | } |
| 360 | $open = true; |
| 361 | } elseif ($this->isEndPoint($point)) { // end of range |
| 362 | // this range could not be started... |
| 363 | if (!$open) { |
| 364 | $fixedRange[] = $this->cloneTimePoint($last ? $last : $point, TimePoint::TYPE_START); |
| 365 | } |
| 366 | $open = false; |
| 367 | } |
| 368 | $fixedRange[] = $point; |
| 369 | $last = $point; |
| 370 | } |
| 371 | |
| 372 | // the last range could be still open... |
| 373 | if ($last && $open) { |
| 374 | $lastTimestamp = $lastTimestamp < $last->getTimestamp() |
| 375 | ? $last->getTimestamp() |
| 376 | : $lastTimestamp; |
| 377 | $fixedRange[] = $this->cloneTimePoint($last, TimePoint::TYPE_END, $lastTimestamp); |
| 378 | } |
| 379 | |
| 380 | return $fixedRange; |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Makes a copy of a TimePoint and forces a particular type |
| 385 | * @param TimePoint $point - The point to duplicate |
| 386 | * @param int $type - The type of the new point. It should be different! |
| 387 | * @param float $timestamp - An optional timestamp to set on the new point. By default keep the source timestamp. |
| 388 | * @return TimePoint |
| 389 | */ |
| 390 | protected function cloneTimePoint(TimePoint $point, $type, $timestamp = null) |
| 391 | { |
| 392 | if (is_null($timestamp)) { |
| 393 | $timestamp = $point->getTimestamp(); |
| 394 | } |
| 395 | \common_Logger::d("Create missing TimePoint at " . $timestamp); |
| 396 | return new TimePoint($point->getTags(), $timestamp, $type, $point->getTarget()); |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Tells if this is a start TimePoint |
| 401 | * @param TimePoint $point |
| 402 | * @return bool |
| 403 | */ |
| 404 | protected function isStartPoint(TimePoint $point) |
| 405 | { |
| 406 | return $point->match(null, TimePoint::TARGET_ALL, TimePoint::TYPE_START); |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Tells if this is a end TimePoint |
| 411 | * @param TimePoint $point |
| 412 | * @return bool |
| 413 | */ |
| 414 | protected function isEndPoint(TimePoint $point) |
| 415 | { |
| 416 | return $point->match(null, TimePoint::TARGET_ALL, TimePoint::TYPE_END); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Computes the duration between two TimePoint |
| 421 | * @param TimePoint $start |
| 422 | * @param TimePoint $end |
| 423 | * @return float |
| 424 | * @throws InconsistentRangeException |
| 425 | */ |
| 426 | protected function getRangeDuration($start, $end) |
| 427 | { |
| 428 | // the two TimePoint must have the same target to be consistent |
| 429 | if ($start->getTarget() != $end->getTarget()) { |
| 430 | throw new InconsistentRangeException('A time range must be defined by two TimePoint with the same target'); |
| 431 | } |
| 432 | |
| 433 | // the two TimePoint must be correctly ordered |
| 434 | $rangeDuration = $end->getTimestamp() - $start->getTimestamp(); |
| 435 | if ($rangeDuration < 0) { |
| 436 | throw new InconsistentRangeException('A START TimePoint cannot take place after the END!'); |
| 437 | } |
| 438 | |
| 439 | return $rangeDuration; |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * @param array $ranges |
| 444 | * @return array |
| 445 | */ |
| 446 | private function sortRanges(array &$ranges) |
| 447 | { |
| 448 | usort($ranges, function (array $a, array $b) { |
| 449 | if ($a[0]->getTimestamp() === $b[0]->getTimestamp()) { |
| 450 | return 0; |
| 451 | } |
| 452 | return ($a[0]->getTimestamp() < $b[0]->getTimestamp()) ? -1 : 1; |
| 453 | }); |
| 454 | return $ranges; |
| 455 | } |
| 456 | } |