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