Merged from 1.7.
[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',
19 'yui_dom' => '/lib/yui/dom/dom-min.js',
20 'yui_event' => '/lib/yui/event/event-min.js',
21 'yui_dragdrop' => '/lib/yui/dragdrop/dragdrop-min.js',
22 'yui_logger' => '/lib/yui/logger/logger-min.js',
23 'yui_connection' => '/lib/yui/connection/connection-min.js',
3e1e2b69 24 'ajaxcourse_blocks' => '/lib/ajax/block_classes.js',
25 'ajaxcourse_sections' => '/lib/ajax/section_classes.js',
9bb74178 26 'ajaxcourse' => '/lib/ajax/ajaxcourse.js'
27 );
28
29
35b974da 30 for ($i=0; $i<count($list); $i++) {
9bb74178 31 if ($translatelist[$list[$i]]) {
2469f7ea 32 $output .= "<script type='text/javascript' src='".$CFG->wwwroot.''.$translatelist[$list[$i]]."'></script>\n";
3972629d 33 if ($translatelist[$list[$i]] == '/lib/yui/logger/logger-min.js') {
2469f7ea 34 // Special case. We need the css.
e9880ed2 35 $output .= "<link type='text/css' rel='stylesheet' href='{$CFG->wwwroot}/lib/yui/logger/assets/logger.css' />";
2469f7ea 36 }
9bb74178 37 } else {
2469f7ea 38 $output .= "<script type='text/javascript' src='".$CFG->wwwroot.''.$list[$i]."'></script>\n";
9bb74178 39 }
40 }
2469f7ea 41 return $output;
9bb74178 42}
88c5092a 43
44
45/**
46 * Returns whether ajax is enabled/allowed or not.
47 */
48function ajaxenabled() {
49
50 global $CFG, $USER;
51
52 if (!check_browser_version('MSIE', 6.0)
4a5548f9 53 && !check_browser_version('Gecko', 20051111)) {
54 // Gecko build 20051111 is what is in Firefox 1.5.
88c5092a 55 // We still have issues with AJAX in other browsers.
56 return false;
57 }
58
2f11bfc0 59 if (!empty($CFG->enableajax) && (!empty($USER->ajax) || !isloggedin())) {
88c5092a 60 return true;
61 } else {
62 return false;
63 }
64}
9bb74178 65
35b974da 66
67/**
2469f7ea 68 * Used to create view of document to be passed to JavaScript on pageload.
69 * We use this class to pass data from PHP to JavaScript.
35b974da 70 */
4fc45e1d 71class jsportal {
9bb74178 72
0a0bb380 73 var $currentblocksection = null;
9bb74178 74 var $blocks = array();
0a0bb380 75
9bb74178 76
35b974da 77 /**
78 * Takes id of block and adds it
79 */
2469f7ea 80 function block_add($id, $hidden=false){
0a0bb380 81 $hidden_binary = 0;
9bb74178 82
83 if ($hidden) {
84 $hidden_binary = 1;
85 }
35b974da 86 $this->blocks[count($this->blocks)] = array($this->currentblocksection, $id, $hidden_binary);
0a0bb380 87 }
9bb74178 88
89
2469f7ea 90 /**
91 * Prints the JavaScript code needed to set up AJAX for the course.
92 */
93 function print_javascript($courseid, $return=false) {
9bb74178 94 global $CFG;
95
d4df8fdc 96 $blocksoutput = $output = '';
35b974da 97 for ($i=0; $i<count($this->blocks); $i++) {
2469f7ea 98 $blocksoutput .= "['".$this->blocks[$i][0]."',
99 '".$this->blocks[$i][1]."',
100 '".$this->blocks[$i][2]."']";
101
102 if ($i != (count($this->blocks) - 1)) {
35b974da 103 $blocksoutput .= ',';
9bb74178 104 }
105 }
35b974da 106 $output .= "<script language='javascript'>\n";
2469f7ea 107 $output .= " main.portal.id = ".$courseid.";\n";
108 $output .= " main.portal.blocks = new Array(".$blocksoutput.");\n";
109 $output .= " main.portal.strings['wwwroot']='".$CFG->wwwroot."';\n";
d8158863 110 $output .= " main.portal.strings['pixpath']='".$CFG->pixpath."';\n";
d2a11d46 111 $output .= " main.portal.strings['move']='".get_string('move')."';\n";
112 $output .= " main.portal.strings['moveleft']='".get_string('moveleft')."';\n";
113 $output .= " main.portal.strings['moveright']='".get_string('moveright')."';\n";
4fc45e1d 114 $output .= " main.portal.strings['update']='".get_string('update')."';\n";
d2a11d46 115 $output .= " main.portal.strings['groupsnone']='".get_string('groupsnone')."';\n";
116 $output .= " main.portal.strings['groupsseparate']='".get_string('groupsseparate')."';\n";
117 $output .= " main.portal.strings['groupsvisible']='".get_string('groupsvisible')."';\n";
118 $output .= " main.portal.strings['clicktochange']='".get_string('clicktochange')."';\n";
2469f7ea 119 $output .= " main.portal.strings['deletecheck']='".get_string('deletecheck','','_var_')."';\n";
120 $output .= " main.portal.strings['resource']='".get_string('resource')."';\n";
121 $output .= " main.portal.strings['activity']='".get_string('activity')."';\n";
122 $output .= " onloadobj.load();\n";
4fc45e1d 123 $output .= " main.process_blocks();\n";
35b974da 124 $output .= "</script>";
2469f7ea 125 if ($return) {
126 return $output;
127 } else {
128 echo $output;
129 }
0a0bb380 130 }
9bb74178 131
2469f7ea 132}
f68a7947 133
134?>