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   * Contains event class for displaying the upcoming view.
  19   *
  20   * @package   core_calendar
  21   * @copyright 2017 Simey Lameze <simey@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_calendar\external;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use core\external\exporter;
  30  use renderer_base;
  31  use moodle_url;
  32  use \core_calendar\local\event\container;
  33  
  34  /**
  35   * Class for displaying the day view.
  36   *
  37   * @package   core_calendar
  38   * @copyright 2017 Simey Lameze <simey@moodle.com>
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class calendar_upcoming_exporter extends exporter {
  42      /**
  43       * @var \calendar_information $calendar The calendar to be rendered.
  44       */
  45      protected $calendar;
  46  
  47      /**
  48       * @var moodle_url $url The URL for the upcoming view page.
  49       */
  50      protected $url;
  51  
  52      /**
  53       * Constructor for upcoming exporter.
  54       *
  55       * @param \calendar_information $calendar The calendar being represented.
  56       * @param array $related The related information
  57       */
  58      public function __construct(\calendar_information $calendar, $related) {
  59          $this->calendar = $calendar;
  60  
  61          parent::__construct([], $related);
  62      }
  63  
  64      /**
  65       * Return the list of additional properties.
  66       *
  67       * @return array
  68       */
  69      protected static function define_other_properties() {
  70          return [
  71              'events' => [
  72                  'type' => calendar_event_exporter::read_properties_definition(),
  73                  'multiple' => true,
  74              ],
  75              'defaulteventcontext' => [
  76                  'type' => PARAM_INT,
  77                  'default' => 0,
  78              ],
  79              'filter_selector' => [
  80                  'type' => PARAM_RAW,
  81              ],
  82              'courseid' => [
  83                  'type' => PARAM_INT,
  84              ],
  85              'categoryid' => [
  86                  'type' => PARAM_INT,
  87                  'optional' => true,
  88                  'default' => 0,
  89              ],
  90              'isloggedin' => [
  91                  'type' => PARAM_BOOL,
  92              ],
  93              'date' => [
  94                  'type' => date_exporter::read_properties_definition(),
  95              ],
  96          ];
  97      }
  98  
  99      /**
 100       * Get the additional values to inject while exporting.
 101       *
 102       * @param renderer_base $output The renderer.
 103       * @return array Keys are the property names, values are their values.
 104       */
 105      protected function get_other_values(renderer_base $output) {
 106          $timestamp = $this->calendar->time;
 107  
 108          $cache = $this->related['cache'];
 109          $url = new moodle_url('/calendar/view.php', [
 110              'view' => 'upcoming',
 111              'time' => $timestamp,
 112              'course' => $this->calendar->course->id,
 113          ]);
 114          $this->url = $url;
 115          $return['isloggedin'] = isloggedin();
 116          $return['events'] = array_map(function($event) use ($cache, $output, $url) {
 117              $context = $cache->get_context($event);
 118              $course = $cache->get_course($event);
 119              $moduleinstance = $cache->get_module_instance($event);
 120              $exporter = new calendar_event_exporter($event, [
 121                  'context' => $context,
 122                  'course' => $course,
 123                  'moduleinstance' => $moduleinstance,
 124                  'daylink' => $url,
 125                  'type' => $this->related['type'],
 126                  'today' => $this->calendar->time,
 127              ]);
 128  
 129              $data = $exporter->export($output);
 130  
 131              // We need to override default formatted time because it differs from day view.
 132              // Formatted time for upcoming view adds a link to the day view.
 133              $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
 134              $data->formattedtime = calendar_format_event_time($legacyevent, time(), null);
 135  
 136              return $data;
 137          }, $this->related['events']);
 138  
 139          if ($context = $this->get_default_add_context()) {
 140              $return['defaulteventcontext'] = $context->id;
 141          }
 142          $return['filter_selector'] = $this->get_course_filter_selector($output);
 143          $return['courseid'] = $this->calendar->courseid;
 144          $date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
 145          $return['date'] = (new date_exporter($date))->export($output);
 146          if ($this->calendar->categoryid) {
 147              $return['categoryid'] = $this->calendar->categoryid;
 148          }
 149  
 150          return $return;
 151      }
 152  
 153      /**
 154       * Get the default context for use when adding a new event.
 155       *
 156       * @return null|\context
 157       */
 158      protected function get_default_add_context() {
 159          if (calendar_user_can_add_event($this->calendar->course)) {
 160              return \context_course::instance($this->calendar->course->id);
 161          }
 162  
 163          return null;
 164      }
 165  
 166      /**
 167       * Get the course filter selector.
 168       *
 169       * @param renderer_base $output
 170       * @return string The html code for the course filter selector.
 171       */
 172      protected function get_course_filter_selector(renderer_base $output) {
 173          return $output->course_filter_selector($this->url, '', $this->calendar->course->id);
 174      }
 175  
 176      /**
 177       * Returns a list of objects that are related.
 178       *
 179       * @return array
 180       */
 181      protected static function define_related() {
 182          return [
 183              'events' => '\core_calendar\local\event\entities\event_interface[]',
 184              'cache' => '\core_calendar\external\events_related_objects_cache',
 185              'type' => '\core_calendar\type_base',
 186          ];
 187      }
 188  }