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.
   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/>.
  16  
  17  /**
  18   * Moodle calendar import
  19   *
  20   * @package    core_calendar
  21   * @copyright  Moodle Pty Ltd
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  require_once('../config.php');
  25  require_once($CFG->libdir . '/bennu/bennu.inc.php');
  26  require_once($CFG->dirroot . '/course/lib.php');
  27  require_once($CFG->dirroot . '/calendar/lib.php');
  28  
  29  $courseid = optional_param('course', SITEID, PARAM_INT);
  30  $groupcourseid  = optional_param('groupcourseid', 0, PARAM_INT);
  31  $category  = optional_param('category', 0, PARAM_INT);
  32  $data = [];
  33  $pageurl = new moodle_url('/calendar/import.php');
  34  $managesubscriptionsurl = new moodle_url('/calendar/managesubscriptions.php');
  35  
  36  $headingstr = $calendarstr = get_string('calendar', 'calendar');
  37  
  38  if (!empty($courseid) && $courseid != SITEID) {
  39      $course = get_course($courseid);
  40      $data['eventtype'] = 'course';
  41      $data['courseid'] = $course->id;
  42      $pageurl->param('course', $course->id);
  43      $managesubscriptionsurl->param('course', $course->id);
  44      $headingstr .= ": {$course->shortname}";
  45      navigation_node::override_active_url(new moodle_url('/course/view.php', ['id' => $course->id]));
  46      $PAGE->navbar->add(
  47          $calendarstr,
  48          new moodle_url('/calendar/view.php', ['view' => 'month', 'course' => $course->id])
  49      );
  50  } else if (!empty($category)) {
  51      $course = get_site();
  52      $pageurl->param('category', $category);
  53      $managesubscriptionsurl->param('category', $category);
  54      $data['category'] = $category;
  55      $data['eventtype'] = 'category';
  56      navigation_node::override_active_url(new moodle_url('/course/index.php', ['categoryid' => $category]));
  57      $PAGE->set_category_by_id($category);
  58      $PAGE->navbar->add(
  59          $calendarstr,
  60          new moodle_url('/calendar/view.php', ['view' => 'month', 'category' => $category])
  61      );
  62  } else {
  63      $course = get_site();
  64      $PAGE->navbar->add($calendarstr, new moodle_url('/calendar/view.php', ['view' => 'month']));
  65  }
  66  
  67  if (!empty($groupcourseid)) {
  68      $pageurl->param('groupcourseid', $groupcourseid);
  69  }
  70  
  71  require_login($course, false);
  72  if (!calendar_user_can_add_event($course)) {
  73      throw new \moodle_exception('errorcannotimport', 'calendar');
  74  }
  75  
  76  $heading = get_string('importcalendar', 'calendar');
  77  $pagetitle = $course->shortname . ': ' . $calendarstr . ': ' . $heading;
  78  
  79  $PAGE->set_secondary_navigation(false);
  80  $PAGE->set_title($pagetitle);
  81  $PAGE->set_heading($headingstr);
  82  $PAGE->set_url($pageurl);
  83  $PAGE->set_pagelayout('admin');
  84  $PAGE->navbar->add(get_string('managesubscriptions', 'calendar'), $managesubscriptionsurl);
  85  $PAGE->navbar->add($heading, $pageurl);
  86  
  87  // Populate the 'group' select box based on the given 'groupcourseid', if necessary.
  88  $groups = [];
  89  if (!empty($groupcourseid)) {
  90      require_once($CFG->libdir . '/grouplib.php');
  91      $groupcoursedata = groups_get_course_data($groupcourseid);
  92      if (!empty($groupcoursedata->groups)) {
  93          foreach ($groupcoursedata->groups as $groupid => $groupdata) {
  94              $groups[$groupid] = $groupdata->name;
  95          }
  96      }
  97      $data['groupcourseid'] = $groupcourseid;
  98      $data['eventtype'] = 'group';
  99  }
 100  if (!empty($category)) {
 101      $managesubscriptionsurl->param('category', $category);
 102      $data['category'] = $category;
 103      $data['eventtype'] = 'category';
 104  }
 105  
 106  $renderer = $PAGE->get_renderer('core_calendar');
 107  
 108  $customdata = [
 109      'courseid' => $course->id,
 110      'groups' => $groups,
 111  ];
 112  
 113  $form = new \core_calendar\local\event\forms\managesubscriptions(null, $customdata);
 114  $form->set_data($data);
 115  
 116  $formdata = $form->get_data();
 117  if (!empty($formdata)) {
 118      require_sesskey();
 119      $subscriptionid = calendar_add_subscription($formdata);
 120      if ($formdata->importfrom == CALENDAR_IMPORT_FROM_FILE) {
 121          // Blank the URL if it's a file import.
 122          $formdata->url = '';
 123          $calendar = $form->get_file_content('importfile');
 124          $ical = new iCalendar();
 125          $ical->unserialize($calendar);
 126          $importresults = calendar_import_events_from_ical($ical, $subscriptionid);
 127      } else {
 128          try {
 129              $importresults = calendar_update_subscription_events($subscriptionid);
 130          } catch (\moodle_exception $e) {
 131              // Delete newly added subscription and show invalid url error.
 132              calendar_delete_subscription($subscriptionid);
 133              throw new \moodle_exception($e->errorcode, $e->module, $PAGE->url);
 134          }
 135      }
 136      if (!empty($formdata->courseid)) {
 137          $managesubscriptionsurl->param('course', $formdata->courseid);
 138      }
 139      if (!empty($formdata->categoryid)) {
 140          $managesubscriptionsurl->param('category', $formdata->categoryid);
 141      }
 142      redirect($managesubscriptionsurl, $renderer->render_import_result($importresults));
 143  }
 144  
 145  echo $OUTPUT->header();
 146  echo $renderer->start_layout();
 147  echo $OUTPUT->heading($heading);
 148  $form->display();
 149  echo $renderer->complete_layout();
 150  echo $OUTPUT->footer();