Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 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              'navigation' => [
 152                  'type' => PARAM_RAW,
 153              ],
 154              'haslastdayofevent' => [
 155                  'type' => PARAM_BOOL,
 156                  'default' => false,
 157              ],
 158          ];
 159      }
 160  
 161      /**
 162       * Get the additional values to inject while exporting.
 163       *
 164       * @param renderer_base $output The renderer.
 165       * @return array Keys are the property names, values are their values.
 166       */
 167      protected function get_other_values(renderer_base $output) {
 168          $daytimestamp = $this->calendar->time;
 169          $timestamp = $this->data[0];
 170          // Need to account for user's timezone.
 171          $usernow = usergetdate(time());
 172          $today = new \DateTimeImmutable();
 173          // The start time should use the day's date but the current
 174          // time of the day (adjusted for user's timezone).
 175          $neweventstarttime = $today->setTimestamp($timestamp)->setTime(
 176              $usernow['hours'],
 177              $usernow['minutes'],
 178              $usernow['seconds']
 179          );
 180  
 181          $return = [
 182              'timestamp' => $timestamp,
 183              'neweventtimestamp' => $neweventstarttime->getTimestamp(),
 184              'previousperiod' => $this->get_previous_day_timestamp($daytimestamp),
 185              'nextperiod' => $this->get_next_day_timestamp($daytimestamp),
 186              'navigation' => $this->get_navigation(),
 187              'viewdaylink' => $this->url->out(false),
 188          ];
 189  
 190          if ($viewdaylinktitle = $this->get_view_link_title()) {
 191              $return['viewdaylinktitle'] = $viewdaylinktitle;
 192          }
 193  
 194  
 195          $cache = $this->related['cache'];
 196          $eventexporters = array_map(function($event) use ($cache, $output) {
 197              $context = $cache->get_context($event);
 198              $course = $cache->get_course($event);
 199              $moduleinstance = $cache->get_module_instance($event);
 200              $exporter = new calendar_event_exporter($event, [
 201                  'context' => $context,
 202                  'course' => $course,
 203                  'moduleinstance' => $moduleinstance,
 204                  'daylink' => $this->url,
 205                  'type' => $this->related['type'],
 206                  'today' => $this->data[0],
 207              ]);
 208  
 209              return $exporter;
 210          }, $this->related['events']);
 211  
 212          $return['events'] = array_map(function($exporter) use ($output) {
 213              return $exporter->export($output);
 214          }, $eventexporters);
 215  
 216          $return['hasevents'] = !empty($return['events']);
 217  
 218          $return['calendareventtypes'] = array_map(function($exporter) {
 219              return $exporter->get_calendar_event_type();
 220          }, $eventexporters);
 221          $return['calendareventtypes'] = array_values(array_unique($return['calendareventtypes']));
 222  
 223          $return['haslastdayofevent'] = false;
 224          foreach ($return['events'] as $event) {
 225              if ($event->islastday) {
 226                  $return['haslastdayofevent'] = true;
 227                  break;
 228              }
 229          }
 230  
 231          return $return;
 232      }
 233  
 234      /**
 235       * Returns a list of objects that are related.
 236       *
 237       * @return array
 238       */
 239      protected static function define_related() {
 240          return [
 241              'events' => '\core_calendar\local\event\entities\event_interface[]',
 242              'cache' => '\core_calendar\external\events_related_objects_cache',
 243              'type' => '\core_calendar\type_base',
 244          ];
 245      }
 246  
 247      /**
 248       * Get the previous day timestamp.
 249       *
 250       * @param int $daytimestamp The current day timestamp.
 251       * @return int The previous day timestamp.
 252       */
 253      protected function get_previous_day_timestamp($daytimestamp) {
 254          return $this->related['type']->get_prev_day($daytimestamp);
 255      }
 256  
 257      /**
 258       * Get the next day timestamp.
 259       *
 260       * @param int $daytimestamp The current day timestamp.
 261       * @return int The next day timestamp.
 262       */
 263      protected function get_next_day_timestamp($daytimestamp) {
 264          return $this->related['type']->get_next_day($daytimestamp);
 265      }
 266  
 267      /**
 268       * Get the calendar navigation controls.
 269       *
 270       * @return string The html code to the calendar top navigation.
 271       */
 272      protected function get_navigation() {
 273          return calendar_top_controls('day', [
 274              'id' => $this->calendar->courseid,
 275              'time' => $this->calendar->time,
 276          ]);
 277      }
 278  
 279      /**
 280       * Get the title for view link.
 281       *
 282       * @return string
 283       */
 284      protected function get_view_link_title() {
 285          $title = null;
 286  
 287          $userdate = userdate($this->data[0], get_string('strftimedayshort'));
 288          if ($this->data['istoday']) {
 289              $title = get_string('todayplustitle', 'calendar', $userdate);
 290          } else if (count($this->related['events'])) {
 291              $title = get_string('eventsfor', 'calendar', $userdate);
 292          }
 293  
 294          return $title;
 295      }
 296  }