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

File diff suppressed because it is too large Load diff

View file

@ -488,333 +488,6 @@
return ($status && $status2);
}
/** this function will restore an entire backup.zip into the specified course
* using standard moodle backup/restore functions, but silently.
* @param string $pathtofile the absolute path to the backup file.
* @param int $destinationcourse the course id to restore to.
* @param boolean $emptyfirst whether to delete all coursedata first.
* @param boolean $userdata whether to include any userdata that may be in the backup file.
* @param array $preferences optional, 0 will be used. Can contain:
* metacourse
* logs
* course_files
* messages
*/
function import_backup_file_silently($pathtofile,$destinationcourse,$emptyfirst=false,$userdata=false, $preferences=array()) {
global $CFG,$SESSION,$USER, $DB; // is there such a thing on cron? I guess so..
if (!defined('RESTORE_SILENTLY')) {
define('RESTORE_SILENTLY',true); // don't output all the stuff to us.
}
$debuginfo = 'import_backup_file_silently: ';
$cleanupafter = false;
$errorstr = ''; // passed by reference to restore_precheck to get errors from.
$course = null;
if ($destinationcourse && !$course = $DB->get_record('course', array('id'=>$destinationcourse))) {
mtrace($debuginfo.'Course with id $destinationcourse was not a valid course!');
return false;
}
// first check we have a valid file.
if (!file_exists($pathtofile) || !is_readable($pathtofile)) {
mtrace($debuginfo.'File '.$pathtofile.' either didn\'t exist or wasn\'t readable');
return false;
}
// now make sure it's a zip file
require_once($CFG->dirroot.'/lib/filelib.php');
$filename = substr($pathtofile,strrpos($pathtofile,'/')+1);
$mimetype = mimeinfo("type", $filename);
if ($mimetype != 'application/zip') {
mtrace($debuginfo.'File '.$pathtofile.' was of wrong mimetype ('.$mimetype.')' );
return false;
}
// restore_precheck wants this within dataroot, so lets put it there if it's not already..
if (strstr($pathtofile,$CFG->dataroot) === false) {
// first try and actually move it..
if (!check_dir_exists($CFG->dataroot.'/temp/backup/',true)) {
mtrace($debuginfo.'File '.$pathtofile.' outside of dataroot and couldn\'t move it! ');
return false;
}
if (!copy($pathtofile,$CFG->dataroot.'/temp/backup/'.$filename)) {
mtrace($debuginfo.'File '.$pathtofile.' outside of dataroot and couldn\'t move it! ');
return false;
} else {
$pathtofile = 'temp/backup/'.$filename;
$cleanupafter = true;
}
} else {
// it is within dataroot, so take it off the path for restore_precheck.
$pathtofile = substr($pathtofile,strlen($CFG->dataroot.'/'));
}
if (!backup_required_functions()) {
mtrace($debuginfo.'Required function check failed (see backup_required_functions)');
return false;
}
@ini_set("max_execution_time","3000");
raise_memory_limit(MEMORY_EXTRA);
if (!$backup_unique_code = restore_precheck($destinationcourse,$pathtofile,$errorstr,true)) {
mtrace($debuginfo.'Failed restore_precheck (error was '.$errorstr.')');
return false;
}
global $restore; // ick
$restore = new StdClass;
// copy back over the stuff that gets set in restore_precheck
$restore->course_header = $SESSION->course_header;
$restore->info = $SESSION->info;
$xmlfile = "$CFG->dataroot/temp/backup/$backup_unique_code/moodle.xml";
$info = restore_read_xml_roles($xmlfile);
$restore->rolesmapping = array();
if (isset($info->roles) && is_array($info->roles)) {
foreach ($info->roles as $id => $info) {
if ($newroleid = $DB->get_field('role', 'id', array('shortname' => $info->shortname))) {
$restore->rolesmapping[$id] = $newroleid;
}
}
}
// add on some extra stuff we need...
$restore->metacourse = (isset($preferences['restore_metacourse']) ? $preferences['restore_metacourse'] : 0);
$restore->course_id = $destinationcourse;
if ($destinationcourse) {
$restore->restoreto = RESTORETO_CURRENT_ADDING;
$restore->course_startdateoffset = $course->startdate - $restore->course_header->course_startdate;
} else {
$restore->restoreto = RESTORETO_NEW_COURSE;
$restore->restore_restorecatto = 0; // let this be handled by the headers
$restore->course_startdateoffset = 0;
}
$restore->users = $userdata;
$restore->user_files = $userdata;
$restore->deleting = $emptyfirst;
$restore->groups = (isset($preferences['restore_groups']) ? $preferences['restore_groups'] : RESTORE_GROUPS_NONE);
$restore->logs = (isset($preferences['restore_logs']) ? $preferences['restore_logs'] : 0);
$restore->messages = (isset($preferences['restore_messages']) ? $preferences['restore_messages'] : 0);
$restore->blogs = (isset($preferences['restore_blogs']) ? $preferences['restore_blogs'] : 0);
$restore->course_files = (isset($preferences['restore_course_files']) ? $preferences['restore_course_files'] : 0);
$restore->site_files = (isset($preferences['restore_site_files']) ? $preferences['restore_site_files'] : 0);
$restore->backup_version = $restore->info->backup_backup_version;
$restore->original_wwwroot = $restore->info->original_wwwroot;
// now copy what we have over to the session
// this needs to happen before restore_setup_for_check
// which for some reason reads the session
$SESSION->restore =& $restore;
// rename the things that are called differently
$SESSION->restore->restore_course_files = $restore->course_files;
$SESSION->restore->restore_site_files = $restore->site_files;
$SESSION->restore->backup_version = $restore->info->backup_backup_version;
restore_setup_for_check($restore, $backup_unique_code);
// maybe we need users (defaults to 2 (none) in restore_setup_for_check)
// so set this again here
if (!empty($userdata)) {
$restore->users = 1;
}
// we also need modules...
if ($allmods = $DB->get_records("modules")) {
foreach ($allmods as $mod) {
$modname = $mod->name;
//Now check that we have that module info in the backup file
if (isset($restore->info->mods[$modname]) && $restore->info->mods[$modname]->backup == "true") {
$restore->mods[$modname]->restore = true;
$restore->mods[$modname]->userinfo = $userdata;
}
else {
// avoid warnings
$restore->mods[$modname]->restore = false;
$restore->mods[$modname]->userinfo = false;
}
}
}
if (!$status = restore_execute($restore,$restore->info,$restore->course_header,$errorstr)) {
mtrace($debuginfo.'Failed restore_execute (error was '.$errorstr.')');
return false;
}
// now get out the new courseid and return that
if ($restore->restoreto = RESTORETO_NEW_COURSE) {
if (!empty($SESSION->restore->course_id)) {
return $SESSION->restore->course_id;
}
return false;
}
return true;
}
/**
* Function to backup an entire course silently and create a zipfile.
*
* @param int $courseid the id of the course
* @param array $prefs see {@link backup_generate_preferences_artificially}
*/
function backup_course_silently($courseid, $prefs, &$errorstring) {
global $CFG, $preferences, $DB; // global preferences here because something else wants it :(
if (!defined('BACKUP_SILENTLY')) {
define('BACKUP_SILENTLY', 1);
}
if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
debugging("Couldn't find course with id $courseid in backup_course_silently");
return false;
}
$preferences = backup_generate_preferences_artificially($course, $prefs);
$preferences->destination = array_key_exists('destination', $prefs) ? $prefs['destination'] : 0;
if (backup_execute($preferences, $errorstring)) {
return $CFG->dataroot . '/' . $course->id . '/backupdata/' . $preferences->backup_name;
}
else {
return false;
}
}
/**
* Function to generate the $preferences variable that
* backup uses. This will back up all modules and instances in a course.
*
* @param object $course course object
* @param array $prefs can contain:
backup_metacourse
backup_users
backup_logs
backup_user_files
backup_course_files
backup_site_files
backup_messages
userdata
* and if not provided, they will not be included.
*/
function backup_generate_preferences_artificially($course, $prefs) {
global $CFG, $DB;
$preferences = new StdClass;
$preferences->backup_unique_code = time();
$preferences->backup_users = (isset($prefs['backup_users']) ? $prefs['backup_users'] : 0);
$preferences->backup_name = backup_get_zipfile_name($course, $preferences->backup_unique_code);
$preferences->mods = array();
$count = 0;
if ($allmods = $DB->get_records("modules") ) {
foreach ($allmods as $mod) {
$modname = $mod->name;
$modfile = "$CFG->dirroot/mod/$modname/backuplib.php";
$modbackup = $modname."_backup_mods";
$modbackupone = $modname."_backup_one_mod";
$modcheckbackup = $modname."_check_backup_mods";
if (!file_exists($modfile)) {
continue;
}
include_once($modfile);
if (!function_exists($modbackup) || !function_exists($modcheckbackup)) {
continue;
}
$var = "exists_".$modname;
$preferences->$var = true;
$count++;
// check that there are instances and we can back them up individually
if (!$DB->count_records('course_modules', array('course'=>$course->id), array('module'=>$mod->id)) || !function_exists($modbackupone)) {
continue;
}
$var = 'exists_one_'.$modname;
$preferences->$var = true;
$varname = $modname.'_instances';
$preferences->$varname = get_all_instances_in_course($modname, $course, NULL, true);
foreach ($preferences->$varname as $instance) {
$preferences->mods[$modname]->instances[$instance->id]->name = $instance->name;
$var = 'backup_'.$modname.'_instance_'.$instance->id;
$preferences->$var = true;
$preferences->mods[$modname]->instances[$instance->id]->backup = true;
$var = 'backup_user_info_'.$modname.'_instance_'.$instance->id;
$preferences->$var = (!array_key_exists('userdata', $prefs) || $prefs['userdata']);
$preferences->mods[$modname]->instances[$instance->id]->userinfo = $preferences->$var;
$var = 'backup_'.$modname.'_instances';
$preferences->$var = 1; // we need this later to determine what to display in modcheckbackup.
}
//Check data
//Check module info
$preferences->mods[$modname]->name = $modname;
$var = "backup_".$modname;
$preferences->$var = true;
$preferences->mods[$modname]->backup = true;
//Check include user info
$var = "backup_user_info_".$modname;
$preferences->$var = (!array_key_exists('userdata', $prefs) || $prefs['userdata']);
$preferences->mods[$modname]->userinfo = $preferences->$var;
//Call the check function to show more info
$modcheckbackup = $modname."_check_backup_mods";
$var = $modname.'_instances';
$instancestopass = array();
if (!empty($preferences->$var) && is_array($preferences->$var) && count($preferences->$var)) {
$table->data = array();
$countinstances = 0;
foreach ($preferences->$var as $instance) {
$var1 = 'backup_'.$modname.'_instance_'.$instance->id;
$var2 = 'backup_user_info_'.$modname.'_instance_'.$instance->id;
if (!empty($preferences->$var1)) {
$obj = new StdClass;
$obj->name = $instance->name;
$obj->userdata = $preferences->$var2;
$obj->id = $instance->id;
$instancestopass[$instance->id]= $obj;
$countinstances++;
}
}
}
$modcheckbackup($course->id,$preferences->$var,$preferences->backup_unique_code,$instancestopass);
}
}
//Check other parameters
$preferences->backup_metacourse = (isset($prefs['backup_metacourse']) ? $prefs['backup_metacourse'] : 0);
$preferences->backup_logs = (isset($prefs['backup_logs']) ? $prefs['backup_logs'] : 0);
$preferences->backup_user_files = (isset($prefs['backup_user_files']) ? $prefs['backup_user_files'] : 0);
$preferences->backup_course_files = (isset($prefs['backup_course_files']) ? $prefs['backup_course_files'] : 0);
$preferences->backup_site_files = (isset($prefs['backup_site_files']) ? $prefs['backup_site_files'] : 0);
$preferences->backup_messages = (isset($prefs['backup_messages']) ? $prefs['backup_messages'] : 0);
$preferences->backup_gradebook_history = (isset($prefs['backup_gradebook_history']) ? $prefs['backup_gradebook_history'] : 0);
$preferences->backup_blogs = (isset($prefs['backup_blogs']) ? $prefs['backup_blogs'] : 0);
$preferences->backup_course = $course->id;
//Check users
user_check_backup($course->id,$preferences->backup_unique_code,$preferences->backup_users,$preferences->backup_messages, $preferences->backup_blogs);
//Check logs
log_check_backup($course->id);
//Check user files
user_files_check_backup($course->id,$preferences->backup_unique_code);
//Check course files
course_files_check_backup($course->id,$preferences->backup_unique_code);
//Check site files
site_files_check_backup($course->id,$preferences->backup_unique_code);
//Role assignments
$roles = $DB->get_records('role', null, 'sortorder');
foreach ($roles as $role) {
$preferences->backuproleassignments[$role->id] = $role;
}
backup_add_static_preferences($preferences);
return $preferences;
}
function add_to_backup_log($starttime,$courseid,$message, $backuptype) {
global $DB;
$log = new stdClass();

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;
}