2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 defined('MOODLE_INTERNAL') OR die('not allowed');
18 require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
20 class feedback_item_numeric extends feedback_item_base {
21 protected $type = "numeric";
22 public $sep_dec, $sep_thous;
23 private $commonparams;
27 public function init() {
28 $this->sep_dec = get_string('separator_decimal', 'feedback');
29 if (substr($this->sep_dec, 0, 2) == '[[') {
30 $this->sep_dec = FEEDBACK_DECIMAL;
33 $this->sep_thous = get_string('separator_thousand', 'feedback');
34 if (substr($this->sep_thous, 0, 2) == '[[') {
35 $this->sep_thous = FEEDBACK_THOUSAND;
39 public function build_editform($item, $feedback, $cm) {
41 require_once('numeric_form.php');
43 //get the lastposition number of the feedback_items
44 $position = $item->position;
45 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
46 if ($position == -1) {
47 $i_formselect_last = $lastposition + 1;
48 $i_formselect_value = $lastposition + 1;
49 $item->position = $lastposition + 1;
51 $i_formselect_last = $lastposition;
52 $i_formselect_value = $item->position;
54 //the elements for position dropdownlist
55 $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true);
57 $item->presentation = empty($item->presentation) ? '' : $item->presentation;
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,
63 floatval($range_from_to[0]));
68 if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
69 $range_to = str_replace(FEEDBACK_DECIMAL,
71 floatval($range_from_to[1]));
76 $item->rangefrom = $range_from;
77 $item->rangeto = $range_to;
79 //all items for dependitem
80 $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
81 $commonparams = array('cmid'=>$cm->id,
82 'id'=>isset($item->id) ? $item->id : null,
84 'items'=>$feedbackitems,
85 'feedback'=>$feedback->id);
88 $customdata = array('item' => $item,
89 'common' => $commonparams,
90 'positionlist' => $positionlist,
91 'position' => $position);
93 $this->item_form = new feedback_numeric_form('edit_item.php', $customdata);
96 //this function only can used after the call of build_editform()
97 public function show_editform() {
98 $this->item_form->display();
101 public function is_cancelled() {
102 return $this->item_form->is_cancelled();
105 public function get_data() {
106 if ($this->item = $this->item_form->get_data()) {
112 public function save_item() {
115 if (!$item = $this->item_form->get_data()) {
119 if (isset($item->clone_item) AND $item->clone_item) {
120 $item->id = ''; //to clone this item
124 $item->hasvalue = $this->get_hasvalue();
126 $item->id = $DB->insert_record('feedback_item', $item);
128 $DB->update_record('feedback_item', $item);
131 return $DB->get_record('feedback_item', array('id'=>$item->id));
135 //liefert eine Struktur ->name, ->data = array(mit Antworten)
136 public function get_analysed($item, $groupid = false, $courseid = false) {
139 $analysed = new stdClass();
140 $analysed->data = array();
141 $analysed->name = $item->name;
142 $values = feedback_get_group_values($item, $groupid, $courseid);
148 foreach ($values as $value) {
149 if (is_numeric($value->value)) {
150 $data[] = $value->value;
151 $avg += $value->value;
155 $avg = $counter > 0 ? $avg / $counter : 0;
156 $analysed->data = $data;
157 $analysed->avg = $avg;
162 public function get_printval($item, $value) {
163 if (!isset($value->value)) {
167 return $value->value;
170 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
172 $values = $this->get_analysed($item, $groupid, $courseid);
174 if (isset($values->data) AND is_array($values->data)) {
175 echo '<tr><th colspan="2" align="left">';
176 echo $itemnr.' ('.$item->label.') '.$item->name;
179 foreach ($values->data as $value) {
180 echo '<tr><td colspan="2" valign="top" align="left">';
181 echo '- '.number_format($value, 2, $this->sep_dec, $this->sep_thous);
185 if (isset($values->avg)) {
186 $avg = number_format($values->avg, 2, $this->sep_dec, $this->sep_thous);
188 $avg = number_format(0, 2, $this->sep_dec, $this->sep_thous);
190 echo '<tr><td align="left" colspan="2"><b>';
191 echo get_string('average', 'feedback').': '.$avg;
192 echo '</b></td></tr>';
196 public function excelprint_item(&$worksheet, $row_offset,
198 $groupid, $courseid = false) {
200 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
202 $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
203 $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
204 $data = $analysed_item->data;
205 if (is_array($data)) {
207 //mittelwert anzeigen
208 $worksheet->write_string($row_offset,
210 get_string('average', 'feedback'),
211 $xls_formats->value_bold);
213 $worksheet->write_number($row_offset + 1,
216 $xls_formats->value_bold);
224 * print the item at the edit-page of feedback
227 * @param object $item
230 public function print_item_preview($item) {
233 $align = right_to_left() ? 'right' : 'left';
234 $str_required_mark = '<span class="feedback_required_mark">*</span>';
237 $range_from_to = explode('|', $item->presentation);
240 if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
241 $range_from = floatval($range_from_to[0]);
247 if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
248 $range_to = floatval($range_from_to[1]);
253 $requiredmark = ($item->required == 1) ? $str_required_mark : '';
254 //print the question and label
255 echo '<div class="feedback_item_label_'.$align.'">';
256 echo '('.$item->label.') ';
257 echo format_text($item->name . $requiredmark, true, false, false);
258 if ($item->dependitem) {
259 $params = array('id'=>$item->dependitem);
260 if ($dependitem = $DB->get_record('feedback_item', $params)) {
261 echo ' <span class="feedback_depend">';
262 echo '('.$dependitem->label.'->'.$item->dependvalue.')';
266 echo '<span class="feedback_item_numinfo">';
268 case ($range_from === '-' AND is_numeric($range_to)):
269 echo ' ('.get_string('maximal', 'feedback').
270 ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
272 case (is_numeric($range_from) AND $range_to === '-'):
273 echo ' ('.get_string('minimal', 'feedback').
274 ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
276 case ($range_from === '-' AND $range_to === '-'):
279 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).
280 ' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
286 //print the presentation
287 echo '<div class="feedback_item_presentation_'.$align.'">';
288 echo '<span class="feedback_item_textfield">';
289 echo '<input type="text" '.
290 'name="'.$item->typ.'_'.$item->id.'" '.
300 * print the item at the complete-page of feedback
303 * @param object $item
304 * @param string $value
305 * @param bool $highlightrequire
308 public function print_item_complete($item, $value = '', $highlightrequire = false) {
310 $align = right_to_left() ? 'right' : 'left';
311 $str_required_mark = '<span class="feedback_required_mark">*</span>';
314 $range_from_to = explode('|', $item->presentation);
317 if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
318 $range_from = floatval($range_from_to[0]);
324 if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
325 $range_to = floatval($range_from_to[1]);
330 if ($highlightrequire AND (!$this->check_value($value, $item))) {
331 $highlight = ' missingrequire';
335 $requiredmark = ($item->required == 1) ? $str_required_mark : '';
337 //print the question and label
338 echo '<div class="feedback_item_label_'.$align.$highlight.'">';
339 echo format_text($item->name . $requiredmark, true, false, false);
340 echo '<span class="feedback_item_numinfo">';
342 case ($range_from === '-' AND is_numeric($range_to)):
343 echo ' ('.get_string('maximal', 'feedback').
344 ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
346 case (is_numeric($range_from) AND $range_to === '-'):
347 echo ' ('.get_string('minimal', 'feedback').
348 ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
350 case ($range_from === '-' AND $range_to === '-'):
353 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).
354 ' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
360 //print the presentation
361 echo '<div class="feedback_item_presentation_'.$align.$highlight.'">';
362 echo '<span class="feedback_item_textfield">';
363 echo '<input type="text" '.
364 'name="'.$item->typ.'_'.$item->id.'" '.
367 'value="'.$value.'" />';
374 * print the item at the complete-page of feedback
377 * @param object $item
378 * @param string $value
381 public function print_item_show_value($item, $value = '') {
383 $align = right_to_left() ? 'right' : 'left';
384 $str_required_mark = '<span class="feedback_required_mark">*</span>';
387 $range_from_to = explode('|', $item->presentation);
389 if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
390 $range_from = floatval($range_from_to[0]);
395 if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
396 $range_to = floatval($range_from_to[1]);
400 $requiredmark = ($item->required == 1) ? $str_required_mark : '';
402 //print the question and label
403 echo '<div class="feedback_item_label_'.$align.'">';
404 echo '('.$item->label.') ';
405 echo format_text($item->name . $requiredmark, true, false, false);
407 case ($range_from === '-' AND is_numeric($range_to)):
408 echo ' ('.get_string('maximal', 'feedback').
409 ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
411 case (is_numeric($range_from) AND $range_to === '-'):
412 echo ' ('.get_string('minimal', 'feedback').
413 ': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
415 case ($range_from === '-' AND $range_to === '-'):
418 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).
419 ' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
424 //print the presentation
425 echo '<div class="feedback_item_presentation_'.$align.'">';
426 echo $OUTPUT->box_start('generalbox boxalign'.$align);
427 if (is_numeric($value)) {
428 $str_num_value = number_format($value, 2, $this->sep_dec, $this->sep_thous);
430 $str_num_value = ' ';
433 echo $OUTPUT->box_end();
437 public function check_value($value, $item) {
438 $value = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $value);
439 //if the item is not required, so the check is true if no value is given
440 if ((!isset($value) OR $value == '') AND $item->required != 1) {
443 if (!is_numeric($value)) {
447 $range_from_to = explode('|', $item->presentation);
448 if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
449 $range_from = floatval($range_from_to[0]);
453 if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
454 $range_to = floatval($range_from_to[1]);
460 case ($range_from === '-' AND is_numeric($range_to)):
461 if (floatval($value) <= $range_to) {
465 case (is_numeric($range_from) AND $range_to === '-'):
466 if (floatval($value) >= $range_from) {
470 case ($range_from === '-' AND $range_to === '-'):
474 if (floatval($value) >= $range_from AND floatval($value) <= $range_to) {
483 public function create_value($data) {
484 $data = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data);
486 if (is_numeric($data)) {
487 $data = floatval($data);
494 //compares the dbvalue with the dependvalue
495 //dbvalue is the number put in by the user
496 //dependvalue is the value that is compared
497 public function compare_value($item, $dbvalue, $dependvalue) {
498 if ($dbvalue == $dependvalue) {
504 public function get_presentation($data) {
505 $num1 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangefrom);
506 if (is_numeric($num1)) {
507 $num1 = floatval($num1);
512 $num2 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangeto);
513 if (is_numeric($num2)) {
514 $num2 = floatval($num2);
519 if ($num1 === '-' OR $num2 === '-') {
520 return $num1 . '|'. $num2;
524 return $num2 . '|'. $num1;
526 return $num1 . '|'. $num2;
530 public function get_hasvalue() {
534 public function can_switch_require() {
538 public function value_type() {
542 public function clean_input_value($value) {
543 if (!is_numeric($value)) {
546 return clean_param($value, $this->value_type());