Added backup and restore features

This commit is contained in:
bobopinna
2004-06-07 13:28:59 +00:00
parent 16315dddf0
commit dde32eea69
2 changed files with 473 additions and 0 deletions
+208
View File
@@ -0,0 +1,208 @@
<?PHP //$Id$
//This php script contains all the stuff to backup/restore
//scorm mods
//This is the "graphical" structure of the scorm mod:
//
// scorm
// (CL,pk->id)--------------------
// | |
// | |
// | |
// scorm_scoes |
// (UL,pk->id, fk->scorm) |
// | |
// | |
// | |
// scorm_sco_users |
// (UL,pk->id, fk->scormid, fk->scoid)----
//
// Meaning: pk->primary key field of the table
// fk->foreign key to link with parent
// nt->nested field (recursive data)
// CL->course level info
// UL->user level info
// files->table may have files)
//
//-----------------------------------------------------------
function scorm_backup_mods($bf,$preferences) {
global $CFG;
$status = true;
//Iterate over scorm table
$scorms = get_records ("scorm","course",$preferences->backup_course,"id");
if ($scorms) {
foreach ($scorms as $scorm) {
//Start mod
fwrite ($bf,start_tag("MOD",3,true));
//Print scorm data
fwrite ($bf,full_tag("ID",4,false,$scorm->id));
fwrite ($bf,full_tag("MODTYPE",4,false,"scorm"));
fwrite ($bf,full_tag("NAME",4,false,$scorm->name));
fwrite ($bf,full_tag("REFERENCE",4,false,$scorm->reference));
fwrite ($bf,full_tag("DATADIR",4,false,$scorm->datadir));
fwrite ($bf,full_tag("LAUNCH",4,false,$scorm->launch));
fwrite ($bf,full_tag("SUMMARY",4,false,$scorm->summary));
fwrite ($bf,full_tag("AUTO",4,false,$scorm->auto));
fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$scorm->timemodified));
$status = backup_scorm_scoes($bf,$preferences,$scorm->id);
//if we've selected to backup users info, then execute backup_scorm_sco_users
if ($status) {
if ($preferences->mods["scorm"]->userinfo) {
$status = backup_scorm_sco_users($bf,$preferences,$scorm->id);
}
}
//End mod
$status =fwrite ($bf,end_tag("MOD",3,true));
}
//backup scorm files
if ($status) {
$status = backup_scorm_files($bf,$preferences);
}
}
return $status;
}
//Backup scorm_scoes contents (executed from scorm_backup_mods)
function backup_scorm_scoes ($bf,$preferences,$scorm) {
global $CFG;
$status = true;
$scorm_scoes = get_records("scorm_scoes","scorm",$scorm,"id");
//If there is submissions
if ($scorm_scoes) {
//Write start tag
$status =fwrite ($bf,start_tag("SCOES",4,true));
//Iterate over each sco
foreach ($scorm_scoes as $sco) {
//Start sco
$status =fwrite ($bf,start_tag("SCO",5,true));
//Print submission contents
fwrite ($bf,full_tag("ID",6,false,$sco->id));
fwrite ($bf,full_tag("PARENT",6,false,$sco->parent));
fwrite ($bf,full_tag("IDENTIFIER",6,false,$sco->identifier));
fwrite ($bf,full_tag("LAUNCH",6,false,$sco->launch));
fwrite ($bf,full_tag("TYPE",6,false,$sco->type));
fwrite ($bf,full_tag("TITLE",6,false,$sco->title));
fwrite ($bf,full_tag("NEXT",6,false,$sco->next));
fwrite ($bf,full_tag("PREVIOUS",6,false,$sco->previous));
//End sco
$status =fwrite ($bf,end_tag("SCO",5,true));
}
//Write end tag
$status =fwrite ($bf,end_tag("SCOES",4,true));
}
return $status;
}
//Backup scorm_sco_users contents (executed from scorm_backup_mods)
function backup_scorm_sco_users ($bf,$preferences,$scorm) {
global $CFG;
$status = true;
$scorm_sco_users = get_records("scorm_sco_users","scormid",$scorm,"id");
//If there is submissions
if ($scorm_sco_users) {
//Write start tag
$status =fwrite ($bf,start_tag("SCO_USERS",4,true));
//Iterate over each sco
foreach ($scorm_sco_users as $sco_user) {
//Start sco
$status =fwrite ($bf,start_tag("SCO_USER",5,true));
//Print submission contents
fwrite ($bf,full_tag("ID",6,false,$sco_user->id));
fwrite ($bf,full_tag("USERID",6,false,$sco_user->userid));
fwrite ($bf,full_tag("SCOID",6,false,$sco_user->scoid));
fwrite ($bf,full_tag("CMI_CORE_LESSON_LOCATION",6,false,$sco_user->cmi_core_lesson_location));
fwrite ($bf,full_tag("CMI_CORE_LESSON_STATUS",6,false,$sco_user->cmi_core_lesson_status));
fwrite ($bf,full_tag("CMI_CORE_EXIT",6,false,$sco_user->cmi_core_exit));
fwrite ($bf,full_tag("CMI_CORE_TOTAL_TIME",6,false,$sco_user->cmi_core_total_time));
fwrite ($bf,full_tag("CMI_CORE_SESSION_TIME",6,false,$sco_user->cmi_core_session_time));
fwrite ($bf,full_tag("CMI_CORE_SCORE_RAW",6,false,$sco_user->cmi_core_score_raw));
fwrite ($bf,full_tag("CMI_SUSPEND_DATA",6,false,$sco_user->cmi_suspend_data));
fwrite ($bf,full_tag("CMI_LAUNCH_DATA",6,false,$sco_user->cmi_launch_data));
//End sco
$status =fwrite ($bf,end_tag("SCO_USER",5,true));
}
//Write end tag
$status =fwrite ($bf,end_tag("SCO_USERS",4,true));
}
return $status;
}
////Return an array of info (name,value)
function scorm_check_backup_mods($course,$user_data=false,$backup_unique_code) {
//First the course data
$info[0][0] = get_string("modulenameplural","scorm");
if ($ids = scorm_ids ($course)) {
$info[0][1] = count($ids);
} else {
$info[0][1] = 0;
}
//Now, if requested, the user_data
if ($user_data) {
$info[1][0] = get_string("scoes","scorm");
if ($ids = scorm_sco_users_ids_by_course ($course)) {
$info[1][1] = count($ids);
} else {
$info[1][1] = 0;
}
}
return $info;
}
//Backup scorm package files
function backup_scorm_files($bf,$preferences) {
global $CFG;
$status = true;
//First we check to moddata exists and create it as necessary
//in temp/backup/$backup_code dir
$status = check_and_create_moddata_dir($preferences->backup_unique_code);
//Now copy the scorm dir
if ($status) {
if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/scorm")) {
$status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/scorm",
$CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/scorm");
}
}
return $status;
}
// INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
//Returns an array of scorms id
function scorm_ids ($course) {
global $CFG;
return get_records_sql ("SELECT a.id, a.course
FROM {$CFG->prefix}scorm a
WHERE a.course = '$course'");
}
//Returns an array of scorm_scoes id
function scorm_sco_users_ids_by_course ($course) {
global $CFG;
return get_records_sql ("SELECT s.id , s.scormid
FROM {$CFG->prefix}scorm_sco_users s,
{$CFG->prefix}scorm a
WHERE a.course = '$course' AND
s.scormid = a.id");
}
?>
+265
View File
@@ -0,0 +1,265 @@
<?PHP //$Id$
//This php script contains all the stuff to backup/restore
//reservation mods
//This is the "graphical" structure of the scorm mod:
//
// scorm
// (CL,pk->id)--------------------
// | |
// | |
// | |
// scorm_scoes |
// (UL,pk->id, fk->scorm) |
// | |
// | |
// | |
// scorm_sco_users |
// (UL,pk->id, fk->scormid, fk->scoid)----
//
// Meaning: pk->primary key field of the table
// fk->foreign key to link with parent
// nt->nested field (recursive data)
// CL->course level info
// UL->user level info
// files->table may have files)
//
//-----------------------------------------------------------
//This function executes all the restore procedure about this mod
function scorm_restore_mods($mod,$restore) {
global $CFG;
$status = true;
//Get record from backup_ids
$data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
if ($data) {
//Now get completed xmlized object
$info = $data->info;
//traverse_xmlize($info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//Now, build the SCORM record structure
$scorm->course = $restore->course_id;
$scorm->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
$scorm->reference = backup_todb($info['MOD']['#']['REFERENCE']['0']['#']);
$scorm->datadir = backup_todb($info['MOD']['#']['DATADIR']['0']['#']);
$scorm->launch = backup_todb($info['MOD']['#']['LAUNCH']['0']['#']);
$scorm->summary = backup_todb($info['MOD']['#']['SUMMARY']['0']['#']);
$scorm->auto = backup_todb($info['MOD']['#']['AUTO']['0']['#']);
$scorm->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
//The structure is equal to the db, so insert the scorm
$newid = insert_record ("scorm",$scorm);
//Do some output
echo "<ul><li>".get_string("modulename","scorm")." \"".$scorm->name."\"<br>";
backup_flush(300);
if ($newid) {
//We have the newid, update backup_ids
backup_putid($restore->backup_unique_code,$mod->modtype,
$mod->id, $newid);
//Now copy moddata associated files
$status = scorm_restore_files ($scorm->datadir, $restore);
if ($status)
$status = scorm_scoes_restore_mods ($newid,$info,$restore);
//Now check if want to restore user data and do it.
if ($restore->mods['scorm']->userinfo) {
//Restore scorm_scoes
if ($status)
$status = scorm_sco_users_restore_mods ($newid,$info,$restore);
}
} else {
$status = false;
}
//Finalize ul
echo "</ul>";
} else {
$status = false;
}
return $status;
}
//This function restores the scorm_scoes
function scorm_scoes_restore_mods($scorm_id,$info,$restore) {
global $CFG;
$status = true;
//Get the sco array
$scoes = $info['MOD']['#']['SCOES']['0']['#']['SCO'];
//Iterate over scoes
for($i = 0; $i < sizeof($scoes); $i++) {
$sub_info = $scoes[$i];
//We'll need this later!!
$oldid = backup_todb($sub_info['#']['ID']['0']['#']);
//Now, build the scorm_scoes record structure
$sco->scorm = $scorm_id;
$sco->parent = backup_todb($sub_info['#']['PARENT']['0']['#']);
$sco->identifier = backup_todb($sub_info['#']['IDENTIFIER']['0']['#']);
$sco->launch = backup_todb($sub_info['#']['LAUNCH']['0']['#']);
$sco->type = backup_todb($sub_info['#']['TYPE']['0']['#']);
$sco->title = backup_todb($sub_info['#']['TITLE']['0']['#']);
$sco->next = backup_todb($sub_info['#']['NEXT']['0']['#']);
$sco->previous = backup_todb($sub_info['#']['PREVIOUS']['0']['#']);
//The structure is equal to the db, so insert the scorm_scoes
$newid = insert_record ("scorm_scoes",$sco);
//Do some output
if (($i+1) % 50 == 0) {
echo ".";
if (($i+1) % 1000 == 0) {
echo "<br>";
}
backup_flush(300);
}
if ($newid) {
//We have the newid, update backup_ids
backup_putid($restore->backup_unique_code,"scorm_scoes", $oldid, $newid);
} else {
$status = false;
}
}
return $status;
}
//This function restores the scorm_sco_users
function scorm_sco_users_restore_mods($scorm_id,$info,$restore) {
global $CFG;
$status = true;
$sco_users = NULL;
//Get the sco array
if (!empty($info['MOD']['#']['SCO_USERS']['0']['#']['SCO_USER']))
$sco_users = $info['MOD']['#']['SCO_USERS']['0']['#']['SCO_USER'];
//Iterate over sco_users
for($i = 0; $i < sizeof($sco_users); $i++) {
$sub_info = $sco_users[$i];
//We'll need this later!!
$oldid = backup_todb($sub_info['#']['ID']['0']['#']);
$oldscoid = backup_todb($sub_info['#']['SCOID']['0']['#']);
$olduserid = backup_todb($sub_info['#']['USERID']['0']['#']);
//Now, build the scorm_sco_users record structure
$sco_user->scormid = $scorm_id;
$sco_user->userid = backup_todb($sub_info['#']['USERID']['0']['#']);
$sco_user->scoid = backup_todb($sub_info['#']['SCOID']['0']['#']);
$sco_user->cmi_core_lesson_location = backup_todb($sub_info['#']['CMI_CORE_LESSON_LOCATION']['0']['#']);
$sco_user->cmi_core_lesson_status = backup_todb($sub_info['#']['CMI_CORE_LESSON_STATUS']['0']['#']);
$sco_user->cmi_core_exit = backup_todb($sub_info['#']['CMI_CORE_EXIT']['0']['#']);
$sco_user->cmi_core_total_time = backup_todb($sub_info['#']['CMI_CORE_TOTAL_TIME']['0']['#']);
$sco_user->cmi_core_session_time = backup_todb($sub_info['#']['CMI_CORE_SESSION_TIME']['0']['#']);
$sco_user->cmi_core_score_raw = backup_todb($sub_info['#']['CMI_CORE_SCORE_RAW']['0']['#']);
$sco_user->cmi_suspend_data = backup_todb($sub_info['#']['CMI_SUSPEND_DATA']['0']['#']);
$sco_user->cmi_launch_data = backup_todb($sub_info['#']['CMI_LAUNCH_DATA']['0']['#']);
//We have to recode the userid field
$user = backup_getid($restore->backup_unique_code,"user",$sco_user->userid);
if ($user) {
$sco_user->userid = $user->new_id;
}
//We have to recode the scoid field
$sco = backup_getid($restore->backup_unique_code,"scorm_scoes",$sco_user->scoid);
if ($sco) {
$sco_user->scoid = $sco->new_id;
}
//The structure is equal to the db, so insert the scorm_sco_users
$newid = insert_record ("scorm_sco_users",$sco_user);
//Do some output
if (($i+1) % 50 == 0) {
echo ".";
if (($i+1) % 1000 == 0) {
echo "<br>";
}
backup_flush(300);
}
}
return $status;
}
//This function copies the scorm related info from backup temp dir to course moddata folder,
//creating it if needed
function scorm_restore_files ($packagedir, $restore) {
global $CFG;
$status = true;
$todo = false;
$moddata_path = "";
$scorm_path = "";
$temp_path = "";
//First, we check to "course_id" exists and create is as necessary
//in CFG->dataroot
$dest_dir = $CFG->dataroot."/".$restore->course_id;
$status = check_dir_exists($dest_dir,true);
//First, locate course's moddata directory
$moddata_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
//Check it exists and create it
$status = check_dir_exists($moddata_path,true);
//Now, locate scorm directory
if ($status) {
$scorm_path = $moddata_path."/scorm";
//Check it exists and create it
$status = check_dir_exists($scorm_path,true);
}
//Now locate the temp dir we are restoring from
if ($status) {
$temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
"/moddata/scorm/".$packagedir;
//Check it exists
if (is_dir($temp_path)) {
$todo = true;
}
}
//If todo, we create the neccesary dirs in course moddata/scorm
if ($status and $todo) {
//Make scorm package directory path
$this_scorm_path = $scorm_path."/".$packagedir;
$status = backup_copy_file($temp_path, $this_scorm_path);
}
return $status;
}
//This function returns a log record with all the necessay transformations
//done. It's used by restore_log_module() to restore modules log.
function scorm_restore_logs($restore,$log) {
$status = true;
return $status;
}
?>