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.

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