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