weekly release 2.2dev
[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:
8ebbb06a
SH
186 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
187 $itemvalue = format_string($course->shortname, true, array('context' => $coursecontext));
afdb1920
AG
188 $itemshowvalue = $itemvalue;
189 break;
190 case 3:
63390481 191 $itemvalue = format_string($coursecategory->name, true, array('context' => get_context_instance(CONTEXT_COURSECAT, $coursecategory->id)));
afdb1920
AG
192 $itemshowvalue = $itemvalue;
193 break;
194 }
1d28bdbd 195
afdb1920
AG
196 //print the question and label
197 echo '<div class="feedback_item_label_'.$align.'">';
198 echo '('.$item->label.') ';
199 echo format_text($item->name.$requiredmark, true, false, false);
73043833
AG
200 if($item->dependitem) {
201 if($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
202 echo ' <span class="feedback_depend">('.$dependitem->label.'-&gt;'.$item->dependvalue.')</span>';
203 }
204 }
afdb1920
AG
205 echo '</div>';
206 //print the presentation
207 echo '<div class="feedback_item_presentation_'.$align.'">';
208 echo '<input type="hidden" name="'.$item->typ.'_'.$item->id.'" value="'.$itemvalue.'" />';
209 echo '<span class="feedback_item_info">'.$itemshowvalue.'</span>';
210 echo '</div>';
9d5fbd65 211 }
1d28bdbd
PS
212
213 /**
9d5fbd65
AG
214 * print the item at the complete-page of feedback
215 *
216 * @global object
217 * @param object $item
218 * @param string $value
219 * @param bool $highlightrequire
220 * @return void
221 */
222 function print_item_complete($item, $value = '', $highlightrequire = false) {
d4b1d58c 223 global $USER, $DB, $OUTPUT;
e372f4c7 224 $align = right_to_left() ? 'right' : 'left';
d4b1d58c 225
ed93c78b 226 $presentation = $item->presentation;
227 if($highlightrequire AND $item->required AND strval($value) == '') {
afdb1920 228 $highlight = ' missingrequire';
ed93c78b 229 }else {
230 $highlight = '';
231 }
232 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
afdb1920
AG
233
234 $feedback = $DB->get_record('feedback', array('id'=>$item->feedback));
235 $course = $DB->get_record('course', array('id'=>$feedback->course));
236 $coursecategory = $DB->get_record('course_categories', array('id'=>$course->category));
237 switch($presentation) {
238 case 1:
239 $itemvalue = time();
240 $itemshowvalue = UserDate($itemvalue);
241 break;
242 case 2:
8ebbb06a
SH
243 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
244 $itemvalue = format_string($course->shortname, true, array('context' => $coursecontext));
afdb1920
AG
245 $itemshowvalue = $itemvalue;
246 break;
247 case 3:
63390481 248 $itemvalue = format_string($coursecategory->name, true, array('context' => get_context_instance(CONTEXT_COURSECAT, $coursecategory->id)));
afdb1920
AG
249 $itemshowvalue = $itemvalue;
250 break;
251 }
1d28bdbd 252
afdb1920
AG
253 //print the question and label
254 echo '<div class="feedback_item_label_'.$align.$highlight.'">';
255 echo format_text($item->name.$requiredmark, true, false, false);
256 echo '</div>';
1d28bdbd 257
afdb1920
AG
258 //print the presentation
259 echo '<div class="feedback_item_presentation_'.$align.'">';
260 echo '<input type="hidden" name="'.$item->typ.'_'.$item->id.'" value="'.$itemvalue.'" />';
261 echo '<span class="feedback_item_info">'.$itemshowvalue.'</span>';
262 echo '</div>';
9d5fbd65
AG
263 }
264
1d28bdbd 265 /**
9d5fbd65
AG
266 * print the item at the complete-page of feedback
267 *
268 * @global object
269 * @param object $item
270 * @param string $value
271 * @return void
272 */
273 function print_item_show_value($item, $value = '') {
274 global $USER, $DB, $OUTPUT;
275 $align = right_to_left() ? 'right' : 'left';
276
277 $presentation = $item->presentation;
278 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
1d28bdbd 279
bf1b9ef7
AG
280 if($presentation == 1) {
281 $value = $value ? UserDate($value) : '&nbsp;';
282 }
283
afdb1920
AG
284 //print the question and label
285 echo '<div class="feedback_item_label_'.$align.'">';
9d5fbd65
AG
286 echo '('.$item->label.') ';
287 echo format_text($item->name . $requiredmark, true, false, false);
afdb1920 288 echo '</div>';
1d28bdbd 289
afdb1920
AG
290 //print the presentation
291 echo $OUTPUT->box_start('generalbox boxalign'.$align);
bf1b9ef7 292 echo $value;
afdb1920 293 echo $OUTPUT->box_end();
ed93c78b 294 }
295
296 function check_value($value, $item) {
297 return true;
298 }
299
300 function create_value($data) {
1d28bdbd 301 $data = clean_text($data);
ed93c78b 302 return $data;
303 }
304
73043833
AG
305 //compares the dbvalue with the dependvalue
306 //the values can be the shortname of a course or the category name
307 //the date is not compareable :(.
308 function compare_value($item, $dbvalue, $dependvalue) {
309 if($dbvalue == $dependvalue) {
310 return true;
311 }
312 return false;
313 }
1d28bdbd 314
ed93c78b 315 function get_presentation($data) {
316 return $data->infotype;
317 }
318
319 function get_hasvalue() {
320 return 1;
321 }
1d28bdbd 322
6cc1599e
AG
323 function can_switch_require() {
324 return false;
325 }
ed93c78b 326}