3 * Library functions for using AJAX with Moodle.
8 * Used to include JavaScript libraries.
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.
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.
21 function require_js($lib='') {
23 static $loadlibs = array();
30 // Add the lib to the list of libs to be loaded, if it isn't already
33 array_map('require_js', $lib);
35 $libpath = ajax_get_lib($lib);
36 if (array_search($libpath, $loadlibs) === false) {
37 $loadlibs[] = $libpath;
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);
44 $loadlibs=$realloadlibs;
49 // Return the html needed to load the JavaScript files defined in
50 // our list of libs to be loaded.
53 foreach ($loadlibs as $loadlib) {
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";
68 * Get the path to a JavaScript library.
69 * @param $libname - the name of the library whose path we need.
72 function ajax_get_lib($libname) {
77 $translatelist = array(
78 'yui_yahoo' => '/lib/yui/yahoo/yahoo-min.js',
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',
84 'yui_dom' => '/lib/yui/dom/dom-min.js',
85 'yui_dom-event' => '/lib/yui/yahoo-dom-event/yahoo-dom-event.js',
86 'yui_dragdrop' => '/lib/yui/dragdrop/dragdrop-min.js',
87 'yui_event' => '/lib/yui/event/event-min.js',
88 'yui_logger' => '/lib/yui/logger/logger-min.js',
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',
94 'ajaxcourse_blocks' => '/lib/ajax/block_classes.js',
95 'ajaxcourse_sections' => '/lib/ajax/section_classes.js',
96 'ajaxcourse' => '/lib/ajax/ajaxcourse.js'
99 if (array_key_exists($libname, $translatelist)) {
100 $libpath = $CFG->wwwroot
. $translatelist[$libname];
105 $testpath = str_replace($CFG->wwwroot
, $CFG->dirroot
, $libpath);
106 if (!file_exists($testpath)) {
107 error('require_js: '.$libpath.' - file not found.');
115 * Returns whether ajax is enabled/allowed or not.
117 function ajaxenabled($browsers = array()) {
121 if (!empty($browsers)) {
123 foreach ($browsers as $brand => $version) {
124 if (check_browser_version($brand, $version)) {
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);
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.
142 // We still have issues with AJAX in other browsers.
146 if (!empty($CFG->enableajax
) && (!empty($USER->ajax
) ||
!isloggedin())) {
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.
160 var $currentblocksection = null;
161 var $blocks = array();
165 * Takes id of block and adds it
167 function block_add($id, $hidden=false){
173 $this->blocks
[count($this->blocks
)] = array($this->currentblocksection
, $id, $hidden_binary);
178 * Prints the JavaScript code needed to set up AJAX for the course.
180 function print_javascript($courseid, $return=false) {
183 $blocksoutput = $output = '';
184 for ($i=0; $i<count($this->blocks
); $i++
) {
185 $blocksoutput .= "['".$this->blocks
[$i][0]."',
186 '".$this->blocks
[$i][1]."',
187 '".$this->blocks
[$i][2]."']";
189 if ($i != (count($this->blocks
) - 1)) {
190 $blocksoutput .= ',';
193 $output .= "<script type=\"text/javascript\">\n";
194 $output .= " main.portal.id = ".$courseid.";\n";
195 $output .= " main.portal.blocks = new Array(".$blocksoutput.");\n";
196 $output .= " main.portal.strings['wwwroot']='".$CFG->wwwroot
."';\n";
197 $output .= " main.portal.strings['pixpath']='".$CFG->pixpath
."';\n";
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";
201 $output .= " main.portal.strings['update']='".get_string('update')."';\n";
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";
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";
209 $output .= " main.portal.strings['sesskey']='".$USER->sesskey
."';\n";
210 $output .= " onloadobj.load();\n";
211 $output .= " main.process_blocks();\n";
212 $output .= "</script>";