Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.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 a list of calendar events.
  19   *
  20   * @package   core_calendar
  21   * @copyright 2017 Ryan Wyllie <ryan@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  
  32  /**
  33   * Class for displaying a list of calendar events.
  34   *
  35   * This class uses the events relateds cache in order to get the related
  36   * data for exporting an event without having to naively hit the database
  37   * for each event.
  38   *
  39   * @package   core_calendar
  40   * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
  41   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class events_exporter extends exporter {
  44  
  45      /**
  46       * @var array $events An array of event_interface objects.
  47       */
  48      protected $events;
  49  
  50      /**
  51       * Constructor.
  52       *
  53       * @param array $events An array of event_interface objects
  54       * @param array $related An array of related objects
  55       */
  56      public function __construct(array $events, $related = []) {
  57          $this->events = $events;
  58          parent::__construct([], $related);
  59      }
  60  
  61      /**
  62       * Return the list of additional properties.
  63       *
  64       * @return array
  65       */
  66      protected static function define_other_properties() {
  67          return [
  68              'events' => [
  69                  'type' => event_exporter::read_properties_definition(),
  70                  'multiple' => true,
  71              ],
  72              'firstid' => [
  73                  'type' => PARAM_INT,
  74                  'null' => NULL_ALLOWED,
  75                  'default' => null,
  76              ],
  77              'lastid' => [
  78                  'type' => PARAM_INT,
  79                  'null' => NULL_ALLOWED,
  80                  'default' => null,
  81              ],
  82          ];
  83      }
  84  
  85      /**
  86       * Get the additional values to inject while exporting.
  87       *
  88       * @param renderer_base $output The renderer.
  89       * @return array Keys are the property names, values are their values.
  90       */
  91      protected function get_other_values(renderer_base $output) {
  92          $return = [];
  93          $cache = $this->related['cache'];
  94  
  95          $return['events'] = array_map(function($event) use ($cache, $output) {
  96              $context = $cache->get_context($event);
  97              $course = $cache->get_course($event);
  98              $exporter = new event_exporter($event, ['context' => $context, 'course' => $course]);
  99  
 100              return $exporter->export($output);
 101          }, $this->events);
 102  
 103          if ($count = count($return['events'])) {
 104              $return['firstid'] = $return['events'][0]->id;
 105              $return['lastid'] = $return['events'][$count - 1]->id;
 106          }
 107  
 108          return $return;
 109      }
 110  
 111      /**
 112       * Returns a list of objects that are related.
 113       *
 114       * @return array
 115       */
 116      protected static function define_related() {
 117          return [
 118              'cache' => 'core_calendar\external\events_related_objects_cache',
 119          ];
 120      }
 121  }