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