MDL-14624:
[moodle-linuxchix.git] / lib / ajax / ajaxlib.php
blobec953df4f13df52d55818f9d4d64334c45220d30
1 <?php
2 /**
3 * Library functions for using AJAX with Moodle.
4 */
6 /**
7 * Get the path to a JavaScript library.
8 * @param $libname - the name of the library whose path we need.
9 * @return string
11 function ajax_get_lib($libname) {
13 global $CFG;
14 $libpath = '';
16 $translatelist = array(
17 'yui_yahoo' => '/lib/yui/yahoo/yahoo-min.js',
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',
23 'yui_dom' => '/lib/yui/dom/dom-min.js',
24 'yui_dom-event' => '/lib/yui/yahoo-dom-event/yahoo-dom-event.js',
25 'yui_dragdrop' => '/lib/yui/dragdrop/dragdrop-min.js',
26 'yui_event' => '/lib/yui/event/event-min.js',
27 'yui_logger' => '/lib/yui/logger/logger-min.js',
28 'yui_menu' => '/lib/yui/menu/menu-min.js',
29 'yui_tabview' => '/lib/yui/tabview/tabview-min.js',
30 'yui_treeview' => '/lib/yui/treeview/treeview-min.js',
31 'yui_slider' => '/lib/yui/slider/slider-min.js',
32 'yui_utilities' => '/lib/yui/utilities/utilities.js',
33 'ajaxcourse_blocks' => '/lib/ajax/block_classes.js',
34 'ajaxcourse_sections' => '/lib/ajax/section_classes.js',
35 'ajaxcourse' => '/lib/ajax/ajaxcourse.js'
38 if (array_key_exists($libname, $translatelist)) {
39 $libpath = $CFG->wwwroot . $translatelist[$libname];
40 } else {
41 $libpath = $libname;
44 $testpath = str_replace($CFG->wwwroot, $CFG->dirroot, $libpath);
45 if (!file_exists($testpath)) {
46 error('require_js: '.$libpath.' - file not found.');
49 return $libpath;
53 /**
54 * Returns whether ajax is enabled/allowed or not.
56 function ajaxenabled($browsers = array()) {
58 global $CFG, $USER;
60 if (!empty($browsers)) {
61 $valid = false;
62 foreach ($browsers as $brand => $version) {
63 if (check_browser_version($brand, $version)) {
64 $valid = true;
68 if (!$valid) {
69 return false;
73 $ie = check_browser_version('MSIE', 6.0);
74 $ff = check_browser_version('Gecko', 20051106);
75 $op = check_browser_version('Opera', 9.0);
76 $sa = check_browser_version('Safari', 412);
78 if (!$ie && !$ff && !$op && !$sa) {
79 /** @see http://en.wikipedia.org/wiki/User_agent */
80 // Gecko build 20051107 is what is in Firefox 1.5.
81 // We still have issues with AJAX in other browsers.
82 return false;
85 if (!empty($CFG->enableajax) && (!empty($USER->ajax) || !isloggedin())) {
86 return true;
87 } else {
88 return false;
93 /**
94 * Used to create view of document to be passed to JavaScript on pageload.
95 * We use this class to pass data from PHP to JavaScript.
97 class jsportal {
99 var $currentblocksection = null;
100 var $blocks = array();
104 * Takes id of block and adds it
106 function block_add($id, $hidden=false){
107 $hidden_binary = 0;
109 if ($hidden) {
110 $hidden_binary = 1;
112 $this->blocks[count($this->blocks)] = array($this->currentblocksection, $id, $hidden_binary);
117 * Prints the JavaScript code needed to set up AJAX for the course.
119 function print_javascript($courseid, $return=false) {
120 global $CFG, $USER;
122 $blocksoutput = $output = '';
123 for ($i=0; $i<count($this->blocks); $i++) {
124 $blocksoutput .= "['".$this->blocks[$i][0]."',
125 '".$this->blocks[$i][1]."',
126 '".$this->blocks[$i][2]."']";
128 if ($i != (count($this->blocks) - 1)) {
129 $blocksoutput .= ',';
132 $output .= "<script type=\"text/javascript\">\n";
133 $output .= " main.portal.id = ".$courseid.";\n";
134 $output .= " main.portal.blocks = new Array(".$blocksoutput.");\n";
135 $output .= " main.portal.strings['wwwroot']='".$CFG->wwwroot."';\n";
136 $output .= " main.portal.strings['pixpath']='".$CFG->pixpath."';\n";
137 $output .= " main.portal.strings['marker']='".get_string('markthistopic', '', '_var_')."';\n";
138 $output .= " main.portal.strings['marked']='".get_string('markedthistopic', '', '_var_')."';\n";
139 $output .= " main.portal.strings['hide']='".get_string('hide')."';\n";
140 $output .= " main.portal.strings['hidesection']='".get_string('hidesection', '', '_var_')."';\n";
141 $output .= " main.portal.strings['show']='".get_string('show')."';\n";
142 $output .= " main.portal.strings['delete']='".get_string('delete')."';\n";
143 $output .= " main.portal.strings['move']='".get_string('move')."';\n";
144 $output .= " main.portal.strings['movesection']='".get_string('movesection', '', '_var_')."';\n";
145 $output .= " main.portal.strings['moveleft']='".get_string('moveleft')."';\n";
146 $output .= " main.portal.strings['moveright']='".get_string('moveright')."';\n";
147 $output .= " main.portal.strings['update']='".get_string('update')."';\n";
148 $output .= " main.portal.strings['groupsnone']='".get_string('groupsnone')."';\n";
149 $output .= " main.portal.strings['groupsseparate']='".get_string('groupsseparate')."';\n";
150 $output .= " main.portal.strings['groupsvisible']='".get_string('groupsvisible')."';\n";
151 $output .= " main.portal.strings['clicktochange']='".get_string('clicktochange')."';\n";
152 $output .= " main.portal.strings['deletecheck']='".get_string('deletecheck','','_var_')."';\n";
153 $output .= " main.portal.strings['resource']='".get_string('resource')."';\n";
154 $output .= " main.portal.strings['activity']='".get_string('activity')."';\n";
155 $output .= " main.portal.strings['sesskey']='".$USER->sesskey."';\n";
156 $output .= " onloadobj.load();\n";
157 $output .= " main.process_blocks();\n";
158 $output .= "</script>";
159 if ($return) {
160 return $output;
161 } else {
162 echo $output;