MDL-29248 backup: take rid of the setting and code handling the 'private' user filearea
[moodle.git] / lib / ajax / ajaxcourse.js
blob3d6e2089d306829e807add5620c22dbac40d185f
1 /**
2  * Contains Main class and supporting functions for ajax course layout
3  */
6 //hide content body until done loading (manipulation looks ugly elsewise)
7 //document.getElementById('content').style.display = 'none';
9 // If firebug console is undefined, define a fake one here
10 if (window.console) {
11     console.vardump = function(data) {
12         retval = '';
13         for (key in data) {
14             retval += key+' = '+data[key] + "\n";
15         }
16         console.log(retval);
17     };
20 //onload object for handling scripts on page load, this insures they run in my order
21 function onload_class() {
22     this.scripts = new Array();
23     this.debug = true;
27 onload_class.prototype.add = function(script) {
28     if (this.debug) {
29         YAHOO.log("onloadobj.add - adding "+script, "junk");
30     }
31     this.scripts[this.scripts.length] = script;
35 onload_class.prototype.load = function() {
36     var scriptcount = this.scripts.length;
37     if (this.debug) {
38         YAHOO.log("onloadobj.load - loading "+scriptcount+" scripts", "info");
39     }
40     for (i=0; i<scriptcount; i++) {
41         eval(this.scripts[i]);
42     }
46 var onloadobj = new onload_class();
49 //main page object
50 function main_class() {
51     this.debug = true;
52     this.portal = new php_portal_class();
54     this.blocks = new Array();
55     this.sections = new Array();
56     this.sectiondates = {};
57     this.leftcolumn = null;
58     this.rightcolumn = null;
59     this.adminBlock = null;
60     this.tempBlock = null;
61     this.icons = [];
62     this.courseformat = null;
63     this.marker = null;
64     this.numsections = null;
65     this.lastsection = null; // real last section num including unavailable
67     //things to process onload
68     onloadobj.add('main.process_document();');
69     onloadobj.add("if (document.getElementById('content')) document.getElementById('content').style.display='block';");
71     //connection queue allows xhttp requests to be sent in order
72     this.connectQueue = [];
73     this.connectQueueHead = 0;
74     this.connectQueueConnection = null;
78 main_class.prototype.process_blocks = function() {
79     //remove unneeded icons (old school position icons and delete/hide
80     //although they will be read)
81     var rmIconClasses = ['icon up', 'icon down', 'icon right', 'icon left', 'icon delete', 'icon hide'];
82     for (var c=0; c<rmIconClasses.length; c++) {
83         els = YAHOO.util.Dom.getElementsByClassName(rmIconClasses[c]);
85         for (var x=0; x<els.length; x++) {
86             els[x].parentNode.removeChild(els[x]);
87         }
88     }
89     //process the block ids passed from php
90     var blockcount = this.portal.blocks.length;
91     YAHOO.log("main.processBlocks - processing "+blockcount+" blocks", "info");
93     for (i=0; i<blockcount; i++) {
94         this.blocks[i] = new block_class(this.portal.blocks[i][1], "blocks");
96         //put in correct side array also
97         if (this.portal.blocks[i][0] == 'l') {
98             main.leftcolumn.add_block(this.blocks[i]);
99         } else if (this.portal.blocks[i][0] == 'r') {
100             main.rightcolumn.add_block(this.blocks[i]);
101         }
103         //hide if called for
104         if (this.portal.blocks[i][2] == 1) {
105             this.blocks[i].toggle_hide(null, null, true);
106         }
107     }
111 main_class.prototype.process_document = function() {
112     //process the document to get important containers
113     YAHOO.log("Processing Document", "info");
115     //process columns for blocks
116     this.leftcolumn = new column_class('left-column', "blocks", null, 'l');
117     this.rightcolumn = new column_class('right-column', "blocks", null, 'r');
119     //process sections
120     //var ct = 0;
121     //while (document.getElementById('section-'+ct) != null) {
122     this.courseformat = this.portal.courseformat;
123     for(var ct=0; ct <= this.portal.lastsection; ct++){
124         if (document.getElementById('section-'+ct) != null) {
125             var dragable = ((ct > 0) && (ct <= this.portal.numsections));
126             this.sections[ct] = new section_class('section-'+ct, "sections", null, dragable);
127             this.sections[ct].addToGroup('resources');
128             if (ct > 0) {
129                 var sectiontitle = YAHOO.util.Selector.query('#section-'+ct+' h3.weekdates')[0];
130                 if (undefined !== sectiontitle) { // Only save date for weekly format
131                     this.sectiondates[ct] = sectiontitle.innerHTML;
132                 }
133             }
134         } else {
135             this.sections[ct] = null;
136         }
137         //ct++;
138     }
139     if (this.debug) {
140         YAHOO.log("Processed "+ct+" sections");
141     }
143     this.adminBlock = YAHOO.util.Dom.getElementsByClassName('block_adminblock')[0];
144     this.tempBlock = YAHOO.util.Dom.getElementsByClassName('tempblockhandler')[0];
148 main_class.prototype.mk_safe_for_transport = function(input) {
149     return input.replace(/&/i, '_.amp._');
153 //return block by id
154 main_class.prototype.get_block_index = function(el) {
155     var blockcount = this.blocks.length;
156     for (i=0; i<blockcount; i++) {
157         if (this.blocks[i] == el) {
158             return i;
159         }
160     }
164 main_class.prototype.get_section_index = function(el) {
165     var sectioncount = this.sections.length;
166     for (i=0; i<sectioncount; i++) {
167         if (this.sections[i] == el) {
168             return i;
169         }
170     }
171     return -1;
174 main_class.prototype.mk_button = function(tag, imgSrc, text, attributes, imgAttributes) {
175     //Create button and return object.
176     //Set the text: the container TITLE or image ALT attributes can be overridden, eg.
177     //  main.mk_button('a', main.portal.icons['move_2d'], strmove, [['title', strmoveshort]]);
178     var container = document.createElement(tag);
179     container.style.cursor = 'pointer';
180     container.setAttribute('title', text);
181     var image = document.createElement('img');
183     image.setAttribute('src', imgSrc);
184     image.setAttribute('alt', text);
185     //image.setAttribute('title', '');
186     container.appendChild(image);
188     if (attributes != null) {
189         for (var c=0; c<attributes.length; c++) {
190             if (attributes[c][0] == 'title' && this.is_ie()) {
191                 image.setAttribute(attributes[c][0], attributes[c][1]); //IE hack: transfer 'title'.
192             } else {
193                 container.setAttribute(attributes[c][0], attributes[c][1]);
194             }
195         }
196     }
197     if (imgAttributes != null) {
198         for (var c=0; c<imgAttributes.length; c++) {
199             image.setAttribute(imgAttributes[c][0], imgAttributes[c][1]);
200         }
201     }
202     image.setAttribute('hspace', '3');
203     return container;
207 main_class.prototype.connect = function(method, urlStub, callback, body) {
208     if (this.debug) {
209         YAHOO.log("Making "+method+" connection to /course/rest.php?courseId="+main.portal.id+"&"+urlStub);
210     }
211     if (callback == null) {
212         if (this.debug) {
213             callback = {
214                 success: function(response) {
215                     YAHOO.log("Response from the Request: " + response.statusText + ": " + response.responseText, 'info');
216                 },
217                 failure: function() {
218                     YAHOO.log("Response from the Request: " + response.statusText + ": " + response.responseText, 'error');
219                 }
220             };
221         } else {
222             callback = {};
223         }
224     }
225     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);
229 main_class.prototype.connectQueue_add = function(method, urlStub, callback, body) {
230     var Qlength = main.connectQueue.length;
231     main.connectQueue[Qlength] = [];
232     main.connectQueue[Qlength]['method'] = method;
233     main.connectQueue[Qlength]['urlStub'] = urlStub;
234     main.connectQueue[Qlength]['body'] = body;
236     if (main.connectQueueConnection == null || !YAHOO.util.Connect.isCallInProgress(main.connectQueueConnection)) {
237         main.connectQueue_fireNext();
238     }
242 main_class.prototype.connectQueue_fireNext = function() {
243     var head = main.connectQueueHead;
244     if (head >= main.connectQueue.length) {
245         return;
246     }
247     var callback = {
248     success: function(){
249              main.connectQueue_fireNext();
250         }
251     };
252     main.connectQueueConnection = main.connect(main.connectQueue[head]['method'],
253             main.connectQueue[head]['urlStub'],
254             callback,
255             main.connectQueue[head]['body']);
256     main.connectQueueHead++;
260 main_class.prototype.update_marker = function(newMarker) {
261     if (this.marker != null) {
262         this.marker.toggle_highlight();
263     }
264     this.marker = newMarker;
265     this.marker.toggle_highlight();
267     this.connect('post', 'class=course&field=marker', null, 'value='+this.marker.sectionId);
271 main_class.prototype.getString = function(identifier, variable) {
272     if (this.portal.strings[identifier]) {
273         return this.portal.strings[identifier].replace(/_var_/, variable);
274     }
277 main_class.prototype.hasString = function(identifier) {
278     if (this.portal.strings[identifier]) {
279         return true;
280     }
281     return false;
284 main_class.prototype.is_ie = function() {
285     var agent = navigator.userAgent.toLowerCase();
286     if ((agent.indexOf('msie') != -1)) {
287         return true;
288     }
289     return false;
293 var main = new main_class();
296 function php_portal_class() {
297     //portal to php data
298     this.id = null;
299     this.debug = null;
301     //array of id's of blocks set at end of page load by php
302     this.blocks = new Array();
303     this.imagePath = null;
305     //flag for week fomat
306     this.isWeek = false;
308     //strings
309     this.strings = [];
311     //icons
312     this.icons = [];
314     YAHOO.log("Instantiated php_portal_class", "info");