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.
   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 day 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_day_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 day view page.
  49       */
  50      protected $url;
  51  
  52      /**
  53       * Constructor for day 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              'neweventtimestamp' => [
  91                  'type' => PARAM_INT,
  92              ],
  93              'date' => [
  94                  'type' => date_exporter::read_properties_definition(),
  95              ],
  96              'periodname' => [
  97                  // Note: We must use RAW here because the calendar type returns the formatted month name based on a
  98                  // calendar format.
  99                  'type' => PARAM_RAW,
 100              ],
 101              'previousperiod' => [
 102                  'type' => date_exporter::read_properties_definition(),
 103              ],
 104              'previousperiodlink' => [
 105                  'type' => PARAM_URL,
 106              ],
 107              'previousperiodname' => [
 108                  // Note: We must use RAW here because the calendar type returns the formatted month name based on a
 109                  // calendar format.
 110                  'type' => PARAM_RAW,
 111              ],
 112              'nextperiod' => [
 113                  'type' => date_exporter::read_properties_definition(),
 114              ],
 115              'nextperiodname' => [
 116                  // Note: We must use RAW here because the calendar type returns the formatted month name based on a
 117                  // calendar format.
 118                  'type' => PARAM_RAW,
 119              ],
 120              'nextperiodlink' => [
 121                  'type' => PARAM_URL,
 122              ],
 123              'larrow' => [
 124                  // The left arrow defined by the theme.
 125                  'type' => PARAM_RAW,
 126              ],
 127              'rarrow' => [
 128                  // The right arrow defined by the theme.
 129                  'type' => PARAM_RAW,
 130              ],
 131          ];
 132      }
 133  
 134      /**
 135       * Get the additional values to inject while exporting.
 136       *
 137       * @param renderer_base $output The renderer.
 138       * @return array Keys are the property names, values are their values.
 139       */
 140      protected function get_other_values(renderer_base $output) {
 141          $timestamp = $this->calendar->time;
 142  
 143          $cache = $this->related['cache'];
 144          $url = new moodle_url('/calendar/view.php', [
 145              'view' => 'day',
 146              'time' => $timestamp,
 147          ]);
 148          if ($this->calendar->course && SITEID !== $this->calendar->course->id) {
 149              $url->param('course', $this->calendar->course->id);
 150          } else if ($this->calendar->categoryid) {
 151              $url->param('category', $this->calendar->categoryid);
 152          }
 153          $this->url = $url;
 154          $return['events'] = array_map(function($event) use ($cache, $output, $url) {
 155              $context = $cache->get_context($event);
 156              $course = $cache->get_course($event);
 157              $moduleinstance = $cache->get_module_instance($event);
 158              $exporter = new calendar_event_exporter($event, [
 159                  'context' => $context,
 160                  'course' => $course,
 161                  'moduleinstance' => $moduleinstance,
 162                  'daylink' => $url,
 163                  'type' => $this->related['type'],
 164                  'today' => $this->calendar->time,
 165              ]);
 166  
 167              $data = $exporter->export($output);
 168  
 169              // We need to override default formatted time because it differs from day view.
 170              // Formatted time for day view adds a link to the day view.
 171              $legacyevent = container::get_event_mapper()->from_event_to_legacy_event($event);
 172              $data->formattedtime = calendar_format_event_time($legacyevent, time(), null);
 173  
 174              return $data;
 175          }, $this->related['events']);
 176  
 177          if ($context = $this->get_default_add_context()) {
 178              $return['defaulteventcontext'] = $context->id;
 179          }
 180  
 181          if ($this->calendar->categoryid) {
 182              $return['categoryid'] = $this->calendar->categoryid;
 183          }
 184  
 185          $return['filter_selector'] = $this->get_course_filter_selector($output);
 186          $return['courseid'] = $this->calendar->courseid;
 187  
 188          $previousperiod = $this->get_previous_day_data();
 189          $nextperiod = $this->get_next_day_data();
 190          $date = $this->related['type']->timestamp_to_date_array($this->calendar->time);
 191  
 192          $nextperiodlink = new moodle_url($this->url);
 193          $nextperiodlink->param('time', $nextperiod[0]);
 194  
 195          $previousperiodlink = new moodle_url($this->url);
 196          $previousperiodlink->param('time', $previousperiod[0]);
 197  
 198          $days = calendar_get_days();
 199          $return['date'] = (new date_exporter($date))->export($output);
 200          $return['periodname'] = userdate($this->calendar->time, get_string('strftimedaydate'));
 201          $return['previousperiod'] = (new date_exporter($previousperiod))->export($output);
 202          $return['previousperiodname'] = $days[$previousperiod['wday']]['fullname'];
 203          $return['previousperiodlink'] = $previousperiodlink->out(false);
 204          $return['nextperiod'] = (new date_exporter($nextperiod))->export($output);
 205          $return['nextperiodname'] = $days[$nextperiod['wday']]['fullname'];
 206          $return['nextperiodlink'] = $nextperiodlink->out(false);
 207          $return['larrow'] = $output->larrow();
 208          $return['rarrow'] = $output->rarrow();
 209  
 210          // Need to account for user's timezone.
 211          $usernow = usergetdate(time());
 212          $today = new \DateTimeImmutable();
 213          $neweventtimestamp = $today->setTimestamp($date[0])->setTime(
 214              $usernow['hours'],
 215              $usernow['minutes'],
 216              $usernow['seconds']
 217          );
 218          $return['neweventtimestamp'] = $neweventtimestamp->getTimestamp();
 219  
 220          return $return;
 221      }
 222  
 223      /**
 224       * Get the default context for use when adding a new event.
 225       *
 226       * @return null|\context
 227       */
 228      protected function get_default_add_context() {
 229          if (calendar_user_can_add_event($this->calendar->course)) {
 230              return \context_course::instance($this->calendar->course->id);
 231          }
 232  
 233          return null;
 234      }
 235  
 236      /**
 237       * Get the course filter selector.
 238       *
 239       * @param renderer_base $output
 240       * @return string The html code for the course filter selector.
 241       */
 242      protected function get_course_filter_selector(renderer_base $output) {
 243          return $output->course_filter_selector($this->url, '', $this->calendar->course->id);
 244      }
 245  
 246      /**
 247       * Returns a list of objects that are related.
 248       *
 249       * @return array
 250       */
 251      protected static function define_related() {
 252          return [
 253              'events' => '\core_calendar\local\event\entities\event_interface[]',
 254              'cache' => '\core_calendar\external\events_related_objects_cache',
 255              'type' => '\core_calendar\type_base',
 256          ];
 257      }
 258  
 259      /**
 260       * Get the previous day timestamp.
 261       *
 262       * @return int The previous day timestamp.
 263       */
 264      protected function get_previous_day_data() {
 265          $type = $this->related['type'];
 266          $time = $type->get_prev_day($this->calendar->time);
 267  
 268          return $type->timestamp_to_date_array($time);
 269      }
 270  
 271      /**
 272       * Get the next day timestamp.
 273       *
 274       * @return int The next day timestamp.
 275       */
 276      protected function get_next_day_data() {
 277          $type = $this->related['type'];
 278          $time = $type->get_next_day($this->calendar->time);
 279  
 280          return $type->timestamp_to_date_array($time);
 281      }
 282  }