Backup refactor: moved backup_execute.html into functions, added new backup_course_silently function as proof of concept (matches import_backup_file_silently)

This commit is contained in:
mjollnir_
2007-03-23 06:14:40 +00:00
parent b2af5654da
commit 9e3775dbc2
5 changed files with 696 additions and 294 deletions
+3 -251
View File
@@ -29,10 +29,7 @@
$preferences = new StdClass;
backup_fetch_prefs_from_request($preferences,$count,$course);
//Another Info
$preferences->moodle_version = $CFG->version;
$preferences->moodle_release = $CFG->release;
$preferences->backup_version = $CFG->backup_version;
$preferences->backup_release = $CFG->backup_release;
backup_add_static_preferences($preferences);
if ($count == 0) {
notice("No backupable modules are installed!");
@@ -58,254 +55,9 @@
//Start the main ul
echo "<ul>";
}
//Check for temp and backup and backup_unique_code directory
//Create them as needed
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
echo "<li>".get_string("deletingolddata").'</li>';
$status = backup_delete_old_data();
if (!$status) {
error ("An error occurred deleting old backup data");
}
//Create the moodle.xml file
if ($status) {
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);
echo "<li>".get_string("writinggeneralinfo").'</li>';
//Prints general info about backup to file
if ($backup_file) {
if (!$status = backup_general_info($backup_file,$preferences)) {
notify("An error occurred while backing up general info");
}
}
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)) {
notify("An error occurred while backing up course start");
}
}
//Metacourse information
if ($status && $preferences->backup_metacourse) {
echo "<li>".get_string("metacourse").'</li>';
if (!$status = backup_course_metacourse($backup_file,$preferences)) {
notify("An error occurred while backing up metacourse info");
}
}
echo "<li>".get_string("blocks").'</li>';
//Blocks information
if ($status) {
if (!$status = backup_course_blocks($backup_file,$preferences)) {
notify("An error occurred while backing up course blocks");
}
}
echo "<li>".get_string("sections").'</li>';
//Section info
if ($status) {
if (!$status = backup_course_sections($backup_file,$preferences)) {
notify("An error occurred while backing up course sections");
}
}
//End course contents (close ul)
echo "</ul></li>";
//User info
if ($status) {
echo "<li>".get_string("writinguserinfo").'</li>';
if (!$status = backup_user_info($backup_file,$preferences)) {
notify("An error occurred while backing up user info");
}
}
//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) {
echo "<li>".get_string("writingmessagesinfo").'</li>';
if (!$status = backup_messages($backup_file,$preferences)) {
notify("An error occurred while backing up messages");
}
}
//If we have selected to backup quizzes, backup categories and
//questions structure (step 1). See notes on mod/quiz/backuplib.php
if ($status and !empty($preferences->mods['quiz']->backup)) {
echo "<li>".get_string("writingcategoriesandquestions").'</li>';
require_once($CFG->dirroot.'/mod/quiz/backuplib.php');
if (!$status = backup_question_categories($backup_file,$preferences)) {
notify("An error occurred while backing up quiz categories");
}
}
//Print logs if selected
if ($status) {
if ($preferences->backup_logs) {
echo "<li>".get_string("writingloginfo").'</li>';
if (!$status = backup_log_info($backup_file,$preferences)) {
notify("An error occurred while backing up log info");
}
}
}
//Print scales info
if ($status) {
echo "<li>".get_string("writingscalesinfo").'</li>';
if (!$status = backup_scales_info($backup_file,$preferences)) {
notify("An error occurred while backing up scales");
}
}
//Print groupings info
if ($status) {
echo "<li>".get_string("writinggroupingsinfo").'</li>';
if (!$status = backup_groupings_info($backup_file,$preferences)) {
notify("An error occurred while backing up groupings");
}
}
//Print groups info
if ($status) {
echo "<li>".get_string("writinggroupsinfo").'</li>';
if (!$status = backup_groups_info($backup_file,$preferences)) {
notify("An error occurred while backing up groups");
}
}
//Print events info
if ($status) {
echo "<li>".get_string("writingeventsinfo").'</li>';
if (!$status = backup_events_info($backup_file,$preferences)) {
notify("An error occurred while backing up events");
}
}
//Print gradebook info
if ($status) {
echo "<li>".get_string("writinggradebookinfo").'</li>';
if (!$status = backup_gradebook_info($backup_file,$preferences)) {
notify("An error occurred while backing up gradebook");
}
}
//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) {
echo "<li>".get_string("writingmoduleinfo");
//Start modules tag
if (!$status = backup_modules_start ($backup_file,$preferences)) {
notify("An error occurred while backing up module info");
}
//Open ul for module list
echo "<ul>";
//Iterate over modules and call backup
foreach ($preferences->mods as $module) {
if ($module->backup and $status) {
echo "<li>".get_string("modulenameplural",$module->name).'</li>';
if (!$status = backup_module($backup_file,$preferences,$module->name)) {
notify("An error occurred while backing up '$module->name'");
}
}
}
//Close ul for module list
echo "</ul></li>";
//Close modules tag
if (!$status = backup_modules_end ($backup_file,$preferences)) {
notify("An error occurred while finishing the module backups");
}
}
}
//Backup course format data, if any.
echo '<li>'.get_string("courseformatdata").'</li>';
if($status) {
if (!$status = backup_format_data($backup_file,$preferences)) {
notify("An error occurred while backing up the course format data");
}
}
//Prints course end
if ($status) {
if (!$status = backup_course_end($backup_file,$preferences)) {
notify("An error occurred while closing the course backup");
}
}
//Close the xml file and xml data
if ($backup_file) {
backup_close_xml($backup_file);
}
//End xml contents (close ul)
echo "</ul></li>";
}
//Now, if selected, copy user files
if ($status) {
if ($preferences->backup_user_files) {
echo "<li>".get_string("copyinguserfiles").'</li>';
if (!$status = backup_copy_user_files ($preferences)) {
notify("An error occurred while copying user files");
}
}
}
//Now, if selected, copy course files
if ($status) {
if ($preferences->backup_course_files) {
echo "<li>".get_string("copyingcoursefiles").'</li>';
if (!$status = backup_copy_course_files ($preferences)) {
notify("An error occurred while copying course files");
}
}
}
//Now, zip all the backup directory contents
if ($status) {
echo "<li>".get_string("zippingbackup").'</li>';
if (!$status = backup_zip ($preferences)) {
notify("An error occurred while zipping the backup");
}
}
//Now, copy the zip file to course directory
if ($status) {
echo "<li>".get_string("copyingzipfile").'</li>';
if (!$status = copy_zip_to_course_dir ($preferences)) {
notify("An error occurred while copying the zip file to the course directory");
}
}
//Now, clean temporary data (db and filesystem)
if ($status) {
echo "<li>".get_string("cleaningtempdata").'</li>';
if (!$status = clean_temp_data ($preferences)) {
notify("An error occurred while cleaning up temporary data");
}
}
$errorstr = '';
$status = backup_execute($preferences, $errorstr);
//Ends th main ul
echo "</ul>";
+2 -36
View File
@@ -279,43 +279,9 @@ function selectItemInCheckboxByName(formId, checkName, checked ) {
$backup_unique_code = time();
//Calculate the backup string
//Calculate the backup word
//Take off some characters in the filename !!
$takeoff = array(" ", ":", "/", "\\", "|");
$backup_word = str_replace($takeoff,"_",moodle_strtolower(get_string("backupfilename")));
//If non-translated, use "backup"
if (substr($backup_word,0,1) == "[") {
$backup_word= "backup";
}
//Calculate the date format string
$backup_date_format = str_replace(" ","_",get_string("backupnameformat"));
//If non-translated, use "%Y%m%d-%H%M"
if (substr($backup_date_format,0,1) == "[") {
$backup_date_format = "%%Y%%m%%d-%%H%%M";
}
//Calculate the shortname
$backup_shortname = clean_filename($course->shortname);
if (empty($backup_shortname) or $backup_shortname == '_' ) {
$backup_shortname = $course->id;
}
//Calculate the final backup filename
//The backup word
$backup_name = $backup_word."-";
//The shortname
$backup_name .= moodle_strtolower($backup_shortname)."-";
//The date format
$backup_name .= userdate(time(),$backup_date_format,99,false);
//The extension
$backup_name .= ".zip";
//And finally, clean everything
$backup_name = clean_filename($backup_name);
//Calculate the backup unique code to allow simultaneus backups (to define
//the temp-directory name and records in backup temp tables
$backup_name = backup_get_zipfile_name($course, $backup_unique_code);
?>
</table>
@@ -328,4 +294,4 @@ function selectItemInCheckboxByName(formId, checkName, checked ) {
<input type="submit" value="<?php print_string("continue") ?>" />
<input type="submit" name="cancel" value="<?php print_string("cancel") ?>" />
</div>
</form>
</form>
+534
View File
@@ -2330,4 +2330,538 @@
}
fwrite ($bf, end_tag("ROLES_ASSIGNMENTS", $startlevel, true));
}
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')) {
error ("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 quizzes, backup categories and
//questions structure (step 1). See notes on mod/quiz/backuplib.php
if ($status and !empty($preferences->mods['quiz']->backup)) {
if (!defined('BACKUP_SILENTLY')) {
echo "<li>".get_string("writingcategoriesandquestions").'</li>';
}
require_once($CFG->dirroot.'/mod/quiz/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 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 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 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, 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;
}
/**
* This function generates the default zipfile name for a backup
* based on the course id and the unique code.
*
* @param object $course course object
* @param string $backup_unique_code (optional, if left out current timestamp used)
*
* @return string filename (excluding path information)
*/
function backup_get_zipfile_name($course, $backup_unique_code='') {
if (empty($backup_unique_code)) {
$backup_unique_code = time();
}
//Calculate the backup word
//Take off some characters in the filename !!
$takeoff = array(" ", ":", "/", "\\", "|");
$backup_word = str_replace($takeoff,"_",moodle_strtolower(get_string("backupfilename")));
//If non-translated, use "backup"
if (substr($backup_word,0,1) == "[") {
$backup_word= "backup";
}
//Calculate the date format string
$backup_date_format = str_replace(" ","_",get_string("backupnameformat"));
//If non-translated, use "%Y%m%d-%H%M"
if (substr($backup_date_format,0,1) == "[") {
$backup_date_format = "%%Y%%m%%d-%%H%%M";
}
//Calculate the shortname
$backup_shortname = clean_filename($course->shortname);
if (empty($backup_shortname) or $backup_shortname == '_' ) {
$backup_shortname = $course->id;
}
//Calculate the final backup filename
//The backup word
$backup_name = $backup_word."-";
//The shortname
$backup_name .= moodle_strtolower($backup_shortname)."-";
//The date format
$backup_name .= userdate(time(),$backup_date_format,99,false);
//The extension
$backup_name .= ".zip";
//And finally, clean everything
$backup_name = clean_filename($backup_name);
return $backup_name;
}
/**
* This function adds on the standard items to the preferences
* Like moodle version and backup version
*
* @param object $preferences existing preferences object.
* (passed by reference)
*/
function backup_add_static_preferences(&$preferences) {
global $CFG;
$preferences->moodle_version = $CFG->version;
$preferences->moodle_release = $CFG->release;
$preferences->backup_version = $CFG->backup_version;
$preferences->backup_release = $CFG->backup_release;
}
?>
+145 -6
View File
@@ -104,6 +104,12 @@
//Copied from the web !!
function delete_dir_contents ($dir,$excludeddir="") {
if (!is_dir($dir)) {
// if we've been given a directory that doesn't exist yet, return true.
// this happens when we're trying to clear out a course that has only just
// been created.
return true;
}
$slash = "/";
// Create arrays to store files and directories
@@ -599,10 +605,15 @@
* @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) {
function import_backup_file_silently($pathtofile,$destinationcourse,$emptyfirst=false,$userdata=false, $preferences=array()) {
global $CFG,$SESSION,$USER; // is there such a thing on cron? I guess so..
global $restore; // ick
if (empty($USER)) {
$USER = get_admin();
$USER->admin = 1; // not sure why, but this doesn't get set
@@ -666,12 +677,24 @@
return false;
}
restore_setup_for_check($SESSION->restore,$backup_unique_code);
$SESSION->restore = new StdClass;
// add on some extra stuff we need...
$SESSION->restore->restoreto = 1;
$SESSION->restore->course_id = $destinationcourse;
$SESSION->restore->deleting = $emptyfirst;
$SESSION->restore->metacourse = $restore->metacourse = (isset($preferences['restore_metacourse']) ? $preferences['restore_metacourse'] : 0);
$SESSION->restore->restoreto = $restore->restoreto = 1;
$SESSION->restore->users = $restore->users = $userdata;
$SESSION->restore->logs = $restore->logs = (isset($preferences['restore_logs']) ? $preferences['restore_logs'] : 0);
$SESSION->restore->user_files = $restore->user_files = $userdata;
$SESSION->restore->messages = $restore->messages = (isset($preferences['restore_messages']) ? $preferences['restore_messages'] : 0);
$SESSION->restore->course_id = $restore->course_id = $destinationcourse;
$SESSION->restore->restoreto = 1;
$SESSION->restore->course_id = $destinationcourse;
$SESSION->restore->deleting = $emptyfirst;
$SESSION->restore->restore_course_files = $restore->course_files = (isset($preferences['restore_course_files']) ? $preferences['restore_course_files'] : 0);
$SESSION->restore->backup_version = $SESSION->info->backup_backup_version;
$SESSION->restore->course_startdateoffset = $course->startdate - $SESSION->course_header->course_startdate;
restore_setup_for_check($SESSION->restore,$backup_unique_code);
// maybe we need users (defaults to 2 in restore_setup_for_check)
if (!empty($userdata)) {
@@ -687,8 +710,14 @@
$SESSION->restore->mods[$modname]->restore = true;
$SESSION->restore->mods[$modname]->userinfo = $userdata;
}
else {
// avoid warnings
$SESSION->restore->mods[$modname]->restore = false;
$SESSION->restore->mods[$modname]->userinfo = false;
}
}
}
$restore = clone($SESSION->restore);
if (!restore_execute($SESSION->restore,$SESSION->info,$SESSION->course_header,$errorstr)) {
mtrace($debuginfo.'Failed restore_execute (error was '.$errorstr.')');
return false;
@@ -696,4 +725,114 @@
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; // global preferences here because something else wants it :(
define('BACKUP_SILENTLY', 1);
if (!$course = get_record('course', 'id', $courseid)) {
debugging("Couldn't find course with id $courseid in backup_course_silently");
return false;
}
$preferences = backup_generate_preferences_artificially($course, $prefs);
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_messages
* and if not provided, they will not be included.
*/
function backup_generate_preferences_artificially($course, $prefs) {
global $CFG;
$preferences = new StdClass;
$preferences->backup_unique_code = time();
$preferences->backup_name = backup_get_zipfile_name($course, $preferences->backup_unique_code);
$count = 0;
if ($allmods = 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 (!count_records('course_modules','course',$course->id,'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);
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 = true;
$preferences->mods[$modname]->instances[$instance->id]->userinfo = true;
$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 = true;
$preferences->mods[$modname]->userinfo = true;
}
}
//Check other parameters
$preferences->backup_metacourse = (isset($prefs['backup_metacourse']) ? $prefs['backup_metacourse'] : 0);
$preferences->backup_users = (isset($prefs['backup_users']) ? $prefs['backup_users'] : 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_messages = (isset($prefs['backup_messages']) ? $prefs['backup_messages'] : 0);
$preferences->backup_course = $course->id;
backup_add_static_preferences($preferences);
return $preferences;
}
?>
+12 -1
View File
@@ -6328,7 +6328,18 @@
}
if ($status = restore_close_html($restore)){
echo '<li>Closing the Restorelog.html file.</li>';
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");
}
else {
$errorstr = "Could not close the restorelog.html file";
return false;
}
}
if (!defined('RESTORE_SILENTLY')) {