MDL-19077 Split outputlib.php into smaller logical files.

This commit is contained in:
nicolasconnault 2009-08-10 06:22:04 +00:00
parent c7a2fd970e
commit d9c8f425fc
6 changed files with 5086 additions and 4950 deletions

160
lib/outputactions.php Normal file
View File

@ -0,0 +1,160 @@
<?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/>.
/**
* Classes representing JS event handlers, used by output components.
*
* Please see http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML
* for an overview.
*
* @package moodlecore
* @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Helper class used by other components that involve an action on the page (URL or JS).
*
* @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
*/
class component_action {
/**
* The DOM event that will trigger this action when caught
* @var string $event DOM event
*/
public $event;
/**
* The JS function you create must have two arguments:
* 1. The event object
* 2. An object/array of arguments ($jsfunctionargs)
* @var string $jsfunction A function name to call when the button is clicked
*/
public $jsfunction = false;
/**
* @var array $jsfunctionargs An array of arguments to pass to the JS function
*/
public $jsfunctionargs = array();
/**
* Constructor
* @param string $event DOM event
* @param moodle_url $url A moodle_url object, required if no jsfunction is given
* @param string $method 'post' or 'get'
* @param string $jsfunction An optional JS function. Required if jsfunctionargs is given
* @param array $jsfunctionargs An array of arguments to pass to the jsfunction
* @return void
*/
public function __construct($event, $jsfunction, $jsfunctionargs=array()) {
$this->event = $event;
$this->jsfunction = $jsfunction;
$this->jsfunctionargs = $jsfunctionargs;
if (!empty($this->jsfunctionargs)) {
if (empty($this->jsfunction)) {
throw new coding_exception('The component_action object needs a jsfunction value to pass the jsfunctionargs to.');
}
}
}
}
/**
* Component action for a popup window.
*
* @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
*/
class popup_action extends component_action {
/**
* @var array $params An array of parameters that will be passed to the openpopup JS function
*/
public $params = array(
'height' => 400,
'width' => 500,
'top' => 0,
'left' => 0,
'menubar' => false,
'location' => false,
'scrollbars' => true,
'resizable' => true,
'toolbar' => true,
'status' => true,
'directories' => false,
'fullscreen' => false,
'dependent' => true);
/**
* Constructor
* @param string $event DOM event
* @param moodle_url $url A moodle_url object, required if no jsfunction is given
* @param string $method 'post' or 'get'
* @param array $params An array of popup parameters
* @return void
*/
public function __construct($event, $url, $name='popup', $params=array()) {
global $CFG;
$this->name = $name;
$url = new moodle_url($url);
if ($this->name) {
$_name = $this->name;
if (($_name = preg_replace("/\s/", '_', $_name)) != $this->name) {
throw new coding_exception('The $name of a popup window shouldn\'t contain spaces - string modified. '. $this->name .' changed to '. $_name);
$this->name = $_name;
}
} else {
$this->name = 'popup';
}
foreach ($this->params as $var => $val) {
if (array_key_exists($var, $params)) {
$this->params[$var] = $params[$var];
}
}
parent::__construct($event, 'openpopup', array('url' => $url->out(false, array(), false), 'name' => $name, 'options' => $this->get_js_options($params)));
}
/**
* Returns a string of concatenated option->value pairs used by JS to call the popup window,
* based on this object's variables
*
* @return string String of option->value pairs for JS popup function.
*/
public function get_js_options() {
$jsoptions = '';
foreach ($this->params as $var => $val) {
if (is_string($val) || is_int($val)) {
$jsoptions .= "$var=$val,";
} elseif (is_bool($val)) {
$jsoptions .= ($val) ? "$var," : "$var=0,";
}
}
$jsoptions = substr($jsoptions, 0, strlen($jsoptions) - 1);
return $jsoptions;
}
}

2113
lib/outputcomponents.php Normal file

File diff suppressed because it is too large Load Diff

332
lib/outputfactories.php Normal file
View File

@ -0,0 +1,332 @@
<?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/>.
/**
* 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
*
* <pre>
* <h1>Hello <?php echo $name ?>!</h1>
* </pre>
*
* @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);
}
}

File diff suppressed because it is too large Load Diff

221
lib/outputpixfinders.php Normal file
View File

@ -0,0 +1,221 @@
<?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/>.
/**
* Interface and classes for icon finders.
*
* 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
*/
/**
* An icon finder is responsible for working out the correct URL for an icon.
*
* A icon finder must also have a constructor that takes a theme object.
* (See {@link standard_icon_finder::__construct} for an example.)
*
* Note that we are planning to change the Moodle icon naming convention before
* the Moodle 2.0 release. Therefore, this API will probably change.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
*/
interface icon_finder {
/**
* Return the URL for an icon identified as in pre-Moodle 2.0 code.
*
* Suppose you have old code like $url = "$CFG->pixpath/i/course.gif";
* then old_icon_url('i/course'); will return the equivalent URL that is correct now.
*
* @param string $iconname the name of the icon.
* @return string the URL for that icon.
*/
public function old_icon_url($iconname);
/**
* Return the URL for an icon identified as in pre-Moodle 2.0 code.
*
* Suppose you have old code like $url = "$CFG->modpixpath/$mod/icon.gif";
* then mod_icon_url('icon', $mod); will return the equivalent URL that is correct now.
*
* @param string $iconname the name of the icon.
* @param string $module the module the icon belongs to.
* @return string the URL for that icon.
*/
public function mod_icon_url($iconname, $module);
}
/**
* This icon finder implements the old scheme that was used when themes that had
* $THEME->custompix = false.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
*/
class pix_icon_finder implements icon_finder {
/**
* Constructor
* @param theme_config $theme the theme we are finding icons for (which is irrelevant).
*/
public function __construct($theme) {
}
/**
* Implement interface method.
* @param string $iconname the name of the icon.
* @return string the URL for that icon.
*/
public function old_icon_url($iconname) {
global $CFG;
if (file_exists($CFG->dirroot . '/pix/' . $iconname . '.png')) {
return $CFG->httpswwwroot . '/pix/' . $iconname . '.png';
} else {
return $CFG->httpswwwroot . '/pix/' . $iconname . '.gif';
}
}
/**
* Implement interface method.
* @param string $iconname the name of the icon.
* @param string $module the module the icon belongs to.
* @return string the URL for that icon.
*/
public function mod_icon_url($iconname, $module) {
global $CFG;
if (file_exists($CFG->dirroot . '/mod/' . $module . '/' . $iconname . '.png')) {
return $CFG->httpswwwroot . '/mod/' . $module . '/' . $iconname . '.png';
} else {
return $CFG->httpswwwroot . '/mod/' . $module . '/' . $iconname . '.gif';
}
}
}
/**
* This icon finder implements the old scheme that was used for themes that had
* $THEME->custompix = true.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
*/
class theme_icon_finder implements icon_finder {
protected $themename;
/**
* Constructor
* @param theme_config $theme the theme we are finding icons for.
*/
public function __construct($theme) {
$this->themename = $theme->name;
}
/**
* Implement interface method.
* @param string $iconname the name of the icon.
* @return string the URL for that icon.
*/
public function old_icon_url($iconname) {
global $CFG;
if (file_exists($CFG->themedir . '/' . $this->themename . '/pix/' . $iconname . '.png')) {
return $CFG->httpsthemewww . '/' . $this->themename . '/pix/' . $iconname . '.png';
} else {
return $CFG->httpsthemewww . '/' . $this->themename . '/pix/' . $iconname . '.gif';
}
}
/**
* Implement interface method.
* @param string $iconname the name of the icon.
* @param string $module the module the icon belongs to.
* @return string the URL for that icon.
*/
public function mod_icon_url($iconname, $module) {
global $CFG;
if (file_exists($CFG->themedir . '/' . $this->themename . '/pix/mod/' . $module . '/' . $iconname . '.png')) {
return $CFG->httpsthemewww . '/' . $this->themename . '/pix/mod/' . $module . '/' . $iconname . '.png';
} else {
return $CFG->httpsthemewww . '/' . $this->themename . '/pix/mod/' . $module . '/' . $iconname . '.gif';
}
}
}
/**
* This icon finder implements the algorithm in pix/smartpix.php.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
*/
class smartpix_icon_finder extends pix_icon_finder {
protected $places = array();
/**
* Constructor
* @param theme_config $theme the theme we are finding icons for.
*/
public function __construct($theme) {
global $CFG;
$this->places[$CFG->themedir . '/' . $theme->name . '/pix/'] =
$CFG->httpsthemewww . '/' . $theme->name . '/pix/';
if (!empty($theme->parent)) {
$this->places[$CFG->themedir . '/' . $theme->parent . '/pix/'] =
$CFG->httpsthemewww . '/' . $theme->parent . '/pix/';
}
}
/**
* Implement interface method.
* @param string $iconname the name of the icon.
* @return string the URL for that icon.
*/
public function old_icon_url($iconname) {
foreach ($this->places as $dirroot => $urlroot) {
if (file_exists($dirroot . $iconname . '.png')) {
return $dirroot . $iconname . '.png';
} else if (file_exists($dirroot . $iconname . '.gif')) {
return $dirroot . $iconname . '.gif';
}
}
return parent::old_icon_url($iconname);
}
/**
* Implement interface method.
* @param string $iconname the name of the icon.
* @param string $module the module the icon belongs to.
* @return string the URL for that icon.
*/
public function mod_icon_url($iconname, $module) {
foreach ($this->places as $dirroot => $urlroot) {
if (file_exists($dirroot . 'mod/' . $iconname . '.png')) {
return $dirroot . 'mod/' . $iconname . '.png';
} else if (file_exists($dirroot . 'mod/' . $iconname . '.gif')) {
return $dirroot . 'mod/' . $iconname . '.gif';
}
}
return parent::old_icon_url($iconname, $module);
}
}

2254
lib/outputrenderers.php Normal file

File diff suppressed because it is too large Load Diff