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   * This is the external API for this tool.
  19   *
  20   * @package    tool_templatelibrary
  21   * @copyright  2015 Damyon Wiese
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace tool_templatelibrary;
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once("$CFG->libdir/externallib.php");
  28  
  29  use external_api;
  30  use external_function_parameters;
  31  use external_value;
  32  use external_format_value;
  33  use external_single_structure;
  34  use external_multiple_structure;
  35  use invalid_parameter_exception;
  36  
  37  /**
  38   * This is the external API for this tool.
  39   *
  40   * @copyright  2015 Damyon Wiese
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class external extends external_api {
  44  
  45      /**
  46       * Returns description of list_templates() parameters.
  47       *
  48       * @return external_function_parameters
  49       */
  50      public static function list_templates_parameters() {
  51          $component = new external_value(
  52              PARAM_COMPONENT,
  53              'The component to search',
  54              VALUE_DEFAULT,
  55              ''
  56          );
  57          $search = new external_value(
  58              PARAM_RAW,
  59              'The search string',
  60              VALUE_DEFAULT,
  61              ''
  62          );
  63          $themename = new external_value(
  64              PARAM_COMPONENT,
  65              'The current theme',
  66              VALUE_DEFAULT,
  67              ''
  68          );
  69          $params = array('component' => $component, 'search' => $search, 'themename' => $themename);
  70          return new external_function_parameters($params);
  71      }
  72  
  73      /**
  74       * Loads the list of templates.
  75       * @param string $component Limit the search to a component.
  76       * @param string $search The search string.
  77       * @param string $themename The name of theme
  78       * @return array[string]
  79       */
  80      public static function list_templates($component, $search, $themename = '') {
  81          $params = self::validate_parameters(self::list_templates_parameters(),
  82                                              array(
  83                                                  'component' => $component,
  84                                                  'search' => $search,
  85                                                  'themename' => $themename,
  86                                              ));
  87  
  88          return api::list_templates($component, $search, $themename);
  89      }
  90  
  91      /**
  92       * Returns description of list_templates() result value.
  93       *
  94       * @return external_description
  95       */
  96      public static function list_templates_returns() {
  97          return new external_multiple_structure(new external_value(PARAM_RAW, 'The template name (format is component/templatename)'));
  98      }
  99  
 100      /**
 101       * Returns description of load_canonical_template() parameters.
 102       *
 103       * @return external_function_parameters
 104       */
 105      public static function load_canonical_template_parameters() {
 106          return new external_function_parameters(
 107                  array('component' => new external_value(PARAM_COMPONENT, 'component containing the template'),
 108                        'template' => new external_value(PARAM_SAFEPATH, 'name of the template'))
 109              );
 110      }
 111  
 112      /**
 113       * Return a mustache template.
 114       * Note - this function differs from the function core_output_load_template
 115       * because it will never return a theme overridden version of a template.
 116       *
 117       * @param string $component The component that holds the template.
 118       * @param string $template The name of the template.
 119       * @return string the template, false if template doesn't exist.
 120       */
 121      public static function load_canonical_template($component, $template) {
 122          $params = self::validate_parameters(self::load_canonical_template_parameters(),
 123                                              array('component' => $component,
 124                                                    'template' => $template));
 125  
 126          $component = $params['component'];
 127          $template = $params['template'];
 128  
 129          return api::load_canonical_template($component, $template);
 130      }
 131  
 132      /**
 133       * Returns description of load_canonical_template() result value.
 134       *
 135       * @return external_description
 136       */
 137      public static function load_canonical_template_returns() {
 138          return new external_value(PARAM_RAW, 'template');
 139      }
 140  }