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

This commit is contained in:
Eloy Lafuente 2010-07-30 23:14:08 +00:00
parent c61b92fd49
commit 967a68fd64
5 changed files with 6 additions and 425 deletions

View file

@ -35,7 +35,8 @@ class backup_chat_activity_structure_step extends backup_activity_structure_step
'chattime', 'schedule', 'timemodified'));
$messages = new backup_nested_element('messages');
$message = new backup_nested_element('message', array('id'), array('userid', 'groupid', 'system', 'message_text', 'timestamp'));
$message = new backup_nested_element('message', array('id'), array(
'userid', 'groupid', 'system', 'message_text', 'timestamp'));
// it is not cool to have two tags with same name, so we need to rename message field to message_text
$message->set_source_alias('message', 'message_text');

View file

@ -55,6 +55,7 @@ class restore_chat_activity_task extends restore_activity_task {
$contents = array();
$contents[] = new restore_decode_content('chat', array('intro'), 'chat');
$contents[] = new restore_decode_content('chat_messages', array('message'), 'chat_message');
return $contents;
}

View file

@ -53,6 +53,7 @@ class restore_chat_activity_structure_step extends restore_activity_structure_st
$data->course = $this->get_courseid();
$data->chattime = $this->apply_date_offset($data->chattime);
$data->timemodified = $this->apply_date_offset($data->timemodified);
// insert the chat record
$newitemid = $DB->insert_record('chat', $data);
@ -69,10 +70,10 @@ class restore_chat_activity_structure_step extends restore_activity_structure_st
$data->userid = $this->get_mappingid('user', $data->userid);
$data->groupid = $this->get_mappingid('group', $data->groupid);
$data->message = $data->message_text;
$data->timestamp = $this->apply_date_offset($data->timestamp);
$newitemid = $DB->insert_record('chat_messages', $data);
// No need to save this mapping as far as nothing depend on it
// (child paths, file areas nor links decoder)
$this->set_mapping('chat_message', $oldid, $newitemid); // because of decode
}
protected function after_execute() {

View file

@ -1,200 +0,0 @@
<?php
//This php script contains all the stuff to backup/restore
//chat mods
//This is the "graphical" structure of the chat mod:
//
// chat
// (CL,pk->id)
// |
// |
// |
// chat_messages
// (UL,pk->id, fk->chatid)
//
// 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 backup procedure about this mod
function chat_backup_mods($bf,$preferences) {
global $CFG, $DB;
$status = true;
//Iterate over chat table
$chats = $DB->get_records ("chat", array("course"=>$preferences->backup_course), "id");
if ($chats) {
foreach ($chats as $chat) {
if (backup_mod_selected($preferences,'chat',$chat->id)) {
$status = chat_backup_one_mod($bf,$preferences,$chat);
}
}
}
return $status;
}
function chat_backup_one_mod($bf,$preferences,$chat) {
global $CFG, $DB;
if (is_numeric($chat)) {
$chat = $DB->get_record('chat', array('id'=>$chat));
}
$status = true;
//Start mod
fwrite ($bf,start_tag("MOD",3,true));
//Print chat data
fwrite ($bf,full_tag("ID",4,false,$chat->id));
fwrite ($bf,full_tag("MODTYPE",4,false,"chat"));
fwrite ($bf,full_tag("NAME",4,false,$chat->name));
fwrite ($bf,full_tag("INTRO",4,false,$chat->intro));
fwrite ($bf,full_tag("KEEPDAYS",4,false,$chat->keepdays));
fwrite ($bf,full_tag("STUDENTLOGS",4,false,$chat->studentlogs));
fwrite ($bf,full_tag("SCHEDULE",4,false,$chat->schedule));
fwrite ($bf,full_tag("CHATTIME",4,false,$chat->chattime));
fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$chat->timemodified));
//if we've selected to backup users info, then execute backup_chat_messages
if (backup_userdata_selected($preferences,'chat',$chat->id)) {
$status = backup_chat_messages($bf,$preferences,$chat->id);
}
//End mod
$status =fwrite ($bf,end_tag("MOD",3,true));
return $status;
}
//Backup chat_messages contents (executed from chat_backup_mods)
function backup_chat_messages ($bf,$preferences,$chat) {
global $CFG, $DB;
$status = true;
$chat_messages = $DB->get_records("chat_messages", array("chatid"=>$chat), "id");
//If there is messages
if ($chat_messages) {
//Write start tag
$status =fwrite ($bf,start_tag("MESSAGES",4,true));
//Iterate over each message
foreach ($chat_messages as $cha_mes) {
//Start message
$status =fwrite ($bf,start_tag("MESSAGE",5,true));
//Print message contents
fwrite ($bf,full_tag("ID",6,false,$cha_mes->id));
fwrite ($bf,full_tag("USERID",6,false,$cha_mes->userid));
fwrite ($bf,full_tag("GROUPID",6,false,$cha_mes->groupid));
fwrite ($bf,full_tag("SYSTEM",6,false,$cha_mes->system));
fwrite ($bf,full_tag("MESSAGE_TEXT",6,false,$cha_mes->message));
fwrite ($bf,full_tag("TIMESTAMP",6,false,$cha_mes->timestamp));
//End submission
$status =fwrite ($bf,end_tag("MESSAGE",5,true));
}
//Write end tag
$status =fwrite ($bf,end_tag("MESSAGES",4,true));
}
return $status;
}
//Return an array of info (name,value)
function chat_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 += chat_check_backup_mods_instances($instance,$backup_unique_code);
}
return $info;
}
//First the course data
$info[0][0] = get_string("modulenameplural","chat");
if ($ids = chat_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("messages","chat");
if ($ids = chat_message_ids_by_course ($course)) {
$info[1][1] = count($ids);
} else {
$info[1][1] = 0;
}
}
return $info;
}
//Return an array of info (name,value)
function chat_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("messages","chat");
if ($ids = chat_message_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 chat_encode_content_links ($content,$preferences) {
global $CFG;
$base = preg_quote($CFG->wwwroot,"/");
//Link to the list of chats
$buscar="/(".$base."\/mod\/chat\/index.php\?id\=)([0-9]+)/";
$result= preg_replace($buscar,'$@CHATINDEX*$2@$',$content);
//Link to chat view by moduleid
$buscar="/(".$base."\/mod\/chat\/view.php\?id\=)([0-9]+)/";
$result= preg_replace($buscar,'$@CHATVIEWBYID*$2@$',$result);
return $result;
}
// INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
//Returns an array of chats id
function chat_ids ($course) {
global $DB;
return $DB->get_records_sql("SELECT c.id, c.course
FROM {chat} c
WHERE c.course = ?", array($course));
}
//Returns an array of assignment_submissions id
function chat_message_ids_by_course ($course) {
global $DB;
return $DB->get_records_sql("SELECT m.id , m.chatid
FROM {chat_messages} m, {chat} c
WHERE c.course = ? AND
m.chatid = c.id", array($course));
}
//Returns an array of chat id
function chat_message_ids_by_instance ($instanceid) {
global $DB;
return $DB->get_records_sql("SELECT m.id , m.chatid
FROM {chat_messages} m
WHERE m.chatid = ?", array($instanceid));
}

View file

@ -21,228 +21,6 @@
//
//-----------------------------------------------------------
//This function executes all the restore procedure about this mod
function chat_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;
//traverse_xmlize($info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
// if necessary, write to restorelog and adjust date/time fields
if ($restore->course_startdateoffset) {
restore_log_date_changes('Chat', $restore, $info['MOD']['#'], array('CHATTIME'));
}
//Now, build the CHAT record structure
$chat->course = $restore->course_id;
$chat->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
$chat->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
$chat->keepdays = backup_todb($info['MOD']['#']['KEEPDAYS']['0']['#']);
$chat->studentlogs = backup_todb($info['MOD']['#']['STUDENTLOGS']['0']['#']);
$chat->schedule = backup_todb($info['MOD']['#']['SCHEDULE']['0']['#']);
$chat->chattime = backup_todb($info['MOD']['#']['CHATTIME']['0']['#']);
$chat->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
//The structure is equal to the db, so insert the chat
$newid = $DB->insert_record ("chat",$chat);
//Do some output
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("modulename","chat")." \"".format_string($chat->name,true)."\"</li>";
}
backup_flush(300);
if ($newid) {
//We have the newid, update backup_ids
backup_putid($restore->backup_unique_code,$mod->modtype,
$mod->id, $newid);
//Now check if want to restore user data and do it.
if (restore_userdata_selected($restore,'chat',$mod->id)) {
//Restore chat_messages
$status = chat_messages_restore_mods ($mod->id, $newid,$info,$restore);
}
} else {
$status = false;
}
} else {
$status = false;
}
return $status;
}
//This function restores the chat_messages
function chat_messages_restore_mods($old_chat_id, $new_chat_id,$info,$restore) {
global $CFG, $DB;
$status = true;
//Get the messages array
$messages = $info['MOD']['#']['MESSAGES']['0']['#']['MESSAGE'];
//Iterate over messages
for($i = 0; $i < sizeof($messages); $i++) {
$mes_info = $messages[$i];
//traverse_xmlize($mes_info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//We'll need this later!!
$oldid = backup_todb($mes_info['#']['ID']['0']['#']);
$olduserid = backup_todb($mes_info['#']['USERID']['0']['#']);
//Now, build the CHAT_MESSAGES record structure
$message = new object();
$message->chatid = $new_chat_id;
$message->userid = backup_todb($mes_info['#']['USERID']['0']['#']);
$message->groupid = backup_todb($mes_info['#']['GROUPID']['0']['#']);
$message->system = backup_todb($mes_info['#']['SYSTEM']['0']['#']);
$message->message = backup_todb($mes_info['#']['MESSAGE_TEXT']['0']['#']);
$message->timestamp = backup_todb($mes_info['#']['TIMESTAMP']['0']['#']);
//We have to recode the userid field
$user = backup_getid($restore->backup_unique_code,"user",$message->userid);
if ($user) {
$message->userid = $user->new_id;
}
//We have to recode the groupid field
$group = restore_group_getid($restore, $message->groupid);
if ($group) {
$message->groupid = $group->new_id;
}
//The structure is equal to the db, so insert the chat_message
$newid = $DB->insert_record ("chat_messages",$message);
//Do some output
if (($i+1) % 50 == 0) {
if (!defined('RESTORE_SILENTLY')) {
echo ".";
if (($i+1) % 1000 == 0) {
echo "<br />";
}
}
backup_flush(300);
}
}
return $status;
}
//Return a content decoded to support interactivities linking. Every module
//should have its own. They are called automatically from
//chat_decode_content_links_caller() function in each module
//in the restore process
function chat_decode_content_links ($content,$restore) {
global $CFG;
$result = $content;
//Link to the list of chats
$searchstring='/\$@(CHATINDEX)\*([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='/\$@(CHATINDEX)\*('.$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/chat/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/chat/index.php?id='.$old_id,$result);
}
}
}
//Link to chat view by moduleid
$searchstring='/\$@(CHATVIEWBYID)\*([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='/\$@(CHATVIEWBYID)\*('.$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/chat/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/chat/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 chat_decode_content_links_caller($restore) {
global $CFG, $DB;
$status = true;
if ($chats = $DB->get_records('chat', array('course'=>$restore->course_id), '', "id,intro")) {
//Iterate over each chat->intro
$i = 0; //Counter to send some output to the browser to avoid timeouts
foreach ($chats as $chat) {
//Increment counter
$i++;
$content = $chat->intro;
$result = restore_decode_content_links_worker($content,$restore);
if ($result != $content) {
//Update record
$chat->intro = $result;
$status = $DB->update_record("chat",$chat);
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 returns a log record with all the necessay transformations
//done. It's used by restore_log_module() to restore modules log.
function chat_restore_logs($restore,$log) {