MDL-23479 backup - choice revised & old code deleted (but restore logs)

This commit is contained in:
Eloy Lafuente
2010-07-30 19:39:50 +00:00
parent 01b4ca0171
commit 1ef927d9f9
3 changed files with 5 additions and 631 deletions
@@ -55,6 +55,7 @@ class restore_choice_activity_structure_step extends restore_activity_structure_
$data->timeopen = $this->apply_date_offset($data->timeopen);
$data->timeclose = $this->apply_date_offset($data->timeclose);
$data->timemodified = $this->apply_date_offset($data->timemodified);
// insert the choice record
$newitemid = $DB->insert_record('choice', $data);
@@ -67,7 +68,9 @@ class restore_choice_activity_structure_step extends restore_activity_structure_
$data = (object)$data;
$oldid = $data->id;
$data->choiceid = $this->get_new_parentid('choice');
$data->timemodified = $this->apply_date_offset($data->timemodified);
$newitemid = $DB->insert_record('choice_options', $data);
$this->set_mapping('choice_option', $oldid, $newitemid);
@@ -78,9 +81,11 @@ class restore_choice_activity_structure_step extends restore_activity_structure_
$data = (object)$data;
$oldid = $data->id;
$data->choiceid = $this->get_new_parentid('choice');
$data->optionid = $this->get_mappingid('choice_option', $oldid);
$data->userid = $this->get_mappingid('user', $data->userid);
$data->timemodified = $this->apply_date_offset($data->timemodified);
$newitemid = $DB->insert_record('choice_answers', $data);
// No need to save this mapping as far as nothing depend on it
-249
View File
@@ -1,249 +0,0 @@
<?php
//This php script contains all the stuff to backup/restore
//choice mods
//This is the "graphical" structure of the choice mod:
//
// choice
// (CL,pk->id)----------|
// | |
// | |
// | |
// choice_options |
// (UL,pk->id, fk->choiceid) |
// | |
// | |
// | |
// choice_answers |
// (UL,pk->id, fk->choiceid, fk->optionid)
//
// 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 choice_backup_mods($bf,$preferences) {
global $CFG, $DB;
$status = true;
//Iterate over choice table
$choices = $DB->get_records("choice", array("course" => $preferences->backup_course), "id");
if ($choices) {
foreach ($choices as $choice) {
if (backup_mod_selected($preferences,'choice',$choice->id)) {
$status = choice_backup_one_mod($bf,$preferences,$choice);
}
}
}
return $status;
}
function choice_backup_one_mod($bf,$preferences,$choice) {
global $CFG, $DB;
if (is_numeric($choice)) {
$choice = $DB->get_record('choice',array('id' => $choice));
}
$status = true;
//Start mod
fwrite ($bf,start_tag("MOD",3,true));
//Print choice data
fwrite ($bf,full_tag("ID",4,false,$choice->id));
fwrite ($bf,full_tag("MODTYPE",4,false,"choice"));
fwrite ($bf,full_tag("NAME",4,false,$choice->name));
fwrite ($bf,full_tag("TEXT",4,false,$choice->intro));
fwrite ($bf,full_tag("FORMAT",4,false,$choice->introformat));
fwrite ($bf,full_tag("PUBLISH",4,false,$choice->publish));
fwrite ($bf,full_tag("SHOWRESULTS",4,false,$choice->showresults));
fwrite ($bf,full_tag("DISPLAY",4,false,$choice->display));
fwrite ($bf,full_tag("ALLOWUPDATE",4,false,$choice->allowupdate));
fwrite ($bf,full_tag("SHOWUNANSWERED",4,false,$choice->showunanswered));
fwrite ($bf,full_tag("LIMITANSWERS",4,false,$choice->limitanswers));
fwrite ($bf,full_tag("TIMEOPEN",4,false,$choice->timeopen));
fwrite ($bf,full_tag("TIMECLOSE",4,false,$choice->timeclose));
fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$choice->timemodified));
//Now backup choice_options
$status = backup_choice_options($bf,$preferences,$choice->id);
//if we've selected to backup users info, then execute backup_choice_answers
if (backup_userdata_selected($preferences,'choice',$choice->id)) {
$status = backup_choice_answers($bf,$preferences,$choice->id);
}
//End mod
$status =fwrite ($bf,end_tag("MOD",3,true));
return $status;
}
//Backup choice_answers contents (executed from choice_backup_mods)
function backup_choice_answers ($bf,$preferences,$choice) {
global $CFG, $DB;
$status = true;
$choice_answers = $DB->get_records("choice_answers", array("choiceid" => $choice),"id");
//If there is answers
if ($choice_answers) {
//Write start tag
$status =fwrite ($bf,start_tag("ANSWERS",4,true));
//Iterate over each answer
foreach ($choice_answers as $cho_ans) {
//Start answer
$status =fwrite ($bf,start_tag("ANSWER",5,true));
//Print answer contents
fwrite ($bf,full_tag("ID",6,false,$cho_ans->id));
fwrite ($bf,full_tag("USERID",6,false,$cho_ans->userid));
fwrite ($bf,full_tag("OPTIONID",6,false,$cho_ans->optionid));
fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$cho_ans->timemodified));
//End answer
$status =fwrite ($bf,end_tag("ANSWER",5,true));
}
//Write end tag
$status =fwrite ($bf,end_tag("ANSWERS",4,true));
}
return $status;
}
//backup choice_options contents (executed from choice_backup_mods)
function backup_choice_options ($bf,$preferences,$choice) {
global $CFG, $DB;
$status = true;
$choice_options = $DB->get_records("choice_options", array("choiceid" => $choice),"id");
//If there is options
if (!empty($choice_options)) {
//Write start tag
$status =fwrite ($bf,start_tag("OPTIONS",4,true));
//Iterate over each answer
foreach ($choice_options as $cho_opt) {
//Start option
$status =fwrite ($bf,start_tag("OPTION",5,true));
//Print option contents
fwrite ($bf,full_tag("ID",6,false,$cho_opt->id));
fwrite ($bf,full_tag("TEXT",6,false,$cho_opt->text));
fwrite ($bf,full_tag("MAXANSWERS",6,false,$cho_opt->maxanswers));
fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$cho_opt->timemodified));
//End answer
$status =fwrite ($bf,end_tag("OPTION",5,true));
}
//Write end tag
$status =fwrite ($bf,end_tag("OPTIONS",4,true));
}
return $status;
}
////Return an array of info (name,value)
function choice_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
if (!empty($instances) && is_array($instances) && count($instances)) {
$info = array();
foreach ($instances as $id => $instance) {
$info += choice_check_backup_mods_instances($instance,$backup_unique_code);
}
return $info;
}
//First the course data
$info[0][0] = get_string("modulenameplural","choice");
if ($ids = choice_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("responses","choice");
if ($ids = choice_answer_ids_by_course ($course)) {
$info[1][1] = count($ids);
} else {
$info[1][1] = 0;
}
}
return $info;
}
////Return an array of info (name,value)
function choice_check_backup_mods_instances($instance,$backup_unique_code) {
//First the course data
$info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
$info[$instance->id.'0'][1] = '';
//Now, if requested, the user_data
if (!empty($instance->userdata)) {
$info[$instance->id.'1'][0] = get_string("responses","choice");
if ($ids = choice_answer_ids_by_instance ($instance->id)) {
$info[$instance->id.'1'][1] = count($ids);
} else {
$info[$instance->id.'1'][1] = 0;
}
}
return $info;
}
//Return a content encoded to support interactivities linking. Every module
//should have its own. They are called automatically from the backup procedure.
function choice_encode_content_links ($content,$preferences) {
global $CFG;
$base = preg_quote($CFG->wwwroot,"/");
//Link to the list of choices
$buscar="/(".$base."\/mod\/choice\/index.php\?id\=)([0-9]+)/";
$result= preg_replace($buscar,'$@CHOICEINDEX*$2@$',$content);
//Link to choice view by moduleid
$buscar="/(".$base."\/mod\/choice\/view.php\?id\=)([0-9]+)/";
$result= preg_replace($buscar,'$@CHOICEVIEWBYID*$2@$',$result);
return $result;
}
// INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
//Returns an array of choices id
function choice_ids ($course) {
global $CFG, $DB;
return $DB->get_records_sql ("SELECT a.id, a.course
FROM {choice} a
WHERE a.course = ?", array($course));
}
//Returns an array of choice_answers id
function choice_answer_ids_by_course ($course) {
global $CFG, $DB;
return $DB->get_records_sql ("SELECT s.id , s.choiceid
FROM {choice_answers} s,
{choice} a
WHERE a.course = ? AND
s.choiceid = a.id", array($course));
}
//Returns an array of choice_answers id
function choice_answer_ids_by_instance ($instanceid) {
global $CFG, $DB;
return $DB->get_records_sql ("SELECT s.id , s.choiceid
FROM {choice_answers} s
WHERE s.choiceid = ?", array($instanceid));
}
-382
View File
@@ -26,388 +26,6 @@
//
//-----------------------------------------------------------
//This function executes all the restore procedure about this mod
function choice_restore_mods($mod,$restore) {
global $CFG, $DB;
$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;
// if necessary, write to restorelog and adjust date/time fields
if ($restore->course_startdateoffset) {
restore_log_date_changes('Choice', $restore, $info['MOD']['#'], array('TIMEOPEN', 'TIMECLOSE'));
}
//traverse_xmlize($info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//Now, build the CHOICE record structure
$choice->course = $restore->course_id;
$choice->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
$choice->intro = backup_todb($info['MOD']['#']['TEXT']['0']['#']);
$choice->introformat = backup_todb($info['MOD']['#']['FORMAT']['0']['#']);
$choice->publish = backup_todb($info['MOD']['#']['PUBLISH']['0']['#']);
$choice->showresults = isset($info['MOD']['#']['SHOWRESULTS']['0']['#'])?backup_todb($info['MOD']['#']['SHOWRESULTS']['0']['#']):'';
$choice->display = backup_todb($info['MOD']['#']['DISPLAY']['0']['#']);
$choice->allowupdate = backup_todb($info['MOD']['#']['ALLOWUPDATE']['0']['#']);
$choice->showunanswered = backup_todb($info['MOD']['#']['SHOWUNANSWERED']['0']['#']);
$choice->limitanswers = backup_todb($info['MOD']['#']['LIMITANSWERS']['0']['#']);
$choice->timeopen = backup_todb($info['MOD']['#']['TIMEOPEN']['0']['#']);
$choice->timeclose = backup_todb($info['MOD']['#']['TIMECLOSE']['0']['#']);
$choice->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
//To mantain compatibilty, in 1.4 the publish setting meaning has changed. We
//have to modify some things it if the release field isn't present in the backup file.
if (! isset($info['MOD']['#']['SHOWRESULTS']['0']['#'])) { //check for previous versions
if (! isset($info['MOD']['#']['RELEASE']['0']['#'])) { //It's a pre-14 backup filea
//Set the allowupdate field
if ($choice->publish == 0) {
$choice->allowupdate = 1;
}
//Set the showresults field as defined by the old publish field
if ($choice->publish > 0) {
$choice->showresults = 1;
}
//Recode the publish field to its 1.4 meaning
if ($choice->publish > 0) {
$choice->publish--;
}
} else { //it's post 1.4 pre 1.6
//convert old release values into new showanswer column.
$choice->showresults = backup_todb($info['MOD']['#']['RELEASE']['0']['#']);
}
}
//The structure is equal to the db, so insert the choice
$newid = $DB->insert_record ("choice",$choice);
if ($newid) {
//We have the newid, update backup_ids
backup_putid($restore->backup_unique_code,$mod->modtype,
$mod->id, $newid);
//Check to see how answers (curently choice_options) are stored in the table
//If answer1 - answer6 exist, this is a pre 1.5 version of choice
if (isset($info['MOD']['#']['ANSWER1']['0']['#']) ||
isset($info['MOD']['#']['ANSWER2']['0']['#']) ||
isset($info['MOD']['#']['ANSWER3']['0']['#']) ||
isset($info['MOD']['#']['ANSWER4']['0']['#']) ||
isset($info['MOD']['#']['ANSWER5']['0']['#']) ||
isset($info['MOD']['#']['ANSWER6']['0']['#']) ) {
//This is a pre 1.5 choice backup, special work begins
$options = array();
$options[1] = backup_todb($info['MOD']['#']['ANSWER1']['0']['#']);
$options[2] = backup_todb($info['MOD']['#']['ANSWER2']['0']['#']);
$options[3] = backup_todb($info['MOD']['#']['ANSWER3']['0']['#']);
$options[4] = backup_todb($info['MOD']['#']['ANSWER4']['0']['#']);
$options[5] = backup_todb($info['MOD']['#']['ANSWER5']['0']['#']);
$options[6] = backup_todb($info['MOD']['#']['ANSWER6']['0']['#']);
for($i = 1; $i < 7; $i++) { //insert old answers (in 1.4) as choice_options (1.5) to db.
if (!empty($options[$i])) { //make sure this option has something in it!
$option->choiceid = $newid;
$option->text = $options[$i];
$option->timemodified = $choice->timemodified;
$newoptionid = $DB->insert_record ("choice_options",$option);
//Save this choice_option to backup_ids
backup_putid($restore->backup_unique_code,"choice_options",$i,$newoptionid);
}
}
} else { //Now we are in a "standard" 1.5 choice, so restore choice_options normally
$status = choice_options_restore_mods($newid,$info,$restore);
}
//now restore the answers for this choice.
if (restore_userdata_selected($restore,'choice',$mod->id)) {
//Restore choice_answers
$status = choice_answers_restore_mods($newid,$info,$restore);
}
} else {
$status = false;
}
//Do some output
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("modulename","choice")." \"".format_string($choice->name,true)."\"</li>";
}
backup_flush(300);
} else {
$status = false;
}
return $status;
}
function choice_options_restore_mods($choiceid,$info,$restore) {
global $CFG, $DB;
$status = true;
$options = $info['MOD']['#']['OPTIONS']['0']['#']['OPTION'];
//Iterate over options
for($i = 0; $i < sizeof($options); $i++) {
$opt_info = $options[$i];
//traverse_xmlize($opt_info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//We'll need this later!!
$oldid = backup_todb($opt_info['#']['ID']['0']['#']);
$olduserid = isset($opt_info['#']['USERID']['0']['#'])?backup_todb($opt_info['#']['USERID']['0']['#']):'';
//Now, build the CHOICE_OPTIONS record structure
$option->choiceid = $choiceid;
$option->text = backup_todb($opt_info['#']['TEXT']['0']['#']);
$option->maxanswers = backup_todb($opt_info['#']['MAXANSWERS']['0']['#']);
$option->timemodified = backup_todb($opt_info['#']['TIMEMODIFIED']['0']['#']);
//The structure is equal to the db, so insert the choice_options
$newid = $DB->insert_record ("choice_options",$option);
//Do some output
if (($i+1) % 50 == 0) {
if (!defined('RESTORE_SILENTLY')) {
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,"choice_options",$oldid,
$newid);
} else {
$status = false;
}
}
return $status;
}
//This function restores the choice_answers
function choice_answers_restore_mods($choiceid,$info,$restore) {
global $CFG, $DB;
$status = true;
if (isset($info['MOD']['#']['ANSWERS']['0']['#']['ANSWER'])) {
$answers = $info['MOD']['#']['ANSWERS']['0']['#']['ANSWER'];
//Iterate over answers
for($i = 0; $i < sizeof($answers); $i++) {
$ans_info = $answers[$i];
//traverse_xmlize($sub_info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//We'll need this later!!
$oldid = backup_todb($ans_info['#']['ID']['0']['#']);
$olduserid = backup_todb($ans_info['#']['USERID']['0']['#']);
//Now, build the CHOICE_ANSWERS record structure
$answer->choiceid = $choiceid;
$answer->userid = backup_todb($ans_info['#']['USERID']['0']['#']);
$answer->optionid = backup_todb($ans_info['#']['OPTIONID']['0']['#']);
$answer->timemodified = backup_todb($ans_info['#']['TIMEMODIFIED']['0']['#']);
//If the answer contains CHOICE_ANSWER, it's a pre 1.5 backup
if (!empty($ans_info['#']['CHOICE_ANSWER']['0']['#'])) {
//optionid was, in pre 1.5 backups, choice_answer
$answer->optionid = backup_todb($ans_info['#']['CHOICE_ANSWER']['0']['#']);
}
//We have to recode the optionid field
$option = backup_getid($restore->backup_unique_code,"choice_options",$answer->optionid);
if ($option) {
$answer->optionid = $option->new_id;
}
//We have to recode the userid field
$user = backup_getid($restore->backup_unique_code,"user",$answer->userid);
if ($user) {
$answer->userid = $user->new_id;
}
//The structure is equal to the db, so insert the choice_answers
$newid = $DB->insert_record ("choice_answers",$answer);
//Do some output
if (($i+1) % 50 == 0) {
if (!defined('RESTORE_SILENTLY')) {
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,"choice_answers",$oldid,
$newid);
} else {
$status = false;
}
}
}
return $status;
}
//Return a content decoded to support interactivities linking. Every module
//should have its own. They are called automatically from
//choice_decode_content_links_caller() function in each module
//in the restore process
function choice_decode_content_links ($content,$restore) {
global $CFG;
$result = $content;
//Link to the list of choices
$searchstring='/\$@(CHOICEINDEX)\*([0-9]+)@\$/';
//We look for it
preg_match_all($searchstring,$content,$foundset);
//If found, then we are going to look for its new id (in backup tables)
if ($foundset[0]) {
//print_object($foundset); //Debug
//Iterate over foundset[2]. They are the old_ids
foreach($foundset[2] as $old_id) {
//We get the needed variables here (course id)
$rec = backup_getid($restore->backup_unique_code,"course",$old_id);
//Personalize the searchstring
$searchstring='/\$@(CHOICEINDEX)\*('.$old_id.')@\$/';
//If it is a link to this course, update the link to its new location
if($rec->new_id) {
//Now replace it
$result= preg_replace($searchstring,$CFG->wwwroot.'/mod/choice/index.php?id='.$rec->new_id,$result);
} else {
//It's a foreign link so leave it as original
$result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/choice/index.php?id='.$old_id,$result);
}
}
}
//Link to choice view by moduleid
$searchstring='/\$@(CHOICEVIEWBYID)\*([0-9]+)@\$/';
//We look for it
preg_match_all($searchstring,$result,$foundset);
//If found, then we are going to look for its new id (in backup tables)
if ($foundset[0]) {
//print_object($foundset); //Debug
//Iterate over foundset[2]. They are the old_ids
foreach($foundset[2] as $old_id) {
//We get the needed variables here (course_modules id)
$rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
//Personalize the searchstring
$searchstring='/\$@(CHOICEVIEWBYID)\*('.$old_id.')@\$/';
//If it is a link to this course, update the link to its new location
if($rec->new_id) {
//Now replace it
$result= preg_replace($searchstring,$CFG->wwwroot.'/mod/choice/view.php?id='.$rec->new_id,$result);
} else {
//It's a foreign link so leave it as original
$result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/choice/view.php?id='.$old_id,$result);
}
}
}
return $result;
}
//This function makes all the necessary calls to xxxx_decode_content_links()
//function in each module, passing them the desired contents to be decoded
//from backup format to destination site/course in order to mantain inter-activities
//working in the backup/restore process. It's called from restore_decode_content_links()
//function in restore process
function choice_decode_content_links_caller($restore) {
global $CFG, $DB;
$status = true;
if ($choices = $DB->get_records('chat', array('course'=>$restore->course_id), '', "id,text")) {
//Iterate over each choice->text
$i = 0; //Counter to send some output to the browser to avoid timeouts
foreach ($choices as $choice) {
//Increment counter
$i++;
$content = $choice->text;
$result = restore_decode_content_links_worker($content,$restore);
if ($result != $content) {
//Update record
$choice->text = $result;
$status = $DB->update_record("choice",$choice);
if (debugging()) {
if (!defined('RESTORE_SILENTLY')) {
echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
}
}
}
//Do some output
if (($i+1) % 5 == 0) {
if (!defined('RESTORE_SILENTLY')) {
echo ".";
if (($i+1) % 100 == 0) {
echo "<br />";
}
}
backup_flush(300);
}
}
}
return $status;
}
//This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
//some texts in the module
function choice_restore_wiki2markdown ($restore) {
global $CFG, $DB;
$status = true;
//Convert choice->text
if ($records = $DB->get_records_sql("SELECT c.id, c.intro, c.introformat
FROM {choice} c, {backup_ids} b
WHERE c.course = ? AND
c.format = ".FORMAT_WIKI. " AND
b.backup_code = ? AND
b.table_name = 'choice' AND
b.new_id = c.id", array($restore->course_id, $restore->backup_unique_code))) {
foreach ($records as $record) {
//Rebuild wiki links
$record->intro = restore_decode_wiki_content($record->intro, $restore);
//Convert to Markdown
$wtm = new WikiToMarkdown();
$record->intro = $wtm->convert($record->intro, $restore->course_id);
$record->introformat = FORMAT_MARKDOWN;
$status = $DB->update_record('choice', $record);
//Do some output
$i++;
if (($i+1) % 1 == 0) {
if (!defined('RESTORE_SILENTLY')) {
echo ".";
if (($i+1) % 20 == 0) {
echo "<br />";
}
}
backup_flush(300);
}
}
}
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 choice_restore_logs($restore,$log) {