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