e9eb01055cc50dcb1126c9d72dc93c4a8b189b58
[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  * @return string or false or nothing.
21  */
22 function require_js($lib='') {
23     global $CFG;
24     static $loadlibs = array();
26     if (!ajaxenabled()) {
27         return false;
28     }
30     if (!empty($lib)) {
31         // Add the lib to the list of libs to be loaded, if it isn't already
32         // in the list.
33         if (is_array($lib)) {
34             array_map('require_js', $lib);
35         } else {
36             $libpath = ajax_get_lib($lib);
37             if (array_search($libpath, $loadlibs) === false) {
38                 array_push($loadlibs, $libpath);
39             }
40         }
41     } else {
42         // Return the html needed to load the JavaScript files defined in
43         // our list of libs to be loaded.
44         $output = '';
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";
53             }
54         }
55         return $output;
56     }
57 }
60 /**
61  * Get the path to a JavaScript library.
62  * @param $libname - the name of the library whose path we need.
63  * @return string
64  */
65 function ajax_get_lib($libname) {
67     global $CFG;
68     $libpath = '';
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'
90             );
92     if (array_key_exists($libname, $translatelist)) {
93         $libpath = $CFG->wwwroot . $translatelist[$libname];
94     } else {
95         $libpath = $libname;
96     }
98     $testpath = str_replace($CFG->wwwroot, $CFG->dirroot, $libpath);
99     if (!file_exists($testpath)) {        
100         error('require_js: '.$libpath.' - file not found.');
101     }
103     return $libpath;
107 /**
108  * Returns whether ajax is enabled/allowed or not.
109  */
110 function ajaxenabled() {
112     global $CFG, $USER;
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.
118         return false;
119     }
121     if (!empty($CFG->enableajax) && (!empty($USER->ajax) || !isloggedin())) {
122         return true;
123     } else {
124         return false;
125     }
129 /**
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.
132  */
133 class jsportal {
135     var $currentblocksection = null;
136     var $blocks = array();
139     /**
140      * Takes id of block and adds it
141      */
142     function block_add($id, $hidden=false){
143         $hidden_binary = 0;
145         if ($hidden) {
146             $hidden_binary = 1;
147         }
148         $this->blocks[count($this->blocks)] = array($this->currentblocksection, $id, $hidden_binary);
149     }
152     /**
153      * Prints the JavaScript code needed to set up AJAX for the course.
154      */
155     function print_javascript($courseid, $return=false) {
156         global $CFG, $USER;
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 .= ',';
166             }
167         }
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>";
188         if ($return) {
189             return $output;
190         } else {
191             echo $output;
192         }
193     }
197 ?>