2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 defined('MOODLE_INTERNAL') OR die('not allowed');
18 require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
20 class feedback_item_info extends feedback_item_base {
21 protected $type = "info";
22 private $commonparams;
26 public function init() {
30 public function build_editform($item, $feedback, $cm) {
32 require_once('info_form.php');
34 //get the lastposition number of the feedback_items
35 $position = $item->position;
36 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
37 if ($position == -1) {
38 $i_formselect_last = $lastposition + 1;
39 $i_formselect_value = $lastposition + 1;
40 $item->position = $lastposition + 1;
42 $i_formselect_last = $lastposition;
43 $i_formselect_value = $item->position;
45 //the elements for position dropdownlist
46 $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true);
48 $item->presentation = empty($item->presentation) ? 1 : $item->presentation;
51 //all items for dependitem
52 $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
53 $commonparams = array('cmid'=>$cm->id,
54 'id'=>isset($item->id) ? $item->id : null,
56 'items'=>$feedbackitems,
57 'feedback'=>$feedback->id);
60 $this->item_form = new feedback_info_form('edit_item.php',
62 'common'=>$commonparams,
63 'positionlist'=>$positionlist,
64 'position' => $position));
67 //this function only can used after the call of build_editform()
68 public function show_editform() {
69 $this->item_form->display();
72 public function is_cancelled() {
73 return $this->item_form->is_cancelled();
76 public function get_data() {
77 if ($this->item = $this->item_form->get_data()) {
83 public function save_item() {
86 if (!$item = $this->item_form->get_data()) {
90 if (isset($item->clone_item) AND $item->clone_item) {
91 $item->id = ''; //to clone this item
95 $item->hasvalue = $this->get_hasvalue();
97 $item->id = $DB->insert_record('feedback_item', $item);
99 $DB->update_record('feedback_item', $item);
102 return $DB->get_record('feedback_item', array('id'=>$item->id));
105 //liefert eine Struktur ->name, ->data = array(mit Antworten)
106 public function get_analysed($item, $groupid = false, $courseid = false) {
108 $presentation = $item->presentation;
109 $analysed_val = new stdClass();
110 $analysed_val->data = null;
111 $analysed_val->name = $item->name;
112 $values = feedback_get_group_values($item, $groupid, $courseid);
115 foreach ($values as $value) {
116 $datavalue = new stdClass();
118 switch($presentation) {
120 $datavalue->value = $value->value;
121 $datavalue->show = userdate($datavalue->value);
124 $datavalue->value = $value->value;
125 $datavalue->show = $datavalue->value;
128 $datavalue->value = $value->value;
129 $datavalue->show = $datavalue->value;
133 $data[] = $datavalue;
135 $analysed_val->data = $data;
137 return $analysed_val;
140 public function get_printval($item, $value) {
142 if (!isset($value->value)) {
145 return $item->presentation == 1 ? userdate($value->value) : $value->value;
148 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
149 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
150 $data = $analysed_item->data;
151 if (is_array($data)) {
152 echo '<tr><th colspan="2" align="left">';
154 if (strval($item->label) !== '') {
155 echo '('. format_string($item->label).') ';
157 echo format_text($item->name, FORMAT_HTML, array('noclean' => true, 'para' => false));
159 $sizeofdata = count($data);
160 for ($i = 0; $i < $sizeofdata; $i++) {
161 echo '<tr><td colspan="2" valign="top" align="left">- ';
162 echo str_replace("\n", '<br />', $data[$i]->show);
168 public function excelprint_item(&$worksheet, $row_offset,
170 $groupid, $courseid = false) {
171 $analysed_item = $this->get_analysed($item, $groupid, $courseid);
173 $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
174 $worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
175 $data = $analysed_item->data;
176 if (is_array($data)) {
177 $worksheet->write_string($row_offset, 2, $data[0]->show, $xls_formats->value_bold);
179 $sizeofdata = count($data);
180 for ($i = 1; $i < $sizeofdata; $i++) {
181 $worksheet->write_string($row_offset, 2, $data[$i]->show, $xls_formats->default);
190 * print the item at the edit-page of feedback
193 * @param object $item
196 public function print_item_preview($item) {
197 global $USER, $DB, $OUTPUT;
199 $align = right_to_left() ? 'right' : 'left';
200 $presentation = $item->presentation;
201 $requiredmark = ($item->required == 1)?'<img class="req" title="'.get_string('requiredelement', 'form').'" alt="'.
202 get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />':'';
204 if ($item->feedback) {
205 $courseid = $DB->get_field('feedback', 'course', array('id'=>$item->feedback));
206 } else { // the item must be a template item
207 $cmid = required_param('id', PARAM_INT);
208 $courseid = $DB->get_field('course_modules', 'course', array('id'=>$cmid));
210 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
211 print_error('error');
213 if ($course->id !== SITEID) {
214 $coursecategory = $DB->get_record('course_categories', array('id'=>$course->category));
216 $coursecategory = false;
218 switch($presentation) {
221 $itemshowvalue = userdate($itemvalue);
224 $coursecontext = context_course::instance($course->id);
225 $itemvalue = format_string($course->shortname,
227 array('context' => $coursecontext));
229 $itemshowvalue = $itemvalue;
232 if ($coursecategory) {
233 $category_context = context_coursecat::instance($coursecategory->id);
234 $itemvalue = format_string($coursecategory->name,
236 array('context' => $category_context));
238 $itemshowvalue = $itemvalue;
246 //print the question and label
247 echo '<div class="feedback_item_label_'.$align.'">';
248 if (strval($item->label) !== '') {
249 echo '('. format_string($item->label).') ';
251 echo format_text($item->name . $requiredmark, FORMAT_HTML, array('noclean' => true, 'para' => false));
252 if ($item->dependitem) {
253 if ($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
254 echo ' <span class="feedback_depend">';
255 echo '('.format_string($dependitem->label).'->'.$item->dependvalue.')';
260 //print the presentation
261 echo '<div class="feedback_item_presentation_'.$align.'">';
262 echo '<input type="hidden" name="'.$item->typ.'_'.$item->id.'" value="'.$itemvalue.'" />';
263 echo '<span class="feedback_item_info">'.$itemshowvalue.'</span>';
268 * print the item at the complete-page of feedback
271 * @param object $item
272 * @param string $value
273 * @param bool $highlightrequire
276 public function print_item_complete($item, $value = '', $highlightrequire = false) {
277 global $USER, $DB, $OUTPUT;
278 $align = right_to_left() ? 'right' : 'left';
280 $presentation = $item->presentation;
281 if ($highlightrequire AND $item->required AND strval($value) == '') {
282 $highlight = 'error';
286 $requiredmark = ($item->required == 1)?'<img class="req" title="'.get_string('requiredelement', 'form').'" alt="'.
287 get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />':'';
289 $feedback = $DB->get_record('feedback', array('id'=>$item->feedback));
291 if ($courseid = optional_param('courseid', 0, PARAM_INT)) {
292 $course = $DB->get_record('course', array('id'=>$courseid));
294 $course = $DB->get_record('course', array('id'=>$feedback->course));
297 if ($course->id !== SITEID) {
298 $coursecategory = $DB->get_record('course_categories', array('id'=>$course->category));
300 $coursecategory = false;
303 switch($presentation) {
305 if ($feedback->anonymous == FEEDBACK_ANONYMOUS_YES) {
307 $itemshowvalue = '-';
310 $itemshowvalue = userdate($itemvalue);
314 $coursecontext = context_course::instance($course->id);
315 $itemvalue = format_string($course->shortname,
317 array('context' => $coursecontext));
319 $itemshowvalue = $itemvalue;
322 if ($coursecategory) {
323 $category_context = context_coursecat::instance($coursecategory->id);
324 $itemvalue = format_string($coursecategory->name,
326 array('context' => $category_context));
328 $itemshowvalue = $itemvalue;
336 //print the question and label
337 echo '<div class="feedback_item_label_'.$align.'">';
338 echo '<span class="'.$highlight.'">';
339 echo format_text($item->name . $requiredmark, FORMAT_HTML, array('noclean' => true, 'para' => false));
343 //print the presentation
344 echo '<div class="feedback_item_presentation_'.$align.'">';
345 echo '<input type="hidden" name="'.$item->typ.'_'.$item->id.'" value="'.$itemvalue.'" />';
346 echo '<span class="feedback_item_info">'.$itemshowvalue.'</span>';
351 * print the item at the complete-page of feedback
354 * @param object $item
355 * @param string $value
358 public function print_item_show_value($item, $value = '') {
359 global $USER, $DB, $OUTPUT;
360 $align = right_to_left() ? 'right' : 'left';
362 $presentation = $item->presentation;
363 $requiredmark = ($item->required == 1)?'<img class="req" title="'.get_string('requiredelement', 'form').'" alt="'.
364 get_string('requiredelement', 'form').'" src="'.$OUTPUT->pix_url('req') .'" />':'';
366 if ($presentation == 1) {
367 $value = $value ? userdate($value) : ' ';
370 //print the question and label
371 echo '<div class="feedback_item_label_'.$align.'">';
372 if (strval($item->label) !== '') {
373 echo '('. format_string($item->label).') ';
375 echo format_text($item->name . $requiredmark, FORMAT_HTML, array('noclean' => true, 'para' => false));
378 //print the presentation
379 echo $OUTPUT->box_start('generalbox boxalign'.$align);
381 echo $OUTPUT->box_end();
384 public function check_value($value, $item) {
388 public function create_value($data) {
389 $data = clean_text($data);
393 //compares the dbvalue with the dependvalue
394 //the values can be the shortname of a course or the category name
395 //the date is not compareable :(.
396 public function compare_value($item, $dbvalue, $dependvalue) {
397 if ($dbvalue == $dependvalue) {
403 public function get_presentation($data) {
404 return $data->infotype;
407 public function get_hasvalue() {
411 public function can_switch_require() {
415 public function value_type() {
419 public function clean_input_value($value) {
420 return clean_param($value, $this->value_type());