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.
   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 week 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  use core\external\exporter;
  30  use renderer_base;
  31  
  32  /**
  33   * Class for displaying the week view.
  34   *
  35   * @package   core_calendar
  36   * @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
  37   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class week_exporter extends exporter {
  40  
  41      /**
  42       * @var array $days An array of day_exporter objects.
  43       */
  44      protected $days = [];
  45  
  46      /**
  47       * @var int $prepadding The number of pre-padding days at the start of the week.
  48       */
  49      protected $prepadding = 0;
  50  
  51      /**
  52       * @var int $postpadding The number of post-padding days at the start of the week.
  53       */
  54      protected $postpadding = 0;
  55  
  56      /**
  57       * @var \calendar_information $calendar The calendar being displayed.
  58       */
  59      protected $calendar;
  60  
  61      /**
  62       * Constructor.
  63       *
  64       * @param \calendar_information $calendar The calendar information for the period being displayed
  65       * @param mixed $days An array of day_exporter objects.
  66       * @param int $prepadding The number of pre-padding days at the start of the week.
  67       * @param int $postpadding The number of post-padding days at the start of the week.
  68       * @param array $related Related objects.
  69       */
  70      public function __construct(\calendar_information $calendar, $days, $prepadding, $postpadding, $related) {
  71          $this->days = $days;
  72          $this->prepadding = $prepadding;
  73          $this->postpadding = $postpadding;
  74          $this->calendar = $calendar;
  75  
  76          parent::__construct([], $related);
  77      }
  78  
  79      /**
  80       * Return the list of additional properties.
  81       *
  82       * @return array
  83       */
  84      protected static function define_other_properties() {
  85          return [
  86              'prepadding' => [
  87                  'type' => PARAM_INT,
  88                  'multiple' => true,
  89              ],
  90              'postpadding' => [
  91                  'type' => PARAM_INT,
  92                  'multiple' => true,
  93              ],
  94              'days' => [
  95                  'type' => week_day_exporter::read_properties_definition(),
  96                  'multiple' => true,
  97              ],
  98          ];
  99      }
 100  
 101      /**
 102       * Get the additional values to inject while exporting.
 103       *
 104       * @param renderer_base $output The renderer.
 105       * @return array Keys are the property names, values are their values.
 106       */
 107      protected function get_other_values(renderer_base $output) {
 108          global $CFG;
 109          $return = [
 110              'prepadding' => [],
 111              'postpadding' => [],
 112              'days' => [],
 113          ];
 114  
 115          for ($i = 0; $i < $this->prepadding; $i++) {
 116              $return['prepadding'][] = $i;
 117          }
 118          for ($i = 0; $i < $this->postpadding; $i++) {
 119              $return['postpadding'][] = $i;
 120          }
 121  
 122          $return['days'] = [];
 123          $today = $this->related['type']->timestamp_to_date_array(time());
 124  
 125          $weekend = CALENDAR_DEFAULT_WEEKEND;
 126          if (isset($CFG->calendar_weekend)) {
 127              $weekend = intval($CFG->calendar_weekend);
 128          }
 129          $numberofdaysinweek = $this->related['type']->get_num_weekdays();
 130  
 131          foreach ($this->days as $daydata) {
 132              $events = [];
 133              foreach ($this->related['events'] as $event) {
 134                  $times = $event->get_times();
 135                  $starttime = $times->get_start_time()->getTimestamp();
 136                  $startdate = $this->related['type']->timestamp_to_date_array($starttime);
 137                  $endtime = $times->get_end_time()->getTimestamp();
 138                  $enddate = $this->related['type']->timestamp_to_date_array($endtime);
 139  
 140                  if ((($startdate['year'] * 366) + $startdate['yday']) > ($daydata['year'] * 366) + $daydata['yday']) {
 141                      // Starts after today.
 142                      continue;
 143                  }
 144                  if ((($enddate['year'] * 366) + $enddate['yday']) < ($daydata['year'] * 366) + $daydata['yday']) {
 145                      // Ends before today.
 146                      continue;
 147                  }
 148                  $events[] = $event;
 149              }
 150  
 151              $istoday = true;
 152              $istoday = $istoday && $today['year'] == $daydata['year'];
 153              $istoday = $istoday && $today['yday'] == $daydata['yday'];
 154              $daydata['istoday'] = $istoday;
 155  
 156              $daydata['isweekend'] = !!($weekend & (1 << ($daydata['wday'] % $numberofdaysinweek)));
 157  
 158              $day = new week_day_exporter($this->calendar, $daydata, [
 159                  'events' => $events,
 160                  'cache' => $this->related['cache'],
 161                  'type' => $this->related['type'],
 162              ]);
 163  
 164              $return['days'][] = $day->export($output);
 165          }
 166  
 167          return $return;
 168      }
 169  
 170      /**
 171       * Returns a list of objects that are related.
 172       *
 173       * @return array
 174       */
 175      protected static function define_related() {
 176          return [
 177              'events' => '\core_calendar\local\event\entities\event_interface[]',
 178              'cache' => '\core_calendar\external\events_related_objects_cache',
 179              'type' => '\core_calendar\type_base',
 180          ];
 181      }
 182  }