MDL-19488 - allow the creation of a "Public" feedback
[moodle.git] / mod / feedback / delete_template.php
1 <?php
3 /**
4 * deletes a template
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('delete_template_form.php');
14 require_once($CFG->libdir.'/tablelib.php');
16 // $SESSION->feedback->current_tab = 'templates';
17 $current_tab = 'templates';
19 $id = required_param('id', PARAM_INT);
20 $canceldelete = optional_param('canceldelete', false, PARAM_INT);
21 $shoulddelete = optional_param('shoulddelete', false, PARAM_INT);
22 $deletetempl = optional_param('deletetempl', false, PARAM_INT);
23 // $formdata = data_submitted();
25 $url = new moodle_url('/mod/feedback/delete_template.php', array('id'=>$id));
26 if ($canceldelete !== false) {
27     $url->param('canceldelete', $canceldelete);
28 }
29 if ($shoulddelete !== false) {
30     $url->param('shoulddelete', $shoulddelete);
31 }
32 if ($deletetempl !== false) {
33     $url->param('deletetempl', $deletetempl);
34 }
35 $PAGE->set_url($url);
37 if(($formdata = data_submitted()) AND !confirm_sesskey()) {
38     print_error('invalidsesskey');
39 }
41 if($canceldelete == 1){
42     $editurl = new moodle_url('/mod/feedback/edit.php', array('id'=>$id, 'do_show'=>'templates'));
43     redirect($editurl->out(false));
44 }
46 if (! $cm = get_coursemodule_from_id('feedback', $id)) {
47     print_error('invalidcoursemodule');
48 }
50 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
51     print_error('coursemisconf');
52 }
54 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
55     print_error('invalidcoursemodule');
56 }
58 if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
59         print_error('badcontext');
60 }
62 require_login($course->id, true, $cm);
64 require_capability('mod/feedback:deletetemplate', $context);
66 $mform = new mod_feedback_delete_template_form();
67 $newformdata = array('id'=>$id,
68                     'deletetempl'=>$deletetempl,
69                     'confirmdelete'=>'1');
71 $mform->set_data($newformdata);
72 $formdata = $mform->get_data();
74 $deleteurl = new moodle_url('/mod/feedback/delete_template.php', array('id'=>$id));
76 if ($mform->is_cancelled()) {
77     redirect($deleteurl->out(false));
78 }
80 if(isset($formdata->confirmdelete) AND $formdata->confirmdelete == 1){
81     if(!$template = $DB->get_record("feedback_template", array("id"=>$deletetempl))) {
82         print_error('error');
83     }
84     
85     if($template->ispublic) {
86         $systemcontext = get_system_context();
87         require_capability('mod/feedback:createpublictemplate', $systemcontext);
88         require_capability('mod/feedback:deletetemplate', $systemcontext);
89     }
90     
91     feedback_delete_template($template);
92     redirect($deleteurl->out(false));
93 }
95 /// Print the page header
96 $strfeedbacks = get_string("modulenameplural", "feedback");
97 $strfeedback  = get_string("modulename", "feedback");
98 $str_delete_feedback = get_string('delete_template','feedback');
100 $PAGE->set_heading(format_string($course->fullname));
101 $PAGE->set_title(format_string($feedback->name));
102 echo $OUTPUT->header();
104 /// print the tabs
105 include('tabs.php');
107 /// Print the main part of the page
108 ///////////////////////////////////////////////////////////////////////////
109 ///////////////////////////////////////////////////////////////////////////
110 ///////////////////////////////////////////////////////////////////////////
111 echo $OUTPUT->heading($str_delete_feedback);
112 if($shoulddelete == 1) {
114     echo $OUTPUT->box_start('generalbox errorboxcontent boxaligncenter boxwidthnormal');
115     echo $OUTPUT->heading(get_string('confirmdeletetemplate', 'feedback'));
116     $mform->display();
117     echo $OUTPUT->box_end();
118 }else {
119     //first we get the own templates
120     $templates = feedback_get_template_list($course, 'own');
121     if(!is_array($templates)) {
122         echo $OUTPUT->box(get_string('no_templates_available_yet', 'feedback'), 'generalbox boxaligncenter');
123     }else {
124         echo $OUTPUT->heading(get_string('course'), 3);
125         echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
126         $tablecolumns = array('template', 'action');
127         $tableheaders = array(get_string('template', 'feedback'), '');
128         $table_course = new flexible_table('feedback_template_course_table');
130         $table_course->define_columns($tablecolumns);
131         $table_course->define_headers($tableheaders);
132         $table_course->define_baseurl($deleteurl);
133         $table_course->column_style('action', 'width', '10%');
135         $table_course->sortable(false);
136         $table_course->set_attribute('width', '100%');
137         $table_course->set_attribute('class', 'generaltable');
138         $table_course->setup();
140         foreach($templates as $template) {
141             $data = array();
142             $data[] = $template->name;
143             $url = new moodle_url($deleteurl, array(
144                                             'id'=>$id,
145                                             'deletetempl'=>$template->id,
146                                             'shoulddelete'=>1,
147                                             ));
148                                                                                   
149             $data[] = $OUTPUT->single_button($url, $str_delete_feedback, 'post');
150             $table_course->add_data($data);
151         }
152         $table_course->finish_output();
153         echo $OUTPUT->box_end();
154     }
155     //now we get the public templates if it is permitted
156     $systemcontext = get_system_context();
157     if(has_capability('mod/feedback:createpublictemplate', $systemcontext) AND
158         has_capability('mod/feedback:deletetemplate', $systemcontext)) {
159         $templates = feedback_get_template_list($course, 'public');
160         if(!is_array($templates)) {
161             echo $OUTPUT->box(get_string('no_templates_available_yet', 'feedback'), 'generalbox boxaligncenter');
162         }else {
163             echo $OUTPUT->heading(get_string('public', 'feedback'), 3);
164             echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
165             $tablecolumns = $tablecolumns = array('template', 'action');
166             $tableheaders = array(get_string('template', 'feedback'), '');
167             $table_public = new flexible_table('feedback_template_public_table');
169             $table_public->define_columns($tablecolumns);
170             $table_public->define_headers($tableheaders);
171             $table_public->define_baseurl($deleteurl);
172             $table_public->column_style('action', 'width', '10%');
174             $table_public->sortable(false);
175             $table_public->set_attribute('width', '100%');
176             $table_public->set_attribute('class', 'generaltable');
177             $table_public->setup();
179             
180             // echo $OUTPUT->heading(get_string('public', 'feedback'), 3);
181             // echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
182             foreach($templates as $template) {
183                 $data = array();
184                 $data[] = $template->name;
185                 $url = new moodle_url($deleteurl, array(
186                                                 'id'=>$id,
187                                                 'deletetempl'=>$template->id,
188                                                 'shoulddelete'=>1,
189                                                 ));
190                                                                                       
191                 $data[] = $OUTPUT->single_button($url, $str_delete_feedback, 'post');
192                 $table_public->add_data($data);
193             }
194             $table_public->finish_output();
195             echo $OUTPUT->box_end();
196         }
197     }
198     
199     echo $OUTPUT->box_start('boxaligncenter boxwidthnormal');
200     $url = new moodle_url($deleteurl, array(
201                                     'id'=>$id,
202                                     'canceldelete'=>1,
203                                     ));
204                                                                           
205     echo $OUTPUT->single_button($url, get_string('back'), 'post');
206     echo $OUTPUT->box_end();
209 echo $OUTPUT->footer();