MDL-21227 - convert Feedback to new files api
[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;
d4b1d58c 8
fc9e2caa 9 function init() {
10 $this->sep_dec = get_string('separator_decimal', 'feedback');
11 if(substr($this->sep_dec, 0, 2) == '[['){
12 $this->sep_dec = FEEDBACK_DECIMAL;
13 }
d4b1d58c 14
fc9e2caa 15 $this->sep_thous = get_string('separator_thousand', 'feedback');
16 if(substr($this->sep_thous, 0, 2) == '[['){
17 $this->sep_thous = FEEDBACK_THOUSAND;
d4b1d58c 18 }
c70ad9f7 19 }
d4b1d58c 20
51859310 21 function show_edit($item, $commonparams, $positionlist, $position) {
6ee09cfe 22 global $CFG;
d4b1d58c 23
6ee09cfe 24 require_once('numeric_form.php');
d4b1d58c 25
51859310 26 $item_form = new feedback_numeric_form('edit_item.php', array('item'=>$item, 'common'=>$commonparams, 'positionlist'=>$positionlist, 'position'=>$position));
c70ad9f7 27
6ee09cfe 28 $item->presentation = empty($item->presentation) ? '' : $item->presentation;
29 $item->name = empty($item->name) ? '' : $item->name;
b7a47958 30 $item->label = empty($item->label) ? '' : $item->label;
d4b1d58c 31
6ee09cfe 32 $item->required = isset($item->required) ? $item->required : 0;
33 if($item->required) {
34 $item_form->requiredcheck->setValue(true);
35 }
c70ad9f7 36
6ee09cfe 37 $item_form->itemname->setValue($item->name);
b7a47958 38 $item_form->itemlabel->setValue($item->label);
d4b1d58c 39
6ee09cfe 40 $range_from_to = explode('|',$item->presentation);
d4b1d58c 41
fc9e2caa 42 $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])) : '-';
43 $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])) : '-';
d4b1d58c 44
6ee09cfe 45 $item_form->selectfrom->setValue($range_from);
d4b1d58c 46
6ee09cfe 47 $item_form->selectto->setValue($range_to);
d4b1d58c 48
6ee09cfe 49 return $item_form;
c70ad9f7 50 }
51
52 //liefert eine Struktur ->name, ->data = array(mit Antworten)
53 function get_analysed($item, $groupid = false, $courseid = false) {
0085fff8 54 global $DB;
55
c70ad9f7 56 $analysed = null;
57 $analysed->data = array();
58 $analysed->name = $item->name;
0085fff8 59 //$values = $DB->get_records('feedback_value', array('item'=>$item->id));
c70ad9f7 60 $values = feedback_get_group_values($item, $groupid, $courseid);
d4b1d58c 61
c70ad9f7 62 $avg = 0.0;
63 $counter = 0;
64 if($values) {
65 $data = array();
66 foreach($values as $value) {
67 if(is_numeric($value->value)) {
68 $data[] = $value->value;
69 $avg += $value->value;
70 $counter++;
71 }
72 }
73 $avg = $counter > 0 ? $avg / $counter : 0;
74 $analysed->data = $data;
75 $analysed->avg = $avg;
76 }
77 return $analysed;
78 }
79
80 function get_printval($item, $value) {
81 if(!isset($value->value)) return '';
d4b1d58c 82
c70ad9f7 83 return $value->value;
84 }
85
efc59167 86 function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
d4b1d58c 87
c70ad9f7 88 // $values = feedback_get_group_values($item, $groupid, $courseid);
89 $values = $this->get_analysed($item, $groupid, $courseid);
90
91 if(isset($values->data) AND is_array($values->data)) {
92 //echo '<table>';2
efc59167 93 // $itemnr++;
b7a47958 94 echo '<tr><th colspan="2" align="left">'. $itemnr . '&nbsp;('. $item->label .') ' . $item->name .'</th></tr>';
c70ad9f7 95 foreach($values->data as $value) {
fc9e2caa 96 echo '<tr><td colspan="2" valign="top" align="left">-&nbsp;&nbsp;' . number_format($value, 2, $this->sep_dec, $this->sep_thous) . '</td></tr>';
c70ad9f7 97 }
98 //echo '</table>';
efc59167 99 if(isset($values->avg)) {
fc9e2caa 100 $avg = number_format($values->avg, 2, $this->sep_dec, $this->sep_thous);
efc59167 101 } else {
fc9e2caa 102 $avg = number_format(0, 2, $this->sep_dec, $this->sep_thous);
efc59167 103 }
c70ad9f7 104 echo '<tr><td align="left" colspan="2"><b>'.get_string('average', 'feedback').': '.$avg.'</b></td></tr>';
105 }
efc59167 106 // return $itemnr;
c70ad9f7 107 }
108
51129b99 109 function excelprint_item(&$worksheet, $rowOffset, $xlsFormats, $item, $groupid, $courseid = false) {
c70ad9f7 110 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
111
51129b99
AG
112 // $worksheet->setFormat("<l><f><ro2><vo><c:green>");
113 $worksheet->write_string($rowOffset, 0, $item->label, $xlsFormats->head2);
114 $worksheet->write_string($rowOffset, 1, $item->name, $xlsFormats->head2);
c70ad9f7 115 $data = $analysed_item->data;
116 if(is_array($data)) {
117 // $worksheet->setFormat("<l><ro2><vo>");
118 // $worksheet->write_number($rowOffset, 1, $data[0]);
119 // $rowOffset++;
120 // for($i = 1; $i < sizeof($data); $i++) {
121 // $worksheet->setFormat("<l><vo>");
122 // $worksheet->write_number($rowOffset, 1, $data[$i]);
123 // $rowOffset++;
124 // }
d4b1d58c 125
c70ad9f7 126 //mittelwert anzeigen
51129b99
AG
127 // $worksheet->setFormat("<l><f><ro2><vo><c:red>");
128 $worksheet->write_string($rowOffset, 2, get_string('average', 'feedback'), $xlsFormats->value_bold);
d4b1d58c 129
51129b99
AG
130 // $worksheet->setFormat("<l><f><vo>");
131 $worksheet->write_number($rowOffset + 1, 2, $analysed_item->avg, $xlsFormats->value_bold);
c70ad9f7 132 $rowOffset++;
133 }
134 $rowOffset++;
135 return $rowOffset;
136 }
9d5fbd65
AG
137
138 /**
139 * print the item at the edit-page of feedback
140 *
141 * @global object
142 * @param object $item
143 * @return void
144 */
145 function print_item_preview($item) {
146 global $OUTPUT;
147 $align = right_to_left() ? 'right' : 'left';
c70ad9f7 148
9d5fbd65
AG
149 //get the range
150 $range_from_to = explode('|',$item->presentation);
151 //get the min-value
152 $range_from = (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) ? floatval($range_from_to[0]) : 0;
153 //get the max-value
154 $range_to = (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) ? floatval($range_from_to[1]) : 0;
155 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
156 ?>
157 <td valign="top" align="<?php echo $align;?>">
158 <?php
159 echo '('.$item->label.') ';
160 echo format_text($item->name . $requiredmark, true, false, false);
161 switch(true) {
162 case ($range_from === '-' AND is_numeric($range_to)):
163 echo ' ('.get_string('maximal', 'feedback').': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
164 break;
165 case (is_numeric($range_from) AND $range_to === '-'):
166 echo ' ('.get_string('minimal', 'feedback').': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
167 break;
168 case ($range_from === '-' AND $range_to === '-'):
169 break;
170 default:
171 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
172 break;
173 }
174 ?>
175 </td>
176 <td valign="top" align="<?php echo $align;?>">
177 <input type="text" name="<?php echo $item->typ.'_'.$item->id; ?>" size="10" maxlength="10" value="" />
178 </td>
179 <?php
180 }
181
182 /**
183 * print the item at the complete-page of feedback
184 *
185 * @global object
186 * @param object $item
187 * @param string $value
188 * @param bool $highlightrequire
189 * @return void
190 */
191 function print_item_complete($item, $value = '', $highlightrequire = false) {
d4b1d58c 192 global $OUTPUT;
e372f4c7 193 $align = right_to_left() ? 'right' : 'left';
d4b1d58c 194
c70ad9f7 195 //get the range
196 $range_from_to = explode('|',$item->presentation);
197 //get the min-value
fc9e2caa 198 $range_from = (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) ? floatval($range_from_to[0]) : 0;
c70ad9f7 199 //get the max-value
fc9e2caa 200 $range_to = (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) ? floatval($range_from_to[1]) : 0;
c70ad9f7 201 if($highlightrequire AND (!$this->check_value($value, $item))) {
202 $highlight = 'bgcolor="#FFAAAA" class="missingrequire"';
203 }else {
204 $highlight = '';
205 }
206 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
9d5fbd65 207 ?>
c70ad9f7 208 <td <?php echo $highlight;?> valign="top" align="<?php echo $align;?>">
d4b1d58c 209 <?php
9d5fbd65
AG
210 echo format_text($item->name . $requiredmark, true, false, false);
211 switch(true) {
212 case ($range_from === '-' AND is_numeric($range_to)):
213 echo ' ('.get_string('maximal', 'feedback').': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
214 break;
215 case (is_numeric($range_from) AND $range_to === '-'):
216 echo ' ('.get_string('minimal', 'feedback').': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
217 break;
218 case ($range_from === '-' AND $range_to === '-'):
219 break;
220 default:
221 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
222 break;
b7a47958 223 }
9d5fbd65
AG
224 ?>
225 </td>
226 <td valign="top" align="<?php echo $align;?>">
227 <input type="text" name="<?php echo $item->typ.'_'.$item->id; ?>" size="10" maxlength="10" value="<?php echo $value ? $value : ''; ?>" />
228 </td>
229 <?php
230 }
231
232 /**
233 * print the item at the complete-page of feedback
234 *
235 * @global object
236 * @param object $item
237 * @param string $value
238 * @return void
239 */
240 function print_item_show_value($item, $value = '') {
241 global $OUTPUT;
242 $align = right_to_left() ? 'right' : 'left';
243
244 //get the range
245 $range_from_to = explode('|',$item->presentation);
246 //get the min-value
247 $range_from = (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) ? floatval($range_from_to[0]) : 0;
248 //get the max-value
249 $range_to = (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) ? floatval($range_from_to[1]) : 0;
250 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
251 ?>
252 <td valign="top" align="<?php echo $align;?>">
253 <?php
254 echo '('.$item->label.') ';
294ce987 255 echo format_text($item->name . $requiredmark, true, false, false);
c70ad9f7 256 switch(true) {
fc9e2caa 257 case ($range_from === '-' AND is_numeric($range_to)):
258 echo ' ('.get_string('maximal', 'feedback').': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
c70ad9f7 259 break;
fc9e2caa 260 case (is_numeric($range_from) AND $range_to === '-'):
261 echo ' ('.get_string('minimal', 'feedback').': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
c70ad9f7 262 break;
fc9e2caa 263 case ($range_from === '-' AND $range_to === '-'):
c70ad9f7 264 break;
265 default:
fc9e2caa 266 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
c70ad9f7 267 break;
268 }
269 ?>
270 </td>
271 <td valign="top" align="<?php echo $align;?>">
9d5fbd65
AG
272 <?php
273 echo $OUTPUT->box_start('generalbox boxalign'.$align);
274 echo (is_numeric($value)) ? number_format($value, 2, $this->sep_dec, $this->sep_thous) : '&nbsp;';
275 echo $OUTPUT->box_end();
276 ?>
c70ad9f7 277 </td>
9d5fbd65 278 <?php
c70ad9f7 279 }
280
281 function check_value($value, $item) {
fc9e2caa 282 $value = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $value);
c70ad9f7 283 //if the item is not required, so the check is true if no value is given
284 if((!isset($value) OR $value == '') AND $item->required != 1) return true;
285 if(!is_numeric($value))return false;
d4b1d58c 286
c70ad9f7 287 $range_from_to = explode('|',$item->presentation);
fc9e2caa 288 $range_from = (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) ? floatval($range_from_to[0]) : '-';
289 $range_to = (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) ? floatval($range_from_to[1]) : '-';
d4b1d58c 290
c70ad9f7 291 switch(true) {
fc9e2caa 292 case ($range_from === '-' AND is_numeric($range_to)):
293 if(floatval($value) <= $range_to) return true;
c70ad9f7 294 break;
fc9e2caa 295 case (is_numeric($range_from) AND $range_to === '-'):
296 if(floatval($value) >= $range_from) return true;
c70ad9f7 297 break;
fc9e2caa 298 case ($range_from === '-' AND $range_to === '-'):
c70ad9f7 299 return true;
300 break;
301 default:
fc9e2caa 302 if(floatval($value) >= $range_from AND floatval($value) <= $range_to) return true;
c70ad9f7 303 break;
304 }
d4b1d58c 305
c70ad9f7 306 return false;
307 }
308
309 function create_value($data) {
fc9e2caa 310 $data = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data);
d4b1d58c 311
fc9e2caa 312 if(is_numeric($data)) {
313 $data = floatval($data);
c70ad9f7 314 }else {
315 $data = '';
316 }
317 return $data;
318 }
319
320 function get_presentation($data) {
fc9e2caa 321 $num1 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangefrom);
322 if(is_numeric($num1)) {
323 $num1 = floatval($num1);
324 }else {
325 $num1 = '-';
326 }
d4b1d58c 327
fc9e2caa 328 $num2 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangeto);
329 if(is_numeric($num2)) {
330 $num2 = floatval($num2);
331 }else {
332 $num2 = '-';
333 }
d4b1d58c 334
fc9e2caa 335 if($num1 === '-' OR $num2 === '-') {
336 return $num1 . '|'. $num2;
337 }
d4b1d58c 338
fc9e2caa 339 if($num1 > $num2) {
340 return $num2 . '|'. $num1;
341 }else {
d4b1d58c 342 return $num1 . '|'. $num2;
fc9e2caa 343 }
c70ad9f7 344 }
345
346 function get_hasvalue() {
347 return 1;
348 }
349}
350?>