MDL-53638 mod_feedback: change items classes
[moodle.git] / mod / feedback / item / label / 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');
19 require_once($CFG->libdir.'/formslib.php');
21 class feedback_item_label extends feedback_item_base {
22     protected $type = "label";
23     private $presentationoptions = null;
24     private $context;
26     /**
27      * Constructor
28      */
29     public function __construct() {
30         $this->presentationoptions = array('maxfiles' => EDITOR_UNLIMITED_FILES,
31                                            'trusttext'=>true);
33     }
35     public function build_editform($item, $feedback, $cm) {
36         global $DB, $CFG;
37         require_once('label_form.php');
39         //get the lastposition number of the feedback_items
40         $position = $item->position;
41         $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
42         if ($position == -1) {
43             $i_formselect_last = $lastposition + 1;
44             $i_formselect_value = $lastposition + 1;
45             $item->position = $lastposition + 1;
46         } else {
47             $i_formselect_last = $lastposition;
48             $i_formselect_value = $item->position;
49         }
50         //the elements for position dropdownlist
51         $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true);
53         //all items for dependitem
54         $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
55         $commonparams = array('cmid'=>$cm->id,
56                              'id'=>isset($item->id) ? $item->id : null,
57                              'typ'=>$item->typ,
58                              'items'=>$feedbackitems,
59                              'feedback'=>$feedback->id);
61         $this->context = context_module::instance($cm->id);
63         //preparing the editor for new file-api
64         $item->presentationformat = FORMAT_HTML;
65         $item->presentationtrust = 1;
67         // Append editor context to presentation options, giving preference to existing context.
68         $this->presentationoptions = array_merge(array('context' => $this->context),
69                                                  $this->presentationoptions);
71         $item = file_prepare_standard_editor($item,
72                                             'presentation', //name of the form element
73                                             $this->presentationoptions,
74                                             $this->context,
75                                             'mod_feedback',
76                                             'item', //the filearea
77                                             $item->id);
79         //build the form
80         $customdata = array('item' => $item,
81                             'common' => $commonparams,
82                             'positionlist' => $positionlist,
83                             'position' => $position,
84                             'presentationoptions' => $this->presentationoptions);
86         $this->item_form = new feedback_label_form('edit_item.php', $customdata);
87     }
89     public function save_item() {
90         global $DB;
92         if (!$item = $this->item_form->get_data()) {
93             return false;
94         }
96         if (isset($item->clone_item) AND $item->clone_item) {
97             $item->id = ''; //to clone this item
98             $item->position++;
99         }
101         $item->presentation = '';
103         $item->hasvalue = $this->get_hasvalue();
104         if (!$item->id) {
105             $item->id = $DB->insert_record('feedback_item', $item);
106         } else {
107             $DB->update_record('feedback_item', $item);
108         }
110         $item = file_postupdate_standard_editor($item,
111                                                 'presentation',
112                                                 $this->presentationoptions,
113                                                 $this->context,
114                                                 'mod_feedback',
115                                                 'item',
116                                                 $item->id);
118         $DB->update_record('feedback_item', $item);
120         return $DB->get_record('feedback_item', array('id'=>$item->id));
121     }
123     /**
124      * prepares the item for output or export to file
125      * @param stdClass $item
126      * @return string
127      */
128     private function print_item($item) {
129         global $DB, $CFG;
131         require_once($CFG->libdir . '/filelib.php');
133         //is the item a template?
134         if (!$item->feedback AND $item->template) {
135             $template = $DB->get_record('feedback_template', array('id'=>$item->template));
136             if ($template->ispublic) {
137                 $context = context_system::instance();
138             } else {
139                 $context = context_course::instance($template->course);
140             }
141             $filearea = 'template';
142         } else {
143             $cm = get_coursemodule_from_instance('feedback', $item->feedback);
144             $context = context_module::instance($cm->id);
145             $filearea = 'item';
146         }
148         $item->presentationformat = FORMAT_HTML;
149         $item->presentationtrust = 1;
151         $output = file_rewrite_pluginfile_urls($item->presentation,
152                                                'pluginfile.php',
153                                                $context->id,
154                                                'mod_feedback',
155                                                $filearea,
156                                                $item->id);
158         $formatoptions = array('overflowdiv'=>true, 'trusted'=>$CFG->enabletrusttext);
159         echo format_text($output, FORMAT_HTML, $formatoptions);
160     }
162     /**
163      * @param stdClass $item
164      * @param bool|true $withpostfix
165      * @return string
166      */
167     public function get_display_name($item, $withpostfix = true) {
168         return '';
169     }
171     /**
172      * Adds an input element to the complete form
173      *
174      * @param stdClass $item
175      * @param mod_feedback_complete_form $form
176      */
177     public function complete_form_element($item, $form) {
178         global $DB;
179         if (!$item->feedback AND $item->template) {
180             // This is a template.
181             $template = $DB->get_record('feedback_template', array('id' => $item->template));
182             if ($template->ispublic) {
183                 $context = context_system::instance();
184             } else {
185                 $context = context_course::instance($template->course);
186             }
187             $filearea = 'template';
188         } else {
189             // This is a question in the current feedback.
190             $context = $form->get_cm()->context;
191             $filearea = 'item';
192         }
193         $output = file_rewrite_pluginfile_urls($item->presentation, 'pluginfile.php',
194                 $context->id, 'mod_feedback', $filearea, $item->id);
195         $formatoptions = array('overflowdiv' => true, 'noclean' => true);
196         $output = format_text($output, FORMAT_HTML, $formatoptions);
198         $inputname = $item->typ . '_' . $item->id;
200         $name = $this->get_display_name($item);
201         $form->add_form_element($item, ['static', $inputname, $name, $output], false, false);
202     }
204     public function compare_value($item, $dbvalue, $dependvalue) {
205         return false;
206     }
208     public function postupdate($item) {
209         global $DB;
211         $context = context_module::instance($item->cmid);
212         $item = file_postupdate_standard_editor($item,
213                                                 'presentation',
214                                                 $this->presentationoptions,
215                                                 $context,
216                                                 'mod_feedback',
217                                                 'item',
218                                                 $item->id);
220         $DB->update_record('feedback_item', $item);
221         return $item->id;
222     }
224     public function get_hasvalue() {
225         return 0;
226     }
228     public function can_switch_require() {
229         return false;
230     }
232     public function excelprint_item(&$worksheet,
233                              $row_offset,
234                              $xls_formats,
235                              $item,
236                              $groupid,
237                              $courseid = false) {
238     }
240     public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
241     }
242     public function get_printval($item, $value) {
243     }