MDL-22164 backup - more cleanup of old 1.9 backup/restore code

This commit is contained in:
Eloy Lafuente 2010-11-01 15:47:07 +00:00
parent 152f2c0777
commit b5e58c1831
3 changed files with 0 additions and 4111 deletions

View file

@ -414,34 +414,6 @@
return $status;
}
//This function creates all the categories and questions
//from xml
function restore_create_questions($restore,$xml_file) {
global $CFG;
$status = true;
//Check it exists
if (!file_exists($xml_file)) {
$status = false;
}
//Get info from xml
if ($status) {
//info will contain the old_id of every category
//in backup_ids->info will be the real info (serialized)
$info = restore_read_xml_questions($restore,$xml_file);
}
//Now, if we have anything in info, we have to restore that
//categories/questions
if ($info) {
if ($info !== true) {
$status = $status && restore_question_categories($info, $restore);
}
} else {
$status = false;
}
return $status;
}
//This function creates all the course events
function restore_create_events($restore,$xml_file) {
global $DB;
@ -960,460 +932,11 @@
return $status;
}
/**
* @param string $errorstr passed by reference, if silent is true,
* errorstr will be populated and this function will return false rather than calling print_error() or notify()
* @param boolean $noredirect (optional) if this is passed, this function will not print continue, or
* redirect to the next step in the restore process, instead will return $backup_unique_code
*/
function restore_precheck($id,$file,&$errorstr,$noredirect=false) {
global $CFG, $SESSION, $OUTPUT;
//Prepend dataroot to variable to have the absolute path
$file = $CFG->dataroot."/".$file;
if (!defined('RESTORE_SILENTLY')) {
//Start the main table
echo "<table cellpadding=\"5\">";
echo "<tr><td>";
//Start the mail ul
echo "<ul>";
}
//Check the file exists
if (!is_file($file)) {
if (!defined('RESTORE_SILENTLY')) {
print_error('nofile');
} else {
$errorstr = "File not exists ($file)";
return false;
}
}
//Check the file name ends with .zip
if (!substr($file,-4) == ".zip") {
if (!defined('RESTORE_SILENTLY')) {
print_error('incorrectext');
} else {
$errorstr = get_string('incorrectext', 'error');
return false;
}
}
//Now calculate the unique_code for this restore
$backup_unique_code = time();
//Now check and create the backup dir (if it doesn't exist)
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("creatingtemporarystructures").'</li>';
}
$status = check_and_create_backup_dir($backup_unique_code);
//Empty dir
if ($status) {
$status = clear_backup_dir($backup_unique_code);
}
//Now delete old data and directories under dataroot/temp/backup
if ($status) {
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("deletingolddata").'</li>';
}
if (!backup_delete_old_data()) {;
$errorstr = "An error occurred deleting old data";
add_to_backup_log(time(),$preferences->backup_course,$errorstr,'restoreprecheck');
if (!defined('RESTORE_SILENTLY')) {
echo $OUTPUT->notification($errorstr);
}
}
}
//Now copy he zip file to dataroot/temp/backup/backup_unique_code
if ($status) {
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("copyingzipfile").'</li>';
}
if (! $status = backup_copy_file($file,$CFG->dataroot."/temp/backup/".$backup_unique_code."/".basename($file))) {
if (!defined('RESTORE_SILENTLY')) {
echo $OUTPUT->notification("Error copying backup file. Invalid name or bad perms.");
} else {
$errorstr = "Error copying backup file. Invalid name or bad perms";
return false;
}
}
}
//Now unzip the file
if ($status) {
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("unzippingbackup").'</li>';
}
if (! $status = restore_unzip ($CFG->dataroot."/temp/backup/".$backup_unique_code."/".basename($file))) {
if (!defined('RESTORE_SILENTLY')) {
echo $OUTPUT->notification("Error unzipping backup file. Invalid zip file.");
} else {
$errorstr = "Error unzipping backup file. Invalid zip file.";
return false;
}
}
}
// If experimental option is enabled (enableimsccimport)
// check for Common Cartridge packages and convert to Moodle format
if ($status && isset($CFG->enableimsccimport) && $CFG->enableimsccimport == 1) {
require_once($CFG->dirroot. '/backup/cc/restore_cc.php');
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string('checkingforimscc', 'imscc').'</li>';
}
$status = cc_convert($CFG->dataroot. DIRECTORY_SEPARATOR .'temp'. DIRECTORY_SEPARATOR . 'backup'. DIRECTORY_SEPARATOR . $backup_unique_code);
}
//Check for Blackboard backups and convert
if ($status){
require_once("$CFG->dirroot/backup/bb/restore_bb.php");
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("checkingforbbexport").'</li>';
}
$status = blackboard_convert($CFG->dataroot."/temp/backup/".$backup_unique_code);
}
//Now check for the moodle.xml file
if ($status) {
$xml_file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("checkingbackup").'</li>';
}
if (! $status = restore_check_moodle_file ($xml_file)) {
if (!is_file($xml_file)) {
$errorstr = 'Error checking backup file. moodle.xml not found at root level of zip file.';
} else {
$errorstr = 'Error checking backup file. moodle.xml is incorrect or corrupted.';
}
if (!defined('RESTORE_SILENTLY')) {
echo $OUTPUT->notification($errorstr);
} else {
return false;
}
}
}
$info = "";
$course_header = "";
//Now read the info tag (all)
if ($status) {
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("readinginfofrombackup").'</li>';
}
//Reading info from file
$info = restore_read_xml_info ($xml_file);
//Reading course_header from file
$course_header = restore_read_xml_course_header ($xml_file);
if(!is_object($course_header)){
// ensure we fail if there is no course header
$course_header = false;
}
}
if (!defined('RESTORE_SILENTLY')) {
//End the main ul
echo "</ul>\n";
//End the main table
echo "</td></tr>";
echo "</table>";
}
//We compare Moodle's versions
if ($status && $CFG->version < $info->backup_moodle_version) {
$message = new stdClass();
$message->serverversion = $CFG->version;
$message->serverrelease = $CFG->release;
$message->backupversion = $info->backup_moodle_version;
$message->backuprelease = $info->backup_moodle_release;
echo $OUTPUT->box(get_string('noticenewerbackup','',$message), "noticebox");
}
//Now we print in other table, the backup and the course it contains info
if ($info and $course_header and $status) {
//First, the course info
if (!defined('RESTORE_SILENTLY')) {
$status = restore_print_course_header($course_header);
}
//Now, the backup info
if ($status) {
if (!defined('RESTORE_SILENTLY')) {
$status = restore_print_info($info);
}
}
}
//Save course header and info into php session
if ($status) {
$SESSION->info = $info;
$SESSION->course_header = $course_header;
}
//Finally, a little form to continue
//with some hidden fields
if ($status) {
if (!defined('RESTORE_SILENTLY')) {
echo "<br /><div style='text-align:center'>";
$hidden["backup_unique_code"] = $backup_unique_code;
$hidden["launch"] = "form";
$hidden["file"] = $file;
$hidden["id"] = $id;
echo $OUTPUT->single_button(new moodle_url("restore.php", $hidden), get_string("continue"));
echo "</div>";
}
else {
if (empty($noredirect)) {
echo $OUTPUT->continue_button(new moodle_url('/backup/restore.php', array('backup_unique_code'=>$backup_unique_code, 'launch'=>'form', 'file'=>$file, 'id'=>$id, 'sesskey'=>sesskey())));
echo $OUTPUT->footer();
die;
} else {
return $backup_unique_code;
}
}
}
if (!$status) {
if (!defined('RESTORE_SILENTLY')) {
print_error('error');
} else {
$errorstr = "An error has occured"; // helpful! :P
return false;
}
}
return true;
}
function restore_execute(&$restore,$info,$course_header,&$errorstr) {
global $CFG, $USER, $DB, $OUTPUT;
$status = true;
//If we've selected to restore into new course
//create it (course)
//Saving conversion id variables into backup_tables
if ($restore->restoreto == RESTORETO_NEW_COURSE) {
if (!defined('RESTORE_SILENTLY')) {
echo '<li>'.get_string('creatingnewcourse') . '</li>';
}
$oldidnumber = $course_header->course_idnumber;
if (!$status = restore_create_new_course($restore,$course_header)) {
if (!defined('RESTORE_SILENTLY')) {
echo $OUTPUT->notification("Error while creating the new empty course.");
} else {
$errorstr = "Error while creating the new empty course.";
return false;
}
}
//Print course fullname and shortname and category
if ($status) {
if (!defined('RESTORE_SILENTLY')) {
echo "<ul>";
echo "<li>".$course_header->course_fullname." (".$course_header->course_shortname.")".'</li>';
echo "<li>".get_string("category").": ".$course_header->category->name.'</li>';
if (!empty($oldidnumber)) {
echo "<li>".get_string("nomoreidnumber","moodle",$oldidnumber)."</li>";
}
echo "</ul>";
//Put the destination course_id
}
$restore->course_id = $course_header->course_id;
}
if ($status = restore_open_html($restore,$course_header)){
if (!defined('RESTORE_SILENTLY')) {
echo "<li>Creating the Restorelog.html in the course backup folder</li>";
}
}
} else {
$course = $DB->get_record("course", array("id"=>$restore->course_id));
if ($course) {
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("usingexistingcourse");
echo "<ul>";
echo "<li>".get_string("from").": ".$course_header->course_fullname." (".$course_header->course_shortname.")".'</li>';
echo "<li>".get_string("to").": ". format_string($course->fullname) ." (".format_string($course->shortname).")".'</li>';
if (($restore->deleting)) {
echo "<li>".get_string("deletingexistingcoursedata").'</li>';
} else {
echo "<li>".get_string("addingdatatoexisting").'</li>';
}
echo "</ul></li>";
}
//If we have selected to restore deleting, we do it now.
if ($restore->deleting) {
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("deletingolddata").'</li>';
}
$status = remove_course_contents($restore->course_id,false) and
delete_dir_contents($CFG->dataroot."/".$restore->course_id,"backupdata");
if ($status) {
//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;
} else {
if (!defined('RESTORE_SILENTLY')) {
echo $OUTPUT->notification("An error occurred while deleting some of the course contents.");
} else {
$errrostr = "An error occurred while deleting some of the course contents.";
return false;
}
}
}
} else {
if (!defined('RESTORE_SILENTLY')) {
echo $OUTPUT->notification("Error opening existing course.");
$status = false;
} else {
$errorstr = "Error opening existing course.";
return false;
}
}
}
//Now create groupings as needed
if ($status and ($restore->groups == RESTORE_GROUPINGS_ONLY or $restore->groups == RESTORE_GROUPS_GROUPINGS)) {
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("creatinggroupings");
}
if (!$status = restore_create_groupings($restore,$xml_file)) {
if (!defined('RESTORE_SILENTLY')) {
echo $OUTPUT->notification("Could not restore groupings!");
} else {
$errorstr = "Could not restore groupings!";
return false;
}
}
if (!defined('RESTORE_SILENTLY')) {
echo '</li>';
}
}
//Now create groupingsgroups as needed
if ($status and $restore->groups == RESTORE_GROUPS_GROUPINGS) {
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("creatinggroupingsgroups");
}
if (!$status = restore_create_groupings_groups($restore,$xml_file)) {
if (!defined('RESTORE_SILENTLY')) {
echo $OUTPUT->notification("Could not restore groups in groupings!");
} else {
$errorstr = "Could not restore groups in groupings!";
return false;
}
}
if (!defined('RESTORE_SILENTLY')) {
echo '</li>';
}
}
//Now create the course_sections and their associated course_modules
//we have to do this after groups and groupings are restored, because we need the new groupings id
if ($status) {
//Into new course
if ($restore->restoreto == RESTORETO_NEW_COURSE) {
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("creatingsections");
}
if (!$status = restore_create_sections($restore,$xml_file)) {
if (!defined('RESTORE_SILENTLY')) {
echo $OUTPUT->notification("Error creating sections in the existing course.");
} else {
$errorstr = "Error creating sections in the existing course.";
return false;
}
}
if (!defined('RESTORE_SILENTLY')) {
echo '</li>';
}
//Into existing course
} else if ($restore->restoreto != RESTORETO_NEW_COURSE) {
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("checkingsections");
}
if (!$status = restore_create_sections($restore,$xml_file)) {
if (!defined('RESTORE_SILENTLY')) {
echo $OUTPUT->notification("Error creating sections in the existing course.");
} else {
$errorstr = "Error creating sections in the existing course.";
return false;
}
}
if (!defined('RESTORE_SILENTLY')) {
echo '</li>';
}
//Error
} else {
if (!defined('RESTORE_SILENTLY')) {
echo $OUTPUT->notification("Neither a new course or an existing one was specified.");
$status = false;
} else {
$errorstr = "Neither a new course or an existing one was specified.";
return false;
}
}
}
//Now create categories and questions as needed
if ($status) {
include_once("$CFG->dirroot/question/restorelib.php");
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("creatingcategoriesandquestions");
echo "<ul>";
}
if (!$status = restore_create_questions($restore,$xml_file)) {
if (!defined('RESTORE_SILENTLY')) {
echo $OUTPUT->notification("Could not restore categories and questions!");
} else {
$errorstr = "Could not restore categories and questions!";
return false;
}
}
if (!defined('RESTORE_SILENTLY')) {
echo "</ul></li>";
}
}
//Now create course files as needed
if ($status and ($restore->course_files)) {
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("copyingcoursefiles");
}
if (!$status = restore_course_files($restore)) {
if (empty($status)) {
echo $OUTPUT->notification("Could not restore course files!");
} else {
$errorstr = "Could not restore course files!";
return false;
}
}
//If all is ok (and we have a counter)
if ($status and ($status !== true)) {
//Inform about user dirs created from backup
if (!defined('RESTORE_SILENTLY')) {
echo "<ul>";
echo "<li>".get_string("filesfolders").": ".$status.'</li>';
echo "</ul>";
}
}
if (!defined('RESTORE_SILENTLY')) {
echo "</li>";
}
}
//Now create events as needed
if ($status) {
if (!defined('RESTORE_SILENTLY')) {
@ -1432,48 +955,6 @@
}
}
//Now create course modules as needed
if ($status) {
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("creatingcoursemodules");
}
if (!$status = restore_create_modules($restore,$xml_file)) {
if (!defined('RESTORE_SILENTLY')) {
echo $OUTPUT->notification("Could not restore modules!");
} else {
$errorstr = "Could not restore modules!";
return false;
}
}
if (!defined('RESTORE_SILENTLY')) {
echo '</li>';
}
}
//Bring back the course blocks -- do it AFTER the modules!!!
if ($status) {
//If we are deleting and bringing into a course or making a new course, same situation
if ($restore->restoreto == RESTORETO_CURRENT_DELETING ||
$restore->restoreto == RESTORETO_EXISTING_DELETING ||
$restore->restoreto == RESTORETO_NEW_COURSE) {
if (!defined('RESTORE_SILENTLY')) {
echo '<li>'.get_string('creatingblocks');
}
$course_header->blockinfo = !empty($course_header->blockinfo) ? $course_header->blockinfo : NULL;
if (!$status = restore_create_blocks($restore, $info->backup_block_format, $course_header->blockinfo, $xml_file)) {
if (!defined('RESTORE_SILENTLY')) {
echo $OUTPUT->notification('Error while creating the course blocks');
} else {
$errorstr = "Error while creating the course blocks";
return false;
}
}
if (!defined('RESTORE_SILENTLY')) {
echo '</li>';
}
}
}
if ($status) {
//If we are deleting and bringing into a course or making a new course, same situation
if ($restore->restoreto == RESTORETO_CURRENT_DELETING ||
@ -1515,26 +996,6 @@
}
}
//Now, if all is OK, adjust the instance field in course_modules !!
//this also calculates the final modinfo information so, after this,
//code needing it can be used (like role_assignments. MDL-13740)
if ($status) {
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("checkinginstances");
}
if (!$status = restore_check_instances($restore)) {
if (!defined('RESTORE_SILENTLY')) {
echo $OUTPUT->notification("Could not adjust instances in course_modules!");
} else {
$errorstr = "Could not adjust instances in course_modules!";
return false;
}
}
if (!defined('RESTORE_SILENTLY')) {
echo '</li>';
}
}
//Now, if all is OK, adjust activity events
if ($status) {
if (!defined('RESTORE_SILENTLY')) {
@ -1552,178 +1013,3 @@
echo '</li>';
}
}
//Now, if all is OK, adjust inter-activity links
if ($status) {
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("decodinginternallinks");
}
if (!$status = restore_decode_content_links($restore)) {
if (!defined('RESTORE_SILENTLY')) {
echo $OUTPUT->notification("Could not decode content links!");
} else {
$errorstr = "Could not decode content links!";
return false;
}
}
if (!defined('RESTORE_SILENTLY')) {
echo '</li>';
}
}
//Now create gradebook as needed -- AFTER modules and blocks!!!
if ($status) {
if ($restore->backup_version > 2007090500) {
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("creatinggradebook");
}
if (!$status = restore_create_gradebook($restore,$xml_file)) {
if (!defined('RESTORE_SILENTLY')) {
echo $OUTPUT->notification("Could not restore gradebook!");
} else {
$errorstr = "Could not restore gradebook!";
return false;
}
}
if (!defined('RESTORE_SILENTLY')) {
echo '</li>';
}
} else {
// for moodle versions before 1.9, those grades need to be converted to use the new gradebook
// this code needs to execute *after* the course_modules are sorted out
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("migratinggrades");
}
/// force full refresh of grading data before migration == crete all items first
if (!$status = restore_migrate_old_gradebook($restore,$xml_file)) {
if (!defined('RESTORE_SILENTLY')) {
echo $OUTPUT->notification("Could not migrate gradebook!");
} else {
$errorstr = "Could not migrade gradebook!";
return false;
}
}
if (!defined('RESTORE_SILENTLY')) {
echo '</li>';
}
}
/// force full refresh of grading data after all items are created
grade_force_full_regrading($restore->course_id);
grade_grab_course_grades($restore->course_id);
}
/*******************************************************************************
************* Restore of Roles and Capabilities happens here ******************
*******************************************************************************/
// try to restore roles even when restore is going to fail - teachers might have
// at least some role assigned - this is not correct though
$status = restore_create_roles($restore, $xml_file) && $status;
$status = restore_roles_and_filter_settings($restore, $xml_file) && $status;
//Now if all is OK, update:
// - course modinfo field
// - categories table
// - add user as teacher
if ($status) {
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("checkingcourse");
}
//categories table
$course = $DB->get_record("course", array("id"=>$restore->course_id));
fix_course_sortorder();
// Check if the user has course update capability in the newly restored course
// there is no need to load his capabilities again, because restore_roles_and_filter_settings
// would have loaded it anyway, if there is any assignments.
// fix for MDL-6831
$newcontext = get_context_instance(CONTEXT_COURSE, $restore->course_id);
if (!has_capability('moodle/course:manageactivities', $newcontext)) {
// fix for MDL-9065, use the new config setting if exists
if ($CFG->creatornewroleid) {
role_assign($CFG->creatornewroleid, $USER->id, 0, $newcontext->id);
} else {
if ($legacyteachers = get_archetype_roles('editingteacher')) {
if ($legacyteacher = array_shift($legacyteachers)) {
role_assign($legacyteacher->id, $USER->id, 0, $newcontext->id);
}
} else {
echo $OUTPUT->notification('Could not find a legacy teacher role. You might need your moodle admin to assign a role with editing privilages to this course.');
}
}
}
// Availability system, if used, needs to find IDs for grade items
$rs=$DB->get_recordset_sql("
SELECT
cma.id,cma.gradeitemid
FROM
{course_modules) cm
INNER JOIN {course_modules_availability} cma on cm.id=cma.coursemoduleid
WHERE
cma.gradeitemid IS NOT NULL
AND cm.course=?
",array($restore->course_id));
foreach($rs as $rec) {
$newgradeid=backup_getid($restore->backup_unique_code,
'grade_items',$rec->gradeitemid);
if($newgradeid) {
$newdata=(object)array(
'id'=>$rec->id,
'gradeitemid'=>$newgradeid->new_id);
$DB->update_record('course_modules_availability',$newdata);
} else {
if (!defined('RESTORE_SILENTLY')) {
echo "<p>Can't find new ID for grade item $data->gradeitemid, ignoring availability condition.</p>";
}
continue;
}
}
$rs->close();
if (!defined('RESTORE_SILENTLY')) {
echo '</li>';
}
}
//Cleanup temps (files and db)
if ($status) {
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("cleaningtempdata");
}
if (!$status = clean_temp_data ($restore)) {
if (!defined('RESTORE_SILENTLY')) {
echo $OUTPUT->notification("Could not clean up temporary data from files and database");
} else {
$errorstr = "Could not clean up temporary data from files and database";
return false;
}
}
if (!defined('RESTORE_SILENTLY')) {
echo '</li>';
}
}
// this is not a critical check - the result can be ignored
if (restore_close_html($restore)){
if (!defined('RESTORE_SILENTLY')) {
echo '<li>Closing the Restorelog.html file.</li>';
}
}
else {
if (!defined('RESTORE_SILENTLY')) {
echo $OUTPUT->notification("Could not close the restorelog.html file");
}
}
if (!defined('RESTORE_SILENTLY')) {
//End the main ul
echo "</ul>";
//End the main table
echo "</td></tr>";
echo "</table>";
}
return $status;
}