Updated the HEAD build version to 20090612
[moodle.git] / lib / ajax / ajaxlib.php
CommitLineData
f72f94a2 1<?php
2/**
3 * Library functions for using AJAX with Moodle.
4 */
5
f72f94a2 6/**
7 * Get the path to a JavaScript library.
8 * @param $libname - the name of the library whose path we need.
9 * @return string
10 */
11function ajax_get_lib($libname) {
1f3ffe33 12 global $CFG, $HTTPSPAGEREQUIRED;
b7658fa0 13
f72f94a2 14 $libpath = '';
b958e941 15 $external_yui = false;
c081fdca 16
9bb74178 17 $translatelist = array(
3972629d 18 'yui_yahoo' => '/lib/yui/yahoo/yahoo-min.js',
72d28452 19 'yui_animation' => '/lib/yui/animation/animation-min.js',
20 'yui_autocomplete' => '/lib/yui/autocomplete/autocomplete-min.js',
483f3067 21 'yui_button' => '/lib/yui/button/button-min.js',
72d28452 22 'yui_calendar' => '/lib/yui/calendar/calendar-min.js',
fbaa7b88 23 'yui_charts' => '/lib/yui/charts/charts-min.js',
483f3067 24 'yui_colorpicker' => '/lib/yui/colorpicker/colorpicker-min.js',
72d28452 25 'yui_connection' => '/lib/yui/connection/connection-min.js',
26 'yui_container' => '/lib/yui/container/container-min.js',
25e3d9f2 27 'yui_cookie' => '/lib/yui/cookie/cookie-min.js',
28 'yui_datasource' => '/lib/yui/datasource/datasource-min.js',
29 'yui_datatable' => '/lib/yui/datatable/datatable-min.js',
3972629d 30 'yui_dom' => '/lib/yui/dom/dom-min.js',
72d28452 31 'yui_dom-event' => '/lib/yui/yahoo-dom-event/yahoo-dom-event.js',
32 'yui_dragdrop' => '/lib/yui/dragdrop/dragdrop-min.js',
25e3d9f2 33 'yui_editor' => '/lib/yui/editor/editor-min.js',
fbaa7b88 34 'yui_element' => '/lib/yui/element/element-min.js',
3972629d 35 'yui_event' => '/lib/yui/event/event-min.js',
483f3067 36 'yui_get' => '/lib/yui/get/get-min.js',
37 'yui_history' => '/lib/yui/history/history-min.js',
fbaa7b88 38 'yui_imagecropper' => '/lib/yui/imagecropper/imagecropper-min.js',
483f3067 39 'yui_imageloader' => '/lib/yui/imageloader/imageloader-min.js',
b244b9b7 40 'yui_json' => '/lib/yui/json/json-min.js',
25e3d9f2 41 'yui_layout' => '/lib/yui/layout/layout-min.js',
3972629d 42 'yui_logger' => '/lib/yui/logger/logger-min.js',
72d28452 43 'yui_menu' => '/lib/yui/menu/menu-min.js',
25e3d9f2 44 'yui_profiler' => '/lib/yui/profiler/profiler-min.js',
fbaa7b88 45 'yui_profilerviewer' => '/lib/yui/profilerviewer/profilerviewer-min.js',
25e3d9f2 46 'yui_resize' => '/lib/yui/resize/resize-min.js',
fbaa7b88 47 'yui_selector' => '/lib/yui/selector/selector-min.js',
25e3d9f2 48 'yui_simpleeditor' => '/lib/yui/editor/simpleeditor-min.js',
483f3067 49 'yui_slider' => '/lib/yui/slider/slider-min.js',
72d28452 50 'yui_tabview' => '/lib/yui/tabview/tabview-min.js',
51 'yui_treeview' => '/lib/yui/treeview/treeview-min.js',
fbaa7b88 52 'yui_uploader' => '/lib/yui/uploader/uploader-min.js',
72d28452 53 'yui_utilities' => '/lib/yui/utilities/utilities.js',
25e3d9f2 54 'yui_yuiloader' => '/lib/yui/yuiloader/yuiloader-min.js',
55 'yui_yuitest' => '/lib/yui/yuitest/yuitest-min.js',
3e1e2b69 56 'ajaxcourse_blocks' => '/lib/ajax/block_classes.js',
57 'ajaxcourse_sections' => '/lib/ajax/section_classes.js',
9bb74178 58 'ajaxcourse' => '/lib/ajax/ajaxcourse.js'
59 );
60
b7658fa0 61 if (!empty($HTTPSPAGEREQUIRED)) {
62 $wwwroot = $CFG->httpswwwroot;
63 } else {
64 $wwwroot = $CFG->wwwroot;
65 }
66
f72f94a2 67 if (array_key_exists($libname, $translatelist)) {
b958e941 68 // If this is a YUI file and we are using external libraries
b7658fa0 69 if (substr($libname, 0, 3) == 'yui' && !empty($CFG->useexternalyui)) {
b958e941 70 $external_yui = true;
71 // Get current version
72 include($CFG->libdir.'/yui/version.php');
73 $libpath = 'http://yui.yahooapis.com/'.$yuiversion.'/build/'.substr($translatelist[$libname], 9);
74 } else {
b7658fa0 75 $libpath = $wwwroot . $translatelist[$libname];
b958e941 76 }
77
f2eb5002 78 // If we are in developer debug mode, use the non-compressed version of YUI for easier debugging.
79 if (debugging('', DEBUG_DEVELOPER)) {
80 $libpath = str_replace('-min.js', '.js', $libpath);
81 }
82
e874af28 83 } else if (preg_match('/^https?:/', $libname)) {
f72f94a2 84 $libpath = $libname;
e874af28 85
86 } else {
b7658fa0 87 $libpath = $wwwroot . '/' . $libname;
9bb74178 88 }
f72f94a2 89
b958e941 90 // Make sure the file exists if it is local.
91 if ($external_yui === false) {
b7658fa0 92 $testpath = str_replace($wwwroot, $CFG->dirroot, $libpath);
b958e941 93 if (!file_exists($testpath)) {
e874af28 94 throw new moodle_exception('unknownjsinrequirejs', '', '', $libpath);
b958e941 95 }
f72f94a2 96 }
97
98 return $libpath;
9bb74178 99}
88c5092a 100
101
102/**
103 * Returns whether ajax is enabled/allowed or not.
104 */
c2a9fc91 105function ajaxenabled($browsers = array()) {
88c5092a 106
107 global $CFG, $USER;
483f3067 108
c2a9fc91 109 if (!empty($browsers)) {
110 $valid = false;
111 foreach ($browsers as $brand => $version) {
112 if (check_browser_version($brand, $version)) {
113 $valid = true;
483f3067 114 }
c2a9fc91 115 }
483f3067 116
c2a9fc91 117 if (!$valid) {
118 return false;
119 }
120 }
483f3067 121
d499142e 122 $ie = check_browser_version('MSIE', 6.0);
123 $ff = check_browser_version('Gecko', 20051106);
124 $op = check_browser_version('Opera', 9.0);
125 $sa = check_browser_version('Safari', 412);
126
127 if (!$ie && !$ff && !$op && !$sa) {
128 /** @see http://en.wikipedia.org/wiki/User_agent */
483f3067 129 // Gecko build 20051107 is what is in Firefox 1.5.
88c5092a 130 // We still have issues with AJAX in other browsers.
131 return false;
132 }
133
2f11bfc0 134 if (!empty($CFG->enableajax) && (!empty($USER->ajax) || !isloggedin())) {
88c5092a 135 return true;
136 } else {
137 return false;
138 }
139}
9bb74178 140
35b974da 141
142/**
2469f7ea 143 * Used to create view of document to be passed to JavaScript on pageload.
144 * We use this class to pass data from PHP to JavaScript.
35b974da 145 */
4fc45e1d 146class jsportal {
9bb74178 147
0a0bb380 148 var $currentblocksection = null;
9bb74178 149 var $blocks = array();
0a0bb380 150
9bb74178 151
35b974da 152 /**
153 * Takes id of block and adds it
154 */
2469f7ea 155 function block_add($id, $hidden=false){
0a0bb380 156 $hidden_binary = 0;
9bb74178 157
158 if ($hidden) {
159 $hidden_binary = 1;
160 }
35b974da 161 $this->blocks[count($this->blocks)] = array($this->currentblocksection, $id, $hidden_binary);
0a0bb380 162 }
9bb74178 163
164
2469f7ea 165 /**
166 * Prints the JavaScript code needed to set up AJAX for the course.
167 */
168 function print_javascript($courseid, $return=false) {
c4ca9cb3 169 global $CFG, $USER;
9bb74178 170
d4df8fdc 171 $blocksoutput = $output = '';
35b974da 172 for ($i=0; $i<count($this->blocks); $i++) {
2469f7ea 173 $blocksoutput .= "['".$this->blocks[$i][0]."',
174 '".$this->blocks[$i][1]."',
175 '".$this->blocks[$i][2]."']";
176
177 if ($i != (count($this->blocks) - 1)) {
35b974da 178 $blocksoutput .= ',';
9bb74178 179 }
180 }
32f0b38a 181 $output .= "<script type=\"text/javascript\">\n";
72d28452 182 $output .= " main.portal.id = ".$courseid.";\n";
2469f7ea 183 $output .= " main.portal.blocks = new Array(".$blocksoutput.");\n";
184 $output .= " main.portal.strings['wwwroot']='".$CFG->wwwroot."';\n";
d8158863 185 $output .= " main.portal.strings['pixpath']='".$CFG->pixpath."';\n";
f8eaeffa 186 $output .= " main.portal.strings['marker']='".get_string('markthistopic', '', '_var_')."';\n";
187 $output .= " main.portal.strings['marked']='".get_string('markedthistopic', '', '_var_')."';\n";
188 $output .= " main.portal.strings['hide']='".get_string('hide')."';\n";
189 $output .= " main.portal.strings['hidesection']='".get_string('hidesection', '', '_var_')."';\n";
190 $output .= " main.portal.strings['show']='".get_string('show')."';\n";
191 $output .= " main.portal.strings['delete']='".get_string('delete')."';\n";
d2a11d46 192 $output .= " main.portal.strings['move']='".get_string('move')."';\n";
f8eaeffa 193 $output .= " main.portal.strings['movesection']='".get_string('movesection', '', '_var_')."';\n";
d2a11d46 194 $output .= " main.portal.strings['moveleft']='".get_string('moveleft')."';\n";
195 $output .= " main.portal.strings['moveright']='".get_string('moveright')."';\n";
4fc45e1d 196 $output .= " main.portal.strings['update']='".get_string('update')."';\n";
d2a11d46 197 $output .= " main.portal.strings['groupsnone']='".get_string('groupsnone')."';\n";
198 $output .= " main.portal.strings['groupsseparate']='".get_string('groupsseparate')."';\n";
199 $output .= " main.portal.strings['groupsvisible']='".get_string('groupsvisible')."';\n";
200 $output .= " main.portal.strings['clicktochange']='".get_string('clicktochange')."';\n";
2469f7ea 201 $output .= " main.portal.strings['deletecheck']='".get_string('deletecheck','','_var_')."';\n";
202 $output .= " main.portal.strings['resource']='".get_string('resource')."';\n";
203 $output .= " main.portal.strings['activity']='".get_string('activity')."';\n";
d4a1fcaf 204 $output .= " main.portal.strings['sesskey']='".sesskey()."';\n";
2469f7ea 205 $output .= " onloadobj.load();\n";
4fc45e1d 206 $output .= " main.process_blocks();\n";
35b974da 207 $output .= "</script>";
2469f7ea 208 if ($return) {
209 return $output;
210 } else {
211 echo $output;
212 }
0a0bb380 213 }
9bb74178 214
2469f7ea 215}
f68a7947 216
c6307019 217?>