. /** * Interface and classes for creating appropriate renderers for various * parts of Moodle. * * Please see http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML * for an overview. * * @package moodlecore * @copyright 2009 Tim Hunt * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ /** * A renderer factory is just responsible for creating an appropriate renderer * for any given part of Moodle. * * Which renderer factory to use is chose by the current theme, and an instance * if created automatically when the theme is set up. * * A renderer factory must also have a constructor that takes a theme_config object. * (See {@link renderer_factory_base::__construct} for an example.) * * @copyright 2009 Tim Hunt * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 2.0 */ interface renderer_factory { /** * Return the renderer for a particular part of Moodle. * * The renderer interfaces are defined by classes called moodle_{plugin}_renderer * where {plugin} is the name of the component. The renderers for core Moodle are * defined in lib/renderer.php. For plugins, they will be defined in a file * called renderer.php inside the plugin. * * Renderers will normally want to subclass the moodle_renderer_base class. * (However, if you really know what you are doing, you don't have to do that.) * * There is no separate interface definition for renderers. The default * moodle_{plugin}_renderer implementation also serves to define the API for * other implementations of the interface, whether or not they subclass it. * For example, {@link custom_corners_core_renderer} does subclass * {@link moodle_core_renderer}. On the other hand, if you are using * {@link template_renderer_factory} then you always get back an instance * of the {@link template_renderer} class, whatever type of renderer you ask * for. This uses the fact that PHP is a dynamic language. * * A particular plugin can define multiple renderers if it wishes, using the * $subtype parameter. For example moodle_mod_workshop_renderer, * moodle_mod_workshop_allocation_manual_renderer etc. * * @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'. * @param moodle_page $page the page the renderer is outputting content for. * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news' * @return object an object implementing the requested renderer interface. */ public function get_renderer($component, $page, $subtype=null); } /** * This is a base class to help you implement the renderer_factory interface. * * It keeps a cache of renderers that have been constructed, so you only need * to construct each one once in you subclass. * * It also has a method to get the name of, and include the renderer.php with * the definition of, the standard renderer class for a given module. * * @copyright 2009 Tim Hunt * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 2.0 */ abstract class renderer_factory_base implements renderer_factory { /** @var theme_config the theme we belong to. */ protected $theme; /** * Constructor. * @param theme_config $theme the theme we belong to. */ public function __construct($theme) { $this->theme = $theme; } /** * For a given module name, return the name of the standard renderer class * that defines the renderer interface for that module. * * Also, if it exists, include the renderer.php file for that module, so * the class definition of the default renderer has been loaded. * * @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'. * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news' * @return string the name of the standard renderer class for that module. */ protected function standard_renderer_class_for_module($component, $subtype=null) { if ($component != 'core') { $pluginrenderer = get_component_directory($component) . '/renderer.php'; if (file_exists($pluginrenderer)) { include_once($pluginrenderer); } } if (is_null($subtype)) { $class = 'moodle_' . $component . '_renderer'; } else { $class = 'moodle_' . $component . '_' . $subtype . '_renderer'; } if (!class_exists($class)) { throw new coding_exception('Request for an unknown renderer class ' . $class); } return $class; } } /** * This is the default renderer factory for Moodle. It simply returns an instance * of the appropriate standard renderer class. * * @copyright 2009 Tim Hunt * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 2.0 */ class standard_renderer_factory extends renderer_factory_base { /** * Implement the subclass method * @param string $module name such as 'core', 'mod_forum' or 'qtype_multichoice'. * @param moodle_page $page the page the renderer is outputting content for. * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news' * @return object an object implementing the requested renderer interface. */ public function get_renderer($module, $page, $subtype=null) { if ($module == 'core') { return new moodle_core_renderer($page); } else { $class = $this->standard_renderer_class_for_module($module, $subtype); return new $class($page, $this->get_renderer('core', $page)); } } } /** * This is a slight variation on the standard_renderer_factory used by CLI scripts. * * @copyright 2009 Tim Hunt * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 2.0 */ class cli_renderer_factory extends standard_renderer_factory { /** * Implement the subclass method * @param string $module name such as 'core', 'mod_forum' or 'qtype_multichoice'. * @param moodle_page $page the page the renderer is outputting content for. * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news' * @return object an object implementing the requested renderer interface. */ public function get_renderer($module, $page, $subtype=null) { if ($module == 'core') { return new cli_core_renderer($page); } else { parent::get_renderer($module, $page, $subtype); } } } /** * This is renderer factory allows themes to override the standard renderers using * php code. * * It will load any code from theme/mytheme/renderers.php and * theme/parenttheme/renderers.php, if then exist. Then whenever you ask for * a renderer for 'component', it will create a mytheme_component_renderer or a * parenttheme_component_renderer, instead of a moodle_component_renderer, * if either of those classes exist. * * This generates the slightly different HTML that the custom_corners theme expects. * * @copyright 2009 Tim Hunt * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 2.0 */ class theme_overridden_renderer_factory extends standard_renderer_factory { protected $prefixes = array(); /** * Constructor. * @param object $theme the theme we are rendering for. */ public function __construct($theme) { global $CFG; parent::__construct($theme); // Initialise $this->prefixes. $renderersfile = $theme->dir . '/renderers.php'; if (is_readable($renderersfile)) { include_once($renderersfile); $this->prefixes[] = $theme->name . '_'; } if (!empty($theme->parent)) { $renderersfile = $CFG->themedir .'/'. $theme->parent . '/renderers.php'; if (is_readable($renderersfile)) { include_once($renderersfile); $this->prefixes[] = $theme->parent . '_'; } } } /** * Implement the subclass method * @param string $module name such as 'core', 'mod_forum' or 'qtype_multichoice'. * @param moodle_page $page the page the renderer is outputting content for. * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news' * @return object an object implementing the requested renderer interface. */ public function get_renderer($module, $page, $subtype=null) { foreach ($this->prefixes as $prefix) { if (is_null($subtype)) { $classname = $prefix . $module . '_renderer'; } else { $classname = $prefix . $module . '_' . $subtype . '_renderer'; } if (class_exists($classname)) { if ($module == 'core') { return new $classname($page); } else { return new $classname($page, $this->get_renderer('core', $page)); } } } return parent::get_renderer($module, $page, $subtype); } } /** * This is renderer factory that allows you to create templated themes. * * This should be considered an experimental proof of concept. In particular, * the performance is probably not very good. Do not try to use in on a busy site * without doing careful load testing first! * * This renderer factory returns instances of {@link template_renderer} class * which which implement the corresponding renderer interface in terms of * templates. To use this your theme must have a templates folder inside it. * Then suppose the method moodle_core_renderer::greeting($name = 'world'); * exists. Then, a call to $OUTPUT->greeting() will cause the template * /theme/yourtheme/templates/core/greeting.php to be rendered, with the variable * $name available. The greeting.php template might contain * *
 * 

Hello !

*
* * @copyright 2009 Tim Hunt * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 2.0 */ class template_renderer_factory extends renderer_factory_base { /** * An array of paths of where to search for templates. Normally this theme, * the parent theme then the standardtemplate theme. (If some of these do * not exist, or are the same as each other, then the list will be shorter. */ protected $searchpaths = array(); /** * Constructor. * @param object $theme the theme we are rendering for. */ public function __construct($theme) { global $CFG; parent::__construct($theme); // Initialise $this->searchpaths. if ($theme->name != 'standardtemplate') { $templatesdir = $theme->dir . '/templates'; if (is_dir($templatesdir)) { $this->searchpaths[] = $templatesdir; } } if (!empty($theme->parent)) { $templatesdir = $CFG->themedir .'/'. $theme->parent . '/templates'; if (is_dir($templatesdir)) { $this->searchpaths[] = $templatesdir; } } $this->searchpaths[] = $CFG->themedir .'/standardtemplate/templates'; } /** * Implement the subclass method * @param string $module name such as 'core', 'mod_forum' or 'qtype_multichoice'. * @param moodle_page $page the page the renderer is outputting content for. * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news' * @return object an object implementing the requested renderer interface. */ public function get_renderer($module, $page, $subtype=null) { // Refine the list of search paths for this module. $searchpaths = array(); foreach ($this->searchpaths as $rootpath) { $path = $rootpath . '/' . $module; if (!is_null($subtype)) { $path .= '/' . $subtype; } if (is_dir($path)) { $searchpaths[] = $path; } } // Create a template_renderer that copies the API of the standard renderer. $copiedclass = $this->standard_renderer_class_for_module($module, $subtype); return new template_renderer($copiedclass, $searchpaths, $page); } }