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.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402]

   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   * Class for exporting calendar footer view options data.
  19   *
  20   * @package    core_calendar
  21   * @copyright  2017 Simey Lameze <simey@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core_calendar\external;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use core\external\exporter;
  29  use renderer_base;
  30  use stdClass;
  31  use moodle_url;
  32  
  33  /**
  34   * Class for exporting calendar footer view options data.
  35   *
  36   * @copyright  2017 Simey Lameze
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class footer_options_exporter extends exporter {
  40  
  41      /**
  42       * @var \calendar_information $calendar The calendar to be rendered.
  43       */
  44      protected $calendar;
  45  
  46      /**
  47       * @var int $userid The user id.
  48       */
  49      protected $userid;
  50  
  51      /**
  52       * @var string $token The user sha1 token.
  53       */
  54      protected $token;
  55  
  56      /**
  57       * @var bool $showfullcalendarlink Whether the full calendar link should be displayed or not.
  58       */
  59      protected $showfullcalendarlink;
  60  
  61      /**
  62       * Constructor for month_exporter.
  63       *
  64       * @param \calendar_information $calendar The calendar being represented
  65       * @param int $userid The user id
  66       * @param string $token The user sha1 token.
  67       * @param array $options Display options for the footer. If an option is not set, a default value will be provided.
  68       *                      It consists of:
  69       *                      - showfullcalendarlink - bool - Whether to show the full calendar link or not. Defaults to false.
  70       */
  71      public function __construct(\calendar_information $calendar, $userid, $token, array $options = []) {
  72          $this->calendar = $calendar;
  73          $this->userid = $userid;
  74          $this->token = $token;
  75          $this->showfullcalendarlink = $options['showfullcalendarlink'] ?? false;
  76      }
  77  
  78      /**
  79       * Get manage subscription link.
  80       *
  81       * @return string|null The manage subscription hyperlink.
  82       */
  83      protected function get_manage_subscriptions_link(): ?string {
  84          if (calendar_user_can_add_event($this->calendar->course)) {
  85              $managesubscriptionurl = new moodle_url('/calendar/managesubscriptions.php');
  86              return $managesubscriptionurl->out(true);
  87          }
  88          return null;
  89      }
  90  
  91      /**
  92       * Get the additional values to inject while exporting.
  93       *
  94       * @param renderer_base $output The renderer.
  95       * @return array Keys are the property names, values are their values.
  96       */
  97      protected function get_other_values(renderer_base $output) {
  98          global $CFG;
  99  
 100          $values = new stdClass();
 101          $values->footerlinks = [];
 102  
 103          if ($this->showfullcalendarlink) {
 104              $values->footerlinks[] = (object)[
 105                  'url' => $this->get_calendar_url(),
 106                  'linkname' => get_string('fullcalendar', 'calendar'),
 107              ];
 108          }
 109  
 110          if (!empty($CFG->enablecalendarexport) && $managesubscriptionlink = $this->get_manage_subscriptions_link()) {
 111              $values->footerlinks[] = (object)[
 112                  'url' => $managesubscriptionlink,
 113                  'linkname' => get_string('managesubscriptions', 'calendar'),
 114              ];
 115          }
 116  
 117          return (array) $values;
 118      }
 119  
 120      /**
 121       * Return the list of additional properties.
 122       *
 123       * @return array
 124       */
 125      public static function define_other_properties() {
 126          return [
 127              'footerlinks' => [
 128                  'type' => [
 129                      'url' => [
 130                          'type' => PARAM_URL,
 131                      ],
 132                      'linkname' => [
 133                          'type' => PARAM_TEXT,
 134                      ],
 135                  ],
 136                  'multiple' => true,
 137                  'optional' => true,
 138              ],
 139          ];
 140      }
 141  
 142      /**
 143       * Build the calendar URL.
 144       *
 145       * @return string The calendar URL.
 146       */
 147      public function get_calendar_url() {
 148          $url = new moodle_url('/calendar/view.php', [
 149              'view' => 'month',
 150              'time' => $this->calendar->time,
 151          ]);
 152  
 153          return $url->out(false);
 154      }
 155  }