Updated the HEAD build version to 20100116
[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));
84973212 155echo $OUTPUT->header();
156
157/// print the tabs
158include('tabs.php');
159
160/// Print the main part of the page
161///////////////////////////////////////////////////////////////////////////
162///////////////////////////////////////////////////////////////////////////
163///////////////////////////////////////////////////////////////////////////
164
165$savereturn=isset($savereturn)?$savereturn:'';
166
167//print the messages
168if($savereturn == 'notsaved_name') {
169 echo '<p align="center"><b><font color="red">'.get_string('name_required','feedback').'</font></b></p>';
170}
171
172if($savereturn == 'saved') {
173 echo '<p align="center"><b><font color="green">'.get_string('template_saved','feedback').'</font></b></p>';
174}
175
176if($savereturn == 'failed') {
177 echo '<p align="center"><b><font color="red">'.get_string('saving_failed','feedback').'</font></b></p>';
178}
179
180feedback_print_errors();
181
182///////////////////////////////////////////////////////////////////////////
183///print the template-section
184///////////////////////////////////////////////////////////////////////////
185if($do_show == 'templates') {
186 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
187 $use_template_form->display();
188
189 if($capabilities->createprivatetemplate OR $capabilities->createpublictemplate) {
190 $create_template_form->display();
191 echo '<p><a href="'.htmlspecialchars('delete_template.php?id='.$id).'">'.get_string('delete_templates', 'feedback').'</a></p>';
192 }else {
193 echo '&nbsp;';
ab5bd34d 194 }
c70ad9f7 195
84973212 196 if($capabilities->edititems) {
197 echo '<p>
198 <a href="'.htmlspecialchars('export.php?action=exportfile&id='.$id).'">'.get_string('export_questions', 'feedback').'</a>/
199 <a href="'.htmlspecialchars('import.php?id='.$id).'">'.get_string('import_questions', 'feedback').'</a>
200 </p>';
c70ad9f7 201 }
84973212 202 echo $OUTPUT->box_end();
203}
204///////////////////////////////////////////////////////////////////////////
205///print the Item-Edit-section
206///////////////////////////////////////////////////////////////////////////
207if($do_show == 'edit') {
d4b1d58c 208
84973212 209 $add_item_form->display();
c70ad9f7 210
84973212 211 if(is_array($feedbackitems)){
212 $itemnr = 0;
d4b1d58c 213
4bcc5118 214 $helpbutton = $OUTPUT->help_icon('preview', get_string('preview','feedback'), 'feedback',true);
d4b1d58c 215
84973212 216 echo $OUTPUT->heading($helpbutton . get_string('preview', 'feedback'));
217 if(isset($SESSION->feedback->moving) AND $SESSION->feedback->moving->shouldmoving == 1) {
218 echo $OUTPUT->heading('<a href="'.htmlspecialchars($ME.'?id='.$id).'">'.get_string('cancel_moving', 'feedback').'</a>');
c70ad9f7 219 }
84973212 220 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
c70ad9f7 221
84973212 222 //check, if there exists required-elements
223 $countreq = $DB->count_records('feedback_item', array('feedback'=>$feedback->id, 'required'=> 1));
224 if($countreq > 0) {
225 // echo '<font color="red">(*)' . get_string('items_are_required', 'feedback') . '</font>';
226 echo '<span class="feedback_required_mark">(*)' . get_string('items_are_required', 'feedback') . '</span>';
c70ad9f7 227 }
c70ad9f7 228
84973212 229 echo '<table>';
230 if(isset($SESSION->feedback->moving) AND $SESSION->feedback->moving->shouldmoving == 1) {
231 $moveposition = 1;
232 echo '<tr>'; //only shown if shouldmoving = 1
233 echo '<td>';
234 $buttonlink = $ME.'?'.htmlspecialchars(feedback_edit_get_default_query($id, $do_show).'&movehere='.$moveposition);
235 echo '<a title="'.get_string('move_here','feedback').'" href="'.$buttonlink.'">
b5d0cafc 236 <img class="movetarget" alt="'.get_string('move_here','feedback').'" src="'.$OUTPUT->pix_url('movehere') . '" />
84973212 237 </a>';
d4b1d58c 238
84973212 239 // echo '<form action="'.$ME.'" method="post"><fieldset>';
b5d0cafc 240 // echo '<input title="'.get_string('move_here','feedback').'" type="image" src="'.$OUTPUT->pix_url('movehere') . '" hspace="1" height="16" width="80" border="0" />';
84973212 241 // echo '<input type="hidden" name="movehere" value="'.$moveposition.'" />';
242 // feedback_edit_print_default_form_values($id, $do_show);
243 // echo '</fieldset></form>';
244 echo '</td>';
245 echo '</tr>';
246 }
247 //print the inserted items
248 $itempos = 0;
249 foreach($feedbackitems as $feedbackitem){
250 $itempos++;
251 if(isset($SESSION->feedback->moving) AND $SESSION->feedback->moving->movingitem == $feedbackitem->id){ //hiding the item to move
252 continue;
c70ad9f7 253 }
84973212 254 echo '<tr>';
255 //items without value only are labels
256 if($feedbackitem->hasvalue == 1 AND $feedback->autonumbering) {
257 $itemnr++;
258 echo '<td valign="top">' . $itemnr . '.&nbsp;</td>';
259 } else {
260 echo '<td>&nbsp;</td>';
c70ad9f7 261 }
84973212 262 if($feedbackitem->typ != 'pagebreak') {
263 feedback_print_item($feedbackitem, false, false, true);
264 }else {
265 echo '<td class="feedback_pagebreak"><b>'.get_string('pagebreak', 'feedback').'</b></td><td><hr width="100%" size="8px" noshade="noshade" /></td>';
266 }
267 echo '<td>('.get_string('position', 'feedback').':'.$itempos .')</td>';
268 echo '<td>';
269 if($feedbackitem->position > 1){
270 $buttonlink = $ME.'?'.htmlspecialchars(feedback_edit_get_default_query($id, $do_show).'&moveupitem='.$feedbackitem->id);
271 echo '<a class="icon up" title="'.get_string('moveup_item','feedback').'" href="'.$buttonlink.'">
b5d0cafc 272 <img alt="'.get_string('moveup_item','feedback').'" src="'.$OUTPUT->pix_url('t/up') . '" />
84973212 273 </a>';
274 //print the button to move-up the item
275 // echo '<form action="'.$ME.'" method="post"><fieldset>';
b5d0cafc
PS
276 // ///////echo '<input title="'.get_string('moveup_item','feedback').'" type="image" src="'.$OUTPUT->pix_url('t/up') . '" hspace="1" height="11" width="11" border="0" />';
277 // echo '<input class="feedback_moveup_button" title="'.get_string('moveup_item','feedback').'" type="image" src="'.$OUTPUT->pix_url('t/up') . '" />';
84973212 278 // echo '<input type="hidden" name="moveupitem" value="'.$feedbackitem->id.'" />';
279 // feedback_edit_print_default_form_values($id, $do_show);
280 // echo '</fieldset></form>';
281 }else{
282 echo '&nbsp;';
283 }
284 echo '</td>';
285 echo '<td>';
286 if($feedbackitem->position < $lastposition - 1){
287 $buttonlink = $ME.'?'.htmlspecialchars(feedback_edit_get_default_query($id, $do_show).'&movedownitem='.$feedbackitem->id);
288 echo '<a class="icon down" title="'.get_string('movedown_item','feedback').'" href="'.$buttonlink.'">
b5d0cafc 289 <img alt="'.get_string('movedown_item','feedback').'" src="'.$OUTPUT->pix_url('t/down') . '" />
84973212 290 </a>';
291 //print the button to move-down the item
292 // echo '<form action="'.$ME.'" method="post"><fieldset>';
b5d0cafc
PS
293 // echo '<input title="'.get_string('movedown_item','feedback').'" type="image" src="'.$OUTPUT->pix_url('t/down') . '" hspace="1" height="11" width="11" border="0" />';
294 // echo '<input class="feedback_movedown_button" title="'.get_string('movedown_item','feedback').'" type="image" src="'.$OUTPUT->pix_url('t/down') . '" />';
84973212 295 // echo '<input type="hidden" name="movedownitem" value="'.$feedbackitem->id.'" />';
296 // feedback_edit_print_default_form_values($id, $do_show);
297 // echo '</fieldset></form>';
298 }else{
299 echo '&nbsp;';
300 }
301 echo '</td>';
302 echo '<td>';
303 $buttonlink = $ME.'?'.htmlspecialchars(feedback_edit_get_default_query($id, $do_show).'&moveitem='.$feedbackitem->id);
304 echo '<a class="editing_move" title="'.get_string('move_item','feedback').'" href="'.$buttonlink.'">
b5d0cafc 305 <img alt="'.get_string('move_item','feedback').'" src="'.$OUTPUT->pix_url('t/move') . '" />
84973212 306 </a>';
307 // echo '<form action="'.$ME.'" method="post"><fieldset>';
b5d0cafc
PS
308 // echo '<input title="'.get_string('move_item','feedback').'" type="image" src="'.$OUTPUT->pix_url('t/move') . '" hspace="1" height="11" width="11" border="0" />';
309 // echo '<input class="feedback_move_button" title="'.get_string('move_item','feedback').'" type="image" src="'.$OUTPUT->pix_url('t/move') . '" />';
84973212 310 // echo '<input type="hidden" name="moveitem" value="'.$feedbackitem->id.'" />';
311 // feedback_edit_print_default_form_values($id, $do_show);
312 // echo '</fieldset></form>';
313 echo '</td>';
314 echo '<td>';
315 //print the button to edit the item
316 if($feedbackitem->typ != 'pagebreak') {
317 $buttonlink = 'edit_item.php?'.htmlspecialchars(feedback_edit_get_default_query($id, $do_show).'&itemid='.$feedbackitem->id.'&typ='.$feedbackitem->typ);
318 echo '<a class="editing_update" title="'.get_string('edit_item','feedback').'" href="'.$buttonlink.'">
b5d0cafc 319 <img alt="'.get_string('edit_item','feedback').'" src="'.$OUTPUT->pix_url('t/edit') . '" />
84973212 320 </a>';
321 // echo '<form action="edit_item.php" method="post"><fieldset>';
b5d0cafc
PS
322 // echo '<input title="'.get_string('edit_item','feedback').'" type="image" src="'.$OUTPUT->pix_url('t/edit') . '" hspace="1" height="11" width="11" border="0" />';
323 // echo '<input class="feedback_edit_button" title="'.get_string('edit_item','feedback').'" type="image" src="'.$OUTPUT->pix_url('t/edit') . '" />';
84973212 324 // echo '<input type="hidden" name="itemid" value="'.$feedbackitem->id.'" />';
325 // echo '<input type="hidden" name="typ" value="'.$feedbackitem->typ.'" />';
326 // feedback_edit_print_default_form_values($id, $do_show);
327 // echo '</fieldset></form>';
328 }else {
329 echo '&nbsp;';
330 }
331 echo '</td>';
332 echo '<td>';
333
334 //print the toggle-button to switch required yes/no
335 if($feedbackitem->hasvalue == 1) {
336 // echo '<form action="'.$ME.'" method="post"><fieldset>';
337 if($feedbackitem->required == 1) {
338 // 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" />';
339 // echo '<input class="feedback_required_button" title="'.get_string('switch_item_to_not_required','feedback').'" type="image" src="pics/required.gif" />';
340 $buttontitle = get_string('switch_item_to_not_required','feedback');
341 $buttonimg = 'pics/required.gif';
342 } else {
343 // echo '<input title="'.get_string('switch_item_to_required','feedback').'" type="image" src="pics/notrequired.gif" hspace="1" height="11" width="11" border="0" />';
344 // echo '<input class="feedback_required_button" title="'.get_string('switch_item_to_required','feedback').'" type="image" src="pics/notrequired.gif" />';
345 $buttontitle = get_string('switch_item_to_required','feedback');
346 $buttonimg = 'pics/notrequired.gif';
347 }
348 $buttonlink = $ME.'?'.htmlspecialchars(feedback_edit_get_default_query($id, $do_show).'&switchitemrequired='.$feedbackitem->id);
349 echo '<a class="icon feedback_switchrequired" title="'.$buttontitle.'" href="'.$buttonlink.'">
350 <img alt="'.$buttontitle.'" src="'.$buttonimg.'" />
351 </a>';
352 // echo '<input type="hidden" name="switchitemrequired" value="'.$feedbackitem->id.'" />';
353 // feedback_edit_print_default_form_values($id, $do_show);
354 // echo '</fieldset></form>';
355 }else {
356 echo '&nbsp;';
357 }
358 echo '</td>';
359 echo '<td>';
360 $buttonlink = 'delete_item.php?'.htmlspecialchars(feedback_edit_get_default_query($id, $do_show).'&deleteitem='.$feedbackitem->id);
361 echo '<a class="icon delete" title="'.get_string('delete_item','feedback').'" href="'.$buttonlink.'">
b5d0cafc 362 <img alt="'.get_string('delete_item','feedback').'" src="'.$OUTPUT->pix_url('t/delete') . '" />
84973212 363 </a>';
364 //print the button to drop the item
365 // echo '<form action="delete_item.php" method="post"><fieldset>';
b5d0cafc 366 // echo '<input class="feedback_delete_button" title="'.get_string('delete_item','feedback').'" type="image" src="'.$OUTPUT->pix_url('t/delete') . '" />';
84973212 367 // echo '<input type="hidden" name="deleteitem" value="'.$feedbackitem->id.'" />';
368 // feedback_edit_print_default_form_values($id, $do_show);
369 // echo '</fieldset></form>';
370 echo '</td>';
371 echo '</tr>';
c70ad9f7 372 if(isset($SESSION->feedback->moving) AND $SESSION->feedback->moving->shouldmoving == 1) {
84973212 373 $moveposition++;
c70ad9f7 374 echo '<tr>'; //only shown if shouldmoving = 1
375 echo '<td>';
84973212 376 $buttonlink = $ME.'?'.htmlspecialchars(feedback_edit_get_default_query($id, $do_show).'&movehere='.$moveposition);
377 echo '<a title="'.get_string('move_here','feedback').'" href="'.$buttonlink.'">
b5d0cafc 378 <img class="movetarget" alt="'.get_string('move_here','feedback').'" src="'.$OUTPUT->pix_url('movehere') . '" />
84973212 379 </a>';
c70ad9f7 380 // echo '<form action="'.$ME.'" method="post"><fieldset>';
b5d0cafc 381 // echo '<input class="feedback_movehere_button" title="'.get_string('move_here','feedback').'" type="image" src="'.$OUTPUT->pix_url('movehere') . '" />';
c70ad9f7 382 // echo '<input type="hidden" name="movehere" value="'.$moveposition.'" />';
383 // feedback_edit_print_default_form_values($id, $do_show);
384 // echo '</fieldset></form>';
385 echo '</td>';
386 echo '</tr>';
84973212 387 }else {
388 echo '<tr><td>&nbsp;</td></tr>';
c70ad9f7 389 }
d4b1d58c 390
c70ad9f7 391 }
84973212 392 echo '</table>';
393 echo $OUTPUT->box_end();
394 }else{
395 echo $OUTPUT->box(get_string('no_items_available_yet','feedback'),'generalbox boxaligncenter');
c70ad9f7 396 }
84973212 397}
398/// Finish the page
399///////////////////////////////////////////////////////////////////////////
400///////////////////////////////////////////////////////////////////////////
401///////////////////////////////////////////////////////////////////////////
c70ad9f7 402
84973212 403echo $OUTPUT->footer();
c70ad9f7 404
84973212 405function feedback_edit_get_default_query($id, $tab) {
406 global $USER;
d4b1d58c 407
84973212 408 $query = 'id='.$id;
409 $query .= '&do_show='.$tab;
410 //$query .= '&sesskey='.sesskey();
d4b1d58c 411
84973212 412 return $query;
413}
c70ad9f7 414
84973212 415function feedback_edit_print_default_form_values($id, $tab) {
416 global $USER;
d4b1d58c 417
84973212 418 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
419 echo '<input type="hidden" name="id" value="'.$id.'" />';
420 echo '<input type="hidden" name="do_show" value="'.$tab.'" />';
421}