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