Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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 calendar event's action.
  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 core_calendar\local\event\entities\action_interface;
  31  use core_calendar\local\event\container;
  32  use renderer_base;
  33  
  34  /**
  35   * Class for displaying a calendar event's action.
  36   *
  37   * @package   core_calendar
  38   * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class event_action_exporter extends exporter {
  42  
  43      /**
  44       * Constructor.
  45       *
  46       * @param action_interface $action The action object.
  47       * @param array $related Related data.
  48       */
  49      public function __construct(action_interface $action, $related = []) {
  50          $data = new \stdClass();
  51          $data->name = $action->get_name();
  52          $data->url = $action->get_url()->out(false);
  53          $data->itemcount = $action->get_item_count();
  54          $data->actionable = $action->is_actionable();
  55  
  56          parent::__construct($data, $related);
  57      }
  58  
  59      /**
  60       * Return the list of properties.
  61       *
  62       * @return array
  63       */
  64      protected static function define_properties() {
  65          return [
  66              'name' => ['type' => PARAM_TEXT],
  67              'url' => ['type' => PARAM_URL],
  68              'itemcount' => ['type' => PARAM_INT],
  69              'actionable' => ['type' => PARAM_BOOL]
  70          ];
  71      }
  72  
  73      /**
  74       * Return the list of additional properties.
  75       *
  76       * @return array
  77       */
  78      protected static function define_other_properties() {
  79          return [
  80              'showitemcount' => ['type' => PARAM_BOOL, 'default' => false]
  81          ];
  82      }
  83  
  84      /**
  85       * Get the additional values to inject while exporting.
  86       *
  87       * @param renderer_base $output The renderer.
  88       * @return array Keys are the property names, values are their values.
  89       */
  90      protected function get_other_values(renderer_base $output) {
  91          $event = $this->related['event'];
  92  
  93          if (!$event->get_component()) {
  94              return ['showitemcount' => false];
  95          }
  96          $showitemcountcallback = 'core_calendar_event_action_shows_item_count';
  97          $mapper = container::get_event_mapper();
  98          $calevent = $mapper->from_event_to_legacy_event($event);
  99          $params = [$calevent, $this->data->itemcount];
 100          $showitemcount = component_callback($event->get_component(), $showitemcountcallback, $params, false);
 101  
 102          // Prepare other values data.
 103          $data = [
 104              'showitemcount' => $showitemcount
 105          ];
 106          return $data;
 107      }
 108  
 109      /**
 110       * Returns a list of objects that are related.
 111       *
 112       * @return array
 113       */
 114      protected static function define_related() {
 115          return [
 116              'context' => 'context',
 117              'event' => '\\core_calendar\\local\\event\\entities\\event_interface'
 118          ];
 119      }
 120  
 121      /**
 122       * Magic method returning parameters for formatting 'name' property
 123       *
 124       * @return bool[]
 125       */
 126      protected function get_format_parameters_for_name() {
 127          return ['escape' => false];
 128      }
 129  }