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 resume an 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 resume_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              'draftid' => new external_value(
  49                  PARAM_INT,
  50                  'The new draft item id to resume files to',
  51                  VALUE_REQUIRED,
  52                  null,
  53                  NULL_ALLOWED
  54              ),
  55          ]);
  56      }
  57  
  58      /**
  59       * Reset the autosave entry for this autosave instance.
  60       *
  61       * If not matching autosave area could be found, the function will
  62       * silently return and this is not treated as an error condition.
  63       *
  64       * @param int $contextid The context id of the owner
  65       * @param string $pagehash The hash of the page
  66       * @param string $pageinstance The instance id of the page
  67       * @param string $elementid The id of the element
  68       * @param int $draftid The id of the draftid to resume to
  69       * @return null
  70       */
  71      public static function execute(
  72          int $contextid,
  73          string $pagehash,
  74          string $pageinstance,
  75          string $elementid,
  76          ?int $draftid
  77      ): array {
  78          global $DB, $USER;
  79  
  80          [
  81              'contextid' => $contextid,
  82              'pagehash' => $pagehash,
  83              'pageinstance' => $pageinstance,
  84              'elementid' => $elementid,
  85              'draftid' => $draftid,
  86          ] = self::validate_parameters(self::execute_parameters(), [
  87              'contextid' => $contextid,
  88              'pagehash' => $pagehash,
  89              'pageinstance' => $pageinstance,
  90              'elementid' => $elementid,
  91              'draftid' => $draftid,
  92          ]);
  93  
  94          $manager = new \tiny_autosave\autosave_manager($contextid, $pagehash, $pageinstance, $elementid);
  95          $sessiondata = $manager->resume_autosave_session($draftid);
  96          return [
  97              'drafttext' => $sessiondata->drafttext,
  98          ];
  99      }
 100  
 101      /**
 102       * Describe the return structure of the external service.
 103       *
 104       * @return external_single_structure
 105       */
 106      public static function execute_returns(): external_single_structure {
 107          return new external_single_structure([
 108              'drafttext' => new external_value(PARAM_RAW, 'The draft text'),
 109          ]);
 110      }
 111  }