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  namespace tiny_autosave\external;
  18  
  19  use external_api;
  20  use external_function_parameters;
  21  use external_single_structure;
  22  use external_value;
  23  
  24  defined('MOODLE_INTERNAL') || die();
  25  
  26  require_once("{$CFG->libdir}/externallib.php");
  27  
  28  /**
  29   * Web Service to reset the autosave session.
  30   *
  31   * @package   tiny_autosave
  32   * @category  external
  33   * @copyright 2022 Andrew Lyons <andrew@nicols.co.uk>
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class reset_autosave_session extends external_api {
  37      /**
  38       * Returns description of method parameters
  39       *
  40       * @return external_function_parameters
  41       */
  42      public static function execute_parameters(): external_function_parameters {
  43          return new external_function_parameters([
  44              'contextid' => new external_value(PARAM_INT, 'The context id that owns the editor', VALUE_REQUIRED),
  45              'pagehash' => new external_value(PARAM_ALPHANUMEXT, 'The page hash', VALUE_REQUIRED),
  46              'pageinstance' => new external_value(PARAM_ALPHANUMEXT, 'The page instance', VALUE_REQUIRED),
  47              'elementid' => new external_value(PARAM_RAW, 'The ID of the element', VALUE_REQUIRED),
  48          ]);
  49      }
  50  
  51      /**
  52       * Reset the autosave entry for this autosave instance.
  53       *
  54       * If not matching autosave area could be found, the function will
  55       * silently return and this is not treated as an error condition.
  56       *
  57       * @param int $contextid The context id of the owner
  58       * @param string $pagehash The hash of the page
  59       * @param string $pageinstance The instance id of the page
  60       * @param string $elementid The id of the element
  61       * @return null
  62       */
  63      public static function execute(
  64          int $contextid,
  65          string $pagehash,
  66          string $pageinstance,
  67          string $elementid
  68      ): array {
  69          global $DB, $USER;
  70  
  71          [
  72              'contextid' => $contextid,
  73              'pagehash' => $pagehash,
  74              'elementid' => $elementid,
  75              'pageinstance' => $pageinstance,
  76          ] = self::validate_parameters(self::execute_parameters(), [
  77              'contextid' => $contextid,
  78              'pagehash' => $pagehash,
  79              'elementid' => $elementid,
  80              'pageinstance' => $pageinstance,
  81  
  82          ]);
  83  
  84          $manager = new \tiny_autosave\autosave_manager($contextid, $pagehash, $pageinstance, $elementid);
  85          $manager->remove_autosave_record();
  86  
  87          return [];
  88      }
  89  
  90      /**
  91       * Describe the return structure of the external service.
  92       *
  93       * @return external_single_structure
  94       */
  95      public static function execute_returns(): external_single_structure {
  96          return new external_single_structure([]);
  97      }
  98  }