'add a second parameter to enable YUI loading from header.html in the themes - MDL...
[moodle.git] / lib / ajax / ajaxlib.php
1 <?php
2 /**
3  * Library functions for using AJAX with Moodle.
4  */
8 /**
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  *        $add - 1 to return the libraries in lib
21  *               not already loaded.
22  * @return string or false or nothing.
23  */
24 function require_js($lib='', $add = 0) {
25     global $CFG;
26     static $loadlibs = array();
28     if (!ajaxenabled()) {
29         return false;
30     }
32     if (!empty($lib)) {
33         // Add the lib to the list of libs to be loaded, if it isn't already
34         // in the list.
35         // if (is_array($lib)) {
36             // array_map('require_js', $lib);
37         // } else {
38         foreach ($lib as $lib1)  {
39             // $libpath = ajax_get_lib($lib);
40             $libpath = ajax_get_lib($lib1);
41             if (array_search($libpath, $loadlibs) === false) {
42                 // array_push($loadlibs, $libpath);
43                 // array_push($addedlibs, $libpath);
44                 $loadlibs[] = $libpath;
45                 $addedlibs[] = $libpath;
46             }
47         }
48     }
49     // } else {
50     if (empty($lib) || ((!empty($addedlibs)) && ($add != 0))) {
51         // Return the html needed to load the JavaScript files defined in
52         // our list of libs to be loaded.
53         $output = '';
55         $thelibs = (!empty($addedlibs)) ? $addedlibs : $loadlibs;
56         // foreach ($loadlibs as $loadlib) {
57         foreach ($thelibs as $loadlib) {
58             $output .= '<script type="text/javascript" ';
59             $output .= " src=\"$loadlib\"></script>\n";
60             if ($loadlib == $CFG->wwwroot.'/lib/yui/logger/logger-min.js') {
61                 // Special case, we need the CSS too.
62                 $output .= '<link type="text/css" rel="stylesheet" ';
63                 $output .= " href=\"{$CFG->wwwroot}/lib/yui/logger/assets/logger.css\" />\n";
64             }
65         }
66         return $output;
67     }
68 }
71 /**
72  * Get the path to a JavaScript library.
73  * @param $libname - the name of the library whose path we need.
74  * @return string
75  */
76 function ajax_get_lib($libname) {
78     global $CFG;
79     $libpath = '';
81     $translatelist = array(
82             'yui_yahoo' => '/lib/yui/yahoo/yahoo-min.js',
83                         'yui_animation' => '/lib/yui/animation/animation-min.js',
84                         'yui_autocomplete' => '/lib/yui/autocomplete/autocomplete-min.js',
85                         'yui_calendar' => '/lib/yui/calendar/calendar-min.js',
86                         'yui_connection' => '/lib/yui/connection/connection-min.js',
87                         'yui_container' => '/lib/yui/container/container-min.js',
88             'yui_dom' => '/lib/yui/dom/dom-min.js',
89                         'yui_dom-event' => '/lib/yui/yahoo-dom-event/yahoo-dom-event.js',
90                         'yui_dragdrop' => '/lib/yui/dragdrop/dragdrop-min.js',
91             'yui_event' => '/lib/yui/event/event-min.js',
92             'yui_logger' => '/lib/yui/logger/logger-min.js',
93                         'yui_menu' => '/lib/yui/menu/menu-min.js',
94                         'yui_tabview' => '/lib/yui/tabview/tabview-min.js',
95                         'yui_treeview' => '/lib/yui/treeview/treeview-min.js',
96                         'yui_slider' => '/lib/yui/slider/slider-min.js',
97                         'yui_utilities' => '/lib/yui/utilities/utilities.js',
98             'ajaxcourse_blocks' => '/lib/ajax/block_classes.js',
99             'ajaxcourse_sections' => '/lib/ajax/section_classes.js',
100             'ajaxcourse' => '/lib/ajax/ajaxcourse.js'
101             );
103     if (array_key_exists($libname, $translatelist)) {
104         $libpath = $CFG->wwwroot . $translatelist[$libname];
105     } else {
106         $libpath = $libname;
107     }
109     $testpath = str_replace($CFG->wwwroot, $CFG->dirroot, $libpath);
110     if (!file_exists($testpath)) {        
111         error('require_js: '.$libpath.' - file not found.');
112     }
114     return $libpath;
118 /**
119  * Returns whether ajax is enabled/allowed or not.
120  */
121 function ajaxenabled() {
123     global $CFG, $USER;
125     if (!check_browser_version('MSIE', 6.0)
126                 && !check_browser_version('Gecko', 20051111)) {
127                 // Gecko build 20051111 is what is in Firefox 1.5.
128         // We still have issues with AJAX in other browsers.
129         return false;
130     }
132     if (!empty($CFG->enableajax) && (!empty($USER->ajax) || !isloggedin())) {
133         return true;
134     } else {
135         return false;
136     }
140 /**
141  * Used to create view of document to be passed to JavaScript on pageload.
142  * We use this class to pass data from PHP to JavaScript.
143  */
144 class jsportal {
146     var $currentblocksection = null;
147     var $blocks = array();
150     /**
151      * Takes id of block and adds it
152      */
153     function block_add($id, $hidden=false){
154         $hidden_binary = 0;
156         if ($hidden) {
157             $hidden_binary = 1;
158         }
159         $this->blocks[count($this->blocks)] = array($this->currentblocksection, $id, $hidden_binary);
160     }
163     /**
164      * Prints the JavaScript code needed to set up AJAX for the course.
165      */
166     function print_javascript($courseid, $return=false) {
167         global $CFG, $USER;
169         $blocksoutput = $output = '';
170         for ($i=0; $i<count($this->blocks); $i++) {
171             $blocksoutput .= "['".$this->blocks[$i][0]."',
172                              '".$this->blocks[$i][1]."',
173                              '".$this->blocks[$i][2]."']";
175             if ($i != (count($this->blocks) - 1)) {
176                 $blocksoutput .= ',';
177             }
178         }
179         $output .= "<script type=\"text/javascript\">\n";
180         $output .= "    main.portal.id = ".$courseid.";\n";
181         $output .= "    main.portal.blocks = new Array(".$blocksoutput.");\n";
182         $output .= "    main.portal.strings['wwwroot']='".$CFG->wwwroot."';\n";
183         $output .= "    main.portal.strings['pixpath']='".$CFG->pixpath."';\n";
184         $output .= "    main.portal.strings['move']='".get_string('move')."';\n";
185         $output .= "    main.portal.strings['moveleft']='".get_string('moveleft')."';\n";
186         $output .= "    main.portal.strings['moveright']='".get_string('moveright')."';\n";
187         $output .= "    main.portal.strings['update']='".get_string('update')."';\n";
188         $output .= "    main.portal.strings['groupsnone']='".get_string('groupsnone')."';\n";
189         $output .= "    main.portal.strings['groupsseparate']='".get_string('groupsseparate')."';\n";
190         $output .= "    main.portal.strings['groupsvisible']='".get_string('groupsvisible')."';\n";
191         $output .= "    main.portal.strings['clicktochange']='".get_string('clicktochange')."';\n";
192         $output .= "    main.portal.strings['deletecheck']='".get_string('deletecheck','','_var_')."';\n";
193         $output .= "    main.portal.strings['resource']='".get_string('resource')."';\n";
194         $output .= "    main.portal.strings['activity']='".get_string('activity')."';\n";
195         $output .= "    main.portal.strings['sesskey']='".$USER->sesskey."';\n";
196         $output .= "    onloadobj.load();\n";
197         $output .= "    main.process_blocks();\n";
198         $output .= "</script>";
199         if ($return) {
200             return $output;
201         } else {
202             echo $output;
203         }
204     }
208 ?>