navigation MDL-22044 Added setting to limit content that is shown on the navigation...
[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 }
137
138 function print_item($item, $value = false, $readonly = false, $edit = false, $highlightrequire = false){
d4b1d58c 139 global $OUTPUT;
e372f4c7 140 $align = right_to_left() ? 'right' : 'left';
d4b1d58c 141
c70ad9f7 142 //get the range
143 $range_from_to = explode('|',$item->presentation);
144 //get the min-value
fc9e2caa 145 $range_from = (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) ? floatval($range_from_to[0]) : 0;
c70ad9f7 146 //get the max-value
fc9e2caa 147 $range_to = (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) ? floatval($range_from_to[1]) : 0;
c70ad9f7 148 if($highlightrequire AND (!$this->check_value($value, $item))) {
149 $highlight = 'bgcolor="#FFAAAA" class="missingrequire"';
150 }else {
151 $highlight = '';
152 }
153 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
154 ?>
155 <td <?php echo $highlight;?> valign="top" align="<?php echo $align;?>">
d4b1d58c 156 <?php
b7a47958 157 if($edit OR $readonly) {
158 echo '('.$item->label.') ';
159 }
294ce987 160 echo format_text($item->name . $requiredmark, true, false, false);
c70ad9f7 161 switch(true) {
fc9e2caa 162 case ($range_from === '-' AND is_numeric($range_to)):
163 echo ' ('.get_string('maximal', 'feedback').': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
c70ad9f7 164 break;
fc9e2caa 165 case (is_numeric($range_from) AND $range_to === '-'):
166 echo ' ('.get_string('minimal', 'feedback').': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
c70ad9f7 167 break;
fc9e2caa 168 case ($range_from === '-' AND $range_to === '-'):
c70ad9f7 169 break;
170 default:
fc9e2caa 171 echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
c70ad9f7 172 break;
173 }
174 ?>
175 </td>
176 <td valign="top" align="<?php echo $align;?>">
177 <?php
178 if($readonly){
d4b1d58c 179 echo $OUTPUT->box_start('generalbox boxalign'.$align);
fc9e2caa 180 echo (is_numeric($value)) ? number_format($value, 2, $this->sep_dec, $this->sep_thous) : '&nbsp;';
d4b1d58c 181 echo $OUTPUT->box_end();
c70ad9f7 182 }else {
183 ?>
184 <input type="text" name="<?php echo $item->typ.'_'.$item->id; ?>"
185 size="10"
186 maxlength="10"
187 value="<?php echo $value ? $value : ''; ?>" />
188 <?php
189 }
190 ?>
191 </td>
192 <?php
193 }
194
195 function check_value($value, $item) {
fc9e2caa 196 $value = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $value);
c70ad9f7 197 //if the item is not required, so the check is true if no value is given
198 if((!isset($value) OR $value == '') AND $item->required != 1) return true;
199 if(!is_numeric($value))return false;
d4b1d58c 200
c70ad9f7 201 $range_from_to = explode('|',$item->presentation);
fc9e2caa 202 $range_from = (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) ? floatval($range_from_to[0]) : '-';
203 $range_to = (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) ? floatval($range_from_to[1]) : '-';
d4b1d58c 204
c70ad9f7 205 switch(true) {
fc9e2caa 206 case ($range_from === '-' AND is_numeric($range_to)):
207 if(floatval($value) <= $range_to) return true;
c70ad9f7 208 break;
fc9e2caa 209 case (is_numeric($range_from) AND $range_to === '-'):
210 if(floatval($value) >= $range_from) return true;
c70ad9f7 211 break;
fc9e2caa 212 case ($range_from === '-' AND $range_to === '-'):
c70ad9f7 213 return true;
214 break;
215 default:
fc9e2caa 216 if(floatval($value) >= $range_from AND floatval($value) <= $range_to) return true;
c70ad9f7 217 break;
218 }
d4b1d58c 219
c70ad9f7 220 return false;
221 }
222
223 function create_value($data) {
fc9e2caa 224 $data = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data);
d4b1d58c 225
fc9e2caa 226 if(is_numeric($data)) {
227 $data = floatval($data);
c70ad9f7 228 }else {
229 $data = '';
230 }
231 return $data;
232 }
233
234 function get_presentation($data) {
fc9e2caa 235 $num1 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangefrom);
236 if(is_numeric($num1)) {
237 $num1 = floatval($num1);
238 }else {
239 $num1 = '-';
240 }
d4b1d58c 241
fc9e2caa 242 $num2 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangeto);
243 if(is_numeric($num2)) {
244 $num2 = floatval($num2);
245 }else {
246 $num2 = '-';
247 }
d4b1d58c 248
fc9e2caa 249 if($num1 === '-' OR $num2 === '-') {
250 return $num1 . '|'. $num2;
251 }
d4b1d58c 252
fc9e2caa 253 if($num1 > $num2) {
254 return $num2 . '|'. $num1;
255 }else {
d4b1d58c 256 return $num1 . '|'. $num2;
fc9e2caa 257 }
c70ad9f7 258 }
259
260 function get_hasvalue() {
261 return 1;
262 }
263}
264?>