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