Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Class renderer.
19
 *
20
 * @package    block_dash
21
 * @copyright  2019 bdecent gmbh <https://bdecent.de>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace block_dash\output;
26
 
27
use block_dash\local\data_source\abstract_data_source;
28
use block_dash\local\data_source\data_source_interface;
29
 
30
use Mustache_Engine;
31
/**
32
 * Class renderer.
33
 *
34
 * @package block_dash
35
 */
36
class renderer extends \plugin_renderer_base {
37
 
38
    /**
39
     * @var Mustache_Engine
40
     */
41
    private $mustache;
42
 
43
    /**
44
     * Return an instance of the mustache class without the custom Moodle file loader.
45
     *
46
     * This will be used for rendering templates from strings only.
47
     *
48
     * @return \Mustache_Engine
49
     */
50
    protected function get_mustache() {
51
        global $CFG;
52
 
53
        if ($this->mustache === null) {
54
            require_once("{$CFG->libdir}/filelib.php");
55
 
56
            $themename = $this->page->theme->name;
57
            $themerev = theme_get_revision();
58
 
59
            // Create new localcache directory.
60
            $cachedir = make_localcache_directory("mustache/$themerev/$themename");
61
 
62
            // Remove old localcache directories.
63
            $mustachecachedirs = glob("{$CFG->localcachedir}/mustache/*", GLOB_ONLYDIR);
64
            foreach ($mustachecachedirs as $localcachedir) {
65
                $cachedrev = [];
66
                preg_match("/\/mustache\/([0-9]+)$/", $localcachedir, $cachedrev);
67
                $cachedrev = isset($cachedrev[1]) ? intval($cachedrev[1]) : 0;
68
                if ($cachedrev > 0 && $cachedrev < $themerev) {
69
                    fulldelete($localcachedir);
70
                }
71
            }
72
 
73
            $loader = new mustache_custom_loader();
74
            $stringhelper = new \core\output\mustache_string_helper();
75
            $quotehelper = new \core\output\mustache_quote_helper();
76
            $jshelper = new \core\output\mustache_javascript_helper($this->page);
77
            $pixhelper = new \core\output\mustache_pix_helper($this);
78
            $shortentexthelper = new \core\output\mustache_shorten_text_helper();
79
            $userdatehelper = new \core\output\mustache_user_date_helper();
80
 
81
            // We only expose the variables that are exposed to JS templates.
82
            $safeconfig = $this->page->requires->get_config_for_javascript($this->page, $this);
83
 
84
            $helpers = ['config' => $safeconfig,
85
                'str' => [$stringhelper, 'str'],
86
                'quote' => [$quotehelper, 'quote'],
87
                'js' => [$jshelper, 'help'],
88
                'pix' => [$pixhelper, 'pix'],
89
                'shortentext' => [$shortentexthelper, 'shorten'],
90
                'userdate' => [$userdatehelper, 'transform'],
91
            ];
92
 
93
            if (block_dash_is_totara()) {
94
                $flexhelper = new \core\output\mustache_flex_icon_helper($this);
95
                $helpers['flex_icon'] = [$flexhelper, 'flex_icon'];
96
            }
97
 
98
            $this->mustache = new Mustache_Engine([
99
                'cache' => $cachedir,
100
                'escape' => 's',
101
                'loader' => $loader,
102
                'helpers' => $helpers,
103
                'pragmas' => [\Mustache_Engine::PRAGMA_BLOCKS],
104
                // Don't allow the JavaScript helper to be executed from within another
105
                // helper. If it's allowed it can be used by users to inject malicious
106
                // JS into the page.
107
                'blacklistednestedhelpers' => ['js'],
108
            ]);
109
 
110
        }
111
 
112
        return $this->mustache;
113
    }
114
 
115
    /**
116
     * Renders a template by string with the given context.
117
     *
118
     * The provided data needs to be array/stdClass made up of only simple types.
119
     * Simple types are array,stdClass,bool,int,float,string
120
     *
121
     * @since 2.9
122
     * @param string $templatestring Raw template string
123
     * @param array|\stdClass $context Context containing data for the template.
124
     * @return string|boolean
125
     */
126
    public function render_from_template($templatestring, $context) {
127
        return trim($this->get_mustache()->render($templatestring, $context));
128
    }
129
 
130
    /**
131
     * Render a data source.
132
     *
133
     * @param abstract_data_source $datasource
134
     * @return bool|string
135
     * @throws \coding_exception
136
     */
137
    public function render_data_source(abstract_data_source $datasource) {
138
        return $this->render_from_template($datasource->get_layout()->get_mustache_template_name(),
139
            $datasource->export_for_template($this));
140
    }
141
 
142
    /**
143
     * Compatibility for Totara.
144
     *
145
     * @param query_debug $debug
146
     * @return bool|string
147
     */
148
    public function render_query_debug(query_debug $debug) {
149
        return $this->render_from_template('block_dash/query_debug', $debug->export_for_template($this));
150
    }
151
}