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