Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ItemResponseVariableSplitter
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
3 / 3
15
100.00% covered (success)
100.00%
1 / 1
 splitByAttempt
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
7
 splitObjByAttempt
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
7
 getVariableTime
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
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) 2020 (original work) Open Assessment Technologies SA
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoResultServer\models\Formatter;
24
25use oat\oatbox\service\ConfigurableService;
26
27class ItemResponseVariableSplitter extends ConfigurableService
28{
29    public function splitByAttempt(array $itemVariables): array
30    {
31        $attempts = [];
32
33        foreach ($itemVariables as $variable) {
34            if ($variable['identifier'] == 'numAttempts') {
35                $attempts[(string)$this->getVariableTime($variable['epoch'])] = [];
36            }
37        }
38
39        foreach ($itemVariables as $variable) {
40            $cand = null;
41            $bestDist = null;
42
43            foreach (array_keys($attempts) as $time) {
44                $dist = abs($time - $this->getVariableTime($variable['epoch']));
45
46                if (is_null($bestDist) || $dist < $bestDist) {
47                    $bestDist = $dist;
48                    $cand = $time;
49                }
50            }
51            $attempts[$cand][] = $variable;
52        }
53
54        return $attempts;
55    }
56
57    public function splitObjByAttempt(array $itemVariables): array
58    {
59        $attempts = [];
60
61        foreach ($itemVariables as $variable) {
62            if ($variable->variable->getIdentifier() == 'numAttempts') {
63                $attempts[(string)$variable->variable->getCreationTime()] = [];
64            }
65        }
66        foreach ($itemVariables as $variable) {
67            $cand = null;
68            $bestDist = null;
69
70            foreach (array_keys($attempts) as $time) {
71                $dist = abs($time - $variable->variable->getCreationTime());
72
73                if (is_null($bestDist) || $dist < $bestDist) {
74                    $bestDist = $dist;
75                    $cand = $time;
76                }
77            }
78            $attempts[$cand][] = $variable;
79        }
80
81        return $attempts;
82    }
83
84
85    private function getVariableTime(string $epoch): float
86    {
87        [$usec, $sec] = explode(' ', $epoch);
88
89        return ((float)$usec + (float)$sec);
90    }
91}