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  declare(strict_types=1);
  18  
  19  namespace core\external;
  20  
  21  use coding_exception;
  22  use context_system;
  23  use core\output\dynamic_tabs\base;
  24  use external_api;
  25  use external_function_parameters;
  26  use external_single_structure;
  27  use external_value;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  global $CFG;
  32  require_once("$CFG->libdir/externallib.php");
  33  
  34  /**
  35   * External method for getting tab contents
  36   *
  37   * @package     core
  38   * @copyright   2021 David Matamoros <davidmc@moodle.com> based on code from Marina Glancy
  39   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class dynamic_tabs_get_content extends external_api {
  42  
  43      /**
  44       * External method parameters
  45       *
  46       * @return external_function_parameters
  47       */
  48      public static function execute_parameters(): external_function_parameters {
  49          return new external_function_parameters([
  50              'tab' => new external_value(PARAM_RAW_TRIMMED, 'Tab class', VALUE_REQUIRED),
  51              'jsondata' => new external_value(PARAM_RAW, 'Json-encoded data', VALUE_REQUIRED),
  52          ]);
  53      }
  54  
  55      /**
  56       * Tab content
  57       *
  58       * @param string $tabclass class of the tab
  59       * @param string $jsondata
  60       * @return array
  61       */
  62      public static function execute(string $tabclass, string $jsondata): array {
  63          global $PAGE, $OUTPUT;
  64  
  65          [
  66              'tab' => $tabclass,
  67              'jsondata' => $jsondata,
  68          ] = self::validate_parameters(self::execute_parameters(), [
  69              'tab' => $tabclass,
  70              'jsondata' => $jsondata,
  71          ]);
  72  
  73          $data = @json_decode($jsondata, true);
  74  
  75          $context = context_system::instance();
  76          self::validate_context($context);
  77  
  78          // This call is needed to avoid debug messages on webserver log.
  79          $PAGE->set_url('/');
  80          // This call is needed to initiate moodle page.
  81          $OUTPUT->header();
  82  
  83          if (!class_exists($tabclass) || !is_subclass_of($tabclass, base::class)) {
  84              throw new coding_exception('unknown dynamic tab class', $tabclass);
  85          }
  86  
  87          /** @var base $tab */
  88          $tab = new $tabclass($data);
  89          $tab->require_access();
  90          $PAGE->start_collecting_javascript_requirements();
  91  
  92          $content = $tab->export_for_template($PAGE->get_renderer('core'));
  93          $jsfooter = $PAGE->requires->get_end_code();
  94          return [
  95              'template' => $tab->get_template(),
  96              'content' => json_encode($content),
  97              'javascript' => $jsfooter,
  98          ];
  99      }
 100  
 101      /**
 102       * External method return value
 103       *
 104       * @return external_single_structure
 105       */
 106      public static function execute_returns(): external_single_structure {
 107          return new external_single_structure([
 108              'template' => new external_value(PARAM_PATH, 'Template name'),
 109              'content' => new external_value(PARAM_RAW, 'JSON-encoded data for template'),
 110              'javascript' => new external_value(PARAM_RAW, 'JavaScript fragment'),
 111          ]);
 112      }
 113  }