1441 |
ariadna |
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 |
namespace core\output;
|
|
|
18 |
|
|
|
19 |
use core\exception\coding_exception;
|
|
|
20 |
use moodle_page;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Basis for all plugin renderers.
|
|
|
24 |
*
|
|
|
25 |
* @copyright Petr Skoda (skodak)
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
* @since Moodle 2.0
|
|
|
28 |
* @package core
|
|
|
29 |
* @category output
|
|
|
30 |
*/
|
|
|
31 |
class plugin_renderer_base extends renderer_base {
|
|
|
32 |
/**
|
|
|
33 |
* @var renderer_base|core_renderer A reference to the current renderer.
|
|
|
34 |
* The renderer provided here will be determined by the page but will in 90%
|
|
|
35 |
* of cases by the {@see core_renderer}
|
|
|
36 |
*/
|
|
|
37 |
protected $output;
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Constructor method, calls the parent constructor
|
|
|
41 |
*
|
|
|
42 |
* @param moodle_page $page
|
|
|
43 |
* @param string $target one of rendering target constants
|
|
|
44 |
*/
|
|
|
45 |
public function __construct(moodle_page $page, $target) {
|
|
|
46 |
if (empty($target) && $page->pagelayout === 'maintenance') {
|
|
|
47 |
// If the page is using the maintenance layout then we're going to force the target to maintenance.
|
|
|
48 |
// This way we'll get a special maintenance renderer that is designed to block access to API's that are likely
|
|
|
49 |
// unavailable for this page layout.
|
|
|
50 |
$target = RENDERER_TARGET_MAINTENANCE;
|
|
|
51 |
}
|
|
|
52 |
$this->output = $page->get_renderer('core', null, $target);
|
|
|
53 |
parent::__construct($page, $target);
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Renders the provided widget and returns the HTML to display it.
|
|
|
58 |
*
|
|
|
59 |
* @param renderable $widget instance with renderable interface
|
|
|
60 |
* @return string
|
|
|
61 |
*/
|
|
|
62 |
public function render(renderable $widget) {
|
|
|
63 |
$classname = get_class($widget);
|
|
|
64 |
|
|
|
65 |
// Strip namespaces.
|
|
|
66 |
$classname = preg_replace('/^.*\\\/', '', $classname);
|
|
|
67 |
|
|
|
68 |
// Keep a copy at this point, we may need to look for a deprecated method.
|
|
|
69 |
$deprecatedmethod = "render_{$classname}";
|
|
|
70 |
|
|
|
71 |
// Remove _renderable suffixes.
|
|
|
72 |
$classname = preg_replace('/_renderable$/', '', $classname);
|
|
|
73 |
$rendermethod = "render_{$classname}";
|
|
|
74 |
|
|
|
75 |
if (method_exists($this, $rendermethod)) {
|
|
|
76 |
// Call the render_[widget_name] function.
|
|
|
77 |
// Note: This has a higher priority than the named_templatable to allow the theme to override the template.
|
|
|
78 |
return $this->$rendermethod($widget);
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
if ($widget instanceof named_templatable) {
|
|
|
82 |
// This is a named templatable.
|
|
|
83 |
// Fetch the template name from the get_template_name function instead.
|
|
|
84 |
// Note: This has higher priority than the deprecated method which is not overridable by themes anyway.
|
|
|
85 |
return $this->render_from_template(
|
|
|
86 |
$widget->get_template_name($this),
|
|
|
87 |
$widget->export_for_template($this)
|
|
|
88 |
);
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
if ($rendermethod !== $deprecatedmethod && method_exists($this, $deprecatedmethod)) {
|
|
|
92 |
// This is exactly where we don't want to be.
|
|
|
93 |
// If you have arrived here you have a renderable component within your plugin that has the name
|
|
|
94 |
// blah_renderable, and you have a render method render_blah_renderable on your plugin.
|
|
|
95 |
// In 2.8 we revamped output, as part of this change we changed slightly how renderables got rendered
|
|
|
96 |
// and the _renderable suffix now gets removed when looking for a render method.
|
|
|
97 |
// You need to change your renderers render_blah_renderable to render_blah.
|
|
|
98 |
// Until you do this it will not be possible for a theme to override the renderer to override your method.
|
|
|
99 |
// Please do it ASAP.
|
|
|
100 |
static $debugged = [];
|
|
|
101 |
if (!isset($debugged[$deprecatedmethod])) {
|
|
|
102 |
debugging(sprintf(
|
|
|
103 |
'Deprecated call. Please rename your renderables render method from %s to %s.',
|
|
|
104 |
$deprecatedmethod,
|
|
|
105 |
$rendermethod
|
|
|
106 |
), DEBUG_DEVELOPER);
|
|
|
107 |
$debugged[$deprecatedmethod] = true;
|
|
|
108 |
}
|
|
|
109 |
return $this->$deprecatedmethod($widget);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
// Pass to core renderer if method not found here.
|
|
|
113 |
// Note: this is not a parent. This is _new_ renderer which respects the requested format, and output type.
|
|
|
114 |
return $this->output->render($widget);
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Magic method used to pass calls otherwise meant for the standard renderer
|
|
|
119 |
* to it to ensure we don't go causing unnecessary grief.
|
|
|
120 |
*
|
|
|
121 |
* @param string $method
|
|
|
122 |
* @param array $arguments
|
|
|
123 |
* @return mixed
|
|
|
124 |
*/
|
|
|
125 |
public function __call($method, $arguments) {
|
|
|
126 |
if (method_exists('renderer_base', $method)) {
|
|
|
127 |
throw new coding_exception('Protected method called against ' . get_class($this) . ' :: ' . $method);
|
|
|
128 |
}
|
|
|
129 |
if (method_exists($this->output, $method)) {
|
|
|
130 |
return call_user_func_array([$this->output, $method], $arguments);
|
|
|
131 |
} else {
|
|
|
132 |
throw new coding_exception('Unknown method called against ' . get_class($this) . ' :: ' . $method);
|
|
|
133 |
}
|
|
|
134 |
}
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
// Alias this class to the old name.
|
|
|
138 |
// This file will be autoloaded by the legacyclasses autoload system.
|
|
|
139 |
// In future all uses of this class will be corrected and the legacy references will be removed.
|
|
|
140 |
class_alias(plugin_renderer_base::class, \plugin_renderer_base::class);
|