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  /**
  18   * TinyMCE Equation external API for filtering the equation.
  19   *
  20   * @package    tiny_equation
  21   * @copyright  2022 Huong Nguyen <huongnv13@gmail.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tiny_equation\external;
  26  
  27  use context;
  28  use external_api;
  29  use external_function_parameters;
  30  use external_single_structure;
  31  use external_value;
  32  use filter_manager;
  33  
  34  /**
  35   * TinyMCE Equation external API for filtering the equation.
  36   *
  37   * @package    tiny_equation
  38   * @copyright  2022 Huong Nguyen <huongnv13@gmail.com>
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class filter extends external_api {
  42  
  43      /**
  44       * Describes the parameters for filtering the equation.
  45       *
  46       * @return external_function_parameters
  47       * @since Moodle 4.1
  48       */
  49      public static function execute_parameters(): external_function_parameters {
  50          return new external_function_parameters([
  51              'contextid' => new external_value(PARAM_INT, 'The context ID', VALUE_REQUIRED),
  52              'content' => new external_value(PARAM_RAW, 'The equation content', VALUE_REQUIRED)
  53          ]);
  54      }
  55  
  56      /**
  57       * External function to filter the equation.
  58       *
  59       * @param int $contextid Context ID.
  60       * @param string $content Equation content.
  61       * @return array
  62       * @since Moodle 4.1
  63       */
  64      public static function execute(int $contextid, string $content): array {
  65          [
  66              'contextid' => $contextid,
  67              'content' => $content
  68          ] = self::validate_parameters(self::execute_parameters(), [
  69              'contextid' => $contextid,
  70              'content' => $content
  71          ]);
  72  
  73          $context = context::instance_by_id($contextid);
  74          self::validate_context($context);
  75          $result = filter_manager::instance()->filter_text($content, $context);
  76  
  77          return [
  78              'content' => $result,
  79          ];
  80      }
  81  
  82      /**
  83       * Describes the data returned from the external function.
  84       *
  85       * @return external_single_structure
  86       * @since Moodle 4.1
  87       */
  88      public static function execute_returns(): external_single_structure {
  89          return new external_single_structure([
  90              'content' => new external_value(PARAM_RAW, 'Filtered content'),
  91          ]);
  92      }
  93  }