MDL-37800 mod_feedback - item "information" does not appear correctly
[moodle.git] / mod / feedback / item / info / lib.php
CommitLineData
1adbd2c3 1<?php
9c5bc7a5
AG
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
01910dff 17defined('MOODLE_INTERNAL') OR die('not allowed');
ed93c78b 18require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
19
20class feedback_item_info extends feedback_item_base {
9c5bc7a5
AG
21 protected $type = "info";
22 private $commonparams;
23 private $item_form;
24 private $item;
a59ff6b0 25
9c5bc7a5 26 public function init() {
d4b1d58c 27
ed93c78b 28 }
d4b1d58c 29
9c5bc7a5 30 public function build_editform($item, $feedback, $cm) {
a59ff6b0 31 global $DB, $CFG;
ed93c78b 32 require_once('info_form.php');
d4b1d58c 33
a59ff6b0
AG
34 //get the lastposition number of the feedback_items
35 $position = $item->position;
36 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
9c5bc7a5 37 if ($position == -1) {
a59ff6b0
AG
38 $i_formselect_last = $lastposition + 1;
39 $i_formselect_value = $lastposition + 1;
40 $item->position = $lastposition + 1;
9c5bc7a5 41 } else {
a59ff6b0
AG
42 $i_formselect_last = $lastposition;
43 $i_formselect_value = $item->position;
44 }
45 //the elements for position dropdownlist
9c5bc7a5 46 $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true);
1d28bdbd 47
a59ff6b0
AG
48 $item->presentation = empty($item->presentation) ? 1 : $item->presentation;
49 $item->required = 0;
d4b1d58c 50
73043833
AG
51 //all items for dependitem
52 $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
a59ff6b0 53 $commonparams = array('cmid'=>$cm->id,
9c5bc7a5 54 'id'=>isset($item->id) ? $item->id : null,
a59ff6b0 55 'typ'=>$item->typ,
73043833 56 'items'=>$feedbackitems,
a59ff6b0 57 'feedback'=>$feedback->id);
d4b1d58c 58
a59ff6b0 59 //build the form
9c5bc7a5
AG
60 $this->item_form = new feedback_info_form('edit_item.php',
61 array('item'=>$item,
62 'common'=>$commonparams,
63 'positionlist'=>$positionlist,
64 'position' => $position));
a59ff6b0 65 }
ed93c78b 66
a59ff6b0 67 //this function only can used after the call of build_editform()
9c5bc7a5 68 public function show_editform() {
a59ff6b0
AG
69 $this->item_form->display();
70 }
1d28bdbd 71
9c5bc7a5 72 public function is_cancelled() {
a59ff6b0
AG
73 return $this->item_form->is_cancelled();
74 }
ed93c78b 75
9c5bc7a5
AG
76 public function get_data() {
77 if ($this->item = $this->item_form->get_data()) {
a59ff6b0
AG
78 return true;
79 }
80 return false;
81 }
d4b1d58c 82
9c5bc7a5 83 public function save_item() {
a59ff6b0 84 global $DB;
1d28bdbd 85
9c5bc7a5 86 if (!$item = $this->item_form->get_data()) {
a59ff6b0
AG
87 return false;
88 }
1d28bdbd 89
9c5bc7a5 90 if (isset($item->clone_item) AND $item->clone_item) {
9e1aed53
AG
91 $item->id = ''; //to clone this item
92 $item->position++;
93 }
1d28bdbd 94
45c2f92a 95 $item->hasvalue = $this->get_hasvalue();
9c5bc7a5 96 if (!$item->id) {
a59ff6b0 97 $item->id = $DB->insert_record('feedback_item', $item);
9c5bc7a5 98 } else {
a59ff6b0
AG
99 $DB->update_record('feedback_item', $item);
100 }
1d28bdbd 101
a59ff6b0 102 return $DB->get_record('feedback_item', array('id'=>$item->id));
ed93c78b 103 }
104
105 //liefert eine Struktur ->name, ->data = array(mit Antworten)
9c5bc7a5 106 public function get_analysed($item, $groupid = false, $courseid = false) {
d4b1d58c 107
ed93c78b 108 $presentation = $item->presentation;
411fd747 109 $analysed_val = new stdClass();;
9c5bc7a5
AG
110 $analysed_val->data = null;
111 $analysed_val->name = $item->name;
ed93c78b 112 $values = feedback_get_group_values($item, $groupid, $courseid);
9c5bc7a5 113 if ($values) {
ed93c78b 114 $data = array();
9c5bc7a5 115 foreach ($values as $value) {
c962e3f1 116 $datavalue = new stdClass();
d4b1d58c 117
ed93c78b 118 switch($presentation) {
119 case 1:
120 $datavalue->value = $value->value;
9c5bc7a5 121 $datavalue->show = userdate($datavalue->value);
ed93c78b 122 break;
123 case 2:
124 $datavalue->value = $value->value;
125 $datavalue->show = $datavalue->value;
126 break;
127 case 3:
128 $datavalue->value = $value->value;
129 $datavalue->show = $datavalue->value;
130 break;
131 }
132
133 $data[] = $datavalue;
134 }
9c5bc7a5 135 $analysed_val->data = $data;
ed93c78b 136 }
9c5bc7a5 137 return $analysed_val;
ed93c78b 138 }
139
9c5bc7a5 140 public function get_printval($item, $value) {
d4b1d58c 141
9c5bc7a5
AG
142 if (!isset($value->value)) {
143 return '';
144 }
145 return userdate($value->value);
ed93c78b 146 }
147
9c5bc7a5 148 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
ed93c78b 149 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
150 $data = $analysed_item->data;
80c12897 151 if (is_array($data)) {
9c5bc7a5
AG
152 echo '<tr><th colspan="2" align="left">';
153 echo $itemnr.'&nbsp;('.$item->label.') '.$item->name;
154 echo '</th></tr>';
155 $sizeofdata = count($data);
80c12897 156 for ($i = 0; $i < $sizeofdata; $i++) {
9c5bc7a5
AG
157 echo '<tr><td colspan="2" valign="top" align="left">-&nbsp;&nbsp;';
158 echo str_replace("\n", '<br />', $data[$i]->show);
159 echo '</td></tr>';
ed93c78b 160 }
161 }
ed93c78b 162 }
163
9c5bc7a5
AG
164 public function excelprint_item(&$worksheet, $row_offset,
165 $xls_formats, $item,
166 $groupid, $courseid = false) {
ed93c78b 167 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
168
9c5bc7a5
AG
169 $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
170 $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
ed93c78b 171 $data = $analysed_item->data;
80c12897 172 if (is_array($data)) {
9c5bc7a5
AG
173 $worksheet->write_string($row_offset, 2, $data[0]->show, $xls_formats->value_bold);
174 $row_offset++;
175 $sizeofdata = count($data);
80c12897 176 for ($i = 1; $i < $sizeofdata; $i++) {
9c5bc7a5
AG
177 $worksheet->write_string($row_offset, 2, $data[$i]->show, $xls_formats->default);
178 $row_offset++;
ed93c78b 179 }
180 }
9c5bc7a5
AG
181 $row_offset++;
182 return $row_offset;
ed93c78b 183 }
1d28bdbd
PS
184
185 /**
9d5fbd65
AG
186 * print the item at the edit-page of feedback
187 *
188 * @global object
189 * @param object $item
190 * @return void
191 */
9c5bc7a5 192 public function print_item_preview($item) {
9d5fbd65 193 global $USER, $DB, $OUTPUT;
ed93c78b 194
9c5bc7a5 195 $align = right_to_left() ? 'right' : 'left';
9d5fbd65
AG
196 $presentation = $item->presentation;
197 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
1d28bdbd 198
9c5bc7a5
AG
199 if ($item->feedback) {
200 $courseid = $DB->get_field('feedback', 'course', array('id'=>$item->feedback));
201 } else { // the item must be a template item
202 $cmid = required_param('id', PARAM_INT);
203 $courseid = $DB->get_field('course_modules', 'course', array('id'=>$cmid));
204 }
205 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
206 print_error('error');
207 }
208 if ($course->id !== SITEID) {
209 $coursecategory = $DB->get_record('course_categories', array('id'=>$course->category));
210 } else {
211 $coursecategory = false;
212 }
afdb1920
AG
213 switch($presentation) {
214 case 1:
215 $itemvalue = time();
9c5bc7a5 216 $itemshowvalue = userdate($itemvalue);
afdb1920
AG
217 break;
218 case 2:
8ebbb06a 219 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
9c5bc7a5
AG
220 $itemvalue = format_string($course->shortname,
221 true,
222 array('context' => $coursecontext));
223
afdb1920
AG
224 $itemshowvalue = $itemvalue;
225 break;
226 case 3:
9c5bc7a5
AG
227 if ($coursecategory) {
228 $category_context = get_context_instance(CONTEXT_COURSECAT, $coursecategory->id);
229 $itemvalue = format_string($coursecategory->name,
230 true,
231 array('context' => $category_context));
232
233 $itemshowvalue = $itemvalue;
234 } else {
235 $itemvalue = '';
236 $itemshowvalue = '';
237 }
afdb1920
AG
238 break;
239 }
1d28bdbd 240
afdb1920
AG
241 //print the question and label
242 echo '<div class="feedback_item_label_'.$align.'">';
243 echo '('.$item->label.') ';
244 echo format_text($item->name.$requiredmark, true, false, false);
9c5bc7a5
AG
245 if ($item->dependitem) {
246 if ($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
247 echo ' <span class="feedback_depend">';
248 echo '('.$dependitem->label.'-&gt;'.$item->dependvalue.')';
249 echo '</span>';
73043833
AG
250 }
251 }
afdb1920
AG
252 echo '</div>';
253 //print the presentation
254 echo '<div class="feedback_item_presentation_'.$align.'">';
9c5bc7a5
AG
255 echo '<input type="hidden" name="'.$item->typ.'_'.$item->id.'" value="'.$itemvalue.'" />';
256 echo '<span class="feedback_item_info">'.$itemshowvalue.'</span>';
afdb1920 257 echo '</div>';
9d5fbd65 258 }
1d28bdbd
PS
259
260 /**
9d5fbd65
AG
261 * print the item at the complete-page of feedback
262 *
263 * @global object
264 * @param object $item
265 * @param string $value
266 * @param bool $highlightrequire
267 * @return void
268 */
9c5bc7a5 269 public function print_item_complete($item, $value = '', $highlightrequire = false) {
d4b1d58c 270 global $USER, $DB, $OUTPUT;
e372f4c7 271 $align = right_to_left() ? 'right' : 'left';
d4b1d58c 272
ed93c78b 273 $presentation = $item->presentation;
9c5bc7a5 274 if ($highlightrequire AND $item->required AND strval($value) == '') {
afdb1920 275 $highlight = ' missingrequire';
9c5bc7a5 276 } else {
ed93c78b 277 $highlight = '';
278 }
279 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
afdb1920
AG
280
281 $feedback = $DB->get_record('feedback', array('id'=>$item->feedback));
c962e3f1
AG
282
283 if ($courseid = optional_param('courseid', 0, PARAM_INT)) {
284 $course = $DB->get_record('course', array('id'=>$courseid));
285 } else {
286 $course = $DB->get_record('course', array('id'=>$feedback->course));
287 }
9c5bc7a5
AG
288
289 if ($course->id !== SITEID) {
290 $coursecategory = $DB->get_record('course_categories', array('id'=>$course->category));
291 } else {
292 $coursecategory = false;
293 }
294
afdb1920
AG
295 switch($presentation) {
296 case 1:
c962e3f1
AG
297 if ($feedback->anonymous == FEEDBACK_ANONYMOUS_YES) {
298 $itemvalue = 0;
299 $itemshowvalue = '-';
300 } else {
301 $itemvalue = time();
302 $itemshowvalue = userdate($itemvalue);
303 }
afdb1920
AG
304 break;
305 case 2:
8ebbb06a 306 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
9c5bc7a5
AG
307 $itemvalue = format_string($course->shortname,
308 true,
309 array('context' => $coursecontext));
310
afdb1920
AG
311 $itemshowvalue = $itemvalue;
312 break;
313 case 3:
9c5bc7a5
AG
314 if ($coursecategory) {
315 $category_context = get_context_instance(CONTEXT_COURSECAT, $coursecategory->id);
316 $itemvalue = format_string($coursecategory->name,
317 true,
318 array('context' => $category_context));
319
320 $itemshowvalue = $itemvalue;
321 } else {
322 $itemvalue = '';
323 $itemshowvalue = '';
324 }
afdb1920
AG
325 break;
326 }
1d28bdbd 327
afdb1920
AG
328 //print the question and label
329 echo '<div class="feedback_item_label_'.$align.$highlight.'">';
330 echo format_text($item->name.$requiredmark, true, false, false);
331 echo '</div>';
1d28bdbd 332
afdb1920
AG
333 //print the presentation
334 echo '<div class="feedback_item_presentation_'.$align.'">';
9c5bc7a5
AG
335 echo '<input type="hidden" name="'.$item->typ.'_'.$item->id.'" value="'.$itemvalue.'" />';
336 echo '<span class="feedback_item_info">'.$itemshowvalue.'</span>';
afdb1920 337 echo '</div>';
9d5fbd65
AG
338 }
339
1d28bdbd 340 /**
9d5fbd65
AG
341 * print the item at the complete-page of feedback
342 *
343 * @global object
344 * @param object $item
345 * @param string $value
346 * @return void
347 */
9c5bc7a5 348 public function print_item_show_value($item, $value = '') {
9d5fbd65
AG
349 global $USER, $DB, $OUTPUT;
350 $align = right_to_left() ? 'right' : 'left';
351
352 $presentation = $item->presentation;
353 $requiredmark = ($item->required == 1)?'<span class="feedback_required_mark">*</span>':'';
1d28bdbd 354
9c5bc7a5
AG
355 if ($presentation == 1) {
356 $value = $value ? userdate($value) : '&nbsp;';
bf1b9ef7
AG
357 }
358
afdb1920
AG
359 //print the question and label
360 echo '<div class="feedback_item_label_'.$align.'">';
9d5fbd65
AG
361 echo '('.$item->label.') ';
362 echo format_text($item->name . $requiredmark, true, false, false);
afdb1920 363 echo '</div>';
1d28bdbd 364
afdb1920
AG
365 //print the presentation
366 echo $OUTPUT->box_start('generalbox boxalign'.$align);
bf1b9ef7 367 echo $value;
afdb1920 368 echo $OUTPUT->box_end();
ed93c78b 369 }
370
9c5bc7a5 371 public function check_value($value, $item) {
ed93c78b 372 return true;
373 }
374
9c5bc7a5 375 public function create_value($data) {
1d28bdbd 376 $data = clean_text($data);
ed93c78b 377 return $data;
378 }
379
73043833
AG
380 //compares the dbvalue with the dependvalue
381 //the values can be the shortname of a course or the category name
382 //the date is not compareable :(.
9c5bc7a5
AG
383 public function compare_value($item, $dbvalue, $dependvalue) {
384 if ($dbvalue == $dependvalue) {
73043833
AG
385 return true;
386 }
387 return false;
388 }
1d28bdbd 389
9c5bc7a5 390 public function get_presentation($data) {
ed93c78b 391 return $data->infotype;
392 }
393
9c5bc7a5 394 public function get_hasvalue() {
ed93c78b 395 return 1;
396 }
1d28bdbd 397
9c5bc7a5 398 public function can_switch_require() {
6cc1599e
AG
399 return false;
400 }
e7fc2ff4
AG
401
402 public function value_type() {
c962e3f1 403 return PARAM_TEXT;
e7fc2ff4
AG
404 }
405
d2448b8b 406 public function clean_input_value($value) {
e7fc2ff4
AG
407 return clean_param($value, $this->value_type());
408 }
ed93c78b 409}