Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 400 and 403] [Versions 401 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   * This is the external method for exporting a calendar token.
  19   *
  20   * @package    core_calendar
  21   * @since      Moodle 3.10
  22   * @copyright  2020 Juan Leyva <juan@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core_calendar\external\export;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  global $CFG;
  31  require_once($CFG->dirroot . '/calendar/lib.php');
  32  
  33  use context_system;
  34  use core_external\external_api;
  35  use core_external\external_function_parameters;
  36  use core_external\external_single_structure;
  37  use core_external\external_value;
  38  use core_external\external_warnings;
  39  use moodle_exception;
  40  
  41  /**
  42   * This is the external method for exporting a calendar token.
  43   *
  44   * @copyright  2020 Juan Leyva <juan@moodle.com>
  45   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  46   */
  47  class token extends external_api {
  48  
  49      /**
  50       * Returns description of method parameters.
  51       *
  52       * @return external_function_parameters.
  53       * @since  Moodle 3.10
  54       */
  55      public static function execute_parameters() {
  56          return new external_function_parameters([]);
  57      }
  58  
  59      /**
  60       * Return the auth token required for exporting a calendar.
  61       *
  62       * @return array The access information
  63       * @throws moodle_exception
  64       * @since  Moodle 3.10
  65       */
  66      public static function execute() {
  67          global $CFG, $USER;
  68  
  69          $context = context_system::instance();
  70          self::validate_context($context);
  71  
  72          if (empty($CFG->enablecalendarexport)) {
  73              throw new moodle_exception('Calendar export is disabled in this site.');
  74          }
  75  
  76          return [
  77              'token' => calendar_get_export_token($USER),
  78              'warnings' => [],
  79          ];
  80      }
  81  
  82      /**
  83       * Returns description of method result value.
  84       *
  85       * @return \core_external\external_description.
  86       * @since  Moodle 3.10
  87       */
  88      public static function execute_returns() {
  89  
  90          return new external_single_structure(
  91              [
  92                  'token' => new external_value(PARAM_RAW, 'The calendar permanent access token for calendar export.'),
  93                  'warnings' => new external_warnings(),
  94              ]
  95          );
  96      }
  97  }