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  namespace core\external;
  18  
  19  use external_api;
  20  use external_function_parameters;
  21  use external_single_structure;
  22  use external_value;
  23  
  24  /**
  25   * Web service to change the edit mode.
  26   *
  27   * @package    core
  28   * @copyright  2021 Bas Brands <bas@sonsbeekmedia.nl>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class editmode extends external_api {
  32  
  33      /**
  34       * Description of the parameters suitable for the `change_editmode` function.
  35       *
  36       * @return external_function_parameters
  37       */
  38      public static function change_editmode_parameters(): external_function_parameters {
  39          return new external_function_parameters(
  40              [
  41                  'setmode' => new external_value(PARAM_BOOL, 'Set edit mode to'),
  42                  'context' => new external_value(PARAM_INT, 'Page context id')
  43              ]
  44          );
  45      }
  46  
  47      /**
  48       * Set the given edit mode
  49       *
  50       * @param bool $setmode the new edit mode
  51       * @param int $contextid the current page context id
  52       * @return array
  53       */
  54      public static function change_editmode(bool $setmode, int $contextid): array {
  55          global $USER;
  56  
  57          $params = self::validate_parameters(
  58              self::change_editmode_parameters(),
  59              [
  60                  'setmode' => $setmode,
  61                  'context' => $contextid
  62              ]
  63          );
  64  
  65          $context = \context_helper::instance_by_id($params['context']);
  66          self::validate_context($context);
  67  
  68          $USER->editing = $params['setmode'];
  69  
  70          return ['success' => true];
  71      }
  72  
  73      /**
  74       * Description of the return value for the `change_editmode` function.
  75       *
  76       * @return external_single_structure
  77       */
  78      public static function change_editmode_returns(): external_single_structure {
  79          $keys = [
  80              'success' => new external_value(PARAM_BOOL, 'The edit mode was changed', VALUE_REQUIRED),
  81          ];
  82  
  83          return new external_single_structure($keys, 'editmode');
  84      }
  85  }