Automatic installer.php lang files by installer_builder (20070212)
[moodle.git] / lib / ajax / ajaxlib.php
CommitLineData
f72f94a2 1<?php
2/**
3 * Library functions for using AJAX with Moodle.
4 */
5
6
9bb74178 7
8/**
f72f94a2 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.
c6307019 20 * $add - 1 to return the libraries in lib
21 * not already loaded.
f72f94a2 22 * @return string or false or nothing.
23 */
c6307019 24function require_js($lib='', $add = 0) {
9bb74178 25 global $CFG;
f72f94a2 26 static $loadlibs = array();
c081fdca 27
88c5092a 28 if (!ajaxenabled()) {
f72f94a2 29 return false;
30 }
31
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.
c6307019 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);
f72f94a2 41 if (array_search($libpath, $loadlibs) === false) {
c6307019 42 // array_push($loadlibs, $libpath);
43 // array_push($addedlibs, $libpath);
44 $loadlibs[] = $libpath;
45 $addedlibs[] = $libpath;
f72f94a2 46 }
47 }
c6307019 48 }
49 // } else {
50 if (empty($lib) || ((!empty($addedlibs)) && ($add != 0))) {
f72f94a2 51 // Return the html needed to load the JavaScript files defined in
52 // our list of libs to be loaded.
53 $output = '';
54
c6307019 55 $thelibs = (!empty($addedlibs)) ? $addedlibs : $loadlibs;
56 // foreach ($loadlibs as $loadlib) {
57 foreach ($thelibs as $loadlib) {
f72f94a2 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;
4fc45e1d 67 }
f72f94a2 68}
69
70
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 */
76function ajax_get_lib($libname) {
77
78 global $CFG;
79 $libpath = '';
c081fdca 80
9bb74178 81 $translatelist = array(
3972629d 82 'yui_yahoo' => '/lib/yui/yahoo/yahoo-min.js',
67c0e16e 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',
3972629d 88 'yui_dom' => '/lib/yui/dom/dom-min.js',
67c0e16e 89 'yui_dom-event' => '/lib/yui/yahoo-dom-event/yahoo-dom-event.js',
90 'yui_dragdrop' => '/lib/yui/dragdrop/dragdrop-min.js',
3972629d 91 'yui_event' => '/lib/yui/event/event-min.js',
3972629d 92 'yui_logger' => '/lib/yui/logger/logger-min.js',
67c0e16e 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',
3e1e2b69 98 'ajaxcourse_blocks' => '/lib/ajax/block_classes.js',
99 'ajaxcourse_sections' => '/lib/ajax/section_classes.js',
9bb74178 100 'ajaxcourse' => '/lib/ajax/ajaxcourse.js'
101 );
102
f72f94a2 103 if (array_key_exists($libname, $translatelist)) {
104 $libpath = $CFG->wwwroot . $translatelist[$libname];
105 } else {
106 $libpath = $libname;
9bb74178 107 }
f72f94a2 108
109 $testpath = str_replace($CFG->wwwroot, $CFG->dirroot, $libpath);
110 if (!file_exists($testpath)) {
111 error('require_js: '.$libpath.' - file not found.');
112 }
113
114 return $libpath;
9bb74178 115}
88c5092a 116
117
118/**
119 * Returns whether ajax is enabled/allowed or not.
120 */
121function ajaxenabled() {
122
123 global $CFG, $USER;
124
125 if (!check_browser_version('MSIE', 6.0)
4a5548f9 126 && !check_browser_version('Gecko', 20051111)) {
127 // Gecko build 20051111 is what is in Firefox 1.5.
88c5092a 128 // We still have issues with AJAX in other browsers.
129 return false;
130 }
131
2f11bfc0 132 if (!empty($CFG->enableajax) && (!empty($USER->ajax) || !isloggedin())) {
88c5092a 133 return true;
134 } else {
135 return false;
136 }
137}
9bb74178 138
35b974da 139
140/**
2469f7ea 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.
35b974da 143 */
4fc45e1d 144class jsportal {
9bb74178 145
0a0bb380 146 var $currentblocksection = null;
9bb74178 147 var $blocks = array();
0a0bb380 148
9bb74178 149
35b974da 150 /**
151 * Takes id of block and adds it
152 */
2469f7ea 153 function block_add($id, $hidden=false){
0a0bb380 154 $hidden_binary = 0;
9bb74178 155
156 if ($hidden) {
157 $hidden_binary = 1;
158 }
35b974da 159 $this->blocks[count($this->blocks)] = array($this->currentblocksection, $id, $hidden_binary);
0a0bb380 160 }
9bb74178 161
162
2469f7ea 163 /**
164 * Prints the JavaScript code needed to set up AJAX for the course.
165 */
166 function print_javascript($courseid, $return=false) {
c4ca9cb3 167 global $CFG, $USER;
9bb74178 168
d4df8fdc 169 $blocksoutput = $output = '';
35b974da 170 for ($i=0; $i<count($this->blocks); $i++) {
2469f7ea 171 $blocksoutput .= "['".$this->blocks[$i][0]."',
172 '".$this->blocks[$i][1]."',
173 '".$this->blocks[$i][2]."']";
174
175 if ($i != (count($this->blocks) - 1)) {
35b974da 176 $blocksoutput .= ',';
9bb74178 177 }
178 }
32f0b38a 179 $output .= "<script type=\"text/javascript\">\n";
2469f7ea 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";
d8158863 183 $output .= " main.portal.strings['pixpath']='".$CFG->pixpath."';\n";
d2a11d46 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";
4fc45e1d 187 $output .= " main.portal.strings['update']='".get_string('update')."';\n";
d2a11d46 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";
2469f7ea 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";
c4ca9cb3 195 $output .= " main.portal.strings['sesskey']='".$USER->sesskey."';\n";
2469f7ea 196 $output .= " onloadobj.load();\n";
4fc45e1d 197 $output .= " main.process_blocks();\n";
35b974da 198 $output .= "</script>";
2469f7ea 199 if ($return) {
200 return $output;
201 } else {
202 echo $output;
203 }
0a0bb380 204 }
9bb74178 205
2469f7ea 206}
f68a7947 207
c6307019 208?>