Merge branch 'MDL-51445_m30v1' of https://github.com/sbourget/moodle into MOODLE_30_S...
[moodle.git] / mod / feedback / edit.php
blobb06274435788aeebdd456eb0f3301fdecc9466e3
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.
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 /**
18 * prints the form to edit the feedback items such moving, deleting and so on
20 * @author Andreas Grabs
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22 * @package mod_feedback
25 require_once('../../config.php');
26 require_once('lib.php');
27 require_once('edit_form.php');
29 feedback_init_feedback_session();
31 $id = required_param('id', PARAM_INT);
33 if (($formdata = data_submitted()) AND !confirm_sesskey()) {
34 print_error('invalidsesskey');
37 $do_show = optional_param('do_show', 'edit', PARAM_ALPHA);
38 $moveupitem = optional_param('moveupitem', false, PARAM_INT);
39 $movedownitem = optional_param('movedownitem', false, PARAM_INT);
40 $moveitem = optional_param('moveitem', false, PARAM_INT);
41 $movehere = optional_param('movehere', false, PARAM_INT);
42 $switchitemrequired = optional_param('switchitemrequired', false, PARAM_INT);
44 $current_tab = $do_show;
46 $url = new moodle_url('/mod/feedback/edit.php', array('id'=>$id, 'do_show'=>$do_show));
48 if (! $cm = get_coursemodule_from_id('feedback', $id)) {
49 print_error('invalidcoursemodule');
52 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
53 print_error('coursemisconf');
56 if (! $feedback = $DB->get_record('feedback', array('id'=>$cm->instance))) {
57 print_error('invalidcoursemodule');
60 $context = context_module::instance($cm->id);
62 require_login($course, true, $cm);
64 require_capability('mod/feedback:edititems', $context);
66 //Move up/down items
67 if ($moveupitem) {
68 $item = $DB->get_record('feedback_item', array('id'=>$moveupitem));
69 feedback_moveup_item($item);
71 if ($movedownitem) {
72 $item = $DB->get_record('feedback_item', array('id'=>$movedownitem));
73 feedback_movedown_item($item);
76 //Moving of items
77 if ($movehere && isset($SESSION->feedback->moving->movingitem)) {
78 $item = $DB->get_record('feedback_item', array('id'=>$SESSION->feedback->moving->movingitem));
79 feedback_move_item($item, intval($movehere));
80 $moveitem = false;
82 if ($moveitem) {
83 $item = $DB->get_record('feedback_item', array('id'=>$moveitem));
84 $SESSION->feedback->moving->shouldmoving = 1;
85 $SESSION->feedback->moving->movingitem = $moveitem;
86 } else {
87 unset($SESSION->feedback->moving);
90 if ($switchitemrequired) {
91 $item = $DB->get_record('feedback_item', array('id'=>$switchitemrequired));
92 @feedback_switch_item_required($item);
93 redirect($url->out(false));
94 exit;
97 //The create_template-form
98 $create_template_form = new feedback_edit_create_template_form();
99 $create_template_form->set_feedbackdata(array('context'=>$context, 'course'=>$course));
100 $create_template_form->set_form_elements();
101 $create_template_form->set_data(array('id'=>$id, 'do_show'=>'templates'));
102 $create_template_formdata = $create_template_form->get_data();
103 if (isset($create_template_formdata->savetemplate) && $create_template_formdata->savetemplate == 1) {
104 //Check the capabilities to create templates.
105 if (!has_capability('mod/feedback:createprivatetemplate', $context) AND
106 !has_capability('mod/feedback:createpublictemplate', $context)) {
107 print_error('cannotsavetempl', 'feedback');
109 if (trim($create_template_formdata->templatename) == '') {
110 $savereturn = 'notsaved_name';
111 } else {
112 //If the feedback is located on the frontpage then templates can be public.
113 if (has_capability('mod/feedback:createpublictemplate', context_system::instance())) {
114 $create_template_formdata->ispublic = isset($create_template_formdata->ispublic) ? 1 : 0;
115 } else {
116 $create_template_formdata->ispublic = 0;
118 if (!feedback_save_as_template($feedback,
119 $create_template_formdata->templatename,
120 $create_template_formdata->ispublic)) {
121 $savereturn = 'failed';
122 } else {
123 $savereturn = 'saved';
128 //Get the feedbackitems
129 $lastposition = 0;
130 $feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedback->id), 'position');
131 if (is_array($feedbackitems)) {
132 $feedbackitems = array_values($feedbackitems);
133 if (count($feedbackitems) > 0) {
134 $lastitem = $feedbackitems[count($feedbackitems)-1];
135 $lastposition = $lastitem->position;
136 } else {
137 $lastposition = 0;
140 $lastposition++;
143 //The add_item-form
144 $add_item_form = new feedback_edit_add_question_form('edit_item.php');
145 $add_item_form->set_data(array('cmid'=>$id, 'position'=>$lastposition));
147 //The use_template-form
148 $use_template_form = new feedback_edit_use_template_form('use_templ.php');
149 $use_template_form->set_feedbackdata(array('course' => $course));
150 $use_template_form->set_form_elements();
151 $use_template_form->set_data(array('id'=>$id));
153 //Print the page header.
154 $strfeedbacks = get_string('modulenameplural', 'feedback');
155 $strfeedback = get_string('modulename', 'feedback');
157 $PAGE->set_url('/mod/feedback/edit.php', array('id'=>$cm->id, 'do_show'=>$do_show));
158 $PAGE->set_heading($course->fullname);
159 $PAGE->set_title($feedback->name);
161 //Adding the javascript module for the items dragdrop.
162 if (count($feedbackitems) > 1) {
163 if ($do_show == 'edit') {
164 $PAGE->requires->strings_for_js(array(
165 'pluginname',
166 'move_item',
167 'position',
168 ), 'feedback');
169 $PAGE->requires->yui_module('moodle-mod_feedback-dragdrop', 'M.mod_feedback.init_dragdrop',
170 array(array('cmid' => $cm->id)));
174 echo $OUTPUT->header();
175 echo $OUTPUT->heading(format_string($feedback->name));
177 /// print the tabs
178 require('tabs.php');
180 /// Print the main part of the page.
181 ///////////////////////////////////////////////////////////////////////////
182 ///////////////////////////////////////////////////////////////////////////
183 ///////////////////////////////////////////////////////////////////////////
185 $savereturn=isset($savereturn)?$savereturn:'';
187 //Print the messages.
188 if ($savereturn == 'notsaved_name') {
189 echo '<p align="center"><b><font color="red">'.
190 get_string('name_required', 'feedback').
191 '</font></b></p>';
194 if ($savereturn == 'saved') {
195 echo '<p align="center"><b><font color="green">'.
196 get_string('template_saved', 'feedback').
197 '</font></b></p>';
200 if ($savereturn == 'failed') {
201 echo '<p align="center"><b><font color="red">'.
202 get_string('saving_failed', 'feedback').
203 '</font></b></p>';
206 ///////////////////////////////////////////////////////////////////////////
207 ///Print the template-section.
208 ///////////////////////////////////////////////////////////////////////////
209 if ($do_show == 'templates') {
210 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
211 $use_template_form->display();
213 if (has_capability('mod/feedback:createprivatetemplate', $context) OR
214 has_capability('mod/feedback:createpublictemplate', $context)) {
215 $deleteurl = new moodle_url('/mod/feedback/delete_template.php', array('id' => $id));
216 $create_template_form->display();
217 echo '<p><a href="'.$deleteurl->out().'">'.
218 get_string('delete_templates', 'feedback').
219 '</a></p>';
220 } else {
221 echo '&nbsp;';
224 if (has_capability('mod/feedback:edititems', $context)) {
225 $urlparams = array('action'=>'exportfile', 'id'=>$id);
226 $exporturl = new moodle_url('/mod/feedback/export.php', $urlparams);
227 $importurl = new moodle_url('/mod/feedback/import.php', array('id'=>$id));
228 echo '<p>
229 <a href="'.$exporturl->out().'">'.get_string('export_questions', 'feedback').'</a>/
230 <a href="'.$importurl->out().'">'.get_string('import_questions', 'feedback').'</a>
231 </p>';
233 echo $OUTPUT->box_end();
235 ///////////////////////////////////////////////////////////////////////////
236 ///Print the Item-Edit-section.
237 ///////////////////////////////////////////////////////////////////////////
238 if ($do_show == 'edit') {
240 $add_item_form->display();
242 if (is_array($feedbackitems)) {
243 $itemnr = 0;
245 $align = right_to_left() ? 'right' : 'left';
247 $helpbutton = $OUTPUT->help_icon('preview', 'feedback');
249 echo $OUTPUT->heading(get_string('preview', 'feedback').$helpbutton, 3);
250 if (isset($SESSION->feedback->moving) AND $SESSION->feedback->moving->shouldmoving == 1) {
251 $anker = '<a href="edit.php?id='.$id.'">';
252 $anker .= get_string('cancel_moving', 'feedback');
253 $anker .= '</a>';
254 echo $OUTPUT->heading($anker);
257 //Check, if there exists required-elements.
258 $params = array('feedback' => $feedback->id, 'required' => 1);
259 $countreq = $DB->count_records('feedback_item', $params);
260 if ($countreq > 0) {
261 echo '<div class="fdescription required">';
262 echo get_string('somefieldsrequired', 'form', '<img alt="'.get_string('requiredelement', 'form').
263 '" src="'.$OUTPUT->pix_url('req') .'" class="req" />');
264 echo '</div>';
267 //Use list instead a table
268 echo $OUTPUT->box_start('feedback_items');
269 if (isset($SESSION->feedback->moving) AND $SESSION->feedback->moving->shouldmoving == 1) {
270 $moveposition = 1;
271 $movehereurl = new moodle_url($url, array('movehere'=>$moveposition));
272 //Only shown if shouldmoving = 1
273 echo $OUTPUT->box_start('feedback_item_box_'.$align.' clipboard');
274 $buttonlink = $movehereurl->out();
275 $strbutton = get_string('move_here', 'feedback');
276 $src = $OUTPUT->pix_url('movehere');
277 echo '<a title="'.$strbutton.'" href="'.$buttonlink.'">
278 <img class="movetarget" alt="'.$strbutton.'" src="'.$src.'" />
279 </a>';
280 echo $OUTPUT->box_end();
282 //Print the inserted items
283 $itempos = 0;
284 echo '<div id="feedback_dragarea">'; //The container for the dragging area
285 echo '<ul id="feedback_draglist">'; //The list what includes the draggable items
286 foreach ($feedbackitems as $feedbackitem) {
287 $itempos++;
288 //Hiding the item to move
289 if (isset($SESSION->feedback->moving)) {
290 if ($SESSION->feedback->moving->movingitem == $feedbackitem->id) {
291 continue;
294 //Here come the draggable items, each one in a single li-element.
295 echo '<li class="feedback_itemlist generalbox" id="feedback_item_'.$feedbackitem->id.'">';
296 echo '<span class="spinnertest"> </span>';
297 if ($feedbackitem->dependitem > 0) {
298 $dependstyle = ' feedback_depend';
299 } else {
300 $dependstyle = '';
302 echo $OUTPUT->box_start('feedback_item_box_'.$align.$dependstyle,
303 'feedback_item_box_'.$feedbackitem->id);
304 //Items without value only are labels
305 if ($feedbackitem->hasvalue == 1 AND $feedback->autonumbering) {
306 $itemnr++;
307 echo $OUTPUT->box_start('feedback_item_number_'.$align);
308 echo $itemnr;
309 echo $OUTPUT->box_end();
311 echo $OUTPUT->box_start('box boxalign_'.$align);
312 echo $OUTPUT->box_start('feedback_item_commands_'.$align);
313 echo '<span class="feedback_item_commands position">';
314 echo '('.get_string('position', 'feedback').':'.$itempos .')';
315 echo '</span>';
316 //Print the moveup-button
317 if ($feedbackitem->position > 1) {
318 echo '<span class="feedback_item_command_moveup">';
319 $moveupurl = new moodle_url($url, array('moveupitem'=>$feedbackitem->id));
320 $buttonlink = $moveupurl->out();
321 $strbutton = get_string('moveup_item', 'feedback');
322 echo '<a class="icon up" title="'.$strbutton.'" href="'.$buttonlink.'">
323 <img alt="'.$strbutton.'" src="'.$OUTPUT->pix_url('t/up') . '" />
324 </a>';
325 echo '</span>';
327 //Print the movedown-button
328 if ($feedbackitem->position < $lastposition - 1) {
329 echo '<span class="feedback_item_command_movedown">';
330 $urlparams = array('movedownitem'=>$feedbackitem->id);
331 $movedownurl = new moodle_url($url, $urlparams);
332 $buttonlink = $movedownurl->out();
333 $strbutton = get_string('movedown_item', 'feedback');
334 echo '<a class="icon down" title="'.$strbutton.'" href="'.$buttonlink.'">
335 <img alt="'.$strbutton.'" src="'.$OUTPUT->pix_url('t/down') . '" />
336 </a>';
337 echo '</span>';
339 //Print the move-button
340 if (count($feedbackitems) > 1) {
341 echo '<span class="feedback_item_command_move">';
342 $moveurl = new moodle_url($url, array('moveitem'=>$feedbackitem->id));
343 $buttonlink = $moveurl->out();
344 $strbutton = get_string('move_item', 'feedback');
345 echo '<a class="editing_move" title="'.$strbutton.'" href="'.$buttonlink.'">
346 <img alt="'.$strbutton.'" src="'.$OUTPUT->pix_url('t/move') . '" />
347 </a>';
348 echo '</span>';
350 //Print the button to edit the item
351 if ($feedbackitem->typ != 'pagebreak') {
352 echo '<span class="feedback_item_command_edit">';
353 $editurl = new moodle_url('/mod/feedback/edit_item.php');
354 $editurl->params(array('do_show'=>$do_show,
355 'cmid'=>$id,
356 'id'=>$feedbackitem->id,
357 'typ'=>$feedbackitem->typ));
359 // In edit_item.php the param id is used for the itemid
360 // and the cmid is the id to get the module.
361 $buttonlink = $editurl->out();
362 $strbutton = get_string('edit_item', 'feedback');
363 echo '<a class="editing_update" title="'.$strbutton.'" href="'.$buttonlink.'">
364 <img alt="'.$strbutton.'" src="'.$OUTPUT->pix_url('t/edit') . '" />
365 </a>';
366 echo '</span>';
369 //Print the toggle-button to switch required yes/no
370 if ($feedbackitem->hasvalue == 1) {
371 echo '<span class="feedback_item_command_toggle">';
372 if ($feedbackitem->required == 1) {
373 $buttontitle = get_string('switch_item_to_not_required', 'feedback');
374 $buttonimg = $OUTPUT->pix_url('required', 'feedback');
375 } else {
376 $buttontitle = get_string('switch_item_to_required', 'feedback');
377 $buttonimg = $OUTPUT->pix_url('notrequired', 'feedback');
379 $urlparams = array('switchitemrequired'=>$feedbackitem->id);
380 $requiredurl = new moodle_url($url, $urlparams);
381 $buttonlink = $requiredurl->out();
382 echo '<a class="icon '.
383 'feedback_switchrequired" '.
384 'title="'.$buttontitle.'" '.
385 'href="'.$buttonlink.'">'.
386 '<img alt="'.$buttontitle.'" src="'.$buttonimg.'" />'.
387 '</a>';
388 echo '</span>';
391 //Print the delete-button
392 echo '<span class="feedback_item_command_toggle">';
393 $deleteitemurl = new moodle_url('/mod/feedback/delete_item.php');
394 $deleteitemurl->params(array('id'=>$id,
395 'do_show'=>$do_show,
396 'deleteitem'=>$feedbackitem->id));
398 $buttonlink = $deleteitemurl->out();
399 $strbutton = get_string('delete_item', 'feedback');
400 $src = $OUTPUT->pix_url('t/delete');
401 echo '<a class="icon delete" title="'.$strbutton.'" href="'.$buttonlink.'">
402 <img alt="'.$strbutton.'" src="'.$src.'" />
403 </a>';
404 echo '</span>';
405 echo $OUTPUT->box_end();
406 if ($feedbackitem->typ != 'pagebreak') {
407 feedback_print_item_preview($feedbackitem);
408 } else {
409 echo $OUTPUT->box_start('feedback_pagebreak');
410 echo get_string('pagebreak', 'feedback').'<hr class="feedback_pagebreak" />';
411 echo $OUTPUT->box_end();
413 echo $OUTPUT->box_end();
414 echo $OUTPUT->box_end();
415 echo '<div class="clearer">&nbsp;</div>';
416 echo '</li>';
417 //Print out the target box if we ar moving an item
418 if (isset($SESSION->feedback->moving) AND $SESSION->feedback->moving->shouldmoving == 1) {
419 echo '<li>';
420 $moveposition++;
421 $movehereurl->param('movehere', $moveposition);
422 echo $OUTPUT->box_start('clipboard'); //Only shown if shouldmoving = 1
423 $buttonlink = $movehereurl->out();
424 $strbutton = get_string('move_here', 'feedback');
425 $src = $OUTPUT->pix_url('movehere');
426 echo '<a title="'.$strbutton.'" href="'.$buttonlink.'">
427 <img class="movetarget" alt="'.$strbutton.'" src="'.$src.'" />
428 </a>';
429 echo $OUTPUT->box_end();
430 echo '</li>';
433 echo $OUTPUT->box_end();
434 echo '</ul>';
435 echo '</div>';
436 } else {
437 echo $OUTPUT->box(get_string('no_items_available_yet', 'feedback'),
438 'generalbox boxaligncenter');
441 /// Finish the page
442 ///////////////////////////////////////////////////////////////////////////
443 ///////////////////////////////////////////////////////////////////////////
444 ///////////////////////////////////////////////////////////////////////////
446 echo $OUTPUT->footer();