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 grouped by course id.
  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 grouped by course id.
  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_grouped_by_course_exporter extends exporter {
  44  
  45      /**
  46       * @var array $events An array of event_interface objects
  47       *                    grouped and index by course id.
  48       */
  49      protected $eventsbycourse;
  50  
  51      /**
  52       * Constructor.
  53       *
  54       * @param array $eventsbycourse An array of event_interface objects
  55       * @param array $related An array of related objects
  56       */
  57      public function __construct(array $eventsbycourse, $related = []) {
  58          $this->eventsbycourse = $eventsbycourse;
  59          parent::__construct([], $related);
  60      }
  61  
  62      /**
  63       * Return the list of additional properties.
  64       *
  65       * @return array
  66       */
  67      protected static function define_other_properties() {
  68          return [
  69              'groupedbycourse' => [
  70                  'type' => events_same_course_exporter::read_properties_definition(),
  71                  'multiple' => true,
  72                  'default' => [],
  73              ],
  74          ];
  75      }
  76  
  77      /**
  78       * Get the additional values to inject while exporting.
  79       *
  80       * @param renderer_base $output The renderer.
  81       * @return array Keys are the property names, values are their values.
  82       */
  83      protected function get_other_values(renderer_base $output) {
  84          $return = [];
  85          $cache = $this->related['cache'];
  86  
  87          foreach ($this->eventsbycourse as $courseid => $events) {
  88              $eventsexporter = new events_same_course_exporter(
  89                  $courseid, $events, ['cache' => $cache]);
  90              $return['groupedbycourse'][] = $eventsexporter->export($output);
  91          }
  92  
  93          return $return;
  94      }
  95  
  96      /**
  97       * Returns a list of objects that are related.
  98       *
  99       * @return array
 100       */
 101      protected static function define_related() {
 102          return [
 103              'cache' => 'core_calendar\external\events_related_objects_cache',
 104          ];
 105      }
 106  }