MDL-19488 - allow the creation of a "Public" feedback
[moodle.git] / mod / feedback / item / label / lib.php
CommitLineData
1adbd2c3 1<?php
01910dff 2defined('MOODLE_INTERNAL') OR die('not allowed');
c70ad9f7 3require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
cb416e50 4require_once($CFG->libdir.'/formslib.php');
c70ad9f7 5
6class feedback_item_label extends feedback_item_base {
7 var $type = "label";
a59ff6b0
AG
8 var $presentationoptions = null;
9 var $commonparams;
10 var $item_form;
11 var $context;
12 var $item;
64f93798 13
c70ad9f7 14 function init() {
6ee09cfe 15 global $CFG;
fc919723
AG
16 $this->presentationoptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'trusttext'=>true);
17
a59ff6b0 18 }
1adbd2c3 19
a59ff6b0
AG
20 function build_editform($item, $feedback, $cm) {
21 global $DB, $CFG;
6ee09cfe 22 require_once('label_form.php');
1adbd2c3 23
a59ff6b0
AG
24 //get the lastposition number of the feedback_items
25 $position = $item->position;
26 $lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
27 if($position == -1){
28 $i_formselect_last = $lastposition + 1;
29 $i_formselect_value = $lastposition + 1;
30 $item->position = $lastposition + 1;
31 }else {
32 $i_formselect_last = $lastposition;
33 $i_formselect_value = $item->position;
34 }
35 //the elements for position dropdownlist
36 $positionlist = array_slice(range(0,$i_formselect_last),1,$i_formselect_last,true);
37
73043833
AG
38 //all items for dependitem
39 $feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
a59ff6b0
AG
40 $commonparams = array('cmid'=>$cm->id,
41 'id'=>isset($item->id) ? $item->id : NULL,
42 'typ'=>$item->typ,
73043833 43 'items'=>$feedbackitems,
a59ff6b0
AG
44 'feedback'=>$feedback->id);
45
46 $this->context = get_context_instance(CONTEXT_MODULE, $cm->id);
64f93798
PS
47
48
a59ff6b0
AG
49 //preparing the editor for new file-api
50 $item->presentationformat = FORMAT_HTML;
51 $item->presentationtrust = 1;
e9de1cf4
RT
52
53 // Append editor context to presentation options, giving preference to existing context.
54 $this->presentationoptions = array_merge(array('context' => $this->context), $this->presentationoptions);
55
a59ff6b0
AG
56 $item = file_prepare_standard_editor($item,
57 'presentation', //name of the form element
58 $this->presentationoptions,
59 $this->context,
64f93798
PS
60 'mod_feedback',
61 'item', //the filearea
a59ff6b0 62 $item->id);
64f93798 63
a59ff6b0
AG
64 //build the form
65 $this->item_form = new feedback_label_form('edit_item.php', array('item'=>$item, 'common'=>$commonparams, 'positionlist'=>$positionlist, 'position'=>$position, 'presentationoptions'=>$this->presentationoptions));
66 }
64f93798 67
a59ff6b0
AG
68 //this function only can used after the call of build_editform()
69 function show_editform() {
70 $this->item_form->display();
71 }
64f93798 72
a59ff6b0
AG
73 function is_cancelled() {
74 return $this->item_form->is_cancelled();
c70ad9f7 75 }
882f08c8 76
a59ff6b0
AG
77 function get_data() {
78 if($this->item = $this->item_form->get_data()) {
79 return true;
80 }
81 return false;
82 }
83
84 function save_item() {
85 global $DB;
64f93798 86
a59ff6b0
AG
87 if(!$item = $this->item_form->get_data()) {
88 return false;
89 }
64f93798 90
b8baf0ad 91 if(isset($item->clone_item) AND $item->clone_item) {
9e1aed53
AG
92 $item->id = ''; //to clone this item
93 $item->position++;
94 }
95
a59ff6b0 96 $item->presentation = '';
64f93798 97
45c2f92a 98 $item->hasvalue = $this->get_hasvalue();
a59ff6b0
AG
99 if(!$item->id) {
100 $item->id = $DB->insert_record('feedback_item', $item);
101 }else {
102 $DB->update_record('feedback_item', $item);
103 }
64f93798 104
a59ff6b0
AG
105 $item = file_postupdate_standard_editor($item,
106 'presentation',
107 $this->presentationoptions,
108 $this->context,
64f93798
PS
109 'mod_feedback',
110 'item',
a59ff6b0 111 $item->id);
64f93798 112
a59ff6b0 113 $DB->update_record('feedback_item', $item);
64f93798 114
a59ff6b0
AG
115 return $DB->get_record('feedback_item', array('id'=>$item->id));
116 }
64f93798 117
882f08c8 118 function print_item($item){
99d19c13
PS
119 global $DB, $CFG;
120
121 require_once($CFG->libdir . '/filelib.php');
14832313
AG
122
123 //is the item a template?
124 if(!$item->feedback AND $item->template) {
125 $template = $DB->get_record('feedback_template', array('id'=>$item->template));
7b1126fd
AG
126 if($template->ispublic) {
127 $context = get_system_context();
128 }else {
129 $context = get_context_instance(CONTEXT_COURSE, $template->course);
130 }
64f93798 131 $filearea = 'template';
14832313
AG
132 }else {
133 $cm = get_coursemodule_from_instance('feedback', $item->feedback);
134 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
64f93798 135 $filearea = 'item';
14832313 136 }
64f93798 137
a59ff6b0
AG
138 $item->presentationformat = FORMAT_HTML;
139 $item->presentationtrust = 1;
64f93798
PS
140
141 $output = file_rewrite_pluginfile_urls($item->presentation, 'pluginfile.php', $context->id, 'mod_feedback', $filearea, $item->id);
367a75fa 142 echo format_text($output, FORMAT_HTML, array('overflowdiv'=>true));
882f08c8
AG
143 }
144
64f93798 145 /**
9d5fbd65
AG
146 * print the item at the edit-page of feedback
147 *
148 * @global object
149 * @param object $item
150 * @return void
151 */
152 function print_item_preview($item) {
73043833 153 global $OUTPUT, $DB;
64f93798 154
73043833
AG
155 if($item->dependitem) {
156 if($dependitem = $DB->get_record('feedback_item', array('id'=>$item->dependitem))) {
157 echo ' <span class="feedback_depend">('.$dependitem->label.'-&gt;'.$item->dependvalue.')</span>';
158 }
159 }
9d5fbd65
AG
160 $this->print_item($item);
161 }
64f93798
PS
162
163 /**
9d5fbd65
AG
164 * print the item at the complete-page of feedback
165 *
166 * @global object
167 * @param object $item
168 * @param string $value
169 * @param bool $highlightrequire
170 * @return void
171 */
172 function print_item_complete($item, $value = '', $highlightrequire = false) {
173 $this->print_item($item);
174 }
175
64f93798 176 /**
9d5fbd65
AG
177 * print the item at the complete-page of feedback
178 *
179 * @global object
180 * @param object $item
181 * @param string $value
182 * @return void
183 */
184 function print_item_show_value($item, $value = '') {
185 $this->print_item($item);
c70ad9f7 186 }
187
188 function create_value($data) {
189 return false;
190 }
191
73043833
AG
192 function compare_value($item, $dbvalue, $dependvalue) {
193 return false;
194 }
64f93798 195
c70ad9f7 196 //used by create_item and update_item functions,
197 //when provided $data submitted from feedback_show_edit
198 function get_presentation($data) {
a59ff6b0 199 // $context = get_context_instance(CONTEXT_MODULE, $data->cmid);
64f93798 200
39790bd8 201 // $presentation = new stdClass();
a59ff6b0
AG
202 // $presentation->id = null;
203 // $presentation->definition = '';
204 // $presentation->format = FORMAT_HTML;
64f93798 205
a59ff6b0 206 // $draftid_editor = file_get_submitted_draft_itemid('presentation');
64f93798 207 // $currenttext = file_prepare_draft_area($draftid_editor, $context->id, 'mod_feedback', 'item_label', $presentation->id, array('subdirs'=>true), $presentation->definition);
a59ff6b0
AG
208 // $presentation->entry = array('text'=>$currenttext, 'format'=>$presentation->format, 'itemid'=>$draftid_editor);
209
210 // return $data->presentation;
c70ad9f7 211 }
212
a59ff6b0
AG
213 function postupdate($item) {
214 global $DB;
64f93798 215
a59ff6b0 216 $context = get_context_instance(CONTEXT_MODULE, $item->cmid);
64f93798 217 $item = file_postupdate_standard_editor($item, 'presentation', $this->presentationoptions, $context, 'mod_feedback', 'item', $item->id);
a59ff6b0 218
39790bd8 219 // $item = new stdClass();
a59ff6b0 220 // $item->id = $data->id
dd88de0e
PS
221 $DB->update_record('feedback_item', $item);
222 return $item->id;
a59ff6b0 223 }
64f93798 224
c70ad9f7 225 function get_hasvalue() {
226 return 0;
227 }
64f93798 228
6cc1599e
AG
229 function can_switch_require() {
230 return false;
231 }
232
233 function check_value($value, $item) {}
234 function excelprint_item(&$worksheet, $rowOffset, $xlsFormats, $item, $groupid, $courseid = false) {}
235 function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {}
236 function get_printval($item, $value) {}
237 function get_analysed($item, $groupid = false, $courseid = false) {}
c70ad9f7 238}