function restore_create_sections(&$restore, $xml_file) { global $CFG, $db; $status = true; //Check it exists if (!file_exists($xml_file)) { $status = false; } //Get info from xml if ($status) { $info = restore_read_xml_sections($xml_file); } //Put the info in the DB, recoding ids and saving the in backup tables $sequence = ""; if ($info) { //first lets get the full guid list and then remove items when needed. $backup_guids = get_records('restore_guids', 'courseid', $restore->course_id, '', 'guid'); //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->summary = backup_todb($sect->summary); $section->visible = $sect->visible; $section->sequence = ""; //Now calculate the section's newid $newid = 0; if ($restore->restoreto == 2) { //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 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; } if ($newid) { //save old and new section id backup_putid($restore->backup_unique_code, "course_sections", $key, $newid); } else { $status = false; } //If all is OK, go with associated mods if ($status) { //If we have mods in the section if (!empty($sect->mods)) { //For each mod inside section foreach ($sect->mods as $keym => $mod) { // Yu: This part is called repeatedly for every instance, // so it is necessary to set the granular flag and check isset() // when the first instance of this type of mod is processed. //if (!isset($restore->mods[$mod->type]->granular) && isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) { if (!isset($restore->mods[$mod->type]->granular)) { if (isset($restore->mods[$mod->type]->instances) && is_array($restore->mods[$mod->type]->instances)) { // This defines whether we want to restore specific // instances of the modules (granular restore), or // whether we don't care and just want to restore // all module instances (non-granular). $restore->mods[$mod->type]->granular = true; } else { $restore->mods[$mod->type]->granular = false; } } //Check if we've to restore this module (and instance) if (!empty($restore->mods[$mod->type]->restore)) { if (empty($restore->mods[$mod->type]->granular) || array_key_exists($mod->instance, $restore->mods[$mod->type]->instances) && !empty($restore->mods[$mod->type]->instances[$mod->instance]->restore)) { //Get the module id from modules $module = get_record("modules", "name", $mod->type); if ($module) { $course_module = new object(); $course_module->course = $restore->course_id; $course_module->module = $module->id; $course_module->section = $newid; $course_module->added = $mod->added; $course_module->score = $mod->score; $course_module->indent = $mod->indent; $course_module->visible = $mod->visible; $course_module->groupmode = $mod->groupmode; if ($mod->groupingid and $grouping = restore_grouping_getid($restore, $mod->groupingid)) { $course_module->groupingid = $grouping->new_id; } else { $course_module->groupingid = 0; } $course_module->groupmembersonly = $mod->groupmembersonly; $course_module->instance = 0; //NOTE: The instance (new) is calculated and updated in db in the // final step of the restore. We don't know it yet. //print_object($course_module); //Debug //Save it to db if ($mod->idnumber) { if (grade_verify_idnumber($mod->idnumber, $restore->course_id)) { $course_module->idnumber = $mod->idnumber; } } if (isset($mod->guid)) { if (isset($backup_guids[$mod->guid])) { unset($backup_guids[$mod->guid]); } $newidmod = restore_with_guid($restore, 'course_modules', 'id', $course_module, $mod->guid); } else { $newidmod = restore_with_guid($restore, 'course_modules', 'id', $course_module); } if ($newidmod) { //save old and new module id //In the info field, we save the original instance of the module //to use it later backup_putid($restore->backup_unique_code, "course_modules", $keym, $newidmod, $mod->instance); $restore->mods[$mod->type]->instances[$mod->instance]->restored_as_course_module = $newidmod; } else { $status = false; } //Now, calculate the sequence field if ($status) { if ($sequence) { //make sure the $newid doesn't already exist in the sequence. if ($sequence === $newidmod or strpos($sequence, ',' . $newidmod . ',') or strpos($sequence, $newidmod . ',') === 0 or strpos($sequence, ',' . $newidmod) == strlen($sequence) - strlen($newidmod) - 1) { //if both the same length then check like this: //it is already in! - don't do anything } else { $sequence .= "," . $newidmod; } } else { $sequence = $newidmod; } } } else { $status = false; } } } } } } //If all is OK, update sequence field in course_sections if ($status) { if (isset($sequence)) { $update_rec = new object(); $update_rec->id = $newid; $update_rec->sequence = $sequence; $status = update_record("course_sections", $update_rec); } } } //now lets remove any old remaining guids if (!empty($backup_guids)) { foreach ($backup_guids as $guid => $backupguid) { $guidtodelete = get_record('restore_guids', 'guid', $guid, 'courseid', $restore->course_id); //delete record referred to by restore_guids delete_records($guidtodelete->src_table, $guidtodelete->src_field, $guidtodelete->src_value); //now delete the actual guid delete_records('restore_guids', 'id', $guidtodelete->id); } } } else { $status = false; } return $status; }
function forum_posts_restore_mods($new_forum_id, $discussion_id, $info, $restore) { global $CFG; $status = true; //Get the posts array $posts = $info['#']['POSTS']['0']['#']['POST']; //Iterate over posts for ($i = 0; $i < sizeof($posts); $i++) { $pos_info = $posts[$i]; //traverse_xmlize($pos_info); //Debug //print_object ($GLOBALS['traverse_array']); //Debug //$GLOBALS['traverse_array']=""; //Debug //We'll need this later!! $oldid = backup_todb($pos_info['#']['ID']['0']['#']); $olduserid = backup_todb($pos_info['#']['USERID']['0']['#']); //Now, build the FORUM_POSTS record structure $post->discussion = $discussion_id; $post->parent = backup_todb($pos_info['#']['PARENT']['0']['#']); $post->userid = backup_todb($pos_info['#']['USERID']['0']['#']); $post->created = backup_todb($pos_info['#']['CREATED']['0']['#']); $post->created += $restore->course_startdateoffset; $post->modified = backup_todb($pos_info['#']['MODIFIED']['0']['#']); $post->modified += $restore->course_startdateoffset; $post->mailed = backup_todb($pos_info['#']['MAILED']['0']['#']); $post->subject = backup_todb($pos_info['#']['SUBJECT']['0']['#']); $post->message = backup_todb($pos_info['#']['MESSAGE']['0']['#']); $post->format = backup_todb($pos_info['#']['FORMAT']['0']['#']); $post->attachment = backup_todb($pos_info['#']['ATTACHMENT']['0']['#']); $post->totalscore = backup_todb($pos_info['#']['TOTALSCORE']['0']['#']); $post->mailnow = backup_todb($pos_info['#']['MAILNOW']['0']['#']); //We have to recode the userid field $user = backup_getid($restore->backup_unique_code, "user", $post->userid); if ($user) { $post->userid = $user->new_id; } //The structure is equal to the db, so insert the forum_posts $guid = backup_todb($pos_info['#']['GUID']['0']['#']); $newid = restore_with_guid($restore, 'forum_posts', 'id', $post, $guid); //Do some output if (($i + 1) % 50 == 0) { if (!defined('RESTORE_SILENTLY')) { echo "."; if (($i + 1) % 1000 == 0) { echo "<br />"; } } backup_flush(300); } if ($newid) { //We have the newid, update backup_ids backup_putid($restore->backup_unique_code, "forum_posts", $oldid, $newid); //Get old forum id from backup_ids $rec = get_record("backup_ids", "backup_code", $restore->backup_unique_code, "table_name", "forum", "new_id", $new_forum_id); //Now copy moddata associated files $status = forum_restore_files($rec->old_id, $new_forum_id, $oldid, $newid, $restore); //Now restore post ratings $status = forum_ratings_restore_mods($newid, $pos_info, $restore); } else { $status = false; } } //Now we get every post in this discussion_id and recalculate its parent post $posts = get_records("forum_posts", "discussion", $discussion_id); if ($posts) { //Iterate over each post foreach ($posts as $post) { //Get its parent $old_parent = $post->parent; //Get its new post_id from backup_ids table $rec = backup_getid($restore->backup_unique_code, "forum_posts", $old_parent); if ($rec) { //Put its new parent $post->parent = $rec->new_id; } else { $post->parent = 0; } //Create temp post record $temp_post->id = $post->id; $temp_post->parent = $post->parent; //echo "Updated parent ".$old_parent." to ".$temp_post->parent."<br />"; //Debug //Update post (only parent will be changed) $status = update_record("forum_posts", $temp_post); } } return $status; }