Ed Coyne's AJAX course format, with some cleanups done.
[moodle.git] / lib / ajax / ajaxlib.php
CommitLineData
0a0bb380 1<?PHP
2/**
3 * Library functions for using ajax with moodle
4 **/
5
6
7/**
8 *Print require statements for javascript libraries
9 *Takes in an array of either full paths or shortnames and it will translate them to full paths
10 **/
11
12function print_require_js($list){
13 global $CFG;
14
15 //list of shortname to filepath translations
16 $translatelist = Array(
17 "yui_yahoo" => "/lib/yui/yahoo/yahoo.js",
18 "yui_dom" => "/lib/yui/dom/dom.js",
19 "yui_event" => "/lib/yui/event/event.js",
20 "yui_dragdrop" => "/lib/yui/dragdrop/dragdrop.js",
21 "yui_logger" => "/lib/yui/logger/logger.js",
22 "yui_connection" => "/lib/yui/connection/connection.js",
23 "ajaxcourse_blocks" => "/lib/ajax/block_classes.js",
24 "ajaxcourse_sections" => "/lib/ajax/section-resource_classes.js",
25 "ajaxcourse_topic" => "/course/format/topicsajax/ajaxcourse-topics.js",
26 "ajaxcourse_week" => "/course/format/weeksajax/ajaxcourse-weeks.js"
27 );
28
29
30 for ($i=0;$i<count($list);$i++) {
31 if ($translatelist[$list[$i]]) {
32 echo "<script language='JavaScript' type='text/javascript' src='".$CFG->wwwroot.''.$translatelist[$list[$i]]."'></script>\n\r";
33 } else {
34 echo "<script language='JavaScript' type='text/javascript' src='".$CFG->wwwroot.''.$list[$i]."'></script>\n\r";
35 }
36 }
37
38}
39
40//used to create view of document to be passed to javascript on pageload
41class jsportal{
42
43 var $currentblocksection = null;
44 var $blocks = array();
45 var $sections = array();
46
47
48 //takes id of block and adds it
49 function block_add($id,$hidden=false){
50 $hidden_binary = 0;
51
52 if ($hidden) {
53 $hidden_binary = 1;
54 }
55
56 $this->blocks[count($this->blocks)] = Array($this->currentblocksection,$id,$hidden_binary);
57 }
58
59
60 function print_javascript($id) {
61 global $CFG;
62
63 $blocksoutput = '';
64 for ($i=0;$i<count($this->blocks);$i++){
65 $blocksoutput.="['".$this->blocks[$i][0]."','".$this->blocks[$i][1]."','".$this->blocks[$i][2]."']";
66 if ($i != (count($this->blocks)-1)) {
67 $blocksoutput.=",";
68 }
69 }
70
71 $output ="<script language='javascript'>\r";
72 $output .=" main.portal.id = ".$id."\r";
73 $output .=" main.portal.blocks = new Array(".$blocksoutput.");\r";
74 $output .=" main.portal.wwwroot = '".$CFG->wwwroot."';\r";
75 $output .=" onload.load()\r";
76 $output .=" main.process_blocks();\r";
77 $output .="</script>";
78
79 echo $output;
80 }
81
82}
83
84?>