Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 113 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
| Composite | |
0.00% |
0 / 113 |
|
0.00% |
0 / 12 |
1406 | |
0.00% |
0 / 1 |
| getRule | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| __construct | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
20 | |||
| create | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| takeOverFrom | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
72 | |||
| add | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getInteractionResponseProcessing | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| getIRPByOutcome | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| replace | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| toQTI | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| takeNoticeOfAddedInteraction | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| takeNoticeOfRemovedInteraction | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| getForm | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getCompositionQTI | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getCompositionRules | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 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 | |
| 23 | namespace oat\taoQtiItem\model\qti\response; |
| 24 | |
| 25 | use oat\taoQtiItem\model\qti\response\Composite; |
| 26 | use oat\taoQtiItem\model\qti\response\ResponseProcessing; |
| 27 | use oat\taoQtiItem\model\qti\response\Rule; |
| 28 | use oat\taoQtiItem\model\qti\Item; |
| 29 | use oat\taoQtiItem\model\qti\OutcomeDeclaration; |
| 30 | use oat\taoQtiItem\model\qti\response\Summation; |
| 31 | use oat\taoQtiItem\model\qti\response\interactionResponseProcessing\InteractionResponseProcessing; |
| 32 | use oat\taoQtiItem\model\qti\response\interactionResponseProcessing\None; |
| 33 | use oat\taoQtiItem\model\qti\response\Template; |
| 34 | use oat\taoQtiItem\model\qti\response\interactionResponseProcessing\Template as IrpTemplate; |
| 35 | use oat\taoQtiItem\model\qti\response\TakeoverFailedException; |
| 36 | use oat\taoQtiItem\model\qti\response\TemplatesDriven; |
| 37 | use oat\taoQtiItem\model\qti\ResponseDeclaration; |
| 38 | use oat\taoQtiItem\model\qti\interaction\Interaction; |
| 39 | use oat\taoQtiItem\model\qti\response\interactionResponseProcessing\MatchCorrectTemplate; |
| 40 | use oat\taoQtiItem\controller\QTIform\CompositeResponseOptions; |
| 41 | use Exception; |
| 42 | use common_Logger; |
| 43 | use common_Exception; |
| 44 | |
| 45 | /** |
| 46 | * Short description of class oat\taoQtiItem\model\qti\response\Composite |
| 47 | * |
| 48 | * @abstract |
| 49 | * @access public |
| 50 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 51 | * @package taoQTI |
| 52 | |
| 53 | */ |
| 54 | abstract class Composite extends ResponseProcessing implements Rule |
| 55 | { |
| 56 | /** |
| 57 | * Short description of attribute components |
| 58 | * |
| 59 | * @access protected |
| 60 | * @var array |
| 61 | */ |
| 62 | protected $components = []; |
| 63 | |
| 64 | /** |
| 65 | * Short description of attribute outcomeIdentifier |
| 66 | * |
| 67 | * @access protected |
| 68 | * @var string |
| 69 | */ |
| 70 | protected $outcomeIdentifier = ''; |
| 71 | |
| 72 | /** |
| 73 | * Short description of method getRule |
| 74 | * |
| 75 | * @access public |
| 76 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 77 | * @return string |
| 78 | */ |
| 79 | public function getRule() |
| 80 | { |
| 81 | $returnValue = (string) ''; |
| 82 | |
| 83 | foreach ($this->components as $irp) { |
| 84 | $returnValue .= $irp->getRule(); |
| 85 | } |
| 86 | foreach ($this->getCompositionRules() as $rule) { |
| 87 | $returnValue .= $rule->getRule(); |
| 88 | } |
| 89 | |
| 90 | return (string) $returnValue; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Short description of method __construct |
| 95 | * |
| 96 | * @access public |
| 97 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 98 | * @param Item item |
| 99 | * @param string outcomeIdentifier |
| 100 | * @return mixed |
| 101 | */ |
| 102 | public function __construct(Item $item, $outcomeIdentifier = 'SCORE') |
| 103 | { |
| 104 | parent::__construct(); |
| 105 | $this->outcomeIdentifier = $outcomeIdentifier; |
| 106 | $outcomeExists = false; |
| 107 | foreach ($item->getOutcomes() as $outcome) { |
| 108 | if ($outcome->getIdentifier() == $outcomeIdentifier) { |
| 109 | $outcomeExists = true; |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | if (!$outcomeExists) { |
| 114 | $outcomes = $item->getOutcomes(); |
| 115 | $outcomes[] = new OutcomeDeclaration([ |
| 116 | 'identifier' => $outcomeIdentifier, |
| 117 | 'baseType' => 'float', |
| 118 | 'cardinality' => 'single', |
| 119 | ]); |
| 120 | $item->setOutcomes($outcomes); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Short description of method create |
| 126 | * |
| 127 | * @access public |
| 128 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 129 | * @param Item item |
| 130 | * @return oat\taoQtiItem\model\qti\response\ResponseProcessing |
| 131 | */ |
| 132 | public static function create(Item $item) |
| 133 | { |
| 134 | $returnValue = new Summation($item); |
| 135 | foreach ($item->getInteractions() as $interaction) { |
| 136 | $irp = InteractionResponseProcessing::create( |
| 137 | None::CLASS_ID, |
| 138 | $interaction->getResponse(), |
| 139 | $item |
| 140 | ); |
| 141 | $returnValue->add($irp, $item); |
| 142 | } |
| 143 | |
| 144 | return $returnValue; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Short description of method takeOverFrom |
| 149 | * |
| 150 | * @access public |
| 151 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 152 | * @param ResponseProcessing responseProcessing |
| 153 | * @param Item item |
| 154 | * @return oat\taoQtiItem\model\qti\response\Composite |
| 155 | */ |
| 156 | public static function takeOverFrom(ResponseProcessing $responseProcessing, Item $item) |
| 157 | { |
| 158 | $returnValue = null; |
| 159 | |
| 160 | if ($responseProcessing instanceof static) { |
| 161 | // already good |
| 162 | $returnValue = $responseProcessing; |
| 163 | } elseif ($responseProcessing instanceof Template) { |
| 164 | // IMS Template |
| 165 | $rp = new Summation($item, 'SCORE'); |
| 166 | foreach ($item->getInteractions() as $interaction) { |
| 167 | $response = $interaction->getResponse(); |
| 168 | try { |
| 169 | $irp = IrpTemplate::createByTemplate( |
| 170 | $responseProcessing->getUri(), |
| 171 | $response, |
| 172 | $item |
| 173 | ); |
| 174 | } catch (Exception $e) { |
| 175 | throw new TakeoverFailedException(); |
| 176 | } |
| 177 | $rp->add($irp, $item); |
| 178 | } |
| 179 | $returnValue = $rp; |
| 180 | } elseif ($responseProcessing instanceof TemplatesDriven) { |
| 181 | // TemplateDriven |
| 182 | $rp = new Summation($item, 'SCORE'); |
| 183 | foreach ($item->getInteractions() as $interaction) { |
| 184 | $response = $interaction->getResponse(); |
| 185 | try { |
| 186 | $irp = IrpTemplate::createByTemplate( |
| 187 | $responseProcessing->getTemplate($response), |
| 188 | $response, |
| 189 | $item |
| 190 | ); |
| 191 | } catch (Exception $e) { |
| 192 | throw new TakeoverFailedException(); |
| 193 | } |
| 194 | $rp->add($irp, $item); |
| 195 | } |
| 196 | $returnValue = $rp; |
| 197 | } else { |
| 198 | common_Logger::d( |
| 199 | 'Composite ResponseProcessing can not takeover from ' . get_class($responseProcessing) . ' yet' |
| 200 | ); |
| 201 | |
| 202 | throw new TakeoverFailedException(); |
| 203 | } |
| 204 | |
| 205 | common_Logger::i('Converted to Composite', ['TAOITEMS', 'QTI']); |
| 206 | |
| 207 | return $returnValue; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Short description of method add |
| 212 | * |
| 213 | * @access public |
| 214 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 215 | * @param InteractionResponseProcessing interactionResponseProcessing |
| 216 | * @return mixed |
| 217 | */ |
| 218 | public function add(InteractionResponseProcessing $interactionResponseProcessing) |
| 219 | { |
| 220 | $this->components[] = $interactionResponseProcessing; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Short description of method getInteractionResponseProcessing |
| 225 | * |
| 226 | * @access public |
| 227 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 228 | * @param Response response |
| 229 | * @return oat\taoQtiItem\model\qti\response\interactionResponseProcessing\InteractionResponseProcessing |
| 230 | */ |
| 231 | public function getInteractionResponseProcessing(ResponseDeclaration $response) |
| 232 | { |
| 233 | foreach ($this->components as $irp) { |
| 234 | if ($irp->getResponse() == $response) { |
| 235 | $returnValue = $irp; |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | if (is_null($returnValue)) { |
| 240 | throw new common_Exception('No interactionResponseProcessing defined for ' . $response->getIdentifier()); |
| 241 | } |
| 242 | return $returnValue; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Short description of method getIRPByOutcome |
| 247 | * |
| 248 | * @access public |
| 249 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 250 | * @param Outcome outcome |
| 251 | * @return oat\taoQtiItem\model\qti\response\interactionResponseProcessing\InteractionResponseProcessing |
| 252 | */ |
| 253 | public function getIRPByOutcome(OutcomeDeclaration $outcome) |
| 254 | { |
| 255 | foreach ($this->components as $irp) { |
| 256 | if ($irp->getOutcome() == $outcome) { |
| 257 | $returnValue = $irp; |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | return $returnValue; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Short description of method replace |
| 267 | * |
| 268 | * @access public |
| 269 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 270 | * @param InteractionResponseProcessing newInteractionResponseProcessing |
| 271 | * @return mixed |
| 272 | */ |
| 273 | public function replace(InteractionResponseProcessing $newInteractionResponseProcessing) |
| 274 | { |
| 275 | $oldkey = null; |
| 276 | foreach ($this->components as $key => $component) { |
| 277 | if ($component->getResponse() == $newInteractionResponseProcessing->getResponse()) { |
| 278 | $oldkey = $key; |
| 279 | break; |
| 280 | } |
| 281 | } |
| 282 | if (!is_null($oldkey)) { |
| 283 | unset($this->components[$oldkey]); |
| 284 | } else { |
| 285 | common_Logger::w('Component to be replaced not found', ['TAOITEMS', 'QTI']); |
| 286 | } |
| 287 | $this->add($newInteractionResponseProcessing); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Short description of method toQTI |
| 292 | * |
| 293 | * @access public |
| 294 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 295 | * @return string |
| 296 | */ |
| 297 | public function toQTI() |
| 298 | { |
| 299 | $returnValue = "<responseProcessing>"; |
| 300 | foreach ($this->components as $irp) { |
| 301 | $returnValue .= $irp->toQTI(); |
| 302 | } |
| 303 | $returnValue .= $this->getCompositionQTI(); |
| 304 | $returnValue .= "</responseProcessing>"; |
| 305 | |
| 306 | return (string) $returnValue; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Short description of method takeNoticeOfAddedInteraction |
| 311 | * |
| 312 | * @access public |
| 313 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 314 | * @param Interaction interaction |
| 315 | * @param Item item |
| 316 | * @return mixed |
| 317 | */ |
| 318 | public function takeNoticeOfAddedInteraction(Interaction $interaction, Item $item) |
| 319 | { |
| 320 | $irp = InteractionResponseProcessing::create( |
| 321 | MatchCorrectTemplate::CLASS_ID, |
| 322 | $interaction->getResponse(), |
| 323 | $item |
| 324 | ); |
| 325 | $this->add($irp); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Short description of method takeNoticeOfRemovedInteraction |
| 330 | * |
| 331 | * @access public |
| 332 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 333 | * @param Interaction interaction |
| 334 | * @param Item item |
| 335 | * @return mixed |
| 336 | */ |
| 337 | public function takeNoticeOfRemovedInteraction(Interaction $interaction, Item $item) |
| 338 | { |
| 339 | $irpExisted = false; |
| 340 | foreach ($this->components as $key => $irp) { |
| 341 | if ($irp->getResponse() === $interaction->getResponse()) { |
| 342 | unset($this->components[$key]); |
| 343 | $irpExisted = true; |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | if (!$irpExisted) { |
| 348 | common_Logger::w( |
| 349 | 'InstanceResponseProcessing not found for removed interaction ' . $interaction->getIdentifier(), |
| 350 | ['TAOITEMS', 'QTI'] |
| 351 | ); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Short description of method getForm |
| 357 | * |
| 358 | * @access public |
| 359 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 360 | * @param Response response |
| 361 | * @return tao_helpers_form_Form |
| 362 | */ |
| 363 | public function getForm(ResponseDeclaration $response) |
| 364 | { |
| 365 | $formContainer = new CompositeResponseOptions($this, $response); |
| 366 | $returnValue = $formContainer->getForm(); |
| 367 | |
| 368 | return $returnValue; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Short description of method getCompositionQTI |
| 373 | * |
| 374 | * @abstract |
| 375 | * @access public |
| 376 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 377 | * @return string |
| 378 | */ |
| 379 | abstract public function getCompositionQTI(); |
| 380 | |
| 381 | /** |
| 382 | * Short description of method getCompositionRules |
| 383 | * |
| 384 | * @abstract |
| 385 | * @access public |
| 386 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 387 | * @return array |
| 388 | */ |
| 389 | abstract public function getCompositionRules(); |
| 390 | } |