Merge branch 'MDL-57742_master' of git://github.com/markn86/moodle
[moodle.git] / lib / classes / output / external.php
blob509ec4403c0b1a2dfe357dc3dea3cc10f54215d5
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.
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/>.
17 /**
18 * Mustache helper to load strings from string_manager.
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
26 namespace core\output;
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;
38 /**
39 * This class contains a list of webservice functions related to output.
41 * @copyright 2015 Damyon Wiese
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 * @since 2.9
45 class external extends external_api {
46 /**
47 * Returns description of load_template() parameters.
49 * @return external_function_parameters
51 public static function load_template_parameters() {
52 return new external_function_parameters(
53 array('component' => new external_value(PARAM_COMPONENT, 'component containing the template'),
54 'template' => new external_value(PARAM_ALPHANUMEXT, 'name of the template'),
55 'themename' => new external_value(PARAM_ALPHANUMEXT, 'The current theme.'),
56 'includecomments' => new external_value(PARAM_BOOL, 'Include comments or not', VALUE_DEFAULT, false)
61 /**
62 * Remove comments from mustache template.
63 * @param string $templatestr
64 * @return mixed
66 protected static function strip_template_comments($templatestr) {
67 return preg_replace('/(?={{!)(.*)(}})/sU', '', $templatestr);
70 /**
71 * Return a mustache template, and all the strings it requires.
73 * @param string $component The component that holds the template.
74 * @param string $templatename The name of the template.
75 * @param string $themename The name of the current theme.
76 * @return string the template
78 public static function load_template($component, $template, $themename, $includecomments = false) {
79 global $DB, $CFG, $PAGE;
81 $params = self::validate_parameters(self::load_template_parameters(),
82 array('component' => $component,
83 'template' => $template,
84 'themename' => $themename,
85 'includecomments' => $includecomments));
87 $component = $params['component'];
88 $template = $params['template'];
89 $themename = $params['themename'];
90 $includecomments = $params['includecomments'];
92 $templatename = $component . '/' . $template;
94 // Will throw exceptions if the template does not exist.
95 $filename = mustache_template_finder::get_template_filepath($templatename, $themename);
96 $templatestr = file_get_contents($filename);
98 // Remove comments from template.
99 if (!$includecomments) {
100 $templatestr = self::strip_template_comments($templatestr);
103 return $templatestr;
107 * Returns description of load_template() result value.
109 * @return external_description
111 public static function load_template_returns() {
112 return new external_value(PARAM_RAW, 'template');
116 * Returns description of load_icon_map() parameters.
118 * @return external_function_parameters
120 public static function load_fontawesome_icon_map_parameters() {
121 return new external_function_parameters([]);
125 * Return a mapping of icon names to icons.
127 * @return array the mapping
129 public static function load_fontawesome_icon_map() {
130 $instance = icon_system::instance(icon_system::FONTAWESOME);
132 $map = $instance->get_icon_name_map();
134 $result = [];
136 foreach ($map as $from => $to) {
137 list($component, $pix) = explode(':', $from);
138 $one = [];
139 $one['component'] = $component;
140 $one['pix'] = $pix;
141 $one['to'] = $to;
142 $result[] = $one;
144 return $result;
148 * Returns description of load_icon_map() result value.
150 * @return external_description
152 public static function load_fontawesome_icon_map_returns() {
153 return new external_multiple_structure(new external_single_structure(
154 array(
155 'component' => new external_value(PARAM_COMPONENT, 'The component for the icon.'),
156 'pix' => new external_value(PARAM_RAW, 'Value to map the icon from.'),
157 'to' => new external_value(PARAM_RAW, 'Value to map the icon to.')