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