MDL-40918 mod_lti: replaced 'view' and 'view all' add_to_log calls with events
[moodle.git] / mod / feedback / edit.php
blob0b0921312ccaaa714294239e25dc66ea8d67b518
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 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(format_string($course->fullname));
159 $PAGE->set_title(format_string($feedback->name));
161 //Adding the javascript module for the items dragdrop.
162 if (count($feedbackitems) > 1) {
163 if ($do_show == 'edit' and $CFG->enableajax) {
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 '<span class="feedback_required_mark">(*)';
262 echo get_string('items_are_required', 'feedback');
263 echo '</span>';
266 //Use list instead a table
267 echo $OUTPUT->box_start('feedback_items');
268 if (isset($SESSION->feedback->moving) AND $SESSION->feedback->moving->shouldmoving == 1) {
269 $moveposition = 1;
270 $movehereurl = new moodle_url($url, array('movehere'=>$moveposition));
271 //Only shown if shouldmoving = 1
272 echo $OUTPUT->box_start('feedback_item_box_'.$align.' clipboard');
273 $buttonlink = $movehereurl->out();
274 $strbutton = get_string('move_here', 'feedback');
275 $src = $OUTPUT->pix_url('movehere');
276 echo '<a title="'.$strbutton.'" href="'.$buttonlink.'">
277 <img class="movetarget" alt="'.$strbutton.'" src="'.$src.'" />
278 </a>';
279 echo $OUTPUT->box_end();
281 //Print the inserted items
282 $itempos = 0;
283 echo '<div id="feedback_dragarea">'; //The container for the dragging area
284 echo '<ul id="feedback_draglist">'; //The list what includes the draggable items
285 foreach ($feedbackitems as $feedbackitem) {
286 $itempos++;
287 //Hiding the item to move
288 if (isset($SESSION->feedback->moving)) {
289 if ($SESSION->feedback->moving->movingitem == $feedbackitem->id) {
290 continue;
293 //Here come the draggable items, each one in a single li-element.
294 echo '<li class="feedback_itemlist generalbox" id="feedback_item_'.$feedbackitem->id.'">';
295 echo '<span class="spinnertest"> </span>';
296 if ($feedbackitem->dependitem > 0) {
297 $dependstyle = ' feedback_depend';
298 } else {
299 $dependstyle = '';
301 echo $OUTPUT->box_start('feedback_item_box_'.$align.$dependstyle,
302 'feedback_item_box_'.$feedbackitem->id);
303 //Items without value only are labels
304 if ($feedbackitem->hasvalue == 1 AND $feedback->autonumbering) {
305 $itemnr++;
306 echo $OUTPUT->box_start('feedback_item_number_'.$align);
307 echo $itemnr;
308 echo $OUTPUT->box_end();
310 echo $OUTPUT->box_start('box boxalign_'.$align);
311 echo $OUTPUT->box_start('feedback_item_commands_'.$align);
312 echo '<span class="feedback_item_commands position">';
313 echo '('.get_string('position', 'feedback').':'.$itempos .')';
314 echo '</span>';
315 //Print the moveup-button
316 if ($feedbackitem->position > 1) {
317 echo '<span class="feedback_item_command_moveup">';
318 $moveupurl = new moodle_url($url, array('moveupitem'=>$feedbackitem->id));
319 $buttonlink = $moveupurl->out();
320 $strbutton = get_string('moveup_item', 'feedback');
321 echo '<a class="icon up" title="'.$strbutton.'" href="'.$buttonlink.'">
322 <img alt="'.$strbutton.'" src="'.$OUTPUT->pix_url('t/up') . '" />
323 </a>';
324 echo '</span>';
326 //Print the movedown-button
327 if ($feedbackitem->position < $lastposition - 1) {
328 echo '<span class="feedback_item_command_movedown">';
329 $urlparams = array('movedownitem'=>$feedbackitem->id);
330 $movedownurl = new moodle_url($url, $urlparams);
331 $buttonlink = $movedownurl->out();
332 $strbutton = get_string('movedown_item', 'feedback');
333 echo '<a class="icon down" title="'.$strbutton.'" href="'.$buttonlink.'">
334 <img alt="'.$strbutton.'" src="'.$OUTPUT->pix_url('t/down') . '" />
335 </a>';
336 echo '</span>';
338 //Print the move-button
339 if (count($feedbackitems) > 1) {
340 echo '<span class="feedback_item_command_move">';
341 $moveurl = new moodle_url($url, array('moveitem'=>$feedbackitem->id));
342 $buttonlink = $moveurl->out();
343 $strbutton = get_string('move_item', 'feedback');
344 echo '<a class="editing_move" title="'.$strbutton.'" href="'.$buttonlink.'">
345 <img alt="'.$strbutton.'" src="'.$OUTPUT->pix_url('t/move') . '" />
346 </a>';
347 echo '</span>';
349 //Print the button to edit the item
350 if ($feedbackitem->typ != 'pagebreak') {
351 echo '<span class="feedback_item_command_edit">';
352 $editurl = new moodle_url('/mod/feedback/edit_item.php');
353 $editurl->params(array('do_show'=>$do_show,
354 'cmid'=>$id,
355 'id'=>$feedbackitem->id,
356 'typ'=>$feedbackitem->typ));
358 // In edit_item.php the param id is used for the itemid
359 // and the cmid is the id to get the module.
360 $buttonlink = $editurl->out();
361 $strbutton = get_string('edit_item', 'feedback');
362 echo '<a class="editing_update" title="'.$strbutton.'" href="'.$buttonlink.'">
363 <img alt="'.$strbutton.'" src="'.$OUTPUT->pix_url('t/edit') . '" />
364 </a>';
365 echo '</span>';
368 //Print the toggle-button to switch required yes/no
369 if ($feedbackitem->hasvalue == 1) {
370 echo '<span class="feedback_item_command_toggle">';
371 if ($feedbackitem->required == 1) {
372 $buttontitle = get_string('switch_item_to_not_required', 'feedback');
373 $buttonimg = $OUTPUT->pix_url('required', 'feedback');
374 } else {
375 $buttontitle = get_string('switch_item_to_required', 'feedback');
376 $buttonimg = $OUTPUT->pix_url('notrequired', 'feedback');
378 $urlparams = array('switchitemrequired'=>$feedbackitem->id);
379 $requiredurl = new moodle_url($url, $urlparams);
380 $buttonlink = $requiredurl->out();
381 echo '<a class="icon '.
382 'feedback_switchrequired" '.
383 'title="'.$buttontitle.'" '.
384 'href="'.$buttonlink.'">'.
385 '<img alt="'.$buttontitle.'" src="'.$buttonimg.'" />'.
386 '</a>';
387 echo '</span>';
390 //Print the delete-button
391 echo '<span class="feedback_item_command_toggle">';
392 $deleteitemurl = new moodle_url('/mod/feedback/delete_item.php');
393 $deleteitemurl->params(array('id'=>$id,
394 'do_show'=>$do_show,
395 'deleteitem'=>$feedbackitem->id));
397 $buttonlink = $deleteitemurl->out();
398 $strbutton = get_string('delete_item', 'feedback');
399 $src = $OUTPUT->pix_url('t/delete');
400 echo '<a class="icon delete" title="'.$strbutton.'" href="'.$buttonlink.'">
401 <img alt="'.$strbutton.'" src="'.$src.'" />
402 </a>';
403 echo '</span>';
404 echo $OUTPUT->box_end();
405 if ($feedbackitem->typ != 'pagebreak') {
406 feedback_print_item_preview($feedbackitem);
407 } else {
408 echo $OUTPUT->box_start('feedback_pagebreak');
409 echo get_string('pagebreak', 'feedback').'<hr class="feedback_pagebreak" />';
410 echo $OUTPUT->box_end();
412 echo $OUTPUT->box_end();
413 echo $OUTPUT->box_end();
414 echo '<div class="clearer">&nbsp;</div>';
415 echo '</li>';
416 //Print out the target box if we ar moving an item
417 if (isset($SESSION->feedback->moving) AND $SESSION->feedback->moving->shouldmoving == 1) {
418 echo '<li>';
419 $moveposition++;
420 $movehereurl->param('movehere', $moveposition);
421 echo $OUTPUT->box_start('clipboard'); //Only shown if shouldmoving = 1
422 $buttonlink = $movehereurl->out();
423 $strbutton = get_string('move_here', 'feedback');
424 $src = $OUTPUT->pix_url('movehere');
425 echo '<a title="'.$strbutton.'" href="'.$buttonlink.'">
426 <img class="movetarget" alt="'.$strbutton.'" src="'.$src.'" />
427 </a>';
428 echo $OUTPUT->box_end();
429 echo '</li>';
432 echo $OUTPUT->box_end();
433 echo '</ul>';
434 echo '</div>';
435 } else {
436 echo $OUTPUT->box(get_string('no_items_available_yet', 'feedback'),
437 'generalbox boxaligncenter');
440 /// Finish the page
441 ///////////////////////////////////////////////////////////////////////////
442 ///////////////////////////////////////////////////////////////////////////
443 ///////////////////////////////////////////////////////////////////////////
445 echo $OUTPUT->footer();