3 * Library functions for using AJAX with Moodle.
9 * Used to include JavaScript libraries.
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.
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.
22 function require_js($lib='') {
24 static $loadlibs = array();
31 // Add the lib to the list of libs to be loaded, if it isn't already
34 array_map('require_js', $lib);
36 $libpath = ajax_get_lib($lib);
37 if (array_search($libpath, $loadlibs) === false) {
38 array_push($loadlibs, $libpath);
42 // Return the html needed to load the JavaScript files defined in
43 // our list of libs to be loaded.
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";
61 * Get the path to a JavaScript library.
62 * @param $libname - the name of the library whose path we need.
65 function ajax_get_lib($libname) {
70 $translatelist = array(
71 'yui_yahoo' => '/lib/yui/yahoo/yahoo-min.js',
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',
77 'yui_dom' => '/lib/yui/dom/dom-min.js',
78 'yui_dom-event' => '/lib/yui/yahoo-dom-event/yahoo-dom-event.js',
79 'yui_dragdrop' => '/lib/yui/dragdrop/dragdrop-min.js',
80 'yui_event' => '/lib/yui/event/event-min.js',
81 'yui_logger' => '/lib/yui/logger/logger-min.js',
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',
87 'ajaxcourse_blocks' => '/lib/ajax/block_classes.js',
88 'ajaxcourse_sections' => '/lib/ajax/section_classes.js',
89 'ajaxcourse' => '/lib/ajax/ajaxcourse.js'
92 if (array_key_exists($libname, $translatelist)) {
93 $libpath = $CFG->wwwroot . $translatelist[$libname];
98 $testpath = str_replace($CFG->wwwroot, $CFG->dirroot, $libpath);
99 if (!file_exists($testpath)) {
100 error('require_js: '.$libpath.' - file not found.');
108 * Returns whether ajax is enabled/allowed or not.
110 function ajaxenabled() {
114 if (!check_browser_version('MSIE', 6.0)
115 && !check_browser_version('Gecko', 20051111)) {
116 // Gecko build 20051111 is what is in Firefox 1.5.
117 // We still have issues with AJAX in other browsers.
121 if (!empty($CFG->enableajax) && (!empty($USER->ajax) || !isloggedin())) {
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.
135 var $currentblocksection = null;
136 var $blocks = array();
140 * Takes id of block and adds it
142 function block_add($id, $hidden=false){
148 $this->blocks[count($this->blocks)] = array($this->currentblocksection, $id, $hidden_binary);
153 * Prints the JavaScript code needed to set up AJAX for the course.
155 function print_javascript($courseid, $return=false) {
158 $blocksoutput = $output = '';
159 for ($i=0; $i<count($this->blocks); $i++) {
160 $blocksoutput .= "['".$this->blocks[$i][0]."',
161 '".$this->blocks[$i][1]."',
162 '".$this->blocks[$i][2]."']";
164 if ($i != (count($this->blocks) - 1)) {
165 $blocksoutput .= ',';
168 $output .= "<script type=\"text/javascript\">\n";
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";
172 $output .= " main.portal.strings['pixpath']='".$CFG->pixpath."';\n";
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";
176 $output .= " main.portal.strings['update']='".get_string('update')."';\n";
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";
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";
184 $output .= " main.portal.strings['sesskey']='".$USER->sesskey."';\n";
185 $output .= " onloadobj.load();\n";
186 $output .= " main.process_blocks();\n";
187 $output .= "</script>";