Merged function rename from stable MDL-7594
[moodle.git] / lib / ajax / ajaxlib.php
CommitLineData
4488daf7 1<?php // Library functions for using AJAX with Moodle
9bb74178 2
3/**
8b8a8779 4 * Print require statements for javascript libraries.
5 * Takes in an array of either full paths or shortnames and it will translate
6 * them to full paths.
9bb74178 7 **/
2469f7ea 8function require_js($list) {
9bb74178 9 global $CFG;
2469f7ea 10 $output = '';
c081fdca 11
f68a7947 12 if (!check_browser_version('MSIE', 6.0)
13 && !check_browser_version('Firefox', 1.5)
2a0d551c 14 && !check_browser_version('Camino', '1.0.2')) {
f68a7947 15 // We still have issues with AJAX in other browsers.
4fc45e1d 16 return;
17 }
c081fdca 18
9bb74178 19 //list of shortname to filepath translations
20 $translatelist = array(
3972629d 21 'yui_yahoo' => '/lib/yui/yahoo/yahoo-min.js',
22 'yui_dom' => '/lib/yui/dom/dom-min.js',
23 'yui_event' => '/lib/yui/event/event-min.js',
24 'yui_dragdrop' => '/lib/yui/dragdrop/dragdrop-min.js',
25 'yui_logger' => '/lib/yui/logger/logger-min.js',
26 'yui_connection' => '/lib/yui/connection/connection-min.js',
3e1e2b69 27 'ajaxcourse_blocks' => '/lib/ajax/block_classes.js',
28 'ajaxcourse_sections' => '/lib/ajax/section_classes.js',
9bb74178 29 'ajaxcourse' => '/lib/ajax/ajaxcourse.js'
30 );
31
32
35b974da 33 for ($i=0; $i<count($list); $i++) {
9bb74178 34 if ($translatelist[$list[$i]]) {
2469f7ea 35 $output .= "<script type='text/javascript' src='".$CFG->wwwroot.''.$translatelist[$list[$i]]."'></script>\n";
3972629d 36 if ($translatelist[$list[$i]] == '/lib/yui/logger/logger-min.js') {
2469f7ea 37 // Special case. We need the css.
38 $output .= "<link type='text/css' rel='stylesheet' href='{$CFG->wwwroot}/lib/yui/logger/assets/logger.css'>";
39 }
9bb74178 40 } else {
2469f7ea 41 $output .= "<script type='text/javascript' src='".$CFG->wwwroot.''.$list[$i]."'></script>\n";
9bb74178 42 }
43 }
2469f7ea 44 return $output;
9bb74178 45}
46
35b974da 47
48/**
2469f7ea 49 * Used to create view of document to be passed to JavaScript on pageload.
50 * We use this class to pass data from PHP to JavaScript.
35b974da 51 */
4fc45e1d 52class jsportal {
9bb74178 53
0a0bb380 54 var $currentblocksection = null;
9bb74178 55 var $blocks = array();
0a0bb380 56
9bb74178 57
35b974da 58 /**
59 * Takes id of block and adds it
60 */
2469f7ea 61 function block_add($id, $hidden=false){
0a0bb380 62 $hidden_binary = 0;
9bb74178 63
64 if ($hidden) {
65 $hidden_binary = 1;
66 }
35b974da 67 $this->blocks[count($this->blocks)] = array($this->currentblocksection, $id, $hidden_binary);
0a0bb380 68 }
9bb74178 69
70
2469f7ea 71 /**
72 * Prints the JavaScript code needed to set up AJAX for the course.
73 */
74 function print_javascript($courseid, $return=false) {
9bb74178 75 global $CFG;
76
d4df8fdc 77 $blocksoutput = $output = '';
35b974da 78 for ($i=0; $i<count($this->blocks); $i++) {
2469f7ea 79 $blocksoutput .= "['".$this->blocks[$i][0]."',
80 '".$this->blocks[$i][1]."',
81 '".$this->blocks[$i][2]."']";
82
83 if ($i != (count($this->blocks) - 1)) {
35b974da 84 $blocksoutput .= ',';
9bb74178 85 }
86 }
35b974da 87 $output .= "<script language='javascript'>\n";
2469f7ea 88 $output .= " main.portal.id = ".$courseid.";\n";
89 $output .= " main.portal.blocks = new Array(".$blocksoutput.");\n";
90 $output .= " main.portal.strings['wwwroot']='".$CFG->wwwroot."';\n";
d8158863 91 $output .= " main.portal.strings['pixpath']='".$CFG->pixpath."';\n";
4fc45e1d 92 $output .= " main.portal.strings['update']='".get_string('update')."';\n";
2469f7ea 93 $output .= " main.portal.strings['deletecheck']='".get_string('deletecheck','','_var_')."';\n";
94 $output .= " main.portal.strings['resource']='".get_string('resource')."';\n";
95 $output .= " main.portal.strings['activity']='".get_string('activity')."';\n";
96 $output .= " onloadobj.load();\n";
4fc45e1d 97 $output .= " main.process_blocks();\n";
35b974da 98 $output .= "</script>";
2469f7ea 99 if ($return) {
100 return $output;
101 } else {
102 echo $output;
103 }
0a0bb380 104 }
9bb74178 105
2469f7ea 106}
f68a7947 107
108?>