MDL-14738 Merged. Roles section on course settings is now "Role renaming", with...
[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) {
12
13 global $CFG;
14 $libpath = '';
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',
20 'yui_calendar' => '/lib/yui/calendar/calendar-min.js',
21 'yui_connection' => '/lib/yui/connection/connection-min.js',
22 'yui_container' => '/lib/yui/container/container-min.js',
b244b9b7 23 'yui_datasource' => '/lib/yui/datasource/datasource-beta-min.js',
3972629d 24 'yui_dom' => '/lib/yui/dom/dom-min.js',
72d28452 25 'yui_dom-event' => '/lib/yui/yahoo-dom-event/yahoo-dom-event.js',
26 'yui_dragdrop' => '/lib/yui/dragdrop/dragdrop-min.js',
b244b9b7 27 'yui_ddsend' => '/lib/yui/dragdrop/ddsend.js',
3972629d 28 'yui_event' => '/lib/yui/event/event-min.js',
b244b9b7 29 'yui_json' => '/lib/yui/json/json-min.js',
3972629d 30 'yui_logger' => '/lib/yui/logger/logger-min.js',
72d28452 31 'yui_menu' => '/lib/yui/menu/menu-min.js',
32 'yui_tabview' => '/lib/yui/tabview/tabview-min.js',
33 'yui_treeview' => '/lib/yui/treeview/treeview-min.js',
34 'yui_slider' => '/lib/yui/slider/slider-min.js',
35 'yui_utilities' => '/lib/yui/utilities/utilities.js',
3e1e2b69 36 'ajaxcourse_blocks' => '/lib/ajax/block_classes.js',
37 'ajaxcourse_sections' => '/lib/ajax/section_classes.js',
9bb74178 38 'ajaxcourse' => '/lib/ajax/ajaxcourse.js'
39 );
40
f72f94a2 41 if (array_key_exists($libname, $translatelist)) {
42 $libpath = $CFG->wwwroot . $translatelist[$libname];
43 } else {
44 $libpath = $libname;
9bb74178 45 }
f72f94a2 46
47 $testpath = str_replace($CFG->wwwroot, $CFG->dirroot, $libpath);
48 if (!file_exists($testpath)) {
5a2a5331 49 print_error('require_js: '.$libpath.' - file not found.');
f72f94a2 50 }
51
52 return $libpath;
9bb74178 53}
88c5092a 54
55
56/**
57 * Returns whether ajax is enabled/allowed or not.
58 */
c2a9fc91 59function ajaxenabled($browsers = array()) {
88c5092a 60
61 global $CFG, $USER;
d499142e 62
c2a9fc91 63 if (!empty($browsers)) {
64 $valid = false;
65 foreach ($browsers as $brand => $version) {
66 if (check_browser_version($brand, $version)) {
67 $valid = true;
68 }
69 }
70
71 if (!$valid) {
72 return false;
73 }
74 }
75
d499142e 76 $ie = check_browser_version('MSIE', 6.0);
77 $ff = check_browser_version('Gecko', 20051106);
78 $op = check_browser_version('Opera', 9.0);
79 $sa = check_browser_version('Safari', 412);
80
81 if (!$ie && !$ff && !$op && !$sa) {
82 /** @see http://en.wikipedia.org/wiki/User_agent */
83 // Gecko build 20051107 is what is in Firefox 1.5.
88c5092a 84 // We still have issues with AJAX in other browsers.
85 return false;
86 }
87
2f11bfc0 88 if (!empty($CFG->enableajax) && (!empty($USER->ajax) || !isloggedin())) {
88c5092a 89 return true;
90 } else {
91 return false;
92 }
93}
9bb74178 94
35b974da 95
96/**
2469f7ea 97 * Used to create view of document to be passed to JavaScript on pageload.
98 * We use this class to pass data from PHP to JavaScript.
35b974da 99 */
4fc45e1d 100class jsportal {
9bb74178 101
0a0bb380 102 var $currentblocksection = null;
9bb74178 103 var $blocks = array();
0a0bb380 104
9bb74178 105
35b974da 106 /**
107 * Takes id of block and adds it
108 */
2469f7ea 109 function block_add($id, $hidden=false){
0a0bb380 110 $hidden_binary = 0;
9bb74178 111
112 if ($hidden) {
113 $hidden_binary = 1;
114 }
35b974da 115 $this->blocks[count($this->blocks)] = array($this->currentblocksection, $id, $hidden_binary);
0a0bb380 116 }
9bb74178 117
118
2469f7ea 119 /**
120 * Prints the JavaScript code needed to set up AJAX for the course.
121 */
122 function print_javascript($courseid, $return=false) {
c4ca9cb3 123 global $CFG, $USER;
9bb74178 124
d4df8fdc 125 $blocksoutput = $output = '';
35b974da 126 for ($i=0; $i<count($this->blocks); $i++) {
2469f7ea 127 $blocksoutput .= "['".$this->blocks[$i][0]."',
128 '".$this->blocks[$i][1]."',
129 '".$this->blocks[$i][2]."']";
130
131 if ($i != (count($this->blocks) - 1)) {
35b974da 132 $blocksoutput .= ',';
9bb74178 133 }
134 }
32f0b38a 135 $output .= "<script type=\"text/javascript\">\n";
72d28452 136 $output .= " main.portal.id = ".$courseid.";\n";
2469f7ea 137 $output .= " main.portal.blocks = new Array(".$blocksoutput.");\n";
138 $output .= " main.portal.strings['wwwroot']='".$CFG->wwwroot."';\n";
d8158863 139 $output .= " main.portal.strings['pixpath']='".$CFG->pixpath."';\n";
f8eaeffa 140 $output .= " main.portal.strings['marker']='".get_string('markthistopic', '', '_var_')."';\n";
141 $output .= " main.portal.strings['marked']='".get_string('markedthistopic', '', '_var_')."';\n";
142 $output .= " main.portal.strings['hide']='".get_string('hide')."';\n";
143 $output .= " main.portal.strings['hidesection']='".get_string('hidesection', '', '_var_')."';\n";
144 $output .= " main.portal.strings['show']='".get_string('show')."';\n";
145 $output .= " main.portal.strings['delete']='".get_string('delete')."';\n";
d2a11d46 146 $output .= " main.portal.strings['move']='".get_string('move')."';\n";
f8eaeffa 147 $output .= " main.portal.strings['movesection']='".get_string('movesection', '', '_var_')."';\n";
d2a11d46 148 $output .= " main.portal.strings['moveleft']='".get_string('moveleft')."';\n";
149 $output .= " main.portal.strings['moveright']='".get_string('moveright')."';\n";
4fc45e1d 150 $output .= " main.portal.strings['update']='".get_string('update')."';\n";
d2a11d46 151 $output .= " main.portal.strings['groupsnone']='".get_string('groupsnone')."';\n";
152 $output .= " main.portal.strings['groupsseparate']='".get_string('groupsseparate')."';\n";
153 $output .= " main.portal.strings['groupsvisible']='".get_string('groupsvisible')."';\n";
154 $output .= " main.portal.strings['clicktochange']='".get_string('clicktochange')."';\n";
2469f7ea 155 $output .= " main.portal.strings['deletecheck']='".get_string('deletecheck','','_var_')."';\n";
156 $output .= " main.portal.strings['resource']='".get_string('resource')."';\n";
157 $output .= " main.portal.strings['activity']='".get_string('activity')."';\n";
c4ca9cb3 158 $output .= " main.portal.strings['sesskey']='".$USER->sesskey."';\n";
2469f7ea 159 $output .= " onloadobj.load();\n";
4fc45e1d 160 $output .= " main.process_blocks();\n";
35b974da 161 $output .= "</script>";
2469f7ea 162 if ($return) {
163 return $output;
164 } else {
165 echo $output;
166 }
0a0bb380 167 }
9bb74178 168
2469f7ea 169}
f68a7947 170
c6307019 171?>