MDL-40759 icons: Refactor to allow theme icon systems
[moodle.git] / lib / outputrequirementslib.php
CommitLineData
0bb38e8c 1<?php
0bb38e8c
PS
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
0bb38e8c
PS
17/**
18 * Library functions to facilitate the use of JavaScript in Moodle.
19 *
48d4fad1
SH
20 * Note: you can find history of this file in lib/ajax/ajaxlib.php
21 *
22 * @copyright 2009 Tim Hunt, 2010 Petr Skoda
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
f8129210 24 * @package core
76be40cc 25 * @category output
0bb38e8c
PS
26 */
27
78bfb562
PS
28defined('MOODLE_INTERNAL') || die();
29
0bb38e8c
PS
30/**
31 * This class tracks all the things that are needed by the current page.
32 *
33 * Normally, the only instance of this class you will need to work with is the
34 * one accessible via $PAGE->requires.
35 *
173ba4f1 36 * Typical usage would be
0bb38e8c 37 * <pre>
adeb96d2 38 * $PAGE->requires->js_call_amd('mod_forum/view', 'init');
0bb38e8c
PS
39 * </pre>
40 *
adeb96d2 41 * It also supports obsoleted coding style with/without YUI3 modules.
0bb38e8c 42 * <pre>
adeb96d2 43 * $PAGE->requires->js_init_call('M.mod_forum.init_view');
173ba4f1 44 * $PAGE->requires->css('/mod/mymod/userstyles.php?id='.$id); // not overridable via themes!
0bb38e8c
PS
45 * $PAGE->requires->js('/mod/mymod/script.js');
46 * $PAGE->requires->js('/mod/mymod/small_but_urgent.js', true);
47 * $PAGE->requires->js_function_call('init_mymod', array($data), true);
48 * </pre>
49 *
50 * There are some natural restrictions on some methods. For example, {@link css()}
51 * can only be called before the <head> tag is output. See the comments on the
52 * individual methods for details.
53 *
54 * @copyright 2009 Tim Hunt, 2010 Petr Skoda
48d4fad1 55 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
0bb38e8c 56 * @since Moodle 2.0
f8129210 57 * @package core
76be40cc 58 * @category output
0bb38e8c
PS
59 */
60class page_requirements_manager {
48d4fad1
SH
61
62 /**
76be40cc 63 * @var array List of string available from JS
48d4fad1 64 */
0bb38e8c 65 protected $stringsforjs = array();
48d4fad1 66
af225f6c
67 /**
68 * @var array List of get_string $a parameters - used for validation only.
69 */
70 protected $stringsforjs_as = array();
71
48d4fad1 72 /**
76be40cc 73 * @var array List of JS variables to be initialised
48d4fad1 74 */
0bb38e8c 75 protected $jsinitvariables = array('head'=>array(), 'footer'=>array());
48d4fad1
SH
76
77 /**
76be40cc 78 * @var array Included JS scripts
48d4fad1 79 */
0bb38e8c 80 protected $jsincludes = array('head'=>array(), 'footer'=>array());
48d4fad1 81
adeb96d2
DW
82 /**
83 * @var array Inline scripts using RequireJS module loading.
84 */
85 protected $amdjscode = array('');
86
48d4fad1 87 /**
76be40cc 88 * @var array List of needed function calls
48d4fad1 89 */
0bb38e8c 90 protected $jscalls = array('normal'=>array(), 'ondomready'=>array());
48d4fad1 91
0bb38e8c 92 /**
76be40cc 93 * @var array List of skip links, those are needed for accessibility reasons
0bb38e8c
PS
94 */
95 protected $skiplinks = array();
48d4fad1 96
0bb38e8c 97 /**
76be40cc
SH
98 * @var array Javascript code used for initialisation of page, it should
99 * be relatively small
0bb38e8c
PS
100 */
101 protected $jsinitcode = array();
48d4fad1 102
0bb38e8c 103 /**
76be40cc 104 * @var array of moodle_url Theme sheets, initialised only from core_renderer
0bb38e8c
PS
105 */
106 protected $cssthemeurls = array();
76be40cc 107
0bb38e8c 108 /**
76be40cc 109 * @var array of moodle_url List of custom theme sheets, these are strongly discouraged!
0bb38e8c 110 * Useful mostly only for CSS submitted by teachers that is not part of the theme.
0bb38e8c
PS
111 */
112 protected $cssurls = array();
48d4fad1 113
0bb38e8c 114 /**
76be40cc 115 * @var array List of requested event handlers
0bb38e8c
PS
116 */
117 protected $eventhandlers = array();
48d4fad1 118
0bb38e8c 119 /**
76be40cc 120 * @var array Extra modules
0bb38e8c
PS
121 */
122 protected $extramodules = array();
48d4fad1 123
b8459bba
TH
124 /**
125 * @var array trackes the names of bits of HTML that are only required once
126 * per page. See {@link has_one_time_item_been_created()},
127 * {@link set_one_time_item_created()} and {@link should_create_one_time_item_now()}.
128 */
129 protected $onetimeitemsoutput = array();
130
48d4fad1 131 /**
76be40cc 132 * @var bool Flag indicated head stuff already printed
48d4fad1 133 */
0bb38e8c 134 protected $headdone = false;
48d4fad1
SH
135
136 /**
76be40cc 137 * @var bool Flag indicating top of body already printed
48d4fad1 138 */
0bb38e8c
PS
139 protected $topofbodydone = false;
140
48d4fad1 141 /**
76be40cc 142 * @var stdClass YUI PHPLoader instance responsible for YUI3 loading from PHP only
48d4fad1 143 */
0bb38e8c 144 protected $yui3loader;
48d4fad1
SH
145
146 /**
15dedb11 147 * @var YUI_config default YUI loader configuration
48d4fad1 148 */
2f422271 149 protected $YUI_config;
48d4fad1 150
2300a312
AN
151 /**
152 * @var array $yuicssmodules
153 */
154 protected $yuicssmodules = array();
155
48d4fad1 156 /**
76be40cc 157 * @var array Some config vars exposed in JS, please no secret stuff there
48d4fad1 158 */
0bb38e8c 159 protected $M_cfg;
48d4fad1 160
63c88397
PS
161 /**
162 * @var array list of requested jQuery plugins
163 */
164 protected $jqueryplugins = array();
165
166 /**
167 * @var array list of jQuery plugin overrides
168 */
169 protected $jquerypluginoverrides = array();
170
0bb38e8c
PS
171 /**
172 * Page requirements constructor.
173 */
174 public function __construct() {
175 global $CFG;
176
7953149e
PS
177 // You may need to set up URL rewrite rule because oversized URLs might not be allowed by web server.
178 $sep = empty($CFG->yuislasharguments) ? '?' : '/';
6e7b4601 179
ef7329dc 180 $this->yui3loader = new stdClass();
cae21a32 181 $this->YUI_config = new YUI_config();
0bb38e8c 182
1e31f118 183 if (is_https()) {
aaee690b
PS
184 // On HTTPS sites all JS must be loaded from https sites,
185 // YUI CDN does not support https yet, sorry.
186 $CFG->useexternalyui = 0;
187 }
188
a33e7407 189 // Set up some loader options.
aaee690b
PS
190 $this->yui3loader->local_base = $CFG->httpswwwroot . '/lib/yuilib/'. $CFG->yui3version . '/';
191 $this->yui3loader->local_comboBase = $CFG->httpswwwroot . '/theme/yui_combo.php'.$sep;
192
193 if (!empty($CFG->useexternalyui)) {
194 $this->yui3loader->base = 'http://yui.yahooapis.com/' . $CFG->yui3version . '/';
0bb38e8c 195 $this->yui3loader->comboBase = 'http://yui.yahooapis.com/combo?';
0bb38e8c 196 } else {
aaee690b
PS
197 $this->yui3loader->base = $this->yui3loader->local_base;
198 $this->yui3loader->comboBase = $this->yui3loader->local_comboBase;
0bb38e8c
PS
199 }
200
a33e7407 201 // Enable combo loader? This significantly helps with caching and performance!
0bb38e8c 202 $this->yui3loader->combine = !empty($CFG->yuicomboloading);
0bb38e8c 203
c8b9f6d9 204 $jsrev = $this->get_jsrev();
f312e878 205
a33e7407 206 // Set up JS YUI loader helper object.
2f422271
PS
207 $this->YUI_config->base = $this->yui3loader->base;
208 $this->YUI_config->comboBase = $this->yui3loader->comboBase;
209 $this->YUI_config->combine = $this->yui3loader->combine;
b3c78403 210
aea29737
AN
211 // If we've had to patch any YUI modules between releases, we must override the YUI configuration to include them.
212 // For important information on patching YUI modules, please see http://docs.moodle.org/dev/YUI/Patching.
213 if (!empty($CFG->yuipatchedmodules) && !empty($CFG->yuipatchlevel)) {
214 $this->YUI_config->define_patched_core_modules($this->yui3loader->local_comboBase,
215 $CFG->yui3version,
216 $CFG->yuipatchlevel,
217 $CFG->yuipatchedmodules);
218 }
219
7cfe3ebb 220 $configname = $this->YUI_config->set_config_source('lib/yui/config/yui2.js');
cae21a32 221 $this->YUI_config->add_group('yui2', array(
a33e7407 222 // Loader configuration for our 2in3, for now ignores $CFG->useexternalyui.
cae21a32
ARN
223 'base' => $CFG->httpswwwroot . '/lib/yuilib/2in3/' . $CFG->yui2version . '/build/',
224 'comboBase' => $CFG->httpswwwroot . '/theme/yui_combo.php'.$sep,
225 'combine' => $this->yui3loader->combine,
226 'ext' => false,
227 'root' => '2in3/' . $CFG->yui2version .'/build/',
228 'patterns' => array(
229 'yui2-' => array(
230 'group' => 'yui2',
b3c78403 231 'configFn' => $configname,
1c76d55a 232 )
2b722f87 233 )
cae21a32 234 ));
7cfe3ebb 235 $configname = $this->YUI_config->set_config_source('lib/yui/config/moodle.js');
cae21a32
ARN
236 $this->YUI_config->add_group('moodle', array(
237 'name' => 'moodle',
03e36e70 238 'base' => $CFG->httpswwwroot . '/theme/yui_combo.php' . $sep . 'm/' . $jsrev . '/',
cae21a32
ARN
239 'combine' => $this->yui3loader->combine,
240 'comboBase' => $CFG->httpswwwroot . '/theme/yui_combo.php'.$sep,
241 'ext' => false,
03e36e70 242 'root' => 'm/'.$jsrev.'/', // Add the rev to the root path so that we can control caching.
cae21a32
ARN
243 'patterns' => array(
244 'moodle-' => array(
245 'group' => 'moodle',
b3c78403 246 'configFn' => $configname,
cae21a32
ARN
247 )
248 )
249 ));
a33e7407 250
5b49f8b3
AN
251 $this->YUI_config->add_group('gallery', array(
252 'name' => 'gallery',
253 'base' => $CFG->httpswwwroot . '/lib/yuilib/gallery/',
254 'combine' => $this->yui3loader->combine,
255 'comboBase' => $CFG->httpswwwroot . '/theme/yui_combo.php' . $sep,
256 'ext' => false,
257 'root' => 'gallery/' . $jsrev . '/',
258 'patterns' => array(
259 'gallery-' => array(
260 'group' => 'gallery',
261 )
262 )
263 ));
264
0872e0e9 265 // Set some more loader options applying to groups too.
96f81ea3 266 if ($CFG->debugdeveloper) {
0872e0e9
ARN
267 // When debugging is enabled, we want to load the non-minified (RAW) versions of YUI library modules rather
268 // than the DEBUG versions as these generally generate too much logging for our purposes.
269 // However we do want the DEBUG versions of our Moodle-specific modules.
270 // To debug a YUI-specific issue, change the yui3loader->filter value to DEBUG.
271 $this->YUI_config->filter = 'RAW';
272 $this->YUI_config->groups['moodle']['filter'] = 'DEBUG';
273
274 // We use the yui3loader->filter setting when writing the YUI3 seed scripts into the header.
275 $this->yui3loader->filter = $this->YUI_config->filter;
276 $this->YUI_config->debug = true;
277 } else {
278 $this->yui3loader->filter = null;
ed67f462 279 $this->YUI_config->groups['moodle']['filter'] = null;
0872e0e9
ARN
280 $this->YUI_config->debug = false;
281 }
282
fa64202a
AN
283 // Include the YUI config log filters.
284 if (!empty($CFG->yuilogexclude) && is_array($CFG->yuilogexclude)) {
285 $this->YUI_config->logExclude = $CFG->yuilogexclude;
286 }
287 if (!empty($CFG->yuiloginclude) && is_array($CFG->yuiloginclude)) {
288 $this->YUI_config->logInclude = $CFG->yuiloginclude;
289 }
290 if (!empty($CFG->yuiloglevel)) {
291 $this->YUI_config->logLevel = $CFG->yuiloglevel;
292 }
293
aa135c4d 294 // Add the moodle group's module data.
da5e59b9 295 $this->YUI_config->add_moodle_metadata();
aa135c4d 296
a33e7407 297 // Every page should include definition of following modules.
984915af 298 $this->js_module($this->find_module('core_filepicker'));
23381ab8 299 $this->js_module($this->find_module('core_comment'));
0bb38e8c
PS
300 }
301
9bdcf579
DW
302 /**
303 * Return the safe config values that get set for javascript in "M.cfg".
304 *
305 * @since 2.9
306 * @return array List of safe config values that are available to javascript.
307 */
308 public function get_config_for_javascript(moodle_page $page, renderer_base $renderer) {
309 global $CFG;
310
311 if (empty($this->M_cfg)) {
312 // JavaScript should always work with $CFG->httpswwwroot rather than $CFG->wwwroot.
313 // Otherwise, in some situations, users will get warnings about insecure content
314 // on secure pages from their web browser.
315
e330b1c2
DW
316 $iconsystem = \core\output\icon_system::instance();
317
9bdcf579
DW
318 $this->M_cfg = array(
319 'wwwroot' => $CFG->httpswwwroot, // Yes, really. See above.
320 'sesskey' => sesskey(),
9bdcf579
DW
321 'themerev' => theme_get_revision(),
322 'slasharguments' => (int)(!empty($CFG->slasharguments)),
323 'theme' => $page->theme->name,
e330b1c2 324 'iconsystemmodule' => $iconsystem->get_amd_name(),
9bdcf579
DW
325 'jsrev' => $this->get_jsrev(),
326 'admin' => $CFG->admin,
b61015cf
RW
327 'svgicons' => $page->theme->use_svg_icons(),
328 'usertimezone' => usertimezone(),
329 'contextid' => $page->context->id,
9bdcf579
DW
330 );
331 if ($CFG->debugdeveloper) {
332 $this->M_cfg['developerdebug'] = true;
333 }
334 if (defined('BEHAT_SITE_RUNNING')) {
335 $this->M_cfg['behatsiterunning'] = true;
336 }
337
338 }
339 return $this->M_cfg;
340 }
341
0bb38e8c
PS
342 /**
343 * Initialise with the bits of JavaScript that every Moodle page should have.
344 *
345 * @param moodle_page $page
7edf3f7e 346 * @param core_renderer $renderer
0bb38e8c
PS
347 */
348 protected function init_requirements_data(moodle_page $page, core_renderer $renderer) {
349 global $CFG;
350
9bdcf579
DW
351 // Init the js config.
352 $this->get_config_for_javascript($page, $renderer);
0bb38e8c 353
a33e7407 354 // Accessibility stuff.
2cf81209 355 $this->skip_link_to('maincontent', get_string('tocontent', 'access'));
0bb38e8c 356
a33e7407 357 // Add strings used on many pages.
0bb38e8c
PS
358 $this->string_for_js('confirmation', 'admin');
359 $this->string_for_js('cancel', 'moodle');
360 $this->string_for_js('yes', 'moodle');
879b4f9a 361
a33e7407 362 // Alter links in top frame to break out of frames.
4aea3cc7
PS
363 if ($page->pagelayout === 'frametop') {
364 $this->js_init_call('M.util.init_frametop');
365 }
606554d5
PN
366
367 // Include block drag/drop if editing is on
368 if ($page->user_is_editing()) {
369 $params = array(
370 'courseid' => $page->course->id,
371 'pagetype' => $page->pagetype,
372 'pagelayout' => $page->pagelayout,
373 'subpage' => $page->subpage,
374 'regions' => $page->blocks->get_regions(),
375 'contextid' => $page->context->id,
376 );
377 if (!empty($page->cm->id)) {
378 $params['cmid'] = $page->cm->id;
379 }
dd66b6ab
DW
380 // Strings for drag and drop.
381 $this->strings_for_js(array('movecontent',
bbb483b2 382 'tocontent',
dd66b6ab
DW
383 'emptydragdropregion'),
384 'moodle');
606554d5
PN
385 $page->requires->yui_module('moodle-core-blocks', 'M.core_blocks.init_dragdrop', array($params), null, true);
386 }
2300a312
AN
387
388 // Include the YUI CSS Modules.
389 $page->requires->set_yuicssmodules($page->theme->yuicssmodules);
0bb38e8c
PS
390 }
391
c8b9f6d9
AN
392 /**
393 * Determine the correct JS Revision to use for this load.
394 *
395 * @return int the jsrev to use.
396 */
397 protected function get_jsrev() {
398 global $CFG;
399
400 if (empty($CFG->cachejs)) {
401 $jsrev = -1;
402 } else if (empty($CFG->jsrev)) {
403 $jsrev = 1;
404 } else {
405 $jsrev = $CFG->jsrev;
406 }
407
408 return $jsrev;
409 }
410
0bb38e8c
PS
411 /**
412 * Ensure that the specified JavaScript file is linked to from this page.
413 *
a33e7407
PS
414 * NOTE: This function is to be used in RARE CASES ONLY, please store your JS in module.js file
415 * and use $PAGE->requires->js_init_call() instead or use /yui/ subdirectories for YUI modules.
0bb38e8c
PS
416 *
417 * By default the link is put at the end of the page, since this gives best page-load performance.
418 *
419 * Even if a particular script is requested more than once, it will only be linked
420 * to once.
421 *
422 * @param string|moodle_url $url The path to the .js file, relative to $CFG->dirroot / $CFG->wwwroot.
423 * For example '/mod/mymod/customscripts.js'; use moodle_url for external scripts
424 * @param bool $inhead initialise in head
0bb38e8c 425 */
48d4fad1 426 public function js($url, $inhead = false) {
0bb38e8c
PS
427 $url = $this->js_fix_url($url);
428 $where = $inhead ? 'head' : 'footer';
429 $this->jsincludes[$where][$url->out()] = $url;
430 }
431
63c88397
PS
432 /**
433 * Request inclusion of jQuery library in the page.
434 *
435 * NOTE: this should not be used in official Moodle distribution!
436 *
63c88397
PS
437 * {@see http://docs.moodle.org/dev/jQuery}
438 */
439 public function jquery() {
440 $this->jquery_plugin('jquery');
441 }
442
443 /**
444 * Request inclusion of jQuery plugin.
445 *
446 * NOTE: this should not be used in official Moodle distribution!
447 *
448 * jQuery plugins are located in plugin/jquery/* subdirectory,
449 * plugin/jquery/plugins.php lists all available plugins.
450 *
451 * Included core plugins:
452 * - jQuery UI
63c88397
PS
453 *
454 * Add-ons may include extra jQuery plugins in jquery/ directory,
455 * plugins.php file defines the mapping between plugin names and
456 * necessary page includes.
457 *
458 * Examples:
459 * <code>
460 * // file: mod/xxx/view.php
461 * $PAGE->requires->jquery();
462 * $PAGE->requires->jquery_plugin('ui');
463 * $PAGE->requires->jquery_plugin('ui-css');
464 * </code>
465 *
466 * <code>
467 * // file: theme/yyy/lib.php
468 * function theme_yyy_page_init(moodle_page $page) {
469 * $page->requires->jquery();
470 * $page->requires->jquery_plugin('ui');
471 * $page->requires->jquery_plugin('ui-css');
472 * }
473 * </code>
474 *
475 * <code>
476 * // file: blocks/zzz/block_zzz.php
477 * public function get_required_javascript() {
478 * parent::get_required_javascript();
479 * $this->page->requires->jquery();
480 * $page->requires->jquery_plugin('ui');
481 * $page->requires->jquery_plugin('ui-css');
482 * }
483 * </code>
484 *
485 * {@see http://docs.moodle.org/dev/jQuery}
486 *
487 * @param string $plugin name of the jQuery plugin as defined in jquery/plugins.php
488 * @param string $component name of the component
489 * @return bool success
490 */
491 public function jquery_plugin($plugin, $component = 'core') {
492 global $CFG;
493
494 if ($this->headdone) {
495 debugging('Can not add jQuery plugins after starting page output!');
496 return false;
497 }
498
68512444 499 if ($component !== 'core' and in_array($plugin, array('jquery', 'ui', 'ui-css'))) {
63c88397
PS
500 debugging("jQuery plugin '$plugin' is included in Moodle core, other components can not use the same name.", DEBUG_DEVELOPER);
501 $component = 'core';
502 } else if ($component !== 'core' and strpos($component, '_') === false) {
503 // Let's normalise the legacy activity names, Frankenstyle rulez!
504 $component = 'mod_' . $component;
505 }
506
507 if (empty($this->jqueryplugins) and ($component !== 'core' or $plugin !== 'jquery')) {
508 // Make sure the jQuery itself is always loaded first,
509 // the order of all other plugins depends on order of $PAGE_>requires->.
510 $this->jquery_plugin('jquery', 'core');
511 }
512
513 if (isset($this->jqueryplugins[$plugin])) {
514 // No problem, we already have something, first Moodle plugin to register the jQuery plugin wins.
515 return true;
516 }
517
b0d1d941 518 $componentdir = core_component::get_component_directory($component);
63c88397
PS
519 if (!file_exists($componentdir) or !file_exists("$componentdir/jquery/plugins.php")) {
520 debugging("Can not load jQuery plugin '$plugin', missing plugins.php in component '$component'.", DEBUG_DEVELOPER);
521 return false;
522 }
523
524 $plugins = array();
525 require("$componentdir/jquery/plugins.php");
526
527 if (!isset($plugins[$plugin])) {
528 debugging("jQuery plugin '$plugin' can not be found in component '$component'.", DEBUG_DEVELOPER);
529 return false;
530 }
531
532 $this->jqueryplugins[$plugin] = new stdClass();
533 $this->jqueryplugins[$plugin]->plugin = $plugin;
534 $this->jqueryplugins[$plugin]->component = $component;
535 $this->jqueryplugins[$plugin]->urls = array();
536
537 foreach ($plugins[$plugin]['files'] as $file) {
96f81ea3 538 if ($CFG->debugdeveloper) {
63c88397
PS
539 if (!file_exists("$componentdir/jquery/$file")) {
540 debugging("Invalid file '$file' specified in jQuery plugin '$plugin' in component '$component'");
541 continue;
542 }
543 $file = str_replace('.min.css', '.css', $file);
544 $file = str_replace('.min.js', '.js', $file);
545 }
546 if (!file_exists("$componentdir/jquery/$file")) {
547 debugging("Invalid file '$file' specified in jQuery plugin '$plugin' in component '$component'");
548 continue;
549 }
550 if (!empty($CFG->slasharguments)) {
551 $url = new moodle_url("$CFG->httpswwwroot/theme/jquery.php");
552 $url->set_slashargument("/$component/$file");
553
554 } else {
555 // This is not really good, we need slasharguments for relative links, this means no caching...
556 $path = realpath("$componentdir/jquery/$file");
557 if (strpos($path, $CFG->dirroot) === 0) {
558 $url = $CFG->httpswwwroot.preg_replace('/^'.preg_quote($CFG->dirroot, '/').'/', '', $path);
bbbdfc86
JP
559 // Replace all occurences of backslashes characters in url to forward slashes.
560 $url = str_replace('\\', '/', $url);
63c88397
PS
561 $url = new moodle_url($url);
562 } else {
563 // Bad luck, fix your server!
564 debugging("Moodle jQuery integration requires 'slasharguments' setting to be enabled.");
565 continue;
566 }
567 }
568 $this->jqueryplugins[$plugin]->urls[] = $url;
569 }
570
571 return true;
572 }
573
574 /**
575 * Request replacement of one jQuery plugin by another.
576 *
577 * This is useful when themes want to replace the jQuery UI theme,
578 * the problem is that theme can not prevent others from including the core ui-css plugin.
579 *
580 * Example:
581 * 1/ generate new jQuery UI theme and place it into theme/yourtheme/jquery/
582 * 2/ write theme/yourtheme/jquery/plugins.php
583 * 3/ init jQuery from theme
584 *
585 * <code>
586 * // file theme/yourtheme/lib.php
587 * function theme_yourtheme_page_init($page) {
588 * $page->requires->jquery_plugin('yourtheme-ui-css', 'theme_yourtheme');
589 * $page->requires->jquery_override_plugin('ui-css', 'yourtheme-ui-css');
590 * }
591 * </code>
592 *
593 * This code prevents loading of standard 'ui-css' which my be requested by other plugins,
594 * the 'yourtheme-ui-css' gets loaded only if some other code requires jquery.
595 *
596 * {@see http://docs.moodle.org/dev/jQuery}
597 *
598 * @param string $oldplugin original plugin
599 * @param string $newplugin the replacement
600 */
601 public function jquery_override_plugin($oldplugin, $newplugin) {
602 if ($this->headdone) {
603 debugging('Can not override jQuery plugins after starting page output!');
604 return;
605 }
606 $this->jquerypluginoverrides[$oldplugin] = $newplugin;
607 }
608
609 /**
610 * Return jQuery related markup for page start.
611 * @return string
612 */
613 protected function get_jquery_headcode() {
614 if (empty($this->jqueryplugins['jquery'])) {
615 // If nobody requested jQuery then do not bother to load anything.
616 // This may be useful for themes that want to override 'ui-css' only if requested by something else.
617 return '';
618 }
619
620 $included = array();
621 $urls = array();
622
623 foreach ($this->jqueryplugins as $name => $unused) {
624 if (isset($included[$name])) {
625 continue;
626 }
627 if (array_key_exists($name, $this->jquerypluginoverrides)) {
628 // The following loop tries to resolve the replacements,
629 // use max 100 iterations to prevent infinite loop resulting
630 // in blank page.
631 $cyclic = true;
632 $oldname = $name;
633 for ($i=0; $i<100; $i++) {
634 $name = $this->jquerypluginoverrides[$name];
635 if (!array_key_exists($name, $this->jquerypluginoverrides)) {
636 $cyclic = false;
637 break;
638 }
639 }
640 if ($cyclic) {
641 // We can not do much with cyclic references here, let's use the old plugin.
642 $name = $oldname;
643 debugging("Cyclic overrides detected for jQuery plugin '$name'");
644
645 } else if (empty($name)) {
646 // Developer requested removal of the plugin.
647 continue;
648
649 } else if (!isset($this->jqueryplugins[$name])) {
650 debugging("Unknown jQuery override plugin '$name' detected");
651 $name = $oldname;
652
653 } else if (isset($included[$name])) {
654 // The plugin was already included, easy.
655 continue;
656 }
657 }
658
659 $plugin = $this->jqueryplugins[$name];
660 $urls = array_merge($urls, $plugin->urls);
661 $included[$name] = true;
662 }
663
664 $output = '';
665 $attributes = array('rel' => 'stylesheet', 'type' => 'text/css');
666 foreach ($urls as $url) {
667 if (preg_match('/\.js$/', $url)) {
668 $output .= html_writer::script('', $url);
669 } else if (preg_match('/\.css$/', $url)) {
670 $attributes['href'] = $url;
671 $output .= html_writer::empty_tag('link', $attributes) . "\n";
672 }
673 }
674
675 return $output;
676 }
677
0bb38e8c
PS
678 /**
679 * Returns the actual url through which a script is served.
48d4fad1 680 *
0bb38e8c
PS
681 * @param moodle_url|string $url full moodle url, or shortened path to script
682 * @return moodle_url
683 */
684 protected function js_fix_url($url) {
685 global $CFG;
686
687 if ($url instanceof moodle_url) {
688 return $url;
689 } else if (strpos($url, '/') === 0) {
8a8914cd
690 // Fix the admin links if needed.
691 if ($CFG->admin !== 'admin') {
692 if (strpos($url, "/admin/") === 0) {
693 $url = preg_replace("|^/admin/|", "/$CFG->admin/", $url);
694 }
695 }
0bb38e8c 696 if (debugging()) {
a33e7407 697 // Check file existence only when in debug mode.
0bb38e8c 698 if (!file_exists($CFG->dirroot . strtok($url, '?'))) {
44fab7fa 699 throw new coding_exception('Attempt to require a JavaScript file that does not exist.', $url);
0bb38e8c
PS
700 }
701 }
ff74627e 702 if (substr($url, -3) === '.js') {
c8b9f6d9 703 $jsrev = $this->get_jsrev();
8ab29743 704 if (empty($CFG->slasharguments)) {
ff74627e 705 return new moodle_url($CFG->httpswwwroot.'/lib/javascript.php', array('rev'=>$jsrev, 'jsfile'=>$url));
8ab29743
PS
706 } else {
707 $returnurl = new moodle_url($CFG->httpswwwroot.'/lib/javascript.php');
ff74627e 708 $returnurl->set_slashargument('/'.$jsrev.$url);
8ab29743
PS
709 return $returnurl;
710 }
0139ec3f
PS
711 } else {
712 return new moodle_url($CFG->httpswwwroot.$url);
713 }
0bb38e8c
PS
714 } else {
715 throw new coding_exception('Invalid JS url, it has to be shortened url starting with / or moodle_url instance.', $url);
716 }
717 }
718
719 /**
173ba4f1 720 * Find out if JS module present and return details.
48d4fad1 721 *
88623be1 722 * @param string $component name of component in frankenstyle, ex: core_group, mod_forum
0bb38e8c
PS
723 * @return array description of module or null if not found
724 */
88623be1 725 protected function find_module($component) {
b5e7b638 726 global $CFG, $PAGE;
0bb38e8c
PS
727
728 $module = null;
729
88623be1 730 if (strpos($component, 'core_') === 0) {
a33e7407
PS
731 // Must be some core stuff - list here is not complete, this is just the stuff used from multiple places
732 // so that we do nto have to repeat the definition of these modules over and over again.
88623be1 733 switch($component) {
0bb38e8c
PS
734 case 'core_filepicker':
735 $module = array('name' => 'core_filepicker',
736 'fullpath' => '/repository/filepicker.js',
0053c77d
DP
737 'requires' => array(
738 'base', 'node', 'node-event-simulate', 'json', 'async-queue', 'io-base', 'io-upload-iframe', 'io-form',
739 'yui2-treeview', 'panel', 'cookie', 'datatable', 'datatable-sort', 'resize-plugin', 'dd-plugin',
740 'escape', 'moodle-core_filepicker', 'moodle-core-notification-dialogue'
741 ),
4325db53
MG
742 'strings' => array(array('lastmodified', 'moodle'), array('name', 'moodle'), array('type', 'repository'), array('size', 'repository'),
743 array('invalidjson', 'repository'), array('error', 'moodle'), array('info', 'moodle'),
744 array('nofilesattached', 'repository'), array('filepicker', 'repository'), array('logout', 'repository'),
745 array('nofilesavailable', 'repository'), array('norepositoriesavailable', 'repository'),
f392caba 746 array('fileexistsdialogheader', 'repository'), array('fileexistsdialog_editor', 'repository'),
9dbdf31f 747 array('fileexistsdialog_filemanager', 'repository'), array('renameto', 'repository'),
d8817b4a 748 array('referencesexist', 'repository'), array('select', 'repository')
879b4f9a 749 ));
0bb38e8c
PS
750 break;
751 case 'core_comment':
752 $module = array('name' => 'core_comment',
753 'fullpath' => '/comment/comment.js',
86f1d1bb 754 'requires' => array('base', 'io-base', 'node', 'json', 'yui2-animation', 'overlay', 'escape'),
535b4a51 755 'strings' => array(array('confirmdeletecomments', 'admin'), array('yes', 'moodle'), array('no', 'moodle'))
34e20eb4 756 );
0bb38e8c
PS
757 break;
758 case 'core_role':
759 $module = array('name' => 'core_role',
87b4981b
SH
760 'fullpath' => '/admin/roles/module.js',
761 'requires' => array('node', 'cookie'));
0bb38e8c
PS
762 break;
763 case 'core_completion':
764 $module = array('name' => 'core_completion',
765 'fullpath' => '/course/completion.js');
766 break;
0bb38e8c
PS
767 case 'core_message':
768 $module = array('name' => 'core_message',
1d72e9d4 769 'requires' => array('base', 'node', 'event', 'node-event-simulate'),
0bb38e8c
PS
770 'fullpath' => '/message/module.js');
771 break;
c2489597
SH
772 case 'core_group':
773 $module = array('name' => 'core_group',
774 'fullpath' => '/group/module.js',
775 'requires' => array('node', 'overlay', 'event-mouseenter'));
776 break;
157434a5
TH
777 case 'core_question_engine':
778 $module = array('name' => 'core_question_engine',
779 'fullpath' => '/question/qengine.js',
780 'requires' => array('node', 'event'));
781 break;
3dcdf440
PS
782 case 'core_rating':
783 $module = array('name' => 'core_rating',
a09aeee4 784 'fullpath' => '/rating/module.js',
0958759d 785 'requires' => array('node', 'event', 'overlay', 'io-base', 'json'));
a09aeee4 786 break;
f08fac7c
DS
787 case 'core_dndupload':
788 $module = array('name' => 'core_dndupload',
789 'fullpath' => '/lib/form/dndupload.js',
d0b2d445 790 'requires' => array('node', 'event', 'json', 'core_filepicker'),
39bb9b1f 791 'strings' => array(array('uploadformlimit', 'moodle'), array('droptoupload', 'moodle'), array('maxfilesreached', 'moodle'),
a2fb838e
BT
792 array('dndenabled_inbox', 'moodle'), array('fileexists', 'moodle'), array('maxbytesfile', 'error'),
793 array('sizegb', 'moodle'), array('sizemb', 'moodle'), array('sizekb', 'moodle'), array('sizeb', 'moodle'),
679af357 794 array('maxareabytesreached', 'moodle'), array('serverconnection', 'error'),
39bb9b1f 795 ));
f08fac7c 796 break;
0bb38e8c
PS
797 }
798
799 } else {
b0d1d941 800 if ($dir = core_component::get_component_directory($component)) {
ae586328
PS
801 if (file_exists("$dir/module.js")) {
802 if (strpos($dir, $CFG->dirroot.'/') === 0) {
803 $dir = substr($dir, strlen($CFG->dirroot));
804 $module = array('name'=>$component, 'fullpath'=>"$dir/module.js", 'requires' => array());
805 }
0bb38e8c
PS
806 }
807 }
808 }
809
810 return $module;
811 }
812
813 /**
814 * Append YUI3 module to default YUI3 JS loader.
48d4fad1
SH
815 * The structure of module array is described at {@link http://developer.yahoo.com/yui/3/yui/}
816 *
0bb38e8c
PS
817 * @param string|array $module name of module (details are autodetected), or full module specification as array
818 * @return void
819 */
820 public function js_module($module) {
821 global $CFG;
822
823 if (empty($module)) {
824 throw new coding_exception('Missing YUI3 module name or full description.');
825 }
826
827 if (is_string($module)) {
828 $module = $this->find_module($module);
829 }
830
831 if (empty($module) or empty($module['name']) or empty($module['fullpath'])) {
832 throw new coding_exception('Missing YUI3 module details.');
833 }
834
835 $module['fullpath'] = $this->js_fix_url($module['fullpath'])->out(false);
a33e7407 836 // Add all needed strings.
87ad1edc
PS
837 if (!empty($module['strings'])) {
838 foreach ($module['strings'] as $string) {
839 $identifier = $string[0];
840 $component = isset($string[1]) ? $string[1] : 'moodle';
841 $a = isset($string[2]) ? $string[2] : null;
842 $this->string_for_js($identifier, $component, $a);
843 }
844 }
845 unset($module['strings']);
846
d067fc47
SH
847 // Process module requirements and attempt to load each. This allows
848 // moodle modules to require each other.
849 if (!empty($module['requires'])){
850 foreach ($module['requires'] as $requirement) {
851 $rmodule = $this->find_module($requirement);
852 if (is_array($rmodule)) {
853 $this->js_module($rmodule);
854 }
855 }
856 }
621df74e 857
0bb38e8c
PS
858 if ($this->headdone) {
859 $this->extramodules[$module['name']] = $module;
860 } else {
cae21a32 861 $this->YUI_config->add_module_config($module['name'], $module);
0bb38e8c 862 }
d067fc47
SH
863 }
864
865 /**
866 * Returns true if the module has already been loaded.
867 *
7edf3f7e 868 * @param string|array $module
d067fc47
SH
869 * @return bool True if the module has already been loaded
870 */
871 protected function js_module_loaded($module) {
872 if (is_string($module)) {
873 $modulename = $module;
874 } else {
875 $modulename = $module['name'];
876 }
2f422271 877 return array_key_exists($modulename, $this->YUI_config->modules) ||
d067fc47
SH
878 array_key_exists($modulename, $this->extramodules);
879 }
880
0bb38e8c
PS
881 /**
882 * Ensure that the specified CSS file is linked to from this page.
883 *
884 * Because stylesheet links must go in the <head> part of the HTML, you must call
885 * this function before {@link get_head_code()} is called. That normally means before
886 * the call to print_header. If you call it when it is too late, an exception
887 * will be thrown.
888 *
889 * Even if a particular style sheet is requested more than once, it will only
890 * be linked to once.
891 *
86c2da17 892 * Please note use of this feature is strongly discouraged,
0bb38e8c
PS
893 * it is suitable only for places where CSS is submitted directly by teachers.
894 * (Students must not be allowed to submit any external CSS because it may
895 * contain embedded javascript!). Example of correct use is mod/data.
896 *
897 * @param string $stylesheet The path to the .css file, relative to $CFG->wwwroot.
898 * For example:
899 * $PAGE->requires->css('mod/data/css.php?d='.$data->id);
900 */
901 public function css($stylesheet) {
902 global $CFG;
903
904 if ($this->headdone) {
905 throw new coding_exception('Cannot require a CSS file after &lt;head> has been printed.', $stylesheet);
906 }
907
908 if ($stylesheet instanceof moodle_url) {
909 // ok
910 } else if (strpos($stylesheet, '/') === 0) {
911 $stylesheet = new moodle_url($CFG->httpswwwroot.$stylesheet);
912 } else {
913 throw new coding_exception('Invalid stylesheet parameter.', $stylesheet);
914 }
915
a33e7407 916 $this->cssurls[$stylesheet->out()] = $stylesheet;
0bb38e8c
PS
917 }
918
919 /**
a33e7407 920 * Add theme stylesheet to page - do not use from plugin code,
0bb38e8c 921 * this should be called only from the core renderer!
48d4fad1 922 *
0bb38e8c
PS
923 * @param moodle_url $stylesheet
924 * @return void
925 */
926 public function css_theme(moodle_url $stylesheet) {
927 $this->cssthemeurls[] = $stylesheet;
928 }
929
930 /**
931 * Ensure that a skip link to a given target is printed at the top of the <body>.
932 *
933 * You must call this function before {@link get_top_of_body_code()}, (if not, an exception
934 * will be thrown). That normally means you must call this before the call to print_header.
935 *
936 * If you ask for a particular skip link to be printed, it is then your responsibility
173ba4f1 937 * to ensure that the appropriate <a name="..."> tag is printed in the body of the
0bb38e8c
PS
938 * page, so that the skip link goes somewhere.
939 *
940 * Even if a particular skip link is requested more than once, only one copy of it will be output.
941 *
a33e7407
PS
942 * @param string $target the name of anchor this link should go to. For example 'maincontent'.
943 * @param string $linktext The text to use for the skip link. Normally get_string('skipto', 'access', ...);
0bb38e8c
PS
944 */
945 public function skip_link_to($target, $linktext) {
946 if ($this->topofbodydone) {
947 debugging('Page header already printed, can not add skip links any more, code needs to be fixed.');
948 return;
949 }
950 $this->skiplinks[$target] = $linktext;
951 }
952
953 /**
954 * !!!DEPRECATED!!! please use js_init_call() if possible
955 * Ensure that the specified JavaScript function is called from an inline script
956 * somewhere on this page.
957 *
958 * By default the call will be put in a script tag at the
959 * end of the page after initialising Y instance, since this gives best page-load
960 * performance and allows you to use YUI3 library.
961 *
962 * If you request that a particular function is called several times, then
963 * that is what will happen (unlike linking to a CSS or JS file, where only
964 * one link will be output).
965 *
173ba4f1 966 * The main benefit of the method is the automatic encoding of all function parameters.
0bb38e8c 967 *
a33e7407
PS
968 * @deprecated
969 *
0bb38e8c
PS
970 * @param string $function the name of the JavaScritp function to call. Can
971 * be a compound name like 'Y.Event.purgeElement'. Can also be
972 * used to create and object by using a 'function name' like 'new user_selector'.
973 * @param array $arguments and array of arguments to be passed to the function.
974 * When generating the function call, this will be escaped using json_encode,
975 * so passing objects and arrays should work.
48d4fad1
SH
976 * @param bool $ondomready If tru the function is only called when the dom is
977 * ready for manipulation.
978 * @param int $delay The delay before the function is called.
0bb38e8c
PS
979 */
980 public function js_function_call($function, array $arguments = null, $ondomready = false, $delay = 0) {
981 $where = $ondomready ? 'ondomready' : 'normal';
982 $this->jscalls[$where][] = array($function, $arguments, $delay);
983 }
984
adeb96d2
DW
985 /**
986 * This function appends a block of code to the AMD specific javascript block executed
987 * in the page footer, just after loading the requirejs library.
988 *
989 * The code passed here can rely on AMD module loading, e.g. require('jquery', function($) {...});
990 *
991 * @param string $code The JS code to append.
992 */
993 public function js_amd_inline($code) {
994 $this->amdjscode[] = $code;
995 }
996
997 /**
998 * This function creates a minimal JS script that requires and calls a single function from an AMD module with arguments.
999 * If it is called multiple times, it will be executed multiple times.
1000 *
1001 * @param string $fullmodule The format for module names is <component name>/<module name>.
1002 * @param string $func The function from the module to call
1003 * @param array $params The params to pass to the function. They will be json encoded, so no nasty classes/types please.
1004 */
1005 public function js_call_amd($fullmodule, $func, $params = array()) {
1006 global $CFG;
1007
1008 list($component, $module) = explode('/', $fullmodule, 2);
1009
1010 $component = clean_param($component, PARAM_COMPONENT);
1011 $module = clean_param($module, PARAM_ALPHANUMEXT);
1012 $func = clean_param($func, PARAM_ALPHANUMEXT);
1013
1014 $jsonparams = array();
1015 foreach ($params as $param) {
1016 $jsonparams[] = json_encode($param);
1017 }
1018 $strparams = implode(', ', $jsonparams);
1019 if ($CFG->debugdeveloper) {
1020 $toomanyparamslimit = 1024;
1021 if (strlen($strparams) > $toomanyparamslimit) {
1022 debugging('Too many params passed to js_call_amd("' . $fullmodule . '", "' . $func . '")', DEBUG_DEVELOPER);
1023 }
1024 }
1025
1026 $js = 'require(["' . $component . '/' . $module . '"], function(amd) { amd.' . $func . '(' . $strparams . '); });';
1027
1028 $this->js_amd_inline($js);
1029 }
1030
2b722f87 1031 /**
a33e7407 1032 * Creates a JavaScript function call that requires one or more modules to be loaded.
2b722f87
SH
1033 *
1034 * This function can be used to include all of the standard YUI module types within JavaScript:
1035 * - YUI3 modules [node, event, io]
1036 * - YUI2 modules [yui2-*]
1037 * - Moodle modules [moodle-*]
1038 * - Gallery modules [gallery-*]
1039 *
adeb96d2
DW
1040 * Before writing new code that makes extensive use of YUI, you should consider it's replacement AMD/JQuery.
1041 * @see js_call_amd()
1042 *
2b722f87
SH
1043 * @param array|string $modules One or more modules
1044 * @param string $function The function to call once modules have been loaded
1045 * @param array $arguments An array of arguments to pass to the function
5b49f8b3 1046 * @param string $galleryversion Deprecated: The gallery version to use
2b722f87
SH
1047 * @param bool $ondomready
1048 */
09f200aa 1049 public function yui_module($modules, $function, array $arguments = null, $galleryversion = null, $ondomready = false) {
2a102b90
SH
1050 if (!is_array($modules)) {
1051 $modules = array($modules);
1052 }
5b49f8b3
AN
1053
1054 if ($galleryversion != null) {
1055 debugging('The galleryversion parameter to yui_module has been deprecated since Moodle 2.3.');
2a102b90 1056 }
5b49f8b3
AN
1057
1058 $jscode = 'Y.use('.join(',', array_map('json_encode', convert_to_array($modules))).',function() {'.js_writer::function_call($function, $arguments).'});';
2a102b90
SH
1059 if ($ondomready) {
1060 $jscode = "Y.on('domready', function() { $jscode });";
1061 }
1062 $this->jsinitcode[] = $jscode;
1063 }
1064
2300a312
AN
1065 /**
1066 * Set the CSS Modules to be included from YUI.
1067 *
1068 * @param array $modules The list of YUI CSS Modules to include.
1069 */
1070 public function set_yuicssmodules(array $modules = array()) {
1071 $this->yuicssmodules = $modules;
1072 }
1073
0bb38e8c
PS
1074 /**
1075 * Ensure that the specified JavaScript function is called from an inline script
1076 * from page footer.
1077 *
1078 * @param string $function the name of the JavaScritp function to with init code,
1079 * usually something like 'M.mod_mymodule.init'
1080 * @param array $extraarguments and array of arguments to be passed to the function.
1081 * The first argument is always the YUI3 Y instance with all required dependencies
1082 * already loaded.
1083 * @param bool $ondomready wait for dom ready (helps with some IE problems when modifying DOM)
1084 * @param array $module JS module specification array
0bb38e8c
PS
1085 */
1086 public function js_init_call($function, array $extraarguments = null, $ondomready = false, array $module = null) {
1087 $jscode = js_writer::function_call_with_Y($function, $extraarguments);
1088 if (!$module) {
a33e7407 1089 // Detect module automatically.
0bb38e8c
PS
1090 if (preg_match('/M\.([a-z0-9]+_[^\.]+)/', $function, $matches)) {
1091 $module = $this->find_module($matches[1]);
1092 }
1093 }
1094
1095 $this->js_init_code($jscode, $ondomready, $module);
1096 }
1097
1098 /**
1099 * Add short static javascript code fragment to page footer.
1100 * This is intended primarily for loading of js modules and initialising page layout.
1101 * Ideally the JS code fragment should be stored in plugin renderer so that themes
1102 * may override it.
a33e7407 1103 *
0bb38e8c
PS
1104 * @param string $jscode
1105 * @param bool $ondomready wait for dom ready (helps with some IE problems when modifying DOM)
1106 * @param array $module JS module specification array
0bb38e8c
PS
1107 */
1108 public function js_init_code($jscode, $ondomready = false, array $module = null) {
1109 $jscode = trim($jscode, " ;\n"). ';';
1110
6e2c417c
DW
1111 $uniqid = html_writer::random_id();
1112 $startjs = " M.util.js_pending('" . $uniqid . "');";
1113 $endjs = " M.util.js_complete('" . $uniqid . "');";
1114
0bb38e8c
PS
1115 if ($module) {
1116 $this->js_module($module);
1117 $modulename = $module['name'];
6e2c417c 1118 $jscode = "$startjs Y.use('$modulename', function(Y) { $jscode $endjs });";
0bb38e8c
PS
1119 }
1120
1121 if ($ondomready) {
6e2c417c 1122 $jscode = "$startjs Y.on('domready', function() { $jscode $endjs });";
0bb38e8c
PS
1123 }
1124
1125 $this->jsinitcode[] = $jscode;
1126 }
1127
1128 /**
1129 * Make a language string available to JavaScript.
1130 *
2b728cb5 1131 * All the strings will be available in a M.str object in the global namespace.
0bb38e8c 1132 * So, for example, after a call to $PAGE->requires->string_for_js('course', 'moodle');
2b728cb5 1133 * then the JavaScript variable M.str.moodle.course will be 'Course', or the
0bb38e8c
PS
1134 * equivalent in the current language.
1135 *
1136 * The arguments to this function are just like the arguments to get_string
7f91ba53
DM
1137 * except that $component is not optional, and there are some aspects to consider
1138 * when the string contains {$a} placeholder.
1139 *
1140 * If the string does not contain any {$a} placeholder, you can simply use
1141 * M.str.component.identifier to obtain it. If you prefer, you can call
1142 * M.util.get_string(identifier, component) to get the same result.
1143 *
1144 * If you need to use {$a} placeholders, there are two options. Either the
1145 * placeholder should be substituted in PHP on server side or it should
1146 * be substituted in Javascript at client side.
1147 *
1148 * To substitute the placeholder at server side, just provide the required
1149 * value for the placeholder when you require the string. Because each string
1150 * is only stored once in the JavaScript (based on $identifier and $module)
1151 * you cannot get the same string with two different values of $a. If you try,
1152 * an exception will be thrown. Once the placeholder is substituted, you can
1153 * use M.str or M.util.get_string() as shown above:
1154 *
a33e7407 1155 * // Require the string in PHP and replace the placeholder.
7f91ba53 1156 * $PAGE->requires->string_for_js('fullnamedisplay', 'moodle', $USER);
a33e7407 1157 * // Use the result of the substitution in Javascript.
7f91ba53
DM
1158 * alert(M.str.moodle.fullnamedisplay);
1159 *
1160 * To substitute the placeholder at client side, use M.util.get_string()
f8129210 1161 * function. It implements the same logic as {@link get_string()}:
7f91ba53 1162 *
a33e7407 1163 * // Require the string in PHP but keep {$a} as it is.
7f91ba53 1164 * $PAGE->requires->string_for_js('fullnamedisplay', 'moodle');
a33e7407 1165 * // Provide the values on the fly in Javascript.
7f91ba53
DM
1166 * user = { firstname : 'Harry', lastname : 'Potter' }
1167 * alert(M.util.get_string('fullnamedisplay', 'moodle', user);
1168 *
1169 * If you do need the same string expanded with different $a values in PHP
1170 * on server side, then the solution is to put them in your own data structure
1171 * (e.g. and array) that you pass to JavaScript with {@link data_for_js()}.
0bb38e8c
PS
1172 *
1173 * @param string $identifier the desired string.
7edf3f7e 1174 * @param string $component the language file to look in.
0bb38e8c
PS
1175 * @param mixed $a any extra data to add into the string (optional).
1176 */
a33e7407 1177 public function string_for_js($identifier, $component, $a = null) {
0bb38e8c 1178 if (!$component) {
af225f6c 1179 throw new coding_exception('The $component parameter is required for page_requirements_manager::string_for_js().');
0bb38e8c 1180 }
af225f6c 1181 if (isset($this->stringsforjs_as[$component][$identifier]) and $this->stringsforjs_as[$component][$identifier] !== $a) {
0bb38e8c 1182 throw new coding_exception("Attempt to re-define already required string '$identifier' " .
af225f6c
1183 "from lang file '$component' with different \$a parameter?");
1184 }
1185 if (!isset($this->stringsforjs[$component][$identifier])) {
1186 $this->stringsforjs[$component][$identifier] = new lang_string($identifier, $component, $a);
1187 $this->stringsforjs_as[$component][$identifier] = $a;
0bb38e8c 1188 }
0bb38e8c
PS
1189 }
1190
1191 /**
a33e7407 1192 * Make an array of language strings available for JS.
0bb38e8c
PS
1193 *
1194 * This function calls the above function {@link string_for_js()} for each requested
1195 * string in the $identifiers array that is passed to the argument for a single module
1196 * passed in $module.
1197 *
1198 * <code>
b9934a17 1199 * $PAGE->requires->strings_for_js(array('one', 'two', 'three'), 'mymod', array('a', null, 3));
0bb38e8c 1200 *
a33e7407 1201 * // The above is identical to calling:
0bb38e8c 1202 *
b9934a17
DM
1203 * $PAGE->requires->string_for_js('one', 'mymod', 'a');
1204 * $PAGE->requires->string_for_js('two', 'mymod');
1205 * $PAGE->requires->string_for_js('three', 'mymod', 3);
0bb38e8c
PS
1206 * </code>
1207 *
1208 * @param array $identifiers An array of desired strings
1209 * @param string $component The module to load for
1210 * @param mixed $a This can either be a single variable that gets passed as extra
1211 * information for every string or it can be an array of mixed data where the
1212 * key for the data matches that of the identifier it is meant for.
1213 *
1214 */
a33e7407 1215 public function strings_for_js($identifiers, $component, $a = null) {
0bb38e8c
PS
1216 foreach ($identifiers as $key => $identifier) {
1217 if (is_array($a) && array_key_exists($key, $a)) {
1218 $extra = $a[$key];
1219 } else {
1220 $extra = $a;
1221 }
1222 $this->string_for_js($identifier, $component, $extra);
1223 }
1224 }
1225
1226 /**
1227 * !!!!!!DEPRECATED!!!!!! please use js_init_call() for everything now.
1228 *
1229 * Make some data from PHP available to JavaScript code.
1230 *
1231 * For example, if you call
1232 * <pre>
1233 * $PAGE->requires->data_for_js('mydata', array('name' => 'Moodle'));
1234 * </pre>
1235 * then in JavsScript mydata.name will be 'Moodle'.
a33e7407
PS
1236 *
1237 * @deprecated
0bb38e8c
PS
1238 * @param string $variable the the name of the JavaScript variable to assign the data to.
1239 * Will probably work if you use a compound name like 'mybuttons.button[1]', but this
1240 * should be considered an experimental feature.
1241 * @param mixed $data The data to pass to JavaScript. This will be escaped using json_encode,
1242 * so passing objects and arrays should work.
1243 * @param bool $inhead initialise in head
1244 * @return void
1245 */
1246 public function data_for_js($variable, $data, $inhead=false) {
1247 $where = $inhead ? 'head' : 'footer';
1248 $this->jsinitvariables[$where][] = array($variable, $data);
1249 }
1250
1251 /**
1252 * Creates a YUI event handler.
1253 *
a33e7407 1254 * @param mixed $selector standard YUI selector for elements, may be array or string, element id is in the form "#idvalue"
0bb38e8c
PS
1255 * @param string $event A valid DOM event (click, mousedown, change etc.)
1256 * @param string $function The name of the function to call
1257 * @param array $arguments An optional array of argument parameters to pass to the function
879b4f9a
DC
1258 */
1259 public function event_handler($selector, $event, $function, array $arguments = null) {
0bb38e8c
PS
1260 $this->eventhandlers[] = array('selector'=>$selector, 'event'=>$event, 'function'=>$function, 'arguments'=>$arguments);
1261 }
1262
1263 /**
1264 * Returns code needed for registering of event handlers.
1265 * @return string JS code
1266 */
1267 protected function get_event_handler_code() {
1268 $output = '';
1269 foreach ($this->eventhandlers as $h) {
1270 $output .= js_writer::event_handler($h['selector'], $h['event'], $h['function'], $h['arguments']);
1271 }
1272 return $output;
1273 }
1274
1275 /**
1276 * Get the inline JavaScript code that need to appear in a particular place.
7edf3f7e
PS
1277 * @param bool $ondomready
1278 * @return string
0bb38e8c
PS
1279 */
1280 protected function get_javascript_code($ondomready) {
1281 $where = $ondomready ? 'ondomready' : 'normal';
1282 $output = '';
1283 if ($this->jscalls[$where]) {
1284 foreach ($this->jscalls[$where] as $data) {
e6a3ea28 1285 $output .= js_writer::function_call($data[0], $data[1], $data[2]);
0bb38e8c
PS
1286 }
1287 if (!empty($ondomready)) {
6e2c417c 1288 $output = " Y.on('domready', function() {\n$output\n});";
0bb38e8c
PS
1289 }
1290 }
1291 return $output;
1292 }
1293
1294 /**
1295 * Returns js code to be executed when Y is available.
48d4fad1 1296 * @return string
0bb38e8c
PS
1297 */
1298 protected function get_javascript_init_code() {
1299 if (count($this->jsinitcode)) {
1300 return implode("\n", $this->jsinitcode) . "\n";
1301 }
1302 return '';
1303 }
1304
adeb96d2
DW
1305 /**
1306 * Returns js code to load amd module loader, then insert inline script tags
1307 * that contain require() calls using RequireJS.
1308 * @return string
1309 */
1310 protected function get_amd_footercode() {
1311 global $CFG;
1312 $output = '';
1313 $jsrev = $this->get_jsrev();
1314
1315 $jsloader = new moodle_url($CFG->httpswwwroot . '/lib/javascript.php');
1316 $jsloader->set_slashargument('/' . $jsrev . '/');
1317 $requirejsloader = new moodle_url($CFG->httpswwwroot . '/lib/requirejs.php');
1318 $requirejsloader->set_slashargument('/' . $jsrev . '/');
1319
1320 $requirejsconfig = file_get_contents($CFG->dirroot . '/lib/requirejs/moodle-config.js');
1321
1322 // No extension required unless slash args is disabled.
1323 $jsextension = '.js';
1324 if (!empty($CFG->slasharguments)) {
1325 $jsextension = '';
1326 }
1327
1328 $requirejsconfig = str_replace('[BASEURL]', $requirejsloader, $requirejsconfig);
1329 $requirejsconfig = str_replace('[JSURL]', $jsloader, $requirejsconfig);
1330 $requirejsconfig = str_replace('[JSEXT]', $jsextension, $requirejsconfig);
1331
1332 $output .= html_writer::script($requirejsconfig);
1333 if ($CFG->debugdeveloper) {
1334 $output .= html_writer::script('', $this->js_fix_url('/lib/requirejs/require.js'));
1335 } else {
1336 $output .= html_writer::script('', $this->js_fix_url('/lib/requirejs/require.min.js'));
1337 }
1338
1339 // First include must be to a module with no dependencies, this prevents multiple requests.
1340 $prefix = "require(['core/first'], function() {\n";
1341 $suffix = "\n});";
1342 $output .= html_writer::script($prefix . implode(";\n", $this->amdjscode) . $suffix);
1343 return $output;
1344 }
1345
0bb38e8c 1346 /**
2300a312 1347 * Returns basic YUI3 CSS code.
0bb38e8c
PS
1348 *
1349 * @return string
1350 */
2300a312 1351 protected function get_yui3lib_headcss() {
ef7329dc
PS
1352 global $CFG;
1353
2300a312
AN
1354 $yuiformat = '-min';
1355 if ($this->yui3loader->filter === 'RAW') {
1356 $yuiformat = '';
1357 }
1358
ef7329dc 1359 $code = '';
2300a312
AN
1360 if ($this->yui3loader->combine) {
1361 if (!empty($this->yuicssmodules)) {
1362 $modules = array();
1363 foreach ($this->yuicssmodules as $module) {
1364 $modules[] = "$CFG->yui3version/$module/$module-min.css";
1365 }
1366 $code .= '<link rel="stylesheet" type="text/css" href="'.$this->yui3loader->comboBase.implode('&amp;', $modules).'" />';
1367 }
1368 $code .= '<link rel="stylesheet" type="text/css" href="'.$this->yui3loader->local_comboBase.'rollup/'.$CFG->yui3version.'/yui-moodlesimple' . $yuiformat . '.css" />';
1369
1370 } else {
1371 if (!empty($this->yuicssmodules)) {
1372 foreach ($this->yuicssmodules as $module) {
1373 $code .= '<link rel="stylesheet" type="text/css" href="'.$this->yui3loader->base.$module.'/'.$module.'-min.css" />';
1374 }
1375 }
1376 $code .= '<link rel="stylesheet" type="text/css" href="'.$this->yui3loader->local_comboBase.'rollup/'.$CFG->yui3version.'/yui-moodlesimple' . $yuiformat . '.css" />';
1377 }
1378
1379 if ($this->yui3loader->filter === 'RAW') {
1380 $code = str_replace('-min.css', '.css', $code);
1381 } else if ($this->yui3loader->filter === 'DEBUG') {
1382 $code = str_replace('-min.css', '.css', $code);
1383 }
1384 return $code;
1385 }
1386
1387 /**
1388 * Returns basic YUI3 JS loading code.
1389 *
1390 * @return string
1391 */
1392 protected function get_yui3lib_headcode() {
1393 global $CFG;
ef7329dc 1394
c8b9f6d9 1395 $jsrev = $this->get_jsrev();
104d698f
AN
1396
1397 $yuiformat = '-min';
1398 if ($this->yui3loader->filter === 'RAW') {
1399 $yuiformat = '';
1400 }
1401
1402 $format = '-min';
1403 if ($this->YUI_config->groups['moodle']['filter'] === 'DEBUG') {
1404 $format = '-debug';
1405 }
1406
0f722cfb
AN
1407 $rollupversion = $CFG->yui3version;
1408 if (!empty($CFG->yuipatchlevel)) {
1409 $rollupversion .= '_' . $CFG->yuipatchlevel;
1410 }
1411
c8b9f6d9 1412 $baserollups = array(
0f722cfb 1413 'rollup/' . $rollupversion . "/yui-moodlesimple{$yuiformat}.js",
c8b9f6d9 1414 );
aaee690b 1415
ef7329dc 1416 if ($this->yui3loader->combine) {
2300a312
AN
1417 return '<script type="text/javascript" src="' .
1418 $this->yui3loader->local_comboBase .
1419 implode('&amp;', $baserollups) .
1420 '"></script>';
ef7329dc 1421 } else {
2300a312 1422 $code = '';
c8b9f6d9
AN
1423 foreach ($baserollups as $rollup) {
1424 $code .= '<script type="text/javascript" src="'.$this->yui3loader->local_comboBase.$rollup.'"></script>';
1425 }
2300a312 1426 return $code;
ef7329dc
PS
1427 }
1428
0bb38e8c
PS
1429 }
1430
0bb38e8c 1431 /**
a33e7407 1432 * Returns html tags needed for inclusion of theme CSS.
48d4fad1 1433 *
0bb38e8c
PS
1434 * @return string
1435 */
1436 protected function get_css_code() {
1437 // First of all the theme CSS, then any custom CSS
1438 // Please note custom CSS is strongly discouraged,
1439 // because it can not be overridden by themes!
1440 // It is suitable only for things like mod/data which accepts CSS from teachers.
692e9fa5 1441 $attributes = array('rel'=>'stylesheet', 'type'=>'text/css');
0bb38e8c 1442
2300a312
AN
1443 // Add the YUI code first. We want this to be overridden by any Moodle CSS.
1444 $code = $this->get_yui3lib_headcss();
1445
692e9fa5
SH
1446 // This line of code may look funny but it is currently required in order
1447 // to avoid MASSIVE display issues in Internet Explorer.
1448 // As of IE8 + YUI3.1.1 the reference stylesheet (firstthemesheet) gets
1449 // ignored whenever another resource is added until such time as a redraw
1450 // is forced, usually by moving the mouse over the affected element.
2300a312 1451 $code .= html_writer::tag('script', '/** Required in order to fix style inclusion problems in IE with YUI **/', array('id'=>'firstthemesheet', 'type'=>'text/css'));
0bb38e8c
PS
1452
1453 $urls = $this->cssthemeurls + $this->cssurls;
0bb38e8c
PS
1454 foreach ($urls as $url) {
1455 $attributes['href'] = $url;
1456 $code .= html_writer::empty_tag('link', $attributes) . "\n";
a33e7407 1457 // This id is needed in first sheet only so that theme may override YUI sheets loaded on the fly.
0bb38e8c
PS
1458 unset($attributes['id']);
1459 }
1460
1461 return $code;
1462 }
1463
1464 /**
a33e7407 1465 * Adds extra modules specified after printing of page header.
48d4fad1 1466 *
7edf3f7e 1467 * @return string
0bb38e8c
PS
1468 */
1469 protected function get_extra_modules_code() {
1470 if (empty($this->extramodules)) {
1471 return '';
1472 }
1473 return html_writer::script(js_writer::function_call('M.yui.add_module', array($this->extramodules)));
1474 }
1475
1476 /**
1477 * Generate any HTML that needs to go inside the <head> tag.
1478 *
1479 * Normally, this method is called automatically by the code that prints the
1480 * <head> tag. You should not normally need to call it in your own code.
1481 *
7edf3f7e
PS
1482 * @param moodle_page $page
1483 * @param core_renderer $renderer
0bb38e8c
PS
1484 * @return string the HTML code to to inside the <head> tag.
1485 */
1486 public function get_head_code(moodle_page $page, core_renderer $renderer) {
1487 global $CFG;
1488
a33e7407
PS
1489 // Note: the $page and $output are not stored here because it would
1490 // create circular references in memory which prevents garbage collection.
0bb38e8c
PS
1491 $this->init_requirements_data($page, $renderer);
1492
43bd8118 1493 $output = '';
0bb38e8c 1494
2300a312
AN
1495 // Add all standard CSS for this page.
1496 $output .= $this->get_css_code();
1497
15dedb11
TH
1498 // Set up the M namespace.
1499 $js = "var M = {}; M.yui = {};\n";
1500
1501 // Capture the time now ASAP during page load. This minimises the lag when
1502 // we try to relate times on the server to times in the browser.
1503 // An example of where this is used is the quiz countdown timer.
1504 $js .= "M.pageloadstarttime = new Date();\n";
1505
1506 // Add a subset of Moodle configuration to the M namespace.
1507 $js .= js_writer::set_variable('M.cfg', $this->M_cfg, false);
1508
a33e7407
PS
1509 // Set up global YUI3 loader object - this should contain all code needed by plugins.
1510 // Note: in JavaScript just use "YUI().use('overlay', function(Y) { .... });",
1511 // this needs to be done before including any other script.
b3c78403 1512 $js .= $this->YUI_config->get_config_functions();
2f422271 1513 $js .= js_writer::set_variable('YUI_config', $this->YUI_config, false) . "\n";
a33e7407 1514 $js .= "M.yui.loader = {modules: {}};\n"; // Backwards compatibility only, not used any more.
b3c78403 1515 $js = $this->YUI_config->update_header_js($js);
15dedb11 1516
0bb38e8c
PS
1517 $output .= html_writer::script($js);
1518
a33e7407 1519 // Add variables.
0bb38e8c
PS
1520 if ($this->jsinitvariables['head']) {
1521 $js = '';
1522 foreach ($this->jsinitvariables['head'] as $data) {
1523 list($var, $value) = $data;
1524 $js .= js_writer::set_variable($var, $value, true);
1525 }
1526 $output .= html_writer::script($js);
1527 }
1528
a33e7407 1529 // Mark head sending done, it is not possible to anything there.
0bb38e8c
PS
1530 $this->headdone = true;
1531
1532 return $output;
1533 }
1534
1535 /**
1536 * Generate any HTML that needs to go at the start of the <body> tag.
1537 *
1538 * Normally, this method is called automatically by the code that prints the
1539 * <head> tag. You should not normally need to call it in your own code.
1540 *
536f0460 1541 * @param renderer_base $renderer
0bb38e8c
PS
1542 * @return string the HTML code to go at the start of the <body> tag.
1543 */
536f0460 1544 public function get_top_of_body_code(renderer_base $renderer) {
a33e7407 1545 // First the skip links.
536f0460 1546 $output = $renderer->render_skip_links($this->skiplinks);
0bb38e8c 1547
2300a312
AN
1548 // YUI3 JS needs to be loaded early in the body. It should be cached well by the browser.
1549 $output .= $this->get_yui3lib_headcode();
1550
1551 // Add hacked jQuery support, it is not intended for standard Moodle distribution!
1552 $output .= $this->get_jquery_headcode();
1553
1554 // Link our main JS file, all core stuff should be there.
1555 $output .= html_writer::script('', $this->js_fix_url('/lib/javascript-static.js'));
1556
1557 // All the other linked things from HEAD - there should be as few as possible.
1558 if ($this->jsincludes['head']) {
1559 foreach ($this->jsincludes['head'] as $url) {
1560 $output .= html_writer::script('', $url);
1561 }
1562 }
1563
a33e7407 1564 // Then the clever trick for hiding of things not needed when JS works.
0bb38e8c
PS
1565 $output .= html_writer::script("document.body.className += ' jsenabled';") . "\n";
1566 $this->topofbodydone = true;
1567 return $output;
1568 }
1569
1570 /**
1571 * Generate any HTML that needs to go at the end of the page.
1572 *
1573 * Normally, this method is called automatically by the code that prints the
1574 * page footer. You should not normally need to call it in your own code.
1575 *
1576 * @return string the HTML code to to at the end of the page.
1577 */
1578 public function get_end_code() {
2cf81209 1579 global $CFG;
adeb96d2
DW
1580 $output = '';
1581
3acc64e3
AN
1582 // Set the log level for the JS logging.
1583 $logconfig = new stdClass();
cfb8019b 1584 $logconfig->level = 'warn';
3acc64e3
AN
1585 if ($CFG->debugdeveloper) {
1586 $logconfig->level = 'trace';
1587 }
1588 $this->js_call_amd('core/log', 'setConfig', array($logconfig));
1589
adeb96d2
DW
1590 // Call amd init functions.
1591 $output .= $this->get_amd_footercode();
a33e7407
PS
1592
1593 // Add other requested modules.
adeb96d2 1594 $output .= $this->get_extra_modules_code();
0bb38e8c 1595
6e2c417c
DW
1596 $this->js_init_code('M.util.js_complete("init");', true);
1597
a33e7407 1598 // All the other linked scripts - there should be as few as possible.
0bb38e8c
PS
1599 if ($this->jsincludes['footer']) {
1600 foreach ($this->jsincludes['footer'] as $url) {
1601 $output .= html_writer::script('', $url);
1602 }
1603 }
1604
a33e7407 1605 // Add all needed strings.
0d1d5423
AN
1606 // First add core strings required for some dialogues.
1607 $this->strings_for_js(array(
1608 'confirm',
1609 'yes',
1610 'no',
1611 'areyousure',
1612 'closebuttontitle',
1613 'unknownerror',
1614 ), 'moodle');
0bb38e8c 1615 if (!empty($this->stringsforjs)) {
af225f6c
1616 $strings = array();
1617 foreach ($this->stringsforjs as $component=>$v) {
1618 foreach($v as $indentifier => $langstring) {
1619 $strings[$component][$indentifier] = $langstring->out();
1620 }
1621 }
1622 $output .= html_writer::script(js_writer::set_variable('M.str', $strings));
0bb38e8c
PS
1623 }
1624
a33e7407 1625 // Add variables.
0bb38e8c
PS
1626 if ($this->jsinitvariables['footer']) {
1627 $js = '';
1628 foreach ($this->jsinitvariables['footer'] as $data) {
1629 list($var, $value) = $data;
1630 $js .= js_writer::set_variable($var, $value, true);
1631 }
1632 $output .= html_writer::script($js);
1633 }
1634
1635 $inyuijs = $this->get_javascript_code(false);
1636 $ondomreadyjs = $this->get_javascript_code(true);
1637 $jsinit = $this->get_javascript_init_code();
1638 $handlersjs = $this->get_event_handler_code();
1639
4e8afd56
DW
1640 // There is a global Y, make sure it is available in your scope.
1641 $js = "(function() {{$inyuijs}{$ondomreadyjs}{$jsinit}{$handlersjs}})();";
0bb38e8c
PS
1642
1643 $output .= html_writer::script($js);
1644
1645 return $output;
1646 }
1647
1648 /**
48d4fad1
SH
1649 * Have we already output the code in the <head> tag?
1650 *
3d3fae72 1651 * @return bool
0bb38e8c
PS
1652 */
1653 public function is_head_done() {
1654 return $this->headdone;
1655 }
1656
1657 /**
48d4fad1
SH
1658 * Have we already output the code at the start of the <body> tag?
1659 *
3d3fae72 1660 * @return bool
0bb38e8c
PS
1661 */
1662 public function is_top_of_body_done() {
1663 return $this->topofbodydone;
1664 }
b8459bba
TH
1665
1666 /**
1667 * Should we generate a bit of content HTML that is only required once on
1668 * this page (e.g. the contents of the modchooser), now? Basically, we call
1669 * {@link has_one_time_item_been_created()}, and if the thing has not already
1670 * been output, we return true to tell the caller to generate it, and also
1671 * call {@link set_one_time_item_created()} to record the fact that it is
1672 * about to be generated.
1673 *
1674 * That is, a typical usage pattern (in a renderer method) is:
1675 * <pre>
1676 * if (!$this->page->requires->should_create_one_time_item_now($thing)) {
1677 * return '';
1678 * }
1679 * // Else generate it.
1680 * </pre>
1681 *
1682 * @param string $thing identifier for the bit of content. Should be of the form
1683 * frankenstyle_things, e.g. core_course_modchooser.
1684 * @return bool if true, the caller should generate that bit of output now, otherwise don't.
1685 */
1686 public function should_create_one_time_item_now($thing) {
1687 if ($this->has_one_time_item_been_created($thing)) {
1688 return false;
1689 }
1690
1691 $this->set_one_time_item_created($thing);
1692 return true;
1693 }
1694
1695 /**
1696 * Has a particular bit of HTML that is only required once on this page
1697 * (e.g. the contents of the modchooser) already been generated?
1698 *
1699 * Normally, you can use the {@link should_create_one_time_item_now()} helper
1700 * method rather than calling this method directly.
1701 *
1702 * @param string $thing identifier for the bit of content. Should be of the form
1703 * frankenstyle_things, e.g. core_course_modchooser.
1704 * @return bool whether that bit of output has been created.
1705 */
1706 public function has_one_time_item_been_created($thing) {
1707 return isset($this->onetimeitemsoutput[$thing]);
1708 }
1709
1710 /**
1711 * Indicate that a particular bit of HTML that is only required once on this
1712 * page (e.g. the contents of the modchooser) has been generated (or is about to be)?
1713 *
1714 * Normally, you can use the {@link should_create_one_time_item_now()} helper
1715 * method rather than calling this method directly.
1716 *
1717 * @param string $thing identifier for the bit of content. Should be of the form
1718 * frankenstyle_things, e.g. core_course_modchooser.
1719 */
1720 public function set_one_time_item_created($thing) {
1721 if ($this->has_one_time_item_been_created($thing)) {
1722 throw new coding_exception($thing . ' is only supposed to be ouput ' .
1723 'once per page, but it seems to be being output again.');
1724 }
1725 return $this->onetimeitemsoutput[$thing] = true;
1726 }
0bb38e8c 1727}
0139ec3f 1728
cae21a32
ARN
1729/**
1730 * This class represents the YUI configuration.
1731 *
53ae22b9 1732 * @copyright 2013 Andrew Nicols
cae21a32
ARN
1733 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1734 * @since Moodle 2.5
1735 * @package core
1736 * @category output
1737 */
1738class YUI_config {
b3c78403
ARN
1739 /**
1740 * These settings must be public so that when the object is converted to json they are exposed.
c199993a
ARN
1741 * Note: Some of these are camelCase because YUI uses camelCase variable names.
1742 *
1743 * The settings are described and documented in the YUI API at:
b3c78403
ARN
1744 * - http://yuilibrary.com/yui/docs/api/classes/config.html
1745 * - http://yuilibrary.com/yui/docs/api/classes/Loader.html
1746 */
cae21a32
ARN
1747 public $debug = false;
1748 public $base;
1749 public $comboBase;
1750 public $combine;
1751 public $filter = null;
1752 public $insertBefore = 'firstthemesheet';
1753 public $groups = array();
1754 public $modules = array();
1755
b3c78403
ARN
1756 /**
1757 * @var array List of functions used by the YUI Loader group pattern recognition.
1758 */
1759 protected $jsconfigfunctions = array();
1760
cae21a32
ARN
1761 /**
1762 * Create a new group within the YUI_config system.
1763 *
1764 * @param String $name The name of the group. This must be unique and
1765 * not previously used.
1766 * @param Array $config The configuration for this group.
1767 * @return void
1768 */
1769 public function add_group($name, $config) {
1770 if (isset($this->groups[$name])) {
aa135c4d 1771 throw new coding_exception("A YUI configuration group for '{$name}' already exists. To make changes to this group use YUI_config->update_group().");
cae21a32
ARN
1772 }
1773 $this->groups[$name] = $config;
1774 }
1775
1776 /**
1777 * Update an existing group configuration
1778 *
1779 * Note, any existing configuration for that group will be wiped out.
1780 * This includes module configuration.
1781 *
1782 * @param String $name The name of the group. This must be unique and
1783 * not previously used.
1784 * @param Array $config The configuration for this group.
1785 * @return void
1786 */
1787 public function update_group($name, $config) {
1788 if (!isset($this->groups[$name])) {
aa135c4d 1789 throw new coding_exception('The Moodle YUI module does not exist. You must define the moodle module config using YUI_config->add_module_config first.');
cae21a32
ARN
1790 }
1791 $this->groups[$name] = $config;
1792 }
1793
b3c78403
ARN
1794 /**
1795 * Set the value of a configuration function used by the YUI Loader's pattern testing.
1796 *
1797 * Only the body of the function should be passed, and not the whole function wrapper.
1798 *
1799 * The JS function your write will be passed a single argument 'name' containing the
1800 * name of the module being loaded.
1801 *
1802 * @param $function String the body of the JavaScript function. This should be used i
1803 * @return String the name of the function to use in the group pattern configuration.
1804 */
1805 public function set_config_function($function) {
1806 $configname = 'yui' . (count($this->jsconfigfunctions) + 1) . 'ConfigFn';
1807 if (isset($this->jsconfigfunctions[$configname])) {
1808 throw new coding_exception("A YUI config function with this name already exists. Config function names must be unique.");
1809 }
1810 $this->jsconfigfunctions[$configname] = $function;
1811 return '@' . $configname . '@';
1812 }
1813
7cfe3ebb
AN
1814 /**
1815 * Allow setting of the config function described in {@see set_config_function} from a file.
1816 * The contents of this file are then passed to set_config_function.
1817 *
1818 * When jsrev is positive, the function is minified and stored in a MUC cache for subsequent uses.
1819 *
1820 * @param $file The path to the JavaScript function used for YUI configuration.
1821 * @return String the name of the function to use in the group pattern configuration.
1822 */
1823 public function set_config_source($file) {
1824 global $CFG;
1825 $cache = cache::make('core', 'yuimodules');
1826
1827 // Attempt to get the metadata from the cache.
1828 $keyname = 'configfn_' . $file;
1829 $fullpath = $CFG->dirroot . '/' . $file;
1830 if (!isset($CFG->jsrev) || $CFG->jsrev == -1) {
1831 $cache->delete($keyname);
1832 $configfn = file_get_contents($fullpath);
1833 } else {
1834 $configfn = $cache->get($keyname);
1835 if ($configfn === false) {
1836 require_once($CFG->libdir . '/jslib.php');
6b32d6bc 1837 $configfn = core_minify::js_files(array($fullpath));
7cfe3ebb
AN
1838 $cache->set($keyname, $configfn);
1839 }
1840 }
1841 return $this->set_config_function($configfn);
1842 }
1843
b3c78403
ARN
1844 /**
1845 * Retrieve the list of JavaScript functions for YUI_config groups.
1846 *
1847 * @return String The complete set of config functions
1848 */
1849 public function get_config_functions() {
1850 $configfunctions = '';
1851 foreach ($this->jsconfigfunctions as $functionname => $function) {
1852 $configfunctions .= "var {$functionname} = function(me) {";
1853 $configfunctions .= $function;
1854 $configfunctions .= "};\n";
1855 }
1856 return $configfunctions;
1857 }
1858
1859 /**
1860 * Update the header JavaScript with any required modification for the YUI Loader.
1861 *
1862 * @param $js String The JavaScript to manipulate.
1863 * @return String the modified JS string.
1864 */
1865 public function update_header_js($js) {
1866 // Update the names of the the configFn variables.
1867 // The PHP json_encode function cannot handle literal names so we have to wrap
1868 // them in @ and then replace them with literals of the same function name.
1869 foreach ($this->jsconfigfunctions as $functionname => $function) {
1870 $js = str_replace('"@' . $functionname . '@"', $functionname, $js);
1871 }
1872 return $js;
1873 }
1874
cae21a32
ARN
1875 /**
1876 * Add configuration for a specific module.
1877 *
1878 * @param String $name The name of the module to add configuration for.
1879 * @param Array $config The configuration for the specified module.
1880 * @param String $group The name of the group to add configuration for.
1881 * If not specified, then this module is added to the global
1882 * configuration.
1883 * @return void
1884 */
1885 public function add_module_config($name, $config, $group = null) {
1886 if ($group) {
1887 if (!isset($this->groups[$name])) {
aa135c4d 1888 throw new coding_exception('The Moodle YUI module does not exist. You must define the moodle module config using YUI_config->add_module_config first.');
cae21a32
ARN
1889 }
1890 if (!isset($this->groups[$group]['modules'])) {
1891 $this->groups[$group]['modules'] = array();
1892 }
1893 $modules = &$this->groups[$group]['modules'];
1894 } else {
1895 $modules = &$this->modules;
1896 }
1897 $modules[$name] = $config;
1898 }
aa135c4d
ARN
1899
1900 /**
da5e59b9 1901 * Add the moodle YUI module metadata for the moodle group to the YUI_config instance.
aa135c4d
ARN
1902 *
1903 * If js caching is disabled, metadata will not be served causing YUI to calculate
1904 * module dependencies as each module is loaded.
1905 *
1906 * If metadata does not exist it will be created and stored in a MUC entry.
1907 *
da5e59b9 1908 * @return void
aa135c4d 1909 */
da5e59b9 1910 public function add_moodle_metadata() {
aa135c4d
ARN
1911 global $CFG;
1912 if (!isset($this->groups['moodle'])) {
1913 throw new coding_exception('The Moodle YUI module does not exist. You must define the moodle module config using YUI_config->add_module_config first.');
1914 }
1915
1916 if (!isset($this->groups['moodle']['modules'])) {
1917 $this->groups['moodle']['modules'] = array();
1918 }
1919
1920 $cache = cache::make('core', 'yuimodules');
2c0b7d16 1921 if (!isset($CFG->jsrev) || $CFG->jsrev == -1) {
aa135c4d 1922 $metadata = array();
ce286101 1923 $metadata = $this->get_moodle_metadata();
aa135c4d
ARN
1924 $cache->delete('metadata');
1925 } else {
1926 // Attempt to get the metadata from the cache.
1927 if (!$metadata = $cache->get('metadata')) {
1928 $metadata = $this->get_moodle_metadata();
1929 $cache->set('metadata', $metadata);
1930 }
1931 }
1932
1933 // Merge with any metadata added specific to this page which was added manually.
1934 $this->groups['moodle']['modules'] = array_merge($this->groups['moodle']['modules'],
1935 $metadata);
aa135c4d
ARN
1936 }
1937
1938 /**
1939 * Determine the module metadata for all moodle YUI modules.
1940 *
1941 * This works through all modules capable of serving YUI modules, and attempts to get
1942 * metadata for each of those modules.
1943 *
1944 * @return Array of module metadata
1945 */
1946 private function get_moodle_metadata() {
1947 $moodlemodules = array();
53ae22b9 1948 // Core isn't a plugin type or subsystem - handle it seperately.
b0d1d941 1949 if ($module = $this->get_moodle_path_metadata(core_component::get_component_directory('core'))) {
aa135c4d
ARN
1950 $moodlemodules = array_merge($moodlemodules, $module);
1951 }
1952
53ae22b9 1953 // Handle other core subsystems.
9e19a0f0 1954 $subsystems = core_component::get_core_subsystems();
53ae22b9
ARN
1955 foreach ($subsystems as $subsystem => $path) {
1956 if (is_null($path)) {
1957 continue;
1958 }
53ae22b9
ARN
1959 if ($module = $this->get_moodle_path_metadata($path)) {
1960 $moodlemodules = array_merge($moodlemodules, $module);
1961 }
1962 }
1963
1964 // And finally the plugins.
9e19a0f0 1965 $plugintypes = core_component::get_plugin_types();
53ae22b9 1966 foreach ($plugintypes as $plugintype => $pathroot) {
bd3b3bba 1967 $pluginlist = core_component::get_plugin_list($plugintype);
aa135c4d
ARN
1968 foreach ($pluginlist as $plugin => $path) {
1969 if ($module = $this->get_moodle_path_metadata($path)) {
1970 $moodlemodules = array_merge($moodlemodules, $module);
1971 }
1972 }
1973 }
1974
1975 return $moodlemodules;
1976 }
1977
1978 /**
1979 * Helper function process and return the YUI metadata for all of the modules under the specified path.
1980 *
1981 * @param String $path the UNC path to the YUI src directory.
1982 * @return Array the complete array for frankenstyle directory.
1983 */
1984 private function get_moodle_path_metadata($path) {
1985 // Add module metadata is stored in frankenstyle_modname/yui/src/yui_modname/meta/yui_modname.json.
1986 $baseyui = $path . '/yui/src';
1987 $modules = array();
1988 if (is_dir($baseyui)) {
1989 $items = new DirectoryIterator($baseyui);
1990 foreach ($items as $item) {
1991 if ($item->isDot() or !$item->isDir()) {
1992 continue;
1993 }
1994 $metafile = realpath($baseyui . '/' . $item . '/meta/' . $item . '.json');
53ae22b9
ARN
1995 if (!is_readable($metafile)) {
1996 continue;
1997 }
aa135c4d
ARN
1998 $metadata = file_get_contents($metafile);
1999 $modules = array_merge($modules, (array) json_decode($metadata));
2000 }
2001 }
2002 return $modules;
2003 }
aea29737
AN
2004
2005 /**
2006 * Define YUI modules which we have been required to patch between releases.
2007 *
2008 * We must do this because we aggressively cache content on the browser, and we must also override use of the
2009 * external CDN which will serve the true authoritative copy of the code without our patches.
2010 *
2011 * @param String combobase The local combobase
2012 * @param String yuiversion The current YUI version
2013 * @param Int patchlevel The patch level we're working to for YUI
2014 * @param Array patchedmodules An array containing the names of the patched modules
2015 * @return void
2016 */
2017 public function define_patched_core_modules($combobase, $yuiversion, $patchlevel, $patchedmodules) {
2018 // The version we use is suffixed with a patchlevel so that we can get additional revisions between YUI releases.
2019 $subversion = $yuiversion . '_' . $patchlevel;
2020
2021 if ($this->comboBase == $combobase) {
2022 // If we are using the local combobase in the loader, we can add a group and still make use of the combo
2023 // loader. We just need to specify a different root which includes a slightly different YUI version number
2024 // to include our patchlevel.
2025 $patterns = array();
2026 $modules = array();
2027 foreach ($patchedmodules as $modulename) {
2028 // We must define the pattern and module here so that the loader uses our group configuration instead of
2029 // the standard module definition. We may lose some metadata provided by upstream but this will be
2030 // loaded when the module is loaded anyway.
2031 $patterns[$modulename] = array(
2032 'group' => 'yui-patched',
2033 );
2034 $modules[$modulename] = array();
2035 }
2036
2037 // Actually add the patch group here.
2038 $this->add_group('yui-patched', array(
2039 'combine' => true,
2040 'root' => $subversion . '/',
2041 'patterns' => $patterns,
2042 'modules' => $modules,
2043 ));
2044
2045 } else {
2046 // The CDN is in use - we need to instead use the local combobase for this module and override the modules
2047 // definition. We cannot use the local base - we must use the combobase because we cannot invalidate the
2048 // local base in browser caches.
2049 $fullpathbase = $combobase . $subversion . '/';
2050 foreach ($patchedmodules as $modulename) {
2051 $this->modules[$modulename] = array(
2052 'fullpath' => $fullpathbase . $modulename . '/' . $modulename . '-min.js'
2053 );
2054 }
2055 }
2056 }
cae21a32
ARN
2057}
2058
0139ec3f
PS
2059/**
2060 * Invalidate all server and client side JS caches.
0139ec3f
PS
2061 */
2062function js_reset_all_caches() {
2063 global $CFG;
0139ec3f 2064
f11db1a6
PS
2065 $next = time();
2066 if (isset($CFG->jsrev) and $next <= $CFG->jsrev and $CFG->jsrev - $next < 60*60) {
2067 // This resolves problems when reset is requested repeatedly within 1s,
2068 // the < 1h condition prevents accidental switching to future dates
2069 // because we might not recover from it.
2070 $next = $CFG->jsrev+1;
2071 }
2072
2073 set_config('jsrev', $next);
67233725 2074}