Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
1.08% |
1 / 93 |
|
6.67% |
1 / 15 |
CRAP | |
0.00% |
0 / 1 |
TemplatesDriven | |
1.08% |
1 / 93 |
|
6.67% |
1 / 15 |
901.28 | |
0.00% |
0 / 1 |
getRule | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isSupportedTemplate | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
create | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
takeOverFrom | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
setTemplate | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getTemplate | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
takeNoticeOfAddedInteraction | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
takeNoticeOfRemovedInteraction | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getForm | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
buildQTI | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
20 | |||
getResponseProcessingTemplate | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
toQTI | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
convertToTemplate | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
toArray | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
20 | |||
getUsedAttributes | |
100.00% |
1 / 1 |
|
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) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
19 | * |
20 | */ |
21 | |
22 | namespace oat\taoQtiItem\model\qti\response; |
23 | |
24 | use oat\taoQtiItem\model\qti\response\ResponseProcessing; |
25 | use oat\taoQtiItem\model\qti\response\Rule; |
26 | use oat\taoQtiItem\model\qti\response\Template; |
27 | use oat\taoQtiItem\model\qti\Item; |
28 | use oat\taoQtiItem\model\qti\OutcomeDeclaration; |
29 | use oat\taoQtiItem\model\qti\response\TakeoverFailedException; |
30 | use oat\taoQtiItem\model\qti\ResponseDeclaration; |
31 | use oat\taoQtiItem\model\qti\interaction\Interaction; |
32 | use oat\taoQtiItem\controller\QTIform\TemplatesDrivenResponseOptions; |
33 | use oat\taoQtiItem\helpers\QtiSerializer; |
34 | use common_exception_Error; |
35 | use taoItems_models_classes_TemplateRenderer; |
36 | |
37 | /** |
38 | * Response processing similar to the QTI templates, but with |
39 | * support for multiple interactions |
40 | * |
41 | * @access public |
42 | * @author Joel Bout, <joel.bout@tudor.lu> |
43 | * @package taoQTI |
44 | */ |
45 | class TemplatesDriven extends ResponseProcessing implements Rule |
46 | { |
47 | /** |
48 | * Short description of method getRule |
49 | * |
50 | * @deprecated now using new qtism lib for response processing |
51 | * @access public |
52 | * @author Joel Bout, <joel.bout@tudor.lu> |
53 | * @return string |
54 | */ |
55 | public function getRule() |
56 | { |
57 | throw new common_exception_Error('please use buildRule for templateDriven instead'); |
58 | } |
59 | |
60 | /** |
61 | * Short description of method isSupportedTemplate |
62 | * |
63 | * @access public |
64 | * @author Joel Bout, <joel.bout@tudor.lu> |
65 | * @param string uri |
66 | * @deprecated |
67 | * @return taoQTI_models_classes_Matching_bool |
68 | */ |
69 | public static function isSupportedTemplate($uri) |
70 | { |
71 | $mythoMap = [ |
72 | Template::MATCH_CORRECT, |
73 | Template::MAP_RESPONSE, |
74 | Template::MAP_RESPONSE_POINT, |
75 | Template::NONE |
76 | ]; |
77 | |
78 | $returnValue = in_array($uri, $mythoMap); |
79 | |
80 | return (bool) $returnValue; |
81 | } |
82 | |
83 | /** |
84 | * Short description of method create |
85 | * |
86 | * @access public |
87 | * @author Joel Bout, <joel.bout@tudor.lu> |
88 | * @param |
89 | * Item item |
90 | * @return oat\taoQtiItem\model\qti\response\ResponseProcessing |
91 | */ |
92 | public static function create(Item $item) |
93 | { |
94 | $returnValue = new TemplatesDriven(); |
95 | if (count($item->getOutcomes()) == 0) { |
96 | $item->setOutcomes([ |
97 | new OutcomeDeclaration([ |
98 | 'identifier' => 'SCORE', |
99 | 'baseType' => 'float', |
100 | 'cardinality' => 'single' |
101 | ]) |
102 | ]); |
103 | } |
104 | foreach ($item->getInteractions() as $interaction) { |
105 | $returnValue->setTemplate($interaction->getResponse(), Template::MATCH_CORRECT); |
106 | } |
107 | |
108 | return $returnValue; |
109 | } |
110 | |
111 | /** |
112 | * Short description of method takeOverFrom |
113 | * |
114 | * @access public |
115 | * @author Joel Bout, <joel.bout@tudor.lu> |
116 | * @param |
117 | * ResponseProcessing responseProcessing |
118 | * @param |
119 | * Item item |
120 | * @return oat\taoQtiItem\model\qti\response\ResponseProcessing |
121 | */ |
122 | public static function takeOverFrom(ResponseProcessing $responseProcessing, Item $item) |
123 | { |
124 | $returnValue = null; |
125 | |
126 | if ($responseProcessing instanceof self) { |
127 | return $responseProcessing; |
128 | } |
129 | |
130 | if ($responseProcessing instanceof Template) { |
131 | $returnValue = new TemplatesDriven(); |
132 | // theoretic only interaction 'RESPONSE' should be considered |
133 | foreach ($item->getInteractions() as $interaction) { |
134 | $returnValue->setTemplate($interaction->getResponse(), $responseProcessing->getUri()); |
135 | } |
136 | $returnValue->setRelatedItem($item); |
137 | } else { |
138 | throw new TakeoverFailedException(); |
139 | } |
140 | |
141 | return $returnValue; |
142 | } |
143 | |
144 | /** |
145 | * Short description of method setTemplate |
146 | * |
147 | * @access public |
148 | * @author Joel Bout, <joel.bout@tudor.lu> |
149 | * @param |
150 | * Response response |
151 | * @param |
152 | * string template |
153 | * @return boolean |
154 | */ |
155 | public function setTemplate(ResponseDeclaration $response, $template) |
156 | { |
157 | $response->setHowMatch($template); |
158 | $returnValue = true; |
159 | |
160 | return (bool) $returnValue; |
161 | } |
162 | |
163 | /** |
164 | * Short description of method getTemplate |
165 | * |
166 | * @access public |
167 | * @author Joel Bout, <joel.bout@tudor.lu> |
168 | * @param |
169 | * Response response |
170 | * @return string |
171 | */ |
172 | public function getTemplate(ResponseDeclaration $response) |
173 | { |
174 | return (string) $response->getHowMatch(); |
175 | } |
176 | |
177 | /** |
178 | * Short description of method takeNoticeOfAddedInteraction |
179 | * |
180 | * @access public |
181 | * @author Joel Bout, <joel.bout@tudor.lu> |
182 | * @param |
183 | * Interaction interaction |
184 | * @param |
185 | * Item item |
186 | * @return mixed |
187 | */ |
188 | public function takeNoticeOfAddedInteraction(Interaction $interaction, Item $item) |
189 | { |
190 | $interaction->getResponse()->setHowMatch(Template::MATCH_CORRECT); |
191 | } |
192 | |
193 | /** |
194 | * Short description of method takeNoticeOfRemovedInteraction |
195 | * |
196 | * @access public |
197 | * @author Joel Bout, <joel.bout@tudor.lu> |
198 | * @param |
199 | * Interaction interaction |
200 | * @param |
201 | * Item item |
202 | * @return mixed |
203 | */ |
204 | public function takeNoticeOfRemovedInteraction(Interaction $interaction, Item $item) |
205 | { |
206 | } |
207 | |
208 | /** |
209 | * Short description of method getForm |
210 | * |
211 | * @access public |
212 | * @author Joel Bout, <joel.bout@tudor.lu> |
213 | * @param |
214 | * Response response |
215 | * @return tao_helpers_form_Form |
216 | */ |
217 | public function getForm(ResponseDeclaration $response) |
218 | { |
219 | $formContainer = new TemplatesDrivenResponseOptions($this, $response); |
220 | $returnValue = $formContainer->getForm(); |
221 | |
222 | return $returnValue; |
223 | } |
224 | |
225 | /** |
226 | * Short description of method buildQTI |
227 | * |
228 | * @access public |
229 | * @author Joel Bout, <joel.bout@tudor.lu> |
230 | * @return string |
231 | */ |
232 | public function buildQTI() |
233 | { |
234 | $template = $this->convertToTemplate(); |
235 | if (! is_null($template)) { |
236 | // if the TemplateDriven rp is convertible to a Template, render that template |
237 | return $template->toQTI(); |
238 | } |
239 | |
240 | $returnValue = "<responseProcessing>"; |
241 | $interactions = $this->getRelatedItem()->getInteractions(); |
242 | |
243 | foreach ($interactions as $interaction) { |
244 | $response = $interaction->getResponse(); |
245 | $uri = $response->getHowMatch(); |
246 | $matchingTemplate = $this->getResponseProcessingTemplate($uri); |
247 | $tplRenderer = new taoItems_models_classes_TemplateRenderer($matchingTemplate, [ |
248 | 'responseIdentifier' => $response->getIdentifier(), |
249 | 'outcomeIdentifier' => 'SCORE' |
250 | ]); |
251 | $returnValue .= $tplRenderer->render(); |
252 | |
253 | // add simple feedback rules: |
254 | foreach ($response->getFeedbackRules() as $rule) { |
255 | $returnValue .= $rule->toQTI(); |
256 | } |
257 | } |
258 | $returnValue .= "</responseProcessing>"; |
259 | |
260 | return (string) $returnValue; |
261 | } |
262 | |
263 | protected function getResponseProcessingTemplate($templateUri) |
264 | { |
265 | if ($templateUri === Template::NONE) { |
266 | $matchingTemplate = dirname(__FILE__) . '/rpTemplate/qti.none.tpl.php'; |
267 | } else { |
268 | $templateName = substr($templateUri, strrpos($templateUri, '/') + 1); |
269 | $matchingTemplate = dirname(__FILE__) . '/rpTemplate/qti.' . $templateName . '.tpl.php'; |
270 | } |
271 | return $matchingTemplate; |
272 | } |
273 | |
274 | /** |
275 | * Short description of method toQTI |
276 | * |
277 | * @access public |
278 | * @author Joel Bout, <joel.bout@tudor.lu> |
279 | * @return string |
280 | */ |
281 | public function toQTI() |
282 | { |
283 | throw new common_exception_Error('please use buildQTI for templateDriven instead'); |
284 | } |
285 | |
286 | /** |
287 | * Convert the TemplateDriven instance into a Template instance if it is possible |
288 | * Returns null otherwise. |
289 | * |
290 | * @return \oat\taoQtiItem\model\qti\response\Template |
291 | */ |
292 | protected function convertToTemplate() |
293 | { |
294 | $returnValue = null; |
295 | $interactions = $this->getRelatedItem()->getInteractions(); |
296 | |
297 | if (count($interactions) == 1) { |
298 | $interaction = reset($interactions); |
299 | // check if the unique interaction has the right responseIdentifier RESPONSE |
300 | if ($interaction->attr('responseIdentifier') === 'RESPONSE') { |
301 | $response = $interaction->getResponse(); |
302 | if (count($response->getFeedbackRules()) == 0) { |
303 | $uri = $response->getHowMatch(); |
304 | $returnValue = new Template($uri); |
305 | } |
306 | } |
307 | } |
308 | return $returnValue; |
309 | } |
310 | |
311 | public function toArray($filterVariableContent = false, &$filtered = []) |
312 | { |
313 | $returnValue = parent::toArray($filterVariableContent, $filtered); |
314 | $rp = null; |
315 | $responseRules = []; |
316 | $template = $this->convertToTemplate(); |
317 | |
318 | if (is_null($template)) { |
319 | // cannot be converted into a Template instance, so build the rp from the current instance |
320 | $rp = $this->buildQTI(); |
321 | } else { |
322 | // can be converted into a Template instance, so get the Template content |
323 | $rp = $template->getTemplateContent(); |
324 | } |
325 | if (!empty(trim($rp))) { |
326 | $rpSerialized = QtiSerializer::parseResponseProcessingXml(simplexml_load_string($rp)); |
327 | $responseRules = $rpSerialized['responseRules']; |
328 | } |
329 | |
330 | $protectedData = [ |
331 | 'processingType' => 'templateDriven', |
332 | 'responseRules' => $responseRules |
333 | ]; |
334 | |
335 | if ($filterVariableContent) { |
336 | $filtered[$this->getSerial()] = $protectedData; |
337 | } else { |
338 | $returnValue = array_merge($returnValue, $protectedData); |
339 | } |
340 | |
341 | return $returnValue; |
342 | } |
343 | |
344 | protected function getUsedAttributes() |
345 | { |
346 | return []; |
347 | } |
348 | } |