calendar MDL-20601 Conversion of calendar to mforms and new html editor + file API
[moodle.git] / calendar / delete.php
CommitLineData
76d9df3f
SH
1<?php
2
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.
14//
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/>.
17
18/**
19 * This file is part of the Calendar section Moodle
20 * It is responsible for deleting a calendar entry + optionally its repeats
21 *
22 * @copyright 2009 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @package calendar
25 */
26
27require_once('../config.php');
28require_once($CFG->dirroot.'/calendar/event_form.php');
29require_once($CFG->dirroot.'/calendar/lib.php');
30require_once($CFG->dirroot.'/course/lib.php');
31
32$eventid = required_param('id', PARAM_INT);
33$confirm = optional_param('confirm', false, PARAM_BOOL);
34$repeats = optional_param('repeats', false, PARAM_BOOL);
35$courseid = optional_param('course', 0, PARAM_INT);
36
37$PAGE->set_url(new moodle_url($CFG->wwwroot.'/calendar/delete.php', array('id'=>$eventid)));
38
39if(!$site = get_site()) {
40 redirect(new moodle_url($CFG->wwwroot.'/'.$CFG->admin.'/index.php'));
41}
42
43$event = calendar_event::load($eventid);
44
45/**
46 * We are going to be picky here, and require that any event types other than
47 * group and site be associated with a course. This means any code that is using
48 * custom event types (and there are a few) will need to associate thier event with
49 * a course
50 */
51if ($event->eventtype !== 'user' && $event->eventtype !== 'site') {
52 if ($courseid !== $event->courseid) {
53 print_error('invalidcourse');
54 }
55 require_login($event->courseid);
56} else {
57 require_login();
58}
59
60// Check the user has the required capabilities to edit an event
61if (!calendar_edit_event_allowed($event)) {
62 print_error('nopermissions');
63}
64
65// Count the repeats, do we need to consider the possibility of deleting repeats
66$event->timedurationuntil = $event->timestart + $event->timeduration;
67$event->count_repeats();
68
69// Is used several times, and sometimes with modification if required
70$viewcalendarurl = new moodle_url(CALENDAR_URL.'view.php', array('view'=>'upcoming'));
71$viewcalendarurl->param('cal_y', date('Y', $event->timestart));
72$viewcalendarurl->param('cal_m', date('m', $event->timestart));
73
74// If confirm is set (PARAM_BOOL) then we have confirmation of initention to delete
75if ($confirm) {
76 // Confirm the session key to stop CSRF
77 if (!confirm_sesskey()) {
78 print_error('confirmsesskeybad');
79 }
80 // Delete the event and possibly repeats
81 $event->delete($repeats);
82 // If the event has an associated course then we need to include it in the redirect link
83 if (!empty($event->courseid) && $event->courseid > 0) {
84 $viewcalendarurl->param('course', $event->courseid);
85 }
86 // And redirect
87 redirect($viewcalendarurl);
88}
89
90// Prepare the page to show the confirmation form
91$title = get_string('deleteevent', 'calendar');
92$strcalendar = get_string('calendar', 'calendar');
93
94$PAGE->navbar->add($strcalendar, $viewcalendarurl);
95$PAGE->navbar->add($title);
96$PAGE->set_title($site->shortname.': '.$strcalendar.': '.$title);
97$PAGE->set_heading($strcalendar);
98$PAGE->set_headingmenu(user_login_string($site));
99
100echo $OUTPUT->header();
101echo $OUTPUT->box_start('eventlist');
102
103// Delete this event button is always shown
104$deleteone = new html_form();
105$deleteone->button->text = get_string('delete');
106$deleteone->url = new moodle_url(CALENDAR_URL.'delete.php', array('id'=>$event->id, 'confirm'=>true));
107$buttons = $OUTPUT->button($deleteone);
108
109// If there are repeated events then add a Delete Repeated button
110$repeatspan = '';
111if (!empty($event->eventrepeats) && $event->eventrepeats > 0) {
112 $deleteall = new html_form();
113 $deleteall->button->text = get_string('deleteall');
114 $deleteall->url = new moodle_url(CALENDAR_URL.'delete.php', array('id'=>$event->repeatid, 'confirm'=>true, 'repeats'=>true));
115 $buttons .= $OUTPUT->button($deleteall);
116 $repeatspan = '<br /><br />'.$OUTPUT->span(get_string('youcandeleteallrepeats', 'calendar'));
117}
118
119// And add the cancel button
120$cancel = new html_form();
121$cancel->button->text = get_string('cancel');
122$cancel->url = $viewcalendarurl;
123$buttons .= $OUTPUT->button($cancel);
124
125// And show the buttons and notes
126echo $OUTPUT->box_start('generalbox', 'notice');
127echo $OUTPUT->box(get_string('confirmeventdelete', 'calendar').$repeatspan);
128echo $OUTPUT->box($buttons, 'buttons');
129echo $OUTPUT->box_end();
130
131// Print the event so that people can visually confirm they have the correct event
132$event->time = calendar_format_event_time($event, time(), '', false);
133calendar_print_event($event, false);
134
135echo $OUTPUT->box_end();
136echo $OUTPUT->footer();