MDL-25637 Fixed whitespace
[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);
1d28bdbd 32
a59ff6b0
AG
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 }
1d28bdbd 52
a59ff6b0
AG
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;
1d28bdbd 66
a59ff6b0
AG
67 if(!$item = $this->item_form->get_data()) {
68 return false;
69 }
1d28bdbd 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 }
1d28bdbd 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 }
1d28bdbd 82
a59ff6b0 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();
39790bd8 97 $datavalue = new stdClass();
ed93c78b 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;
80c12897 131 if (is_array($data)) {
b7a47958 132 echo '<tr><th colspan="2" align="left">'. $itemnr . '&nbsp;('. $item->label .') ' . $item->name .'</th></tr>';
b692e4aa 133 $sizeofdata = sizeof($data);
80c12897 134 for ($i = 0; $i < $sizeofdata; $i++) {
ed93c78b 135 echo '<tr><td colspan="2" valign="top" align="left">-&nbsp;&nbsp;' . str_replace("\n", '<br />', $data[$i]->show) . '</td></tr>';
136 }
137 }
138 // return $itemnr;
139 }
140
51129b99 141 function excelprint_item(&$worksheet, $rowOffset, $xlsFormats, $item, $groupid, $courseid = false) {
ed93c78b 142 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
143
51129b99
AG
144 // $worksheet->setFormat("<l><f><ro2><vo><c:green>");
145 $worksheet->write_string($rowOffset, 0, $item->label, $xlsFormats->head2);
146 $worksheet->write_string($rowOffset, 1, $item->name, $xlsFormats->head2);
ed93c78b 147 $data = $analysed_item->data;
80c12897 148 if (is_array($data)) {
51129b99
AG
149 // $worksheet->setFormat("<l><ro2><vo>");
150 $worksheet->write_string($rowOffset, 2, $data[0]->show, $xlsFormats->value_bold);
ed93c78b 151 $rowOffset++;
b692e4aa 152 $sizeofdata = sizeof($data);
80c12897 153 for ($i = 1; $i < $sizeofdata; $i++) {
51129b99
AG
154 // $worksheet->setFormat("<l><vo>");
155 $worksheet->write_string($rowOffset, 2, $data[$i]->show, $xlsFormats->default);
ed93c78b 156 $rowOffset++;
157 }
158 }
159 $rowOffset++;
160 return $rowOffset;
161 }
1d28bdbd
PS
162
163 /**
9d5fbd65
AG
164 * print the item at the edit-page of feedback
165 *
166 * @global object
167 * @param object $item
168 * @return void
169 */
170 function print_item_preview($item) {
171 global $USER, $DB, $OUTPUT;
172 $align = right_to_left() ? 'right' : 'left';
ed93c78b 173
9d5fbd65
AG
174 $presentation = $item->presentation;
175 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
1d28bdbd 176
afdb1920
AG
177 $feedback = $DB->get_record('feedback', array('id'=>$item->feedback));
178 $course = $DB->get_record('course', array('id'=>$feedback->course));
179 $coursecategory = $DB->get_record('course_categories', array('id'=>$course->category));
180 switch($presentation) {
181 case 1:
182 $itemvalue = time();
183 $itemshowvalue = UserDate($itemvalue);
184 break;
185 case 2:
186 $itemvalue = $course->shortname;
187 $itemshowvalue = $itemvalue;
188 break;
189 case 3:
190 $itemvalue = $coursecategory->name;
191 $itemshowvalue = $itemvalue;
192 break;
193 }
1d28bdbd 194
afdb1920
AG
195 //print the question and label
196 echo '<div class="feedback_item_label_'.$align.'">';
197 echo '('.$item->label.') ';
198 echo format_text($item->name.$requiredmark, true, false, false);
73043833
AG
199 if($item->dependitem) {
200 if($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
201 echo ' <span class="feedback_depend">('.$dependitem->label.'-&gt;'.$item->dependvalue.')</span>';
202 }
203 }
afdb1920
AG
204 echo '</div>';
205 //print the presentation
206 echo '<div class="feedback_item_presentation_'.$align.'">';
207 echo '<input type="hidden" name="'.$item->typ.'_'.$item->id.'" value="'.$itemvalue.'" />';
208 echo '<span class="feedback_item_info">'.$itemshowvalue.'</span>';
209 echo '</div>';
9d5fbd65 210 }
1d28bdbd
PS
211
212 /**
9d5fbd65
AG
213 * print the item at the complete-page of feedback
214 *
215 * @global object
216 * @param object $item
217 * @param string $value
218 * @param bool $highlightrequire
219 * @return void
220 */
221 function print_item_complete($item, $value = '', $highlightrequire = false) {
d4b1d58c 222 global $USER, $DB, $OUTPUT;
e372f4c7 223 $align = right_to_left() ? 'right' : 'left';
d4b1d58c 224
ed93c78b 225 $presentation = $item->presentation;
226 if($highlightrequire AND $item->required AND strval($value) == '') {
afdb1920 227 $highlight = ' missingrequire';
ed93c78b 228 }else {
229 $highlight = '';
230 }
231 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
afdb1920
AG
232
233 $feedback = $DB->get_record('feedback', array('id'=>$item->feedback));
234 $course = $DB->get_record('course', array('id'=>$feedback->course));
235 $coursecategory = $DB->get_record('course_categories', array('id'=>$course->category));
236 switch($presentation) {
237 case 1:
238 $itemvalue = time();
239 $itemshowvalue = UserDate($itemvalue);
240 break;
241 case 2:
242 $itemvalue = $course->shortname;
243 $itemshowvalue = $itemvalue;
244 break;
245 case 3:
246 $itemvalue = $coursecategory->name;
247 $itemshowvalue = $itemvalue;
248 break;
249 }
1d28bdbd 250
afdb1920
AG
251 //print the question and label
252 echo '<div class="feedback_item_label_'.$align.$highlight.'">';
253 echo format_text($item->name.$requiredmark, true, false, false);
254 echo '</div>';
1d28bdbd 255
afdb1920
AG
256 //print the presentation
257 echo '<div class="feedback_item_presentation_'.$align.'">';
258 echo '<input type="hidden" name="'.$item->typ.'_'.$item->id.'" value="'.$itemvalue.'" />';
259 echo '<span class="feedback_item_info">'.$itemshowvalue.'</span>';
260 echo '</div>';
9d5fbd65
AG
261 }
262
1d28bdbd 263 /**
9d5fbd65
AG
264 * print the item at the complete-page of feedback
265 *
266 * @global object
267 * @param object $item
268 * @param string $value
269 * @return void
270 */
271 function print_item_show_value($item, $value = '') {
272 global $USER, $DB, $OUTPUT;
273 $align = right_to_left() ? 'right' : 'left';
274
275 $presentation = $item->presentation;
276 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
1d28bdbd 277
bf1b9ef7
AG
278 if($presentation == 1) {
279 $value = $value ? UserDate($value) : '&nbsp;';
280 }
281
afdb1920
AG
282 //print the question and label
283 echo '<div class="feedback_item_label_'.$align.'">';
9d5fbd65
AG
284 echo '('.$item->label.') ';
285 echo format_text($item->name . $requiredmark, true, false, false);
afdb1920 286 echo '</div>';
1d28bdbd 287
afdb1920
AG
288 //print the presentation
289 echo $OUTPUT->box_start('generalbox boxalign'.$align);
bf1b9ef7 290 echo $value;
afdb1920 291 echo $OUTPUT->box_end();
ed93c78b 292 }
293
294 function check_value($value, $item) {
295 return true;
296 }
297
298 function create_value($data) {
1d28bdbd 299 $data = clean_text($data);
ed93c78b 300 return $data;
301 }
302
73043833
AG
303 //compares the dbvalue with the dependvalue
304 //the values can be the shortname of a course or the category name
305 //the date is not compareable :(.
306 function compare_value($item, $dbvalue, $dependvalue) {
307 if($dbvalue == $dependvalue) {
308 return true;
309 }
310 return false;
311 }
1d28bdbd 312
ed93c78b 313 function get_presentation($data) {
314 return $data->infotype;
315 }
316
317 function get_hasvalue() {
318 return 1;
319 }
1d28bdbd 320
6cc1599e
AG
321 function can_switch_require() {
322 return false;
323 }
ed93c78b 324}