Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 102
0.00% covered (danger)
0.00%
0 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
SimpleFeedbackRule
0.00% covered (danger)
0.00%
0 / 102
0.00% covered (danger)
0.00%
0 / 13
1640
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 getUsedAttributes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFeedbackOutcome
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 comparedOutcome
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFeedbackThen
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFeedbackElse
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCondition
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setFeedbackThen
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 removeFeedbackElse
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setFeedbackElse
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setCondition
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
156
 toArray
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
30
 toQTI
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 1
132
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
23namespace oat\taoQtiItem\model\qti\response;
24
25use oat\taoQtiItem\model\qti\Element;
26use oat\taoQtiItem\model\qti\OutcomeDeclaration;
27use oat\taoQtiItem\model\qti\feedback\Feedback;
28use oat\taoQtiItem\model\qti\VariableDeclaration;
29use oat\taoQtiItem\model\qti\ResponseDeclaration;
30use oat\taoQtiItem\model\qti\response\Template;
31use taoItems_models_classes_TemplateRenderer;
32use InvalidArgumentException;
33
34class 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                if ($comparedOutcome instanceof ResponseDeclaration) {
110                    $this->comparedOutcome = $comparedOutcome;
111                    $this->condition = $condition;
112                    // we may leave the comparedValue current default (if not nul) if we would like to switch back
113                    // to another condition
114                    $returnValue = true;
115                } else {
116                    throw new InvalidArgumentException(
117                        'compared outcome must be a response for correct or incorrect condition'
118                    );
119                }
120                break;
121
122            case 'lt':
123            case 'lte':
124            case 'equal':
125            case 'gte':
126            case 'gt':
127                if (!is_null($comparedValue)) {
128                    $this->comparedOutcome = $comparedOutcome;
129                    $this->condition = $condition;
130                    $this->comparedValue = $comparedValue;
131                    $returnValue = true;
132                } else {
133                    throw new InvalidArgumentException('compared value must not be null');
134                }
135                break;
136
137            case 'choices':
138                if (is_array($comparedValue)) {
139                    $this->comparedOutcome = $comparedOutcome;
140                    $this->condition = $condition;
141                    $this->comparedValue = $comparedValue;
142                    $returnValue = true;
143                } else {
144                    throw new InvalidArgumentException('compared value must not be an array');
145                }
146                break;
147        }
148
149        return $returnValue;
150    }
151
152    public function toArray($filterVariableContent = false, &$filtered = [])
153    {
154
155        $data = [
156            'serial' => $this->getSerial(),
157            'qtiClass' => '_simpleFeedbackRule',
158            'comparedOutcome' => is_null($this->comparedOutcome) ? '' : $this->comparedOutcome->getSerial(),
159            'comparedValue' => $this->comparedValue,
160            'condition' => $this->condition,
161            'feedbackOutcome' => is_null($this->feedbackOutcome) ? '' : $this->feedbackOutcome->getSerial(),
162            'feedbackThen' => is_null($this->feedbackThen) ? '' : $this->feedbackThen->getSerial(),
163            'feedbackElse' => is_null($this->feedbackElse) ? '' : $this->feedbackElse->getSerial()
164        ];
165
166        return $data;
167    }
168
169    public function toQTI()
170    {
171
172        $dir = \common_ext_ExtensionsManager::singleton()->getExtensionById('taoQtiItem')->getDir();
173        $tplPath = $dir . 'model/qti/templates/feedbacks/rules/';
174
175        $variables = [];
176        $variables['feedbackOutcomeIdentifier'] = $this->feedbackOutcome->getIdentifier();
177        $variables['feedbackIdentifierThen'] = $this->feedbackThen->getIdentifier();
178        $variables['feedbackIdentifierElse'] = is_null($this->feedbackElse)
179            ? null
180            : $this->feedbackElse->getIdentifier();
181
182        if ($this->condition == 'correct' || $this->condition == 'incorrect') {
183            $tpl = 'qti.' . $this->condition . '.tpl.php';
184            //the response processing tpl does not need to be CORRECT to allow condition to be correct
185            $variables['responseIdentifier'] = $this->comparedOutcome->getIdentifier();
186        } elseif ($this->condition == 'choices') {
187            $tpl = 'qti.choices.tpl.php';
188            $variables['responseIdentifier'] = $this->comparedOutcome->getIdentifier();
189            $variables['multiple'] = $this->comparedOutcome->attr('cardinality') == 'multiple'
190                || $this->comparedOutcome->attr('cardinality') == 'ordered';
191
192            if ($variables['multiple']) {
193                $variables['choices'] = $this->comparedValue;//an array
194            } else {
195                $variables['choice'] = reset($this->comparedValue);//an array
196            }
197        } else {
198            $tpl = 'qti.condition.tpl.php';
199            if ($this->comparedOutcome instanceof ResponseDeclaration) {
200                $response = $this->comparedOutcome;
201                $variables['responseIdentifier'] = $response->getIdentifier();
202                switch ($response->getHowMatch()) {
203                    case Template::MAP_RESPONSE:
204                        $variables['map'] = true;
205                        $variables['mapPoint'] = false;
206                        break;
207
208                    case Template::MAP_RESPONSE_POINT:
209                        $variables['mapPoint'] = true;
210                        $variables['map'] = false;
211                        break;
212
213                    case Template::MATCH_CORRECT:
214                        $variables['map'] = true;
215                        $variables['mapPoint'] = false;
216                        // allow loose control: assume simple response mapping 'MAP_RESPONSE' for beedback rule
217                        // evaluation
218                        // throw new common_Exception('condition needs to be set to correct for match correct');
219                        break;
220                }
221            } else {
222                $variables['outcomeIdentifier'] = $this->comparedOutcome->getIdentifier();
223            }
224
225            $variables['condition'] = $this->condition;
226            $variables['value'] = $this->comparedValue;
227        }
228
229        $tplRenderer = new taoItems_models_classes_TemplateRenderer($tplPath . $tpl, $variables);
230
231        $returnValue = $tplRenderer->render();
232
233        return (string) $returnValue;
234    }
235}