Updated the HEAD build version to 20091227
[moodle.git] / lib / ajax / ajaxlib.php
CommitLineData
f72f94a2 1<?php
b2330db6 2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18
19/**
20 * Library functions to facilitate the use of JavaScript in Moodle.
21 *
22 * @package moodlecore
23 * @copyright 2009 Tim Hunt
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27
28/**
2c144fc3 29 * Initialise a {@link page_requirements_manager} with the bits of JavaScript that every
30 * Moodle page should have.
b2330db6 31 *
32 * @param page_requirements_manager $requires The page_requirements_manager to initialise.
33 */
34function setup_core_javascript(page_requirements_manager $requires) {
78946b9b 35 global $CFG, $OUTPUT, $PAGE;
b2330db6 36
37 // JavaScript should always work with $CFG->httpswwwroot rather than $CFG->wwwroot.
38 // Otherwise, in some situations, users will get warnings about insecure content
39 // on sercure pages from their web browser.
40
41 $config = array(
42 'wwwroot' => $CFG->httpswwwroot, // Yes, really. See above.
b2330db6 43 'sesskey' => sesskey(),
b5d0cafc 44 'loadingicon' => $OUTPUT->pix_url('i/loading_small', 'moodle')->out_raw(),
78946b9b
PS
45 'themerev' => theme_get_revision(),
46 'theme' => $PAGE->theme->name,
b2330db6 47 );
48 if (debugging('', DEBUG_DEVELOPER)) {
49 $config['developerdebug'] = true;
50 }
51 $requires->data_for_js('moodle_cfg', $config)->in_head();
52
cf615522 53 if (debugging('', DEBUG_DEVELOPER)) {
f44b10ed 54 $requires->yui2_lib('logger');
cf615522 55 }
56
9fd49f5f
PS
57 // YUI3 init code
58 $requires->yui3_lib(array('cssreset', 'cssbase', 'cssfonts', 'cssgrids')); // full CSS reset
59 $requires->yui3_lib('yui'); // allows autoloading of everything else
60
61
d4a03c00 62 $requires->skip_link_to('maincontent', get_string('tocontent', 'access'));
63
117bd748 64 // Note that, as a short-cut, the code
b2330db6 65 // $js = "document.body.className += ' jsenabled';\n";
2c144fc3 66 // is hard-coded in {@link page_requirements_manager::get_top_of_body_code)
f44b10ed
PS
67 $requires->yui2_lib('container');
68 $requires->yui2_lib('connection');
5f56f0a8 69 $requires->string_for_js('confirmation', 'admin');
f8065dd2 70 $requires->string_for_js('cancel', 'moodle');
71 $requires->string_for_js('yes', 'moodle');
117bd748 72 $requires->js_function_call('init_help_icons');
b2330db6 73}
74
75
76/**
77 * This class tracks all the things that are needed by the current page.
78 *
79 * Normally, the only instance of this class you will need to work with is the
80 * one accessible via $PAGE->requires.
81 *
82 * Typical useage would be
9ca13950 83 * <pre>
78946b9b 84 * $PAGE->requires->css('mod/mymod/userstyles.php?id='.$id); // not overriddable via themes!
b2330db6 85 * $PAGE->requires->js('mod/mymod/script.js');
86 * $PAGE->requires->js('mod/mymod/small_but_urgent.js')->in_head();
87 * $PAGE->requires->js_function_call('init_mymod', array($data))->on_dom_ready();
9ca13950 88 * </pre>
b2330db6 89 *
2c144fc3 90 * There are some natural restrictions on some methods. For example, {@link css()}
91 * can only be called before the <head> tag is output. See the comments on the
92 * individual methods for details.
93 *
b2330db6 94 * @copyright 2009 Tim Hunt
95 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5c5418fe 96 * @since Moodle 2.0
b2330db6 97 */
98class page_requirements_manager {
99 const WHEN_IN_HEAD = 0;
100 const WHEN_TOP_OF_BODY = 10;
101 const WHEN_AT_END = 20;
102 const WHEN_ON_DOM_READY = 30;
103
9e43d2b3 104 protected $linkedrequirements = array();
b2330db6 105 protected $stringsforjs = array();
106 protected $requiredjscode = array();
107
108 protected $variablesinitialised = array('mstr' => 1); // 'mstr' is special. See string_for_js.
109
110 protected $headdone = false;
111 protected $topofbodydone = false;
112
985d1d1d 113 /** YUI PHPLoader instance responsible for YUI3 laoding in page head only */
4c907141 114 protected $yui3loader;
985d1d1d
PS
115 /** YUI PHPLoader instance responsible for YUI2 laoding */
116 protected $yui2loader;
4c907141
PS
117
118 /**
119 * Page requirements constructor.
120 */
121 public function __construct() {
122 global $CFG;
123 require_once("$CFG->libdir/yui/phploader/phploader/loader.php");
124
125 $this->yui3loader = new YAHOO_util_Loader($CFG->yui3version);
985d1d1d 126 $this->yui2loader = new YAHOO_util_Loader($CFG->yui2version);
4c907141
PS
127
128 // set up some loader options
4c907141
PS
129 if (debugging('', DEBUG_DEVELOPER)) {
130 $this->yui3loader->filter = YUI_DEBUG; // alternatively we could use just YUI_RAW here
985d1d1d 131 $this->yui2loader->filter = YUI_DEBUG; // alternatively we could use just YUI_RAW here
4c907141
PS
132 } else {
133 $this->yui3loader->filter = null;
985d1d1d 134 $this->yui2loader->filter = null;
95fbe49b 135 }
4c907141
PS
136 if (!empty($CFG->useexternalyui)) {
137 $this->yui3loader->base = 'http://yui.yahooapis.com/' . $CFG->yui3version . '/build/';
985d1d1d 138 $this->yui2loader->base = 'http://yui.yahooapis.com/' . $CFG->yui2version . '/build/';
4c907141 139 } else {
aa42314d
PS
140 $this->yui3loader->base = $CFG->httpswwwroot . '/lib/yui/'. $CFG->yui3version . '/build/';
141 $this->yui2loader->base = $CFG->httpswwwroot . '/lib/yui/'. $CFG->yui2version . '/build/';
4c907141
PS
142 }
143
60f2c866 144 // This file helps to minimise number of http requests and implements proper caching
aa42314d
PS
145 $this->yui3loader->comboBase = $CFG->httpswwwroot . '/theme/yui_combo.php?';
146 $this->yui2loader->comboBase = $CFG->httpswwwroot . '/theme/yui_combo.php?';
147
148 // enable combo loader? this significantly helps with caching and performance
149 $this->yui3loader->combine = !empty($CFG->yuicomboloading);
150 $this->yui2loader->combine = !empty($CFG->yuicomboloading);
4c907141
PS
151 }
152
b2330db6 153 /**
2c144fc3 154 * Ensure that the specified JavaScript file is linked to from this page.
155 *
156 * By default the link is put at the end of the page, since this gives best page-load performance.
b2330db6 157 *
158 * Even if a particular script is requested more than once, it will only be linked
159 * to once.
160 *
161 * @param $jsfile The path to the .js file, relative to $CFG->dirroot / $CFG->wwwroot.
162 * No leading slash. For example 'mod/mymod/customscripts.js';
163 * @param boolean $fullurl This parameter is intended for internal use only.
cf615522 164 * However, in exceptional circumstances you may wish to use it to link
165 * to JavaScript on another server. For example, lib/recaptchalib.php has to
166 * do this. This really should only be done in exceptional circumstances. This
167 * may change in the future without warning.
b2330db6 168 * (If true, $jsfile is treaded as a full URL, not relative $CFG->wwwroot.)
2c144fc3 169 * @return required_js The required_js object. This allows you to control when the
170 * link to the script is output by calling methods like {@link required_js::asap()} or
171 * {@link required_js::in_head()}.
b2330db6 172 */
173 public function js($jsfile, $fullurl = false) {
174 global $CFG;
175 if (!$fullurl) {
d76b8a20 176 // strtok is used to trim off any GET string arguments before looking for the file
177 if (!file_exists($CFG->dirroot . '/' . strtok($jsfile, '?'))) {
b2330db6 178 throw new coding_exception('Attept to require a JavaScript file that does not exist.', $jsfile);
179 }
180 $url = $CFG->httpswwwroot . '/' . $jsfile;
181 } else {
182 $url = $jsfile;
183 }
9e43d2b3 184 if (!isset($this->linkedrequirements[$url])) {
185 $this->linkedrequirements[$url] = new required_js($this, $url);
b2330db6 186 }
9e43d2b3 187 return $this->linkedrequirements[$url];
b2330db6 188 }
189
190 /**
4c907141 191 * Ensure that the specified YUI2 library file, and all its required dependancies,
2c144fc3 192 * are linked to from this page.
193 *
194 * By default the link is put at the end of the page, since this gives best page-load
195 * performance. Optional dependencies are not loaded automatically - if you want
196 * them you will need to load them first with other calls to this method.
b2330db6 197 *
b2330db6 198 * Even if a particular library is requested more than once (perhaps as a dependancy
199 * of other libraries) it will only be linked to once.
200 *
985d1d1d
PS
201 * The library is leaded as soon as possible, if $OUTPUT->header() not used yet it
202 * is put into the page header, otherwise it is loaded in the page footer.
203 *
204 * @param string|array $libname the name of the YUI2 library you require. For example 'autocomplete'.
205 * @return void
b2330db6 206 */
f44b10ed 207 public function yui2_lib($libname) {
985d1d1d
PS
208 $libnames = (array)$libname;
209 foreach ($libnames as $lib) {
210 $this->yui2loader->load($lib);
b2330db6 211 }
b2330db6 212 }
213
4c907141
PS
214 /**
215 * Ensure that the specified YUI3 library file, and all its required dependancies,
216 * are laoded automatically on this page.
985d1d1d 217 *
4c907141
PS
218 * @param string|array $libname the name of the YUI3 library you require. For example 'overlay'.
219 * @return void
220 */
221 public function yui3_lib($libname) {
222 if ($this->headdone) {
223 throw new coding_exception('YUI3 libraries can be preloaded by PHP only from HEAD, please use YUI autoloading instead: ', $stylesheet);
224 }
225 $libnames = (array)$libname;
226 foreach ($libnames as $lib) {
227 $this->yui3loader->load($lib);
228 }
229 }
230
b2330db6 231 /**
2c144fc3 232 * Ensure that the specified CSS file is linked to from this page.
233 *
234 * Because stylesheet links must go in the <head> part of the HTML, you must call
235 * this function before {@link get_head_code()} is called. That normally means before
b2330db6 236 * the call to print_header. If you call it when it is too late, an exception
237 * will be thrown.
238 *
239 * Even if a particular style sheet is requested more than once, it will only
240 * be linked to once.
241 *
9ca13950 242 * @param string $stylesheet The path to the .css file, relative to
b2330db6 243 * $CFG->dirroot / $CFG->wwwroot. No leading slash. For example
244 * 'mod/mymod/styles.css';
245 * @param boolean $fullurl This parameter is intended for internal use only.
246 * (If true, $stylesheet is treaded as a full URL, not relative $CFG->wwwroot.)
247 */
248 public function css($stylesheet, $fullurl = false) {
249 global $CFG;
9f509643
DC
250 if (!$fullurl) {
251 if (!file_exists($CFG->dirroot . '/' . strtok($stylesheet, '?'))) {
252 throw new coding_exception('Attempt to require a CSS file that does not exist.', $stylesheet);
253 }
254 $url = $CFG->httpswwwroot . '/' . $stylesheet;
255 } else {
256 $url = $stylesheet;
257 }
b2330db6 258
70f83b58 259 if (isset($this->linkedrequirements[$url])) {
9f509643 260 // already required, ignore it
70f83b58 261 return;
b2330db6 262 } else {
70f83b58
DC
263 if ($this->headdone) {
264 throw new coding_exception('Cannot require a CSS file after &lt;head> has been printed.', $stylesheet);
265 }
9e43d2b3 266 $this->linkedrequirements[$url] = new required_css($this, $url);
b2330db6 267 }
268 }
269
270 /**
2c144fc3 271 * Ensure that a skip link to a given target is printed at the top of the <body>.
272 *
273 * You must call this function before {@link get_top_of_body_code()}, (if not, an exception
b2330db6 274 * will be thrown). That normally means you must call this before the call to print_header.
275 *
276 * If you ask for a particular skip link to be printed, it is then your responsibility
2c144fc3 277 * to ensure that the appropraite <a name="..."> tag is printed in the body of the
b2330db6 278 * page, so that the skip link goes somewhere.
279 *
280 * Even if a particular skip link is requested more than once, only one copy of it will be output.
281 *
282 * @param $target the name of anchor this link should go to. For example 'maincontent'.
283 * @param $linktext The text to use for the skip link. Normally get_string('skipto', 'access', ...);
284 */
285 public function skip_link_to($target, $linktext) {
9e43d2b3 286 if (!isset($this->linkedrequirements[$target])) {
287 $this->linkedrequirements[$target] = new required_skip_link($this, $target, $linktext);
b2330db6 288 }
289 }
290
291 /**
292 * Ensure that the specified JavaScript function is called from an inline script
2c144fc3 293 * somewhere on this page.
294 *
295 * By default the call will be put in a script tag at the
b2330db6 296 * end of the page, since this gives best page-load performance.
297 *
298 * If you request that a particular function is called several times, then
299 * that is what will happen (unlike linking to a CSS or JS file, where only
300 * one link will be output).
301 *
302 * @param string $function the name of the JavaScritp function to call. Can
cf615522 303 * be a compound name like 'YAHOO.util.Event.addListener'. Can also be
304 * used to create and object by using a 'function name' like 'new user_selector'.
b2330db6 305 * @param array $arguments and array of arguments to be passed to the function.
306 * When generating the function call, this will be escaped using json_encode,
307 * so passing objects and arrays should work.
2c144fc3 308 * @return required_js_function_call The required_js_function_call object.
b2330db6 309 * This allows you to control when the link to the script is output by
2c144fc3 310 * calling methods like {@link required_js_function_call::in_head()},
311 * {@link required_js_function_call::at_top_of_body()},
312 * {@link required_js_function_call::on_dom_ready()} or
313 * {@link required_js_function_call::after_delay()} methods.
b2330db6 314 */
315 public function js_function_call($function, $arguments = array()) {
316 $requirement = new required_js_function_call($this, $function, $arguments);
317 $this->requiredjscode[] = $requirement;
318 return $requirement;
319 }
320
321 /**
2c144fc3 322 * Make a language string available to JavaScript.
117bd748 323 *
2c144fc3 324 * All the strings will be available in a mstr object in the global namespace.
325 * So, for example, after a call to $PAGE->requires->string_for_js('course', 'moodle');
b2330db6 326 * then the JavaScript variable mstr.moodle.course will be 'Course', or the
327 * equivalent in the current language.
328 *
329 * The arguments to this function are just like the arguments to get_string
330 * except that $module is not optional, and there are limitations on how you
331 * use $a. Because each string is only stored once in the JavaScript (based
332 * on $identifier and $module) you cannot get the same string with two different
333 * values of $a. If you try, an exception will be thrown.
334 *
335 * If you do need the same string expanded with different $a values, then
336 * the solution is to put them in your own data structure (e.g. and array)
2c144fc3 337 * that you pass to JavaScript with {@link data_for_js()}.
b2330db6 338 *
339 * @param string $identifier the desired string.
340 * @param string $module the language file to look in.
341 * @param mixed $a any extra data to add into the string (optional).
342 */
343 public function string_for_js($identifier, $module, $a = NULL) {
344 $string = get_string($identifier, $module, $a);
345 if (!$module) {
2c144fc3 346 throw new coding_exception('The $module parameter is required for page_requirements_manager::string_for_js.');
b2330db6 347 }
348 if (isset($this->stringsforjs[$module][$identifier]) && $this->stringsforjs[$module][$identifier] != $string) {
349 throw new coding_exception("Attempt to re-define already required string '$identifier' " .
350 "from lang file '$module'. Did you already ask for it with a different \$a?");
351 }
352 $this->stringsforjs[$module][$identifier] = $string;
353 }
354
747b85cb 355 /**
356 * Make an array of language strings available for JS
357 *
358 * This function calls the above function {@link string_for_js()} for each requested
359 * string in the $identifiers array that is passed to the argument for a single module
360 * passed in $module.
361 *
362 * <code>
363 * $PAGE->strings_for_js(Array('one', 'two', 'three'), 'mymod', Array('a', null, 3));
364 *
365 * // The above is identifical to calling
366 *
367 * $PAGE->string_for_js('one', 'mymod', 'a');
368 * $PAGE->string_for_js('two', 'mymod');
369 * $PAGE->string_for_js('three', 'mymod', 3);
370 * </code>
371 *
372 * @param array $identifiers An array of desired strings
373 * @param string $module The module to load for
374 * @param mixed $a This can either be a single variable that gets passed as extra
375 * information for every string or it can be an array of mixed data where the
376 * key for the data matches that of the identifier it is meant for.
377 *
378 */
379 public function strings_for_js($identifiers, $module, $a=NULL) {
380 foreach ($identifiers as $key => $identifier) {
381 if (is_array($a) && array_key_exists($key, $a)) {
382 $extra = $a[$key];
383 } else {
384 $extra = $a;
385 }
386 $this->string_for_js($identifier, $module, $extra);
387 }
388 }
389
b2330db6 390 /**
2c144fc3 391 * Make some data from PHP available to JavaScript code.
117bd748 392 *
2c144fc3 393 * For example, if you call
394 * <pre>
b2330db6 395 * $PAGE->requires->data_for_js('mydata', array('name' => 'Moodle'));
2c144fc3 396 * </pre>
b2330db6 397 * then in JavsScript mydata.name will be 'Moodle'.
398 *
399 * You cannot call this function more than once with the same variable name
400 * (if you try, it will throw an exception). Your code should prepare all the
401 * date you want, and then pass it to this method. There is no way to change
402 * the value associated with a particular variable later.
403 *
404 * @param string $variable the the name of the JavaScript variable to assign the data to.
405 * Will probably work if you use a compound name like 'mybuttons.button[1]', but this
406 * should be considered an experimental feature.
407 * @param mixed $data The data to pass to JavaScript. This will be escaped using json_encode,
408 * so passing objects and arrays should work.
2c144fc3 409 * @return required_data_for_js The required_data_for_js object.
b2330db6 410 * This allows you to control when the link to the script is output by
2c144fc3 411 * calling methods like {@link required_data_for_js::asap()},
412 * {@link required_data_for_js::in_head()} or
8a0cb3a1 413 * {@link required_data_for_js::at_top_of_body()} methods.
b2330db6 414 */
415 public function data_for_js($variable, $data) {
416 if (isset($this->variablesinitialised[$variable])) {
417 throw new coding_exception("A variable called '" . $variable .
418 "' has already been passed ot JavaScript. You cannot overwrite it.");
419 }
420 $requirement = new required_data_for_js($this, $variable, $data);
421 $this->requiredjscode[] = $requirement;
422 $this->variablesinitialised[$variable] = 1;
423 return $requirement;
424 }
117bd748 425
647df7e3 426 /**
427 * Creates a YUI event handler.
428 *
429 * @param string $id The id of the DOM element that will be listening for the event
430 * @param string $event A valid DOM event (click, mousedown, change etc.)
431 * @param string $function The name of the function to call
432 * @param array $arguments An optional array of argument parameters to pass to the function
433 * @return required_event_handler The event_handler object
434 */
435 public function event_handler($id, $event, $function, $arguments=array()) {
f8065dd2 436 $requirement = new required_event_handler($this, $id, $event, $function, $arguments);
647df7e3 437 $this->requiredjscode[] = $requirement;
985d1d1d 438 $this->yui2_lib('event');
647df7e3 439 return $requirement;
440 }
b2330db6 441
442 /**
443 * Get the code for the linked resources that need to appear in a particular place.
444 * @param $when one of the WHEN_... constants.
445 * @return string the HTML that should be output in that place.
446 */
447 protected function get_linked_resources_code($when) {
448 $output = '';
9e43d2b3 449 foreach ($this->linkedrequirements as $requirement) {
b2330db6 450 if (!$requirement->is_done() && $requirement->get_when() == $when) {
451 $output .= $requirement->get_html();
452 $requirement->mark_done();
453 }
454 }
455 return $output;
456 }
457
458 /**
2c144fc3 459 * Get the inline JavaScript code that need to appear in a particular place.
b2330db6 460 * @param $when one of the WHEN_... constants.
461 * @return string the javascript that should be output in that place.
462 */
463 protected function get_javascript_code($when, $indent = '') {
464 $output = '';
465 foreach ($this->requiredjscode as $requirement) {
466 if (!$requirement->is_done() && $requirement->get_when() == $when) {
467 $output .= $indent . $requirement->get_js_code();
468 $requirement->mark_done();
469 }
470 }
471 return $output;
472 }
473
4c907141
PS
474 /**
475 * Returns basic YUI3 JS loading code.
985d1d1d 476 * YUI3 is using autoloading of both CSS and JS code.
4c907141
PS
477 *
478 * @return string
479 */
985d1d1d
PS
480 protected function get_yui3lib_headcode() {
481 return $this->yui3loader->css() . $this->yui3loader->script();
482 }
483
484 /**
485 * Returns basic YUI2 JS loading code.
486 * It can be called manually at any time.
487 *
488 * @return string JS embedding code
489 */
490 public function get_yui2lib_code() {
491 // All YUI2 CSS is loaded automatically
492 if ($this->headdone) {
493 return $this->yui2loader->script_embed();
494 } else {
495 return $this->yui2loader->script();
496 }
4c907141
PS
497 }
498
b2330db6 499 /**
2c144fc3 500 * Generate any HTML that needs to go inside the <head> tag.
501 *
502 * Normally, this method is called automatically by the code that prints the
503 * <head> tag. You should not normally need to call it in your own code.
b2330db6 504 *
2c144fc3 505 * @return string the HTML code to to inside the <head> tag.
b2330db6 506 */
507 public function get_head_code() {
cf615522 508 setup_core_javascript($this);
985d1d1d
PS
509 $output = $this->get_yui3lib_headcode();
510 $output .= $this->get_yui2lib_code();
4c907141 511 $output .= $this->get_linked_resources_code(self::WHEN_IN_HEAD);
b2330db6 512 $js = $this->get_javascript_code(self::WHEN_IN_HEAD);
513 $output .= ajax_generate_script_tag($js);
514 $this->headdone = true;
515 return $output;
516 }
517
518 /**
2c144fc3 519 * Generate any HTML that needs to go at the start of the <body> tag.
520 *
521 * Normally, this method is called automatically by the code that prints the
522 * <head> tag. You should not normally need to call it in your own code.
b2330db6 523 *
2c144fc3 524 * @return string the HTML code to go at the start of the <body> tag.
b2330db6 525 */
526 public function get_top_of_body_code() {
ac63efae 527 $output = '<div class="skiplinks">' . $this->get_linked_resources_code(self::WHEN_TOP_OF_BODY) . '</div>';
b2330db6 528 $js = "document.body.className += ' jsenabled';\n";
529 $js .= $this->get_javascript_code(self::WHEN_TOP_OF_BODY);
530 $output .= ajax_generate_script_tag($js);
531 $this->topofbodydone = true;
532 return $output;
533 }
534
535 /**
536 * Generate any HTML that needs to go at the end of the page.
537 *
2c144fc3 538 * Normally, this method is called automatically by the code that prints the
539 * page footer. You should not normally need to call it in your own code.
540 *
b2330db6 541 * @return string the HTML code to to at the end of the page.
542 */
543 public function get_end_code() {
985d1d1d
PS
544 $output = $this->get_yui2lib_code();
545 $output .= $this->get_linked_resources_code(self::WHEN_AT_END);
b2330db6 546
cf615522 547 if (!empty($this->stringsforjs)) {
548 array_unshift($this->requiredjscode, new required_data_for_js($this, 'mstr', $this->stringsforjs));
549 }
550
b2330db6 551 $js = $this->get_javascript_code(self::WHEN_AT_END);
552
553 $ondomreadyjs = $this->get_javascript_code(self::WHEN_ON_DOM_READY, ' ');
554 if ($ondomreadyjs) {
555 $js .= "YAHOO.util.Event.onDOMReady(function() {\n" . $ondomreadyjs . "});\n";
556 }
557
558 $output .= ajax_generate_script_tag($js);
559
560 return $output;
561 }
562
563 /**
2c144fc3 564 * @return boolean Have we already output the code in the <head> tag?
b2330db6 565 */
566 public function is_head_done() {
567 return $this->headdone;
568 }
569
570 /**
2c144fc3 571 * @return boolean Have we already output the code at the start of the <body> tag?
b2330db6 572 */
573 public function is_top_of_body_done() {
574 return $this->topofbodydone;
575 }
576}
577
578
579/**
580 * This is the base class for all sorts of requirements. just to factor out some
581 * common code.
582 *
583 * @copyright 2009 Tim Hunt
584 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5c5418fe 585 * @since Moodle 2.0
b2330db6 586 */
587abstract class requirement_base {
588 protected $manager;
589 protected $when;
590 protected $done = false;
591
592 /**
593 * Constructor. Normally the class and its subclasses should not be created
594 * directly. Client code should create them via a page_requirements_manager
595 * method like ->js(...).
596 *
597 * @param page_requirements_manager $manager the page_requirements_manager we are associated with.
598 */
599 protected function __construct(page_requirements_manager $manager) {
600 $this->manager = $manager;
601 }
602
603 /**
604 * Mark that this requirement has been satisfied (that is, that the HTML
2c144fc3 605 * returned by {@link get_html()} has been output.
b2330db6 606 * @return boolean has this requirement been satisfied yet? That is, has
2c144fc3 607 * that the HTML returned by {@link get_html()} has been output already.
b2330db6 608 */
609 public function is_done() {
610 return $this->done;
611 }
612
613 /**
614 * Mark that this requirement has been satisfied (that is, that the HTML
2c144fc3 615 * returned by {@link get_html()} has been output.
b2330db6 616 */
617 public function mark_done() {
618 $this->done = true;
619 }
620
621 /**
622 * Where on the page the HTML this requirement is meant to go.
2c144fc3 623 * @return integer One of the {@link page_requirements_manager}::WHEN_... constants.
b2330db6 624 */
625 public function get_when() {
626 return $this->when;
627 }
628}
629
630/**
631 * This class represents something that must be output somewhere in the HTML.
632 *
633 * Examples include links to JavaScript or CSS files. However, it should not
634 * necessarily be output immediately, we may have to wait for an appropriate time.
635 *
636 * @copyright 2009 Tim Hunt
637 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5c5418fe 638 * @since Moodle 2.0
b2330db6 639 */
640abstract class linked_requirement extends requirement_base {
641 protected $url;
642
643 /**
644 * Constructor. Normally the class and its subclasses should not be created
645 * directly. Client code should create them via a page_requirements_manager
646 * method like ->js(...).
647 *
648 * @param page_requirements_manager $manager the page_requirements_manager we are associated with.
649 * @param string $url The URL of the thing we are linking to.
650 */
651 protected function __construct(page_requirements_manager $manager, $url) {
652 parent::__construct($manager);
653 $this->url = $url;
654 }
655
656 /**
657 * @return string the HTML needed to satisfy this requirement.
658 */
659 abstract public function get_html();
660}
661
662
663/**
2c144fc3 664 * A subclass of {@link linked_requirement} to represent a requried JavaScript file.
b2330db6 665 *
666 * You should not create instances of this class directly. Instead you should
2c144fc3 667 * work with a {@link page_requirements_manager} - and probably the only
b2330db6 668 * page_requirements_manager you will ever need is the one at $PAGE->requires.
669 *
2c144fc3 670 * The methods {@link asap()}, {@link in_head()} and {@link at_top_of_body()}
671 * are indented to be used as a fluid API, so you can say things like
b2330db6 672 * $PAGE->requires->js('mod/mymod/script.js')->in_head();
673 *
674 * However, by default JavaScript files are included at the end of the HTML.
675 * This is recommended practice because it means that the web browser will only
676 * start loading the javascript files after the rest of the page is loaded, and
677 * that gives the best performance for users.
678 *
679 * @copyright 2009 Tim Hunt
680 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5c5418fe 681 * @since Moodle 2.0
b2330db6 682 */
683class required_js extends linked_requirement {
684 /**
685 * Constructor. Normally instances of this class should not be created
686 * directly. Client code should create them via the page_requirements_manager
2c144fc3 687 * method {@link page_requirements_manager::js()}.
b2330db6 688 *
689 * @param page_requirements_manager $manager the page_requirements_manager we are associated with.
690 * @param string $url The URL of the JavaScript file we are linking to.
691 */
692 public function __construct(page_requirements_manager $manager, $url) {
693 parent::__construct($manager, $url);
694 $this->when = page_requirements_manager::WHEN_AT_END;
695 }
696
697 public function get_html() {
cf615522 698 return ajax_get_link_to_script($this->url);
b2330db6 699 }
700
701 /**
702 * Indicate that the link to this JavaScript file should be output as soon as
703 * possible. That is, if this requirement has already been output, this method
2c144fc3 704 * does nothing. Otherwise, if the <head> tag has not yet been printed, the link
705 * to this script will be put in <head>. Otherwise, this method returns a
b2330db6 706 * fragment of HTML that the caller is responsible for outputting as soon as
707 * possible. In fact, it is recommended that you only call this function from
708 * an echo statement, like:
2c144fc3 709 * <pre>
b2330db6 710 * echo $PAGE->requires->js(...)->asap();
2c144fc3 711 * </pre>
b2330db6 712 *
713 * @return string The HTML required to include this JavaScript file. The caller
714 * is responsible for outputting this HTML promptly.
715 */
716 public function asap() {
b2330db6 717 if (!$this->manager->is_head_done()) {
718 $this->in_head();
719 return '';
4262a2f8 720 } else {
721 return $this->now();
722 }
723 }
724
725 /**
726 * Return the required JavaScript immediately, so it can be included in some
727 * HTML that is being built.
728 *
729 * This is not really recommeneded. But is necessary in some legacy code that
730 * includes a .js files that does document.write.
731 *
732 * @return string The HTML for the script tag. The caller
733 * is responsible for making sure it is output.
734 */
735 public function now() {
736 if ($this->is_done()) {
737 return '';
b2330db6 738 }
bfbbfdeb 739 $output = $this->get_html();
b2330db6 740 $this->mark_done();
bfbbfdeb 741 return $output;
b2330db6 742 }
743
744 /**
745 * Indicate that the link to this JavaScript file should be output in the
2c144fc3 746 * <head> section of the HTML. If it too late for this request to be
b2330db6 747 * satisfied, an exception is thrown.
748 */
749 public function in_head() {
750 if ($this->is_done() || $this->when <= page_requirements_manager::WHEN_IN_HEAD) {
751 return;
752 }
753 if ($this->manager->is_head_done()) {
754 throw new coding_exception('Too late to ask for a JavaScript file to be linked to from &lt;head>.');
755 }
756 $this->when = page_requirements_manager::WHEN_IN_HEAD;
757 }
758
759 /**
760 * Indicate that the link to this JavaScript file should be output at the top
2c144fc3 761 * of the <body> section of the HTML. If it too late for this request to be
b2330db6 762 * satisfied, an exception is thrown.
763 */
764 public function at_top_of_body() {
765 if ($this->is_done() || $this->when <= page_requirements_manager::WHEN_TOP_OF_BODY) {
766 return;
767 }
768 if ($this->manager->is_top_of_body_done()) {
769 throw new coding_exception('Too late to ask for a JavaScript file to be linked to from the top of &lt;body>.');
770 }
771 $this->when = page_requirements_manager::WHEN_TOP_OF_BODY;
772 }
773}
774
b2330db6 775
776/**
2c144fc3 777 * A subclass of {@link linked_requirement} to represent a required CSS file.
778 * Of course, all links to CSS files must go in the <head> section of the HTML.
b2330db6 779 *
780 * @copyright 2009 Tim Hunt
781 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5c5418fe 782 * @since Moodle 2.0
b2330db6 783 */
784class required_css extends linked_requirement {
785 /**
2c144fc3 786 * Constructor. Normally instances of this class should not be created directly.
787 * Client code should create them via the page_requirements_manager
788 * method {@link page_requirements_manager::css()}
b2330db6 789 *
790 * @param page_requirements_manager $manager the page_requirements_manager we are associated with.
791 * @param string $url The URL of the CSS file we are linking to.
792 */
793 public function __construct(page_requirements_manager $manager, $url) {
794 parent::__construct($manager, $url);
795 $this->when = page_requirements_manager::WHEN_IN_HEAD;
796 }
797
798 public function get_html() {
799 return '<link rel="stylesheet" type="text/css" href="' . $this->url . '" />' . "\n";;
800 }
801}
802
803
804/**
2c144fc3 805 * A subclass of {@link linked_requirement} to represent a skip link.
b2330db6 806 * A skip link is a concept from accessibility. You have some links like
807 * 'Skip to main content' linking to an #maincontent anchor, at the start of the
2c144fc3 808 * <body> tag, so that users using assistive technologies like screen readers
b2330db6 809 * can easily get to the main content without having to work their way through
810 * any navigation, blocks, etc. that comes before it in the HTML.
811 *
812 * @copyright 2009 Tim Hunt
813 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5c5418fe 814 * @since Moodle 2.0
b2330db6 815 */
816class required_skip_link extends linked_requirement {
817 protected $linktext;
818
819 /**
2c144fc3 820 * Constructor. Normally instances of this class should not be created directly.
821 * Client code should create them via the page_requirements_manager
f44b10ed 822 * method {@link page_requirements_manager::yui2_lib()}.
b2330db6 823 *
824 * @param page_requirements_manager $manager the page_requirements_manager we are associated with.
825 * @param string $target the name of the anchor in the page we are linking to.
826 * @param string $linktext the test to use for the link.
827 */
828 public function __construct(page_requirements_manager $manager, $target, $linktext) {
829 parent::__construct($manager, $target);
830 $this->when = page_requirements_manager::WHEN_TOP_OF_BODY;
831 $this->linktext = $linktext;
832 }
833
834 public function get_html() {
ac63efae 835 return '<a class="skip" href="#' . $this->url . '">' . $this->linktext . "</a>\n";
b2330db6 836 }
837}
838
839
840/**
841 * This is the base class for requirements that are JavaScript code.
842 *
843 * @copyright 2009 Tim Hunt
844 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5c5418fe 845 * @since Moodle 2.0
b2330db6 846 */
847abstract class required_js_code extends requirement_base {
848
849 /**
2c144fc3 850 * Constructor.
b2330db6 851 * @param page_requirements_manager $manager the page_requirements_manager we are associated with.
852 */
853 protected function __construct(page_requirements_manager $manager) {
854 parent::__construct($manager);
855 $this->when = page_requirements_manager::WHEN_AT_END;
856 }
857
858 /**
859 * @return string the JavaScript code needed to satisfy this requirement.
860 */
861 abstract public function get_js_code();
862
863 /**
864 * Indicate that the link to this JavaScript file should be output as soon as
865 * possible. That is, if this requirement has already been output, this method
2c144fc3 866 * does nothing. Otherwise, if the <head> tag has not yet been printed, the link
867 * to this script will be put in <head>. Otherwise, this method returns a
b2330db6 868 * fragment of HTML that the caller is responsible for outputting as soon as
869 * possible. In fact, it is recommended that you only call this function from
870 * an echo statement, like:
2c144fc3 871 * <pre>
b2330db6 872 * echo $PAGE->requires->js(...)->asap();
2c144fc3 873 * </pre>
b2330db6 874 *
c966c8a2 875 * @return string The HTML for the script tag. The caller
b2330db6 876 * is responsible for outputting this HTML promptly.
877 */
878 public function asap() {
c966c8a2 879 if ($this->manager->is_head_done()) {
880 return $this->now();
881 } else {
b2330db6 882 $this->in_head();
883 return '';
884 }
c966c8a2 885 }
886
887 /**
888 * Return the required JavaScript immediately, so it can be included in some
889 * HTML that is being built.
890 * @return string The HTML for the script tag. The caller
891 * is responsible for making sure it is output.
892 */
893 public function now() {
894 if ($this->is_done()) {
895 return '';
896 }
b2330db6 897 $js = $this->get_js_code();
898 $output = ajax_generate_script_tag($js);
899 $this->mark_done();
900 return $output;
901 }
902
903 /**
904 * Indicate that the link to this JavaScript file should be output in the
2c144fc3 905 * <head> section of the HTML. If it too late for this request to be
b2330db6 906 * satisfied, an exception is thrown.
907 */
908 public function in_head() {
909 if ($this->is_done() || $this->when <= page_requirements_manager::WHEN_IN_HEAD) {
910 return;
911 }
912 if ($this->manager->is_head_done()) {
913 throw new coding_exception('Too late to ask for some JavaScript code to be output in &lt;head>.');
914 }
915 $this->when = page_requirements_manager::WHEN_IN_HEAD;
916 }
917
918 /**
919 * Indicate that the link to this JavaScript file should be output at the top
2c144fc3 920 * of the <body> section of the HTML. If it too late for this request to be
b2330db6 921 * satisfied, an exception is thrown.
922 */
923 public function at_top_of_body() {
924 if ($this->is_done() || $this->when <= page_requirements_manager::WHEN_TOP_OF_BODY) {
925 return;
926 }
927 if ($this->manager->is_top_of_body_done()) {
928 throw new coding_exception('Too late to ask for some JavaScript code to be output at the top of &lt;body>.');
929 }
930 $this->when = page_requirements_manager::WHEN_TOP_OF_BODY;
931 }
932}
933
934
935/**
936 * This class represents a JavaScript function that must be called from the HTML
937 * page. By default the call will be made at the end of the page, but you can
2c144fc3 938 * chage that using the {@link asap()}, {@link in_head()}, etc. methods.
b2330db6 939 *
940 * @copyright 2009 Tim Hunt
941 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5c5418fe 942 * @since Moodle 2.0
b2330db6 943 */
944class required_js_function_call extends required_js_code {
945 protected $function;
946 protected $arguments;
cf615522 947 protected $delay = 0;
b2330db6 948
949 /**
2c144fc3 950 * Constructor. Normally instances of this class should not be created directly.
951 * Client code should create them via the page_requirements_manager
952 * method {@link page_requirements_manager::js_function_call()}.
b2330db6 953 *
954 * @param page_requirements_manager $manager the page_requirements_manager we are associated with.
955 * @param string $function the name of the JavaScritp function to call.
956 * Can be a compound name like 'YAHOO.util.Event.addListener'.
957 * @param array $arguments and array of arguments to be passed to the function.
958 * When generating the function call, this will be escaped using json_encode,
959 * so passing objects and arrays should work.
960 */
961 public function __construct(page_requirements_manager $manager, $function, $arguments) {
962 parent::__construct($manager);
963 $this->function = $function;
964 $this->arguments = $arguments;
965 }
966
967 public function get_js_code() {
968 $quotedargs = array();
969 foreach ($this->arguments as $arg) {
970 $quotedargs[] = json_encode($arg);
971 }
cf615522 972 $js = $this->function . '(' . implode(', ', $quotedargs) . ');';
973 if ($this->delay) {
974 $js = 'setTimeout(function() { ' . $js . ' }, ' . ($this->delay * 1000) . ');';
975 }
976 return $js . "\n";
b2330db6 977 }
978
979 /**
980 * Indicate that this function should be called in YUI's onDomReady event.
981 *
982 * Not that this is probably not necessary most of the time. Just having the
983 * function call at the end of the HTML should normally be sufficient.
984 */
985 public function on_dom_ready() {
986 if ($this->is_done() || $this->when < page_requirements_manager::WHEN_AT_END) {
987 return;
988 }
f44b10ed 989 $this->manager->yui2_lib('event');
b2330db6 990 $this->when = page_requirements_manager::WHEN_ON_DOM_READY;
991 }
cf615522 992
993 /**
994 * Indicate that this function should be called a certain number of seconds
995 * after the page has finished loading. (More exactly, a number of seconds
996 * after the onDomReady event fires.)
997 *
998 * @param integer $seconds the number of seconds delay.
999 */
1000 public function after_delay($seconds) {
1001 if ($seconds) {
1002 $this->on_dom_ready();
1003 }
1004 $this->delay = $seconds;
1005 }
b2330db6 1006}
1007
1008
1009/**
1010 * This class represents some data from PHP that needs to be made available in a
1011 * global JavaScript variable. By default the data will be output at the end of
2c144fc3 1012 * the page, but you can chage that using the {@link asap()}, {@link in_head()}, etc. methods.
b2330db6 1013 *
1014 * @copyright 2009 Tim Hunt
1015 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5c5418fe 1016 * @since Moodle 2.0
b2330db6 1017 */
1018class required_data_for_js extends required_js_code {
1019 protected $variable;
1020 protected $data;
1021
1022 /**
2c144fc3 1023 * Constructor. Normally the class and its subclasses should not be created directly.
1024 * Client code should create them via the page_requirements_manager
1025 * method {@link page_requirements_manager::data_for_js()}.
b2330db6 1026 *
1027 * @param page_requirements_manager $manager the page_requirements_manager we are associated with.
1028 * @param string $variable the the name of the JavaScript variable to assign the data to.
1029 * Will probably work if you use a compound name like 'mybuttons.button[1]', but this
1030 * should be considered an experimental feature.
1031 * @param mixed $data The data to pass to JavaScript. This will be escaped using json_encode,
1032 * so passing objects and arrays should work.
1033 */
1034 public function __construct(page_requirements_manager $manager, $variable, $data) {
1035 parent::__construct($manager);
1036 $this->variable = $variable;
1037 $this->data = json_encode($data);
1038 // json_encode immediately, so that if $data is an object (and therefore was
1039 // passed in by reference) we get the data at the time the call was made, and
1040 // not whatever the data happened to be when this is output.
1041 }
1042
1043 public function get_js_code() {
1044 $prefix = 'var ';
1045 if (strpos($this->variable, '.') || strpos($this->variable, '[')) {
1046 $prefix = '';
1047 }
1048 return $prefix . $this->variable . ' = ' . $this->data . ";\n";
1049 }
1050}
1051
647df7e3 1052/**
117bd748 1053 * This class represents a Javascript event handler, listening for a
647df7e3 1054 * specific Event to occur on a DOM element identified by a given id.
117bd748 1055 * By default the data will be output at the end of the page, but you
647df7e3 1056 * can change that using the {@link asap()}, {@link in_head()}, etc. methods.
1057 *
1058 * @copyright 2009 Nicolas Connault
1059 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1060 * @since Moodle 2.0
1061 */
1062class required_event_handler extends required_js_code {
1063 protected $id;
1064 protected $event;
1065 protected $function;
1066 protected $args = array();
1067
1068 /**
1069 * Constructor. Normally the class and its subclasses should not be created directly.
1070 * Client code should create them via the page_requirements_manager
1071 * method {@link page_requirements_manager::data_for_js()}.
1072 *
1073 * @param page_requirements_manager $manager the page_requirements_manager we are associated with.
1074 * @param string $id The id of the DOM element that will be listening for the event
1075 * @param string $event A valid DOM event (click, mousedown, change etc.)
1076 * @param string $function The name of the function to call
1077 * @param array $arguments An optional array of argument parameters to pass to the function
1078 */
1079 public function __construct(page_requirements_manager $manager, $id, $event, $function, $args=array()) {
1080 parent::__construct($manager);
1081 $this->id = $id;
1082 $this->event = $event;
1083 $this->function = $function;
1084 $this->args = $args;
1085 }
1086
1087 public function get_js_code() {
f8065dd2 1088 $output = "YAHOO.util.Event.addListener('$this->id', '$this->event', $this->function";
647df7e3 1089 if (!empty($this->args)) {
1090 $output .= ', ' . json_encode($this->args);
1091 }
1092 return $output . ");\n";
1093 }
1094}
b2330db6 1095
1096/**
1097 * Generate a script tag containing the the specified code.
1098 *
1099 * @param string $js the JavaScript code
1100 * @return string HTML, the code wrapped in <script> tags.
1101 */
1102function ajax_generate_script_tag($js) {
cf615522 1103 if ($js) {
1104 return '<script type="text/javascript">' . "\n//<![CDATA[\n" .
1105 $js . "//]]>\n</script>\n";
1106 } else {
1107 return '';
1108 }
b2330db6 1109}
1110
1111
f72f94a2 1112/**
cf615522 1113 * Return the HTML required to link to a JavaScript file.
1114 * @param $url the URL of a JavaScript file.
1115 * @return string the required HTML.
f72f94a2 1116 */
cf615522 1117function ajax_get_link_to_script($url) {
1118 return '<script type="text/javascript" src="' . $url . '"></script>' . "\n";
9bb74178 1119}
88c5092a 1120
1121
1122/**
1123 * Returns whether ajax is enabled/allowed or not.
1124 */
c2a9fc91 1125function ajaxenabled($browsers = array()) {
88c5092a 1126
1127 global $CFG, $USER;
483f3067 1128
c2a9fc91 1129 if (!empty($browsers)) {
1130 $valid = false;
1131 foreach ($browsers as $brand => $version) {
1132 if (check_browser_version($brand, $version)) {
1133 $valid = true;
483f3067 1134 }
c2a9fc91 1135 }
483f3067 1136
c2a9fc91 1137 if (!$valid) {
1138 return false;
1139 }
1140 }
483f3067 1141
d499142e 1142 $ie = check_browser_version('MSIE', 6.0);
1143 $ff = check_browser_version('Gecko', 20051106);
1144 $op = check_browser_version('Opera', 9.0);
1145 $sa = check_browser_version('Safari', 412);
1146
1147 if (!$ie && !$ff && !$op && !$sa) {
1148 /** @see http://en.wikipedia.org/wiki/User_agent */
483f3067 1149 // Gecko build 20051107 is what is in Firefox 1.5.
88c5092a 1150 // We still have issues with AJAX in other browsers.
1151 return false;
1152 }
1153
2f11bfc0 1154 if (!empty($CFG->enableajax) && (!empty($USER->ajax) || !isloggedin())) {
88c5092a 1155 return true;
1156 } else {
1157 return false;
1158 }
1159}
9bb74178 1160
35b974da 1161
1162/**
2469f7ea 1163 * Used to create view of document to be passed to JavaScript on pageload.
1164 * We use this class to pass data from PHP to JavaScript.
35b974da 1165 */
4fc45e1d 1166class jsportal {
9bb74178 1167
0a0bb380 1168 var $currentblocksection = null;
9bb74178 1169 var $blocks = array();
0a0bb380 1170
9bb74178 1171
35b974da 1172 /**
1173 * Takes id of block and adds it
1174 */
2469f7ea 1175 function block_add($id, $hidden=false){
0a0bb380 1176 $hidden_binary = 0;
9bb74178 1177
1178 if ($hidden) {
1179 $hidden_binary = 1;
1180 }
35b974da 1181 $this->blocks[count($this->blocks)] = array($this->currentblocksection, $id, $hidden_binary);
0a0bb380 1182 }
9bb74178 1183
1184
2469f7ea 1185 /**
1186 * Prints the JavaScript code needed to set up AJAX for the course.
1187 */
1188 function print_javascript($courseid, $return=false) {
ddc66060 1189 global $CFG, $USER, $OUTPUT, $COURSE;
9bb74178 1190
d4df8fdc 1191 $blocksoutput = $output = '';
35b974da 1192 for ($i=0; $i<count($this->blocks); $i++) {
2469f7ea 1193 $blocksoutput .= "['".$this->blocks[$i][0]."',
1194 '".$this->blocks[$i][1]."',
1195 '".$this->blocks[$i][2]."']";
1196
1197 if ($i != (count($this->blocks) - 1)) {
35b974da 1198 $blocksoutput .= ',';
9bb74178 1199 }
1200 }
32f0b38a 1201 $output .= "<script type=\"text/javascript\">\n";
72d28452 1202 $output .= " main.portal.id = ".$courseid.";\n";
2469f7ea 1203 $output .= " main.portal.blocks = new Array(".$blocksoutput.");\n";
ddc66060 1204 $output .= " main.portal.strings['courseformat']='".$COURSE->format."';\n";
2469f7ea 1205 $output .= " main.portal.strings['wwwroot']='".$CFG->wwwroot."';\n";
f8eaeffa 1206 $output .= " main.portal.strings['marker']='".get_string('markthistopic', '', '_var_')."';\n";
1207 $output .= " main.portal.strings['marked']='".get_string('markedthistopic', '', '_var_')."';\n";
b1b4c2d7 1208 $output .= " main.portal.numsections = ".$COURSE->numsections.";\n";
f8eaeffa 1209 $output .= " main.portal.strings['hide']='".get_string('hide')."';\n";
1210 $output .= " main.portal.strings['hidesection']='".get_string('hidesection', '', '_var_')."';\n";
1211 $output .= " main.portal.strings['show']='".get_string('show')."';\n";
1212 $output .= " main.portal.strings['delete']='".get_string('delete')."';\n";
d2a11d46 1213 $output .= " main.portal.strings['move']='".get_string('move')."';\n";
f8eaeffa 1214 $output .= " main.portal.strings['movesection']='".get_string('movesection', '', '_var_')."';\n";
d2a11d46 1215 $output .= " main.portal.strings['moveleft']='".get_string('moveleft')."';\n";
1216 $output .= " main.portal.strings['moveright']='".get_string('moveright')."';\n";
4fc45e1d 1217 $output .= " main.portal.strings['update']='".get_string('update')."';\n";
d2a11d46 1218 $output .= " main.portal.strings['groupsnone']='".get_string('groupsnone')."';\n";
1219 $output .= " main.portal.strings['groupsseparate']='".get_string('groupsseparate')."';\n";
1220 $output .= " main.portal.strings['groupsvisible']='".get_string('groupsvisible')."';\n";
1221 $output .= " main.portal.strings['clicktochange']='".get_string('clicktochange')."';\n";
2469f7ea 1222 $output .= " main.portal.strings['deletecheck']='".get_string('deletecheck','','_var_')."';\n";
1223 $output .= " main.portal.strings['resource']='".get_string('resource')."';\n";
1224 $output .= " main.portal.strings['activity']='".get_string('activity')."';\n";
d4a1fcaf 1225 $output .= " main.portal.strings['sesskey']='".sesskey()."';\n";
b5d0cafc
PS
1226 $output .= " main.portal.icons['spacerimg']='".$OUTPUT->pix_url('spaces')."';\n";
1227 $output .= " main.portal.icons['marker']='".$OUTPUT->pix_url('i/marker')."';\n";
1228 $output .= " main.portal.icons['ihide']='".$OUTPUT->pix_url('i/hide')."';\n";
1229 $output .= " main.portal.icons['move_2d']='".$OUTPUT->pix_url('i/move_2d')."';\n";
1230 $output .= " main.portal.icons['show']='".$OUTPUT->pix_url('t/show')."';\n";
1231 $output .= " main.portal.icons['hide']='".$OUTPUT->pix_url('t/hide')."';\n";
1232 $output .= " main.portal.icons['delete']='".$OUTPUT->pix_url('t/delete')."';\n";
1233 $output .= " main.portal.icons['groupn']='".$OUTPUT->pix_url('t/groupn')."';\n";
1234 $output .= " main.portal.icons['groups']='".$OUTPUT->pix_url('t/groups')."';\n";
1235 $output .= " main.portal.icons['groupv']='".$OUTPUT->pix_url('t/groupv')."';\n";
ddedf979 1236 if (right_to_left()) {
b5d0cafc
PS
1237 $output .= " main.portal.icons['backwards']='".$OUTPUT->pix_url('t/right')."';\n";
1238 $output .= " main.portal.icons['forwards']='".$OUTPUT->pix_url('t/left')."';\n";
ddedf979 1239 } else {
b5d0cafc
PS
1240 $output .= " main.portal.icons['backwards']='".$OUTPUT->pix_url('t/left')."';\n";
1241 $output .= " main.portal.icons['forwards']='".$OUTPUT->pix_url('t/right')."';\n";
ddedf979 1242 }
1243
2469f7ea 1244 $output .= " onloadobj.load();\n";
4fc45e1d 1245 $output .= " main.process_blocks();\n";
35b974da 1246 $output .= "</script>";
2469f7ea 1247 if ($return) {
1248 return $output;
1249 } else {
1250 echo $output;
1251 }
0a0bb380 1252 }
9bb74178 1253
2469f7ea 1254}