timeline: if a section is set to hidden and the user is not capable of editing a...
[moodle-blog-course-format.git] / lib / ajax / ajaxcourse.js
blob1ef863e5b42e0a02353e54c2543eb43de75183eb
1 /**
2  * Contains Main class and supporting functions for ajax course layout
3  *
4  * $Id$
5  */
8 //hide content body until done loading (manipulation looks ugly elsewise)
9 //document.getElementById('content').style.display = 'none';
11 // If firebug console is undefined, define a fake one here
12 if (window.console) {
13     console.vardump = function(data) {
14         retval = '';
15         for (key in data) {
16             retval += key+' = '+data[key] + "\n";
17         }
18         console.log(retval);
19     };
22 //onload object for handling scripts on page load, this insures they run in my order
23 function onload_class() {
24     this.scripts = new Array();
25     this.debug = true;
29 onload_class.prototype.add = function(script) {
30     if (this.debug) {
31         YAHOO.log("onloadobj.add - adding "+script, "junk");
32     }
33     this.scripts[this.scripts.length] = script;
37 onload_class.prototype.load = function() {
38     var scriptcount = this.scripts.length;
39     if (this.debug) {
40         YAHOO.log("onloadobj.load - loading "+scriptcount+" scripts", "info");
41     }
42     for (i=0; i<scriptcount; i++) {
43         eval(this.scripts[i]);
44     }
48 var onloadobj = new onload_class();
51 //main page object
52 function main_class() {
53     this.debug = true;
54     this.portal = new php_portal_class();
56     this.blocks = new Array();
57     this.sections = new Array();
58     this.sectiondates = {};
59     this.leftcolumn = null;
60     this.rightcolumn = null;
61     this.adminBlock = null;
62     this.tempBlock = null;
63     this.icons = [];
64     this.marker = null;
65     this.courseformat = null;
66     this.numsections = null;
68     //things to process onload
69     onloadobj.add('main.process_document();');
70     onloadobj.add("document.getElementById('content').style.display='block';");
72     //connection queue allows xhttp requests to be sent in order
73     this.connectQueue = [];
74     this.connectQueueHead = 0;
75     this.connectQueueConnection = null;
79 main_class.prototype.process_blocks = function() {
80     //remove unneeded icons (old school position icons and delete/hide
81     //although they will be read)
82     var rmIconClasses = ['icon up', 'icon down', 'icon right', 'icon left', 'icon delete', 'icon hide'];
83     for (var c=0; c<rmIconClasses.length; c++) {
84         els = YAHOO.util.Dom.getElementsByClassName(rmIconClasses[c]);
86         for (var x=0; x<els.length; x++) {
87             els[x].parentNode.removeChild(els[x]);
88         }
89     }    
90     //process the block ids passed from php
91     var blockcount = this.portal.blocks.length;
92     YAHOO.log("main.processBlocks - processing "+blockcount+" blocks", "info");
94     for (i=0; i<blockcount; i++) {
95         this.blocks[i] = new block_class(this.portal.blocks[i][1], "blocks");
97         //put in correct side array also
98         if (this.portal.blocks[i][0] == 'l') {
99             main.leftcolumn.add_block(this.blocks[i]);
100         } else if (this.portal.blocks[i][0] == 'r') {
101             main.rightcolumn.add_block(this.blocks[i]);  
102         }
104         //hide if called for
105         if (this.portal.blocks[i][2] == 1) {
106             this.blocks[i].toggle_hide(null, null, true);
107         }
108     }     
112 main_class.prototype.process_document = function() {
113     //process the document to get important containers
114     YAHOO.log("Processing Document", "info");
116     //process columns for blocks
117     this.leftcolumn = new column_class('left-column', "blocks", null, 'l');
118     this.rightcolumn = new column_class('right-column', "blocks", null, 'r');
120     this.courseformat = this.portal.courseformat;
121     //process sections
122     var tr_sections = YAHOO.util.Dom.getElementsByClassName('section main', 'tr');
123     for (var ct=0; ct<tr_sections.length; ct++) {
124         this.sections[ct] = new section_class(tr_sections[ct].id, "sections", null, ct!=0?true:false);
125         this.sections[ct].addToGroup('resources');
126         if (ct > 0) {
127             var sectiontitle = YAHOO.util.Selector.query('#section-'+ct+' h3.weekdates')[0];
128             if (undefined !== sectiontitle) { // Only save date for weekly format
129                 this.sectiondates[ct] = sectiontitle.innerHTML;
130             }
131         }     
132     }
133     if (this.debug) {
134         YAHOO.log("Processed "+ct+" sections");
135     }
137     this.adminBlock = YAHOO.util.Dom.getElementsByClassName('block_adminblock')[0];
138     this.tempBlock = YAHOO.util.Dom.getElementsByClassName('tempblockhandler')[0];
139     
140     YAHOO.log("admin - "+this.adminBlock.className);
144 main_class.prototype.mk_safe_for_transport = function(input) {
145     return input.replace(/&/i, '_.amp._');
149 //return block by id
150 main_class.prototype.get_block_index = function(el) {
151     var blockcount = this.blocks.length;
152     for (i=0; i<blockcount; i++) {
153         if (this.blocks[i] == el) {
154             return i;
155         }
156     }
160 main_class.prototype.get_section_index = function(el) {
161     var sectioncount = this.sections.length;
162     for (i=0; i<sectioncount; i++) {
163         if (this.sections[i] == el) {
164             return i;
165         }
166     }
169 main_class.prototype.mk_button = function(tag, imgSrc, text, attributes, imgAttributes) {
170     //Create button and return object.
171     //Set the text: the container TITLE or image ALT attributes can be overridden, eg.
172     //  main.mk_button('a', '/i/move_2d.gif', strmove, [['title', strmoveshort]]);
173     var container = document.createElement(tag);
174     container.style.cursor = 'pointer';
175     container.setAttribute('title', text);
176     var image = document.createElement('img');
178     image.setAttribute('src', main.portal.strings['pixpath']+imgSrc);
179     image.setAttribute('alt', text);
180     //image.setAttribute('title', '');
181     container.appendChild(image);
183     if (attributes != null) {
184         for (var c=0; c<attributes.length; c++) {
185             if (attributes[c][0] == 'title' && this.is_ie()) {
186                 image.setAttribute(attributes[c][0], attributes[c][1]); //IE hack: transfer 'title'.
187             } else {
188                 container.setAttribute(attributes[c][0], attributes[c][1]);
189             }
190         }
191     }
192     if (imgAttributes != null) {
193         for (var c=0; c<imgAttributes.length; c++) {
194             image.setAttribute(imgAttributes[c][0], imgAttributes[c][1]);
195         }
196     }
197     image.setAttribute('hspace', '3');
198     return container;
202 main_class.prototype.connect = function(method, urlStub, callback, body) {
203     if (this.debug) {
204         YAHOO.log("Making "+method+" connection to /course/rest.php?courseId="+main.portal.id+"&"+urlStub);
205     }
206     if (callback == null) {
207         if (this.debug) {
208             callback = {
209                 success: function(response) {
210                     YAHOO.log("Response from the Request: " + response.statusText + ": " + response.responseText, 'info');
211                 },
212                 failure: function() {
213                     YAHOO.log("Response from the Request: " + response.statusText + ": " + response.responseText, 'error');
214                 }
215             };
216         } else {
217             callback = {};
218         }
219     }
220     return YAHOO.util.Connect.asyncRequest(method, this.portal.strings['wwwroot']+"/course/rest.php?courseId="+main.portal.id+"&sesskey="+this.portal.strings['sesskey']+"&"+urlStub, callback, body);
224 main_class.prototype.connectQueue_add = function(method, urlStub, callback, body) {
225     var Qlength = main.connectQueue.length;
226     main.connectQueue[Qlength] = [];
227     main.connectQueue[Qlength]['method'] = method;
228     main.connectQueue[Qlength]['urlStub'] = urlStub;
229     main.connectQueue[Qlength]['body'] = body;
231     if (main.connectQueueConnection == null || !YAHOO.util.Connect.isCallInProgress(main.connectQueueConnection)) {
232         main.connectQueue_fireNext();
233     }
237 main_class.prototype.connectQueue_fireNext = function() {
238     var head = main.connectQueueHead;
239     if (head >= main.connectQueue.length) {
240         return;
241     }
242     var callback = {
243     success: function(){
244              main.connectQueue_fireNext();
245         }
246     }
247     main.connectQueueConnection = main.connect(main.connectQueue[head]['method'],
248             main.connectQueue[head]['urlStub'],
249             callback,
250             main.connectQueue[head]['body']);
251     main.connectQueueHead++;
255 main_class.prototype.update_marker = function(newMarker) {
256     if (this.marker != null) {
257         this.marker.toggle_highlight();
258     }
259     this.marker = newMarker;     
260     this.marker.toggle_highlight();
262     this.connect('post', 'class=course&field=marker', null, 'value='+this.marker.sectionId);
266 main_class.prototype.getString = function(identifier, variable) {
267     if (this.portal.strings[identifier]) {
268         return this.portal.strings[identifier].replace(/_var_/, variable);
269     }
272 main_class.prototype.is_ie = function() {
273     var agent = navigator.userAgent.toLowerCase();
274     if ((agent.indexOf('msie') != -1)) {
275         return true;
276     }
277     return false;
281 var main = new main_class();
284 function php_portal_class() {
285     //portal to php data
286     this.id = null;
287     this.debug = null;
289     //array of id's of blocks set at end of page load by php    
290     this.blocks = new Array();
291     this.imagePath = null;
293     //flag for week fomat
294     this.isWeek = false;
296     //strings    
297     this.strings = [];
299     YAHOO.log("Instantiated php_portal_class", "info");