MDL-31623 Course: Course reset bypass modules, which are removed
[moodle.git] / calendar / delete.php
blob4f72df71f058ce178bf599583a54f81e7b4748ca
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * This file is part of the Calendar section Moodle
20 * It is responsible for deleting a calendar entry + optionally its repeats
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @package calendar
27 require_once('../config.php');
28 require_once($CFG->dirroot.'/calendar/event_form.php');
29 require_once($CFG->dirroot.'/calendar/lib.php');
30 require_once($CFG->dirroot.'/course/lib.php');
31 require_once($CFG->dirroot.'/calendar/renderer.php');
33 $eventid = required_param('id', PARAM_INT);
34 $confirm = optional_param('confirm', false, PARAM_BOOL);
35 $repeats = optional_param('repeats', false, PARAM_BOOL);
36 $courseid = optional_param('course', 0, PARAM_INT);
38 $PAGE->set_url('/calendar/delete.php', array('id'=>$eventid));
40 if(!$site = get_site()) {
41 redirect(new moodle_url('/admin/index.php'));
44 $event = calendar_event::load($eventid);
46 /**
47 * We are going to be picky here, and require that any event types other than
48 * group and site be associated with a course. This means any code that is using
49 * custom event types (and there are a few) will need to associate thier event with
50 * a course
52 if ($event->eventtype !== 'user' && $event->eventtype !== 'site') {
53 $courseid = $event->courseid;
55 $course = $DB->get_record('course', array('id'=>$courseid));
56 require_login($course);
57 if (!$course) {
58 $PAGE->set_context(context_system::instance()); //TODO: wrong
61 // Check the user has the required capabilities to edit an event
62 if (!calendar_edit_event_allowed($event)) {
63 print_error('nopermissions');
66 // Count the repeats, do we need to consider the possibility of deleting repeats
67 $event->timedurationuntil = $event->timestart + $event->timeduration;
68 $event->count_repeats();
70 // Is used several times, and sometimes with modification if required
71 $viewcalendarurl = new moodle_url(CALENDAR_URL.'view.php', array('view'=>'upcoming'));
72 $viewcalendarurl->param('cal_y', userdate($event->timestart, '%Y'));
73 $viewcalendarurl->param('cal_m', userdate($event->timestart, '%m'));
75 // If confirm is set (PARAM_BOOL) then we have confirmation of initention to delete
76 if ($confirm) {
77 // Confirm the session key to stop CSRF
78 if (!confirm_sesskey()) {
79 print_error('confirmsesskeybad');
81 // Delete the event and possibly repeats
82 $event->delete($repeats);
83 // If the event has an associated course then we need to include it in the redirect link
84 if (!empty($event->courseid) && $event->courseid > 0) {
85 $viewcalendarurl->param('course', $event->courseid);
87 // And redirect
88 redirect($viewcalendarurl);
91 // Prepare the page to show the confirmation form
92 $title = get_string('deleteevent', 'calendar');
93 $strcalendar = get_string('calendar', 'calendar');
95 $PAGE->navbar->add($strcalendar, $viewcalendarurl);
96 $PAGE->navbar->add($title);
97 $PAGE->set_title($site->shortname.': '.$strcalendar.': '.$title);
98 $PAGE->set_heading($COURSE->fullname);
99 echo $OUTPUT->header();
100 echo $OUTPUT->box_start('eventlist');
102 // Delete this event button is always shown
103 $url = new moodle_url(CALENDAR_URL.'delete.php', array('id'=>$event->id, 'confirm'=>true));
104 $buttons = $OUTPUT->single_button($url, get_string('delete'));
106 // If there are repeated events then add a Delete Repeated button
107 $repeatspan = '';
108 if (!empty($event->eventrepeats) && $event->eventrepeats > 0) {
109 $url = new moodle_url(CALENDAR_URL.'delete.php', array('id'=>$event->repeatid, 'confirm'=>true, 'repeats'=>true));
110 $buttons .= $OUTPUT->single_button($url, get_string('deleteall'));
111 $repeatspan = '<br /><br /><span>'.get_string('youcandeleteallrepeats', 'calendar', $event->eventrepeats).'</span>';
114 // And add the cancel button
115 $buttons .= $OUTPUT->single_button($viewcalendarurl, get_string('cancel'));
117 // And show the buttons and notes
118 echo $OUTPUT->box_start('generalbox', 'notice');
119 echo $OUTPUT->box(get_string('confirmeventdelete', 'calendar').$repeatspan);
120 echo $OUTPUT->box($buttons, 'buttons');
121 echo $OUTPUT->box_end();
123 // Print the event so that people can visually confirm they have the correct event
124 $event->time = calendar_format_event_time($event, time(), null, false);
125 $renderer = $PAGE->get_renderer('core_calendar');
126 echo $renderer->start_layout();
127 echo $renderer->event($event, false);
128 echo $renderer->complete_layout();
130 echo $OUTPUT->box_end();
131 echo $OUTPUT->footer();