MDL-8290, fix for broken plugins
[moodle.git] / lib / ajax / ajaxlib.php
CommitLineData
f72f94a2 1<?php
2/**
3 * Library functions for using AJAX with Moodle.
4 */
5
6
9bb74178 7
8/**
f72f94a2 9 * Used to include JavaScript libraries.
10 *
11 * When the $lib parameter is given, the function will add $lib to an
12 * internal list of libraries. When called without any parameters, it will
13 * return the html that is needed to load the JavaScript libraries in that
14 * list. Libraries that are included more than once will still only get loaded
15 * once, so this works like require_once() in PHP.
16 *
17 * @param $lib - string or array of strings
18 * string(s) should be the shortname for the library or the
19 * full path to the library file.
20 * @return string or false or nothing.
21 */
22function require_js($lib='') {
9bb74178 23 global $CFG;
f72f94a2 24 static $loadlibs = array();
c081fdca 25
88c5092a 26 if (!ajaxenabled()) {
f72f94a2 27 return false;
28 }
29
30 if (!empty($lib)) {
31 // Add the lib to the list of libs to be loaded, if it isn't already
32 // in the list.
33 if (is_array($lib)) {
34 array_map('require_js', $lib);
35 } else {
36 $libpath = ajax_get_lib($lib);
37 if (array_search($libpath, $loadlibs) === false) {
38 array_push($loadlibs, $libpath);
39 }
40 }
41 } else {
42 // Return the html needed to load the JavaScript files defined in
43 // our list of libs to be loaded.
44 $output = '';
45
46 foreach ($loadlibs as $loadlib) {
47 $output .= '<script type="text/javascript" ';
48 $output .= " src=\"$loadlib\"></script>\n";
49 if ($loadlib == $CFG->wwwroot.'/lib/yui/logger/logger-min.js') {
50 // Special case, we need the CSS too.
51 $output .= '<link type="text/css" rel="stylesheet" ';
52 $output .= " href=\"{$CFG->wwwroot}/lib/yui/logger/assets/logger.css\" />\n";
53 }
54 }
55 return $output;
4fc45e1d 56 }
f72f94a2 57}
58
59
60/**
61 * Get the path to a JavaScript library.
62 * @param $libname - the name of the library whose path we need.
63 * @return string
64 */
65function ajax_get_lib($libname) {
66
67 global $CFG;
68 $libpath = '';
c081fdca 69
9bb74178 70 $translatelist = array(
3972629d 71 'yui_yahoo' => '/lib/yui/yahoo/yahoo-min.js',
67c0e16e 72 'yui_animation' => '/lib/yui/animation/animation-min.js',
73 'yui_autocomplete' => '/lib/yui/autocomplete/autocomplete-min.js',
74 'yui_calendar' => '/lib/yui/calendar/calendar-min.js',
75 'yui_connection' => '/lib/yui/connection/connection-min.js',
76 'yui_container' => '/lib/yui/container/container-min.js',
3972629d 77 'yui_dom' => '/lib/yui/dom/dom-min.js',
67c0e16e 78 'yui_dom-event' => '/lib/yui/yahoo-dom-event/yahoo-dom-event.js',
79 'yui_dragdrop' => '/lib/yui/dragdrop/dragdrop-min.js',
3972629d 80 'yui_event' => '/lib/yui/event/event-min.js',
3972629d 81 'yui_logger' => '/lib/yui/logger/logger-min.js',
67c0e16e 82 'yui_menu' => '/lib/yui/menu/menu-min.js',
83 'yui_tabview' => '/lib/yui/tabview/tabview-min.js',
84 'yui_treeview' => '/lib/yui/treeview/treeview-min.js',
85 'yui_slider' => '/lib/yui/slider/slider-min.js',
86 'yui_utilities' => '/lib/yui/utilities/utilities.js',
3e1e2b69 87 'ajaxcourse_blocks' => '/lib/ajax/block_classes.js',
88 'ajaxcourse_sections' => '/lib/ajax/section_classes.js',
9bb74178 89 'ajaxcourse' => '/lib/ajax/ajaxcourse.js'
90 );
91
f72f94a2 92 if (array_key_exists($libname, $translatelist)) {
93 $libpath = $CFG->wwwroot . $translatelist[$libname];
94 } else {
95 $libpath = $libname;
9bb74178 96 }
f72f94a2 97
98 $testpath = str_replace($CFG->wwwroot, $CFG->dirroot, $libpath);
99 if (!file_exists($testpath)) {
100 error('require_js: '.$libpath.' - file not found.');
101 }
102
103 return $libpath;
9bb74178 104}
88c5092a 105
106
107/**
108 * Returns whether ajax is enabled/allowed or not.
109 */
110function ajaxenabled() {
111
112 global $CFG, $USER;
113
114 if (!check_browser_version('MSIE', 6.0)
4a5548f9 115 && !check_browser_version('Gecko', 20051111)) {
116 // Gecko build 20051111 is what is in Firefox 1.5.
88c5092a 117 // We still have issues with AJAX in other browsers.
118 return false;
119 }
120
2f11bfc0 121 if (!empty($CFG->enableajax) && (!empty($USER->ajax) || !isloggedin())) {
88c5092a 122 return true;
123 } else {
124 return false;
125 }
126}
9bb74178 127
35b974da 128
129/**
2469f7ea 130 * Used to create view of document to be passed to JavaScript on pageload.
131 * We use this class to pass data from PHP to JavaScript.
35b974da 132 */
4fc45e1d 133class jsportal {
9bb74178 134
0a0bb380 135 var $currentblocksection = null;
9bb74178 136 var $blocks = array();
0a0bb380 137
9bb74178 138
35b974da 139 /**
140 * Takes id of block and adds it
141 */
2469f7ea 142 function block_add($id, $hidden=false){
0a0bb380 143 $hidden_binary = 0;
9bb74178 144
145 if ($hidden) {
146 $hidden_binary = 1;
147 }
35b974da 148 $this->blocks[count($this->blocks)] = array($this->currentblocksection, $id, $hidden_binary);
0a0bb380 149 }
9bb74178 150
151
2469f7ea 152 /**
153 * Prints the JavaScript code needed to set up AJAX for the course.
154 */
155 function print_javascript($courseid, $return=false) {
c4ca9cb3 156 global $CFG, $USER;
9bb74178 157
d4df8fdc 158 $blocksoutput = $output = '';
35b974da 159 for ($i=0; $i<count($this->blocks); $i++) {
2469f7ea 160 $blocksoutput .= "['".$this->blocks[$i][0]."',
161 '".$this->blocks[$i][1]."',
162 '".$this->blocks[$i][2]."']";
163
164 if ($i != (count($this->blocks) - 1)) {
35b974da 165 $blocksoutput .= ',';
9bb74178 166 }
167 }
32f0b38a 168 $output .= "<script type=\"text/javascript\">\n";
2469f7ea 169 $output .= " main.portal.id = ".$courseid.";\n";
170 $output .= " main.portal.blocks = new Array(".$blocksoutput.");\n";
171 $output .= " main.portal.strings['wwwroot']='".$CFG->wwwroot."';\n";
d8158863 172 $output .= " main.portal.strings['pixpath']='".$CFG->pixpath."';\n";
d2a11d46 173 $output .= " main.portal.strings['move']='".get_string('move')."';\n";
174 $output .= " main.portal.strings['moveleft']='".get_string('moveleft')."';\n";
175 $output .= " main.portal.strings['moveright']='".get_string('moveright')."';\n";
4fc45e1d 176 $output .= " main.portal.strings['update']='".get_string('update')."';\n";
d2a11d46 177 $output .= " main.portal.strings['groupsnone']='".get_string('groupsnone')."';\n";
178 $output .= " main.portal.strings['groupsseparate']='".get_string('groupsseparate')."';\n";
179 $output .= " main.portal.strings['groupsvisible']='".get_string('groupsvisible')."';\n";
180 $output .= " main.portal.strings['clicktochange']='".get_string('clicktochange')."';\n";
2469f7ea 181 $output .= " main.portal.strings['deletecheck']='".get_string('deletecheck','','_var_')."';\n";
182 $output .= " main.portal.strings['resource']='".get_string('resource')."';\n";
183 $output .= " main.portal.strings['activity']='".get_string('activity')."';\n";
c4ca9cb3 184 $output .= " main.portal.strings['sesskey']='".$USER->sesskey."';\n";
2469f7ea 185 $output .= " onloadobj.load();\n";
4fc45e1d 186 $output .= " main.process_blocks();\n";
35b974da 187 $output .= "</script>";
2469f7ea 188 if ($return) {
189 return $output;
190 } else {
191 echo $output;
192 }
0a0bb380 193 }
9bb74178 194
2469f7ea 195}
f68a7947 196
197?>