MDL-20296 creating common functions to handle units in numerical, calculated and...
[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
50 $commonparams = array('cmid'=>$cm->id,
51 'id'=>isset($item->id) ? $item->id : NULL,
52 'typ'=>$item->typ,
53 'feedback'=>$feedback->id);
d4b1d58c 54
271fdd62
AG
55 //build the form
56 $this->item_form = new feedback_numeric_form('edit_item.php', array('item'=>$item, 'common'=>$commonparams, 'positionlist'=>$positionlist, 'position'=>$position));
57 }
d4b1d58c 58
271fdd62
AG
59 //this function only can used after the call of build_editform()
60 function show_editform() {
61 $this->item_form->display();
62 }
63
64 function is_cancelled() {
65 return $this->item_form->is_cancelled();
66 }
d4b1d58c 67
271fdd62
AG
68 function get_data() {
69 if($this->item = $this->item_form->get_data()) {
70 return true;
71 }
72 return false;
c70ad9f7 73 }
74
271fdd62
AG
75 function save_item() {
76 global $DB;
77
78 if(!$item = $this->item_form->get_data()) {
79 return false;
80 }
81
45c2f92a 82 $item->hasvalue = $this->get_hasvalue();
271fdd62
AG
83 if(!$item->id) {
84 $item->id = $DB->insert_record('feedback_item', $item);
85 }else {
86 $DB->update_record('feedback_item', $item);
87 }
88
89 return $DB->get_record('feedback_item', array('id'=>$item->id));
90 }
91
92
c70ad9f7 93 //liefert eine Struktur ->name, ->data = array(mit Antworten)
94 function get_analysed($item, $groupid = false, $courseid = false) {
0085fff8 95 global $DB;
96
c70ad9f7 97 $analysed = null;
98 $analysed->data = array();
99 $analysed->name = $item->name;
0085fff8 100 //$values = $DB->get_records('feedback_value', array('item'=>$item->id));
c70ad9f7 101 $values = feedback_get_group_values($item, $groupid, $courseid);
d4b1d58c 102
c70ad9f7 103 $avg = 0.0;
104 $counter = 0;
105 if($values) {
106 $data = array();
107 foreach($values as $value) {
108 if(is_numeric($value->value)) {
109 $data[] = $value->value;
110 $avg += $value->value;
111 $counter++;
112 }
113 }
114 $avg = $counter > 0 ? $avg / $counter : 0;
115 $analysed->data = $data;
116 $analysed->avg = $avg;
117 }
118 return $analysed;
119 }
120
121 function get_printval($item, $value) {
122 if(!isset($value->value)) return '';
d4b1d58c 123
c70ad9f7 124 return $value->value;
125 }
126
efc59167 127 function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
d4b1d58c 128
c70ad9f7 129 // $values = feedback_get_group_values($item, $groupid, $courseid);
130 $values = $this->get_analysed($item, $groupid, $courseid);
131
132 if(isset($values->data) AND is_array($values->data)) {
133 //echo '<table>';2
efc59167 134 // $itemnr++;
b7a47958 135 echo '<tr><th colspan="2" align="left">'. $itemnr . '&nbsp;('. $item->label .') ' . $item->name .'</th></tr>';
c70ad9f7 136 foreach($values->data as $value) {
fc9e2caa 137 echo '<tr><td colspan="2" valign="top" align="left">-&nbsp;&nbsp;' . number_format($value, 2, $this->sep_dec, $this->sep_thous) . '</td></tr>';
c70ad9f7 138 }
139 //echo '</table>';
efc59167 140 if(isset($values->avg)) {
fc9e2caa 141 $avg = number_format($values->avg, 2, $this->sep_dec, $this->sep_thous);
efc59167 142 } else {
fc9e2caa 143 $avg = number_format(0, 2, $this->sep_dec, $this->sep_thous);
efc59167 144 }
c70ad9f7 145 echo '<tr><td align="left" colspan="2"><b>'.get_string('average', 'feedback').': '.$avg.'</b></td></tr>';
146 }
efc59167 147 // return $itemnr;
c70ad9f7 148 }
149
51129b99 150 function excelprint_item(&$worksheet, $rowOffset, $xlsFormats, $item, $groupid, $courseid = false) {
c70ad9f7 151 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
152
51129b99
AG
153 // $worksheet->setFormat("<l><f><ro2><vo><c:green>");
154 $worksheet->write_string($rowOffset, 0, $item->label, $xlsFormats->head2);
155 $worksheet->write_string($rowOffset, 1, $item->name, $xlsFormats->head2);
c70ad9f7 156 $data = $analysed_item->data;
157 if(is_array($data)) {
158 // $worksheet->setFormat("<l><ro2><vo>");
159 // $worksheet->write_number($rowOffset, 1, $data[0]);
160 // $rowOffset++;
161 // for($i = 1; $i < sizeof($data); $i++) {
162 // $worksheet->setFormat("<l><vo>");
163 // $worksheet->write_number($rowOffset, 1, $data[$i]);
164 // $rowOffset++;
165 // }
d4b1d58c 166
c70ad9f7 167 //mittelwert anzeigen
51129b99
AG
168 // $worksheet->setFormat("<l><f><ro2><vo><c:red>");
169 $worksheet->write_string($rowOffset, 2, get_string('average', 'feedback'), $xlsFormats->value_bold);
d4b1d58c 170
51129b99
AG
171 // $worksheet->setFormat("<l><f><vo>");
172 $worksheet->write_number($rowOffset + 1, 2, $analysed_item->avg, $xlsFormats->value_bold);
c70ad9f7 173 $rowOffset++;
174 }
175 $rowOffset++;
176 return $rowOffset;
177 }
9d5fbd65
AG
178
179 /**
180 * print the item at the edit-page of feedback
181 *
182 * @global object
183 * @param object $item
184 * @return void
185 */
186 function print_item_preview($item) {
187 global $OUTPUT;
188 $align = right_to_left() ? 'right' : 'left';
c70ad9f7 189
9d5fbd65
AG
190 //get the range
191 $range_from_to = explode('|',$item->presentation);
192 //get the min-value
193 $range_from = (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) ? floatval($range_from_to[0]) : 0;
194 //get the max-value
195 $range_to = (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) ? floatval($range_from_to[1]) : 0;
196 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
197 ?>
198 <td valign="top" align="<?php echo $align;?>">
199 <?php
200 echo '('.$item->label.') ';
201 echo format_text($item->name . $requiredmark, true, false, false);
202 switch(true) {
203 case ($range_from === '-' AND is_numeric($range_to)):
204 echo ' ('.get_string('maximal', 'feedback').': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
205 break;
206 case (is_numeric($range_from) AND $range_to === '-'):
207 echo ' ('.get_string('minimal', 'feedback').': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
208 break;
209 case ($range_from === '-' AND $range_to === '-'):
210 break;
211 default:
212 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
213 break;
214 }
215 ?>
216 </td>
217 <td valign="top" align="<?php echo $align;?>">
218 <input type="text" name="<?php echo $item->typ.'_'.$item->id; ?>" size="10" maxlength="10" value="" />
219 </td>
220 <?php
221 }
222
223 /**
224 * print the item at the complete-page of feedback
225 *
226 * @global object
227 * @param object $item
228 * @param string $value
229 * @param bool $highlightrequire
230 * @return void
231 */
232 function print_item_complete($item, $value = '', $highlightrequire = false) {
d4b1d58c 233 global $OUTPUT;
e372f4c7 234 $align = right_to_left() ? 'right' : 'left';
d4b1d58c 235
c70ad9f7 236 //get the range
237 $range_from_to = explode('|',$item->presentation);
238 //get the min-value
fc9e2caa 239 $range_from = (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) ? floatval($range_from_to[0]) : 0;
c70ad9f7 240 //get the max-value
fc9e2caa 241 $range_to = (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) ? floatval($range_from_to[1]) : 0;
c70ad9f7 242 if($highlightrequire AND (!$this->check_value($value, $item))) {
243 $highlight = 'bgcolor="#FFAAAA" class="missingrequire"';
244 }else {
245 $highlight = '';
246 }
247 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
9d5fbd65 248 ?>
c70ad9f7 249 <td <?php echo $highlight;?> valign="top" align="<?php echo $align;?>">
d4b1d58c 250 <?php
9d5fbd65
AG
251 echo format_text($item->name . $requiredmark, true, false, false);
252 switch(true) {
253 case ($range_from === '-' AND is_numeric($range_to)):
254 echo ' ('.get_string('maximal', 'feedback').': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
255 break;
256 case (is_numeric($range_from) AND $range_to === '-'):
257 echo ' ('.get_string('minimal', 'feedback').': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
258 break;
259 case ($range_from === '-' AND $range_to === '-'):
260 break;
261 default:
262 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
263 break;
b7a47958 264 }
9d5fbd65
AG
265 ?>
266 </td>
267 <td valign="top" align="<?php echo $align;?>">
268 <input type="text" name="<?php echo $item->typ.'_'.$item->id; ?>" size="10" maxlength="10" value="<?php echo $value ? $value : ''; ?>" />
269 </td>
270 <?php
271 }
272
273 /**
274 * print the item at the complete-page of feedback
275 *
276 * @global object
277 * @param object $item
278 * @param string $value
279 * @return void
280 */
281 function print_item_show_value($item, $value = '') {
282 global $OUTPUT;
283 $align = right_to_left() ? 'right' : 'left';
284
285 //get the range
286 $range_from_to = explode('|',$item->presentation);
287 //get the min-value
288 $range_from = (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) ? floatval($range_from_to[0]) : 0;
289 //get the max-value
290 $range_to = (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) ? floatval($range_from_to[1]) : 0;
291 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
292 ?>
293 <td valign="top" align="<?php echo $align;?>">
294 <?php
295 echo '('.$item->label.') ';
294ce987 296 echo format_text($item->name . $requiredmark, true, false, false);
c70ad9f7 297 switch(true) {
fc9e2caa 298 case ($range_from === '-' AND is_numeric($range_to)):
299 echo ' ('.get_string('maximal', 'feedback').': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
c70ad9f7 300 break;
fc9e2caa 301 case (is_numeric($range_from) AND $range_to === '-'):
302 echo ' ('.get_string('minimal', 'feedback').': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
c70ad9f7 303 break;
fc9e2caa 304 case ($range_from === '-' AND $range_to === '-'):
c70ad9f7 305 break;
306 default:
fc9e2caa 307 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
c70ad9f7 308 break;
309 }
310 ?>
311 </td>
312 <td valign="top" align="<?php echo $align;?>">
9d5fbd65
AG
313 <?php
314 echo $OUTPUT->box_start('generalbox boxalign'.$align);
315 echo (is_numeric($value)) ? number_format($value, 2, $this->sep_dec, $this->sep_thous) : '&nbsp;';
316 echo $OUTPUT->box_end();
317 ?>
c70ad9f7 318 </td>
9d5fbd65 319 <?php
c70ad9f7 320 }
321
322 function check_value($value, $item) {
fc9e2caa 323 $value = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $value);
c70ad9f7 324 //if the item is not required, so the check is true if no value is given
325 if((!isset($value) OR $value == '') AND $item->required != 1) return true;
326 if(!is_numeric($value))return false;
d4b1d58c 327
c70ad9f7 328 $range_from_to = explode('|',$item->presentation);
fc9e2caa 329 $range_from = (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) ? floatval($range_from_to[0]) : '-';
330 $range_to = (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) ? floatval($range_from_to[1]) : '-';
d4b1d58c 331
c70ad9f7 332 switch(true) {
fc9e2caa 333 case ($range_from === '-' AND is_numeric($range_to)):
334 if(floatval($value) <= $range_to) return true;
c70ad9f7 335 break;
fc9e2caa 336 case (is_numeric($range_from) AND $range_to === '-'):
337 if(floatval($value) >= $range_from) return true;
c70ad9f7 338 break;
fc9e2caa 339 case ($range_from === '-' AND $range_to === '-'):
c70ad9f7 340 return true;
341 break;
342 default:
fc9e2caa 343 if(floatval($value) >= $range_from AND floatval($value) <= $range_to) return true;
c70ad9f7 344 break;
345 }
d4b1d58c 346
c70ad9f7 347 return false;
348 }
349
350 function create_value($data) {
fc9e2caa 351 $data = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data);
d4b1d58c 352
fc9e2caa 353 if(is_numeric($data)) {
354 $data = floatval($data);
c70ad9f7 355 }else {
356 $data = '';
357 }
358 return $data;
359 }
360
361 function get_presentation($data) {
fc9e2caa 362 $num1 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangefrom);
363 if(is_numeric($num1)) {
364 $num1 = floatval($num1);
365 }else {
366 $num1 = '-';
367 }
d4b1d58c 368
fc9e2caa 369 $num2 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangeto);
370 if(is_numeric($num2)) {
371 $num2 = floatval($num2);
372 }else {
373 $num2 = '-';
374 }
d4b1d58c 375
fc9e2caa 376 if($num1 === '-' OR $num2 === '-') {
377 return $num1 . '|'. $num2;
378 }
d4b1d58c 379
fc9e2caa 380 if($num1 > $num2) {
381 return $num2 . '|'. $num1;
382 }else {
d4b1d58c 383 return $num1 . '|'. $num2;
fc9e2caa 384 }
c70ad9f7 385 }
386
387 function get_hasvalue() {
388 return 1;
389 }
390}
391?>