Merge branch 'MDL-41619_master' of https://github.com/markn86/moodle
[moodle.git] / mod / feedback / delete_template.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * deletes a template
19  *
20  * @author Andreas Grabs
21  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22  * @package feedback
23  */
25 require_once("../../config.php");
26 require_once("lib.php");
27 require_once('delete_template_form.php');
28 require_once($CFG->libdir.'/tablelib.php');
30 $current_tab = 'templates';
32 $id = required_param('id', PARAM_INT);
33 $canceldelete = optional_param('canceldelete', false, PARAM_INT);
34 $shoulddelete = optional_param('shoulddelete', false, PARAM_INT);
35 $deletetempl = optional_param('deletetempl', false, PARAM_INT);
37 $url = new moodle_url('/mod/feedback/delete_template.php', array('id'=>$id));
38 if ($canceldelete !== false) {
39     $url->param('canceldelete', $canceldelete);
40 }
41 if ($shoulddelete !== false) {
42     $url->param('shoulddelete', $shoulddelete);
43 }
44 if ($deletetempl !== false) {
45     $url->param('deletetempl', $deletetempl);
46 }
47 $PAGE->set_url($url);
49 if (($formdata = data_submitted()) AND !confirm_sesskey()) {
50     print_error('invalidsesskey');
51 }
53 if ($canceldelete == 1) {
54     $editurl = new moodle_url('/mod/feedback/edit.php', array('id'=>$id, 'do_show'=>'templates'));
55     redirect($editurl->out(false));
56 }
58 if (! $cm = get_coursemodule_from_id('feedback', $id)) {
59     print_error('invalidcoursemodule');
60 }
62 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
63     print_error('coursemisconf');
64 }
66 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
67     print_error('invalidcoursemodule');
68 }
70 $context = context_module::instance($cm->id);
72 require_login($course, true, $cm);
74 require_capability('mod/feedback:deletetemplate', $context);
76 $mform = new mod_feedback_delete_template_form();
77 $newformdata = array('id'=>$id,
78                     'deletetempl'=>$deletetempl,
79                     'confirmdelete'=>'1');
81 $mform->set_data($newformdata);
82 $formdata = $mform->get_data();
84 $deleteurl = new moodle_url('/mod/feedback/delete_template.php', array('id'=>$id));
86 if ($mform->is_cancelled()) {
87     redirect($deleteurl->out(false));
88 }
90 if (isset($formdata->confirmdelete) AND $formdata->confirmdelete == 1) {
91     if (!$template = $DB->get_record("feedback_template", array("id"=>$deletetempl))) {
92         print_error('error');
93     }
95     if ($template->ispublic) {
96         $systemcontext = context_system::instance();
97         require_capability('mod/feedback:createpublictemplate', $systemcontext);
98         require_capability('mod/feedback:deletetemplate', $systemcontext);
99     }
101     feedback_delete_template($template);
102     redirect($deleteurl->out(false));
105 /// Print the page header
106 $strfeedbacks = get_string("modulenameplural", "feedback");
107 $strfeedback  = get_string("modulename", "feedback");
108 $strdeletefeedback = get_string('delete_template', 'feedback');
110 $PAGE->set_heading(format_string($course->fullname));
111 $PAGE->set_title(format_string($feedback->name));
112 echo $OUTPUT->header();
114 /// print the tabs
115 require('tabs.php');
117 /// Print the main part of the page
118 ///////////////////////////////////////////////////////////////////////////
119 ///////////////////////////////////////////////////////////////////////////
120 ///////////////////////////////////////////////////////////////////////////
121 echo $OUTPUT->heading($strdeletefeedback);
122 if ($shoulddelete == 1) {
124     echo $OUTPUT->box_start('generalbox errorboxcontent boxaligncenter boxwidthnormal');
125     echo $OUTPUT->heading(get_string('confirmdeletetemplate', 'feedback'));
126     $mform->display();
127     echo $OUTPUT->box_end();
128 } else {
129     //first we get the own templates
130     $templates = feedback_get_template_list($course, 'own');
131     if (!is_array($templates)) {
132         echo $OUTPUT->box(get_string('no_templates_available_yet', 'feedback'),
133                          'generalbox boxaligncenter');
134     } else {
135         echo $OUTPUT->heading(get_string('course'), 3);
136         echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
137         $tablecolumns = array('template', 'action');
138         $tableheaders = array(get_string('template', 'feedback'), '');
139         $tablecourse = new flexible_table('feedback_template_course_table');
141         $tablecourse->define_columns($tablecolumns);
142         $tablecourse->define_headers($tableheaders);
143         $tablecourse->define_baseurl($deleteurl);
144         $tablecourse->column_style('action', 'width', '10%');
146         $tablecourse->sortable(false);
147         $tablecourse->set_attribute('width', '100%');
148         $tablecourse->set_attribute('class', 'generaltable');
149         $tablecourse->setup();
151         foreach ($templates as $template) {
152             $data = array();
153             $data[] = $template->name;
154             $url = new moodle_url($deleteurl, array(
155                                             'id'=>$id,
156                                             'deletetempl'=>$template->id,
157                                             'shoulddelete'=>1,
158                                             ));
160             $data[] = $OUTPUT->single_button($url, $strdeletefeedback, 'post');
161             $tablecourse->add_data($data);
162         }
163         $tablecourse->finish_output();
164         echo $OUTPUT->box_end();
165     }
166     //now we get the public templates if it is permitted
167     $systemcontext = context_system::instance();
168     if (has_capability('mod/feedback:createpublictemplate', $systemcontext) AND
169         has_capability('mod/feedback:deletetemplate', $systemcontext)) {
170         $templates = feedback_get_template_list($course, 'public');
171         if (!is_array($templates)) {
172             echo $OUTPUT->box(get_string('no_templates_available_yet', 'feedback'),
173                               'generalbox boxaligncenter');
174         } else {
175             echo $OUTPUT->heading(get_string('public', 'feedback'), 3);
176             echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthnormal');
177             $tablecolumns = array('template', 'action');
178             $tableheaders = array(get_string('template', 'feedback'), '');
179             $tablepublic = new flexible_table('feedback_template_public_table');
181             $tablepublic->define_columns($tablecolumns);
182             $tablepublic->define_headers($tableheaders);
183             $tablepublic->define_baseurl($deleteurl);
184             $tablepublic->column_style('action', 'width', '10%');
186             $tablepublic->sortable(false);
187             $tablepublic->set_attribute('width', '100%');
188             $tablepublic->set_attribute('class', 'generaltable');
189             $tablepublic->setup();
191             foreach ($templates as $template) {
192                 $data = array();
193                 $data[] = $template->name;
194                 $url = new moodle_url($deleteurl, array(
195                                                 'id'=>$id,
196                                                 'deletetempl'=>$template->id,
197                                                 'shoulddelete'=>1,
198                                                 ));
200                 $data[] = $OUTPUT->single_button($url, $strdeletefeedback, 'post');
201                 $tablepublic->add_data($data);
202             }
203             $tablepublic->finish_output();
204             echo $OUTPUT->box_end();
205         }
206     }
208     echo $OUTPUT->box_start('boxaligncenter boxwidthnormal');
209     $url = new moodle_url($deleteurl, array(
210                                     'id'=>$id,
211                                     'canceldelete'=>1,
212                                     ));
214     echo $OUTPUT->single_button($url, get_string('back'), 'post');
215     echo $OUTPUT->box_end();
218 echo $OUTPUT->footer();