1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
<?php //$Id: restorelib.php,v 1.14 2006/03/28 23:31:11 stronk7 Exp $
//This php script contains all the stuff to backup/restore
//journal mods
//This is the "graphical" structure of the journal mod:
//
// journal
// (CL,pk->id)
// |
// |
// |
// journal_entries
// (UL,pk->id, fk->journal)
//
// 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 journal_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 JOURNAL record structure
$journal->course = $restore->course_id;
$journal->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
$journal->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
$journal->introformat = backup_todb($info['MOD']['#']['INTROFORMAT']['0']['#']);
$journal->days = backup_todb($info['MOD']['#']['DAYS']['0']['#']);
$journal->assessed = backup_todb($info['MOD']['#']['ASSESSED']['0']['#']);
$journal->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
//We have to recode the assessed field if it is <0 (scale)
if ($journal->assessed < 0) {
$scale = backup_getid($restore->backup_unique_code,"scale",abs($journal->assessed));
if ($scale) {
$journal->assessed = -($scale->new_id);
}
}
//The structure is equal to the db, so insert the journal
$newid = insert_record ("journal",$journal);
//Do some output
if (!defined('RESTORE_SILENTLY')) {
echo "<li>".get_string("modulename","journal")." \"".format_string(stripslashes($journal->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,'journal',$mod->id)) {
//Restore journal_entries
$status = journal_entries_restore_mods ($mod->id, $newid,$info,$restore);
}
} else {
$status = false;
}
} else {
$status = false;
}
return $status;
}
//This function restores the journal_entries
function journal_entries_restore_mods($old_journal_id, $new_journal_id,$info,$restore) {
global $CFG;
$status = true;
//Get the entries array
$entries = $info['MOD']['#']['ENTRIES']['0']['#']['ENTRY'];
//Iterate over entries
for($i = 0; $i < sizeof($entries); $i++) {
$entry_info = $entries[$i];
//traverse_xmlize($entry_info); //Debug
//print_object ($GLOBALS['traverse_array']); //Debug
//$GLOBALS['traverse_array']=""; //Debug
//We'll need this later!!
$oldid = backup_todb($sub_info['#']['ID']['0']['#']);
$olduserid = backup_todb($sub_info['#']['USERID']['0']['#']);
//Now, build the JOURNAL_ENTRIES record structure
$entry->journal = $new_journal_id;
$entry->userid = backup_todb($entry_info['#']['USERID']['0']['#']);
$entry->modified = backup_todb($entry_info['#']['MODIFIED']['0']['#']);
$entry->text = backup_todb($entry_info['#']['TEXT']['0']['#']);
$entry->format = backup_todb($entry_info['#']['FORMAT']['0']['#']);
$entry->rating = backup_todb($entry_info['#']['RATING']['0']['#']);
$entry->comment = backup_todb($entry_info['#']['COMMENT']['0']['#']);
$entry->teacher = backup_todb($entry_info['#']['TEACHER']['0']['#']);
$entry->timemarked = backup_todb($entry_info['#']['TIMEMARKED']['0']['#']);
$entry->mailed = backup_todb($entry_info['#']['MAILED']['0']['#']);
//We have to recode the userid field
$user = backup_getid($restore->backup_unique_code,"user",$entry->userid);
if ($user) {
$entry->userid = $user->new_id;
}
//We have to recode the teacher field
$user = backup_getid($restore->backup_unique_code,"user",$entry->teacher);
if ($user) {
$entry->teacher = $user->new_id;
}
//The structure is equal to the db, so insert the journal_entry
$newid = insert_record ("journal_entries",$entry);
//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,"journal_entry",$oldid,
$newid);
} else {
$status = false;
}
}
return $status;
}
//This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
//some texts in the module
function journal_restore_wiki2markdown ($restore) {
global $CFG;
$status = true;
//Convert journal_entries->text
if ($records = get_records_sql ("SELECT e.id, e.text, e.format
FROM {$CFG->prefix}journal_entries e,
{$CFG->prefix}journal j,
{$CFG->prefix}backup_ids b
WHERE j.id = e.journal AND
j.course = $restore->course_id AND
e.format = ".FORMAT_WIKI. " AND
b.backup_code = $restore->backup_unique_code AND
b.table_name = 'journal_entries' AND
b.new_id = e.id")) {
foreach ($records as $record) {
//Rebuild wiki links
$record->text = restore_decode_wiki_content($record->text, $restore);
//Convert to Markdown
$wtm = new WikiToMarkdown();
$record->text = $wtm->convert($record->text, $restore->course_id);
$record->format = FORMAT_MARKDOWN;
$status = update_record('journal_entries', addslashes_object($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);
}
}
}
//Convert journal->intro
if ($records = get_records_sql ("SELECT j.id, j.intro, j.introformat
FROM {$CFG->prefix}journal j,
{$CFG->prefix}backup_ids b
WHERE j.course = $restore->course_id AND
j.introformat = ".FORMAT_WIKI. " AND
b.backup_code = $restore->backup_unique_code AND
b.table_name = 'journal' AND
b.new_id = j.id")) {
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 = update_record('journal', addslashes_object($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 journal_restore_logs($restore,$log) {
$status = false;
//Depending of the action, we recode different things
switch ($log->action) {
case "add":
if ($log->cmid) {
//Get the new_id of the module (to recode the info field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
$log->url = "view.php?id=".$log->cmid;
$log->info = $mod->new_id;
$status = true;
}
}
break;
case "update":
if ($log->cmid) {
//Get the new_id of the module (to recode the info field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
$log->url = "view.php?id=".$log->cmid;
$log->info = $mod->new_id;
$status = true;
}
}
break;
case "view":
if ($log->cmid) {
//Get the new_id of the module (to recode the info field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
$log->url = "view.php?id=".$log->cmid;
$log->info = $mod->new_id;
$status = true;
}
}
break;
case "add entry":
if ($log->cmid) {
//Get the new_id of the module (to recode the info field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
$log->url = "view.php?id=".$log->cmid;
$log->info = $mod->new_id;
$status = true;
}
}
break;
case "update entry":
if ($log->cmid) {
//Get the new_id of the module (to recode the info field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
$log->url = "view.php?id=".$log->cmid;
$log->info = $mod->new_id;
$status = true;
}
}
break;
case "view responses":
if ($log->cmid) {
//Get the new_id of the module (to recode the info field)
$mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
if ($mod) {
$log->url = "report.php?id=".$log->cmid;
$log->info = $mod->new_id;
$status = true;
}
}
break;
case "update feedback":
if ($log->cmid) {
$log->url = "report.php?id=".$log->cmid;
$status = true;
}
break;
case "view all":
$log->url = "index.php?id=".$log->course;
$status = true;
break;
default:
if (!defined('RESTORE_SILENTLY')) {
echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
}
break;
}
if ($status) {
$status = $log;
}
return $status;
}
?>
|