Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 78 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
ResponseVariableFormatter | |
0.00% |
0 / 78 |
|
0.00% |
0 / 5 |
1482 | |
0.00% |
0 / 1 |
trimIdentifier | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
formatStringValue | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
240 | |||
formatVariableToPci | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
182 | |||
formatStructuredVariablesToItemState | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
72 | |||
formatVariableFile | |
0.00% |
0 / 3 |
|
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) 2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
19 | * |
20 | */ |
21 | |
22 | namespace oat\taoOutcomeUi\helper; |
23 | |
24 | use taoResultServer_models_classes_ResponseVariable as ResponseVariable; |
25 | |
26 | /** |
27 | * The ResponseVariableFormatter enables you to format the output of the ResultsService into an associative compatible |
28 | * with the client code |
29 | * Class ResponseVariableFormatter |
30 | * @package oat\taoOutcomeUi\helper |
31 | */ |
32 | class ResponseVariableFormatter |
33 | { |
34 | /** |
35 | * Special trim for identifier in response variables |
36 | * @param $identifierString |
37 | * @return string |
38 | */ |
39 | private static function trimIdentifier($identifierString) |
40 | { |
41 | return trim($identifierString, " \t\n\r\0\x0B'\""); |
42 | } |
43 | |
44 | /** |
45 | * Format a string into the appropriate format according to the base type |
46 | * @param $baseType |
47 | * @param $stringValue |
48 | * @return array|bool|float|int|string |
49 | * @throws \common_Exception |
50 | */ |
51 | private static function formatStringValue($baseType, $stringValue) |
52 | { |
53 | |
54 | switch (strtolower($baseType)) { |
55 | case 'string': |
56 | case 'duration': |
57 | return $stringValue; |
58 | case 'identifier': |
59 | return self::trimIdentifier($stringValue); |
60 | case 'integer': |
61 | return intval($stringValue); |
62 | case 'float': |
63 | return floatval($stringValue); |
64 | case 'boolean': |
65 | return (trim($stringValue) === 'true' || $stringValue === true) ? true : false; |
66 | case 'pair': |
67 | case 'directedpair': |
68 | $pair = explode(' ', trim($stringValue)); |
69 | if (count($pair) != 2) { |
70 | throw new \common_Exception('invalid pair string'); |
71 | } |
72 | return [self::trimIdentifier($pair[0]), self::trimIdentifier($pair[1])]; |
73 | case 'point': |
74 | $pair = explode(' ', trim($stringValue)); |
75 | if (count($pair) != 2) { |
76 | throw new \common_Exception('invalid point string'); |
77 | } |
78 | return [intval($pair[0]), intval($pair[1])]; |
79 | default: |
80 | throw new \common_exception_NotImplemented('unknown basetype'); |
81 | } |
82 | } |
83 | |
84 | /** |
85 | * Format a ResponseVariable into a associative array, directly usable on the client side. |
86 | * |
87 | * @param ResponseVariable $var |
88 | * @return array |
89 | * @throws \common_Exception |
90 | */ |
91 | public static function formatVariableToPci(ResponseVariable $var) |
92 | { |
93 | $value = $var->getValue(); |
94 | switch ($var->getCardinality()) { |
95 | case 'record': |
96 | case 'single': |
97 | if (strlen($value) === 0) { |
98 | $formatted = ['base' => null]; |
99 | } else { |
100 | try { |
101 | $formatted = [ |
102 | 'base' => [$var->getBaseType() => self::formatStringValue($var->getBaseType(), $value)] |
103 | ]; |
104 | } catch (\common_exception_NotImplemented $e) { |
105 | // simply ignore unsupported data/type |
106 | $formatted = ['base' => null]; |
107 | } |
108 | } |
109 | break; |
110 | case 'ordered': |
111 | case 'multiple': |
112 | $list = []; |
113 | |
114 | if (!empty($value) && preg_match('/^\s*[\[|<](.*)[\]>]\s*$/', $value, $content)) { |
115 | $matches = explode(';', $content[1]); |
116 | foreach ($matches as $valueString) { |
117 | if (empty(trim($valueString))) { |
118 | continue; |
119 | } |
120 | |
121 | try { |
122 | $list[] = self::formatStringValue($var->getBaseType(), trim($valueString, " '")); |
123 | } catch (\common_exception_NotImplemented $e) { |
124 | // simply ignore unsupported data/type |
125 | } |
126 | } |
127 | } |
128 | |
129 | $formatted = ['list' => [$var->getBaseType() => $list]]; |
130 | break; |
131 | default: |
132 | throw new \common_Exception('unknown response cardinality'); |
133 | } |
134 | return $formatted; |
135 | } |
136 | |
137 | /** |
138 | * Format the output of oat\taoOutcomeUi\model\ResultsService::getStructuredVariables() into a client usable array |
139 | * |
140 | * @param array $testResultVariables - the array output from |
141 | * oat\taoOutcomeUi\model\ResultsService::getStructuredVariables(); |
142 | * @param array $itemFilter = [] - the array of item uri to be included in the formatted output, all item if empty. |
143 | * @return array |
144 | * @throws \common_Exception |
145 | */ |
146 | public static function formatStructuredVariablesToItemState( |
147 | array $testResultVariables, |
148 | array $itemFilter = [] |
149 | ): array { |
150 | |
151 | $formatted = []; |
152 | foreach ($testResultVariables as $itemKey => $itemResult) { |
153 | if (!isset($itemResult['uri'])) { |
154 | continue; |
155 | } |
156 | |
157 | if (!empty($itemFilter) && !in_array($itemResult['uri'], $itemFilter)) { |
158 | continue; |
159 | } |
160 | |
161 | $itemResults = []; |
162 | foreach ($itemResult['taoResultServer_models_classes_ResponseVariable'] as $var) { |
163 | /** @var $responseVariable ResponseVariable */ |
164 | $responseVariable = $var['var']; |
165 | |
166 | if ($responseVariable->getBaseType() === 'file') { |
167 | $itemResults[$responseVariable->getIdentifier()] = [ |
168 | 'response' => ['base' => ['file' => self::formatVariableFile($responseVariable)]] |
169 | ]; |
170 | } elseif ($responseVariable->getCardinality() === 'record') { |
171 | $itemResults[$responseVariable->getIdentifier()] = [ |
172 | 'response' => json_decode($responseVariable->getValue(), true) |
173 | ?? ['base' => ''] |
174 | ]; |
175 | } else { |
176 | $itemResults[$responseVariable->getIdentifier()] = [ |
177 | 'response' => self::formatVariableToPci($responseVariable) |
178 | ]; |
179 | } |
180 | } |
181 | |
182 | $formatted[$itemKey][$itemResult['attempt']] = $itemResults; |
183 | } |
184 | |
185 | return $formatted; |
186 | } |
187 | |
188 | private static function formatVariableFile(ResponseVariable $responseVariable): array |
189 | { |
190 | $file = Datatypes::decodeFile($responseVariable->getCandidateResponse()); |
191 | |
192 | $file['data'] = base64_encode($file['data']); |
193 | |
194 | return $file; |
195 | } |
196 | } |