|
Moodle
2.2.1
http://www.collinsharper.com
|
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_init_call('M.mod_forum.init_view');
It also supports obsoleted coding style withouth YUI3 modules.
$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.
Definition at line 59 of file outputrequirementslib.php.
| __construct | ( | ) |
Page requirements constructor.
Definition at line 118 of file outputrequirementslib.php.

| add_yui2_modules | ( | ) | [protected] |
This method adds yui2 modules into the yui3 JS loader-
Definition at line 211 of file outputrequirementslib.php.


| 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.
| string | $stylesheet | The path to the .css file, relative to $CFG->wwwroot. For example: $PAGE->requires->css('mod/data/css.php?d='.$data->id); |
Definition at line 592 of file outputrequirementslib.php.
| css_theme | ( | moodle_url $ | stylesheet | ) |
Add theme stylkesheet to page - do not use from plugin code, this should be called only from the core renderer!
| moodle_url | $stylesheet |
Definition at line 616 of file outputrequirementslib.php.
| data_for_js | ( | $ | variable, |
| $ | data, | ||
| $ | inhead = false |
||
| ) |
!!!!!!DEPRECATED!!!!!! please use js_init_call() for everything now.
Make some data from PHP available to JavaScript code.
For example, if you call
$PAGE->requires->data_for_js('mydata', array('name' => 'Moodle'));
then in JavsScript mydata.name will be 'Moodle'.
| string | $variable | the the name of the JavaScript variable to assign the data to. Will probably work if you use a compound name like 'mybuttons.button[1]', but this should be considered an experimental feature. |
| mixed | $data | The data to pass to JavaScript. This will be escaped using json_encode, so passing objects and arrays should work. |
| bool | $inhead | initialise in head |
Definition at line 890 of file outputrequirementslib.php.
| event_handler | ( | $ | selector, |
| $ | event, | ||
| $ | function, | ||
| array $ | arguments = null |
||
| ) |
Creates a YUI event handler.
| mixed | $selector | standard YUI selector for elemnts, may be array or string, element id is in the form "#idvalue" |
| string | $event | A valid DOM event (click, mousedown, change etc.) |
| string | $function | The name of the function to call |
| array | $arguments | An optional array of argument parameters to pass to the function |
Definition at line 904 of file outputrequirementslib.php.

| find_module | ( | $ | component | ) | [protected] |
Find out if JS module present and return details.
| string | $component | name of component in frankenstyle, ex: core_group, mod_forum |
Definition at line 386 of file outputrequirementslib.php.


| get_css_code | ( | ) | [protected] |
Returns html tags needed for inclusion of theme CSS
Definition at line 1027 of file outputrequirementslib.php.


| get_end_code | ( | ) |
Generate any HTML that needs to go at the end of the page.
Normally, this method is called automatically by the code that prints the page footer. You should not normally need to call it in your own code.
Definition at line 1158 of file outputrequirementslib.php.

| get_event_handler_code | ( | ) | [protected] |
Returns code needed for registering of event handlers.
Definition at line 912 of file outputrequirementslib.php.


| get_extra_modules_code | ( | ) | [protected] |
Adds extra modules specified after printing of page header
Definition at line 1056 of file outputrequirementslib.php.


| get_head_code | ( | moodle_page $ | page, |
| core_renderer $ | renderer | ||
| ) |
Generate any HTML that needs to go inside the <head> tag.
Normally, this method is called automatically by the code that prints the <head> tag. You should not normally need to call it in your own code.
| moodle_page | $page | |
| core_renderer | $renderer |
Definition at line 1073 of file outputrequirementslib.php.

| get_javascript_code | ( | $ | ondomready | ) | [protected] |
Get the inline JavaScript code that need to appear in a particular place.
| bool | $ondomready |
Definition at line 925 of file outputrequirementslib.php.


| get_javascript_init_code | ( | ) | [protected] |
Returns js code to be executed when Y is available.
Definition at line 943 of file outputrequirementslib.php.

Returns the stacktraces from loading js modules.
Definition at line 568 of file outputrequirementslib.php.
Generate any HTML that needs to go at the start of the <body> tag.
Normally, this method is called automatically by the code that prints the <head> tag. You should not normally need to call it in your own code.
Definition at line 1134 of file outputrequirementslib.php.

| get_yui2lib_code | ( | ) |
Returns basic YUI2 JS loading code. It can be called manually at any time. If called manually the result needs to be output using echo().
Major benefit of this compared to standard js loader is much improved caching, better browser cache utilisation, much fewer http requests.
All YUI2 CSS is loaded automatically.
Definition at line 1002 of file outputrequirementslib.php.


| get_yui3lib_headcode | ( | ) | [protected] |
Returns basic YUI3 JS loading code. YUI3 is using autoloading of both CSS and JS code.
Major benefit of this compared to standard js/csss loader is much improved caching, better browser cache utilisation, much fewer http requests.
Definition at line 959 of file outputrequirementslib.php.

| init_requirements_data | ( | moodle_page $ | page, |
| core_renderer $ | renderer | ||
| ) | [protected] |
Initialise with the bits of JavaScript that every Moodle page should have.
| moodle_page | $page | |
| core_renderer | $renderer |
Definition at line 273 of file outputrequirementslib.php.


| is_head_done | ( | ) |
Definition at line 1204 of file outputrequirementslib.php.
Definition at line 1211 of file outputrequirementslib.php.
| js | ( | $ | url, |
| $ | inhead = false |
||
| ) |
Ensure that the specified JavaScript file is linked to from this page.
NOTE: This function is to be used in rare cases only, please store your JS in module.js file and use $PAGE->requires->js_init_call() instead.
By default the link is put at the end of the page, since this gives best page-load performance.
Even if a particular script is requested more than once, it will only be linked to once.
| string | moodle_url | $url | The path to the .js file, relative to $CFG->dirroot / $CFG->wwwroot. For example '/mod/mymod/customscripts.js'; use moodle_url for external scripts |
| bool | $inhead | initialise in head |
Definition at line 324 of file outputrequirementslib.php.

| js_fix_url | ( | $ | url | ) | [protected] |
Returns the actual url through which a script is served.
| moodle_url | string | $url | full moodle url, or shortened path to script |
Definition at line 359 of file outputrequirementslib.php.


| 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.
By default the call will be put in a script tag at the end of the page after initialising Y instance, since this gives best page-load performance and allows you to use YUI3 library.
If you request that a particular function is called several times, then that is what will happen (unlike linking to a CSS or JS file, where only one link will be output).
The main benefit of the method is the automatic encoding of all function parameters.
| string | $function | the name of the JavaScritp function to call. Can be a compound name like 'Y.Event.purgeElement'. Can also be used to create and object by using a 'function name' like 'new user_selector'. |
| array | $arguments | and array of arguments to be passed to the function. When generating the function call, this will be escaped using json_encode, so passing objects and arrays should work. |
| bool | $ondomready | |
| int | $delay |
Definition at line 668 of file outputrequirementslib.php.
| js_gallery_module | ( | $ | modules, |
| $ | version, | ||
| $ | function, | ||
| array $ | arguments = null, |
||
| $ | ondomready = false |
||
| ) |
Adds a call to make use of a YUI gallery module. DEPRECATED DO NOT USE!!!
| string | array | $modules | One or more gallery modules to require |
| string | $version | |
| string | $function | |
| array | $arguments | |
| bool | $ondomready |
Definition at line 684 of file outputrequirementslib.php.

| 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.
| string | $function | the name of the JavaScritp function to with init code, usually something like 'M.mod_mymodule.init' |
| array | $extraarguments | and array of arguments to be passed to the function. The first argument is always the YUI3 Y instance with all required dependencies already loaded. |
| bool | $ondomready | wait for dom ready (helps with some IE problems when modifying DOM) |
| array | $module | JS module specification array |
Definition at line 738 of file outputrequirementslib.php.


| js_init_code | ( | $ | jscode, |
| $ | ondomready = false, |
||
| array $ | module = null |
||
| ) |
Add short static javascript code fragment to page footer. This is intended primarily for loading of js modules and initialising page layout. Ideally the JS code fragment should be stored in plugin renderer so that themes may override it.
| string | $jscode | |
| bool | $ondomready | wait for dom ready (helps with some IE problems when modifying DOM) |
| array | $module | JS module specification array |
Definition at line 760 of file outputrequirementslib.php.


| js_module | ( | $ | module | ) |
Append YUI3 module to default YUI3 JS loader. The structure of module array is described at http://developer.yahoo.com/yui/3/yui/:
| string | array | $module | name of module (details are autodetected), or full module specification as array |
Definition at line 489 of file outputrequirementslib.php.


| js_module_loaded | ( | $ | module | ) | [protected] |
Returns true if the module has already been loaded.
| string | array | $module |
Definition at line 554 of file outputrequirementslib.php.

| 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.
| $target | the name of anchor this link should go to. For example 'maincontent'. |
| $linktext | The text to use for the skip link. Normally get_string('skipto', 'access', ...); |
Definition at line 635 of file outputrequirementslib.php.


| 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 {
// 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().
| string | $identifier | the desired string. |
| string | $component | the language file to look in. |
| mixed | $a | any extra data to add into the string (optional). |
Definition at line 825 of file outputrequirementslib.php.

| 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 identitical 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);
| 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. |
Definition at line 861 of file outputrequirementslib.php.

| yui2_lib | ( | $ | libname | ) |
Ensure that the specified YUI2 library file, and all its required dependencies, are linked to from this page.
By default the link is put at the end of the page, since this gives best page-load performance. Optional dependencies are not loaded automatically - if you want them you will need to load them first with other calls to this method.
Even if a particular library is requested more than once (perhaps as a dependency of other libraries) it will only be linked to once.
The library is leaded as soon as possible, if $OUTPUT->header() not used yet it is put into the page header, otherwise it is loaded in the page footer.
| string | array | $libname | the name of the YUI2 library you require. For example 'autocomplete'. |
Definition at line 347 of file outputrequirementslib.php.

| yui_module | ( | $ | modules, |
| $ | function, | ||
| array $ | arguments = null, |
||
| $ | galleryversion = '2010.04.08-12-35', |
||
| $ | ondomready = false |
||
| ) |
Creates a JavaScript function call that requires one or more modules to be loaded
This function can be used to include all of the standard YUI module types within JavaScript:
| array | string | $modules | One or more modules |
| string | $function | The function to call once modules have been loaded |
| array | $arguments | An array of arguments to pass to the function |
| string | $galleryversion | The gallery version to use |
| bool | $ondomready |
Definition at line 705 of file outputrequirementslib.php.


$cssthemeurls = array() [protected] |
Definition at line 82 of file outputrequirementslib.php.
$cssurls = array() [protected] |
Definition at line 88 of file outputrequirementslib.php.
$debug_moduleloadstacktraces = array() [protected] |
stores debug backtraces from when JS modules were included in the page
Definition at line 113 of file outputrequirementslib.php.
$eventhandlers = array() [protected] |
Definition at line 93 of file outputrequirementslib.php.
$extramodules = array() [protected] |
Definition at line 98 of file outputrequirementslib.php.
$headdone = false [protected] |
Flag indicated head stuff already printed
Definition at line 100 of file outputrequirementslib.php.
List of needed function calls
Definition at line 67 of file outputrequirementslib.php.
Included JS scripts
Definition at line 65 of file outputrequirementslib.php.
$jsinitcode = array() [protected] |
Definition at line 77 of file outputrequirementslib.php.
List of JS variables to be initialised
Definition at line 63 of file outputrequirementslib.php.
$M_cfg [protected] |
some config vars exposed in JS, please no secret stuff there
Definition at line 111 of file outputrequirementslib.php.
$M_yui_loader [protected] |
YUI loader information for YUI3 loading from javascript
Definition at line 109 of file outputrequirementslib.php.
$skiplinks = array() [protected] |
Definition at line 72 of file outputrequirementslib.php.
$stringsforjs = array() [protected] |
List of string available from JS
Definition at line 61 of file outputrequirementslib.php.
$topofbodydone = false [protected] |
Flag indicating top of body already printed
Definition at line 102 of file outputrequirementslib.php.
$yui2loader [protected] |
YUI PHPLoader instance responsible for YUI2 loading from PHP only
Definition at line 105 of file outputrequirementslib.php.
$yui3loader [protected] |
YUI PHPLoader instance responsible for YUI3 loading from PHP only
Definition at line 107 of file outputrequirementslib.php.