Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / journal / restorelib.php
blobce8ff0f3bf7d0208c07e0f7121562e817392d7e0
1 <?php //$Id$
2 //This php script contains all the stuff to backup/restore
3 //journal mods
5 //This is the "graphical" structure of the journal mod:
6 //
7 // journal
8 // (CL,pk->id)
9 // |
10 // |
11 // |
12 // journal_entries
13 // (UL,pk->id, fk->journal)
15 // Meaning: pk->primary key field of the table
16 // fk->foreign key to link with parent
17 // nt->nested field (recursive data)
18 // CL->course level info
19 // UL->user level info
20 // files->table may have files)
22 //-----------------------------------------------------------
24 //This function executes all the restore procedure about this mod
25 function journal_restore_mods($mod,$restore) {
27 global $CFG;
29 $status = true;
31 //Get record from backup_ids
32 $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
34 if ($data) {
35 //Now get completed xmlized object
36 $info = $data->info;
37 //if necessary, write to restorelog and adjust date/time fields
38 if ($restore->course_startdateoffset) {
39 restore_log_date_changes('Journal', $restore, $info['MOD']['#'], array('TIMEMODIFIED'));
41 //traverse_xmlize($info); //Debug
42 //print_object ($GLOBALS['traverse_array']); //Debug
43 //$GLOBALS['traverse_array']=""; //Debug
45 //Now, build the JOURNAL record structure
46 $journal->course = $restore->course_id;
47 $journal->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
48 $journal->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
49 $journal->introformat = backup_todb($info['MOD']['#']['INTROFORMAT']['0']['#']);
50 $journal->days = backup_todb($info['MOD']['#']['DAYS']['0']['#']);
51 $journal->assessed = backup_todb($info['MOD']['#']['ASSESSED']['0']['#']);
52 $journal->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
54 //We have to recode the assessed field if it is <0 (scale)
55 if ($journal->assessed < 0) {
56 $scale = backup_getid($restore->backup_unique_code,"scale",abs($journal->assessed));
57 if ($scale) {
58 $journal->assessed = -($scale->new_id);
62 //The structure is equal to the db, so insert the journal
63 $newid = insert_record ("journal",$journal);
65 //Do some output
66 if (!defined('RESTORE_SILENTLY')) {
67 echo "<li>".get_string("modulename","journal")." \"".format_string(stripslashes($journal->name),true)."\"</li>";
69 backup_flush(300);
71 if ($newid) {
72 //We have the newid, update backup_ids
73 backup_putid($restore->backup_unique_code,$mod->modtype,
74 $mod->id, $newid);
75 //Now check if want to restore user data and do it.
76 if (restore_userdata_selected($restore,'journal',$mod->id)) {
77 //Restore journal_entries
78 $status = journal_entries_restore_mods ($mod->id, $newid,$info,$restore);
80 } else {
81 $status = false;
83 } else {
84 $status = false;
87 return $status;
91 //This function restores the journal_entries
92 function journal_entries_restore_mods($old_journal_id, $new_journal_id,$info,$restore) {
94 global $CFG;
96 $status = true;
98 //Get the entries array
99 $entries = $info['MOD']['#']['ENTRIES']['0']['#']['ENTRY'];
101 //Iterate over entries
102 for($i = 0; $i < sizeof($entries); $i++) {
103 $entry_info = $entries[$i];
104 //traverse_xmlize($entry_info); //Debug
105 //print_object ($GLOBALS['traverse_array']); //Debug
106 //$GLOBALS['traverse_array']=""; //Debug
108 //We'll need this later!! $sub_info changed to $entry_info
109 $oldid = backup_todb($entry_info['#']['ID']['0']['#']);
110 $olduserid = backup_todb($entry_info['#']['USERID']['0']['#']);
112 //Now, build the JOURNAL_ENTRIES record structure
113 $entry->journal = $new_journal_id;
114 $entry->userid = backup_todb($entry_info['#']['USERID']['0']['#']);
115 $entry->modified = backup_todb($entry_info['#']['MODIFIED']['0']['#']);
116 $entry->modified += $restore->course_startdateoffset;
117 $entry->text = backup_todb($entry_info['#']['TEXT']['0']['#']);
118 $entry->format = backup_todb($entry_info['#']['FORMAT']['0']['#']);
119 $entry->rating = backup_todb($entry_info['#']['RATING']['0']['#']);
120 if (isset($entry_info['#']['COMMENT']['0']['#'])) {
121 $entry->entrycomment = backup_todb($entry_info['#']['COMMENT']['0']['#']);
122 } else {
123 $entry->entrycomment = backup_todb($entry_info['#']['ENTRYCOMMENT']['0']['#']);
125 $entry->teacher = backup_todb($entry_info['#']['TEACHER']['0']['#']);
126 $entry->timemarked = backup_todb($entry_info['#']['TIMEMARKED']['0']['#']);
127 $entry->timemarked += $restore->course_startdateoffset;
128 $entry->mailed = backup_todb($entry_info['#']['MAILED']['0']['#']);
130 //We have to recode the userid field
131 $user = backup_getid($restore->backup_unique_code,"user",$entry->userid);
132 if ($user) {
133 $entry->userid = $user->new_id;
136 //We have to recode the teacher field
137 $user = backup_getid($restore->backup_unique_code,"user",$entry->teacher);
138 if ($user) {
139 $entry->teacher = $user->new_id;
142 //The structure is equal to the db, so insert the journal_entry
143 $newid = insert_record ("journal_entries",$entry);
145 //Do some output
146 if (($i+1) % 50 == 0) {
147 if (!defined('RESTORE_SILENTLY')) {
148 echo ".";
149 if (($i+1) % 1000 == 0) {
150 echo "<br />";
153 backup_flush(300);
156 if ($newid) {
157 //We have the newid, update backup_ids
158 backup_putid($restore->backup_unique_code,"journal_entry",$oldid,
159 $newid);
160 } else {
161 $status = false;
165 return $status;
168 //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
169 //some texts in the module
170 function journal_restore_wiki2markdown ($restore) {
172 global $CFG;
174 $status = true;
176 //Convert journal_entries->text
177 if ($records = get_records_sql ("SELECT e.id, e.text, e.format
178 FROM {$CFG->prefix}journal_entries e,
179 {$CFG->prefix}journal j,
180 {$CFG->prefix}backup_ids b
181 WHERE j.id = e.journal AND
182 j.course = $restore->course_id AND
183 e.format = ".FORMAT_WIKI. " AND
184 b.backup_code = $restore->backup_unique_code AND
185 b.table_name = 'journal_entries' AND
186 b.new_id = e.id")) {
187 foreach ($records as $record) {
188 //Rebuild wiki links
189 $record->text = restore_decode_wiki_content($record->text, $restore);
190 //Convert to Markdown
191 $wtm = new WikiToMarkdown();
192 $record->text = $wtm->convert($record->text, $restore->course_id);
193 $record->format = FORMAT_MARKDOWN;
194 $status = update_record('journal_entries', addslashes_object($record));
195 //Do some output
196 $i++;
197 if (($i+1) % 1 == 0) {
198 if (!defined('RESTORE_SILENTLY')) {
199 echo ".";
200 if (($i+1) % 20 == 0) {
201 echo "<br />";
204 backup_flush(300);
210 //Convert journal->intro
211 if ($records = get_records_sql ("SELECT j.id, j.intro, j.introformat
212 FROM {$CFG->prefix}journal j,
213 {$CFG->prefix}backup_ids b
214 WHERE j.course = $restore->course_id AND
215 j.introformat = ".FORMAT_WIKI. " AND
216 b.backup_code = $restore->backup_unique_code AND
217 b.table_name = 'journal' AND
218 b.new_id = j.id")) {
219 foreach ($records as $record) {
220 //Rebuild wiki links
221 $record->intro = restore_decode_wiki_content($record->intro, $restore);
222 //Convert to Markdown
223 $wtm = new WikiToMarkdown();
224 $record->intro = $wtm->convert($record->intro, $restore->course_id);
225 $record->introformat = FORMAT_MARKDOWN;
226 $status = update_record('journal', addslashes_object($record));
227 //Do some output
228 $i++;
229 if (($i+1) % 1 == 0) {
230 if (!defined('RESTORE_SILENTLY')) {
231 echo ".";
232 if (($i+1) % 20 == 0) {
233 echo "<br />";
236 backup_flush(300);
242 return $status;
245 //This function returns a log record with all the necessay transformations
246 //done. It's used by restore_log_module() to restore modules log.
247 function journal_restore_logs($restore,$log) {
249 $status = false;
251 //Depending of the action, we recode different things
252 switch ($log->action) {
253 case "add":
254 if ($log->cmid) {
255 //Get the new_id of the module (to recode the info field)
256 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
257 if ($mod) {
258 $log->url = "view.php?id=".$log->cmid;
259 $log->info = $mod->new_id;
260 $status = true;
263 break;
264 case "update":
265 if ($log->cmid) {
266 //Get the new_id of the module (to recode the info field)
267 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
268 if ($mod) {
269 $log->url = "view.php?id=".$log->cmid;
270 $log->info = $mod->new_id;
271 $status = true;
274 break;
275 case "view":
276 if ($log->cmid) {
277 //Get the new_id of the module (to recode the info field)
278 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
279 if ($mod) {
280 $log->url = "view.php?id=".$log->cmid;
281 $log->info = $mod->new_id;
282 $status = true;
285 break;
286 case "add entry":
287 if ($log->cmid) {
288 //Get the new_id of the module (to recode the info field)
289 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
290 if ($mod) {
291 $log->url = "view.php?id=".$log->cmid;
292 $log->info = $mod->new_id;
293 $status = true;
296 break;
297 case "update entry":
298 if ($log->cmid) {
299 //Get the new_id of the module (to recode the info field)
300 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
301 if ($mod) {
302 $log->url = "view.php?id=".$log->cmid;
303 $log->info = $mod->new_id;
304 $status = true;
307 break;
308 case "view responses":
309 if ($log->cmid) {
310 //Get the new_id of the module (to recode the info field)
311 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
312 if ($mod) {
313 $log->url = "report.php?id=".$log->cmid;
314 $log->info = $mod->new_id;
315 $status = true;
318 break;
319 case "update feedback":
320 if ($log->cmid) {
321 $log->url = "report.php?id=".$log->cmid;
322 $status = true;
324 break;
325 case "view all":
326 $log->url = "index.php?id=".$log->course;
327 $status = true;
328 break;
329 default:
330 if (!defined('RESTORE_SILENTLY')) {
331 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
333 break;
336 if ($status) {
337 $status = $log;
339 return $status;