function backup_execute(&$preferences, &$errorstr)
{
    global $CFG;
    $status = true;
    //Check for temp and backup and backup_unique_code directory
    //Create them as needed
    if (!defined('BACKUP_SILENTLY')) {
        echo "<li>" . get_string("creatingtemporarystructures") . '</li>';
    }
    $status = check_and_create_backup_dir($preferences->backup_unique_code);
    //Empty dir
    if ($status) {
        $status = clear_backup_dir($preferences->backup_unique_code);
    }
    //Delete old_entries from backup tables
    if (!defined('BACKUP_SILENTLY')) {
        echo "<li>" . get_string("deletingolddata") . '</li>';
    }
    $status = backup_delete_old_data();
    if (!$status) {
        if (!defined('BACKUP_SILENTLY')) {
            notify("An error occurred deleting old backup data");
        } else {
            $errorstr = "An error occurred deleting old backup data";
            return false;
        }
    }
    //Create the moodle.xml file
    if ($status) {
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("creatingxmlfile");
            //Begin a new list to xml contents
            echo "<ul>";
            echo "<li>" . get_string("writingheader") . '</li>';
        }
        //Obtain the xml file (create and open) and print prolog information
        $backup_file = backup_open_xml($preferences->backup_unique_code);
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("writinggeneralinfo") . '</li>';
        }
        //Prints general info about backup to file
        if ($backup_file) {
            if (!($status = backup_general_info($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up general info");
                } else {
                    $errorstr = "An error occurred while backing up general info";
                    return false;
                }
            }
        }
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("writingcoursedata");
            //Start new ul (for course)
            echo "<ul>";
            echo "<li>" . get_string("courseinfo") . '</li>';
        }
        //Prints course start (tag and general info)
        if ($status) {
            if (!($status = backup_course_start($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up course start");
                } else {
                    $errorstr = "An error occurred while backing up course start";
                    return false;
                }
            }
        }
        //Metacourse information
        if ($status && $preferences->backup_metacourse) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("metacourse") . '</li>';
            }
            if (!($status = backup_course_metacourse($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up metacourse info");
                } else {
                    $errorstr = "An error occurred while backing up metacourse info";
                    return false;
                }
            }
        }
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("blocks") . '</li>';
        }
        //Blocks information
        if ($status) {
            if (!($status = backup_course_blocks($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up course blocks");
                } else {
                    $errorstr = "An error occurred while backing up course blocks";
                    return false;
                }
            }
        }
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("sections") . '</li>';
        }
        //Section info
        if ($status) {
            if (!($status = backup_course_sections($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up course sections");
                } else {
                    $errorstr = "An error occurred while backing up course sections";
                    return false;
                }
            }
        }
        //End course contents (close ul)
        if (!defined('BACKUP_SILENTLY')) {
            echo "</ul></li>";
        }
        //User info
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writinguserinfo") . '</li>';
            }
            if (!($status = backup_user_info($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up user info");
                } else {
                    $errorstr = "An error occurred while backing up user info";
                    return false;
                }
            }
        }
        //If we have selected to backup messages and we are
        //doing a SITE backup, let's do it
        if ($status && $preferences->backup_messages && $preferences->backup_course == SITEID) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writingmessagesinfo") . '</li>';
            }
            if (!($status = backup_messages($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up messages");
                } else {
                    $errorstr = "An error occurred while backing up messages";
                    return false;
                }
            }
        }
        //If we have selected to backup blogs and we are
        //doing a SITE backup, let's do it
        if ($status && $preferences->backup_blogs && $preferences->backup_course == SITEID) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writingblogsinfo") . '</li>';
            }
            if (!($status = backup_blogs($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up blogs");
                } else {
                    $errorstr = "An error occurred while backing up blogs";
                    return false;
                }
            }
        }
        //If we have selected to backup quizzes or other modules that use questions
        //we've already added ids of categories and questions to backup to backup_ids table
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writingcategoriesandquestions") . '</li>';
            }
            require_once $CFG->dirroot . '/question/backuplib.php';
            if (!($status = backup_question_categories($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up quiz categories");
                } else {
                    $errorstr = "An error occurred while backing up quiz categories";
                    return false;
                }
            }
        }
        //Print logs if selected
        if ($status) {
            if ($preferences->backup_logs) {
                if (!defined('BACKUP_SILENTLY')) {
                    echo "<li>" . get_string("writingloginfo") . '</li>';
                }
                if (!($status = backup_log_info($backup_file, $preferences))) {
                    if (!defined('BACKUP_SILENTLY')) {
                        notify("An error occurred while backing up log info");
                    } else {
                        $errorstr = "An error occurred while backing up log info";
                        return false;
                    }
                }
            }
        }
        //Print scales info
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writingscalesinfo") . '</li>';
            }
            if (!($status = backup_scales_info($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up scales");
                } else {
                    $errorstr = "An error occurred while backing up scales";
                    return false;
                }
            }
        }
        //Print groups info
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writinggroupsinfo") . '</li>';
            }
            if (!($status = backup_groups_info($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up groups");
                } else {
                    $errostr = "An error occurred while backing up groups";
                    return false;
                }
            }
        }
        //Print groupings info
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writinggroupingsinfo") . '</li>';
            }
            if (!($status = backup_groupings_info($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up groupings");
                } else {
                    $errorstr = "An error occurred while backing up groupings";
                    return false;
                }
            }
        }
        //Print groupings_groups info
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writinggroupingsgroupsinfo") . '</li>';
            }
            if (!($status = backup_groupings_groups_info($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up groupings groups");
                } else {
                    $errorstr = "An error occurred while backing up groupings groups";
                    return false;
                }
            }
        }
        //Print events info
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writingeventsinfo") . '</li>';
            }
            if (!($status = backup_events_info($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up events");
                } else {
                    $errorstr = "An error occurred while backing up events";
                    return false;
                }
            }
        }
        //Print gradebook info
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writinggradebookinfo") . '</li>';
            }
            if (!($status = backup_gradebook_info($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up gradebook");
                } else {
                    $errorstr = "An error occurred while backing up gradebook";
                    return false;
                }
            }
        }
        //Module info, this unique function makes all the work!!
        //db export and module fileis copy
        if ($status) {
            $mods_to_backup = false;
            //Check if we have any mod to backup
            foreach ($preferences->mods as $module) {
                if ($module->backup) {
                    $mods_to_backup = true;
                }
            }
            //If we have to backup some module
            if ($mods_to_backup) {
                if (!defined('BACKUP_SILENTLY')) {
                    echo "<li>" . get_string("writingmoduleinfo");
                }
                //Start modules tag
                if (!($status = backup_modules_start($backup_file, $preferences))) {
                    if (!defined('BACKUP_SILENTLY')) {
                        notify("An error occurred while backing up module info");
                    } else {
                        $errorstr = "An error occurred while backing up module info";
                        return false;
                    }
                }
                //Open ul for module list
                if (!defined('BACKUP_SILENTLY')) {
                    echo "<ul>";
                }
                //Iterate over modules and call backup
                foreach ($preferences->mods as $module) {
                    if ($module->backup and $status) {
                        if (!defined('BACKUP_SILENTLY')) {
                            echo "<li>" . get_string("modulenameplural", $module->name) . '</li>';
                        }
                        if (!($status = backup_module($backup_file, $preferences, $module->name))) {
                            if (!defined('BACKUP_SILENTLY')) {
                                notify("An error occurred while backing up '{$module->name}'");
                            } else {
                                $errorstr = "An error occurred while backing up '{$module->name}'";
                                return false;
                            }
                        }
                    }
                }
                //Close ul for module list
                if (!defined('BACKUP_SILENTLY')) {
                    echo "</ul></li>";
                }
                //Close modules tag
                if (!($status = backup_modules_end($backup_file, $preferences))) {
                    if (!defined('BACKUP_SILENTLY')) {
                        notify("An error occurred while finishing the module backups");
                    } else {
                        $errorstr = "An error occurred while finishing the module backups";
                        return false;
                    }
                }
            }
        }
        //Backup course format data, if any.
        if (!defined('BACKUP_SILENTLY')) {
            echo '<li>' . get_string("courseformatdata") . '</li>';
        }
        if ($status) {
            if (!($status = backup_format_data($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up the course format data");
                } else {
                    $errorstr = "An error occurred while backing up the course format data";
                    return false;
                }
            }
        }
        //Prints course end
        if ($status) {
            if (!($status = backup_course_end($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while closing the course backup");
                } else {
                    $errorstr = "An error occurred while closing the course backup";
                    return false;
                }
            }
        }
        //Close the xml file and xml data
        if ($backup_file) {
            backup_close_xml($backup_file);
        }
        //End xml contents (close ul)
        if (!defined('BACKUP_SILENTLY')) {
            echo "</ul></li>";
        }
    }
    //Now, if selected, copy user files
    if ($status) {
        if ($preferences->backup_user_files) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("copyinguserfiles") . '</li>';
            }
            if (!($status = backup_copy_user_files($preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while copying user files");
                } else {
                    $errorstr = "An error occurred while copying user files";
                    return false;
                }
            }
        }
    }
    //Now, if selected, copy course files
    if ($status) {
        if ($preferences->backup_course_files) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("copyingcoursefiles") . '</li>';
            }
            if (!($status = backup_copy_course_files($preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while copying course files");
                } else {
                    $errorstr = "An error occurred while copying course files";
                    return false;
                }
            }
        }
    }
    //Now, if selected, copy site files
    if ($status) {
        if ($preferences->backup_site_files) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("copyingsitefiles") . '</li>';
            }
            if (!($status = backup_copy_site_files($preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while copying site files");
                } else {
                    $errorstr = "An error occurred while copying site files";
                    return false;
                }
            }
        }
    }
    //Now, zip all the backup directory contents
    if ($status) {
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("zippingbackup") . '</li>';
        }
        if (!($status = backup_zip($preferences))) {
            if (!defined('BACKUP_SILENTLY')) {
                notify("An error occurred while zipping the backup");
            } else {
                $errorstr = "An error occurred while zipping the backup";
                return false;
            }
        }
    }
    //Now, copy the zip file to course directory
    if ($status) {
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("copyingzipfile") . '</li>';
        }
        if (!($status = copy_zip_to_course_dir($preferences))) {
            if (!defined('BACKUP_SILENTLY')) {
                notify("An error occurred while copying the zip file to the course directory");
            } else {
                $errorstr = "An error occurred while copying the zip file to the course directory";
                return false;
            }
        }
    }
    //Now, clean temporary data (db and filesystem)
    if ($status) {
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("cleaningtempdata") . '</li>';
        }
        if (!($status = clean_temp_data($preferences))) {
            if (!defined('BACKUP_SILENTLY')) {
                notify("An error occurred while cleaning up temporary data");
            } else {
                $errorstr = "An error occurred while cleaning up temporary data";
                return false;
            }
        }
    }
    return $status;
}
Example #2
0
function schedule_inc_backup_course_execute($preferences, $starttime = 0)
{
    global $CFG;
    $status = true;
    //Some parts of the backup doesn't know about $preferences, so we
    //put a copy of it inside that CFG (always global) to be able to
    //use it. Then, when needed I search for preferences inside CFG
    //Used to avoid some problems in full_tag() when preferences isn't
    //set globally (i.e. in scheduled backups)
    $CFG->backup_preferences = $preferences;
    //Check for temp and backup and backup_unique_code directory
    //Create them as needed
    backup_add_to_log($starttime, $preferences->backup_course, "    checking temp structures", 'incrementalbackup');
    $status = check_and_create_backup_dir($preferences->backup_unique_code);
    //Empty backup dir
    if ($status) {
        backup_add_to_log($starttime, $preferences->backup_course, "    cleaning current dir", 'incrementalbackup');
        $status = clear_backup_dir($preferences->backup_unique_code);
    }
    //Create the moodle.xml file
    if ($status) {
        backup_add_to_log($starttime, $preferences->backup_course, "    creating backup file", 'incrementalbackup');
        //Obtain the xml file (create and open) and print prolog information
        $backup_file = backup_open_xml($preferences->backup_unique_code);
        //Prints general info about backup to file
        if ($backup_file) {
            backup_add_to_log($starttime, $preferences->backup_course, "      general info", 'incrementalbackup');
            $status = backup_general_info($backup_file, $preferences);
        } else {
            $status = false;
        }
        //Prints course start (tag and general info)
        if ($status) {
            $status = backup_course_start($backup_file, $preferences);
        }
        //Metacourse information
        if ($status && $preferences->backup_metacourse) {
            backup_add_to_log($starttime, $preferences->backup_course, "      metacourse info", 'incrementalbackup');
            $status = backup_course_metacourse($backup_file, $preferences);
        }
        //Block info
        if ($status) {
            backup_add_to_log($starttime, $preferences->backup_course, "      blocks info", 'incrementalbackup');
            $status = backup_course_blocks($backup_file, $preferences);
        }
        //Section info
        if ($status) {
            backup_add_to_log($starttime, $preferences->backup_course, "      sections info", 'incrementalbackup');
            $status = backup_course_sections($backup_file, $preferences);
        }
        //User info
        if ($status) {
            backup_add_to_log($starttime, $preferences->backup_course, "      user info", 'incrementalbackup');
            $status = backup_user_info($backup_file, $preferences);
        }
        //If we have selected to backup messages and we are
        //doing a SITE backup, let's do it
        if ($status && $preferences->backup_messages && $preferences->backup_course == SITEID) {
            backup_add_to_log($starttime, $preferences->backup_course, "      messages", 'incrementalbackup');
            if (!($status = backup_messages($backup_file, $preferences))) {
                notify("An error occurred while backing up messages");
            }
        }
        //If we have selected to backup blogs and we are
        //doing a SITE backup, let's do it
        if ($status && isset($preferences->backup_blogs) && isset($preferences->backup_course) && $preferences->backup_blogs && $preferences->backup_course == SITEID) {
            schedule_backup_log($starttime, $preferences->backup_course, "      blogs", 'incrementalbackup');
            $status = backup_blogs($backup_file, $preferences);
        }
        //If we have selected to backup quizzes, backup categories and
        //questions structure (step 1). See notes on mod/quiz/backuplib.php
        if ($status and isset($preferences->mods['quiz']->backup)) {
            backup_add_to_log($starttime, $preferences->backup_course, "      categories & questions", 'incrementalbackup');
            $status = backup_question_categories($backup_file, $preferences);
        }
        //Print logs if selected
        if ($status) {
            if ($preferences->backup_logs) {
                backup_add_to_log($starttime, $preferences->backup_course, "      logs", 'incrementalbackup');
                $status = backup_log_info($backup_file, $preferences);
            }
        }
        //Print scales info
        if ($status) {
            backup_add_to_log($starttime, $preferences->backup_course, "      scales", 'incrementalbackup');
            $status = backup_scales_info($backup_file, $preferences);
        }
        //Print groups info
        if ($status) {
            backup_add_to_log($starttime, $preferences->backup_course, "      groups", 'incrementalbackup');
            $status = backup_groups_info($backup_file, $preferences);
        }
        //Print groupings info
        if ($status) {
            backup_add_to_log($starttime, $preferences->backup_course, "      groupings", 'incrementalbackup');
            $status = backup_groupings_info($backup_file, $preferences);
        }
        //Print groupings_groups info
        if ($status) {
            backup_add_to_log($starttime, $preferences->backup_course, "      groupings_groups", 'incrementalbackup');
            $status = backup_groupings_groups_info($backup_file, $preferences);
        }
        //Print events info
        if ($status) {
            backup_add_to_log($starttime, $preferences->backup_course, "      events", 'incrementalbackup');
            $status = backup_events_info($backup_file, $preferences);
        }
        //Print gradebook info
        if ($status) {
            backup_add_to_log($starttime, $preferences->backup_course, "      gradebook", 'incrementalbackup');
            $status = backup_gradebook_info($backup_file, $preferences);
        }
        //Module info, this unique function makes all the work!!
        //db export and module fileis copy
        if ($status) {
            $mods_to_backup = false;
            //Check if we have any mod to backup
            if (!empty($preferences->mods)) {
                foreach ($preferences->mods as $module) {
                    if ($module->backup) {
                        $mods_to_backup = true;
                    }
                }
            }
            //If we have to backup some module
            if ($mods_to_backup) {
                backup_add_to_log($starttime, $preferences->backup_course, "      modules", 'incrementalbackup');
                //Start modules tag
                $status = backup_modules_start($backup_file, $preferences);
                //Iterate over modules and call backup
                foreach ($preferences->mods as $module) {
                    if ($module->backup and $status) {
                        backup_add_to_log($starttime, $preferences->backup_course, "        {$module->name}", 'incrementalbackup');
                        $status = backup_module($backup_file, $preferences, $module->name);
                    }
                }
                //Close modules tag
                $status = backup_modules_end($backup_file, $preferences);
            }
        }
        //Backup course format data, if any.
        if ($status) {
            backup_add_to_log($starttime, $preferences->backup_course, "      course format data", 'incrementalbackup');
            $status = backup_format_data($backup_file, $preferences);
        }
        //Prints course end
        if ($status) {
            $status = backup_course_end($backup_file, $preferences);
        }
        //Close the xml file and xml data
        if ($backup_file) {
            backup_close_xml($backup_file);
        }
    }
    //Now, if selected, copy user files
    if ($status) {
        if ($preferences->backup_user_files) {
            backup_add_to_log($starttime, $preferences->backup_course, "    copying user files", 'incrementalbackup');
            $status = backup_copy_user_files($preferences);
        }
    }
    //Now, if selected, copy course files
    if ($status) {
        if ($preferences->backup_course_files) {
            backup_add_to_log($starttime, $preferences->backup_course, "    copying course files", 'incrementalbackup');
            $status = backup_copy_course_files($preferences);
        }
    }
    //Now, if selected, copy site files
    if ($status) {
        if ($preferences->backup_site_files) {
            backup_add_to_log($starttime, $preferences->backup_course, "    copying site files", 'incrementalbackup');
            $status = backup_copy_site_files($preferences);
        }
    }
    //Now, zip all the backup directory contents
    if ($status) {
        backup_add_to_log($starttime, $preferences->backup_course, "    zipping files", 'incrementalbackup');
        $status = backup_zip($preferences);
    }
    //Now, copy the zip file to course directory
    if ($status) {
        backup_add_to_log($starttime, $preferences->backup_course, "    copying backup", 'incrementalbackup');
        $status = copy_zip_to_course_dir($preferences);
    }
    //Now, clean temporary data (db and filesystem)
    if ($status) {
        backup_add_to_log($starttime, $preferences->backup_course, "    cleaning temp data", 'incrementalbackup');
        $status = clean_temp_data($preferences);
    }
    //Unset CFG->backup_preferences only needed in scheduled backups
    unset($CFG->backup_preferences);
    return $status;
}
Example #3
0
function restore_execute(&$restore, $info, $course_header, &$errorstr)
{
    global $CFG, $USER;
    $status = true;
    //Checks for the required files/functions to restore every module
    //and include them
    if ($allmods = get_records("modules")) {
        foreach ($allmods as $mod) {
            $modname = $mod->name;
            $modfile = "{$CFG->dirroot}/mod/{$modname}/restorelib.php";
            //If file exists and we have selected to restore that type of module
            if (file_exists($modfile) and !empty($restore->mods[$modname]) and $restore->mods[$modname]->restore) {
                include_once $modfile;
            }
        }
    }
    if (!defined('RESTORE_SILENTLY')) {
        //Start the main table
        echo "<table cellpadding=\"5\">";
        echo "<tr><td>";
        //Start the main ul
        echo "<ul>";
    }
    //Location of the xml file
    $xml_file = $CFG->dataroot . "/temp/backup/" . $restore->backup_unique_code . "/moodle.xml";
    //Preprocess the moodle.xml file spliting into smaller chucks (modules, users, logs...)
    //for optimal parsing later in the restore process.
    if (!empty($CFG->experimentalsplitrestore)) {
        if (!defined('RESTORE_SILENTLY')) {
            echo '<li>' . get_string('preprocessingbackupfile') . '</li>';
        }
        //First of all, split moodle.xml into handy files
        if (!restore_split_xml($xml_file, $restore)) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Error proccessing moodle.xml file. Process ended.");
            } else {
                $errorstr = "Error proccessing moodle.xml file. Process ended.";
            }
            return false;
        }
    }
    //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')) {
                notify("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 = get_record("course", "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')) {
                        notify("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')) {
                notify("Error opening existing course.");
                $status = false;
            } else {
                $errorstr = "Error opening existing course.";
                return false;
            }
        }
    }
    //Now create users as needed
    if ($status and ($restore->users == 0 or $restore->users == 1)) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatingusers") . "<br />";
        }
        if (!($status = restore_create_users($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore users.");
            } else {
                $errorstr = "Could not restore users.";
                return false;
            }
        }
        //Now print info about the work done
        if ($status) {
            $recs = get_records_sql("select old_id, new_id from {$CFG->prefix}backup_ids\n                                     where backup_code = '{$restore->backup_unique_code}' and\n                                     table_name = 'user'");
            //We've records
            if ($recs) {
                $new_count = 0;
                $exists_count = 0;
                $student_count = 0;
                $teacher_count = 0;
                $counter = 0;
                //Iterate, filling counters
                foreach ($recs as $rec) {
                    //Get full record, using backup_getids
                    $record = backup_getid($restore->backup_unique_code, "user", $rec->old_id);
                    if (strpos($record->info, "new") !== false) {
                        $new_count++;
                    }
                    if (strpos($record->info, "exists") !== false) {
                        $exists_count++;
                    }
                    if (strpos($record->info, "student") !== false) {
                        $student_count++;
                    } else {
                        if (strpos($record->info, "teacher") !== false) {
                            $teacher_count++;
                        }
                    }
                    //Do some output
                    $counter++;
                    if ($counter % 10 == 0) {
                        if (!defined('RESTORE_SILENTLY')) {
                            echo ".";
                            if ($counter % 200 == 0) {
                                echo "<br />";
                            }
                        }
                        backup_flush(300);
                    }
                }
                if (!defined('RESTORE_SILENTLY')) {
                    //Now print information gathered
                    echo " (" . get_string("new") . ": " . $new_count . ", " . get_string("existing") . ": " . $exists_count . ")";
                    echo "<ul>";
                    echo "<li>" . get_string("students") . ": " . $student_count . '</li>';
                    echo "<li>" . get_string("teachers") . ": " . $teacher_count . '</li>';
                    echo "</ul>";
                }
            } else {
                if (!defined('RESTORE_SILENTLY')) {
                    notify("No users were found!");
                }
                // no need to return false here, it's recoverable.
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo "</li>";
        }
    }
    //Now create groups as needed
    if ($status and ($restore->groups == RESTORE_GROUPS_ONLY or $restore->groups == RESTORE_GROUPS_GROUPINGS)) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatinggroups");
        }
        if (!($status = restore_create_groups($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore groups!");
            } else {
                $errorstr = "Could not restore groups!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //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')) {
                notify("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')) {
                notify("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')) {
                    notify("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')) {
                        notify("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')) {
                    notify("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 metacourse info
    if ($status and $restore->metacourse) {
        //Only to new courses!
        if ($restore->restoreto == RESTORETO_NEW_COURSE) {
            if (!defined('RESTORE_SILENTLY')) {
                echo "<li>" . get_string("creatingmetacoursedata");
            }
            if (!($status = restore_create_metacourse($restore, $xml_file))) {
                if (!defined('RESTORE_SILENTLY')) {
                    notify("Error creating metacourse in the course.");
                } else {
                    $errorstr = "Error creating metacourse in the course.";
                    return false;
                }
            }
            if (!defined('RESTORE_SILENTLY')) {
                echo '</li>';
            }
        }
    }
    //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')) {
                notify("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 user_files as needed
    if ($status and $restore->user_files) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("copyinguserfiles");
        }
        if (!($status = restore_user_files($restore))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore user files!");
            } else {
                $errorstr = "Could not restore user 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("userzones") . ": " . $status;
                echo "</li></ul>";
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</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)) {
                notify("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 site files as needed
    if ($status and $restore->site_files) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string('copyingsitefiles');
        }
        if (!($status = restore_site_files($restore))) {
            if (empty($status)) {
                notify("Could not restore site files!");
            } else {
                $errorstr = "Could not restore site 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 messages as needed
    if ($status and $restore->messages) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatingmessagesinfo");
        }
        if (!($status = restore_create_messages($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore messages!");
            } else {
                $errorstr = "Could not restore messages!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo "</li>";
        }
    }
    //Now create blogs as needed
    if ($status and $restore->blogs) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatingblogsinfo");
        }
        if (!($status = restore_create_blogs($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore blogs!");
            } else {
                $errorstr = "Could not restore blogs!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo "</li>";
        }
    }
    //Now create scales as needed
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatingscales");
        }
        if (!($status = restore_create_scales($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore custom scales!");
            } else {
                $errorstr = "Could not restore custom scales!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now create events as needed
    if ($status) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatingevents");
        }
        if (!($status = restore_create_events($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore course events!");
            } else {
                $errorstr = "Could not restore course events!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //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')) {
                notify("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')) {
                    notify('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 || $restore->restoreto == RESTORETO_EXISTING_DELETING || $restore->restoreto == RESTORETO_NEW_COURSE) {
            if (!defined('RESTORE_SILENTLY')) {
                echo '<li>' . get_string('courseformatdata');
            }
            if (!($status = restore_set_format_data($restore, $xml_file))) {
                $error = "Error while setting the course format data";
                if (!defined('RESTORE_SILENTLY')) {
                    notify($error);
                } else {
                    $errorstr = $error;
                    return false;
                }
            }
            if (!defined('RESTORE_SILENTLY')) {
                echo '</li>';
            }
        }
    }
    //Now create log entries as needed
    if ($status and $restore->logs) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("creatinglogentries");
        }
        if (!($status = restore_create_logs($restore, $xml_file))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not restore logs!");
            } else {
                $errorstr = "Could not restore logs!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //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')) {
                notify("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')) {
            echo "<li>" . get_string("refreshingevents");
        }
        if (!($status = restore_refresh_events($restore))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not refresh events for activities!");
            } else {
                $errorstr = "Could not refresh events for activities!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            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')) {
                notify("Could not decode content links!");
            } else {
                $errorstr = "Could not decode content links!";
                return false;
            }
        }
        if (!defined('RESTORE_SILENTLY')) {
            echo '</li>';
        }
    }
    //Now, with backup files prior to version 2005041100,
    //convert all the wiki texts in the course to markdown
    if ($status && $restore->backup_version < 2005041100) {
        if (!defined('RESTORE_SILENTLY')) {
            echo "<li>" . get_string("convertingwikitomarkdown");
        }
        if (!($status = restore_convert_wiki2markdown($restore))) {
            if (!defined('RESTORE_SILENTLY')) {
                notify("Could not convert wiki texts to markdown!");
            } else {
                $errorstr = "Could not convert wiki texts to markdown!";
                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')) {
                    notify("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')) {
                    notify("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_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 = get_record("course", "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_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_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM))) {
                    if ($legacyteacher = array_shift($legacyteachers)) {
                        role_assign($legacyteacher->id, $USER->id, 0, $newcontext->id);
                    }
                } else {
                    notify('Could not find a legacy teacher role. You might need your moodle admin to assign a role with editing privilages to this course.');
                }
            }
        }
        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')) {
                notify("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')) {
            notify("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;
}
 protected function cleanupDir()
 {
     // クリーンアップ
     if (!clean_temp_data($this->prefs)) {
         throw new SharingCart_Exception('Cleanup failure');
     }
 }