Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
43.59% |
34 / 78 |
|
35.71% |
5 / 14 |
CRAP | |
0.00% |
0 / 1 |
RunnerParamParserTrait | |
43.59% |
34 / 78 |
|
35.71% |
5 / 14 |
241.51 | |
0.00% |
0 / 1 |
hasRequestParameter | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getRequestParameter | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getServiceLocator | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
initServiceContext | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getRunnerService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getServiceContext | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
setServiceContext | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
endItemTimer | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
saveItemState | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
saveItemResponses | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
156 | |||
getItemRef | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
setNavigationContextToCommand | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
setItemContextToCommand | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
setToolsStateContextToCommand | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getItemDuration | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
getItemState | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
getItemResponse | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 |
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 ; |
19 | */ |
20 | |
21 | namespace oat\taoQtiTest\models\runner; |
22 | |
23 | use oat\taoQtiTest\model\Service\ItemContextAwareInterface; |
24 | use oat\taoQtiTest\model\Service\NavigationContextAwareInterface; |
25 | use oat\taoQtiTest\model\Service\ToolsStateAwareInterface; |
26 | use oat\taoQtiTest\models\runner\map\QtiRunnerMap; |
27 | use taoQtiTest_helpers_TestRunnerUtils as TestRunnerUtils; |
28 | |
29 | trait RunnerParamParserTrait |
30 | { |
31 | /** |
32 | * @var QtiRunnerServiceContext |
33 | */ |
34 | protected $serviceContext; |
35 | |
36 | /** |
37 | * Check if the parameter associated to $name exists |
38 | * |
39 | * @param $name |
40 | * @return mixed |
41 | */ |
42 | abstract public function hasRequestParameter($name); |
43 | |
44 | /** |
45 | * Get the parameter associated to $name |
46 | * |
47 | * @param $name |
48 | * @return mixed |
49 | */ |
50 | abstract public function getRequestParameter($name); |
51 | |
52 | /** |
53 | * Get the Service Locator |
54 | * |
55 | * @return mixed |
56 | */ |
57 | abstract public function getServiceLocator(); |
58 | |
59 | /** |
60 | * Initialize and verify the current service context |
61 | * useful when the context was opened but not checked. |
62 | * |
63 | * @return boolean true if initialized |
64 | * @throws \common_Exception |
65 | */ |
66 | protected function initServiceContext() |
67 | { |
68 | $serviceContext = $this->getServiceContext(); |
69 | $this->getRunnerService()->check($serviceContext); |
70 | return $serviceContext->init(); |
71 | } |
72 | |
73 | /** |
74 | * @return QtiRunnerService |
75 | */ |
76 | protected function getRunnerService() |
77 | { |
78 | return $this->getServiceLocator()->get(QtiRunnerService::SERVICE_ID); |
79 | } |
80 | |
81 | /** |
82 | * Get the Service Context |
83 | * WARNING, there is not CSRF token check |
84 | * |
85 | * @return QtiRunnerServiceContext |
86 | * @throws \common_Exception |
87 | */ |
88 | protected function getServiceContext() |
89 | { |
90 | if (!$this->serviceContext) { |
91 | $testExecution = $this->getRequestParameter('serviceCallId'); |
92 | $testDefinition = $this->getRequestParameter('testDefinition'); |
93 | $testCompilation = $this->getRequestParameter('testCompilation'); |
94 | |
95 | $this->serviceContext = $this->getRunnerService()->getServiceContext( |
96 | $testDefinition, |
97 | $testCompilation, |
98 | $testExecution |
99 | ); |
100 | } |
101 | |
102 | return $this->serviceContext; |
103 | } |
104 | |
105 | /** |
106 | * @param QtiRunnerServiceContext $serviceContext |
107 | */ |
108 | public function setServiceContext($serviceContext) |
109 | { |
110 | $this->serviceContext = $serviceContext; |
111 | } |
112 | |
113 | |
114 | /** |
115 | * End the item timer to QtiTimeLine |
116 | |
117 | * @param null $timestamp The start of action, optional |
118 | * @return bool |
119 | */ |
120 | protected function endItemTimer($timestamp = null) |
121 | { |
122 | if ($this->hasRequestParameter('itemDuration')) { |
123 | $serviceContext = $this->getServiceContext(); |
124 | $itemDuration = $this->getRequestParameter('itemDuration'); |
125 | return $this->getRunnerService()->endTimer($serviceContext, $itemDuration, $timestamp); |
126 | } |
127 | return false; |
128 | } |
129 | |
130 | /** |
131 | * Save the actual item state. |
132 | * Requires params itemDefinition and itemState |
133 | * |
134 | * @return boolean true if saved |
135 | * @throws \common_Exception |
136 | */ |
137 | protected function saveItemState() |
138 | { |
139 | if ($this->getRequestParameter('itemDefinition') && $this->getRequestParameter('itemState')) { |
140 | $serviceContext = $this->getServiceContext(); |
141 | $itemIdentifier = $this->getRequestParameter('itemDefinition'); |
142 | $state = $this->getRequestParameter('itemState') |
143 | ? json_decode($this->getRequestParameter('itemState'), true) |
144 | : new \stdClass(); |
145 | |
146 | return $this->getRunnerService()->setItemState($serviceContext, $itemIdentifier, $state); |
147 | } |
148 | |
149 | return false; |
150 | } |
151 | |
152 | /** |
153 | * Save the item responses |
154 | * Requires params itemDuration and optionally consumedExtraTime |
155 | * |
156 | * @param boolean $emptyAllowed if we allow empty responses |
157 | * @return boolean true if saved |
158 | * @throws \common_Exception |
159 | * @throws QtiRunnerEmptyResponsesException if responses are empty, emptyAllowed is false and no allowSkipping |
160 | */ |
161 | protected function saveItemResponses($emptyAllowed = true) |
162 | { |
163 | if ($this->getRequestParameter('itemDefinition') && $this->getRequestParameter('itemResponse')) { |
164 | $itemDefinition = $this->getRequestParameter('itemDefinition'); |
165 | $serviceContext = $this->getServiceContext(); |
166 | $itemResponse = $this->getRequestParameter('itemResponse') |
167 | ? json_decode($this->getRequestParameter('itemResponse'), true) |
168 | : null; |
169 | |
170 | $currentAssessmentItemRef = $serviceContext->getCurrentAssessmentItemRef(); |
171 | if ( |
172 | $currentAssessmentItemRef |
173 | && $serviceContext->getCurrentAssessmentItemRef()->getIdentifier() !== $itemDefinition |
174 | ) { |
175 | throw new QtiRunnerItemResponseException(__('Item response identifier does not match current item')); |
176 | } |
177 | |
178 | if (!is_null($itemResponse) && !empty($itemDefinition)) { |
179 | $responses = $this |
180 | ->getRunnerService() |
181 | ->parsesItemResponse($serviceContext, $itemDefinition, $itemResponse); |
182 | |
183 | //still verify allowSkipping & empty responses |
184 | if ( |
185 | !$emptyAllowed |
186 | && $this->getRunnerService()->getTestConfig()->getConfigValue('enableAllowSkipping') |
187 | && !TestRunnerUtils::doesAllowSkipping($serviceContext->getTestSession()) |
188 | ) { |
189 | if ($this->getRunnerService()->emptyResponse($serviceContext, $responses)) { |
190 | throw new QtiRunnerEmptyResponsesException(); |
191 | } |
192 | } |
193 | |
194 | return $this->getRunnerService()->storeItemResponse($serviceContext, $itemDefinition, $responses); |
195 | } |
196 | } |
197 | return false; |
198 | } |
199 | |
200 | /** |
201 | * Gets the item reference for the current itemRef |
202 | * |
203 | * @param string $itemIdentifier the item id |
204 | * @return string the state id |
205 | */ |
206 | protected function getItemRef($itemIdentifier) |
207 | { |
208 | $serviceContext = $this->getServiceContext(); |
209 | /** @var QtiRunnerMap $mapService */ |
210 | $mapService = $this->getServiceLocator()->get(QtiRunnerMap::SERVICE_ID); |
211 | return $mapService->getItemHref($serviceContext, $itemIdentifier); |
212 | } |
213 | |
214 | protected function setNavigationContextToCommand(NavigationContextAwareInterface $command): void |
215 | { |
216 | $command->setNavigationContext( |
217 | $this->getRequestParameter('direction'), |
218 | $this->getRequestParameter('scope'), |
219 | $this->getRequestParameter('ref') |
220 | ); |
221 | } |
222 | |
223 | protected function setItemContextToCommand(ItemContextAwareInterface $command): void |
224 | { |
225 | if (empty($this->getRequestParameter('itemDefinition'))) { |
226 | return; |
227 | } |
228 | |
229 | $command->setItemContext( |
230 | $this->getRequestParameter('itemDefinition'), |
231 | $this->getItemState(), |
232 | $this->getItemDuration(), |
233 | $this->getItemResponse(), |
234 | $this->getTime() |
235 | ); |
236 | } |
237 | |
238 | protected function setToolsStateContextToCommand(ToolsStateAwareInterface $command): void |
239 | { |
240 | $command->setToolsState($this->getToolStatesFromRequest()); |
241 | } |
242 | |
243 | private function getItemDuration(): ?float |
244 | { |
245 | if (!$this->hasRequestParameter('itemDuration')) { |
246 | return null; |
247 | } |
248 | |
249 | return (float)$this->getRequestParameter('itemDuration'); |
250 | } |
251 | |
252 | private function getItemState(): ?array |
253 | { |
254 | $itemState = $this->getRequestParameter('itemState'); |
255 | |
256 | if (empty($itemState)) { |
257 | return null; |
258 | } |
259 | |
260 | return (array)json_decode($itemState, true); |
261 | } |
262 | |
263 | private function getItemResponse(): ?array |
264 | { |
265 | $itemResponse = $this->getRequestParameter('itemResponse'); |
266 | |
267 | if (empty($itemResponse)) { |
268 | return null; |
269 | } |
270 | |
271 | return (array)json_decode($itemResponse, true); |
272 | } |
273 | } |