Added help title for switchrole help button MDL-8378
[moodle.git] / lib / ajax / ajaxlib.php
CommitLineData
4488daf7 1<?php // Library functions for using AJAX with Moodle
9bb74178 2
3/**
8b8a8779 4 * Print require statements for javascript libraries.
5 * Takes in an array of either full paths or shortnames and it will translate
6 * them to full paths.
9bb74178 7 **/
2469f7ea 8function require_js($list) {
9bb74178 9 global $CFG;
2469f7ea 10 $output = '';
c081fdca 11
88c5092a 12 if (!ajaxenabled()) {
4fc45e1d 13 return;
14 }
c081fdca 15
9bb74178 16 //list of shortname to filepath translations
17 $translatelist = array(
3972629d 18 'yui_yahoo' => '/lib/yui/yahoo/yahoo-min.js',
67c0e16e 19 'yui_animation' => '/lib/yui/animation/animation-min.js',
20 'yui_autocomplete' => '/lib/yui/autocomplete/autocomplete-min.js',
21 'yui_calendar' => '/lib/yui/calendar/calendar-min.js',
22 'yui_connection' => '/lib/yui/connection/connection-min.js',
23 'yui_container' => '/lib/yui/container/container-min.js',
3972629d 24 'yui_dom' => '/lib/yui/dom/dom-min.js',
67c0e16e 25 'yui_dom-event' => '/lib/yui/yahoo-dom-event/yahoo-dom-event.js',
26 'yui_dragdrop' => '/lib/yui/dragdrop/dragdrop-min.js',
3972629d 27 'yui_event' => '/lib/yui/event/event-min.js',
3972629d 28 'yui_logger' => '/lib/yui/logger/logger-min.js',
67c0e16e 29 'yui_menu' => '/lib/yui/menu/menu-min.js',
30 'yui_tabview' => '/lib/yui/tabview/tabview-min.js',
31 'yui_treeview' => '/lib/yui/treeview/treeview-min.js',
32 'yui_slider' => '/lib/yui/slider/slider-min.js',
33 'yui_utilities' => '/lib/yui/utilities/utilities.js',
3e1e2b69 34 'ajaxcourse_blocks' => '/lib/ajax/block_classes.js',
35 'ajaxcourse_sections' => '/lib/ajax/section_classes.js',
9bb74178 36 'ajaxcourse' => '/lib/ajax/ajaxcourse.js'
37 );
38
35b974da 39 for ($i=0; $i<count($list); $i++) {
9bb74178 40 if ($translatelist[$list[$i]]) {
2469f7ea 41 $output .= "<script type='text/javascript' src='".$CFG->wwwroot.''.$translatelist[$list[$i]]."'></script>\n";
3972629d 42 if ($translatelist[$list[$i]] == '/lib/yui/logger/logger-min.js') {
2469f7ea 43 // Special case. We need the css.
e9880ed2 44 $output .= "<link type='text/css' rel='stylesheet' href='{$CFG->wwwroot}/lib/yui/logger/assets/logger.css' />";
2469f7ea 45 }
9bb74178 46 } else {
2469f7ea 47 $output .= "<script type='text/javascript' src='".$CFG->wwwroot.''.$list[$i]."'></script>\n";
9bb74178 48 }
49 }
2469f7ea 50 return $output;
9bb74178 51}
88c5092a 52
53
54/**
55 * Returns whether ajax is enabled/allowed or not.
56 */
57function ajaxenabled() {
58
59 global $CFG, $USER;
60
61 if (!check_browser_version('MSIE', 6.0)
4a5548f9 62 && !check_browser_version('Gecko', 20051111)) {
63 // Gecko build 20051111 is what is in Firefox 1.5.
88c5092a 64 // We still have issues with AJAX in other browsers.
65 return false;
66 }
67
2f11bfc0 68 if (!empty($CFG->enableajax) && (!empty($USER->ajax) || !isloggedin())) {
88c5092a 69 return true;
70 } else {
71 return false;
72 }
73}
9bb74178 74
35b974da 75
76/**
2469f7ea 77 * Used to create view of document to be passed to JavaScript on pageload.
78 * We use this class to pass data from PHP to JavaScript.
35b974da 79 */
4fc45e1d 80class jsportal {
9bb74178 81
0a0bb380 82 var $currentblocksection = null;
9bb74178 83 var $blocks = array();
0a0bb380 84
9bb74178 85
35b974da 86 /**
87 * Takes id of block and adds it
88 */
2469f7ea 89 function block_add($id, $hidden=false){
0a0bb380 90 $hidden_binary = 0;
9bb74178 91
92 if ($hidden) {
93 $hidden_binary = 1;
94 }
35b974da 95 $this->blocks[count($this->blocks)] = array($this->currentblocksection, $id, $hidden_binary);
0a0bb380 96 }
9bb74178 97
98
2469f7ea 99 /**
100 * Prints the JavaScript code needed to set up AJAX for the course.
101 */
102 function print_javascript($courseid, $return=false) {
c4ca9cb3 103 global $CFG, $USER;
9bb74178 104
d4df8fdc 105 $blocksoutput = $output = '';
35b974da 106 for ($i=0; $i<count($this->blocks); $i++) {
2469f7ea 107 $blocksoutput .= "['".$this->blocks[$i][0]."',
108 '".$this->blocks[$i][1]."',
109 '".$this->blocks[$i][2]."']";
110
111 if ($i != (count($this->blocks) - 1)) {
35b974da 112 $blocksoutput .= ',';
9bb74178 113 }
114 }
32f0b38a 115 $output .= "<script type=\"text/javascript\">\n";
2469f7ea 116 $output .= " main.portal.id = ".$courseid.";\n";
117 $output .= " main.portal.blocks = new Array(".$blocksoutput.");\n";
118 $output .= " main.portal.strings['wwwroot']='".$CFG->wwwroot."';\n";
d8158863 119 $output .= " main.portal.strings['pixpath']='".$CFG->pixpath."';\n";
d2a11d46 120 $output .= " main.portal.strings['move']='".get_string('move')."';\n";
121 $output .= " main.portal.strings['moveleft']='".get_string('moveleft')."';\n";
122 $output .= " main.portal.strings['moveright']='".get_string('moveright')."';\n";
4fc45e1d 123 $output .= " main.portal.strings['update']='".get_string('update')."';\n";
d2a11d46 124 $output .= " main.portal.strings['groupsnone']='".get_string('groupsnone')."';\n";
125 $output .= " main.portal.strings['groupsseparate']='".get_string('groupsseparate')."';\n";
126 $output .= " main.portal.strings['groupsvisible']='".get_string('groupsvisible')."';\n";
127 $output .= " main.portal.strings['clicktochange']='".get_string('clicktochange')."';\n";
2469f7ea 128 $output .= " main.portal.strings['deletecheck']='".get_string('deletecheck','','_var_')."';\n";
129 $output .= " main.portal.strings['resource']='".get_string('resource')."';\n";
130 $output .= " main.portal.strings['activity']='".get_string('activity')."';\n";
c4ca9cb3 131 $output .= " main.portal.strings['sesskey']='".$USER->sesskey."';\n";
2469f7ea 132 $output .= " onloadobj.load();\n";
4fc45e1d 133 $output .= " main.process_blocks();\n";
35b974da 134 $output .= "</script>";
2469f7ea 135 if ($return) {
136 return $output;
137 } else {
138 echo $output;
139 }
0a0bb380 140 }
9bb74178 141
2469f7ea 142}
f68a7947 143
144?>