Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   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  
  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');
  32  
  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);
  37  
  38  $PAGE->set_url('/calendar/delete.php', array('id'=>$eventid));
  39  
  40  if(!$site = get_site()) {
  41      redirect(new moodle_url('/admin/index.php'));
  42  }
  43  
  44  $event = calendar_event::load($eventid);
  45  
  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
  51   */
  52  if ($event->eventtype !== 'user' && $event->eventtype !== 'site') {
  53      $courseid = $event->courseid;
  54  }
  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
  59  }
  60  $title = get_string('deleteevent', 'calendar');
  61  // Check the user has the required capabilities to delete an event
  62  if (!calendar_delete_event_allowed($event)) {
  63      throw new \moodle_exception('nopermissions', 'error', $PAGE->url, $title);
  64  }
  65  
  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();
  69  
  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('time', $event->timestart, '%Y');
  73  
  74  // If confirm is set (PARAM_BOOL) then we have confirmation of initention to delete
  75  if ($confirm) {
  76      // Confirm the session key to stop CSRF
  77      if (!confirm_sesskey()) {
  78          throw new \moodle_exception('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  $strcalendar = get_string('calendar', 'calendar');
  92  
  93  $PAGE->navbar->add($strcalendar, $viewcalendarurl);
  94  $PAGE->navbar->add($title);
  95  $PAGE->set_title($strcalendar.': '.$title);
  96  $PAGE->set_heading($COURSE->fullname);
  97  if ($course) {
  98      $PAGE->set_secondary_navigation(false);
  99  }
 100  echo $OUTPUT->header();
 101  echo $OUTPUT->box_start('eventlist');
 102  
 103  // Delete this event button is always shown
 104  $url = new moodle_url(CALENDAR_URL.'delete.php', array('id'=>$event->id, 'confirm'=>true));
 105  $buttons = $OUTPUT->single_button($url, get_string('delete'));
 106  
 107  // And add the cancel button.
 108  $buttons .= $OUTPUT->single_button($viewcalendarurl, get_string('cancel'));
 109  
 110  // And show the buttons and notes.
 111  echo $OUTPUT->box_start('generalbox', 'notice');
 112  echo html_writer::tag('p', get_string('confirmeventdelete', 'calendar', format_string($event->name)));
 113  
 114  // If there are repeated events then add a Delete Repeated button.
 115  if (!empty($event->eventrepeats) && $event->eventrepeats > 0) {
 116      $url = new moodle_url(CALENDAR_URL.'delete.php', array('id'=>$event->repeatid, 'confirm'=>true, 'repeats'=>true));
 117      $buttons .= $OUTPUT->single_button($url, get_string('deleteall'));
 118      echo html_writer::tag('p', get_string('youcandeleteallrepeats', 'calendar', $event->eventrepeats));
 119  }
 120  
 121  echo $OUTPUT->box($buttons, 'buttons');
 122  echo $OUTPUT->box_end();
 123  
 124  echo $OUTPUT->box_end();
 125  echo $OUTPUT->footer();