Safari 2.0 and Opera 9.0 are now detected and supported for YUI. However, preliminary...
[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;
f72f94a2 38 }
39 }
604c6341 40 } else {
f72f94a2 41 // Return the html needed to load the JavaScript files defined in
42 // our list of libs to be loaded.
43 $output = '';
44
604c6341 45 foreach ($loadlibs as $loadlib) {
f72f94a2 46 $output .= '<script type="text/javascript" ';
47 $output .= " src=\"$loadlib\"></script>\n";
48 if ($loadlib == $CFG->wwwroot.'/lib/yui/logger/logger-min.js') {
49 // Special case, we need the CSS too.
50 $output .= '<link type="text/css" rel="stylesheet" ';
51 $output .= " href=\"{$CFG->wwwroot}/lib/yui/logger/assets/logger.css\" />\n";
52 }
53 }
54 return $output;
4fc45e1d 55 }
f72f94a2 56}
57
58
59/**
60 * Get the path to a JavaScript library.
61 * @param $libname - the name of the library whose path we need.
62 * @return string
63 */
64function ajax_get_lib($libname) {
65
66 global $CFG;
67 $libpath = '';
c081fdca 68
9bb74178 69 $translatelist = array(
3972629d 70 'yui_yahoo' => '/lib/yui/yahoo/yahoo-min.js',
67c0e16e 71 'yui_animation' => '/lib/yui/animation/animation-min.js',
72 'yui_autocomplete' => '/lib/yui/autocomplete/autocomplete-min.js',
73 'yui_calendar' => '/lib/yui/calendar/calendar-min.js',
74 'yui_connection' => '/lib/yui/connection/connection-min.js',
75 'yui_container' => '/lib/yui/container/container-min.js',
3972629d 76 'yui_dom' => '/lib/yui/dom/dom-min.js',
67c0e16e 77 'yui_dom-event' => '/lib/yui/yahoo-dom-event/yahoo-dom-event.js',
78 'yui_dragdrop' => '/lib/yui/dragdrop/dragdrop-min.js',
3972629d 79 'yui_event' => '/lib/yui/event/event-min.js',
3972629d 80 'yui_logger' => '/lib/yui/logger/logger-min.js',
67c0e16e 81 'yui_menu' => '/lib/yui/menu/menu-min.js',
82 'yui_tabview' => '/lib/yui/tabview/tabview-min.js',
83 'yui_treeview' => '/lib/yui/treeview/treeview-min.js',
84 'yui_slider' => '/lib/yui/slider/slider-min.js',
85 'yui_utilities' => '/lib/yui/utilities/utilities.js',
3e1e2b69 86 'ajaxcourse_blocks' => '/lib/ajax/block_classes.js',
87 'ajaxcourse_sections' => '/lib/ajax/section_classes.js',
9bb74178 88 'ajaxcourse' => '/lib/ajax/ajaxcourse.js'
89 );
90
f72f94a2 91 if (array_key_exists($libname, $translatelist)) {
92 $libpath = $CFG->wwwroot . $translatelist[$libname];
93 } else {
94 $libpath = $libname;
9bb74178 95 }
f72f94a2 96
97 $testpath = str_replace($CFG->wwwroot, $CFG->dirroot, $libpath);
98 if (!file_exists($testpath)) {
99 error('require_js: '.$libpath.' - file not found.');
100 }
101
102 return $libpath;
9bb74178 103}
88c5092a 104
105
106/**
107 * Returns whether ajax is enabled/allowed or not.
108 */
109function ajaxenabled() {
110
111 global $CFG, $USER;
d499142e 112
113 $ie = check_browser_version('MSIE', 6.0);
114 $ff = check_browser_version('Gecko', 20051106);
115 $op = check_browser_version('Opera', 9.0);
116 $sa = check_browser_version('Safari', 412);
117
118 if (!$ie && !$ff && !$op && !$sa) {
119 /** @see http://en.wikipedia.org/wiki/User_agent */
120 // Gecko build 20051107 is what is in Firefox 1.5.
88c5092a 121 // We still have issues with AJAX in other browsers.
122 return false;
123 }
124
2f11bfc0 125 if (!empty($CFG->enableajax) && (!empty($USER->ajax) || !isloggedin())) {
88c5092a 126 return true;
127 } else {
128 return false;
129 }
130}
9bb74178 131
35b974da 132
133/**
2469f7ea 134 * Used to create view of document to be passed to JavaScript on pageload.
135 * We use this class to pass data from PHP to JavaScript.
35b974da 136 */
4fc45e1d 137class jsportal {
9bb74178 138
0a0bb380 139 var $currentblocksection = null;
9bb74178 140 var $blocks = array();
0a0bb380 141
9bb74178 142
35b974da 143 /**
144 * Takes id of block and adds it
145 */
2469f7ea 146 function block_add($id, $hidden=false){
0a0bb380 147 $hidden_binary = 0;
9bb74178 148
149 if ($hidden) {
150 $hidden_binary = 1;
151 }
35b974da 152 $this->blocks[count($this->blocks)] = array($this->currentblocksection, $id, $hidden_binary);
0a0bb380 153 }
9bb74178 154
155
2469f7ea 156 /**
157 * Prints the JavaScript code needed to set up AJAX for the course.
158 */
159 function print_javascript($courseid, $return=false) {
c4ca9cb3 160 global $CFG, $USER;
9bb74178 161
d4df8fdc 162 $blocksoutput = $output = '';
35b974da 163 for ($i=0; $i<count($this->blocks); $i++) {
2469f7ea 164 $blocksoutput .= "['".$this->blocks[$i][0]."',
165 '".$this->blocks[$i][1]."',
166 '".$this->blocks[$i][2]."']";
167
168 if ($i != (count($this->blocks) - 1)) {
35b974da 169 $blocksoutput .= ',';
9bb74178 170 }
171 }
32f0b38a 172 $output .= "<script type=\"text/javascript\">\n";
2469f7ea 173 $output .= " main.portal.id = ".$courseid.";\n";
174 $output .= " main.portal.blocks = new Array(".$blocksoutput.");\n";
175 $output .= " main.portal.strings['wwwroot']='".$CFG->wwwroot."';\n";
d8158863 176 $output .= " main.portal.strings['pixpath']='".$CFG->pixpath."';\n";
d2a11d46 177 $output .= " main.portal.strings['move']='".get_string('move')."';\n";
178 $output .= " main.portal.strings['moveleft']='".get_string('moveleft')."';\n";
179 $output .= " main.portal.strings['moveright']='".get_string('moveright')."';\n";
4fc45e1d 180 $output .= " main.portal.strings['update']='".get_string('update')."';\n";
d2a11d46 181 $output .= " main.portal.strings['groupsnone']='".get_string('groupsnone')."';\n";
182 $output .= " main.portal.strings['groupsseparate']='".get_string('groupsseparate')."';\n";
183 $output .= " main.portal.strings['groupsvisible']='".get_string('groupsvisible')."';\n";
184 $output .= " main.portal.strings['clicktochange']='".get_string('clicktochange')."';\n";
2469f7ea 185 $output .= " main.portal.strings['deletecheck']='".get_string('deletecheck','','_var_')."';\n";
186 $output .= " main.portal.strings['resource']='".get_string('resource')."';\n";
187 $output .= " main.portal.strings['activity']='".get_string('activity')."';\n";
c4ca9cb3 188 $output .= " main.portal.strings['sesskey']='".$USER->sesskey."';\n";
2469f7ea 189 $output .= " onloadobj.load();\n";
4fc45e1d 190 $output .= " main.process_blocks();\n";
35b974da 191 $output .= "</script>";
2469f7ea 192 if ($return) {
193 return $output;
194 } else {
195 echo $output;
196 }
0a0bb380 197 }
9bb74178 198
2469f7ea 199}
f68a7947 200
c6307019 201?>