mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
api.php
144 lines (125 loc) · 5.22 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Class for listing mustache templates.
*
* @package tool_templatelibrary
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_templatelibrary;
use core_component;
use core\output\mustache_template_finder;
use coding_exception;
use moodle_exception;
use required_capability_exception;
use stdClass;
/**
* API exposed by tool_templatelibrary
*
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class api {
/**
* Return a list of details about installed templates.
*
* @param string $component Filter the list to a single component.
* @param string $search Search string to optionally filter the list of templates.
* @param string $themename The name of the current theme.
* @return array[string] Where each template is in the form "component/templatename".
*/
public static function list_templates($component = '', $search = '', $themename = '') {
global $CFG;
$templatedirs = array();
$results = array();
if ($component !== '') {
// Just look at one component for templates.
$dirs = mustache_template_finder::get_template_directories_for_component($component, $themename);
$templatedirs[$component] = $dirs;
} else {
// Look at all the templates dirs for core.
$templatedirs['core'] = mustache_template_finder::get_template_directories_for_component('core', $themename);
// Look at all the templates dirs for subsystems.
$subsystems = core_component::get_core_subsystems();
foreach ($subsystems as $subsystem => $dir) {
$dir .= '/templates';
if (is_dir($dir)) {
$dirs = mustache_template_finder::get_template_directories_for_component('core_' . $subsystem, $themename);
$templatedirs['core_' . $subsystem] = $dirs;
}
}
// Look at all the templates dirs for plugins.
$plugintypes = core_component::get_plugin_types();
foreach ($plugintypes as $type => $dir) {
$plugins = core_component::get_plugin_list_with_file($type, 'templates', false);
foreach ($plugins as $plugin => $dir) {
if (!empty($dir) && is_dir($dir)) {
$pluginname = $type . '_' . $plugin;
$dirs = mustache_template_finder::get_template_directories_for_component($pluginname, $themename);
$templatedirs[$pluginname] = $dirs;
}
}
}
}
foreach ($templatedirs as $templatecomponent => $dirs) {
foreach ($dirs as $dir) {
// List it.
$files = glob($dir . '/*.mustache');
foreach ($files as $file) {
$templatename = basename($file, '.mustache');
if ($search == '' || strpos($templatename, $search) !== false) {
$results[$templatecomponent . '/' . $templatename] = 1;
}
}
}
}
$results = array_keys($results);
sort($results);
return $results;
}
/**
* Return a mustache template.
* Note - this function differs from the function core_output_load_template
* because it will never return a theme overridden version of a template.
*
* @param string $component The component that holds the template.
* @param string $template The name of the template.
* @return string the template
*/
public static function load_canonical_template($component, $template) {
// Get the list of possible template directories.
$dirs = mustache_template_finder::get_template_directories_for_component($component);
$filename = false;
$themedir = core_component::get_plugin_types()['theme'];
foreach ($dirs as $dir) {
// Skip theme dirs - we only want the original plugin/core template.
if (strpos($dir, $themedir) === 0) {
continue;
}
$candidate = $dir . $template . '.mustache';
if (file_exists($candidate)) {
$filename = $candidate;
break;
}
}
if ($filename === false) {
throw new moodle_exception('filenotfound', 'error');
}
$templatestr = file_get_contents($filename);
return $templatestr;
}
}