Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 106 |
|
0.00% |
0 / 13 |
CRAP | |
0.00% |
0 / 1 |
| SimpleFeedbackRule | |
0.00% |
0 / 106 |
|
0.00% |
0 / 13 |
1806 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getUsedAttributes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFeedbackOutcome | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| comparedOutcome | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFeedbackThen | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFeedbackElse | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCondition | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setFeedbackThen | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| removeFeedbackElse | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setFeedbackElse | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setCondition | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
182 | |||
| toArray | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
30 | |||
| toQTI | |
0.00% |
0 / 45 |
|
0.00% |
0 / 1 |
156 | |||
| 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\Element; |
| 26 | use oat\taoQtiItem\model\qti\OutcomeDeclaration; |
| 27 | use oat\taoQtiItem\model\qti\feedback\Feedback; |
| 28 | use oat\taoQtiItem\model\qti\VariableDeclaration; |
| 29 | use oat\taoQtiItem\model\qti\ResponseDeclaration; |
| 30 | use oat\taoQtiItem\model\qti\response\Template; |
| 31 | use taoItems_models_classes_TemplateRenderer; |
| 32 | use InvalidArgumentException; |
| 33 | |
| 34 | class SimpleFeedbackRule extends Element |
| 35 | { |
| 36 | protected $condition = 'correct'; //lt, lte, equal, gte, gt |
| 37 | protected $comparedOutcome = null; |
| 38 | protected $comparedValue = 0.0; //value to be compared with, required is condition is different from correct |
| 39 | protected $feedbackThen = null; |
| 40 | protected $feedbackElse = null; |
| 41 | protected $feedbackOutcome = null; |
| 42 | |
| 43 | public function __construct(OutcomeDeclaration $feedbackOutcome, $feedbackThen = null, $feedbackElse = null) |
| 44 | { |
| 45 | $this->feedbackOutcome = $feedbackOutcome; |
| 46 | if (!is_null($feedbackThen)) { |
| 47 | $this->setFeedbackThen($feedbackThen); |
| 48 | } |
| 49 | if (!is_null($feedbackElse)) { |
| 50 | $this->setFeedbackElse($feedbackElse); |
| 51 | } |
| 52 | $this->getSerial(); |
| 53 | } |
| 54 | |
| 55 | public function getUsedAttributes() |
| 56 | { |
| 57 | return []; |
| 58 | } |
| 59 | |
| 60 | public function getFeedbackOutcome() |
| 61 | { |
| 62 | return $this->feedbackOutcome; |
| 63 | } |
| 64 | |
| 65 | public function comparedOutcome() |
| 66 | { |
| 67 | return $this->comparedOutcome; |
| 68 | } |
| 69 | |
| 70 | public function getFeedbackThen() |
| 71 | { |
| 72 | return $this->feedbackThen; |
| 73 | } |
| 74 | |
| 75 | public function getFeedbackElse() |
| 76 | { |
| 77 | return $this->feedbackElse; |
| 78 | } |
| 79 | |
| 80 | public function getCondition() |
| 81 | { |
| 82 | return $this->condition; |
| 83 | } |
| 84 | |
| 85 | public function setFeedbackThen(Feedback $feedback) |
| 86 | { |
| 87 | $this->feedbackThen = $feedback; |
| 88 | } |
| 89 | |
| 90 | public function removeFeedbackElse() |
| 91 | { |
| 92 | $this->feedbackElse = null; |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | public function setFeedbackElse(Feedback $feedback) |
| 97 | { |
| 98 | $this->feedbackElse = $feedback; |
| 99 | } |
| 100 | |
| 101 | public function setCondition(VariableDeclaration $comparedOutcome, $condition, $comparedValue = null) |
| 102 | { |
| 103 | |
| 104 | $returnValue = false; |
| 105 | |
| 106 | switch ($condition) { |
| 107 | case 'correct': |
| 108 | case 'incorrect': |
| 109 | case 'isNull': |
| 110 | if ($comparedOutcome instanceof ResponseDeclaration) { |
| 111 | $this->comparedOutcome = $comparedOutcome; |
| 112 | $this->condition = $condition; |
| 113 | // we may leave the comparedValue current default (if not nul) if we would like to switch back |
| 114 | // to another condition |
| 115 | $returnValue = true; |
| 116 | } else { |
| 117 | throw new InvalidArgumentException( |
| 118 | 'compared outcome must be a response for correct or incorrect condition' |
| 119 | ); |
| 120 | } |
| 121 | break; |
| 122 | |
| 123 | case 'lt': |
| 124 | case 'lte': |
| 125 | case 'equal': |
| 126 | case 'gte': |
| 127 | case 'gt': |
| 128 | if (!is_null($comparedValue)) { |
| 129 | $this->comparedOutcome = $comparedOutcome; |
| 130 | $this->condition = $condition; |
| 131 | $this->comparedValue = $comparedValue; |
| 132 | $returnValue = true; |
| 133 | } else { |
| 134 | throw new InvalidArgumentException('compared value must not be null'); |
| 135 | } |
| 136 | break; |
| 137 | |
| 138 | case 'choices': |
| 139 | if (is_array($comparedValue)) { |
| 140 | $this->comparedOutcome = $comparedOutcome; |
| 141 | $this->condition = $condition; |
| 142 | $this->comparedValue = $comparedValue; |
| 143 | $returnValue = true; |
| 144 | } else { |
| 145 | throw new InvalidArgumentException('compared value must not be an array'); |
| 146 | } |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | return $returnValue; |
| 151 | } |
| 152 | |
| 153 | public function toArray($filterVariableContent = false, &$filtered = []) |
| 154 | { |
| 155 | |
| 156 | $data = [ |
| 157 | 'serial' => $this->getSerial(), |
| 158 | 'qtiClass' => '_simpleFeedbackRule', |
| 159 | 'comparedOutcome' => is_null($this->comparedOutcome) ? '' : $this->comparedOutcome->getSerial(), |
| 160 | 'comparedValue' => $this->comparedValue, |
| 161 | 'condition' => $this->condition, |
| 162 | 'feedbackOutcome' => is_null($this->feedbackOutcome) ? '' : $this->feedbackOutcome->getSerial(), |
| 163 | 'feedbackThen' => is_null($this->feedbackThen) ? '' : $this->feedbackThen->getSerial(), |
| 164 | 'feedbackElse' => is_null($this->feedbackElse) ? '' : $this->feedbackElse->getSerial() |
| 165 | ]; |
| 166 | |
| 167 | return $data; |
| 168 | } |
| 169 | |
| 170 | public function toQTI() |
| 171 | { |
| 172 | |
| 173 | $dir = \common_ext_ExtensionsManager::singleton()->getExtensionById('taoQtiItem')->getDir(); |
| 174 | $tplPath = $dir . 'model/qti/templates/feedbacks/rules/'; |
| 175 | |
| 176 | $variables = []; |
| 177 | $variables['feedbackOutcomeIdentifier'] = $this->feedbackOutcome->getIdentifier(); |
| 178 | $variables['feedbackIdentifierThen'] = $this->feedbackThen->getIdentifier(); |
| 179 | $variables['feedbackIdentifierElse'] = is_null($this->feedbackElse) |
| 180 | ? null |
| 181 | : $this->feedbackElse->getIdentifier(); |
| 182 | |
| 183 | if ($this->condition == 'correct' || $this->condition == 'incorrect') { |
| 184 | $tpl = 'qti.' . $this->condition . '.tpl.php'; |
| 185 | //the response processing tpl does not need to be CORRECT to allow condition to be correct |
| 186 | $variables['responseIdentifier'] = $this->comparedOutcome->getIdentifier(); |
| 187 | } elseif ($this->condition == 'choices') { |
| 188 | $tpl = 'qti.choices.tpl.php'; |
| 189 | $variables['responseIdentifier'] = $this->comparedOutcome->getIdentifier(); |
| 190 | $variables['multiple'] = $this->comparedOutcome->attr('cardinality') == 'multiple' |
| 191 | || $this->comparedOutcome->attr('cardinality') == 'ordered'; |
| 192 | |
| 193 | if ($variables['multiple']) { |
| 194 | $variables['choices'] = $this->comparedValue;//an array |
| 195 | } else { |
| 196 | $variables['choice'] = reset($this->comparedValue);//an array |
| 197 | } |
| 198 | } elseif ($this->condition == 'isNull') { |
| 199 | $tpl = 'qti.isNull.tpl.php'; |
| 200 | $variables['responseIdentifier'] = $this->comparedOutcome->getIdentifier(); |
| 201 | } else { |
| 202 | $tpl = 'qti.condition.tpl.php'; |
| 203 | if ($this->comparedOutcome instanceof ResponseDeclaration) { |
| 204 | $response = $this->comparedOutcome; |
| 205 | $variables['responseIdentifier'] = $response->getIdentifier(); |
| 206 | switch ($response->getHowMatch()) { |
| 207 | case Template::MAP_RESPONSE: |
| 208 | $variables['map'] = true; |
| 209 | $variables['mapPoint'] = false; |
| 210 | break; |
| 211 | |
| 212 | case Template::MAP_RESPONSE_POINT: |
| 213 | $variables['mapPoint'] = true; |
| 214 | $variables['map'] = false; |
| 215 | break; |
| 216 | |
| 217 | case Template::MATCH_CORRECT: |
| 218 | $variables['map'] = true; |
| 219 | $variables['mapPoint'] = false; |
| 220 | // allow loose control: assume simple response mapping 'MAP_RESPONSE' for beedback rule |
| 221 | // evaluation |
| 222 | // throw new common_Exception('condition needs to be set to correct for match correct'); |
| 223 | break; |
| 224 | } |
| 225 | } else { |
| 226 | $variables['outcomeIdentifier'] = $this->comparedOutcome->getIdentifier(); |
| 227 | } |
| 228 | |
| 229 | $variables['condition'] = $this->condition; |
| 230 | $variables['value'] = $this->comparedValue; |
| 231 | } |
| 232 | |
| 233 | $tplRenderer = new taoItems_models_classes_TemplateRenderer($tplPath . $tpl, $variables); |
| 234 | |
| 235 | $returnValue = $tplRenderer->render(); |
| 236 | |
| 237 | return (string) $returnValue; |
| 238 | } |
| 239 | } |