weekly release 2.2dev
[moodle.git] / mod / feedback / item / numeric / lib.php
CommitLineData
1adbd2c3 1<?php
01910dff 2defined('MOODLE_INTERNAL') OR die('not allowed');
c70ad9f7 3require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
4
5class feedback_item_numeric extends feedback_item_base {
6 var $type = "numeric";
fc9e2caa 7 var $sep_dec, $sep_thous;
271fdd62
AG
8 var $commonparams;
9 var $item_form;
10 var $item;
d4b1d58c 11
fc9e2caa 12 function init() {
13 $this->sep_dec = get_string('separator_decimal', 'feedback');
14 if(substr($this->sep_dec, 0, 2) == '[['){
15 $this->sep_dec = FEEDBACK_DECIMAL;
16 }
d4b1d58c 17
fc9e2caa 18 $this->sep_thous = get_string('separator_thousand', 'feedback');
19 if(substr($this->sep_thous, 0, 2) == '[['){
20 $this->sep_thous = FEEDBACK_THOUSAND;
d4b1d58c 21 }
c70ad9f7 22 }
d4b1d58c 23
271fdd62
AG
24 function build_editform($item, $feedback, $cm) {
25 global $DB, $CFG;
6ee09cfe 26 require_once('numeric_form.php');
d4b1d58c 27
271fdd62
AG
28 //get the lastposition number of the feedback_items
29 $position = $item->position;
30 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
31 if($position == -1){
32 $i_formselect_last = $lastposition + 1;
33 $i_formselect_value = $lastposition + 1;
34 $item->position = $lastposition + 1;
35 }else {
36 $i_formselect_last = $lastposition;
37 $i_formselect_value = $item->position;
6ee09cfe 38 }
271fdd62
AG
39 //the elements for position dropdownlist
40 $positionlist = array_slice(range(0,$i_formselect_last),1,$i_formselect_last,true);
41
42 $item->presentation = empty($item->presentation) ? '' : $item->presentation;
43
6ee09cfe 44 $range_from_to = explode('|',$item->presentation);
fc9e2caa 45 $range_from = (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) ? str_replace(FEEDBACK_DECIMAL, $this->sep_dec, floatval($range_from_to[0])) : '-';
46 $range_to = (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) ? str_replace(FEEDBACK_DECIMAL, $this->sep_dec, floatval($range_from_to[1])) : '-';
271fdd62
AG
47 $item->rangefrom = $range_from;
48 $item->rangeto = $range_to;
49
73043833
AG
50 //all items for dependitem
51 $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
271fdd62
AG
52 $commonparams = array('cmid'=>$cm->id,
53 'id'=>isset($item->id) ? $item->id : NULL,
54 'typ'=>$item->typ,
73043833 55 'items'=>$feedbackitems,
271fdd62 56 'feedback'=>$feedback->id);
d4b1d58c 57
271fdd62
AG
58 //build the form
59 $this->item_form = new feedback_numeric_form('edit_item.php', array('item'=>$item, 'common'=>$commonparams, 'positionlist'=>$positionlist, 'position'=>$position));
60 }
d4b1d58c 61
271fdd62
AG
62 //this function only can used after the call of build_editform()
63 function show_editform() {
64 $this->item_form->display();
65 }
66
67 function is_cancelled() {
68 return $this->item_form->is_cancelled();
69 }
d4b1d58c 70
271fdd62
AG
71 function get_data() {
72 if($this->item = $this->item_form->get_data()) {
73 return true;
74 }
75 return false;
c70ad9f7 76 }
77
271fdd62
AG
78 function save_item() {
79 global $DB;
80
81 if(!$item = $this->item_form->get_data()) {
82 return false;
83 }
84
b8baf0ad 85 if(isset($item->clone_item) AND $item->clone_item) {
9e1aed53
AG
86 $item->id = ''; //to clone this item
87 $item->position++;
88 }
89
45c2f92a 90 $item->hasvalue = $this->get_hasvalue();
271fdd62
AG
91 if(!$item->id) {
92 $item->id = $DB->insert_record('feedback_item', $item);
93 }else {
94 $DB->update_record('feedback_item', $item);
95 }
96
97 return $DB->get_record('feedback_item', array('id'=>$item->id));
98 }
99
100
c70ad9f7 101 //liefert eine Struktur ->name, ->data = array(mit Antworten)
102 function get_analysed($item, $groupid = false, $courseid = false) {
0085fff8 103 global $DB;
104
c70ad9f7 105 $analysed = null;
106 $analysed->data = array();
107 $analysed->name = $item->name;
0085fff8 108 //$values = $DB->get_records('feedback_value', array('item'=>$item->id));
c70ad9f7 109 $values = feedback_get_group_values($item, $groupid, $courseid);
d4b1d58c 110
c70ad9f7 111 $avg = 0.0;
112 $counter = 0;
113 if($values) {
114 $data = array();
115 foreach($values as $value) {
116 if(is_numeric($value->value)) {
117 $data[] = $value->value;
118 $avg += $value->value;
119 $counter++;
120 }
121 }
122 $avg = $counter > 0 ? $avg / $counter : 0;
123 $analysed->data = $data;
124 $analysed->avg = $avg;
125 }
126 return $analysed;
127 }
128
129 function get_printval($item, $value) {
130 if(!isset($value->value)) return '';
d4b1d58c 131
c70ad9f7 132 return $value->value;
133 }
134
efc59167 135 function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
d4b1d58c 136
c70ad9f7 137 // $values = feedback_get_group_values($item, $groupid, $courseid);
138 $values = $this->get_analysed($item, $groupid, $courseid);
139
140 if(isset($values->data) AND is_array($values->data)) {
141 //echo '<table>';2
efc59167 142 // $itemnr++;
b7a47958 143 echo '<tr><th colspan="2" align="left">'. $itemnr . '&nbsp;('. $item->label .') ' . $item->name .'</th></tr>';
c70ad9f7 144 foreach($values->data as $value) {
fc9e2caa 145 echo '<tr><td colspan="2" valign="top" align="left">-&nbsp;&nbsp;' . number_format($value, 2, $this->sep_dec, $this->sep_thous) . '</td></tr>';
c70ad9f7 146 }
147 //echo '</table>';
efc59167 148 if(isset($values->avg)) {
fc9e2caa 149 $avg = number_format($values->avg, 2, $this->sep_dec, $this->sep_thous);
efc59167 150 } else {
fc9e2caa 151 $avg = number_format(0, 2, $this->sep_dec, $this->sep_thous);
efc59167 152 }
c70ad9f7 153 echo '<tr><td align="left" colspan="2"><b>'.get_string('average', 'feedback').': '.$avg.'</b></td></tr>';
154 }
efc59167 155 // return $itemnr;
c70ad9f7 156 }
157
51129b99 158 function excelprint_item(&$worksheet, $rowOffset, $xlsFormats, $item, $groupid, $courseid = false) {
c70ad9f7 159 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
160
51129b99
AG
161 // $worksheet->setFormat("<l><f><ro2><vo><c:green>");
162 $worksheet->write_string($rowOffset, 0, $item->label, $xlsFormats->head2);
163 $worksheet->write_string($rowOffset, 1, $item->name, $xlsFormats->head2);
c70ad9f7 164 $data = $analysed_item->data;
165 if(is_array($data)) {
166 // $worksheet->setFormat("<l><ro2><vo>");
167 // $worksheet->write_number($rowOffset, 1, $data[0]);
168 // $rowOffset++;
169 // for($i = 1; $i < sizeof($data); $i++) {
170 // $worksheet->setFormat("<l><vo>");
171 // $worksheet->write_number($rowOffset, 1, $data[$i]);
172 // $rowOffset++;
173 // }
d4b1d58c 174
c70ad9f7 175 //mittelwert anzeigen
51129b99
AG
176 // $worksheet->setFormat("<l><f><ro2><vo><c:red>");
177 $worksheet->write_string($rowOffset, 2, get_string('average', 'feedback'), $xlsFormats->value_bold);
d4b1d58c 178
51129b99
AG
179 // $worksheet->setFormat("<l><f><vo>");
180 $worksheet->write_number($rowOffset + 1, 2, $analysed_item->avg, $xlsFormats->value_bold);
c70ad9f7 181 $rowOffset++;
182 }
183 $rowOffset++;
184 return $rowOffset;
185 }
9d5fbd65
AG
186
187 /**
188 * print the item at the edit-page of feedback
189 *
190 * @global object
191 * @param object $item
192 * @return void
193 */
194 function print_item_preview($item) {
73043833
AG
195 global $OUTPUT, $DB;
196
9d5fbd65 197 $align = right_to_left() ? 'right' : 'left';
c70ad9f7 198
9d5fbd65
AG
199 //get the range
200 $range_from_to = explode('|',$item->presentation);
201 //get the min-value
202 $range_from = (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) ? floatval($range_from_to[0]) : 0;
203 //get the max-value
204 $range_to = (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) ? floatval($range_from_to[1]) : 0;
205 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
afdb1920
AG
206 //print the question and label
207 echo '<div class="feedback_item_label_'.$align.'">';
208 echo '('.$item->label.') ';
209 echo format_text($item->name . $requiredmark, true, false, false);
73043833
AG
210 if($item->dependitem) {
211 if($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
212 echo ' <span class="feedback_depend">('.$dependitem->label.'-&gt;'.$item->dependvalue.')</span>';
213 }
214 }
afdb1920
AG
215 echo '<span class="feedback_item_numinfo">';
216 switch(true) {
217 case ($range_from === '-' AND is_numeric($range_to)):
218 echo ' ('.get_string('maximal', 'feedback').': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
219 break;
220 case (is_numeric($range_from) AND $range_to === '-'):
221 echo ' ('.get_string('minimal', 'feedback').': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
222 break;
223 case ($range_from === '-' AND $range_to === '-'):
224 break;
225 default:
226 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
227 break;
228 }
229 echo '</span>';
230 echo '</div>';
231
232 //print the presentation
233 echo '<div class="feedback_item_presentation_'.$align.'">';
234 echo '<span class="feedback_item_textfield">';
235 echo '<input type="text" name="'.$item->typ.'_'.$item->id.'" size="10" maxlength="10" value="" />';
236 echo '</span>';
237 echo '</div>';
9d5fbd65
AG
238 }
239
240 /**
241 * print the item at the complete-page of feedback
242 *
243 * @global object
244 * @param object $item
245 * @param string $value
246 * @param bool $highlightrequire
247 * @return void
248 */
249 function print_item_complete($item, $value = '', $highlightrequire = false) {
d4b1d58c 250 global $OUTPUT;
e372f4c7 251 $align = right_to_left() ? 'right' : 'left';
d4b1d58c 252
c70ad9f7 253 //get the range
254 $range_from_to = explode('|',$item->presentation);
255 //get the min-value
fc9e2caa 256 $range_from = (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) ? floatval($range_from_to[0]) : 0;
c70ad9f7 257 //get the max-value
fc9e2caa 258 $range_to = (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) ? floatval($range_from_to[1]) : 0;
c70ad9f7 259 if($highlightrequire AND (!$this->check_value($value, $item))) {
afdb1920 260 $highlight = ' missingrequire';
c70ad9f7 261 }else {
262 $highlight = '';
263 }
264 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
afdb1920
AG
265
266 //print the question and label
267 echo '<div class="feedback_item_label_'.$align.$highlight.'">';
268 echo format_text($item->name . $requiredmark, true, false, false);
269 echo '<span class="feedback_item_numinfo">';
270 switch(true) {
271 case ($range_from === '-' AND is_numeric($range_to)):
272 echo ' ('.get_string('maximal', 'feedback').': '.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').': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
276 break;
277 case ($range_from === '-' AND $range_to === '-'):
278 break;
279 default:
280 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
281 break;
282 }
283 echo '</span>';
284 echo '</div>';
285
286 //print the presentation
287 echo '<div class="feedback_item_presentation_'.$align.$highlight.'">';
288 echo '<span class="feedback_item_textfield">';
289 echo '<input type="text" name="'.$item->typ.'_'.$item->id.'" size="10" maxlength="10" value="'.($value ? $value : '').'" />';
290 echo '</span>';
291 echo '</div>';
9d5fbd65
AG
292 }
293
294 /**
295 * print the item at the complete-page of feedback
296 *
297 * @global object
298 * @param object $item
299 * @param string $value
300 * @return void
301 */
302 function print_item_show_value($item, $value = '') {
303 global $OUTPUT;
304 $align = right_to_left() ? 'right' : 'left';
305
306 //get the range
307 $range_from_to = explode('|',$item->presentation);
308 //get the min-value
309 $range_from = (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) ? floatval($range_from_to[0]) : 0;
310 //get the max-value
311 $range_to = (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) ? floatval($range_from_to[1]) : 0;
312 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
afdb1920
AG
313
314 //print the question and label
315 echo '<div class="feedback_item_label_'.$align.'">';
316 echo '('.$item->label.') ';
317 echo format_text($item->name . $requiredmark, true, false, false);
318 switch(true) {
319 case ($range_from === '-' AND is_numeric($range_to)):
320 echo ' ('.get_string('maximal', 'feedback').': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
321 break;
322 case (is_numeric($range_from) AND $range_to === '-'):
323 echo ' ('.get_string('minimal', 'feedback').': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
324 break;
325 case ($range_from === '-' AND $range_to === '-'):
326 break;
327 default:
328 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
329 break;
330 }
331 echo '</div>';
332
333 //print the presentation
334 echo '<div class="feedback_item_presentation_'.$align.'">';
9d5fbd65
AG
335 echo $OUTPUT->box_start('generalbox boxalign'.$align);
336 echo (is_numeric($value)) ? number_format($value, 2, $this->sep_dec, $this->sep_thous) : '&nbsp;';
337 echo $OUTPUT->box_end();
afdb1920 338 echo '</div>';
c70ad9f7 339 }
340
341 function check_value($value, $item) {
fc9e2caa 342 $value = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $value);
c70ad9f7 343 //if the item is not required, so the check is true if no value is given
344 if((!isset($value) OR $value == '') AND $item->required != 1) return true;
345 if(!is_numeric($value))return false;
d4b1d58c 346
c70ad9f7 347 $range_from_to = explode('|',$item->presentation);
fc9e2caa 348 $range_from = (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) ? floatval($range_from_to[0]) : '-';
349 $range_to = (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) ? floatval($range_from_to[1]) : '-';
d4b1d58c 350
c70ad9f7 351 switch(true) {
fc9e2caa 352 case ($range_from === '-' AND is_numeric($range_to)):
353 if(floatval($value) <= $range_to) return true;
c70ad9f7 354 break;
fc9e2caa 355 case (is_numeric($range_from) AND $range_to === '-'):
356 if(floatval($value) >= $range_from) return true;
c70ad9f7 357 break;
fc9e2caa 358 case ($range_from === '-' AND $range_to === '-'):
c70ad9f7 359 return true;
360 break;
361 default:
fc9e2caa 362 if(floatval($value) >= $range_from AND floatval($value) <= $range_to) return true;
c70ad9f7 363 break;
364 }
d4b1d58c 365
c70ad9f7 366 return false;
367 }
368
369 function create_value($data) {
fc9e2caa 370 $data = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data);
d4b1d58c 371
fc9e2caa 372 if(is_numeric($data)) {
373 $data = floatval($data);
c70ad9f7 374 }else {
375 $data = '';
376 }
377 return $data;
378 }
379
73043833
AG
380 //compares the dbvalue with the dependvalue
381 //dbvalue is the number put in by the user
382 //dependvalue is the value that is compared
383 function compare_value($item, $dbvalue, $dependvalue) {
384 if($dbvalue == $dependvalue) {
385 return true;
386 }
387 return false;
388 }
389
c70ad9f7 390 function get_presentation($data) {
fc9e2caa 391 $num1 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangefrom);
392 if(is_numeric($num1)) {
393 $num1 = floatval($num1);
394 }else {
395 $num1 = '-';
396 }
d4b1d58c 397
fc9e2caa 398 $num2 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangeto);
399 if(is_numeric($num2)) {
400 $num2 = floatval($num2);
401 }else {
402 $num2 = '-';
403 }
d4b1d58c 404
fc9e2caa 405 if($num1 === '-' OR $num2 === '-') {
406 return $num1 . '|'. $num2;
407 }
d4b1d58c 408
fc9e2caa 409 if($num1 > $num2) {
410 return $num2 . '|'. $num1;
411 }else {
d4b1d58c 412 return $num1 . '|'. $num2;
fc9e2caa 413 }
c70ad9f7 414 }
415
416 function get_hasvalue() {
417 return 1;
418 }
6cc1599e
AG
419
420 function can_switch_require() {
421 return true;
422 }
c70ad9f7 423}