|
| __construct () |
| Page requirements constructor.
|
|
| css ($stylesheet) |
| Ensure that the specified CSS file is linked to from this page.
|
|
| css_theme (moodle_url $stylesheet) |
| Add theme stylesheet to page - do not use from plugin code, this should be called only from the core renderer!
|
|
| data_for_js ($variable, $data, $inhead=false) |
| !!!!!!DEPRECATED!!!!!! please use js_init_call() for everything now.
|
|
| event_handler ($selector, $event, $function, array $arguments=null) |
| Creates a YUI event handler.
|
|
| get_config_for_javascript (moodle_page $page, renderer_base $renderer) |
| Return the safe config values that get set for javascript in "M.cfg".
|
|
| get_end_code () |
| Generate any HTML that needs to go at the end of the page.
|
|
| get_head_code (moodle_page $page, core_renderer $renderer) |
| Generate any HTML that needs to go inside the <head> tag.
|
|
| get_jsrev () |
| Determine the correct JS Revision to use for this load.
|
|
| get_top_of_body_code (renderer_base $renderer) |
| Generate any HTML that needs to go at the start of the <body> tag.
|
|
| has_one_time_item_been_created ($thing) |
| Has a particular bit of HTML that is only required once on this page (e.g.
|
|
| is_head_done () |
| Have we already output the code in the <head> tag?
|
|
| is_top_of_body_done () |
| Have we already output the code at the start of the <body> tag?
|
|
| jquery () |
| Request inclusion of jQuery library in the page.
|
|
| jquery_override_plugin ($oldplugin, $newplugin) |
| Request replacement of one jQuery plugin by another.
|
|
| jquery_plugin ($plugin, $component='core') |
| Request inclusion of jQuery plugin.
|
|
| js ($url, $inhead=false) |
| Ensure that the specified JavaScript file is linked to from this page.
|
|
| js_amd_inline ($code) |
| This function appends a block of code to the AMD specific javascript block executed in the page footer, just after loading the requirejs library.
|
|
| js_call_amd ($fullmodule, $func=null, $params=array()) |
| Load an AMD module and eventually call its method.
|
|
| js_function_call ($function, array $arguments=null, $ondomready=false, $delay=0) |
| !!!DEPRECATED!!! please use js_init_call() if possible Ensure that the specified JavaScript function is called from an inline script somewhere on this page.
|
|
| js_init_call ($function, array $extraarguments=null, $ondomready=false, array $module=null) |
| Ensure that the specified JavaScript function is called from an inline script from page footer.
|
|
| js_init_code ($jscode, $ondomready=false, array $module=null) |
| Add short static javascript code fragment to page footer.
|
|
| js_module ($module) |
| Append YUI3 module to default YUI3 JS loader.
|
|
| set_one_time_item_created ($thing) |
| Indicate that a particular bit of HTML that is only required once on this page (e.g.
|
|
| set_yuicssmodules (array $modules=array()) |
| Set the CSS Modules to be included from YUI.
|
|
| should_create_one_time_item_now ($thing) |
| Should we generate a bit of content HTML that is only required once on this page (e.g.
|
|
| skip_link_to ($target, $linktext) |
| Ensure that a skip link to a given target is printed at the top of the <body>.
|
|
| string_for_js ($identifier, $component, $a=null) |
| Make a language string available to JavaScript.
|
|
| strings_for_js ($identifiers, $component, $a=null) |
| Make an array of language strings available for JS.
|
|
| yui_module ($modules, $function, array $arguments=null, $galleryversion=null, $ondomready=false) |
| Creates a JavaScript function call that requires one or more modules to be loaded.
|
|
|
array | $amdjscode = array('') |
| Inline scripts using RequireJS module loading.
|
|
array | $cssthemeurls = array() |
| of moodle_url Theme sheets, initialised only from core_renderer
|
|
array | $cssurls = array() |
| of moodle_url List of custom theme sheets, these are strongly discouraged! Useful mostly only for CSS submitted by teachers that is not part of the theme.
|
|
array | $eventhandlers = array() |
| List of requested event handlers.
|
|
array | $extramodules = array() |
| Extra modules.
|
|
bool | $headdone = false |
| Flag indicated head stuff already printed.
|
|
array | $jquerypluginoverrides = array() |
| list of jQuery plugin overrides
|
|
array | $jqueryplugins = array() |
| list of requested jQuery plugins
|
|
array | $jscalls = array('normal'=>array(), 'ondomready'=>array()) |
| List of needed function calls.
|
|
array | $jsincludes = array('head'=>array(), 'footer'=>array()) |
| Included JS scripts.
|
|
array | $jsinitcode = array() |
| Javascript code used for initialisation of page, it should be relatively small.
|
|
array | $jsinitvariables = array('head'=>array(), 'footer'=>array()) |
| List of JS variables to be initialised.
|
|
array | $M_cfg |
| Some config vars exposed in JS, please no secret stuff there.
|
|
array | $onetimeitemsoutput = array() |
| trackes the names of bits of HTML that are only required once per page.
|
|
array | $skiplinks = array() |
| List of skip links, those are needed for accessibility reasons.
|
|
array | $stringsforjs = array() |
| List of string available from JS.
|
|
array | $stringsforjs_as = array() |
| List of get_string $a parameters - used for validation only.
|
|
bool | $topofbodydone = false |
| Flag indicating top of body already printed.
|
|
stdClass | $yui3loader |
| YUI PHPLoader instance responsible for YUI3 loading from PHP only.
|
|
YUI_config | $YUI_config |
| default YUI loader configuration
|
|
array | $yuicssmodules = array() |
| $yuicssmodules
|
|
This class tracks all the things that are needed by the current page.
Normally, the only instance of this class you will need to work with is the one accessible via $PAGE->requires.
Typical usage would be
$PAGE->requires->js_call_amd('mod_forum/view', 'init');
It also supports obsoleted coding style with/without YUI3 modules.
$PAGE->requires->js_init_call('M.mod_forum.init_view');
$PAGE->requires->css('/mod/mymod/userstyles.php?id='.$id); // not overridable via themes!
$PAGE->requires->js('/mod/mymod/script.js');
$PAGE->requires->js('/mod/mymod/small_but_urgent.js', true);
$PAGE->requires->js_function_call('init_mymod', array($data), true);
There are some natural restrictions on some methods. For example, css() can only be called before the <head> tag is output. See the comments on the individual methods for details.
- Copyright
- 2009 Tim Hunt, 2010 Petr Skoda
- License
- http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- Since
- Moodle 2.0
page_requirements_manager::css |
( |
| $stylesheet | ) |
|
Ensure that the specified CSS file is linked to from this page.
Because stylesheet links must go in the <head> part of the HTML, you must call this function before get_head_code() is called. That normally means before the call to print_header. If you call it when it is too late, an exception will be thrown.
Even if a particular style sheet is requested more than once, it will only be linked to once.
Please note use of this feature is strongly discouraged, it is suitable only for places where CSS is submitted directly by teachers. (Students must not be allowed to submit any external CSS because it may contain embedded javascript!). Example of correct use is mod/data.
- Parameters
-
string | $stylesheet | The path to the .css file, relative to $CFG->wwwroot. For example: $PAGE->requires->css('mod/data/css.php?d='.$data->id); |
page_requirements_manager::jquery_override_plugin |
( |
| $oldplugin, |
|
|
| $newplugin ) |
Request replacement of one jQuery plugin by another.
This is useful when themes want to replace the jQuery UI theme, the problem is that theme can not prevent others from including the core ui-css plugin.
Example: 1/ generate new jQuery UI theme and place it into theme/yourtheme/jquery/ 2/ write theme/yourtheme/jquery/plugins.php 3/ init jQuery from theme
// file theme/yourtheme/lib.php function theme_yourtheme_page_init($page) { $page->requires->jquery_plugin('yourtheme-ui-css', 'theme_yourtheme'); $page->requires->jquery_override_plugin('ui-css', 'yourtheme-ui-css'); }
This code prevents loading of standard 'ui-css' which my be requested by other plugins, the 'yourtheme-ui-css' gets loaded only if some other code requires jquery.
- Parameters
-
string | $oldplugin | original plugin |
string | $newplugin | the replacement |
page_requirements_manager::jquery_plugin |
( |
| $plugin, |
|
|
| $component = 'core' ) |
Request inclusion of jQuery plugin.
NOTE: this should not be used in official Moodle distribution!
jQuery plugins are located in plugin/jquery/* subdirectory, plugin/jquery/plugins.php lists all available plugins.
Included core plugins:
Add-ons may include extra jQuery plugins in jquery/ directory, plugins.php file defines the mapping between plugin names and necessary page includes.
Examples: // file: mod/xxx/view.php $PAGE->requires->jquery(); $PAGE->requires->jquery_plugin('ui'); $PAGE->requires->jquery_plugin('ui-css');
// file: theme/yyy/lib.php function theme_yyy_page_init(moodle_page $page) { $page->requires->jquery(); $page->requires->jquery_plugin('ui'); $page->requires->jquery_plugin('ui-css'); }
// file: blocks/zzz/block_zzz.php public function get_required_javascript() { parent\get_required_javascript(); $this->page->requires->jquery(); $page->requires->jquery_plugin('ui'); $page->requires->jquery_plugin('ui-css'); }
- Parameters
-
string | $plugin | name of the jQuery plugin as defined in jquery/plugins.php |
string | $component | name of the component |
- Return values
-
page_requirements_manager::should_create_one_time_item_now |
( |
| $thing | ) |
|
Should we generate a bit of content HTML that is only required once on this page (e.g.
the contents of the modchooser), now? Basically, we call has_one_time_item_been_created(), and if the thing has not already been output, we return true to tell the caller to generate it, and also call set_one_time_item_created() to record the fact that it is about to be generated.
That is, a typical usage pattern (in a renderer method) is:
if (!$this->page->requires->should_create_one_time_item_now($thing)) {
return '';
}
// Else generate it.
- Parameters
-
string | $thing | identifier for the bit of content. Should be of the form frankenstyle_things, e.g. core_course_modchooser. |
- Return values
-
bool | if true, the caller should generate that bit of output now, otherwise don't. |
page_requirements_manager::skip_link_to |
( |
| $target, |
|
|
| $linktext ) |
Ensure that a skip link to a given target is printed at the top of the <body>.
You must call this function before get_top_of_body_code(), (if not, an exception will be thrown). That normally means you must call this before the call to print_header.
If you ask for a particular skip link to be printed, it is then your responsibility to ensure that the appropriate tag is printed in the body of the page, so that the skip link goes somewhere.
Even if a particular skip link is requested more than once, only one copy of it will be output.
- Parameters
-
string | $target | the name of anchor this link should go to. For example 'maincontent'. |
string | $linktext | The text to use for the skip link. Normally get_string('skipto', 'access', ...); |
page_requirements_manager::string_for_js |
( |
| $identifier, |
|
|
| $component, |
|
|
| $a = null ) |
Make a language string available to JavaScript.
All the strings will be available in a M.str object in the global namespace. So, for example, after a call to $PAGE->requires->string_for_js('course', 'moodle'); then the JavaScript variable M.str.moodle.course will be 'Course', or the equivalent in the current language.
The arguments to this function are just like the arguments to get_string except that $component is not optional, and there are some aspects to consider when the string contains {$a} placeholder.
If the string does not contain any {$a} placeholder, you can simply use M.str.component.identifier to obtain it. If you prefer, you can call M.util.get_string(identifier, component) to get the same result.
If you need to use {$a} placeholders, there are two options. Either the placeholder should be substituted in PHP on server side or it should be substituted in Javascript at client side.
To substitute the placeholder at server side, just provide the required value for the placeholder when you require the string. Because each string is only stored once in the JavaScript (based on $identifier and $module) you cannot get the same string with two different values of $a. If you try, an exception will be thrown. Once the placeholder is substituted, you can use M.str or M.util.get_string() as shown above:
// Require the string in PHP and replace the placeholder.
$PAGE->requires->string_for_js('fullnamedisplay', 'moodle', $USER);
// Use the result of the substitution in Javascript.
alert(M.str.moodle.fullnamedisplay);
To substitute the placeholder at client side, use M.util.get_string() function. It implements the same logic as get_string():
// Require the string in PHP but keep {$a} as it is.
$PAGE->requires->string_for_js('fullnamedisplay', 'moodle');
// Provide the values on the fly in Javascript.
user = { firstname : 'Harry', lastname : 'Potter' }
alert(M.util.get_string('fullnamedisplay', 'moodle', user);
If you do need the same string expanded with different $a values in PHP on server side, then the solution is to put them in your own data structure (e.g. and array) that you pass to JavaScript with data_for_js().
- Parameters
-
string | $identifier | the desired string. |
string | $component | the language file to look in. |
mixed | $a | any extra data to add into the string (optional). |
page_requirements_manager::strings_for_js |
( |
| $identifiers, |
|
|
| $component, |
|
|
| $a = null ) |
Make an array of language strings available for JS.
This function calls the above function string_for_js() for each requested string in the $identifiers array that is passed to the argument for a single module passed in $module.
$PAGE->requires->strings_for_js(array('one', 'two', 'three'), 'mymod', array('a', null, 3));
// The above is identical to calling:
$PAGE->requires->string_for_js('one', 'mymod', 'a'); $PAGE->requires->string_for_js('two', 'mymod'); $PAGE->requires->string_for_js('three', 'mymod', 3);
- Parameters
-
array | $identifiers | An array of desired strings |
string | $component | The module to load for |
mixed | $a | This can either be a single variable that gets passed as extra information for every string or it can be an array of mixed data where the key for the data matches that of the identifier it is meant for. |