Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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.

Differences Between: [Versions 310 and 402] [Versions 310 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 on month 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 renderer_base;
  30  use moodle_url;
  31  
  32  /**
  33   * Class for displaying the day on month 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_day_exporter extends day_exporter {
  40  
  41      /**
  42       * Constructor.
  43       *
  44       * @param \calendar_information $calendar The calendar information for the period being displayed
  45       * @param mixed $data Either an stdClass or an array of values.
  46       * @param array $related Related objects.
  47       */
  48      public function __construct(\calendar_information $calendar, $data, $related) {
  49          parent::__construct($calendar, $data, $related);
  50          // Fix the url for today to be based on the today timestamp
  51          // rather than the calendar_information time set in the parent
  52          // constructor.
  53          $this->url->param('time', $this->data[0]);
  54      }
  55  
  56      /**
  57       * Return the list of properties.
  58       *
  59       * @return array
  60       */
  61      protected static function define_properties() {
  62          $return = parent::define_properties();
  63          $return = array_merge($return, [
  64              // These are additional params.
  65              'istoday' => [
  66                  'type' => PARAM_BOOL,
  67                  'default' => false,
  68              ],
  69              'isweekend' => [
  70                  'type' => PARAM_BOOL,
  71                  'default' => false,
  72              ],
  73          ]);
  74  
  75          return $return;
  76      }
  77      /**
  78       * Return the list of additional properties.
  79       *
  80       * @return array
  81       */
  82      protected static function define_other_properties() {
  83          $return = parent::define_other_properties();
  84          $return = array_merge($return, [
  85              'popovertitle' => [
  86                  'type' => PARAM_RAW,
  87                  'default' => '',
  88              ],
  89              'daytitle' => [
  90                  'type' => PARAM_RAW,
  91              ]
  92          ]);
  93  
  94          return $return;
  95      }
  96  
  97      /**
  98       * Get the additional values to inject while exporting.
  99       *
 100       * @param renderer_base $output The renderer.
 101       * @return array Keys are the property names, values are their values.
 102       */
 103      protected function get_other_values(renderer_base $output) {
 104          $return = parent::get_other_values($output);
 105  
 106          if ($popovertitle = $this->get_popover_title()) {
 107              $return['popovertitle'] = $popovertitle;
 108          }
 109  
 110          $return['daytitle'] = $this->get_day_title();
 111  
 112          return $return;
 113      }
 114  
 115      /**
 116       * Returns a list of objects that are related.
 117       *
 118       * @return array
 119       */
 120      protected static function define_related() {
 121          return [
 122              'events' => '\core_calendar\local\event\entities\event_interface[]',
 123              'cache' => '\core_calendar\external\events_related_objects_cache',
 124              'type' => '\core_calendar\type_base',
 125          ];
 126      }
 127  
 128      /**
 129       * Get the title for this popover.
 130       *
 131       * @return string
 132       */
 133      protected function get_popover_title() {
 134          $title = null;
 135  
 136          $userdate = userdate($this->data[0], get_string('strftimedayshort'));
 137          if (count($this->related['events'])) {
 138              $title = get_string('eventsfor', 'calendar', $userdate);
 139          } else if ($this->data['istoday']) {
 140              $title = $userdate;
 141          }
 142  
 143          if ($this->data['istoday']) {
 144              $title = get_string('todayplustitle', 'calendar', $userdate);
 145          }
 146  
 147          return $title;
 148      }
 149  
 150      /**
 151       * Get the title for this day.
 152       *
 153       * @return string
 154       */
 155      protected function get_day_title(): string {
 156          $userdate = userdate($this->data[0], get_string('strftimedayshort'));
 157  
 158          $numevents = count($this->related['events']);
 159          if ($numevents == 1) {
 160              $title = get_string('dayeventsone', 'calendar', $userdate);
 161          } else if ($numevents) {
 162              $title = get_string('dayeventsmany', 'calendar', ['num' => $numevents, 'day' => $userdate]);
 163          } else {
 164              $title = get_string('dayeventsnone', 'calendar', $userdate);
 165          }
 166  
 167          return $title;
 168      }
 169  }