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 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [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   * Mustache helper to load strings from string_manager.
  19   *
  20   * @package    core
  21   * @category   output
  22   * @copyright  2015 Damyon Wiese
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core\output;
  27  
  28  use external_api;
  29  use external_function_parameters;
  30  use external_multiple_structure;
  31  use external_single_structure;
  32  use external_value;
  33  use core_component;
  34  use moodle_exception;
  35  use context_system;
  36  use theme_config;
  37  use core\external\output\icon_system\load_fontawesome_map;
  38  
  39  /**
  40   * This class contains a list of webservice functions related to output.
  41   *
  42   * @copyright  2015 Damyon Wiese
  43   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  44   * @since      2.9
  45   */
  46  class external extends external_api {
  47      /**
  48       * Returns description of load_template() parameters.
  49       *
  50       * @return external_function_parameters
  51       */
  52      public static function load_template_parameters() {
  53          return new external_function_parameters(
  54                  array('component' => new external_value(PARAM_COMPONENT, 'component containing the template'),
  55                        'template' => new external_value(PARAM_SAFEPATH, 'name of the template'),
  56                        'themename' => new external_value(PARAM_ALPHANUMEXT, 'The current theme.'),
  57                        'includecomments' => new external_value(PARAM_BOOL, 'Include comments or not', VALUE_DEFAULT, false)
  58                           )
  59              );
  60      }
  61  
  62      /**
  63       * Return a mustache template, and all the strings it requires.
  64       *
  65       * @param string $component The component that holds the template.
  66       * @param string $templatename The name of the template.
  67       * @param string $themename The name of the current theme.
  68       * @return string the template
  69       */
  70      public static function load_template($component, $template, $themename, $includecomments = false) {
  71          global $DB, $CFG, $PAGE;
  72  
  73          $PAGE->set_context(context_system::instance());
  74          $params = self::validate_parameters(self::load_template_parameters(),
  75                                              array('component' => $component,
  76                                                    'template' => $template,
  77                                                    'themename' => $themename,
  78                                                    'includecomments' => $includecomments));
  79  
  80          $loader = new mustache_template_source_loader();
  81          // Will throw exceptions if the template does not exist.
  82          return $loader->load(
  83              $params['component'],
  84              $params['template'],
  85              $params['themename'],
  86              $params['includecomments']
  87          );
  88      }
  89  
  90      /**
  91       * Returns description of load_template() result value.
  92       *
  93       * @return external_description
  94       */
  95      public static function load_template_returns() {
  96          return new external_value(PARAM_RAW, 'template');
  97      }
  98  
  99      /**
 100       * Returns description of load_template_with_dependencies() parameters.
 101       *
 102       * @return external_function_parameters
 103       */
 104      public static function load_template_with_dependencies_parameters() {
 105          return new external_function_parameters([
 106              'component' => new external_value(PARAM_COMPONENT, 'component containing the template'),
 107              'template' => new external_value(PARAM_SAFEPATH, 'name of the template'),
 108              'themename' => new external_value(PARAM_ALPHANUMEXT, 'The current theme.'),
 109              'includecomments' => new external_value(PARAM_BOOL, 'Include comments or not', VALUE_DEFAULT, false),
 110              'lang' => new external_value(PARAM_LANG, 'lang', VALUE_DEFAULT, null),
 111          ]);
 112      }
 113  
 114      /**
 115       * Return a mustache template, and all the child templates and strings it requires.
 116       *
 117       * @param string $component The component that holds the template.
 118       * @param string $template The name of the template.
 119       * @param string $themename The name of the current theme.
 120       * @param bool $includecomments Whether to strip comments from the template source.
 121       * @param string $lang moodle translation language, null means use current.
 122       * @return string the template
 123       */
 124      public static function load_template_with_dependencies(
 125          string $component,
 126          string $template,
 127          string $themename,
 128          bool $includecomments = false,
 129          string $lang = null
 130      ) {
 131          global $DB, $CFG, $PAGE;
 132  
 133          $params = self::validate_parameters(
 134              self::load_template_with_dependencies_parameters(),
 135              [
 136                  'component' => $component,
 137                  'template' => $template,
 138                  'themename' => $themename,
 139                  'includecomments' => $includecomments,
 140                  'lang' => $lang
 141              ]
 142          );
 143  
 144          $loader = new mustache_template_source_loader();
 145          // Will throw exceptions if the template does not exist.
 146          $dependencies = $loader->load_with_dependencies(
 147              $params['component'],
 148              $params['template'],
 149              $params['themename'],
 150              $params['includecomments'],
 151              [],
 152              [],
 153              $params['lang']
 154          );
 155          $formatdependencies = function($dependency) {
 156              $results = [];
 157              foreach ($dependency as $dependencycomponent => $dependencyvalues) {
 158                  foreach ($dependencyvalues as $dependencyname => $dependencyvalue) {
 159                      array_push($results, [
 160                          'component' => $dependencycomponent,
 161                          'name' => $dependencyname,
 162                          'value' => $dependencyvalue
 163                      ]);
 164                  }
 165              }
 166              return $results;
 167          };
 168  
 169          // Now we have to unpack the dependencies into a format that can be returned
 170          // by external functions (because they don't support dynamic keys).
 171          return [
 172              'templates' => $formatdependencies($dependencies['templates']),
 173              'strings' => $formatdependencies($dependencies['strings'])
 174          ];
 175      }
 176  
 177      /**
 178       * Returns description of load_template_with_dependencies() result value.
 179       *
 180       * @return external_description
 181       */
 182      public static function load_template_with_dependencies_returns() {
 183          $resourcestructure = new external_single_structure([
 184              'component' => new external_value(PARAM_COMPONENT, 'component containing the resource'),
 185              'name' => new external_value(PARAM_TEXT, 'name of the resource'),
 186              'value' => new external_value(PARAM_RAW, 'resource value')
 187          ]);
 188  
 189          return new external_single_structure([
 190              'templates' => new external_multiple_structure($resourcestructure),
 191              'strings' => new external_multiple_structure($resourcestructure)
 192          ]);
 193      }
 194  
 195      /**
 196       * Returns description of load_icon_map() parameters.
 197       *
 198       * @return external_function_parameters
 199       */
 200      public static function load_fontawesome_icon_map_parameters() {
 201          return new external_function_parameters([]);
 202      }
 203  
 204      /**
 205       * Return a mapping of icon names to icons.
 206       *
 207       * @deprecated since Moodle 3.10
 208       * @return array the mapping
 209       */
 210      public static function load_fontawesome_icon_map() {
 211          global $PAGE;
 212  
 213          return load_fontawesome_map::execute($PAGE->theme->name);
 214      }
 215  
 216      /**
 217       * Returns description of load_icon_map() result value.
 218       *
 219       * @return external_description
 220       */
 221      public static function load_fontawesome_icon_map_returns() {
 222          return load_fontawesome_map::execute_returns();
 223      }
 224  
 225      /**
 226       * The `load_fontawesome_icon_map` function has been replaced with
 227       * @see load_fontawesome_map::execute()
 228       *
 229       * @return bool
 230       */
 231      public static function load_fontawesome_icon_map_is_deprecated() {
 232          return true;
 233      }
 234  }