Abstracted all the stuff that gets backup prefs from request, and put

it into session. This makes it easier to add a new stages
	and not have to write hidden fields and such like
This commit is contained in:
mjollnir_
2006-01-16 00:39:01 +00:00
parent 11c8a91aeb
commit a94baeaf20
5 changed files with 187 additions and 214 deletions
+2
View File
@@ -116,6 +116,8 @@
//Call the form, depending the step we are
if (!$launch) {
// if we're at the start, clear the cache of prefs
unset($SESSION->backupprefs[$course->id]);
include_once("backup_form.html");
} else if ($launch == "check") {
include_once("backup_check.html");
+38 -129
View File
@@ -20,63 +20,16 @@
}
}
$backupprefs = new StdClass;
$count = 0;
backup_fetch_prefs_from_request($backupprefs,$count,$course);
//Check site
if (!$site = get_site()) {
error("Site not found!");
}
//Checks for the required files/functions to backup every mod
//And check if there is data about it
$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)) {
include_once($modfile);
if (function_exists($modbackup) and function_exists($modcheckbackup)) {
$var = "exists_".$modname;
$$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)) {
$var = 'exists_one_'.$modname;
$$var = true;
$varname = $modname.'_instances';
$$varname = get_all_instances_in_course($modname,$course);
foreach ($$varname as $instance) {
$var = 'backup_'.$modname.'_instance_'.$instance->id;
$$var = optional_param($var,0);
$var = 'backup_user_info_'.$modname.'_instance_'.$instance->id;
$$var = optional_param($var,0);
$var = 'backup_'.$modname.'_instances';
$$var = 1; // we need this later to determine what to display in modcheckbackup.
}
}
}
}
//Check data
//Check module info
$var = "backup_".$modname;
$$var = optional_param( $var,0);
//Check include user info
$var = "backup_user_info_".$modname;
$$var = optional_param( $var,0);
}
}
//Check other parameters
$backup_metacourse = optional_param('backup_metacourse',1);
$backup_users = optional_param('backup_users',1);
$backup_logs = optional_param('backup_logs',0);
$backup_user_files = optional_param('backup_user_files',1);
$backup_course_files = optional_param('backup_course_files',1);
$backup_messages = optional_param('backup_messages',1);
if ($count == 0) {
notice("No backupable modules are installed!");
}
@@ -86,46 +39,6 @@
<table cellpadding="5">
<?php
//Calculate the backup string
//Calculate the backup word
//Take off some characters in the filename !!
$takeoff = array(" ", ":", "/", "\\", "|");
$backup_word = str_replace($takeoff,"_",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 .= 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_unique_code = time();
if (empty($to)) {
//Now print the Backup Name tr
echo "<tr>";
@@ -133,7 +46,7 @@
echo get_string("name").":";
echo "</b></td><td>";
//Add as text field
echo "<input type=\"text\" name=\"backup_name\" size=\"40\" value=\"".$backup_name."\" />";
echo "<input type=\"text\" name=\"backup_name\" size=\"40\" value=\"".$backupprefs->backup_name."\" />";
echo "</td></tr>";
//Line
@@ -143,32 +56,30 @@
echo "<tr>";
echo "<td colspan=\"2\" align=\"center\"><b>";
} else {
echo '<input type="hidden" name="backup_name" value="'.$backup_name.'" /></b>';
}
//Here we check if backup_users = None. Then, we switch off every module
//user info, user_files, logs and exercises, workshop and messages backups. A Warning is showed to
//inform the user.
if ($backup_users == 2) {
if ($backupprefs->backup_users == 2) {
if ($allmods = get_records("modules") ) {
foreach ($allmods as $mod) {
$modname = $mod->name;
$var = "backup_user_info_".$modname;
if (isset($$var)) {
$$var = 0;
if (isset($backupprefs->$var)) {
$backupprefs->$var = 0;
}
$var = "backup_".$modname;
if (isset($$var)) {
if (isset($backupprefs->$var)) {
if ($modname == "exercise" || $modname == "workshop") {
$$var = 0;
$backupprefs->$var = 0;
}
}
}
$backup_user_files = 0;
$backup_logs = 0;
$backup_messages = 0;
$backupprefs->backup_user_files = 0;
$backupprefs->backup_logs = 0;
$backupprefs->backup_messages = 0;
}
print_simple_box("<font color=\"red\">".get_string("backupnoneusersinfo")."</font>","center", "70%", '', "20", "noticebox");
echo "<hr />";
@@ -185,21 +96,24 @@
if ($allmods = get_records("modules") ) {
foreach ($allmods as $mod) {
$modname = $mod->name;
$modfile = $CFG->dirroot.'/mod/'.$modname.'/backuplib.php';
if (!file_exists($modfile)) {
continue;
}
require_once($modfile);
$modbackup = $modname."_backup_mods";
//If exists the lib & function
$var = "exists_".$modname;
if (isset($$var) && $$var) {
if (isset($backupprefs->$var) && $backupprefs->$var) {
$var = "backup_".$modname;
//Only if selected
if ($$var == 1) {
if ($backupprefs->$var == 1) {
//Print the full tr
echo "<tr>";
echo "<td colspan=\"2\">";
//Add hidden fields
$var = "backup_".$modname;
echo "<input type=\"hidden\" name=\"".$var."\" value=\"".$$var."\" />";
$var = "backup_user_info_".$modname;
echo "<input type=\"hidden\" name=\"".$var."\" value=\"".$$var."\" />";
//Print the mod name
echo "<b>".get_string("include")." ".get_string("modulenameplural",$modname)." ";
//Now look for user-data status
@@ -207,31 +121,29 @@
$backup_user_options[1] = get_string("withuserdata");
$var = "backup_user_info_".$modname;
//Print the user info
echo $backup_user_options[$$var]."</b>";
echo $backup_user_options[$backupprefs->$var]."</b>";
//Call the check function to show more info
$modcheckbackup = $modname."_check_backup_mods";
$var = $modname.'_instances';
$instancestopass = array();
if (!empty($$var) && is_array($$var) && count($$var)) {
if (!empty($backupprefs->$var) && is_array($backupprefs->$var) && count($backupprefs->$var)) {
$table->data = array();
$countinstances = 0;
foreach ($$var as $instance) {
foreach ($backupprefs->$var as $instance) {
$var1 = 'backup_'.$modname.'_instance_'.$instance->id;
$var2 = 'backup_user_info_'.$modname.'_instance_'.$instance->id;
if (!empty($$var1)) {
if (!empty($backupprefs->$var1)) {
$obj = new StdClass;
$obj->name = $instance->name;
$obj->userdata = $$var2;
$obj->userdata = $backupprefs->$var2;
$obj->id = $instance->id;
$instancestopass[$instance->id]= $obj;
$countinstances++;
echo '<input type="hidden" name="'.$var1.'" value="'.$$var1.'" />';
echo '<input type="hidden" name="'.$var2.'" value="'.$$var2.'" />';
}
}
}
$table->data = $modcheckbackup($id,$$var,$backup_unique_code,$instancestopass);
$table->data = $modcheckbackup($id,$backupprefs->$var,$backupprefs->backup_unique_code,$instancestopass);
print_table($table);
echo "</td></tr>";
}
@@ -249,16 +161,16 @@
$user_options[0] = get_string("includeallusers");
$user_options[1] = get_string("includecourseusers");
$user_options[2] = get_string("includenoneusers");
echo $user_options[$backup_users].'</b>';
echo $user_options[$backupprefs->backup_users].'</b>';
//Print info
$table->data = user_check_backup($id,$backup_unique_code,$backup_users,$backup_messages);
$table->data = user_check_backup($id,$backupprefs->backup_unique_code,$backupprefs->backup_users,$backupprefs->backup_messages);
print_table($table);
echo "</td></tr>";
}
//Now print the Logs tr conditionally
if ($backup_logs && empty($to)) {
if ($backupprefs->backup_logs && empty($to)) {
echo "<tr>";
echo "<td colspan=\"2\"><b>";
echo get_string("includelogentries").'</b>';
@@ -269,38 +181,35 @@
}
//Now print the User Files tr conditionally
if ($backup_user_files) {
if ($backupprefs->backup_user_files) {
echo "<tr>";
echo "<td colspan=\"2\"><b>";
echo get_string("includeuserfiles").'</b>';
//Print info
$table->data = user_files_check_backup($id,$backup_unique_code);
$table->data = user_files_check_backup($id,$backupprefs->backup_unique_code);
print_table($table);
echo "</td></tr>";
}
//Now print the Course Files tr conditionally
if ($backup_course_files) {
if ($backupprefs->backup_course_files) {
echo "<tr>";
echo "<td colspan=\"2\"><b>";
echo get_string("includecoursefiles").'</b>';
//Print info
$table->data = course_files_check_backup($id,$backup_unique_code);
$table->data = course_files_check_backup($id,$backupprefs->backup_unique_code);
print_table($table);
echo "</td></tr>";
}
}
// now keep it for next time.
$SESSION->backupprefs[$course->id] = $backupprefs;
?>
</table>
<br />
<center>
<input type="hidden" name="backup_unique_code" value="<?php p($backup_unique_code) ?>" />
<input type="hidden" name="backup_metacourse" value="<?php p($backup_metacourse) ?>" />
<input type="hidden" name="backup_users" value="<?php p($backup_users) ?>" />
<input type="hidden" name="backup_logs" value="<?php p($backup_logs) ?>" />
<input type="hidden" name="backup_user_files" value="<?php p($backup_user_files) ?>" />
<input type="hidden" name="backup_course_files" value="<?php p($backup_course_files) ?>" />
<input type="hidden" name="backup_messages" value="<?php p($backup_messages) ?>" />
<input type="hidden" name="to" value="<?php p($to) ?>" />
<input type="hidden" name="id" value="<?php p($id) ?>" />
<input type="hidden" name="launch" value="execute" />
+8 -84
View File
@@ -26,87 +26,8 @@
error("Site not found!");
}
//Checks for the required files/functions to backup every mod
//And check if there is data about it
$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)) {
include_once($modfile);
if (function_exists($modbackup) and function_exists($modcheckbackup)) {
$var = "exists_".$modname;
$$var = true;
$count++;
if (count_records('course_modules','course',$course->id,'module',$mod->id) && function_exists($modbackupone)) {
$var = 'exists_one_'.$modname;
$$var = true;
$varname = $modname.'_instances';
$$varname = get_all_instances_in_course($modname,$course);
foreach ($$varname as $instance) {
$preferences->mods[$modname]->instances[$instance->id]->name = $instance->name;
$var = 'backup_'.$modname.'_instance_'.$instance->id;
$$var = optional_param($var,0);
$preferences->mods[$modname]->instances[$instance->id]->backup = $$var;
$var = 'backup_user_info_'.$modname.'_instance_'.$instance->id;
$$var = optional_param($var,0);
$preferences->mods[$modname]->instances[$instance->id]->userinfo = $$var;
}
}
}
}
//Check data
//Check module info
$var = "backup_".$modname;
$$var = optional_param( $var,0,PARAM_INT );
//Now stores all the mods preferences into an array into preferences
$preferences->mods[$modname]->backup = $$var;
//Check include user info
$var = "backup_user_info_".$modname;
$$var = optional_param( $var,0,PARAM_INT );
//Now stores all the mods preferences into an array into preferences
$preferences->mods[$modname]->userinfo = $$var;
//And the name of the mod
$preferences->mods[$modname]->name = $modname;
}
}
//Check other parameters
$backup_metacourse = optional_param( 'backup_metacourse',1,PARAM_INT );
$preferences->backup_metacourse = $backup_metacourse;
$backup_users = optional_param( 'backup_users',1,PARAM_INT );
$preferences->backup_users = $backup_users;
$backup_logs = optional_param( 'backup_logs',0,PARAM_INT );
$preferences->backup_logs = $backup_logs;
$backup_user_files = optional_param( 'backup_user_files',1,PARAM_INT );
$preferences->backup_user_files = $backup_user_files;
$backup_course_files = optional_param( 'backup_course_files',2,PARAM_INT );
$preferences->backup_course_files = $backup_course_files;
$backup_messages = optional_param( 'backup_messages',1,PARAM_INT );
$preferences->backup_messages = $backup_messages;
$id = required_param('id', PARAM_INT);
$preferences->backup_course = $id;
$backup_name = required_param('backup_name',PARAM_FILE );
$preferences->backup_name = $backup_name;
$backup_unique_code = required_param('backup_unique_code', PARAM_INT);
$preferences->backup_unique_code = $backup_unique_code;
$preferences = new StdClass;
backup_fetch_prefs_from_request($preferences,$count,$course);
//Another Info
$preferences->moodle_version = $CFG->version;
$preferences->moodle_release = $CFG->release;
@@ -140,10 +61,10 @@
//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($backup_unique_code);
$status = check_and_create_backup_dir($preferences->backup_unique_code);
//Empty dir
if ($status) {
$status = clear_backup_dir($backup_unique_code);
$status = clear_backup_dir($preferences->backup_unique_code);
}
//Delete old_entries from backup tables
@@ -160,7 +81,7 @@
echo "<ul>";
echo "<li>".get_string("writingheader").'</li>';
//Obtain the xml file (create and open) and print prolog information
$backup_file = backup_open_xml($backup_unique_code);
$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) {
@@ -226,6 +147,7 @@
//questions structure (step 1). See notes on mod/quiz/backuplib.php
if ($status and $preferences->mods['quiz']->backup) {
echo "<li>".get_string("writingcategoriesandquestions").'</li>';
require_once($CFG->dirroot.'/mod/quiz/backuplib.php');
if (!$status = quiz_backup_question_categories($backup_file,$preferences)) {
notify("An error occurred while backing up quiz categories");
}
@@ -398,4 +320,6 @@
$SESSION->import_preferences = $preferences;
print_continue($CFG->wwwroot.'/course/import/activities/index.php?id='.$to.'&fromcourse='.$id.'&filename='.$filename);
}
$SESSION->backupprefs[$course->id] = null; // unset it so we're clear next time.
?>
+43
View File
@@ -276,12 +276,55 @@ function selectItemInCheckboxByName(formId, checkName, checked ) {
echo '<input type="hidden" name="backup_messages" value="0" />';
}
}
$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,"_",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 .= 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
?>
</table>
<br />
<center>
<input type="hidden" name="id" value="<?php p($id) ?>" />
<input type="hidden" name="to" value="<?php p($to) ?>" />
<input type="hidden" name="backup_unique_code" value="<?php p($backup_unique_code); ?>" />
<input type="hidden" name="backup_name" value="<?php p($backup_name); ?>" />
<input type="hidden" name="launch" value="check" />
<input type="submit" value="<?php print_string("continue") ?>" />
<input type="submit" name="cancel" value="<?php print_string("cancel") ?>" />
+96 -1
View File
@@ -498,7 +498,7 @@
$included = "true";
if ($element->userinfo) {
$userinfo = "true";
}
}
}
//Prints the mod start
fwrite ($bf,start_tag("MOD",3,true));
@@ -1646,6 +1646,8 @@
$status = true;
require_once($CFG->dirroot.'/mod/'.$module.'/backuplib.php');
if (is_array($preferences->mods[$module]->instances)) {
$onemodbackup = $module.'_backup_one_mod';
if (function_exists($onemodbackup)) {
@@ -1927,4 +1929,97 @@
&& !empty($preferences->mods[$modname]->instances[$modid]->backup)));
}
/*
* Checks for the required files/functions to backup every mod
* And check if there is data about it
*/
function backup_fetch_prefs_from_request(&$preferences,&$count,$course) {
global $CFG,$SESSION;
// check to see if it's in the session already
if (!empty($SESSION->backupprefs) && array_key_exists($course->id,$SESSION->backupprefs) && !empty($SESSION->backupprefs[$course->id])) {
$sprefs = $SESSION->backupprefs[$course->id];
$preferences = $sprefs;
// refetch backup_name just in case.
$bn = optional_param('backup_name','',PARAM_FILE);
if (!empty($bn)) {
$preferences->backup_name = $bn;
}
$count = 1;
return true;
}
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;
$$var = optional_param($var,0);
$preferences->$var = $$var;
$preferences->mods[$modname]->instances[$instance->id]->backup = $$var;
$var = 'backup_user_info_'.$modname.'_instance_'.$instance->id;
$$var = optional_param($var,0);
$preferences->$var = $$var;
$preferences->mods[$modname]->instances[$instance->id]->userinfo = $$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;
$$var = optional_param( $var,0);
$preferences->$var = $$var;
$preferences->mods[$modname]->backup = $$var;
//Check include user info
$var = "backup_user_info_".$modname;
$$var = optional_param( $var,0);
$preferences->$var = $$var;
$preferences->mods[$modname]->userinfo = $$var;
}
}
//Check other parameters
$preferences->backup_metacourse = optional_param('backup_metacourse',1,PARAM_INT);
$preferences->backup_users = optional_param('backup_users',1,PARAM_INT);
$preferences->backup_logs = optional_param('backup_logs',0,PARAM_INT);
$preferences->backup_user_files = optional_param('backup_user_files',1,PARAM_INT);
$preferences->backup_course_files = optional_param('backup_course_files',1,PARAM_INT);
$preferences->backup_messages = optional_param('backup_messages',1,PARAM_INT);
$preferences->backup_course = $course->id;
$preferences->backup_name = required_param('backup_name',PARAM_FILE );
$preferences->backup_unique_code = required_param('backup_unique_code');
// put it (back) in the session
$SESSION->backupprefs[$course->id] = $preferences;
}
?>