MDL-19488 - allow the creation of a "Public" feedback
[moodle.git] / mod / feedback / delete_template.php
CommitLineData
84973212 1<?php
2
c70ad9f7 3/**
4* deletes a template
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('delete_template_form.php');
7b1126fd 14require_once($CFG->libdir.'/tablelib.php');
84973212 15
16// $SESSION->feedback->current_tab = 'templates';
17$current_tab = 'templates';
18
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();
24
a6855934 25$url = new moodle_url('/mod/feedback/delete_template.php', array('id'=>$id));
84973212 26if ($canceldelete !== false) {
27 $url->param('canceldelete', $canceldelete);
28}
29if ($shoulddelete !== false) {
30 $url->param('shoulddelete', $shoulddelete);
31}
32if ($deletetempl !== false) {
33 $url->param('deletetempl', $deletetempl);
34}
35$PAGE->set_url($url);
36
37if(($formdata = data_submitted()) AND !confirm_sesskey()) {
38 print_error('invalidsesskey');
39}
40
41if($canceldelete == 1){
5dee2210
AG
42 $editurl = new moodle_url('/mod/feedback/edit.php', array('id'=>$id, 'do_show'=>'templates'));
43 redirect($editurl->out(false));
84973212 44}
45
b6f26001
AG
46if (! $cm = get_coursemodule_from_id('feedback', $id)) {
47 print_error('invalidcoursemodule');
48}
d4b1d58c 49
b6f26001
AG
50if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
51 print_error('coursemisconf');
52}
d4b1d58c 53
b6f26001
AG
54if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
55 print_error('invalidcoursemodule');
84973212 56}
c987dbad
AG
57
58if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
59 print_error('badcontext');
60}
84973212 61
62require_login($course->id, true, $cm);
63
53ba55ec 64require_capability('mod/feedback:deletetemplate', $context);
84973212 65
66$mform = new mod_feedback_delete_template_form();
67$newformdata = array('id'=>$id,
68 'deletetempl'=>$deletetempl,
69 'confirmdelete'=>'1');
70
71$mform->set_data($newformdata);
72$formdata = $mform->get_data();
73
5dee2210
AG
74$deleteurl = new moodle_url('/mod/feedback/delete_template.php', array('id'=>$id));
75
84973212 76if ($mform->is_cancelled()) {
5dee2210 77 redirect($deleteurl->out(false));
84973212 78}
79
80if(isset($formdata->confirmdelete) AND $formdata->confirmdelete == 1){
7b1126fd
AG
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);
5dee2210 92 redirect($deleteurl->out(false));
84973212 93}
94
95/// Print the page header
96$strfeedbacks = get_string("modulenameplural", "feedback");
97$strfeedback = get_string("modulename", "feedback");
7b1126fd 98$str_delete_feedback = get_string('delete_template','feedback');
84973212 99
9d092aa9 100$PAGE->set_heading(format_string($course->fullname));
84973212 101$PAGE->set_title(format_string($feedback->name));
84973212 102echo $OUTPUT->header();
103
104/// print the tabs
105include('tabs.php');
106
107/// Print the main part of the page
108///////////////////////////////////////////////////////////////////////////
109///////////////////////////////////////////////////////////////////////////
110///////////////////////////////////////////////////////////////////////////
7b1126fd 111echo $OUTPUT->heading($str_delete_feedback);
84973212 112if($shoulddelete == 1) {
113
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 {
7b1126fd 119 //first we get the own templates
485ec594 120 $templates = feedback_get_template_list($course, 'own');
84973212 121 if(!is_array($templates)) {
122 echo $OUTPUT->box(get_string('no_templates_available_yet', 'feedback'), 'generalbox boxaligncenter');
c70ad9f7 123 }else {
7b1126fd
AG
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');
129
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%');
134
135 $table_course->sortable(false);
136 $table_course->set_attribute('width', '100%');
137 $table_course->set_attribute('class', 'generaltable');
138 $table_course->setup();
139
84973212 140 foreach($templates as $template) {
7b1126fd
AG
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);
c70ad9f7 151 }
7b1126fd
AG
152 $table_course->finish_output();
153 echo $OUTPUT->box_end();
84973212 154 }
7b1126fd
AG
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');
168
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%');
173
174 $table_public->sortable(false);
175 $table_public->set_attribute('width', '100%');
176 $table_public->set_attribute('class', 'generaltable');
177 $table_public->setup();
178
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();
84973212 207}
c70ad9f7 208
84973212 209echo $OUTPUT->footer();
c70ad9f7 210