Merge branch 'MDL-27675_master' of git://github.com/grabs/moodle
[moodle.git] / mod / feedback / item / label / 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');
c70ad9f7 18require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
cb416e50 19require_once($CFG->libdir.'/formslib.php');
c70ad9f7 20
21class feedback_item_label extends feedback_item_base {
9c5bc7a5
AG
22 protected $type = "label";
23 private $presentationoptions = null;
24 private $commonparams;
25 private $item_form;
26 private $context;
27 private $item;
28
29 public function init() {
6ee09cfe 30 global $CFG;
9c5bc7a5
AG
31 $this->presentationoptions = array('maxfiles' => EDITOR_UNLIMITED_FILES,
32 'trusttext'=>true);
fc919723 33
a59ff6b0 34 }
1adbd2c3 35
9c5bc7a5 36 public function build_editform($item, $feedback, $cm) {
a59ff6b0 37 global $DB, $CFG;
6ee09cfe 38 require_once('label_form.php');
1adbd2c3 39
a59ff6b0
AG
40 //get the lastposition number of the feedback_items
41 $position = $item->position;
42 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
9c5bc7a5 43 if ($position == -1) {
a59ff6b0
AG
44 $i_formselect_last = $lastposition + 1;
45 $i_formselect_value = $lastposition + 1;
46 $item->position = $lastposition + 1;
9c5bc7a5 47 } else {
a59ff6b0
AG
48 $i_formselect_last = $lastposition;
49 $i_formselect_value = $item->position;
50 }
51 //the elements for position dropdownlist
9c5bc7a5 52 $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true);
a59ff6b0 53
73043833
AG
54 //all items for dependitem
55 $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
a59ff6b0 56 $commonparams = array('cmid'=>$cm->id,
9c5bc7a5 57 'id'=>isset($item->id) ? $item->id : null,
a59ff6b0 58 'typ'=>$item->typ,
73043833 59 'items'=>$feedbackitems,
a59ff6b0
AG
60 'feedback'=>$feedback->id);
61
62 $this->context = get_context_instance(CONTEXT_MODULE, $cm->id);
64f93798 63
a59ff6b0
AG
64 //preparing the editor for new file-api
65 $item->presentationformat = FORMAT_HTML;
66 $item->presentationtrust = 1;
e9de1cf4
RT
67
68 // Append editor context to presentation options, giving preference to existing context.
9c5bc7a5
AG
69 $this->presentationoptions = array_merge(array('context' => $this->context),
70 $this->presentationoptions);
e9de1cf4 71
a59ff6b0
AG
72 $item = file_prepare_standard_editor($item,
73 'presentation', //name of the form element
74 $this->presentationoptions,
75 $this->context,
64f93798
PS
76 'mod_feedback',
77 'item', //the filearea
a59ff6b0 78 $item->id);
64f93798 79
a59ff6b0 80 //build the form
9c5bc7a5
AG
81 $customdata = array('item' => $item,
82 'common' => $commonparams,
83 'positionlist' => $positionlist,
84 'position' => $position,
85 'presentationoptions' => $this->presentationoptions);
86
87 $this->item_form = new feedback_label_form('edit_item.php', $customdata);
a59ff6b0 88 }
64f93798 89
a59ff6b0 90 //this function only can used after the call of build_editform()
9c5bc7a5 91 public function show_editform() {
a59ff6b0
AG
92 $this->item_form->display();
93 }
64f93798 94
9c5bc7a5 95 public function is_cancelled() {
a59ff6b0 96 return $this->item_form->is_cancelled();
c70ad9f7 97 }
882f08c8 98
9c5bc7a5
AG
99 public function get_data() {
100 if ($this->item = $this->item_form->get_data()) {
a59ff6b0
AG
101 return true;
102 }
103 return false;
104 }
105
9c5bc7a5 106 public function save_item() {
a59ff6b0 107 global $DB;
64f93798 108
9c5bc7a5 109 if (!$item = $this->item_form->get_data()) {
a59ff6b0
AG
110 return false;
111 }
64f93798 112
9c5bc7a5 113 if (isset($item->clone_item) AND $item->clone_item) {
9e1aed53
AG
114 $item->id = ''; //to clone this item
115 $item->position++;
116 }
117
a59ff6b0 118 $item->presentation = '';
64f93798 119
45c2f92a 120 $item->hasvalue = $this->get_hasvalue();
9c5bc7a5 121 if (!$item->id) {
a59ff6b0 122 $item->id = $DB->insert_record('feedback_item', $item);
9c5bc7a5 123 } else {
a59ff6b0
AG
124 $DB->update_record('feedback_item', $item);
125 }
64f93798 126
a59ff6b0
AG
127 $item = file_postupdate_standard_editor($item,
128 'presentation',
129 $this->presentationoptions,
130 $this->context,
64f93798
PS
131 'mod_feedback',
132 'item',
a59ff6b0 133 $item->id);
64f93798 134
a59ff6b0 135 $DB->update_record('feedback_item', $item);
64f93798 136
a59ff6b0
AG
137 return $DB->get_record('feedback_item', array('id'=>$item->id));
138 }
64f93798 139
9c5bc7a5 140 public function print_item($item) {
99d19c13
PS
141 global $DB, $CFG;
142
143 require_once($CFG->libdir . '/filelib.php');
14832313
AG
144
145 //is the item a template?
9c5bc7a5 146 if (!$item->feedback AND $item->template) {
14832313 147 $template = $DB->get_record('feedback_template', array('id'=>$item->template));
9c5bc7a5 148 if ($template->ispublic) {
7b1126fd 149 $context = get_system_context();
9c5bc7a5 150 } else {
7b1126fd
AG
151 $context = get_context_instance(CONTEXT_COURSE, $template->course);
152 }
64f93798 153 $filearea = 'template';
9c5bc7a5 154 } else {
14832313
AG
155 $cm = get_coursemodule_from_instance('feedback', $item->feedback);
156 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
64f93798 157 $filearea = 'item';
14832313 158 }
64f93798 159
a59ff6b0
AG
160 $item->presentationformat = FORMAT_HTML;
161 $item->presentationtrust = 1;
64f93798 162
9c5bc7a5
AG
163 $output = file_rewrite_pluginfile_urls($item->presentation,
164 'pluginfile.php',
165 $context->id,
166 'mod_feedback',
167 $filearea,
168 $item->id);
169
bf4fb3b3
AG
170 $formatoptions = array('overflowdiv'=>true, 'trusted'=>$CFG->enabletrusttext);
171 echo format_text($output, FORMAT_HTML, $formatoptions);
882f08c8
AG
172 }
173
64f93798 174 /**
9d5fbd65
AG
175 * print the item at the edit-page of feedback
176 *
177 * @global object
178 * @param object $item
179 * @return void
180 */
9c5bc7a5 181 public function print_item_preview($item) {
73043833 182 global $OUTPUT, $DB;
64f93798 183
9c5bc7a5
AG
184 if ($item->dependitem) {
185 if ($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
186 echo ' <span class="feedback_depend">';
187 echo '('.$dependitem->label.'-&gt;'.$item->dependvalue.')';
188 echo '</span>';
73043833
AG
189 }
190 }
9d5fbd65
AG
191 $this->print_item($item);
192 }
64f93798
PS
193
194 /**
9d5fbd65
AG
195 * print the item at the complete-page of feedback
196 *
197 * @global object
198 * @param object $item
199 * @param string $value
200 * @param bool $highlightrequire
201 * @return void
202 */
9c5bc7a5 203 public function print_item_complete($item, $value = '', $highlightrequire = false) {
9d5fbd65
AG
204 $this->print_item($item);
205 }
206
64f93798 207 /**
9d5fbd65
AG
208 * print the item at the complete-page of feedback
209 *
210 * @global object
211 * @param object $item
212 * @param string $value
213 * @return void
214 */
9c5bc7a5 215 public function print_item_show_value($item, $value = '') {
9d5fbd65 216 $this->print_item($item);
c70ad9f7 217 }
218
9c5bc7a5 219 public function create_value($data) {
c70ad9f7 220 return false;
221 }
222
9c5bc7a5 223 public function compare_value($item, $dbvalue, $dependvalue) {
73043833
AG
224 return false;
225 }
64f93798 226
c70ad9f7 227 //used by create_item and update_item functions,
228 //when provided $data submitted from feedback_show_edit
9c5bc7a5 229 public function get_presentation($data) {
c70ad9f7 230 }
231
9c5bc7a5 232 public function postupdate($item) {
a59ff6b0 233 global $DB;
64f93798 234
a59ff6b0 235 $context = get_context_instance(CONTEXT_MODULE, $item->cmid);
9c5bc7a5
AG
236 $item = file_postupdate_standard_editor($item,
237 'presentation',
238 $this->presentationoptions,
239 $context,
240 'mod_feedback',
241 'item',
242 $item->id);
a59ff6b0 243
dd88de0e
PS
244 $DB->update_record('feedback_item', $item);
245 return $item->id;
a59ff6b0 246 }
64f93798 247
9c5bc7a5 248 public function get_hasvalue() {
c70ad9f7 249 return 0;
250 }
64f93798 251
9c5bc7a5 252 public function can_switch_require() {
6cc1599e
AG
253 return false;
254 }
255
9c5bc7a5
AG
256 public function check_value($value, $item) {
257 }
258
259 public function excelprint_item(&$worksheet,
260 $row_offset,
261 $xls_formats,
262 $item,
263 $groupid,
264 $courseid = false) {
265 }
266
267 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
268 }
269 public function get_printval($item, $value) {
270 }
271 public function get_analysed($item, $groupid = false, $courseid = false) {
272 }
e7fc2ff4
AG
273 public function value_type() {
274 return PARAM_BOOL;
275 }
276 public function clean_input_value($value) {
277 return '';
278 }
c70ad9f7 279}