MDL-14493 First checkin of Feedback module into HEAD.
[moodle.git] / mod / feedback / item / numeric / lib.php
CommitLineData
c70ad9f7 1<?php // $Id$
2defined('FEEDBACK_INCLUDE_TEST') OR die('not allowed');
3require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
4
5class feedback_item_numeric extends feedback_item_base {
6 var $type = "numeric";
7 function init() {
8
9 }
10
11 function show_edit($item, $usehtmleditor = false) {
12
13 $item->presentation=empty($item->presentation)?'':$item->presentation;
14
15 ?>
16 <table>
17 <tr>
18 <th colspan="2"><?php print_string('numeric', 'feedback');?>
19 &nbsp;(<input type="checkbox" name="required" value="1" <?php
20 $item->required=isset($item->required)?$item->required:0;
21 echo ($item->required == 1?'checked="checked"':'');
22 ?> />&nbsp;<?php print_string('required', 'feedback');?>)
23 </th>
24 </tr>
25 <tr>
26 <td><?php print_string('item_name', 'feedback');?></td>
27 <td><input type="text" id="itemname" name="itemname" size="40" maxlength="255" value="<?php echo isset($item->name)?htmlspecialchars(stripslashes_safe($item->name)):'';?>" /></td>
28 </tr>
29 <tr>
30 <?php
31 //Dropdown-Items fuer die Textfeldbreite
32 $range_from_to = explode('|',$item->presentation);
33 $range_from = isset($range_from_to[0]) ? intval($range_from_to[0]) : 0;
34 $range_to = isset($range_from_to[1]) ? intval($range_from_to[1]) : 0;
35 ?>
36 <td><?php print_string('numeric_range_from', 'feedback');?></td>
37 <td>
38 <input type="text" name="numericrangefrom" value="<?php echo $range_from;?>" />
39 </td>
40 </tr>
41 <tr>
42 <td><?php print_string('numeric_range_to', 'feedback');?></td>
43 <td>
44 <input type="text" name="numericrangeto" value="<?php echo $range_to;?>" />
45 </td>
46 </tr>
47 </table>
48 <?php
49 }
50
51 //liefert eine Struktur ->name, ->data = array(mit Antworten)
52 function get_analysed($item, $groupid = false, $courseid = false) {
53 $analysed = null;
54 $analysed->data = array();
55 $analysed->name = $item->name;
56 //$values = get_records('feedback_value', 'item', $item->id);
57 $values = feedback_get_group_values($item, $groupid, $courseid);
58
59 $avg = 0.0;
60 $counter = 0;
61 if($values) {
62 $data = array();
63 foreach($values as $value) {
64 if(is_numeric($value->value)) {
65 $data[] = $value->value;
66 $avg += $value->value;
67 $counter++;
68 }
69 }
70 $avg = $counter > 0 ? $avg / $counter : 0;
71 $analysed->data = $data;
72 $analysed->avg = $avg;
73 }
74 return $analysed;
75 }
76
77 function get_printval($item, $value) {
78 if(!isset($value->value)) return '';
79
80 return $value->value;
81 }
82
83 function print_analysed($item, $itemnr = 0, $groupid = false, $courseid = false) {
84 $sep_dec = get_string('separator_decimal', 'feedback');
85 if(substr($sep_dec, 0, 2) == '[['){
86 $sep_dec = FEEDBACK_DECIMAL;
87 }
88
89 $sep_thous = get_string('separator_thousand', 'feedback');
90 if(substr($sep_thous, 0, 2) == '[['){
91 $sep_thous = FEEDBACK_THOUSAND;
92 }
93
94 // $values = feedback_get_group_values($item, $groupid, $courseid);
95 $values = $this->get_analysed($item, $groupid, $courseid);
96
97 if(isset($values->data) AND is_array($values->data)) {
98 //echo '<table>';2
99 $itemnr++;
100 echo '<tr><th colspan="2" align="left">'. $itemnr . '.)&nbsp;' . stripslashes($item->name) .'</th></tr>';
101 foreach($values->data as $value) {
102 echo '<tr><td colspan="2" valign="top" align="left">-&nbsp;&nbsp;' . $value . '</td></tr>';
103 }
104 //echo '</table>';
105 $avg = number_format($values->avg, 2, $sep_dec, $sep_thous);
106 echo '<tr><td align="left" colspan="2"><b>'.get_string('average', 'feedback').': '.$avg.'</b></td></tr>';
107 }
108 return $itemnr;
109 }
110
111 function excelprint_item(&$worksheet, $rowOffset, $item, $groupid, $courseid = false) {
112 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
113
114 $worksheet->setFormat("<l><f><ro2><vo><c:green>");
115 $worksheet->write_string($rowOffset, 0, stripslashes($item->name));
116 $data = $analysed_item->data;
117 if(is_array($data)) {
118 // $worksheet->setFormat("<l><ro2><vo>");
119 // $worksheet->write_number($rowOffset, 1, $data[0]);
120 // $rowOffset++;
121 // for($i = 1; $i < sizeof($data); $i++) {
122 // $worksheet->setFormat("<l><vo>");
123 // $worksheet->write_number($rowOffset, 1, $data[$i]);
124 // $rowOffset++;
125 // }
126
127 //mittelwert anzeigen
128 $worksheet->setFormat("<l><f><ro2><vo><c:red>");
129 $worksheet->write_string($rowOffset, 1, get_string('average', 'feedback'));
130
131 $worksheet->setFormat("<l><f><vo>");
132 $worksheet->write_number($rowOffset + 1, 1, $analysed_item->avg);
133 $rowOffset++;
134 }
135 $rowOffset++;
136 return $rowOffset;
137 }
138
139 function print_item($item, $value = false, $readonly = false, $edit = false, $highlightrequire = false){
140 $align = get_string('thisdirection') == 'ltr' ? 'left' : 'right';
141
142 //get the range
143 $range_from_to = explode('|',$item->presentation);
144 //get the min-value
145 $range_from = isset($range_from_to[0]) ? intval($range_from_to[0]) : 0;
146 //get the max-value
147 $range_to = isset($range_from_to[1]) ? intval($range_from_to[1]) : 0;
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;?>">
156 <?php
157 echo format_text(stripslashes_safe($item->name) . $requiredmark, true, false, false);
158 switch(true) {
159 case ($range_from === 0 AND $range_to > 0):
160 echo ' ('.get_string('maximal', 'feedback').': '.$range_to.')';
161 break;
162 case ($range_from > 0 AND $range_to === 0):
163 echo ' ('.get_string('minimal', 'feedback').': '.$range_from.')';
164 break;
165 case ($range_from === 0 AND $range_to === 0):
166 break;
167 default:
168 echo ' ('.$range_from.'-'.$range_to.')';
169 break;
170 }
171 ?>
172 </td>
173 <td valign="top" align="<?php echo $align;?>">
174 <?php
175 if($readonly){
176 // print_simple_box_start($align);
177 print_box_start('generalbox boxalign'.$align);
178 echo $value ? $value : '&nbsp;';
179 // print_simple_box_end();
180 print_box_end();
181 }else {
182 ?>
183 <input type="text" name="<?php echo $item->typ.'_'.$item->id; ?>"
184 size="10"
185 maxlength="10"
186 value="<?php echo $value ? $value : ''; ?>" />
187 <?php
188 }
189 ?>
190 </td>
191 <?php
192 }
193
194 function check_value($value, $item) {
195 //if the item is not required, so the check is true if no value is given
196 if((!isset($value) OR $value == '') AND $item->required != 1) return true;
197 if(!is_numeric($value))return false;
198
199 $range_from_to = explode('|',$item->presentation);
200 $range_from = isset($range_from_to[0]) ? intval($range_from_to[0]) : 0;
201 $range_to = isset($range_from_to[1]) ? intval($range_from_to[1]) : 0;
202
203 switch(true) {
204 case ($range_from === 0 AND $range_to > 0):
205 if(intval($value) <= $range_to) return true;
206 break;
207 case ($range_from > 0 AND $range_to === 0):
208 if(intval($value) >= $range_from) return true;
209 break;
210 case ($range_from === 0 AND $range_to === 0):
211 return true;
212 break;
213 default:
214 if(intval($value) >= $range_from AND intval($value) <= $range_to) return true;
215 break;
216 }
217
218 return false;
219 }
220
221 function create_value($data) {
222 if($data AND $data != '') {
223 $data = intval($data);
224 }else {
225 $data = '';
226 }
227 return $data;
228 }
229
230 function get_presentation($data) {
231 return $data->numericrangefrom . '|'. $data->numericrangeto;
232 }
233
234 function get_hasvalue() {
235 return 1;
236 }
237}
238?>