weekly release 2.7dev
[moodle.git] / mod / feedback / item / numeric / lib.php
CommitLineData
1adbd2c3 1<?php
9c5bc7a5
AG
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
16
01910dff 17defined('MOODLE_INTERNAL') OR die('not allowed');
c70ad9f7 18require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
19
20class feedback_item_numeric extends feedback_item_base {
9c5bc7a5
AG
21 protected $type = "numeric";
22 public $sep_dec, $sep_thous;
23 private $commonparams;
24 private $item_form;
25 private $item;
d4b1d58c 26
9c5bc7a5 27 public function init() {
fc9e2caa 28 $this->sep_dec = get_string('separator_decimal', 'feedback');
9c5bc7a5 29 if (substr($this->sep_dec, 0, 2) == '[[') {
fc9e2caa 30 $this->sep_dec = FEEDBACK_DECIMAL;
31 }
d4b1d58c 32
fc9e2caa 33 $this->sep_thous = get_string('separator_thousand', 'feedback');
9c5bc7a5 34 if (substr($this->sep_thous, 0, 2) == '[[') {
fc9e2caa 35 $this->sep_thous = FEEDBACK_THOUSAND;
d4b1d58c 36 }
c70ad9f7 37 }
d4b1d58c 38
9c5bc7a5 39 public function build_editform($item, $feedback, $cm) {
271fdd62 40 global $DB, $CFG;
6ee09cfe 41 require_once('numeric_form.php');
d4b1d58c 42
271fdd62
AG
43 //get the lastposition number of the feedback_items
44 $position = $item->position;
45 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
9c5bc7a5 46 if ($position == -1) {
271fdd62
AG
47 $i_formselect_last = $lastposition + 1;
48 $i_formselect_value = $lastposition + 1;
49 $item->position = $lastposition + 1;
9c5bc7a5 50 } else {
271fdd62
AG
51 $i_formselect_last = $lastposition;
52 $i_formselect_value = $item->position;
6ee09cfe 53 }
271fdd62 54 //the elements for position dropdownlist
9c5bc7a5
AG
55 $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true);
56
271fdd62 57 $item->presentation = empty($item->presentation) ? '' : $item->presentation;
9c5bc7a5
AG
58
59 $range_from_to = explode('|', $item->presentation);
60 if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
61 $range_from = str_replace(FEEDBACK_DECIMAL,
62 $this->sep_dec,
63 floatval($range_from_to[0]));
64 } else {
65 $range_from = '-';
66 }
67
68 if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
69 $range_to = str_replace(FEEDBACK_DECIMAL,
70 $this->sep_dec,
71 floatval($range_from_to[1]));
72 } else {
73 $range_to = '-';
74 }
75
271fdd62
AG
76 $item->rangefrom = $range_from;
77 $item->rangeto = $range_to;
78
73043833
AG
79 //all items for dependitem
80 $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
271fdd62 81 $commonparams = array('cmid'=>$cm->id,
9c5bc7a5 82 'id'=>isset($item->id) ? $item->id : null,
271fdd62 83 'typ'=>$item->typ,
73043833 84 'items'=>$feedbackitems,
271fdd62 85 'feedback'=>$feedback->id);
d4b1d58c 86
271fdd62 87 //build the form
9c5bc7a5
AG
88 $customdata = array('item' => $item,
89 'common' => $commonparams,
90 'positionlist' => $positionlist,
91 'position' => $position);
92
93 $this->item_form = new feedback_numeric_form('edit_item.php', $customdata);
271fdd62 94 }
d4b1d58c 95
271fdd62 96 //this function only can used after the call of build_editform()
9c5bc7a5 97 public function show_editform() {
271fdd62
AG
98 $this->item_form->display();
99 }
9c5bc7a5
AG
100
101 public function is_cancelled() {
271fdd62
AG
102 return $this->item_form->is_cancelled();
103 }
d4b1d58c 104
9c5bc7a5
AG
105 public function get_data() {
106 if ($this->item = $this->item_form->get_data()) {
271fdd62
AG
107 return true;
108 }
109 return false;
c70ad9f7 110 }
111
9c5bc7a5 112 public function save_item() {
271fdd62 113 global $DB;
9c5bc7a5
AG
114
115 if (!$item = $this->item_form->get_data()) {
271fdd62
AG
116 return false;
117 }
9c5bc7a5
AG
118
119 if (isset($item->clone_item) AND $item->clone_item) {
9e1aed53
AG
120 $item->id = ''; //to clone this item
121 $item->position++;
122 }
9c5bc7a5 123
45c2f92a 124 $item->hasvalue = $this->get_hasvalue();
9c5bc7a5 125 if (!$item->id) {
271fdd62 126 $item->id = $DB->insert_record('feedback_item', $item);
9c5bc7a5 127 } else {
271fdd62
AG
128 $DB->update_record('feedback_item', $item);
129 }
9c5bc7a5 130
271fdd62
AG
131 return $DB->get_record('feedback_item', array('id'=>$item->id));
132 }
133
134
c70ad9f7 135 //liefert eine Struktur ->name, ->data = array(mit Antworten)
9c5bc7a5 136 public function get_analysed($item, $groupid = false, $courseid = false) {
0085fff8 137 global $DB;
138
411fd747 139 $analysed = new stdClass();
c70ad9f7 140 $analysed->data = array();
141 $analysed->name = $item->name;
c70ad9f7 142 $values = feedback_get_group_values($item, $groupid, $courseid);
d4b1d58c 143
c70ad9f7 144 $avg = 0.0;
145 $counter = 0;
9c5bc7a5 146 if ($values) {
c70ad9f7 147 $data = array();
9c5bc7a5
AG
148 foreach ($values as $value) {
149 if (is_numeric($value->value)) {
c70ad9f7 150 $data[] = $value->value;
151 $avg += $value->value;
152 $counter++;
153 }
154 }
155 $avg = $counter > 0 ? $avg / $counter : 0;
156 $analysed->data = $data;
157 $analysed->avg = $avg;
158 }
159 return $analysed;
160 }
161
9c5bc7a5
AG
162 public function get_printval($item, $value) {
163 if (!isset($value->value)) {
164 return '';
165 }
d4b1d58c 166
c70ad9f7 167 return $value->value;
168 }
169
9c5bc7a5 170 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
d4b1d58c 171
c70ad9f7 172 $values = $this->get_analysed($item, $groupid, $courseid);
173
9c5bc7a5
AG
174 if (isset($values->data) AND is_array($values->data)) {
175 echo '<tr><th colspan="2" align="left">';
176 echo $itemnr.'&nbsp;('.$item->label.') '.$item->name;
177 echo '</th></tr>';
178
179 foreach ($values->data as $value) {
180 echo '<tr><td colspan="2" valign="top" align="left">';
181 echo '-&nbsp;&nbsp;'.number_format($value, 2, $this->sep_dec, $this->sep_thous);
182 echo '</td></tr>';
c70ad9f7 183 }
9c5bc7a5
AG
184
185 if (isset($values->avg)) {
fc9e2caa 186 $avg = number_format($values->avg, 2, $this->sep_dec, $this->sep_thous);
efc59167 187 } else {
fc9e2caa 188 $avg = number_format(0, 2, $this->sep_dec, $this->sep_thous);
efc59167 189 }
9c5bc7a5
AG
190 echo '<tr><td align="left" colspan="2"><b>';
191 echo get_string('average', 'feedback').': '.$avg;
192 echo '</b></td></tr>';
c70ad9f7 193 }
c70ad9f7 194 }
195
9c5bc7a5
AG
196 public function excelprint_item(&$worksheet, $row_offset,
197 $xls_formats, $item,
198 $groupid, $courseid = false) {
199
c70ad9f7 200 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
201
9c5bc7a5
AG
202 $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
203 $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
c70ad9f7 204 $data = $analysed_item->data;
9c5bc7a5 205 if (is_array($data)) {
d4b1d58c 206
c70ad9f7 207 //mittelwert anzeigen
9c5bc7a5
AG
208 $worksheet->write_string($row_offset,
209 2,
210 get_string('average', 'feedback'),
211 $xls_formats->value_bold);
212
213 $worksheet->write_number($row_offset + 1,
214 2,
215 $analysed_item->avg,
216 $xls_formats->value_bold);
217 $row_offset++;
c70ad9f7 218 }
9c5bc7a5
AG
219 $row_offset++;
220 return $row_offset;
c70ad9f7 221 }
9c5bc7a5 222
5f17311f 223 /**
9d5fbd65
AG
224 * print the item at the edit-page of feedback
225 *
226 * @global object
227 * @param object $item
228 * @return void
229 */
9c5bc7a5 230 public function print_item_preview($item) {
73043833 231 global $OUTPUT, $DB;
9c5bc7a5 232
9d5fbd65 233 $align = right_to_left() ? 'right' : 'left';
9c5bc7a5 234 $str_required_mark = '<span class="feedback_required_mark">*</span>';
c70ad9f7 235
9d5fbd65 236 //get the range
9c5bc7a5
AG
237 $range_from_to = explode('|', $item->presentation);
238
9d5fbd65 239 //get the min-value
9c5bc7a5
AG
240 if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
241 $range_from = floatval($range_from_to[0]);
242 } else {
243 $range_from = 0;
244 }
245
9d5fbd65 246 //get the max-value
9c5bc7a5
AG
247 if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
248 $range_to = floatval($range_from_to[1]);
249 } else {
250 $range_to = 0;
251 }
252
253 $requiredmark = ($item->required == 1) ? $str_required_mark : '';
afdb1920 254 //print the question and label
bb4191ff 255 $inputname = $item->typ . '_' . $item->id;
afdb1920 256 echo '<div class="feedback_item_label_'.$align.'">';
690266bc 257 echo '<label for="'. $inputname .'">';
9c5bc7a5
AG
258 echo '('.$item->label.') ';
259 echo format_text($item->name . $requiredmark, true, false, false);
260 if ($item->dependitem) {
261 $params = array('id'=>$item->dependitem);
262 if ($dependitem = $DB->get_record('feedback_item', $params)) {
263 echo ' <span class="feedback_depend">';
264 echo '('.$dependitem->label.'-&gt;'.$item->dependvalue.')';
265 echo '</span>';
afdb1920 266 }
9c5bc7a5
AG
267 }
268 echo '<span class="feedback_item_numinfo">';
269 switch(true) {
270 case ($range_from === '-' AND is_numeric($range_to)):
271 echo ' ('.get_string('maximal', 'feedback').
272 ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
273 break;
274 case (is_numeric($range_from) AND $range_to === '-'):
275 echo ' ('.get_string('minimal', 'feedback').
276 ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
277 break;
278 case ($range_from === '-' AND $range_to === '-'):
279 break;
280 default:
281 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).
282 ' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
283 break;
284 }
285 echo '</span>';
bb4191ff 286 echo '</label>';
afdb1920
AG
287 echo '</div>';
288
289 //print the presentation
290 echo '<div class="feedback_item_presentation_'.$align.'">';
291 echo '<span class="feedback_item_textfield">';
9c5bc7a5 292 echo '<input type="text" '.
690266bc
MM
293 'id="'.$inputname.'" '.
294 'name="'.$inputname.'" '.
9c5bc7a5
AG
295 'size="10" '.
296 'maxlength="10" '.
297 'value="" />';
298
afdb1920
AG
299 echo '</span>';
300 echo '</div>';
9d5fbd65 301 }
9c5bc7a5 302
5f17311f 303 /**
9d5fbd65
AG
304 * print the item at the complete-page of feedback
305 *
306 * @global object
307 * @param object $item
308 * @param string $value
309 * @param bool $highlightrequire
310 * @return void
311 */
9c5bc7a5 312 public function print_item_complete($item, $value = '', $highlightrequire = false) {
d4b1d58c 313 global $OUTPUT;
e372f4c7 314 $align = right_to_left() ? 'right' : 'left';
9c5bc7a5 315 $str_required_mark = '<span class="feedback_required_mark">*</span>';
d4b1d58c 316
c70ad9f7 317 //get the range
9c5bc7a5
AG
318 $range_from_to = explode('|', $item->presentation);
319
c70ad9f7 320 //get the min-value
9c5bc7a5
AG
321 if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
322 $range_from = floatval($range_from_to[0]);
323 } else {
324 $range_from = 0;
325 }
326
c70ad9f7 327 //get the max-value
9c5bc7a5
AG
328 if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
329 $range_to = floatval($range_from_to[1]);
330 } else {
331 $range_to = 0;
332 }
333
334 if ($highlightrequire AND (!$this->check_value($value, $item))) {
afdb1920 335 $highlight = ' missingrequire';
9c5bc7a5 336 } else {
c70ad9f7 337 $highlight = '';
338 }
9c5bc7a5
AG
339 $requiredmark = ($item->required == 1) ? $str_required_mark : '';
340
afdb1920 341 //print the question and label
bb4191ff 342 $inputname = $item->typ . '_' . $item->id;
afdb1920 343 echo '<div class="feedback_item_label_'.$align.$highlight.'">';
690266bc 344 echo '<label for="'. $inputname .'">';
9c5bc7a5
AG
345 echo format_text($item->name . $requiredmark, true, false, false);
346 echo '<span class="feedback_item_numinfo">';
347 switch(true) {
348 case ($range_from === '-' AND is_numeric($range_to)):
349 echo ' ('.get_string('maximal', 'feedback').
350 ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
351 break;
352 case (is_numeric($range_from) AND $range_to === '-'):
353 echo ' ('.get_string('minimal', 'feedback').
354 ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
355 break;
356 case ($range_from === '-' AND $range_to === '-'):
357 break;
358 default:
359 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).
360 ' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
361 break;
362 }
363 echo '</span>';
bb4191ff 364 echo '</label>';
afdb1920 365 echo '</div>';
9c5bc7a5 366
afdb1920
AG
367 //print the presentation
368 echo '<div class="feedback_item_presentation_'.$align.$highlight.'">';
369 echo '<span class="feedback_item_textfield">';
9c5bc7a5 370 echo '<input type="text" '.
690266bc 371 'id="'.$inputname.'" '.
9c5bc7a5
AG
372 'name="'.$item->typ.'_'.$item->id.'" '.
373 'size="10" '.
374 'maxlength="10" '.
e7fc2ff4 375 'value="'.$value.'" />';
9c5bc7a5 376
afdb1920
AG
377 echo '</span>';
378 echo '</div>';
9d5fbd65
AG
379 }
380
5f17311f 381 /**
9d5fbd65
AG
382 * print the item at the complete-page of feedback
383 *
384 * @global object
385 * @param object $item
386 * @param string $value
387 * @return void
388 */
9c5bc7a5 389 public function print_item_show_value($item, $value = '') {
9d5fbd65
AG
390 global $OUTPUT;
391 $align = right_to_left() ? 'right' : 'left';
9c5bc7a5 392 $str_required_mark = '<span class="feedback_required_mark">*</span>';
9d5fbd65
AG
393
394 //get the range
9c5bc7a5 395 $range_from_to = explode('|', $item->presentation);
9d5fbd65 396 //get the min-value
9c5bc7a5
AG
397 if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
398 $range_from = floatval($range_from_to[0]);
399 } else {
400 $range_from = 0;
401 }
9d5fbd65 402 //get the max-value
9c5bc7a5
AG
403 if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
404 $range_to = floatval($range_from_to[1]);
405 } else {
406 $range_to = 0;
407 }
408 $requiredmark = ($item->required == 1) ? $str_required_mark : '';
409
afdb1920
AG
410 //print the question and label
411 echo '<div class="feedback_item_label_'.$align.'">';
9c5bc7a5
AG
412 echo '('.$item->label.') ';
413 echo format_text($item->name . $requiredmark, true, false, false);
414 switch(true) {
415 case ($range_from === '-' AND is_numeric($range_to)):
416 echo ' ('.get_string('maximal', 'feedback').
417 ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
418 break;
419 case (is_numeric($range_from) AND $range_to === '-'):
420 echo ' ('.get_string('minimal', 'feedback').
421 ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
422 break;
423 case ($range_from === '-' AND $range_to === '-'):
424 break;
425 default:
426 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).
427 ' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
428 break;
429 }
afdb1920 430 echo '</div>';
9c5bc7a5 431
afdb1920
AG
432 //print the presentation
433 echo '<div class="feedback_item_presentation_'.$align.'">';
9d5fbd65 434 echo $OUTPUT->box_start('generalbox boxalign'.$align);
9c5bc7a5
AG
435 if (is_numeric($value)) {
436 $str_num_value = number_format($value, 2, $this->sep_dec, $this->sep_thous);
437 } else {
438 $str_num_value = '&nbsp;';
439 }
440 echo $str_num_value;
9d5fbd65 441 echo $OUTPUT->box_end();
afdb1920 442 echo '</div>';
c70ad9f7 443 }
444
9c5bc7a5 445 public function check_value($value, $item) {
fc9e2caa 446 $value = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $value);
c70ad9f7 447 //if the item is not required, so the check is true if no value is given
9c5bc7a5
AG
448 if ((!isset($value) OR $value == '') AND $item->required != 1) {
449 return true;
450 }
451 if (!is_numeric($value)) {
452 return false;
453 }
d4b1d58c 454
9c5bc7a5
AG
455 $range_from_to = explode('|', $item->presentation);
456 if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
457 $range_from = floatval($range_from_to[0]);
458 } else {
459 $range_from = '-';
460 }
461 if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
462 $range_to = floatval($range_from_to[1]);
463 } else {
464 $range_to = '-';
465 }
d4b1d58c 466
c70ad9f7 467 switch(true) {
fc9e2caa 468 case ($range_from === '-' AND is_numeric($range_to)):
9c5bc7a5
AG
469 if (floatval($value) <= $range_to) {
470 return true;
471 }
c70ad9f7 472 break;
fc9e2caa 473 case (is_numeric($range_from) AND $range_to === '-'):
9c5bc7a5
AG
474 if (floatval($value) >= $range_from) {
475 return true;
476 }
c70ad9f7 477 break;
fc9e2caa 478 case ($range_from === '-' AND $range_to === '-'):
c70ad9f7 479 return true;
480 break;
481 default:
9c5bc7a5
AG
482 if (floatval($value) >= $range_from AND floatval($value) <= $range_to) {
483 return true;
484 }
c70ad9f7 485 break;
486 }
d4b1d58c 487
c70ad9f7 488 return false;
489 }
490
9c5bc7a5 491 public function create_value($data) {
fc9e2caa 492 $data = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data);
d4b1d58c 493
9c5bc7a5 494 if (is_numeric($data)) {
fc9e2caa 495 $data = floatval($data);
9c5bc7a5 496 } else {
c70ad9f7 497 $data = '';
498 }
499 return $data;
500 }
501
73043833
AG
502 //compares the dbvalue with the dependvalue
503 //dbvalue is the number put in by the user
504 //dependvalue is the value that is compared
9c5bc7a5
AG
505 public function compare_value($item, $dbvalue, $dependvalue) {
506 if ($dbvalue == $dependvalue) {
73043833
AG
507 return true;
508 }
509 return false;
510 }
9c5bc7a5
AG
511
512 public function get_presentation($data) {
fc9e2caa 513 $num1 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangefrom);
9c5bc7a5 514 if (is_numeric($num1)) {
fc9e2caa 515 $num1 = floatval($num1);
9c5bc7a5 516 } else {
fc9e2caa 517 $num1 = '-';
518 }
d4b1d58c 519
fc9e2caa 520 $num2 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangeto);
9c5bc7a5 521 if (is_numeric($num2)) {
fc9e2caa 522 $num2 = floatval($num2);
9c5bc7a5 523 } else {
fc9e2caa 524 $num2 = '-';
525 }
d4b1d58c 526
9c5bc7a5 527 if ($num1 === '-' OR $num2 === '-') {
fc9e2caa 528 return $num1 . '|'. $num2;
529 }
d4b1d58c 530
9c5bc7a5 531 if ($num1 > $num2) {
fc9e2caa 532 return $num2 . '|'. $num1;
9c5bc7a5 533 } else {
d4b1d58c 534 return $num1 . '|'. $num2;
fc9e2caa 535 }
c70ad9f7 536 }
537
9c5bc7a5 538 public function get_hasvalue() {
c70ad9f7 539 return 1;
540 }
9c5bc7a5
AG
541
542 public function can_switch_require() {
6cc1599e
AG
543 return true;
544 }
5f17311f 545
e7fc2ff4
AG
546 public function value_type() {
547 return PARAM_FLOAT;
548 }
549
d2448b8b 550 public function clean_input_value($value) {
94848b07 551 $value = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $value);
e7fc2ff4 552 if (!is_numeric($value)) {
94848b07
AG
553 if ($value == '') {
554 return null; //an empty string should be null
555 } else {
556 return clean_param($value, PARAM_TEXT); //we have to know the value if it is wrong
557 }
e7fc2ff4
AG
558 }
559 return clean_param($value, $this->value_type());
560 }
c70ad9f7 561}