7583b79da02c910cb14955b4a1bed74748b54152
[moodle.git] / mod / feedback / item / info / lib.php
1 <?php
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/>.
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;
23     private $item_form;
24     private $item;
26     public function init() {
28     }
30     public function build_editform($item, $feedback, $cm) {
31         global $DB, $CFG;
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;
41         } else {
42             $i_formselect_last = $lastposition;
43             $i_formselect_value = $item->position;
44         }
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;
49         $item->required = 0;
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,
55                              'typ'=>$item->typ,
56                              'items'=>$feedbackitems,
57                              'feedback'=>$feedback->id);
59         //build the form
60         $this->item_form = new feedback_info_form('edit_item.php',
61                                                   array('item'=>$item,
62                                                   'common'=>$commonparams,
63                                                   'positionlist'=>$positionlist,
64                                                   'position' => $position));
65     }
67     //this function only can used after the call of build_editform()
68     public function show_editform() {
69         $this->item_form->display();
70     }
72     public function is_cancelled() {
73         return $this->item_form->is_cancelled();
74     }
76     public function get_data() {
77         if ($this->item = $this->item_form->get_data()) {
78             return true;
79         }
80         return false;
81     }
83     public function save_item() {
84         global $DB;
86         if (!$item = $this->item_form->get_data()) {
87             return false;
88         }
90         if (isset($item->clone_item) AND $item->clone_item) {
91             $item->id = ''; //to clone this item
92             $item->position++;
93         }
95         $item->hasvalue = $this->get_hasvalue();
96         if (!$item->id) {
97             $item->id = $DB->insert_record('feedback_item', $item);
98         } else {
99             $DB->update_record('feedback_item', $item);
100         }
102         return $DB->get_record('feedback_item', array('id'=>$item->id));
103     }
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);
113         if ($values) {
114             $data = array();
115             foreach ($values as $value) {
116                 $datavalue = new stdClass();
118                 switch($presentation) {
119                     case 1:
120                         $datavalue->value = $value->value;
121                         $datavalue->show = userdate($datavalue->value);
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                 }
133                 $data[] = $datavalue;
134             }
135             $analysed_val->data = $data;
136         }
137         return $analysed_val;
138     }
140     public function get_printval($item, $value) {
142         if (!isset($value->value)) {
143             return '';
144         }
145         return $item->presentation == 1 ? userdate($value->value) : $value->value;
146     }
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">';
153             echo $itemnr . ' ';
154             if (strval($item->label) !== '') {
155                 echo '('. format_string($item->label).') ';
156             }
157             echo format_text($item->name, FORMAT_HTML, array('noclean' => true, 'para' => false));
158             echo '</th></tr>';
159             $sizeofdata = count($data);
160             for ($i = 0; $i < $sizeofdata; $i++) {
161                 echo '<tr><td colspan="2" valign="top" align="left">-&nbsp;&nbsp;';
162                 echo str_replace("\n", '<br />', $data[$i]->show);
163                 echo '</td></tr>';
164             }
165         }
166     }
168     public function excelprint_item(&$worksheet, $row_offset,
169                              $xls_formats, $item,
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);
178             $row_offset++;
179             $sizeofdata = count($data);
180             for ($i = 1; $i < $sizeofdata; $i++) {
181                 $worksheet->write_string($row_offset, 2, $data[$i]->show, $xls_formats->default);
182                 $row_offset++;
183             }
184         }
185         $row_offset++;
186         return $row_offset;
187     }
189     /**
190      * print the item at the edit-page of feedback
191      *
192      * @global object
193      * @param object $item
194      * @return void
195      */
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));
209         }
210         if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
211             print_error('error');
212         }
213         if ($course->id !== SITEID) {
214             $coursecategory = $DB->get_record('course_categories', array('id'=>$course->category));
215         } else {
216             $coursecategory = false;
217         }
218         switch($presentation) {
219             case 1:
220                 $itemvalue = time();
221                 $itemshowvalue = userdate($itemvalue);
222                 break;
223             case 2:
224                 $coursecontext = context_course::instance($course->id);
225                 $itemvalue = format_string($course->shortname,
226                                            true,
227                                            array('context' => $coursecontext));
229                 $itemshowvalue = $itemvalue;
230                 break;
231             case 3:
232                 if ($coursecategory) {
233                     $category_context = context_coursecat::instance($coursecategory->id);
234                     $itemvalue = format_string($coursecategory->name,
235                                                true,
236                                                array('context' => $category_context));
238                     $itemshowvalue = $itemvalue;
239                 } else {
240                     $itemvalue = '';
241                     $itemshowvalue = '';
242                 }
243                 break;
244         }
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).') ';
250         }
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).'-&gt;'.$item->dependvalue.')';
256                 echo '</span>';
257             }
258         }
259         echo '</div>';
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>';
264         echo '</div>';
265     }
267     /**
268      * print the item at the complete-page of feedback
269      *
270      * @global object
271      * @param object $item
272      * @param string $value
273      * @param bool $highlightrequire
274      * @return void
275      */
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';
283         } else {
284             $highlight = '';
285         }
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));
293         } else {
294             $course = $DB->get_record('course', array('id'=>$feedback->course));
295         }
297         if ($course->id !== SITEID) {
298             $coursecategory = $DB->get_record('course_categories', array('id'=>$course->category));
299         } else {
300             $coursecategory = false;
301         }
303         switch($presentation) {
304             case 1:
305                 if ($feedback->anonymous == FEEDBACK_ANONYMOUS_YES) {
306                     $itemvalue = 0;
307                     $itemshowvalue = '-';
308                 } else {
309                     $itemvalue = time();
310                     $itemshowvalue = userdate($itemvalue);
311                 }
312                 break;
313             case 2:
314                 $coursecontext = context_course::instance($course->id);
315                 $itemvalue = format_string($course->shortname,
316                                            true,
317                                            array('context' => $coursecontext));
319                 $itemshowvalue = $itemvalue;
320                 break;
321             case 3:
322                 if ($coursecategory) {
323                     $category_context = context_coursecat::instance($coursecategory->id);
324                     $itemvalue = format_string($coursecategory->name,
325                                                true,
326                                                array('context' => $category_context));
328                     $itemshowvalue = $itemvalue;
329                 } else {
330                     $itemvalue = '';
331                     $itemshowvalue = '';
332                 }
333                 break;
334         }
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));
340         echo '</span>';
341         echo '</div>';
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>';
347         echo '</div>';
348     }
350     /**
351      * print the item at the complete-page of feedback
352      *
353      * @global object
354      * @param object $item
355      * @param string $value
356      * @return void
357      */
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) : '&nbsp;';
368         }
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).') ';
374         }
375         echo format_text($item->name . $requiredmark, FORMAT_HTML, array('noclean' => true, 'para' => false));
376         echo '</div>';
378         //print the presentation
379         echo $OUTPUT->box_start('generalbox boxalign'.$align);
380         echo $value;
381         echo $OUTPUT->box_end();
382     }
384     public function check_value($value, $item) {
385         return true;
386     }
388     public function create_value($data) {
389         $data = clean_text($data);
390         return $data;
391     }
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) {
398             return true;
399         }
400         return false;
401     }
403     public function get_presentation($data) {
404         return $data->infotype;
405     }
407     public function get_hasvalue() {
408         return 1;
409     }
411     public function can_switch_require() {
412         return false;
413     }
415     public function value_type() {
416         return PARAM_TEXT;
417     }
419     public function clean_input_value($value) {
420         return clean_param($value, $this->value_type());
421     }