2006-09-25 14:59:21 +00:00
|
|
|
<?php // Library functions for using AJAX with Moodle
|
2006-09-23 14:03:48 +00:00
|
|
|
|
|
|
|
/**
|
2006-10-18 07:05:21 +00:00
|
|
|
* Print require statements for javascript libraries.
|
|
|
|
* Takes in an array of either full paths or shortnames and it will translate
|
|
|
|
* them to full paths.
|
2006-09-23 14:03:48 +00:00
|
|
|
**/
|
2006-10-24 08:11:38 +00:00
|
|
|
function require_js($list) {
|
2006-09-23 14:03:48 +00:00
|
|
|
global $CFG;
|
2006-10-24 08:11:38 +00:00
|
|
|
$output = '';
|
2006-10-18 09:47:44 +00:00
|
|
|
|
2006-11-17 05:54:21 +00:00
|
|
|
if (!ajaxenabled()) {
|
2006-10-17 05:33:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-10-18 09:47:44 +00:00
|
|
|
|
2006-09-23 14:03:48 +00:00
|
|
|
//list of shortname to filepath translations
|
|
|
|
$translatelist = array(
|
2006-11-14 05:34:38 +00:00
|
|
|
'yui_yahoo' => '/lib/yui/yahoo/yahoo-min.js',
|
|
|
|
'yui_dom' => '/lib/yui/dom/dom-min.js',
|
|
|
|
'yui_event' => '/lib/yui/event/event-min.js',
|
|
|
|
'yui_dragdrop' => '/lib/yui/dragdrop/dragdrop-min.js',
|
|
|
|
'yui_logger' => '/lib/yui/logger/logger-min.js',
|
|
|
|
'yui_connection' => '/lib/yui/connection/connection-min.js',
|
2006-09-23 15:11:37 +00:00
|
|
|
'ajaxcourse_blocks' => '/lib/ajax/block_classes.js',
|
|
|
|
'ajaxcourse_sections' => '/lib/ajax/section_classes.js',
|
2006-09-23 14:03:48 +00:00
|
|
|
'ajaxcourse' => '/lib/ajax/ajaxcourse.js'
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2006-10-05 08:47:13 +00:00
|
|
|
for ($i=0; $i<count($list); $i++) {
|
2006-09-23 14:03:48 +00:00
|
|
|
if ($translatelist[$list[$i]]) {
|
2006-10-24 08:11:38 +00:00
|
|
|
$output .= "<script type='text/javascript' src='".$CFG->wwwroot.''.$translatelist[$list[$i]]."'></script>\n";
|
2006-11-14 05:34:38 +00:00
|
|
|
if ($translatelist[$list[$i]] == '/lib/yui/logger/logger-min.js') {
|
2006-10-24 08:11:38 +00:00
|
|
|
// Special case. We need the css.
|
2006-12-14 04:47:29 +00:00
|
|
|
$output .= "<link type='text/css' rel='stylesheet' href='{$CFG->wwwroot}/lib/yui/logger/assets/logger.css' />";
|
2006-10-24 08:11:38 +00:00
|
|
|
}
|
2006-09-23 14:03:48 +00:00
|
|
|
} else {
|
2006-10-24 08:11:38 +00:00
|
|
|
$output .= "<script type='text/javascript' src='".$CFG->wwwroot.''.$list[$i]."'></script>\n";
|
2006-09-23 14:03:48 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-24 08:11:38 +00:00
|
|
|
return $output;
|
2006-09-23 14:03:48 +00:00
|
|
|
}
|
2006-11-17 05:54:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether ajax is enabled/allowed or not.
|
|
|
|
*/
|
|
|
|
function ajaxenabled() {
|
|
|
|
|
|
|
|
global $CFG, $USER;
|
|
|
|
|
|
|
|
if (!check_browser_version('MSIE', 6.0)
|
2006-12-01 03:46:55 +00:00
|
|
|
&& !check_browser_version('Gecko', 20051111)) {
|
|
|
|
// Gecko build 20051111 is what is in Firefox 1.5.
|
2006-11-17 05:54:21 +00:00
|
|
|
// We still have issues with AJAX in other browsers.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-11-27 03:22:28 +00:00
|
|
|
if (!empty($CFG->enableajax) && (!empty($USER->ajax) || !isloggedin())) {
|
2006-11-17 05:54:21 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2006-09-23 14:03:48 +00:00
|
|
|
|
2006-10-05 08:47:13 +00:00
|
|
|
|
|
|
|
/**
|
2006-10-24 08:11:38 +00:00
|
|
|
* Used to create view of document to be passed to JavaScript on pageload.
|
|
|
|
* We use this class to pass data from PHP to JavaScript.
|
2006-10-05 08:47:13 +00:00
|
|
|
*/
|
2006-10-17 05:33:18 +00:00
|
|
|
class jsportal {
|
2006-09-23 14:03:48 +00:00
|
|
|
|
2006-08-24 03:20:37 +00:00
|
|
|
var $currentblocksection = null;
|
2006-09-23 14:03:48 +00:00
|
|
|
var $blocks = array();
|
2006-08-24 03:20:37 +00:00
|
|
|
|
2006-09-23 14:03:48 +00:00
|
|
|
|
2006-10-05 08:47:13 +00:00
|
|
|
/**
|
|
|
|
* Takes id of block and adds it
|
|
|
|
*/
|
2006-10-24 08:11:38 +00:00
|
|
|
function block_add($id, $hidden=false){
|
2006-08-24 03:20:37 +00:00
|
|
|
$hidden_binary = 0;
|
2006-09-23 14:03:48 +00:00
|
|
|
|
|
|
|
if ($hidden) {
|
|
|
|
$hidden_binary = 1;
|
|
|
|
}
|
2006-10-05 08:47:13 +00:00
|
|
|
$this->blocks[count($this->blocks)] = array($this->currentblocksection, $id, $hidden_binary);
|
2006-08-24 03:20:37 +00:00
|
|
|
}
|
2006-09-23 14:03:48 +00:00
|
|
|
|
|
|
|
|
2006-10-24 08:11:38 +00:00
|
|
|
/**
|
|
|
|
* Prints the JavaScript code needed to set up AJAX for the course.
|
|
|
|
*/
|
|
|
|
function print_javascript($courseid, $return=false) {
|
2006-09-23 14:03:48 +00:00
|
|
|
global $CFG;
|
|
|
|
|
2006-08-28 02:05:02 +00:00
|
|
|
$blocksoutput = $output = '';
|
2006-10-05 08:47:13 +00:00
|
|
|
for ($i=0; $i<count($this->blocks); $i++) {
|
2006-10-24 08:11:38 +00:00
|
|
|
$blocksoutput .= "['".$this->blocks[$i][0]."',
|
|
|
|
'".$this->blocks[$i][1]."',
|
|
|
|
'".$this->blocks[$i][2]."']";
|
|
|
|
|
|
|
|
if ($i != (count($this->blocks) - 1)) {
|
2006-10-05 08:47:13 +00:00
|
|
|
$blocksoutput .= ',';
|
2006-09-23 14:03:48 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-05 08:47:13 +00:00
|
|
|
$output .= "<script language='javascript'>\n";
|
2006-10-24 08:11:38 +00:00
|
|
|
$output .= " main.portal.id = ".$courseid.";\n";
|
|
|
|
$output .= " main.portal.blocks = new Array(".$blocksoutput.");\n";
|
|
|
|
$output .= " main.portal.strings['wwwroot']='".$CFG->wwwroot."';\n";
|
2006-11-14 03:07:18 +00:00
|
|
|
$output .= " main.portal.strings['pixpath']='".$CFG->pixpath."';\n";
|
2006-11-20 09:21:27 +00:00
|
|
|
$output .= " main.portal.strings['move']='".get_string('move')."';\n";
|
|
|
|
$output .= " main.portal.strings['moveleft']='".get_string('moveleft')."';\n";
|
|
|
|
$output .= " main.portal.strings['moveright']='".get_string('moveright')."';\n";
|
2006-10-17 05:33:18 +00:00
|
|
|
$output .= " main.portal.strings['update']='".get_string('update')."';\n";
|
2006-11-20 09:21:27 +00:00
|
|
|
$output .= " main.portal.strings['groupsnone']='".get_string('groupsnone')."';\n";
|
|
|
|
$output .= " main.portal.strings['groupsseparate']='".get_string('groupsseparate')."';\n";
|
|
|
|
$output .= " main.portal.strings['groupsvisible']='".get_string('groupsvisible')."';\n";
|
|
|
|
$output .= " main.portal.strings['clicktochange']='".get_string('clicktochange')."';\n";
|
2006-10-24 08:11:38 +00:00
|
|
|
$output .= " main.portal.strings['deletecheck']='".get_string('deletecheck','','_var_')."';\n";
|
|
|
|
$output .= " main.portal.strings['resource']='".get_string('resource')."';\n";
|
|
|
|
$output .= " main.portal.strings['activity']='".get_string('activity')."';\n";
|
|
|
|
$output .= " onloadobj.load();\n";
|
2006-10-17 05:33:18 +00:00
|
|
|
$output .= " main.process_blocks();\n";
|
2006-10-05 08:47:13 +00:00
|
|
|
$output .= "</script>";
|
2006-10-24 08:11:38 +00:00
|
|
|
if ($return) {
|
|
|
|
return $output;
|
|
|
|
} else {
|
|
|
|
echo $output;
|
|
|
|
}
|
2006-08-24 03:20:37 +00:00
|
|
|
}
|
2006-09-23 14:03:48 +00:00
|
|
|
|
2006-10-24 08:11:38 +00:00
|
|
|
}
|
2006-11-15 08:07:32 +00:00
|
|
|
|
|
|
|
?>
|