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