Moodle  2.2.1
http://www.collinsharper.com
page_requirements_manager Class Reference

Public Member Functions

 __construct ()
 js ($url, $inhead=false)
 yui2_lib ($libname)
 js_module ($module)
 get_loaded_modules ()
 css ($stylesheet)
 css_theme (moodle_url $stylesheet)
 skip_link_to ($target, $linktext)
 js_function_call ($function, array $arguments=null, $ondomready=false, $delay=0)
 js_gallery_module ($modules, $version, $function, array $arguments=null, $ondomready=false)
 yui_module ($modules, $function, array $arguments=null, $galleryversion= '2010.04.08-12-35', $ondomready=false)
 js_init_call ($function, array $extraarguments=null, $ondomready=false, array $module=null)
 js_init_code ($jscode, $ondomready=false, array $module=null)
 string_for_js ($identifier, $component, $a=NULL)
 strings_for_js ($identifiers, $component, $a=NULL)
 data_for_js ($variable, $data, $inhead=false)
 event_handler ($selector, $event, $function, array $arguments=null)
 get_yui2lib_code ()
 get_head_code (moodle_page $page, core_renderer $renderer)
 get_top_of_body_code ()
 get_end_code ()
 is_head_done ()
 is_top_of_body_done ()

Protected Member Functions

 add_yui2_modules ()
 init_requirements_data (moodle_page $page, core_renderer $renderer)
 js_fix_url ($url)
 find_module ($component)
 js_module_loaded ($module)
 get_event_handler_code ()
 get_javascript_code ($ondomready)
 get_javascript_init_code ()
 get_yui3lib_headcode ()
 get_css_code ()
 get_extra_modules_code ()

Protected Attributes

 $stringsforjs = array()
 $jsinitvariables = array('head'=>array(), 'footer'=>array())
 $jsincludes = array('head'=>array(), 'footer'=>array())
 $jscalls = array('normal'=>array(), 'ondomready'=>array())
 $skiplinks = array()
 $jsinitcode = array()
 $cssthemeurls = array()
 $cssurls = array()
 $eventhandlers = array()
 $extramodules = array()
 $headdone = false
 $topofbodydone = false
 $yui2loader
 $yui3loader
 $M_yui_loader
 $M_cfg
 $debug_moduleloadstacktraces = array()

Detailed Description

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.

Since:
Moodle 2.0

Definition at line 59 of file outputrequirementslib.php.


Constructor & Destructor Documentation

Page requirements constructor.

Definition at line 118 of file outputrequirementslib.php.

Here is the call graph for this function:


Member Function Documentation

add_yui2_modules ( ) [protected]

This method adds yui2 modules into the yui3 JS loader-

Returns:
void

Definition at line 211 of file outputrequirementslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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$stylesheetThe 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!

Parameters:
moodle_url$stylesheet
Returns:
void

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'.

Parameters:
string$variablethe 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$dataThe data to pass to JavaScript. This will be escaped using json_encode, so passing objects and arrays should work.
bool$inheadinitialise in head
Returns:
void

Definition at line 890 of file outputrequirementslib.php.

event_handler ( selector,
event,
function,
array arguments = null 
)

Creates a YUI event handler.

Parameters:
mixed$selectorstandard YUI selector for elemnts, may be array or string, element id is in the form "#idvalue"
string$eventA valid DOM event (click, mousedown, change etc.)
string$functionThe name of the function to call
array$argumentsAn optional array of argument parameters to pass to the function
Returns:
void

Definition at line 904 of file outputrequirementslib.php.

Here is the caller graph for this function:

find_module ( component) [protected]

Find out if JS module present and return details.

Parameters:
string$componentname of component in frankenstyle, ex: core_group, mod_forum
Returns:
array description of module or null if not found

Definition at line 386 of file outputrequirementslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

get_css_code ( ) [protected]

Returns html tags needed for inclusion of theme CSS

Returns:
string

Definition at line 1027 of file outputrequirementslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Returns:
string the HTML code to to at the end of the page.

Definition at line 1158 of file outputrequirementslib.php.

Here is the call graph for this function:

get_event_handler_code ( ) [protected]

Returns code needed for registering of event handlers.

Returns:
string JS code

Definition at line 912 of file outputrequirementslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

get_extra_modules_code ( ) [protected]

Adds extra modules specified after printing of page header

Returns:
string

Definition at line 1056 of file outputrequirementslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Parameters:
moodle_page$page
core_renderer$renderer
Returns:
string the HTML code to to inside the <head> tag.

Definition at line 1073 of file outputrequirementslib.php.

Here is the call graph for this function:

get_javascript_code ( ondomready) [protected]

Get the inline JavaScript code that need to appear in a particular place.

Parameters:
bool$ondomready
Returns:
string

Definition at line 925 of file outputrequirementslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

get_javascript_init_code ( ) [protected]

Returns js code to be executed when Y is available.

Returns:
unknown_type

Definition at line 943 of file outputrequirementslib.php.

Here is the caller graph for this function:

Returns the stacktraces from loading js modules.

Returns:
array

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.

Returns:
string the HTML code to go at the start of the <body> tag.

Definition at line 1134 of file outputrequirementslib.php.

Here is the call graph for this function:

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.

Returns:
string JS embedding code

Definition at line 1002 of file outputrequirementslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Returns:
string

Definition at line 959 of file outputrequirementslib.php.

Here is the caller graph for this function:

init_requirements_data ( moodle_page page,
core_renderer renderer 
) [protected]

Initialise with the bits of JavaScript that every Moodle page should have.

Parameters:
moodle_page$page
core_renderer$renderer

Definition at line 273 of file outputrequirementslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

Returns:
boolean Have we already output the code in the <head> tag?

Definition at line 1204 of file outputrequirementslib.php.

Returns:
boolean Have we already output the code at the start of the <body> tag?

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.

Parameters:
string | moodle_url$urlThe path to the .js file, relative to $CFG->dirroot / $CFG->wwwroot. For example '/mod/mymod/customscripts.js'; use moodle_url for external scripts
bool$inheadinitialise in head
Returns:
void

Definition at line 324 of file outputrequirementslib.php.

Here is the call graph for this function:

js_fix_url ( url) [protected]

Returns the actual url through which a script is served.

Parameters:
moodle_url | string$urlfull moodle url, or shortened path to script
Returns:
moodle_url

Definition at line 359 of file outputrequirementslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Parameters:
string$functionthe 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$argumentsand 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
Returns:
void

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!!!

Deprecated:
DO NOT USE
Parameters:
string | array$modulesOne or more gallery modules to require
string$version
string$function
array$arguments
bool$ondomready

Definition at line 684 of file outputrequirementslib.php.

Here is the call graph for this function:

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.

Parameters:
string$functionthe name of the JavaScritp function to with init code, usually something like 'M.mod_mymodule.init'
array$extraargumentsand 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$ondomreadywait for dom ready (helps with some IE problems when modifying DOM)
array$moduleJS module specification array
Returns:
void

Definition at line 738 of file outputrequirementslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Parameters:
string$jscode
bool$ondomreadywait for dom ready (helps with some IE problems when modifying DOM)
array$moduleJS module specification array
Returns:
void

Definition at line 760 of file outputrequirementslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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/:

Parameters:
string | array$modulename of module (details are autodetected), or full module specification as array
Returns:
void

Definition at line 489 of file outputrequirementslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

js_module_loaded ( module) [protected]

Returns true if the module has already been loaded.

Parameters:
string | array$module
Returns:
bool True if the module has already been loaded

Definition at line 554 of file outputrequirementslib.php.

Here is the caller graph for this function:

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:
$targetthe name of anchor this link should go to. For example 'maincontent'.
$linktextThe text to use for the skip link. Normally get_string('skipto', 'access', ...);

Definition at line 635 of file outputrequirementslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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 {

See also:
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$identifierthe desired string.
string$componentthe language file to look in.
mixed$aany extra data to add into the string (optional).

Definition at line 825 of file outputrequirementslib.php.

Here is the caller graph for this function:

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);

Parameters:
array$identifiersAn array of desired strings
string$componentThe module to load for
mixed$aThis 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.

Here is the call graph for this function:

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.

Parameters:
string | array$libnamethe name of the YUI2 library you require. For example 'autocomplete'.
Returns:
void

Definition at line 347 of file outputrequirementslib.php.

Here is the caller graph for this function:

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:

  • YUI3 modules [node, event, io]
  • YUI2 modules [yui2-*]
  • Moodle modules [moodle-*]
  • Gallery modules [gallery-*]
Parameters:
array | string$modulesOne or more modules
string$functionThe function to call once modules have been loaded
array$argumentsAn array of arguments to pass to the function
string$galleryversionThe gallery version to use
bool$ondomready

Definition at line 705 of file outputrequirementslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:


Field Documentation

$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.

$jscalls = array('normal'=>array(), 'ondomready'=>array()) [protected]

List of needed function calls

Definition at line 67 of file outputrequirementslib.php.

$jsincludes = array('head'=>array(), 'footer'=>array()) [protected]

Included JS scripts

Definition at line 65 of file outputrequirementslib.php.

$jsinitcode = array() [protected]

Definition at line 77 of file outputrequirementslib.php.

$jsinitvariables = array('head'=>array(), 'footer'=>array()) [protected]

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.


The documentation for this class was generated from the following file:
 All Data Structures Namespaces Files Functions Variables Enumerations