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  // 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  
  34  $url = new moodle_url('/calendar/managesubscriptions.php');
  35  if ($courseid != SITEID && !empty($courseid)) {
  36      $url->param('course', $courseid);
  37      navigation_node::override_active_url(new moodle_url('/course/view.php', ['id' => $courseid]));
  38      $PAGE->navbar->add(
  39          get_string('calendar', 'calendar'),
  40          new moodle_url('/calendar/view.php', ['view' => 'month', 'course' => $courseid])
  41      );
  42  } else if ($categoryid) {
  43      $url->param('categoryid', $categoryid);
  44      navigation_node::override_active_url(new moodle_url('/course/index.php', ['categoryid' => $categoryid]));
  45      $PAGE->set_category_by_id($categoryid);
  46      $PAGE->navbar->add(
  47          get_string('calendar', 'calendar'),
  48          new moodle_url('/calendar/view.php', ['view' => 'month', 'category' => $categoryid])
  49      );
  50  } else {
  51      $PAGE->navbar->add(get_string('calendar', 'calendar'), new moodle_url('/calendar/view.php', ['view' => 'month']));
  52  }
  53  
  54  $PAGE->set_url($url);
  55  $PAGE->set_pagelayout('admin');
  56  $PAGE->set_secondary_navigation(false);
  57  
  58  if ($courseid != SITEID && !empty($courseid)) {
  59      // Course ID must be valid and existing.
  60      $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
  61      $courses = array($course->id => $course);
  62  } else {
  63      $course = get_site();
  64      $courses = calendar_get_default_courses();
  65  }
  66  require_login($course, false);
  67  
  68  if (!calendar_user_can_add_event($course)) {
  69      throw new \moodle_exception('errorcannotimport', 'calendar');
  70  }
  71  $PAGE->navbar->add(get_string('managesubscriptions', 'calendar'), $PAGE->url);
  72  
  73  $types = calendar_get_allowed_event_types($courseid);
  74  
  75  $searches = [];
  76  $params = [];
  77  
  78  $usedefaultfilters = true;
  79  
  80  if (!empty($types['site'])) {
  81      $searches[] = "(eventtype = 'site')";
  82      $usedefaultfilters = false;
  83  }
  84  
  85  if (!empty($types['user'])) {
  86      $searches[] = "(eventtype = 'user' AND userid = :userid)";
  87      $params['userid'] = $USER->id;
  88      $usedefaultfilters = false;
  89  }
  90  
  91  if (!empty($courseid) && !empty($types['course'])) {
  92      $searches[] = "((eventtype = 'course' OR eventtype = 'group') AND courseid = :courseid)";
  93      $params += ['courseid' => $courseid];
  94      $usedefaultfilters = false;
  95  }
  96  
  97  if (!empty($types['category'])) {
  98      if (!empty($categoryid)) {
  99          $searches[] = "(eventtype = 'category' AND categoryid = :categoryid)";
 100          $params += ['categoryid' => $categoryid];
 101      } else {
 102          $searches[] = "(eventtype = 'category')";
 103      }
 104  
 105      $usedefaultfilters = false;
 106  }
 107  
 108  if ($usedefaultfilters) {
 109      $searches[] = "(eventtype = 'user' AND userid = :userid)";
 110      $params['userid'] = $USER->id;
 111  
 112      if (!empty($types['site'])) {
 113          $searches[] = "(eventtype = 'site' AND courseid  = :siteid)";
 114          $params += ['siteid' => SITEID];
 115      }
 116  
 117      if (!empty($types['course'])) {
 118          $courses = calendar_get_default_courses(null, 'id', true);
 119          if (!empty($courses)) {
 120              $courseids = array_map(function ($c) {
 121                  return $c->id;
 122              }, $courses);
 123  
 124              list($courseinsql, $courseparams) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED, 'course');
 125              $searches[] = "((eventtype = 'course' OR eventtype = 'group') AND courseid {$courseinsql})";
 126              $params += $courseparams;
 127          }
 128      }
 129  
 130      if (!empty($types['category'])) {
 131          list($categoryinsql, $categoryparams) = $DB->get_in_or_equal(
 132                  array_keys(\core_course_category::make_categories_list('moodle/category:manage')), SQL_PARAMS_NAMED, 'category');
 133          $searches[] = "(eventtype = 'category' AND categoryid {$categoryinsql})";
 134          $params += $categoryparams;
 135      }
 136  }
 137  
 138  $sql = "SELECT * FROM {event_subscriptions} WHERE " . implode(' OR ', $searches);;
 139  $subscriptions = $DB->get_records_sql($sql, $params);
 140  
 141  // Print title and header.
 142  $PAGE->set_title("$course->shortname: ".get_string('calendar', 'calendar').": ".get_string('subscriptions', 'calendar'));
 143  $heading = get_string('calendar', 'core_calendar');
 144  $heading = ($courseid != SITEID && !empty($courseid)) ? "{$heading}: {$COURSE->shortname}" : $heading;
 145  $PAGE->set_heading($heading);
 146  
 147  $renderer = $PAGE->get_renderer('core_calendar');
 148  
 149  echo $OUTPUT->header();
 150  echo $renderer->render_subscriptions_header();
 151  
 152  // Filter subscriptions which user can't edit.
 153  foreach($subscriptions as $subscription) {
 154      if (!calendar_can_edit_subscription($subscription)) {
 155          unset($subscriptions[$subscription->id]);
 156      }
 157  }
 158  
 159  // Display a table of subscriptions.
 160  if (empty($subscription)) {
 161      echo $renderer->render_no_calendar_subscriptions();
 162  } else {
 163      echo $renderer->subscription_details($courseid, $subscriptions);
 164  }
 165  echo $OUTPUT->footer();