diff --git a/lib/ajax/ajaxcourse.js b/lib/ajax/ajaxcourse.js index b29a25024e0..0e5dd3421da 100644 --- a/lib/ajax/ajaxcourse.js +++ b/lib/ajax/ajaxcourse.js @@ -62,6 +62,7 @@ function main_class() { this.courseformat = null; this.marker = null; this.numsections = null; + this.lastsection = null; // real last section num including unavailable //things to process onload onloadobj.add('main.process_document();'); @@ -118,11 +119,11 @@ main_class.prototype.process_document = function() { //process sections //var ct = 0; //while (document.getElementById('section-'+ct) != null) { - var maxct = this.portal.numsections; this.courseformat = this.portal.courseformat; - for(var ct=0; ct <= maxct; ct++){ - if(document.getElementById('section-'+ct) != null) { - this.sections[ct] = new section_class('section-'+ct, "sections", null, ct!=0?true:false); + for(var ct=0; ct <= this.portal.lastsection; ct++){ + if (document.getElementById('section-'+ct) != null) { + var dragable = ((ct > 0) && (ct <= this.portal.numsections)); + this.sections[ct] = new section_class('section-'+ct, "sections", null, dragable); this.sections[ct].addToGroup('resources'); if (ct > 0) { var sectiontitle = YAHOO.util.Selector.query('#section-'+ct+' h3.weekdates')[0]; @@ -130,6 +131,8 @@ main_class.prototype.process_document = function() { this.sectiondates[ct] = sectiontitle.innerHTML; } } + } else { + this.sections[ct] = null; } //ct++; } @@ -165,6 +168,7 @@ main_class.prototype.get_section_index = function(el) { return i; } } + return -1; }; main_class.prototype.mk_button = function(tag, imgSrc, text, attributes, imgAttributes) { diff --git a/lib/ajax/ajaxlib.php b/lib/ajax/ajaxlib.php index dbbf55435f8..fdfb4927e1f 100644 --- a/lib/ajax/ajaxlib.php +++ b/lib/ajax/ajaxlib.php @@ -118,7 +118,7 @@ class jsportal { * Prints the JavaScript code needed to set up AJAX for the course. */ function print_javascript($courseid, $return=false) { - global $CFG, $USER, $OUTPUT, $COURSE; + global $CFG, $USER, $OUTPUT, $COURSE, $DB; $blocksoutput = $output = ''; for ($i=0; $iblocks); $i++) { @@ -138,6 +138,7 @@ class jsportal { $output .= " main.portal.strings['marker']='".get_string('markthistopic', '', '_var_')."';\n"; $output .= " main.portal.strings['marked']='".get_string('markedthistopic', '', '_var_')."';\n"; $output .= " main.portal.numsections = ".$COURSE->numsections.";\n"; + $output .= " main.portal.lastsection = ".$DB->get_field_sql("SELECT MAX(section) FROM {course_sections} WHERE course = ?", array($courseid)).";\n"; // needed for orphaned activities in unavailable sections $output .= " main.portal.strings['hide']='".get_string('hide')."';\n"; $output .= " main.portal.strings['hidesection']='".get_string('hidesection', '', '_var_')."';\n"; $output .= " main.portal.strings['show']='".get_string('show')."';\n"; diff --git a/lib/ajax/section_classes.js b/lib/ajax/section_classes.js index f2a828eb5e6..ee79949bbb2 100755 --- a/lib/ajax/section_classes.js +++ b/lib/ajax/section_classes.js @@ -77,6 +77,11 @@ section_class.prototype.init_section = function(id, group, config, isDraggable) section_class.prototype.init_buttons = function() { + if (this.sectionId > main.portal.numsections) { + // no need to do anything in orphaned sections + return; + } + var commandContainer = YAHOO.util.Dom.getElementsByClassName('right',null,this.getEl())[0]; //clear all but show only button @@ -92,7 +97,7 @@ section_class.prototype.init_buttons = function() { commandContainer.appendChild(highlightbutton); this.highlightButton = highlightbutton; } - if (this.sectionId > 0 ) { + if (this.sectionId > 0) { var viewbutton = main.mk_button('div', main.portal.icons['hide'], main.getString('hidesection', this.sectionId), [['title', main.portal.strings['hide'] ]]); YAHOO.util.Event.addListener(viewbutton, 'click', this.toggle_hide, this,true); @@ -150,7 +155,14 @@ section_class.prototype.process_section = function() { var resource = this.resources_ul.getElementsByTagName('li')[i]; this.resources[this.resources.length] = new resource_class(resource.id, 'resources', null, this); } - this.summary = YAHOO.util.Dom.getElementsByClassName('summary', null, this.getEl())[0].firstChild.data || ''; + + var sum = YAHOO.util.Dom.getElementsByClassName('summary', null, this.getEl()); + if (sum[0]) { + this.summary = sum[0].firstChild.data || ''; + } else { + // orphaned activities + this.summary = null; + } }; @@ -205,6 +217,14 @@ section_class.prototype.move_to_section = function(target) { var oIndex = main.get_section_index(this); var tIndex = main.get_section_index(target); + if (oIndex == -1) { + // source must exist + return; + } + if (tIndex == -1) { + // target must exist + return; + } if (this.debug) { YAHOO.log("original is at: "+oIndex+" target is at:"+tIndex+" of "+(sectionCount-1)); } @@ -257,8 +277,17 @@ section_class.prototype.move_to_section = function(target) { section_class.prototype.swap_with_section = function(sectionIn) { var tmpStore = null; - thisIndex = main.get_section_index(this); - targetIndex = main.get_section_index(sectionIn); + var thisIndex = main.get_section_index(this); + var targetIndex = main.get_section_index(sectionIn); + if (thisIndex == -1) { + // source must exist + return; + } + if (targetIndex == -1) { + // target must exist + return; + } + main.sections[targetIndex] = this; main.sections[thisIndex] = sectionIn; @@ -286,6 +315,11 @@ section_class.prototype.swap_with_section = function(sectionIn) { section_class.prototype.toggle_hide = function(e,target,superficial) { + if (this.sectionId > main.portal.numsections) { + // no need to do anything in orphaned sections + return; + } + var strhide = main.portal.strings['hide']; var strshow = main.portal.strings['show']; if (this.hidden) {