MDL-24058 removed forgotted addslashes
[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
73043833
AG
36 //all items for dependitem
37 $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
a59ff6b0
AG
38 $commonparams = array('cmid'=>$cm->id,
39 'id'=>isset($item->id) ? $item->id : NULL,
40 'typ'=>$item->typ,
73043833 41 'items'=>$feedbackitems,
a59ff6b0 42 'feedback'=>$feedback->id);
d4b1d58c 43
a59ff6b0
AG
44 //build the form
45 $this->item_form = new feedback_info_form('edit_item.php', array('item'=>$item, 'common'=>$commonparams, 'positionlist'=>$positionlist, 'position'=>$position));
46 }
ed93c78b 47
a59ff6b0
AG
48 //this function only can used after the call of build_editform()
49 function show_editform() {
50 $this->item_form->display();
51 }
52
53 function is_cancelled() {
54 return $this->item_form->is_cancelled();
55 }
ed93c78b 56
a59ff6b0
AG
57 function get_data() {
58 if($this->item = $this->item_form->get_data()) {
59 return true;
60 }
61 return false;
62 }
d4b1d58c 63
a59ff6b0
AG
64 function save_item() {
65 global $DB;
66
67 if(!$item = $this->item_form->get_data()) {
68 return false;
69 }
70
b8baf0ad 71 if(isset($item->clone_item) AND $item->clone_item) {
9e1aed53
AG
72 $item->id = ''; //to clone this item
73 $item->position++;
74 }
75
45c2f92a 76 $item->hasvalue = $this->get_hasvalue();
a59ff6b0
AG
77 if(!$item->id) {
78 $item->id = $DB->insert_record('feedback_item', $item);
79 }else {
80 $DB->update_record('feedback_item', $item);
81 }
82
83 return $DB->get_record('feedback_item', array('id'=>$item->id));
ed93c78b 84 }
85
86 //liefert eine Struktur ->name, ->data = array(mit Antworten)
87 function get_analysed($item, $groupid = false, $courseid = false) {
d4b1d58c 88
ed93c78b 89 $presentation = $item->presentation;
90 $aVal = null;
91 $aVal->data = null;
92 $aVal->name = $item->name;
93 //$values = get_records('feedback_value', 'item', $item->id);
94 $values = feedback_get_group_values($item, $groupid, $courseid);
95 if($values) {
96 $data = array();
97 $datavalue = new object();
98 foreach($values as $value) {
d4b1d58c 99
ed93c78b 100 switch($presentation) {
101 case 1:
102 $datavalue->value = $value->value;
103 $datavalue->show = UserDate($datavalue->value);
104 break;
105 case 2:
106 $datavalue->value = $value->value;
107 $datavalue->show = $datavalue->value;
108 break;
109 case 3:
110 $datavalue->value = $value->value;
111 $datavalue->show = $datavalue->value;
112 break;
113 }
114
115 $data[] = $datavalue;
116 }
117 $aVal->data = $data;
118 }
119 return $aVal;
120 }
121
122 function get_printval($item, $value) {
d4b1d58c 123
ed93c78b 124 if(!isset($value->value)) return '';
125 return UserDate($value->value);
126 }
127
128 function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
129 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
130 $data = $analysed_item->data;
131 if(is_array($data)) {
b7a47958 132 echo '<tr><th colspan="2" align="left">'. $itemnr . '&nbsp;('. $item->label .') ' . $item->name .'</th></tr>';
ed93c78b 133 for($i = 0; $i < sizeof($data); $i++) {
134 echo '<tr><td colspan="2" valign="top" align="left">-&nbsp;&nbsp;' . str_replace("\n", '<br />', $data[$i]->show) . '</td></tr>';
135 }
136 }
137 // return $itemnr;
138 }
139
51129b99 140 function excelprint_item(&$worksheet, $rowOffset, $xlsFormats, $item, $groupid, $courseid = false) {
ed93c78b 141 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
142
51129b99
AG
143 // $worksheet->setFormat("<l><f><ro2><vo><c:green>");
144 $worksheet->write_string($rowOffset, 0, $item->label, $xlsFormats->head2);
145 $worksheet->write_string($rowOffset, 1, $item->name, $xlsFormats->head2);
ed93c78b 146 $data = $analysed_item->data;
147 if(is_array($data)) {
51129b99
AG
148 // $worksheet->setFormat("<l><ro2><vo>");
149 $worksheet->write_string($rowOffset, 2, $data[0]->show, $xlsFormats->value_bold);
ed93c78b 150 $rowOffset++;
151 for($i = 1; $i < sizeof($data); $i++) {
51129b99
AG
152 // $worksheet->setFormat("<l><vo>");
153 $worksheet->write_string($rowOffset, 2, $data[$i]->show, $xlsFormats->default);
ed93c78b 154 $rowOffset++;
155 }
156 }
157 $rowOffset++;
158 return $rowOffset;
159 }
9d5fbd65
AG
160
161 /**
162 * print the item at the edit-page of feedback
163 *
164 * @global object
165 * @param object $item
166 * @return void
167 */
168 function print_item_preview($item) {
169 global $USER, $DB, $OUTPUT;
170 $align = right_to_left() ? 'right' : 'left';
ed93c78b 171
9d5fbd65
AG
172 $presentation = $item->presentation;
173 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
afdb1920
AG
174
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 //print the question and label
194 echo '<div class="feedback_item_label_'.$align.'">';
195 echo '('.$item->label.') ';
196 echo format_text($item->name.$requiredmark, true, false, false);
73043833
AG
197 if($item->dependitem) {
198 if($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
199 echo ' <span class="feedback_depend">('.$dependitem->label.'-&gt;'.$item->dependvalue.')</span>';
200 }
201 }
afdb1920
AG
202 echo '</div>';
203 //print the presentation
204 echo '<div class="feedback_item_presentation_'.$align.'">';
205 echo '<input type="hidden" name="'.$item->typ.'_'.$item->id.'" value="'.$itemvalue.'" />';
206 echo '<span class="feedback_item_info">'.$itemshowvalue.'</span>';
207 echo '</div>';
9d5fbd65
AG
208 }
209
210 /**
211 * print the item at the complete-page of feedback
212 *
213 * @global object
214 * @param object $item
215 * @param string $value
216 * @param bool $highlightrequire
217 * @return void
218 */
219 function print_item_complete($item, $value = '', $highlightrequire = false) {
d4b1d58c 220 global $USER, $DB, $OUTPUT;
e372f4c7 221 $align = right_to_left() ? 'right' : 'left';
d4b1d58c 222
ed93c78b 223 $presentation = $item->presentation;
224 if($highlightrequire AND $item->required AND strval($value) == '') {
afdb1920 225 $highlight = ' missingrequire';
ed93c78b 226 }else {
227 $highlight = '';
228 }
229 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
afdb1920
AG
230
231 $feedback = $DB->get_record('feedback', array('id'=>$item->feedback));
232 $course = $DB->get_record('course', array('id'=>$feedback->course));
233 $coursecategory = $DB->get_record('course_categories', array('id'=>$course->category));
234 switch($presentation) {
235 case 1:
236 $itemvalue = time();
237 $itemshowvalue = UserDate($itemvalue);
238 break;
239 case 2:
240 $itemvalue = $course->shortname;
241 $itemshowvalue = $itemvalue;
242 break;
243 case 3:
244 $itemvalue = $coursecategory->name;
245 $itemshowvalue = $itemvalue;
246 break;
247 }
248
249 //print the question and label
250 echo '<div class="feedback_item_label_'.$align.$highlight.'">';
251 echo format_text($item->name.$requiredmark, true, false, false);
252 echo '</div>';
253
254 //print the presentation
255 echo '<div class="feedback_item_presentation_'.$align.'">';
256 echo '<input type="hidden" name="'.$item->typ.'_'.$item->id.'" value="'.$itemvalue.'" />';
257 echo '<span class="feedback_item_info">'.$itemshowvalue.'</span>';
258 echo '</div>';
9d5fbd65
AG
259 }
260
261 /**
262 * print the item at the complete-page of feedback
263 *
264 * @global object
265 * @param object $item
266 * @param string $value
267 * @return void
268 */
269 function print_item_show_value($item, $value = '') {
270 global $USER, $DB, $OUTPUT;
271 $align = right_to_left() ? 'right' : 'left';
272
273 $presentation = $item->presentation;
274 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
afdb1920
AG
275
276 //print the question and label
277 echo '<div class="feedback_item_label_'.$align.'">';
9d5fbd65
AG
278 echo '('.$item->label.') ';
279 echo format_text($item->name . $requiredmark, true, false, false);
afdb1920
AG
280 echo '</div>';
281
282 //print the presentation
283 echo $OUTPUT->box_start('generalbox boxalign'.$align);
284 echo $value ? UserDate($value):'&nbsp;';
285 echo $OUTPUT->box_end();
ed93c78b 286 }
287
288 function check_value($value, $item) {
289 return true;
290 }
291
292 function create_value($data) {
293 $data = addslashes(clean_text($data));
294 return $data;
295 }
296
73043833
AG
297 //compares the dbvalue with the dependvalue
298 //the values can be the shortname of a course or the category name
299 //the date is not compareable :(.
300 function compare_value($item, $dbvalue, $dependvalue) {
301 if($dbvalue == $dependvalue) {
302 return true;
303 }
304 return false;
305 }
306
ed93c78b 307 function get_presentation($data) {
308 return $data->infotype;
309 }
310
311 function get_hasvalue() {
312 return 1;
313 }
6cc1599e
AG
314
315 function can_switch_require() {
316 return false;
317 }
ed93c78b 318}