Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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   * Contains event class for displaying the day view.
  19   *
  20   * @package   core_calendar
  21   * @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
  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  require_once($CFG->dirroot . '/calendar/lib.php');
  30  
  31  use core\external\exporter;
  32  use renderer_base;
  33  use moodle_url;
  34  
  35  /**
  36   * Class for displaying the day view.
  37   *
  38   * @package   core_calendar
  39   * @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
  40   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class day_exporter extends exporter {
  43  
  44      /**
  45       * @var \calendar_information $calendar The calendar being displayed.
  46       */
  47      protected $calendar;
  48  
  49      /**
  50       * @var moodle_url
  51       */
  52      protected $url;
  53      /**
  54       * Constructor.
  55       *
  56       * @param \calendar_information $calendar The calendar information for the period being displayed
  57       * @param mixed $data Either an stdClass or an array of values.
  58       * @param array $related Related objects.
  59       */
  60      public function __construct(\calendar_information $calendar, $data, $related) {
  61          $this->calendar = $calendar;
  62  
  63          $url = new moodle_url('/calendar/view.php', [
  64                  'view' => 'day',
  65                  'time' => $calendar->time,
  66              ]);
  67  
  68          if ($this->calendar->course && SITEID !== $this->calendar->course->id) {
  69              $url->param('course', $this->calendar->course->id);
  70          } else if ($this->calendar->categoryid) {
  71              $url->param('category', $this->calendar->categoryid);
  72          }
  73  
  74          $this->url = $url;
  75  
  76          parent::__construct($data, $related);
  77      }
  78  
  79      /**
  80       * Return the list of properties.
  81       *
  82       * @return array
  83       */
  84      protected static function define_properties() {
  85          // These are the default properties as returned by getuserdate()
  86          // but without the formatted month and week names.
  87          return [
  88              'seconds' => [
  89                  'type' => PARAM_INT,
  90              ],
  91              'minutes' => [
  92                  'type' => PARAM_INT,
  93              ],
  94              'hours' => [
  95                  'type' => PARAM_INT,
  96              ],
  97              'mday' => [
  98                  'type' => PARAM_INT,
  99              ],
 100              'wday' => [
 101                  'type' => PARAM_INT,
 102              ],
 103              'year' => [
 104                  'type' => PARAM_INT,
 105              ],
 106              'yday' => [
 107                  'type' => PARAM_INT,
 108              ],
 109          ];
 110      }
 111  
 112      /**
 113       * Return the list of additional properties.
 114       *
 115       * @return array
 116       */
 117      protected static function define_other_properties() {
 118          return [
 119              'timestamp' => [
 120                  'type' => PARAM_INT,
 121              ],
 122              'neweventtimestamp' => [
 123                  'type' => PARAM_INT,
 124              ],
 125              'viewdaylink' => [
 126                  'type' => PARAM_URL,
 127                  'optional' => true,
 128              ],
 129              'viewdaylinktitle' => [
 130                  'type' => PARAM_RAW,
 131                  'optional' => true,
 132              ],
 133              'events' => [
 134                  'type' => calendar_event_exporter::read_properties_definition(),
 135                  'multiple' => true,
 136              ],
 137              'hasevents' => [
 138                  'type' => PARAM_BOOL,
 139                  'default' => false,
 140              ],
 141              'calendareventtypes' => [
 142                  'type' => PARAM_RAW,
 143                  'multiple' => true,
 144              ],
 145              'previousperiod' => [
 146                  'type' => PARAM_INT,
 147              ],
 148              'nextperiod' => [
 149                  'type' => PARAM_INT,
 150              ],
 151              'haslastdayofevent' => [
 152                  'type' => PARAM_BOOL,
 153                  'default' => false,
 154              ],
 155          ];
 156      }
 157  
 158      /**
 159       * Get the additional values to inject while exporting.
 160       *
 161       * @param renderer_base $output The renderer.
 162       * @return array Keys are the property names, values are their values.
 163       */
 164      protected function get_other_values(renderer_base $output) {
 165          $daytimestamp = $this->calendar->time;
 166          $timestamp = $this->data[0];
 167          // Need to account for user's timezone.
 168          $usernow = usergetdate(time());
 169          $today = new \DateTimeImmutable();
 170          // The start time should use the day's date but the current
 171          // time of the day (adjusted for user's timezone).
 172          $neweventstarttime = $today->setTimestamp($timestamp)->setTime(
 173              $usernow['hours'],
 174              $usernow['minutes'],
 175              $usernow['seconds']
 176          );
 177  
 178          $return = [
 179              'timestamp' => $timestamp,
 180              'neweventtimestamp' => $neweventstarttime->getTimestamp(),
 181              'previousperiod' => $this->get_previous_day_timestamp($daytimestamp),
 182              'nextperiod' => $this->get_next_day_timestamp($daytimestamp),
 183              'viewdaylink' => $this->url->out(false),
 184          ];
 185  
 186          if ($viewdaylinktitle = $this->get_view_link_title()) {
 187              $return['viewdaylinktitle'] = $viewdaylinktitle;
 188          }
 189  
 190  
 191          $cache = $this->related['cache'];
 192          $eventexporters = array_map(function($event) use ($cache, $output) {
 193              $context = $cache->get_context($event);
 194              $course = $cache->get_course($event);
 195              $moduleinstance = $cache->get_module_instance($event);
 196              $exporter = new calendar_event_exporter($event, [
 197                  'context' => $context,
 198                  'course' => $course,
 199                  'moduleinstance' => $moduleinstance,
 200                  'daylink' => $this->url,
 201                  'type' => $this->related['type'],
 202                  'today' => $this->data[0],
 203              ]);
 204  
 205              return $exporter;
 206          }, $this->related['events']);
 207  
 208          $return['events'] = array_map(function($exporter) use ($output) {
 209              return $exporter->export($output);
 210          }, $eventexporters);
 211  
 212          $return['hasevents'] = !empty($return['events']);
 213  
 214          $return['calendareventtypes'] = array_map(function($exporter) {
 215              return $exporter->get_calendar_event_type();
 216          }, $eventexporters);
 217          $return['calendareventtypes'] = array_values(array_unique($return['calendareventtypes']));
 218  
 219          $return['haslastdayofevent'] = false;
 220          foreach ($return['events'] as $event) {
 221              if ($event->islastday) {
 222                  $return['haslastdayofevent'] = true;
 223                  break;
 224              }
 225          }
 226  
 227          return $return;
 228      }
 229  
 230      /**
 231       * Returns a list of objects that are related.
 232       *
 233       * @return array
 234       */
 235      protected static function define_related() {
 236          return [
 237              'events' => '\core_calendar\local\event\entities\event_interface[]',
 238              'cache' => '\core_calendar\external\events_related_objects_cache',
 239              'type' => '\core_calendar\type_base',
 240          ];
 241      }
 242  
 243      /**
 244       * Get the previous day timestamp.
 245       *
 246       * @param int $daytimestamp The current day timestamp.
 247       * @return int The previous day timestamp.
 248       */
 249      protected function get_previous_day_timestamp($daytimestamp) {
 250          return $this->related['type']->get_prev_day($daytimestamp);
 251      }
 252  
 253      /**
 254       * Get the next day timestamp.
 255       *
 256       * @param int $daytimestamp The current day timestamp.
 257       * @return int The next day timestamp.
 258       */
 259      protected function get_next_day_timestamp($daytimestamp) {
 260          return $this->related['type']->get_next_day($daytimestamp);
 261      }
 262  
 263      /**
 264       * Get the title for view link.
 265       *
 266       * @return string
 267       */
 268      protected function get_view_link_title() {
 269          $title = null;
 270  
 271          $userdate = userdate($this->data[0], get_string('strftimedayshort'));
 272          if ($this->data['istoday']) {
 273              $title = get_string('todayplustitle', 'calendar', $userdate);
 274          } else if (count($this->related['events'])) {
 275              $title = get_string('eventsfor', 'calendar', $userdate);
 276          }
 277  
 278          return $title;
 279      }
 280  }