diff --git a/backup/restorelib.php b/backup/restorelib.php
index 8443f57..5714f15 100644
--- a/backup/restorelib.php
+++ b/backup/restorelib.php
@@ -232,20 +232,20 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
         }
 
         // For each block, call its encode_content_links method
-        static $blockobjects = null; 
-        if (!isset($blockobjects)) { 
-            $blockobjects = array(); 
-            if ($blocks = get_records('block', 'visible', 1)) { 
-                foreach ($blocks as $block) { 
+        static $blockobjects = null;
+        if (!isset($blockobjects)) {
+            $blockobjects = array();
+            if ($blocks = get_records('block', 'visible', 1)) {
+                foreach ($blocks as $block) {
                     if ($blockobject = block_instance($block->name)) {
-                        $blockobjects[] = $blockobject; 
+                        $blockobjects[] = $blockobject;
                     }
                 }
             }
         }
-        
-        foreach ($blockobjects as $blockobject) { 
-            $content = $blockobject->decode_content_links($content,$restore); 
+
+        foreach ($blockobjects as $blockobject) {
+            $content = $blockobject->decode_content_links($content,$restore);
         }
 
         return $content;
@@ -1345,7 +1345,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
 
                     // This will only be set if we come from 1.7 and above backups
                     //  Also, must do this before insert (insert_record unsets id)
-                    if (!empty($instance->id)) { 
+                    if (!empty($instance->id)) {
                         $oldid = $instance->id;
                     } else {
                         $oldid = 0;
@@ -1417,51 +1417,19 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
         $sequence = "";
 
         if ($info) {
+            $course = get_record("course", "id", $restore->course_id);
+            $course_sections_max = get_field('course_sections', 'MAX(section) as sec', 'course', $course->id);
+            $num_sections = max($course->numsections, $course_sections_max);
             //For each, section, save it to db
             foreach ($info->sections as $key => $sect) {
                 $sequence = "";
                 $section = new object();
                 $section->course = $restore->course_id;
-                $section->section = $sect->number;
+                $section->section = $sect->number + $num_sections + 1;
                 $section->summary = backup_todb($sect->summary);
                 $section->visible = $sect->visible;
                 $section->sequence = "";
-                //Now calculate the section's newid
-                $newid = 0;
-                if ($restore->restoreto == RESTORETO_NEW_COURSE) {
-                    //Save it to db (only if restoring to new course)
-                    $newid = insert_record("course_sections",$section);
-                } else {
-                    //Get section id when restoring in existing course
-                    $rec = get_record("course_sections","course",$restore->course_id,
-                                                        "section",$section->section);
-                    //If section exists, has empty summary and backup has some summary, use it. MDL-8848
-                    if ($rec && empty($rec->summary) && !empty($section->summary)) {
-                        $rec->summary = $section->summary;
-                        update_record("course_sections", $rec);
-                    }
-                    //If that section doesn't exist, get section 0 (every mod will be
-                    //asigned there
-                    if(!$rec) {
-                        $rec = get_record("course_sections","course",$restore->course_id,
-                                                            "section","0");
-                    }
-                    //New check. If section 0 doesn't exist, insert it here !!
-                    //Teorically this never should happen but, in practice, some users
-                    //have reported this issue.
-                    if(!$rec) {
-                        $zero_sec = new object();
-                        $zero_sec->course = $restore->course_id;
-                        $zero_sec->section = 0;
-                        $zero_sec->summary = "";
-                        $zero_sec->sequence = "";
-                        $newid = insert_record("course_sections",$zero_sec);
-                        $rec->id = $newid;
-                        $rec->sequence = "";
-                    }
-                    $newid = $rec->id;
-                    $sequence = $rec->sequence;
-                }
+                $newid = insert_record("course_sections",$section);
                 if ($newid) {
                     //save old and new section id
                     backup_putid ($restore->backup_unique_code,"course_sections",$key,$newid);
@@ -1555,6 +1523,21 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                         }
                     }
                 }
+                //New check. If section 0 doesn't exist, insert it here !!
+                //Teorically this never should happen but, in practice, some users
+                //have reported this issue.
+                $zero = get_record("course_sections","course",$restore->course_id, "section","0");
+                if(!$zero) {
+                    $zero_sec = new object();
+                    $zero_sec->course = $restore->course_id;
+                    $zero_sec->section = 0;
+                    $zero_sec->summary = "";
+                    $zero_sec->sequence = "";
+                    $newid = insert_record("course_sections",$zero_sec);
+                    $rec->id = $newid;
+                    $rec->sequence = "";
+                }
+
                 //If all is OK, update sequence field in course_sections
                 if ($status) {
                     if (isset($sequence)) {
@@ -1568,7 +1551,8 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
         } else {
             $status = false;
         }
-        return $status;
+        $course->numsections = sizeof($info->sections) + $num_sections;
+        return $status && update_record('course', $course);
     }
 
     //Called to set up any course-format specific data that may be in the file
@@ -2243,7 +2227,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                                 $oldoutcome = backup_todb($info['GRADE_ITEM']['#']['OUTCOMEID']['0']['#']);
                                 if (empty($outcomes[$oldoutcome])) {
                                     continue; // error!
-                                } 
+                                }
                                 if (empty($outcomes[$oldoutcome]->id)) {
                                     $outcomes[$oldoutcome]->insert('restore');
                                     $outcomes[$oldoutcome]->use_in($restore->course_id);
@@ -2346,7 +2330,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
 
 
         if ($status and !$importing and $restore_histories) {
-            /// following code is very inefficient 
+            /// following code is very inefficient
 
             $gchcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_categories_history');
             $gghcount = count_records ('backup_ids', 'backup_code', $restore->backup_unique_code, 'table_name', 'grade_grades_history');
@@ -2379,7 +2363,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                                 //traverse_xmlize($info);                            //Debug
                                 //print_object ($GLOBALS['traverse_array']);         //Debug
                                 //$GLOBALS['traverse_array']="";                     //Debug
-    
+
                                 $oldobj = backup_getid($restore->backup_unique_code,"grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['OLDID']['0']['#']));
                                 if (empty($oldobj->new_id)) {
                                     // if the old object is not being restored, can't restoring its history
@@ -2390,12 +2374,12 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                                 $dbrec->action = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['ACTION']['0']['#']);
                                 $dbrec->source = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['SOURCE']['0']['#']);
                                 $dbrec->timemodified = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['TIMEMODIFIED']['0']['#']);
-    
+
                                 // loggeduser might not be restored, e.g. admin
                                 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
                                     $dbrec->loggeduser = $oldobj->new_id;
                                 }
-    
+
                                 // this item might not have a parent at all, do not skip it if no parent is specified
                                 if (backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#'])) {
                                     $oldobj = backup_getid($restore->backup_unique_code,"grade_categories", backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['PARENT']['0']['#']));
@@ -2427,15 +2411,15 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                                 $dbrec->aggregation = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGRETGATION']['0']['#']);
                                 $dbrec->keephigh = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['KEEPHIGH']['0']['#']);
                                 $dbrec->droplow = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['DROPLOW']['0']['#']);
-                                
+
                                 $dbrec->aggregateonlygraded = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATEONLYGRADED']['0']['#']);
                                 $dbrec->aggregateoutcomes = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATEOUTCOMES']['0']['#']);
                                 $dbrec->aggregatesubcats = backup_todb($info['GRADE_CATEGORIES_HISTORY']['#']['AGGREGATESUBCATS']['0']['#']);
-    
+
                                 $dbrec->courseid = $restore->course_id;
                                 insert_record('grade_categories_history', $dbrec);
                                 unset($dbrec);
-    
+
                             }
                             //Increment counters
                             $counter++;
@@ -2453,7 +2437,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                     }
                 }
             }
-    
+
             // process histories
             if ($gghcount && $status) {
                 if (!defined('RESTORE_SILENTLY')) {
@@ -2477,7 +2461,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                                 //traverse_xmlize($info);                            //Debug
                                 //print_object ($GLOBALS['traverse_array']);         //Debug
                                 //$GLOBALS['traverse_array']="";                     //Debug
-    
+
                                 $oldobj = backup_getid($restore->backup_unique_code,"grade_grades", backup_todb($info['GRADE_GRADES_HISTORY']['#']['OLDID']['0']['#']));
                                 if (empty($oldobj->new_id)) {
                                     // if the old object is not being restored, can't restoring its history
@@ -2491,7 +2475,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                                 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOGGEDUSER']['0']['#']))) {
                                     $dbrec->loggeduser = $oldobj->new_id;
                                 }
-                                
+
                                 $oldobj = backup_getid($restore->backup_unique_code,"grade_items", backup_todb($info['GRADE_GRADES_HISTORY']['#']['ITEMID']['0']['#']));
                                 $dbrec->itemid = $oldobj->new_id;
                                 if (empty($dbrec->itemid)) {
@@ -2506,12 +2490,12 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                                 if ($oldobj = backup_getid($restore->backup_unique_code,"user", backup_todb($info['GRADE_GRADES_HISTORY']['#']['USERMODIFIED']['0']['#']))) {
                                     $dbrec->usermodified = $oldobj->new_id;
                                 }
-                                
+
                                 if (backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWSCALEID']['0']['#'])) {
                                     $scale = backup_getid($restore->backup_unique_code,"scale",backup_todb($info['GRADE_GRADES_HISTORY']['#']['RAWSCALEID']['0']['#']));
                                     $dbrec->rawscaleid = $scale->new_id;
                                 }
-                                
+
                                 $dbrec->finalgrade = backup_todb($info['GRADE_GRADES_HISTORY']['#']['FINALGRADE']['0']['#']);
                                 $dbrec->hidden = backup_todb($info['GRADE_GRADES_HISTORY']['#']['HIDDEN']['0']['#']);
                                 $dbrec->locked = backup_todb($info['GRADE_GRADES_HISTORY']['#']['LOCKED']['0']['#']);
@@ -2523,10 +2507,10 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                                 $dbrec->feedbackformat = backup_todb($info['GRADE_TEXT_HISTORY']['#']['FEEDBACKFORMAT']['0']['#']);
                                 $dbrec->information = backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATION']['0']['#']);
                                 $dbrec->informationformat = backup_todb($info['GRADE_TEXT_HISTORY']['#']['INFORMATIONFORMAT']['0']['#']);
-    
+
                                 insert_record('grade_grades_history', $dbrec);
                                 unset($dbrec);
-    
+
                             }
                             //Increment counters
                             $counter++;
@@ -2544,9 +2528,9 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                     }
                 }
             }
-    
+
             // process histories
-    
+
             if ($gihcount && $status) {
                 if (!defined('RESTORE_SILENTLY')) {
                     echo '<li>'.get_string('gradeitemshistory','grades').'</li>';
@@ -2569,8 +2553,8 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                                 //traverse_xmlize($info);                            //Debug
                                 //print_object ($GLOBALS['traverse_array']);         //Debug
                                 //$GLOBALS['traverse_array']="";                     //Debug
-    
-    
+
+
                                 $oldobj = backup_getid($restore->backup_unique_code,"grade_items", backup_todb($info['GRADE_ITEM_HISTORY']['#']['OLDID']['0']['#']));
                                 if (empty($oldobj->new_id)) {
                                     // if the old object is not being restored, can't restoring its history
@@ -2591,31 +2575,31 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                                     $counter++;
                                     continue; // category not restored
                                 }
-    
+
                                 $dbrec->itemname= backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNAME']['0']['#']);
                                 $dbrec->itemtype = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMTYPE']['0']['#']);
                                 $dbrec->itemmodule = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMMODULE']['0']['#']);
-    
+
                                 // code from grade_items restore
                                 $iteminstance = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINSTANCE']['0']['#']);
                                 // do not restore if this grade_item is a mod, and
                                 if ($dbrec->itemtype == 'mod') {
-    
+
                                     if (!restore_userdata_selected($restore,  $dbrec->itemmodule, $iteminstance)) {
                                         // module instance not selected when restored using granular
                                         // skip this item
                                         $counter++;
                                         continue;
                                     }
-    
+
                                     // iteminstance should point to new mod
-    
+
                                     $mod = backup_getid($restore->backup_unique_code,$dbrec->itemmodule, $iteminstance);
                                     $dbrec->iteminstance = $mod->new_id;
-    
+
                                 } else if ($dbrec->itemtype == 'category') {
                                     // the item instance should point to the new grade category
-    
+
                                     // only proceed if we are restoring all grade items
                                     if ($restoreall) {
                                         $category = backup_getid($restore->backup_unique_code,'grade_categories', $iteminstance);
@@ -2630,16 +2614,16 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                                         // TODO any special code needed here to restore course item without duplicating it?
                                         // find the course category with depth 1, and course id = current course id
                                         // this would have been already restored
-    
+
                                         $cat = get_record('grade_categories', 'depth', 1, 'courseid', $restore->course_id);
                                         $dbrec->iteminstance = $cat->id;
-    
+
                                     } else {
                                         $counter++;
                                         continue;
                                     }
                                 }
-    
+
                                 $dbrec->itemnumber = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMNUMBER']['0']['#']);
                                 $dbrec->iteminfo = backup_todb($info['GRADE_ITEM_HISTORY']['#']['ITEMINFO']['0']['#']);
                                 $dbrec->idnumber = backup_todb($info['GRADE_ITEM_HISTORY']['#']['IDNUMBER']['0']['#']);
@@ -2666,10 +2650,10 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                                 $dbrec->locked = backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKED']['0']['#']);
                                 $dbrec->locktime = backup_todb($info['GRADE_ITEM_HISTORY']['#']['LOCKTIME']['0']['#']);
                                 $dbrec->needsupdate = backup_todb($info['GRADE_ITEM_HISTORY']['#']['NEEDSUPDATE']['0']['#']);
-    
+
                                 insert_record('grade_items_history', $dbrec);
                                 unset($dbrec);
-    
+
                             }
                             //Increment counters
                             $counter++;
@@ -2687,7 +2671,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                     }
                 }
             }
-    
+
             // process histories
             if ($gohcount && $status) {
                 if (!defined('RESTORE_SILENTLY')) {
@@ -2711,7 +2695,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                                 //traverse_xmlize($info);                            //Debug
                                 //print_object ($GLOBALS['traverse_array']);         //Debug
                                 //$GLOBALS['traverse_array']="";                     //Debug
-    
+
                                 $oldobj = backup_getid($restore->backup_unique_code,"grade_outcomes", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['OLDID']['0']['#']));
                                 if (empty($oldobj->new_id)) {
                                     // if the old object is not being restored, can't restoring its history
@@ -2731,10 +2715,10 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                                 $oldobj = backup_getid($restore->backup_unique_code,"scale", backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['SCALEID']['0']['#']));
                                 $dbrec->scaleid = $oldobj->new_id;
                                 $dbrec->description = backup_todb($info['GRADE_OUTCOME_HISTORY']['#']['DESCRIPTION']['0']['#']);
-    
+
                                 insert_record('grade_outcomes_history', $dbrec);
                                 unset($dbrec);
-    
+
                             }
                             //Increment counters
                             $counter++;
@@ -3651,7 +3635,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
     function restore_group_getid($restore, $groupid) {
         //We have to recode the groupid field
         $group = backup_getid($restore->backup_unique_code, 'groups', $groupid);
-        
+
         if ($restore->groups == RESTORE_GROUPS_NONE or $restore->groups == RESTORE_GROUPINGS_ONLY) {
             $group->new_id = 0;
         }
@@ -3665,7 +3649,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
     function restore_grouping_getid($restore, $groupingid) {
         //We have to recode the groupid field
         $grouping = backup_getid($restore->backup_unique_code, 'groupings', $groupingid);
-        
+
         if ($restore->groups != RESTORE_GROUPS_GROUPINGS and $restore->groups != RESTORE_GROUPINGS_ONLY) {
             $grouping->new_id = 0;
         }
@@ -3798,7 +3782,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
             //We have to recode the userid field
             if (!$user = backup_getid($restore->backup_unique_code,"user",$group_member->userid)) {
                 debugging("group membership can not be restored, user id $group_member->userid not present in backup");
-                // do not not block the restore 
+                // do not not block the restore
                 continue;
             }
 
@@ -3881,7 +3865,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
         // now fix the defaultgroupingid in course
         $course = get_record('course', 'id', $restore->course_id);
         if ($course->defaultgroupingid) {
-            if ($grouping = restore_grouping_getid($restore, $course->defaultgroupingid)) { 
+            if ($grouping = restore_grouping_getid($restore, $course->defaultgroupingid)) {
                 set_field('course', 'defaultgroupingid', $grouping->new_id, 'id', $course->id);
             } else {
                 set_field('course', 'defaultgroupingid', 0, 'id', $course->id);
@@ -4133,7 +4117,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
 
         $counter = 0;
 
-        // 'users' is the old users folder, 'user' is the new one, with a new hierarchy. Detect which one is here and treat accordingly 
+        // 'users' is the old users folder, 'user' is the new one, with a new hierarchy. Detect which one is here and treat accordingly
         //in CFG->dataroot
         $dest_dir = $CFG->dataroot."/user";
         $status = check_dir_exists($dest_dir,true);
@@ -4141,7 +4125,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
         //Now, we iterate over "user_files" records to check if that user dir must be
         //copied (and renamed) to the "users" dir.
         $rootdir = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code."/user_files";
-        
+
         //Check if directory exists
         $userlist = array();
 
@@ -4158,7 +4142,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                 }
             }
 
-            foreach ($userlist as $olduserid => $backup_location) { 
+            foreach ($userlist as $olduserid => $backup_location) {
                 //Look for dir like username in backup_ids
                 //If that user exists in backup_ids
                 if ($user = backup_getid($restore->backup_unique_code,"user",$olduserid)) {
@@ -4839,7 +4823,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                                            FROM {$CFG->prefix}course_modules
                                            WHERE id = '$cm_module->new_id' AND
                                                  instance = '0'");
-                                                 
+
                     if($course_modules_inst_zero){ // Clean up the invalid instances
                          foreach($course_modules_inst_zero as $course_modules_inst){
                              delete_records('course_modules', 'id',$course_modules_inst->id);
@@ -5983,7 +5967,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                 if (!isset($this->temp)) {
                     $this->temp = "";
                 }
-                $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";    
+                $this->temp .= htmlspecialchars(trim($this->content))."</".$tagName.">";
 
                 //Dependig of different combinations, do different things
                 if ($this->level == 4) {
@@ -5998,7 +5982,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                             $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
                             //Call to xmlize for this portion of xml data (one BLOCK)
                             //echo "-XMLIZE: ".strftime ("%X",time()),"-";                                                //Debug
-                            $data = xmlize($xml_data,0);         
+                            $data = xmlize($xml_data,0);
                             //echo strftime ("%X",time())."<p>";                                                          //Debug
                             //traverse_xmlize($data);                                                                     //Debug
                             //print_object ($GLOBALS['traverse_array']);                                                  //Debug
@@ -6008,7 +5992,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                                 //Get old id
                                 $oldid = $data['BLOCK']['#']['ID']['0']['#'];
                                 //Get instancedata
-                                
+
                                 if ($data = $data['BLOCK']['#']['INSTANCEDATA']['0']['#']) {
                                     //Restore code calls this multiple times - so might already have the newid
                                     if ($newid = backup_getid($this->preferences->backup_unique_code,'block_instance',$oldid)) {
@@ -6517,7 +6501,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                     //Reset temp
                     unset($this->temp);
                 }
-                
+
                 if (($this->level == 5) and ($tagName == "GRADE_LETTER")) {
                     //Prepend XML standard header to info gathered
                     $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".$this->temp;
@@ -6696,7 +6680,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                     $preference_id = $data["GRADE_PREFERENCE"]["#"]["ID"]["0"]["#"];
                     $this->counter++;
                     //Save to db
-                    $status = backup_putid($this->preferences->backup_unique_code, 'grade_preferences', $preference_id, 
+                    $status = backup_putid($this->preferences->backup_unique_code, 'grade_preferences', $preference_id,
                                            null,$data);
                     //Create returning info
                     $this->info = $this->counter;
@@ -6719,7 +6703,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                     $letter_id = $data["GRADE_LETTER"]["#"]["ID"]["0"]["#"];
                     $this->counter++;
                     //Save to db
-                    $status = backup_putid($this->preferences->backup_unique_code, 'grade_letter' ,$letter_id, 
+                    $status = backup_putid($this->preferences->backup_unique_code, 'grade_letter' ,$letter_id,
                                            null,$data);
                     //Create returning info
                     $this->info = $this->counter;
@@ -7845,12 +7829,12 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
             // MDL-9290 performance improvement on reading large xml
             $lasttime = time(); // crmas
             while ($data = fread($fp, 8192) and !$moodle_parser->finished) {
-             
+
                 if ((time() - $lasttime) > 5) {
                     $lasttime = time();
                     backup_flush(1);
                 }
-             
+
                 xml_parse($xml_parser, $data, feof($fp))
                         or die(sprintf("XML error: %s at line %d",
                         xml_error_string(xml_get_error_code($xml_parser)),
@@ -8313,6 +8297,22 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                     $status = remove_course_contents($restore->course_id,false) and
                         delete_dir_contents($CFG->dataroot."/".$restore->course_id,"backupdata");
                     if ($status) {
+
+                        // Restaura alguns dados que eram ignorados pelo moodle,mas que nos sao uteis
+                        $course->format = addslashes($course_header->course_format);
+                        $course->numsections = $course_header->numsections;
+                        $course->hiddensections = addslashes($course_header->course_hiddensections);
+                        $course->newsitems = addslashes($course_header->course_newsitems);
+                        $course->showgrades = addslashes($course_header->course_showgrades);
+                        $course->showreports = addslashes($course_header->course_showreports);
+                        if (!update_record('course', $course)) {
+                            if (!defined('RESTORE_SILENTLY')) {
+                                notify("An error occurred while updating the course configuration.");
+                            } else {
+                                $errrostr = "An error occurred while updating the course configuration.";
+                                return false;
+                            }
+                        }
                         //Now , this situation is equivalent to the "restore to new course" one (we
                         //have a course record and nothing more), so define it as "to new course"
                         $restore->restoreto = RESTORETO_NEW_COURSE;
