1 | <?php |
||
2 | |||
3 | /* For licensing terms, see /license.txt */ |
||
4 | |||
5 | use Webit\Util\EvalMath\EvalMath; |
||
6 | |||
7 | /** |
||
8 | * Class CalculatedAnswer |
||
9 | * This class contains calculated answer form and answer processing functions. |
||
10 | * |
||
11 | * @author Imanol Losada |
||
12 | */ |
||
13 | class CalculatedAnswer extends Question |
||
14 | { |
||
15 | public $typePicture = 'calculated_answer.png'; |
||
16 | public $explanationLangVar = 'CalculatedAnswer'; |
||
17 | |||
18 | /** |
||
19 | * Constructor. |
||
20 | */ |
||
21 | public function __construct() |
||
22 | { |
||
23 | parent::__construct(); |
||
24 | $this->type = CALCULATED_ANSWER; |
||
25 | $this->isContent = $this->getIsContent(); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | */ |
||
31 | public function createAnswersForm($form) |
||
32 | { |
||
33 | $defaults = []; |
||
34 | $defaults['answer'] = get_lang('DefaultTextInBlanks'); |
||
35 | if (!empty($this->iid)) { |
||
36 | $objAnswer = new Answer($this->iid); |
||
37 | $preArray = explode('@@', $objAnswer->selectAnswer(1)); |
||
38 | $defaults['formula'] = array_pop($preArray); |
||
39 | $defaults['answer'] = array_shift($preArray); |
||
40 | $defaults['answer'] = preg_replace("/\[.*\]/", '', $defaults['answer']); |
||
41 | $defaults['weighting'] = $this->weighting; |
||
42 | } |
||
43 | $lowestValue = '1.00'; |
||
44 | $highestValue = '20.00'; |
||
45 | |||
46 | // javascript // |
||
47 | echo '<script> |
||
48 | function parseTextNumber(textNumber, floatValue) { |
||
49 | if (textNumber.indexOf(".") > -1) { |
||
50 | textNumber = parseFloat(textNumber); |
||
51 | floatValue.exists = "true"; |
||
52 | } else { |
||
53 | textNumber = parseInt(textNumber); |
||
54 | } |
||
55 | return textNumber; |
||
56 | } |
||
57 | |||
58 | function updateRandomValue(element) { |
||
59 | // "floatValue" helps to distinguish between an integer (10) and a float with all 0 decimals (10.00) |
||
60 | var floatValue = { exists: "false" }; |
||
61 | var index = (element.name).match(/\[[^\]]*\]/g); |
||
62 | var lowestValue = parseTextNumber(document.getElementById("lowestValue"+index).value, floatValue); |
||
63 | var highestValue = parseTextNumber(document.getElementById("highestValue"+index).value, floatValue); |
||
64 | var result = Math.random() * (highestValue - lowestValue) + lowestValue; |
||
65 | if (floatValue.exists == "true") { |
||
66 | result = parseFloat(result).toFixed(2); |
||
67 | } else { |
||
68 | result = parseInt(result); |
||
69 | } |
||
70 | document.getElementById("randomValue"+index).innerHTML = "'.get_lang("ExampleValue").': " + result; |
||
71 | } |
||
72 | |||
73 | CKEDITOR.on("instanceCreated", function(e) { |
||
74 | if (e.editor.name === "answer") { |
||
75 | e.editor.on("change", updateBlanks); |
||
76 | } |
||
77 | }); |
||
78 | |||
79 | var firstTime = true; |
||
80 | function updateBlanks(e) { |
||
81 | if (firstTime) { |
||
82 | field = document.getElementById("answer"); |
||
83 | var answer = field.value; |
||
84 | } else { |
||
85 | var answer = e.editor.getData(); |
||
86 | } |
||
87 | var blanks = answer.match(/\[[^\]]*\]/g); |
||
88 | var fields = "<div class=\"form-group\"><label class=\"col-sm-2\">'.get_lang('VariableRanges').'</label><div class=\"col-sm-8\"><table>"; |
||
89 | if (blanks!=null) { |
||
90 | if (typeof updateBlanks.randomValues === "undefined") { |
||
91 | updateBlanks.randomValues = []; |
||
92 | } |
||
93 | for (i=0 ; i<blanks.length ; i++){ |
||
94 | if (document.getElementById("lowestValue["+i+"]") && document.getElementById("highestValue["+i+"]")) { |
||
95 | lowestValue = document.getElementById("lowestValue["+i+"]").value; |
||
96 | highestValue = document.getElementById("highestValue["+i+"]").value; |
||
97 | } else { |
||
98 | lowestValue = '.$lowestValue.'.toFixed(2); |
||
99 | highestValue = '.$highestValue.'.toFixed(2); |
||
100 | for (j=0; j<blanks.length; j++) { |
||
101 | updateBlanks.randomValues[j] = parseFloat(Math.random() * (highestValue - lowestValue) + lowestValue).toFixed(2); |
||
102 | } |
||
103 | } |
||
104 | fields += "<tr><td><label>"+blanks[i]+"</label></td><td><input class=\"span1\" style=\"margin-left: 0em;\" size=\"5\" value=\""+lowestValue+"\" type=\"text\" id=\"lowestValue["+i+"]\" name=\"lowestValue["+i+"]\" onblur=\"updateRandomValue(this)\"/></td><td><input class=\"span1\" style=\"margin-left: 0em; width:80px;\" size=\"5\" value=\""+highestValue+"\" type=\"text\" id=\"highestValue["+i+"]\" name=\"highestValue["+i+"]\" onblur=\"updateRandomValue(this)\"/></td><td><label class=\"span3\" id=\"randomValue["+i+"]\"/>'.get_lang('ExampleValue').': "+updateBlanks.randomValues[i]+"</label></td></tr>"; |
||
105 | } |
||
106 | } |
||
107 | document.getElementById("blanks_weighting").innerHTML = fields + "</table></div></div>"; |
||
108 | if (firstTime) { |
||
109 | firstTime = false; |
||
110 | } |
||
111 | } |
||
112 | |||
113 | window.onload = updateBlanks; |
||
114 | |||
115 | </script>'; |
||
116 | |||
117 | // answer |
||
118 | $form->addElement('label', null, '<br /><br />'.get_lang('TypeTextBelow').', '.get_lang('And').' '.get_lang('UseTagForBlank')); |
||
119 | $form->addElement( |
||
120 | 'html_editor', |
||
121 | 'answer', |
||
122 | Display::return_icon('fill_field.png'), |
||
123 | [ |
||
124 | 'id' => 'answer', |
||
125 | 'onkeyup' => 'javascript: updateBlanks(this);', |
||
126 | ], |
||
127 | [ |
||
128 | 'ToolbarSet' => 'TestQuestionDescription', |
||
129 | 'Width' => '100%', |
||
130 | 'Height' => '350', |
||
131 | ] |
||
132 | ); |
||
133 | |||
134 | $form->addRule('answer', get_lang('GiveText'), 'required'); |
||
135 | $form->addRule('answer', get_lang('DefineBlanks'), 'regex', '/\[.*\]/'); |
||
136 | |||
137 | $form->addElement('label', null, get_lang('IfYouWantOnlyIntegerValuesWriteBothLimitsWithoutDecimals')); |
||
138 | $form->addElement('html', '<div id="blanks_weighting"></div>'); |
||
139 | |||
140 | $notationListButton = Display::url( |
||
141 | get_lang('NotationList'), |
||
142 | api_get_path(WEB_CODE_PATH).'exercise/evalmathnotation.php', |
||
143 | [ |
||
144 | 'class' => 'btn btn-info ajax', |
||
145 | 'data-title' => get_lang('NotationList'), |
||
146 | '_target' => '_blank', |
||
147 | ] |
||
148 | ); |
||
149 | $form->addElement( |
||
150 | 'label', |
||
151 | null, |
||
152 | $notationListButton |
||
153 | ); |
||
154 | |||
155 | $form->addElement('text', 'formula', [get_lang('Formula'), get_lang('FormulaExample')], ['id' => 'formula']); |
||
156 | $form->addRule('formula', get_lang('GiveFormula'), 'required'); |
||
157 | |||
158 | $form->addElement('text', 'weighting', get_lang('Weighting'), ['id' => 'weighting']); |
||
159 | $form->setDefaults(['weighting' => '10']); |
||
160 | |||
161 | $form->addElement('text', 'answerVariations', get_lang('AnswerVariations')); |
||
162 | $form->addRule( |
||
163 | 'answerVariations', |
||
164 | get_lang('GiveAnswerVariations'), |
||
165 | 'required' |
||
166 | ); |
||
167 | $form->setDefaults(['answerVariations' => '1']); |
||
168 | |||
169 | global $text; |
||
170 | // setting the save button here and not in the question class.php |
||
171 | $form->addButtonSave($text, 'submitQuestion'); |
||
172 | |||
173 | if (!empty($this->iid)) { |
||
174 | $form->setDefaults($defaults); |
||
175 | } else { |
||
176 | if ($this->isContent == 1) { |
||
177 | $form->setDefaults($defaults); |
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | public function processAnswersCreation($form, $exercise) |
||
186 | { |
||
187 | if (!self::isAnswered()) { |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
188 | $table = Database::get_course_table(TABLE_QUIZ_ANSWER); |
||
189 | Database::delete( |
||
190 | $table, |
||
191 | [ |
||
192 | 'question_id = ?' => [ |
||
193 | $this->iid, |
||
194 | ], |
||
195 | ] |
||
196 | ); |
||
197 | $answer = $form->getSubmitValue('answer'); |
||
198 | $formula = $form->getSubmitValue('formula'); |
||
199 | $lowestValues = $form->getSubmitValue('lowestValue'); |
||
200 | $highestValues = $form->getSubmitValue('highestValue'); |
||
201 | $answerVariations = $form->getSubmitValue('answerVariations'); |
||
202 | $this->weighting = $form->getSubmitValue('weighting'); |
||
203 | |||
204 | // Create as many answers as $answerVariations |
||
205 | for ($j = 0; $j < $answerVariations; $j++) { |
||
206 | $auxAnswer = $answer; |
||
207 | $auxFormula = $formula; |
||
208 | $nb = preg_match_all('/\[[^\]]*\]/', $auxAnswer, $blanks); |
||
209 | if ($nb > 0) { |
||
210 | for ($i = 0; $i < $nb; $i++) { |
||
211 | $blankItem = $blanks[0][$i]; |
||
212 | |||
213 | // take random float values when one or both edge values have a decimal point |
||
214 | $randomValue = |
||
215 | (strpos($lowestValues[$i], '.') !== false || |
||
216 | strpos($highestValues[$i], '.') !== false) ? |
||
217 | mt_rand($lowestValues[$i] * 100, $highestValues[$i] * 100) / 100 : mt_rand($lowestValues[$i], $highestValues[$i]); |
||
218 | |||
219 | $auxAnswer = str_replace($blankItem, $randomValue, $auxAnswer); |
||
220 | $auxFormula = str_replace($blankItem, $randomValue, $auxFormula); |
||
221 | } |
||
222 | $math = new EvalMath(); |
||
223 | $result = $math->evaluate($auxFormula); |
||
224 | $result = number_format($result, 2, '.', ''); |
||
225 | // Remove decimal trailing zeros |
||
226 | $result = rtrim($result, '0'); |
||
227 | // If it is an integer (ends in .00) remove the decimal point |
||
228 | if (mb_substr($result, -1) === '.') { |
||
229 | $result = str_replace('.', '', $result); |
||
230 | } |
||
231 | // Attach formula |
||
232 | $auxAnswer .= " [".$result."]@@".$formula; |
||
233 | } |
||
234 | $this->save($exercise); |
||
235 | $objAnswer = new Answer($this->iid); |
||
236 | $objAnswer->createAnswer($auxAnswer, 1, '', $this->weighting, ''); |
||
237 | $objAnswer->position = []; |
||
238 | $objAnswer->save(); |
||
239 | } |
||
240 | } |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | */ |
||
246 | public function return_header(Exercise $exercise, $counter = null, $score = []) |
||
247 | { |
||
248 | $header = parent::return_header($exercise, $counter, $score); |
||
249 | $header .= '<table class="'.$this->question_table_class.'"><tr>'; |
||
250 | $header .= '<th>'.get_lang('Answer').'</th>'; |
||
251 | if ($exercise->showExpectedChoice()) { |
||
252 | $header .= '<th>'.get_lang('YourChoice').'</th>'; |
||
253 | if ($exercise->showExpectedChoiceColumn()) { |
||
254 | $header .= '<th>'.get_lang('ExpectedChoice').'</th>'; |
||
255 | } |
||
256 | $header .= '<th>'.get_lang('Status').'</th>'; |
||
257 | } |
||
258 | $header .= '</tr>'; |
||
259 | |||
260 | return $header; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Returns true if the current question has been attempted to be answered. |
||
265 | * |
||
266 | * @return bool |
||
267 | */ |
||
268 | public function isAnswered() |
||
269 | { |
||
270 | $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT); |
||
271 | $result = Database::select( |
||
272 | 'question_id', |
||
273 | $table, |
||
274 | [ |
||
275 | 'where' => [ |
||
276 | 'question_id = ? AND c_id = ?' => [ |
||
277 | $this->iid, |
||
278 | $this->course['real_id'], |
||
279 | ], |
||
280 | ], |
||
281 | ] |
||
282 | ); |
||
283 | |||
284 | return empty($result) ? false : true; |
||
285 | } |
||
286 | } |
||
287 |