MDL-20296 creating common functions to handle units in numerical, calculated and...
[moodle.git] / mod / feedback / item / info / lib.php
CommitLineData
1adbd2c3 1<?php
01910dff 2defined('MOODLE_INTERNAL') OR die('not allowed');
ed93c78b 3require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
4
5class feedback_item_info extends feedback_item_base {
6 var $type = "info";
a59ff6b0
AG
7 var $commonparams;
8 var $item_form;
9 var $item;
10
ed93c78b 11 function init() {
d4b1d58c 12
ed93c78b 13 }
d4b1d58c 14
a59ff6b0
AG
15 function build_editform($item, $feedback, $cm) {
16 global $DB, $CFG;
ed93c78b 17 require_once('info_form.php');
d4b1d58c 18
a59ff6b0
AG
19 //get the lastposition number of the feedback_items
20 $position = $item->position;
21 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
22 if($position == -1){
23 $i_formselect_last = $lastposition + 1;
24 $i_formselect_value = $lastposition + 1;
25 $item->position = $lastposition + 1;
26 }else {
27 $i_formselect_last = $lastposition;
28 $i_formselect_value = $item->position;
29 }
30 //the elements for position dropdownlist
31 $positionlist = array_slice(range(0,$i_formselect_last),1,$i_formselect_last,true);
32
33 $item->presentation = empty($item->presentation) ? 1 : $item->presentation;
34 $item->required = 0;
d4b1d58c 35
a59ff6b0
AG
36 $commonparams = array('cmid'=>$cm->id,
37 'id'=>isset($item->id) ? $item->id : NULL,
38 'typ'=>$item->typ,
39 'feedback'=>$feedback->id);
d4b1d58c 40
a59ff6b0
AG
41 //build the form
42 $this->item_form = new feedback_info_form('edit_item.php', array('item'=>$item, 'common'=>$commonparams, 'positionlist'=>$positionlist, 'position'=>$position));
43 }
ed93c78b 44
a59ff6b0
AG
45 //this function only can used after the call of build_editform()
46 function show_editform() {
47 $this->item_form->display();
48 }
49
50 function is_cancelled() {
51 return $this->item_form->is_cancelled();
52 }
ed93c78b 53
a59ff6b0
AG
54 function get_data() {
55 if($this->item = $this->item_form->get_data()) {
56 return true;
57 }
58 return false;
59 }
d4b1d58c 60
a59ff6b0
AG
61 function save_item() {
62 global $DB;
63
64 if(!$item = $this->item_form->get_data()) {
65 return false;
66 }
67
45c2f92a 68 $item->hasvalue = $this->get_hasvalue();
a59ff6b0
AG
69 if(!$item->id) {
70 $item->id = $DB->insert_record('feedback_item', $item);
71 }else {
72 $DB->update_record('feedback_item', $item);
73 }
74
75 return $DB->get_record('feedback_item', array('id'=>$item->id));
ed93c78b 76 }
77
78 //liefert eine Struktur ->name, ->data = array(mit Antworten)
79 function get_analysed($item, $groupid = false, $courseid = false) {
d4b1d58c 80
ed93c78b 81 $presentation = $item->presentation;
82 $aVal = null;
83 $aVal->data = null;
84 $aVal->name = $item->name;
85 //$values = get_records('feedback_value', 'item', $item->id);
86 $values = feedback_get_group_values($item, $groupid, $courseid);
87 if($values) {
88 $data = array();
89 $datavalue = new object();
90 foreach($values as $value) {
d4b1d58c 91
ed93c78b 92 switch($presentation) {
93 case 1:
94 $datavalue->value = $value->value;
95 $datavalue->show = UserDate($datavalue->value);
96 break;
97 case 2:
98 $datavalue->value = $value->value;
99 $datavalue->show = $datavalue->value;
100 break;
101 case 3:
102 $datavalue->value = $value->value;
103 $datavalue->show = $datavalue->value;
104 break;
105 }
106
107 $data[] = $datavalue;
108 }
109 $aVal->data = $data;
110 }
111 return $aVal;
112 }
113
114 function get_printval($item, $value) {
d4b1d58c 115
ed93c78b 116 if(!isset($value->value)) return '';
117 return UserDate($value->value);
118 }
119
120 function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
121 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
122 $data = $analysed_item->data;
123 if(is_array($data)) {
b7a47958 124 echo '<tr><th colspan="2" align="left">'. $itemnr . '&nbsp;('. $item->label .') ' . $item->name .'</th></tr>';
ed93c78b 125 for($i = 0; $i < sizeof($data); $i++) {
126 echo '<tr><td colspan="2" valign="top" align="left">-&nbsp;&nbsp;' . str_replace("\n", '<br />', $data[$i]->show) . '</td></tr>';
127 }
128 }
129 // return $itemnr;
130 }
131
51129b99 132 function excelprint_item(&$worksheet, $rowOffset, $xlsFormats, $item, $groupid, $courseid = false) {
ed93c78b 133 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
134
51129b99
AG
135 // $worksheet->setFormat("<l><f><ro2><vo><c:green>");
136 $worksheet->write_string($rowOffset, 0, $item->label, $xlsFormats->head2);
137 $worksheet->write_string($rowOffset, 1, $item->name, $xlsFormats->head2);
ed93c78b 138 $data = $analysed_item->data;
139 if(is_array($data)) {
51129b99
AG
140 // $worksheet->setFormat("<l><ro2><vo>");
141 $worksheet->write_string($rowOffset, 2, $data[0]->show, $xlsFormats->value_bold);
ed93c78b 142 $rowOffset++;
143 for($i = 1; $i < sizeof($data); $i++) {
51129b99
AG
144 // $worksheet->setFormat("<l><vo>");
145 $worksheet->write_string($rowOffset, 2, $data[$i]->show, $xlsFormats->default);
ed93c78b 146 $rowOffset++;
147 }
148 }
149 $rowOffset++;
150 return $rowOffset;
151 }
9d5fbd65
AG
152
153 /**
154 * print the item at the edit-page of feedback
155 *
156 * @global object
157 * @param object $item
158 * @return void
159 */
160 function print_item_preview($item) {
161 global $USER, $DB, $OUTPUT;
162 $align = right_to_left() ? 'right' : 'left';
ed93c78b 163
9d5fbd65
AG
164 $presentation = $item->presentation;
165 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
166 ?>
167 <td valign="top" align="<?php echo $align;?>">
168 <?php
169 echo '('.$item->label.') ';
170 echo format_text($item->name . $requiredmark, true, false, false);
171 ?>
172 </td>
173 <td valign="top" align="<?php echo $align;?>">
174 <?php
175 $feedback = $DB->get_record('feedback', array('id'=>$item->feedback));
176 $course = $DB->get_record('course', array('id'=>$feedback->course));
177 $coursecategory = $DB->get_record('course_categories', array('id'=>$course->category));
178 switch($presentation) {
179 case 1:
180 $itemvalue = time();
181 $itemshowvalue = UserDate($itemvalue);
182 break;
183 case 2:
184 $itemvalue = $course->shortname;
185 $itemshowvalue = $itemvalue;
186 break;
187 case 3:
188 $itemvalue = $coursecategory->name;
189 $itemshowvalue = $itemvalue;
190 break;
191 }
192 ?>
193 <input type="hidden" name="<?php echo $item->typ . '_' . $item->id;?>"
194 value="<?php echo $itemvalue;?>" />
195 <span><?php echo $itemshowvalue;?></span>
196 </td>
197 <?php
198 }
199
200 /**
201 * print the item at the complete-page of feedback
202 *
203 * @global object
204 * @param object $item
205 * @param string $value
206 * @param bool $highlightrequire
207 * @return void
208 */
209 function print_item_complete($item, $value = '', $highlightrequire = false) {
d4b1d58c 210 global $USER, $DB, $OUTPUT;
e372f4c7 211 $align = right_to_left() ? 'right' : 'left';
d4b1d58c 212
ed93c78b 213 $presentation = $item->presentation;
214 if($highlightrequire AND $item->required AND strval($value) == '') {
215 $highlight = 'bgcolor="#FFAAAA" class="missingrequire"';
216 }else {
217 $highlight = '';
218 }
219 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
220 ?>
b7a47958 221 <td <?php echo $highlight;?> valign="top" align="<?php echo $align;?>">
222 <?php
b7a47958 223 echo format_text($item->name . $requiredmark, true, false, false);
224 ?>
225 </td>
ed93c78b 226 <td valign="top" align="<?php echo $align;?>">
227 <?php
ed93c78b 228 $feedback = $DB->get_record('feedback', array('id'=>$item->feedback));
229 $course = $DB->get_record('course', array('id'=>$feedback->course));
230 $coursecategory = $DB->get_record('course_categories', array('id'=>$course->category));
231 switch($presentation) {
232 case 1:
233 $itemvalue = time();
234 $itemshowvalue = UserDate($itemvalue);
235 break;
236 case 2:
237 $itemvalue = $course->shortname;
238 $itemshowvalue = $itemvalue;
239 break;
240 case 3:
241 $itemvalue = $coursecategory->name;
242 $itemshowvalue = $itemvalue;
243 break;
244 }
245 ?>
246 <input type="hidden" name="<?php echo $item->typ . '_' . $item->id;?>"
247 value="<?php echo $itemvalue;?>" />
248 <span><?php echo $itemshowvalue;?></span>
9d5fbd65 249 </td>
ed93c78b 250 <?php
9d5fbd65
AG
251 }
252
253 /**
254 * print the item at the complete-page of feedback
255 *
256 * @global object
257 * @param object $item
258 * @param string $value
259 * @return void
260 */
261 function print_item_show_value($item, $value = '') {
262 global $USER, $DB, $OUTPUT;
263 $align = right_to_left() ? 'right' : 'left';
264
265 $presentation = $item->presentation;
266 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
267 ?>
268 <td valign="top" align="<?php echo $align;?>">
269 <?php
270 echo '('.$item->label.') ';
271 echo format_text($item->name . $requiredmark, true, false, false);
272 ?>
273 </td>
274 <td valign="top" align="<?php echo $align;?>">
275 <?php
276 echo $OUTPUT->box_start('generalbox boxalign'.$align);
277 echo $value ? UserDate($value):'&nbsp;';
278 echo $OUTPUT->box_end();
ed93c78b 279 ?>
280 </td>
281 <?php
282 }
283
284 function check_value($value, $item) {
285 return true;
286 }
287
288 function create_value($data) {
289 $data = addslashes(clean_text($data));
290 return $data;
291 }
292
293 function get_presentation($data) {
294 return $data->infotype;
295 }
296
297 function get_hasvalue() {
298 return 1;
299 }
300}
301?>