MDL-21940 removing the old custom exception handling from nonajax code too + redirect...
[moodle.git] / mod / feedback / edit.php
CommitLineData
84973212 1<?php
2
c70ad9f7 3/**
4* prints the form to edit the feedback items such moving, deleting and so on
5*
c70ad9f7 6* @author Andreas Grabs
7* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8* @package feedback
9*/
10
84973212 11require_once("../../config.php");
12require_once("lib.php");
13require_once('edit_form.php');
f1b9e2df 14
84973212 15feedback_init_feedback_session();
d4b1d58c 16
84973212 17$id = required_param('id', PARAM_INT);
b7a47958 18
84973212 19if(($formdata = data_submitted()) AND !confirm_sesskey()) {
20 print_error('invalidsesskey');
21}
d4b1d58c 22
84973212 23$do_show = optional_param('do_show', 'edit', PARAM_ALPHA);
24$moveupitem = optional_param('moveupitem', false, PARAM_INT);
25$movedownitem = optional_param('movedownitem', false, PARAM_INT);
26$moveitem = optional_param('moveitem', false, PARAM_INT);
27$movehere = optional_param('movehere', false, PARAM_INT);
28$switchitemrequired = optional_param('switchitemrequired', false, PARAM_INT);
d4b1d58c 29
84973212 30// $SESSION->feedback->current_tab = $do_show;
31$current_tab = $do_show;
d4b1d58c 32
84973212 33if ($id) {
34 if (! $cm = get_coursemodule_from_id('feedback', $id)) {
35 print_error('invalidcoursemodule');
c70ad9f7 36 }
d4b1d58c 37
84973212 38 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
39 print_error('coursemisconf');
c70ad9f7 40 }
41
84973212 42 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
43 print_error('invalidcoursemodule');
c70ad9f7 44 }
84973212 45}
46$capabilities = feedback_load_capabilities($cm->id);
47
48require_login($course->id, true, $cm);
49
50if(!$capabilities->edititems){
51 print_error('error');
52}
53
54//move up/down items
55if($moveupitem){
56 $item = $DB->get_record('feedback_item', array('id'=>$moveupitem));
57 feedback_moveup_item($item);
58}
59if($movedownitem){
60 $item = $DB->get_record('feedback_item', array('id'=>$movedownitem));
61 feedback_movedown_item($item);
62}
63
64//moving of items
65if($movehere && isset($SESSION->feedback->moving->movingitem)){
66 $item = $DB->get_record('feedback_item', array('id'=>$SESSION->feedback->moving->movingitem));
67 feedback_move_item($item, intval($movehere));
68 $moveitem = false;
69}
70if($moveitem){
71 $item = $DB->get_record('feedback_item', array('id'=>$moveitem));
72 $SESSION->feedback->moving->shouldmoving = 1;
73 $SESSION->feedback->moving->movingitem = $moveitem;
74} else {
75 unset($SESSION->feedback->moving);
76}
77
78if($switchitemrequired) {
79 $item = $DB->get_record('feedback_item', array('id'=>$switchitemrequired));
80 @feedback_switch_item_required($item);
a6855934 81 redirect($CFG->wwwroot.'/mod/feedback/edit.php?'.feedback_edit_get_default_query($id, $do_show));
84973212 82 exit;
83}
84
85//the create_template-form
86$create_template_form = new feedback_edit_create_template_form();
87$create_template_form->set_feedbackdata(array('capabilities' => $capabilities));
88$create_template_form->set_form_elements();
89$create_template_form->set_data(array('id'=>$id, 'do_show'=>'templates'));
90$create_template_formdata = $create_template_form->get_data();
91if(isset($create_template_formdata->savetemplate) && $create_template_formdata->savetemplate == 1) {
92 //check the capabilities to create templates
93 if(!$capabilities->createprivatetemplate AND !$capabilities->createpublictemplate) {
94 print_error('cannotsavetempl', 'feedback');
c70ad9f7 95 }
84973212 96 if(trim($create_template_formdata->templatename) == '')
97 {
98 $savereturn = 'notsaved_name';
99 }else {
100 if($capabilities->createpublictemplate) {
101 $create_template_formdata->ispublic = isset($create_template_formdata->ispublic) ? 1 : 0;
102 }else {
103 $create_template_formdata->ispublic = 0;
c70ad9f7 104 }
84973212 105 if(!feedback_save_as_template($feedback, $create_template_formdata->templatename, $create_template_formdata->ispublic))
c70ad9f7 106 {
84973212 107 $savereturn = 'failed';
c70ad9f7 108 }else {
84973212 109 $savereturn = 'saved';
c70ad9f7 110 }
111 }
84973212 112}
113
114//get the feedbackitems
115$lastposition = 0;
116$feedbackitems = $DB->get_records('feedback_item', array('feedback'=>$feedback->id), 'position');
117if(is_array($feedbackitems)){
118 $feedbackitems = array_values($feedbackitems);
119 if(count($feedbackitems) > 0) {
120 $lastitem = $feedbackitems[count($feedbackitems)-1];
121 $lastposition = $lastitem->position;
122 }else {
123 $lastposition = 0;
c70ad9f7 124 }
84973212 125}
126$lastposition++;
127
128
129//the add_item-form
130$add_item_form = new feedback_edit_add_question_form('edit_item.php');
7e55c2ef 131$add_item_form->set_data(array('cmid'=>$id, 'position'=>$lastposition));
84973212 132
133//the use_template-form
134$use_template_form = new feedback_edit_use_template_form('use_templ.php');
135$use_template_form->set_feedbackdata(array('course' => $course));
136$use_template_form->set_form_elements();
137$use_template_form->set_data(array('id'=>$id));
138
139//the create_template-form
140//$create_template_form = new feedback_edit_create_template_form('use_templ.php');
141
142/// Print the page header
143$strfeedbacks = get_string("modulenameplural", "feedback");
144$strfeedback = get_string("modulename", "feedback");
145
146if ($do_show == 'edit') {
147 $PAGE->navbar->add(get_string('edit_items', 'feedback'));
148} else {
149 $PAGE->navbar->add(get_string($do_show, 'feedback'));
150}
a6855934 151$PAGE->set_url('/mod/feedback/edit.php', array('id'=>$cm->id, 'do_show'=>$do_show));
84973212 152$PAGE->set_title(format_string($feedback->name));
84973212 153echo $OUTPUT->header();
154
155/// print the tabs
156include('tabs.php');
157
158/// Print the main part of the page
159///////////////////////////////////////////////////////////////////////////
160///////////////////////////////////////////////////////////////////////////
161///////////////////////////////////////////////////////////////////////////
162
163$savereturn=isset($savereturn)?$savereturn:'';
164
165//print the messages
166if($savereturn == 'notsaved_name') {
167 echo '<p align="center"><b><font color="red">'.get_string('name_required','feedback').'</font></b></p>';
168}
169
170if($savereturn == 'saved') {
171 echo '<p align="center"><b><font color="green">'.get_string('template_saved','feedback').'</font></b></p>';
172}
173
174if($savereturn == 'failed') {
175 echo '<p align="center"><b><font color="red">'.get_string('saving_failed','feedback').'</font></b></p>';
176}
177
178feedback_print_errors();
179
180///////////////////////////////////////////////////////////////////////////
181///print the template-section
182///////////////////////////////////////////////////////////////////////////
183if($do_show == 'templates') {
184 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
185 $use_template_form->display();
186
187 if($capabilities->createprivatetemplate OR $capabilities->createpublictemplate) {
188 $create_template_form->display();
189 echo '<p><a href="'.htmlspecialchars('delete_template.php?id='.$id).'">'.get_string('delete_templates', 'feedback').'</a></p>';
190 }else {
191 echo '&nbsp;';
ab5bd34d 192 }
c70ad9f7 193
84973212 194 if($capabilities->edititems) {
195 echo '<p>
196 <a href="'.htmlspecialchars('export.php?action=exportfile&id='.$id).'">'.get_string('export_questions', 'feedback').'</a>/
197 <a href="'.htmlspecialchars('import.php?id='.$id).'">'.get_string('import_questions', 'feedback').'</a>
198 </p>';
c70ad9f7 199 }
84973212 200 echo $OUTPUT->box_end();
201}
202///////////////////////////////////////////////////////////////////////////
203///print the Item-Edit-section
204///////////////////////////////////////////////////////////////////////////
205if($do_show == 'edit') {
d4b1d58c 206
84973212 207 $add_item_form->display();
c70ad9f7 208
84973212 209 if(is_array($feedbackitems)){
210 $itemnr = 0;
d4b1d58c 211
4bcc5118 212 $helpbutton = $OUTPUT->help_icon('preview', get_string('preview','feedback'), 'feedback',true);
d4b1d58c 213
84973212 214 echo $OUTPUT->heading($helpbutton . get_string('preview', 'feedback'));
215 if(isset($SESSION->feedback->moving) AND $SESSION->feedback->moving->shouldmoving == 1) {
a6855934 216 echo $OUTPUT->heading('<a href="edit.php?id='.$id.'">'.get_string('cancel_moving', 'feedback').'</a>');
c70ad9f7 217 }
84973212 218 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
c70ad9f7 219
84973212 220 //check, if there exists required-elements
221 $countreq = $DB->count_records('feedback_item', array('feedback'=>$feedback->id, 'required'=> 1));
222 if($countreq > 0) {
223 // echo '<font color="red">(*)' . get_string('items_are_required', 'feedback') . '</font>';
224 echo '<span class="feedback_required_mark">(*)' . get_string('items_are_required', 'feedback') . '</span>';
c70ad9f7 225 }
c70ad9f7 226
84973212 227 echo '<table>';
228 if(isset($SESSION->feedback->moving) AND $SESSION->feedback->moving->shouldmoving == 1) {
229 $moveposition = 1;
230 echo '<tr>'; //only shown if shouldmoving = 1
231 echo '<td>';
a6855934 232 $buttonlink = 'edit.php?'.htmlspecialchars(feedback_edit_get_default_query($id, $do_show).'&movehere='.$moveposition);
84973212 233 echo '<a title="'.get_string('move_here','feedback').'" href="'.$buttonlink.'">
b5d0cafc 234 <img class="movetarget" alt="'.get_string('move_here','feedback').'" src="'.$OUTPUT->pix_url('movehere') . '" />
84973212 235 </a>';
d4b1d58c 236
a6855934 237 // echo '<form action="edit.php" method="post"><fieldset>';
b5d0cafc 238 // echo '<input title="'.get_string('move_here','feedback').'" type="image" src="'.$OUTPUT->pix_url('movehere') . '" hspace="1" height="16" width="80" border="0" />';
84973212 239 // echo '<input type="hidden" name="movehere" value="'.$moveposition.'" />';
240 // feedback_edit_print_default_form_values($id, $do_show);
241 // echo '</fieldset></form>';
242 echo '</td>';
243 echo '</tr>';
244 }
245 //print the inserted items
246 $itempos = 0;
247 foreach($feedbackitems as $feedbackitem){
248 $itempos++;
249 if(isset($SESSION->feedback->moving) AND $SESSION->feedback->moving->movingitem == $feedbackitem->id){ //hiding the item to move
250 continue;
c70ad9f7 251 }
84973212 252 echo '<tr>';
253 //items without value only are labels
254 if($feedbackitem->hasvalue == 1 AND $feedback->autonumbering) {
255 $itemnr++;
256 echo '<td valign="top">' . $itemnr . '.&nbsp;</td>';
257 } else {
258 echo '<td>&nbsp;</td>';
c70ad9f7 259 }
84973212 260 if($feedbackitem->typ != 'pagebreak') {
261 feedback_print_item($feedbackitem, false, false, true);
262 }else {
263 echo '<td class="feedback_pagebreak"><b>'.get_string('pagebreak', 'feedback').'</b></td><td><hr width="100%" size="8px" noshade="noshade" /></td>';
264 }
265 echo '<td>('.get_string('position', 'feedback').':'.$itempos .')</td>';
266 echo '<td>';
267 if($feedbackitem->position > 1){
a6855934 268 $buttonlink = 'edit.php?'.htmlspecialchars(feedback_edit_get_default_query($id, $do_show).'&moveupitem='.$feedbackitem->id);
84973212 269 echo '<a class="icon up" title="'.get_string('moveup_item','feedback').'" href="'.$buttonlink.'">
b5d0cafc 270 <img alt="'.get_string('moveup_item','feedback').'" src="'.$OUTPUT->pix_url('t/up') . '" />
84973212 271 </a>';
272 //print the button to move-up the item
a6855934 273 // echo '<form action="edit.php" method="post"><fieldset>';
b5d0cafc
PS
274 // ///////echo '<input title="'.get_string('moveup_item','feedback').'" type="image" src="'.$OUTPUT->pix_url('t/up') . '" hspace="1" height="11" width="11" border="0" />';
275 // echo '<input class="feedback_moveup_button" title="'.get_string('moveup_item','feedback').'" type="image" src="'.$OUTPUT->pix_url('t/up') . '" />';
84973212 276 // echo '<input type="hidden" name="moveupitem" value="'.$feedbackitem->id.'" />';
277 // feedback_edit_print_default_form_values($id, $do_show);
278 // echo '</fieldset></form>';
279 }else{
280 echo '&nbsp;';
281 }
282 echo '</td>';
283 echo '<td>';
284 if($feedbackitem->position < $lastposition - 1){
a6855934 285 $buttonlink = 'edit.php?'.htmlspecialchars(feedback_edit_get_default_query($id, $do_show).'&movedownitem='.$feedbackitem->id);
84973212 286 echo '<a class="icon down" title="'.get_string('movedown_item','feedback').'" href="'.$buttonlink.'">
b5d0cafc 287 <img alt="'.get_string('movedown_item','feedback').'" src="'.$OUTPUT->pix_url('t/down') . '" />
84973212 288 </a>';
289 //print the button to move-down the item
a6855934 290 // echo '<form action="edit.php" method="post"><fieldset>';
b5d0cafc
PS
291 // echo '<input title="'.get_string('movedown_item','feedback').'" type="image" src="'.$OUTPUT->pix_url('t/down') . '" hspace="1" height="11" width="11" border="0" />';
292 // echo '<input class="feedback_movedown_button" title="'.get_string('movedown_item','feedback').'" type="image" src="'.$OUTPUT->pix_url('t/down') . '" />';
84973212 293 // echo '<input type="hidden" name="movedownitem" value="'.$feedbackitem->id.'" />';
294 // feedback_edit_print_default_form_values($id, $do_show);
295 // echo '</fieldset></form>';
296 }else{
297 echo '&nbsp;';
298 }
299 echo '</td>';
300 echo '<td>';
a6855934 301 $buttonlink = 'edit.php?'.htmlspecialchars(feedback_edit_get_default_query($id, $do_show).'&moveitem='.$feedbackitem->id);
84973212 302 echo '<a class="editing_move" title="'.get_string('move_item','feedback').'" href="'.$buttonlink.'">
b5d0cafc 303 <img alt="'.get_string('move_item','feedback').'" src="'.$OUTPUT->pix_url('t/move') . '" />
84973212 304 </a>';
a6855934 305 // echo '<form action="edit.php" method="post"><fieldset>';
b5d0cafc
PS
306 // echo '<input title="'.get_string('move_item','feedback').'" type="image" src="'.$OUTPUT->pix_url('t/move') . '" hspace="1" height="11" width="11" border="0" />';
307 // echo '<input class="feedback_move_button" title="'.get_string('move_item','feedback').'" type="image" src="'.$OUTPUT->pix_url('t/move') . '" />';
84973212 308 // echo '<input type="hidden" name="moveitem" value="'.$feedbackitem->id.'" />';
309 // feedback_edit_print_default_form_values($id, $do_show);
310 // echo '</fieldset></form>';
311 echo '</td>';
312 echo '<td>';
313 //print the button to edit the item
314 if($feedbackitem->typ != 'pagebreak') {
d5e51c5a
AG
315 // in edit_item.php the param id is used for the itemid and the cmid is the id to get the module
316 $buttonlink = 'edit_item.php?'.htmlspecialchars('do_show='.$do_show.'&cmid='.$id.'&id='.$feedbackitem->id.'&typ='.$feedbackitem->typ);
84973212 317 echo '<a class="editing_update" title="'.get_string('edit_item','feedback').'" href="'.$buttonlink.'">
b5d0cafc 318 <img alt="'.get_string('edit_item','feedback').'" src="'.$OUTPUT->pix_url('t/edit') . '" />
84973212 319 </a>';
320 // echo '<form action="edit_item.php" method="post"><fieldset>';
b5d0cafc
PS
321 // echo '<input title="'.get_string('edit_item','feedback').'" type="image" src="'.$OUTPUT->pix_url('t/edit') . '" hspace="1" height="11" width="11" border="0" />';
322 // echo '<input class="feedback_edit_button" title="'.get_string('edit_item','feedback').'" type="image" src="'.$OUTPUT->pix_url('t/edit') . '" />';
84973212 323 // echo '<input type="hidden" name="itemid" value="'.$feedbackitem->id.'" />';
324 // echo '<input type="hidden" name="typ" value="'.$feedbackitem->typ.'" />';
325 // feedback_edit_print_default_form_values($id, $do_show);
326 // echo '</fieldset></form>';
327 }else {
328 echo '&nbsp;';
329 }
330 echo '</td>';
331 echo '<td>';
332
333 //print the toggle-button to switch required yes/no
334 if($feedbackitem->hasvalue == 1) {
a6855934 335 // echo '<form action="edit.php" method="post"><fieldset>';
84973212 336 if($feedbackitem->required == 1) {
337 // echo '<input title="'.get_string('switch_item_to_not_required','feedback').'" type="image" src="pics/required.gif" hspace="1" height="11" width="11" border="0" />';
338 // echo '<input class="feedback_required_button" title="'.get_string('switch_item_to_not_required','feedback').'" type="image" src="pics/required.gif" />';
339 $buttontitle = get_string('switch_item_to_not_required','feedback');
340 $buttonimg = 'pics/required.gif';
341 } else {
342 // echo '<input title="'.get_string('switch_item_to_required','feedback').'" type="image" src="pics/notrequired.gif" hspace="1" height="11" width="11" border="0" />';
343 // echo '<input class="feedback_required_button" title="'.get_string('switch_item_to_required','feedback').'" type="image" src="pics/notrequired.gif" />';
344 $buttontitle = get_string('switch_item_to_required','feedback');
345 $buttonimg = 'pics/notrequired.gif';
346 }
a6855934 347 $buttonlink = 'edit.php?'.htmlspecialchars(feedback_edit_get_default_query($id, $do_show).'&switchitemrequired='.$feedbackitem->id);
84973212 348 echo '<a class="icon feedback_switchrequired" title="'.$buttontitle.'" href="'.$buttonlink.'">
349 <img alt="'.$buttontitle.'" src="'.$buttonimg.'" />
350 </a>';
351 // echo '<input type="hidden" name="switchitemrequired" value="'.$feedbackitem->id.'" />';
352 // feedback_edit_print_default_form_values($id, $do_show);
353 // echo '</fieldset></form>';
354 }else {
355 echo '&nbsp;';
356 }
357 echo '</td>';
358 echo '<td>';
359 $buttonlink = 'delete_item.php?'.htmlspecialchars(feedback_edit_get_default_query($id, $do_show).'&deleteitem='.$feedbackitem->id);
360 echo '<a class="icon delete" title="'.get_string('delete_item','feedback').'" href="'.$buttonlink.'">
b5d0cafc 361 <img alt="'.get_string('delete_item','feedback').'" src="'.$OUTPUT->pix_url('t/delete') . '" />
84973212 362 </a>';
363 //print the button to drop the item
364 // echo '<form action="delete_item.php" method="post"><fieldset>';
b5d0cafc 365 // echo '<input class="feedback_delete_button" title="'.get_string('delete_item','feedback').'" type="image" src="'.$OUTPUT->pix_url('t/delete') . '" />';
84973212 366 // echo '<input type="hidden" name="deleteitem" value="'.$feedbackitem->id.'" />';
367 // feedback_edit_print_default_form_values($id, $do_show);
368 // echo '</fieldset></form>';
369 echo '</td>';
370 echo '</tr>';
c70ad9f7 371 if(isset($SESSION->feedback->moving) AND $SESSION->feedback->moving->shouldmoving == 1) {
84973212 372 $moveposition++;
c70ad9f7 373 echo '<tr>'; //only shown if shouldmoving = 1
374 echo '<td>';
a6855934 375 $buttonlink = 'edit.php?'.htmlspecialchars(feedback_edit_get_default_query($id, $do_show).'&movehere='.$moveposition);
84973212 376 echo '<a title="'.get_string('move_here','feedback').'" href="'.$buttonlink.'">
b5d0cafc 377 <img class="movetarget" alt="'.get_string('move_here','feedback').'" src="'.$OUTPUT->pix_url('movehere') . '" />
84973212 378 </a>';
a6855934 379 // echo '<form action="edit.php" method="post"><fieldset>';
b5d0cafc 380 // echo '<input class="feedback_movehere_button" title="'.get_string('move_here','feedback').'" type="image" src="'.$OUTPUT->pix_url('movehere') . '" />';
c70ad9f7 381 // echo '<input type="hidden" name="movehere" value="'.$moveposition.'" />';
382 // feedback_edit_print_default_form_values($id, $do_show);
383 // echo '</fieldset></form>';
384 echo '</td>';
385 echo '</tr>';
84973212 386 }else {
387 echo '<tr><td>&nbsp;</td></tr>';
c70ad9f7 388 }
d4b1d58c 389
c70ad9f7 390 }
84973212 391 echo '</table>';
392 echo $OUTPUT->box_end();
393 }else{
394 echo $OUTPUT->box(get_string('no_items_available_yet','feedback'),'generalbox boxaligncenter');
c70ad9f7 395 }
84973212 396}
397/// Finish the page
398///////////////////////////////////////////////////////////////////////////
399///////////////////////////////////////////////////////////////////////////
400///////////////////////////////////////////////////////////////////////////
c70ad9f7 401
84973212 402echo $OUTPUT->footer();
c70ad9f7 403
84973212 404function feedback_edit_get_default_query($id, $tab) {
405 global $USER;
d4b1d58c 406
84973212 407 $query = 'id='.$id;
408 $query .= '&do_show='.$tab;
409 //$query .= '&sesskey='.sesskey();
d4b1d58c 410
84973212 411 return $query;
412}
c70ad9f7 413
84973212 414function feedback_edit_print_default_form_values($id, $tab) {
415 global $USER;
d4b1d58c 416
84973212 417 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
418 echo '<input type="hidden" name="id" value="'.$id.'" />';
419 echo '<input type="hidden" name="do_show" value="'.$tab.'" />';
420}