Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   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   * Allows the user to manage calendar subscriptions.
  19   *
  20   * @copyright 2012 Jonathan Harker
  21   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   * @package calendar
  23   */
  24  
  25  require_once('../config.php');
  26  require_once($CFG->libdir.'/bennu/bennu.inc.php');
  27  require_once($CFG->dirroot.'/course/lib.php');
  28  require_once($CFG->dirroot.'/calendar/lib.php');
  29  
  30  // Required use.
  31  $courseid = optional_param('course', null, PARAM_INT);
  32  $categoryid = optional_param('category', null, PARAM_INT);
  33  // Used for processing subscription actions.
  34  $subscriptionid = optional_param('id', 0, PARAM_INT);
  35  $pollinterval  = optional_param('pollinterval', 0, PARAM_INT);
  36  $groupcourseid  = optional_param('groupcourseid', 0, PARAM_INT);
  37  $action = optional_param('action', '', PARAM_INT);
  38  
  39  $url = new moodle_url('/calendar/managesubscriptions.php');
  40  if ($courseid != SITEID) {
  41      $url->param('course', $courseid);
  42  }
  43  if ($categoryid) {
  44      $url->param('categoryid', $categoryid);
  45  }
  46  navigation_node::override_active_url(new moodle_url('/calendar/view.php', array('view' => 'month')));
  47  $PAGE->set_url($url);
  48  $PAGE->set_pagelayout('admin');
  49  $PAGE->navbar->add(get_string('managesubscriptions', 'calendar'));
  50  
  51  if ($courseid != SITEID && !empty($courseid)) {
  52      // Course ID must be valid and existing.
  53      $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
  54      $courses = array($course->id => $course);
  55  } else {
  56      $course = get_site();
  57      $courses = calendar_get_default_courses();
  58  }
  59  require_login($course, false);
  60  
  61  if (!calendar_user_can_add_event($course)) {
  62      print_error('errorcannotimport', 'calendar');
  63  }
  64  
  65  // Populate the 'group' select box based on the given 'groupcourseid', if necessary.
  66  $groups = [];
  67  if (!empty($groupcourseid)) {
  68      require_once($CFG->libdir . '/grouplib.php');
  69      $groupcoursedata = groups_get_course_data($groupcourseid);
  70      if (!empty($groupcoursedata->groups)) {
  71          foreach ($groupcoursedata->groups as $groupid => $groupdata) {
  72              $groups[$groupid] = $groupdata->name;
  73          }
  74      }
  75  }
  76  $customdata = [
  77      'courseid' => $course->id,
  78      'groups' => $groups,
  79  ];
  80  $form = new \core_calendar\local\event\forms\managesubscriptions(null, $customdata);
  81  $form->set_data(array(
  82      'course' => $course->id
  83  ));
  84  
  85  $importresults = '';
  86  
  87  $formdata = $form->get_data();
  88  if (!empty($formdata)) {
  89      require_sesskey(); // Must have sesskey for all actions.
  90      $subscriptionid = calendar_add_subscription($formdata);
  91      if ($formdata->importfrom == CALENDAR_IMPORT_FROM_FILE) {
  92          // Blank the URL if it's a file import.
  93          $formdata->url = '';
  94          $calendar = $form->get_file_content('importfile');
  95          $ical = new iCalendar();
  96          $ical->unserialize($calendar);
  97          $importresults = calendar_import_icalendar_events($ical, null, $subscriptionid);
  98      } else {
  99          try {
 100              $importresults = calendar_update_subscription_events($subscriptionid);
 101          } catch (moodle_exception $e) {
 102              // Delete newly added subscription and show invalid url error.
 103              calendar_delete_subscription($subscriptionid);
 104              print_error($e->errorcode, $e->module, $PAGE->url);
 105          }
 106      }
 107      // Redirect to prevent refresh issues.
 108      redirect($PAGE->url, $importresults);
 109  } else if (!empty($subscriptionid)) {
 110      // The user is wanting to perform an action upon an existing subscription.
 111      require_sesskey(); // Must have sesskey for all actions.
 112      if (calendar_can_edit_subscription($subscriptionid)) {
 113          try {
 114              $importresults = calendar_process_subscription_row($subscriptionid, $pollinterval, $action);
 115          } catch (moodle_exception $e) {
 116              // If exception caught, then user should be redirected to page where he/she came from.
 117              print_error($e->errorcode, $e->module, $PAGE->url);
 118          }
 119      } else {
 120          print_error('nopermissions', 'error', $PAGE->url, get_string('managesubscriptions', 'calendar'));
 121      }
 122  }
 123  
 124  $types = calendar_get_allowed_event_types($courseid);
 125  
 126  $searches = [];
 127  $params = [];
 128  
 129  $usedefaultfilters = true;
 130  
 131  if (!empty($types['site'])) {
 132      $searches[] = "(eventtype = 'site')";
 133      $usedefaultfilters = false;
 134  }
 135  
 136  if (!empty($types['user'])) {
 137      $searches[] = "(eventtype = 'user' AND userid = :userid)";
 138      $params['userid'] = $USER->id;
 139      $usedefaultfilters = false;
 140  }
 141  
 142  if (!empty($courseid) && !empty($types['course'])) {
 143      $searches[] = "((eventtype = 'course' OR eventtype = 'group') AND courseid = :courseid)";
 144      $params += ['courseid' => $courseid];
 145      $usedefaultfilters = false;
 146  }
 147  
 148  if (!empty($types['category'])) {
 149      if (!empty($categoryid)) {
 150          $searches[] = "(eventtype = 'category' AND categoryid = :categoryid)";
 151          $params += ['categoryid' => $categoryid];
 152      } else {
 153          $searches[] = "(eventtype = 'category')";
 154      }
 155  
 156      $usedefaultfilters = false;
 157  }
 158  
 159  if ($usedefaultfilters) {
 160      $searches[] = "(eventtype = 'user' AND userid = :userid)";
 161      $params['userid'] = $USER->id;
 162  
 163      if (!empty($types['site'])) {
 164          $searches[] = "(eventtype = 'site' AND courseid  = :siteid)";
 165          $params += ['siteid' => SITEID];
 166      }
 167  
 168      if (!empty($types['course'])) {
 169          $courses = calendar_get_default_courses(null, 'id', true);
 170          if (!empty($courses)) {
 171              $courseids = array_map(function ($c) {
 172                  return $c->id;
 173              }, $courses);
 174  
 175              list($courseinsql, $courseparams) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED, 'course');
 176              $searches[] = "((eventtype = 'course' OR eventtype = 'group') AND courseid {$courseinsql})";
 177              $params += $courseparams;
 178          }
 179      }
 180  
 181      if (!empty($types['category'])) {
 182          list($categoryinsql, $categoryparams) = $DB->get_in_or_equal(
 183                  array_keys(\core_course_category::make_categories_list('moodle/category:manage')), SQL_PARAMS_NAMED, 'category');
 184          $searches[] = "(eventtype = 'category' AND categoryid {$categoryinsql})";
 185          $params += $categoryparams;
 186      }
 187  }
 188  
 189  $sql = "SELECT * FROM {event_subscriptions} WHERE " . implode(' OR ', $searches);;
 190  $subscriptions = $DB->get_records_sql($sql, $params);
 191  
 192  // Print title and header.
 193  $PAGE->set_title("$course->shortname: ".get_string('calendar', 'calendar').": ".get_string('subscriptions', 'calendar'));
 194  $PAGE->set_heading($course->fullname);
 195  
 196  $renderer = $PAGE->get_renderer('core_calendar');
 197  
 198  echo $OUTPUT->header();
 199  
 200  // Filter subscriptions which user can't edit.
 201  foreach($subscriptions as $subscription) {
 202      if (!calendar_can_edit_subscription($subscription)) {
 203          unset($subscriptions[$subscription->id]);
 204      }
 205  }
 206  
 207  // Display a table of subscriptions.
 208  echo $renderer->subscription_details($courseid, $subscriptions, $importresults);
 209  // Display the add subscription form.
 210  $form->display();
 211  echo $OUTPUT->footer();