Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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 qbank_viewquestiontext\external;
  18  
  19  use context_system;
  20  use core_external\external_api;
  21  use core_external\external_function_parameters;
  22  use core_external\external_value;
  23  use qbank_viewquestiontext\output\question_text_format;
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once($CFG->dirroot . '/question/editlib.php');
  28  
  29  /**
  30   * External function for setting the question text format.
  31   *
  32   * @package   qbank_viewquestiontext
  33   * @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net}
  34   * @author    Mark Johnson <mark.johnson@catalyst-eu.net>
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class set_question_text_format extends external_api {
  38      /**
  39       * Returns description of method parameters.
  40       *
  41       * @return external_function_parameters
  42       */
  43      public static function execute_parameters(): external_function_parameters {
  44          return new external_function_parameters([
  45              'format' => new external_value(PARAM_INT, 'Format for the question text', VALUE_REQUIRED),
  46          ]);
  47      }
  48  
  49      /**
  50       * Returns description of method result value.
  51       */
  52      public static function execute_returns(): void {
  53      }
  54  
  55      /**
  56       * Save the question text format preference for the current user.
  57       *
  58       * @param int $format Format for the question text.
  59       */
  60      public static function execute(int $format): void {
  61          [
  62              'format' => $format,
  63          ] = self::validate_parameters(
  64              self::execute_parameters(),
  65              [
  66                  'format' => $format,
  67              ]
  68          );
  69  
  70          if (!in_array($format, [question_text_format::OFF, question_text_format::PLAIN, question_text_format::FULL])) {
  71              throw new \invalid_parameter_exception('$format must be one of question_text_format::OFF, ::PLAIN or ::FULL.');
  72          }
  73  
  74          $context = context_system::instance();
  75          self::validate_context($context);
  76  
  77          \question_set_or_get_user_preference('qbshowtext', $format, 0, new \moodle_url('/'));
  78      }
  79  }