Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 402] [Versions 400 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   * Calendar external API for deleting the subscription.
  19   *
  20   * @package core_calendar
  21   * @category external
  22   * @copyright 2021 Huong Nguyen <huongnv13@gmail.com>
  23   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core_calendar\external\subscription;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  require_once($CFG->dirroot . '/calendar/lib.php');
  31  
  32  use external_api;
  33  use external_function_parameters;
  34  use external_single_structure;
  35  use external_value;
  36  use external_warnings;
  37  
  38  /**
  39   * Calendar external API for deleting the subscription.
  40   *
  41   * @package core_calendar
  42   * @category external
  43   * @copyright 2021 Huong Nguyen <huongnv13@gmail.com>
  44   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45   */
  46  class delete extends external_api {
  47  
  48      /**
  49       * Describes the parameters for deleting the subscription.
  50       *
  51       * @return external_function_parameters
  52       * @since Moodle 4.0
  53       */
  54      public static function execute_parameters(): external_function_parameters {
  55          return new external_function_parameters([
  56              'subscriptionid' => new external_value(PARAM_INT, 'The id of the subscription', VALUE_REQUIRED)
  57          ]);
  58      }
  59  
  60      /**
  61       * External function to delete the calendar subscription.
  62       *
  63       * @param int $subscriptionid Subscription id.
  64       * @return array
  65       */
  66      public static function execute(int $subscriptionid): array {
  67          [
  68              'subscriptionid' => $subscriptionid
  69          ] = self::validate_parameters(self::execute_parameters(), [
  70              'subscriptionid' => $subscriptionid
  71          ]);
  72          $status = false;
  73          $warnings = [];
  74          if (calendar_can_edit_subscription($subscriptionid)) {
  75              // Fetch the subscription from the database making sure it exists.
  76              $sub = calendar_get_subscription($subscriptionid);
  77              calendar_delete_subscription($subscriptionid);
  78              $status = true;
  79          } else {
  80              $warnings = [
  81                  'item' => $subscriptionid,
  82                  'warningcode' => 'errordeletingsubscription',
  83                  'message' => get_string('nopermissions', 'error')
  84              ];
  85          }
  86          return [
  87              'status' => $status,
  88              'warnings' => $warnings
  89          ];
  90      }
  91  
  92      /**
  93       * Describes the data returned from the external function.
  94       *
  95       * @return external_single_structure
  96       * @since Moodle 4.0
  97       */
  98      public static function execute_returns(): external_single_structure {
  99          return new external_single_structure([
 100              'status' => new external_value(PARAM_BOOL, 'status: true if success'),
 101              'warnings' => new external_warnings()
 102          ]);
 103      }
 104  }