MDL-10092:
[moodle-linuxchix.git] / mod / forum / restorelib.php
blob61101a3e7b6808f89aedd84c749680d60763b02a
1 <?php //$Id$
2 //This php script contains all the stuff to backup/restore
3 //forum mods
5 //This is the "graphical" structure of the forum mod:
6 //
7 // forum
8 // (CL,pk->id)
9 // |
10 // ---------------------------------------------------
11 // | |
12 // subscriptions forum_discussions
13 //(UL,pk->id, fk->forum) ---------------(UL,pk->id, fk->forum)
14 // | |
15 // | |
16 // | |
17 // | forum_posts
18 // |-------------(UL,pk->id,fk->discussion,
19 // | nt->parent,files)
20 // | |
21 // | |
22 // | |
23 // forum_read forum_ratings
24 // (UL,pk->id,fk->post (UL,pk->id,fk->post)
26 // Meaning: pk->primary key field of the table
27 // fk->foreign key to link with parent
28 // nt->nested field (recursive data)
29 // CL->course level info
30 // UL->user level info
31 // files->table may have files)
33 //-----------------------------------------------------------
35 function forum_restore_mods($mod,$restore) {
37 global $CFG,$db;
39 $status = true;
41 //Get record from backup_ids
42 $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
44 if ($data) {
45 //Now get completed xmlized object
46 $info = $data->info;
47 //if necessary, write to restorelog and adjust date/time fields
48 if ($restore->course_startdateoffset) {
49 restore_log_date_changes('Forum', $restore, $info['MOD']['#'], array('ASSESSTIMESTART', 'ASSESSTIMEFINISH'));
51 //traverse_xmlize($info); //Debug
52 //print_object ($GLOBALS['traverse_array']); //Debug
53 //$GLOBALS['traverse_array']=""; //Debug
55 //Now, build the FORUM record structure
56 $forum->course = $restore->course_id;
57 $forum->type = backup_todb($info['MOD']['#']['TYPE']['0']['#']);
58 $forum->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
59 $forum->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
61 // These get dropped in Moodle 1.7 when the new Roles System gets
62 // set up. Therefore they might or not be there depending on whether
63 // we are restoring a 1.6+ forum or a 1.7 or later forum backup.
64 if (isset($info['MOD']['#']['OPEN']['0']['#'])) {
65 $forum->open = backup_todb($info['MOD']['#']['OPEN']['0']['#']);
67 if (isset($info['MOD']['#']['ASSESSPUBLIC']['0']['#'])) {
68 $forum->assesspublic = backup_todb($info['MOD']['#']['ASSESSPUBLIC']['0']['#']);
71 $forum->assessed = backup_todb($info['MOD']['#']['ASSESSED']['0']['#']);
72 $forum->assesstimestart = backup_todb($info['MOD']['#']['ASSESSTIMESTART']['0']['#']);
73 $forum->assesstimefinish = backup_todb($info['MOD']['#']['ASSESSTIMEFINISH']['0']['#']);
74 $forum->maxbytes = backup_todb($info['MOD']['#']['MAXBYTES']['0']['#']);
75 $forum->scale = backup_todb($info['MOD']['#']['SCALE']['0']['#']);
76 $forum->forcesubscribe = backup_todb($info['MOD']['#']['FORCESUBSCRIBE']['0']['#']);
77 $forum->trackingtype = backup_todb($info['MOD']['#']['TRACKINGTYPE']['0']['#']);
78 $forum->rsstype = backup_todb($info['MOD']['#']['RSSTYPE']['0']['#']);
79 $forum->rssarticles = backup_todb($info['MOD']['#']['RSSARTICLES']['0']['#']);
80 $forum->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
81 $forum->warnafter = isset($info['MOD']['#']['WARNAFTER']['0']['#'])?backup_todb($info['MOD']['#']['WARNAFTER']['0']['#']):'';
82 $forum->blockafter = isset($info['MOD']['#']['BLOCKAFTER']['0']['#'])?backup_todb($info['MOD']['#']['BLOCKAFTER']['0']['#']):'';
83 $forum->blockperiod = isset($info['MOD']['#']['BLOCKPERIOD']['0']['#'])?backup_todb($info['MOD']['#']['BLOCKPERIOD']['0']['#']):'';
86 //We have to recode the scale field if it's <0 (positive is a grade, not a scale)
87 if ($forum->scale < 0) {
88 $scale = backup_getid($restore->backup_unique_code,"scale",abs($forum->scale));
89 if ($scale) {
90 $forum->scale = -($scale->new_id);
94 $newid = insert_record("forum", $forum);
97 //Do some output
98 if (!defined('RESTORE_SILENTLY')) {
99 echo "<li>".get_string("modulename","forum")." \"".format_string(stripslashes($forum->name),true)."\"</li>";
101 backup_flush(300);
103 if ($newid) {
104 //We have the newid, update backup_ids
105 backup_putid($restore->backup_unique_code,$mod->modtype,
106 $mod->id, $newid);
108 $forum->id = $newid;
110 //Now check if want to restore user data and do it.
111 if (restore_userdata_selected($restore,'forum',$mod->id)) {
112 //Restore forum_subscriptions
113 $status = forum_subscriptions_restore_mods ($newid,$info,$restore);
114 if ($status) {
115 //Restore forum_discussions
116 $status = forum_discussions_restore_mods ($newid,$info,$restore);
118 if ($status) {
119 //Restore forum_read
120 $status = forum_read_restore_mods ($newid,$info,$restore);
124 // If forum type is single, just recreate the initial discussion/post automatically
125 // if it hasn't been created still (because no user data was selected on backup or
126 // restore.
127 if ($forum->type == 'single' && !record_exists('forum_discussions', 'forum', $newid)) {
128 //Load forum/lib.php
129 require_once ($CFG->dirroot.'/mod/forum/lib.php');
130 // Calculate the default format
131 if (can_use_html_editor()) {
132 $defaultformat = FORMAT_HTML;
133 } else {
134 $defaultformat = FORMAT_MOODLE;
136 //Create discussion/post data
137 $sd = new stdClass;
138 $sd->course = $forum->course;
139 $sd->forum = $newid;
140 $sd->name = $forum->name;
141 $sd->intro = $forum->intro;
142 $sd->assessed = $forum->assessed;
143 $sd->format = $defaultformat;
144 $sd->mailnow = false;
145 //Insert dicussion/post data
146 $sdid = forum_add_discussion($sd, $sd->intro);
147 //Now, mark the initial post of the discussion as mailed!
148 if ($sdid) {
149 set_field ('forum_posts','mailed', '1', 'discussion', $sdid);
153 } else {
154 $status = false;
157 // If the backup contained $forum->open and $forum->assesspublic,
158 // we need to convert the forum to use Roles. It means the backup
159 // was made pre Moodle 1.7.
160 if (isset($forum->open) && isset($forum->assesspublic)) {
162 $forummod = get_record('modules', 'name', 'forum');
164 if (!$teacherroles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW)) {
165 notice('Default teacher role was not found. Roles and permissions '.
166 'for all your forums will have to be manually set.');
168 if (!$studentroles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) {
169 notice('Default student role was not found. Roles and permissions '.
170 'for all your forums will have to be manually set.');
172 if (!$guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) {
173 notice('Default guest role was not found. Roles and permissions '.
174 'for teacher forums will have to be manually set.');
176 require_once($CFG->dirroot.'/mod/forum/lib.php');
177 forum_convert_to_roles($forum, $forummod->id,
178 $teacherroles, $studentroles, $guestroles,
179 $restore->mods['forum']->instances[$mod->id]->restored_as_course_module);
182 } else {
183 $status = false;
186 return $status;
189 //This function restores the forum_subscriptions
190 function forum_subscriptions_restore_mods($forum_id,$info,$restore) {
192 global $CFG;
194 $status = true;
196 //Get the discussions array
197 $subscriptions = array();
198 if (isset($info['MOD']['#']['SUBSCRIPTIONS']['0']['#']['SUBSCRIPTION'])) {
199 $subscriptions = $info['MOD']['#']['SUBSCRIPTIONS']['0']['#']['SUBSCRIPTION'];
202 //Iterate over subscriptions
203 for($i = 0; $i < sizeof($subscriptions); $i++) {
204 $sus_info = $subscriptions[$i];
205 //traverse_xmlize($sus_info); //Debug
206 //print_object ($GLOBALS['traverse_array']); //Debug
207 //$GLOBALS['traverse_array']=""; //Debug
209 //We'll need this later!!
210 $oldid = backup_todb($sus_info['#']['ID']['0']['#']);
211 $olduserid = backup_todb($sus_info['#']['USERID']['0']['#']);
213 //Now, build the FORUM_SUBSCRIPTIONS record structure
214 $subscription->forum = $forum_id;
215 $subscription->userid = backup_todb($sus_info['#']['USERID']['0']['#']);
217 //We have to recode the userid field
218 $user = backup_getid($restore->backup_unique_code,"user",$subscription->userid);
219 if ($user) {
220 $subscription->userid = $user->new_id;
223 //The structure is equal to the db, so insert the forum_subscription
224 $newid = insert_record ("forum_subscriptions",$subscription);
226 //Do some output
227 if (($i+1) % 50 == 0) {
228 if (!defined('RESTORE_SILENTLY')) {
229 echo ".";
230 if (($i+1) % 1000 == 0) {
231 echo "<br />";
234 backup_flush(300);
237 if ($newid) {
238 //We have the newid, update backup_ids
239 backup_putid($restore->backup_unique_code,"forum_subscriptions",$oldid,
240 $newid);
241 } else {
242 $status = false;
246 return $status;
249 //This function restores the forum_discussions
250 function forum_discussions_restore_mods($forum_id,$info,$restore) {
252 global $CFG;
254 $status = true;
256 //Get the discussions array
257 $discussions = array();
259 if (!empty($info['MOD']['#']['DISCUSSIONS']['0']['#']['DISCUSSION'])) {
260 $discussions = $info['MOD']['#']['DISCUSSIONS']['0']['#']['DISCUSSION'];
263 //Iterate over discussions
264 for($i = 0; $i < sizeof($discussions); $i++) {
265 $dis_info = $discussions[$i];
266 //traverse_xmlize($dis_info); //Debug
267 //print_object ($GLOBALS['traverse_array']); //Debug
268 //$GLOBALS['traverse_array']=""; //Debug
270 //We'll need this later!!
271 $oldid = backup_todb($dis_info['#']['ID']['0']['#']);
272 $olduserid = backup_todb($dis_info['#']['USERID']['0']['#']);
274 //Now, build the FORUM_DISCUSSIONS record structure
275 $discussion->forum = $forum_id;
276 $discussion->course = $restore->course_id;
277 $discussion->name = backup_todb($dis_info['#']['NAME']['0']['#']);
278 $discussion->firstpost = backup_todb($dis_info['#']['FIRSTPOST']['0']['#']);
279 $discussion->userid = backup_todb($dis_info['#']['USERID']['0']['#']);
280 $discussion->groupid = backup_todb($dis_info['#']['GROUPID']['0']['#']);
281 $discussion->assessed = backup_todb($dis_info['#']['ASSESSED']['0']['#']);
282 $discussion->timemodified = backup_todb($dis_info['#']['TIMEMODIFIED']['0']['#']);
283 $discussion->timemodified += $restore->course_startdateoffset;
284 $discussion->usermodified = backup_todb($dis_info['#']['USERMODIFIED']['0']['#']);
285 $discussion->timestart = backup_todb($dis_info['#']['TIMESTART']['0']['#']);
286 $discussion->timestart += $restore->course_startdateoffset;
287 $discussion->timeend = backup_todb($dis_info['#']['TIMEEND']['0']['#']);
288 $discussion->timeend += $restore->course_startdateoffset;
289 //We have to recode the userid field
290 $user = backup_getid($restore->backup_unique_code,"user",$discussion->userid);
291 if ($user) {
292 $discussion->userid = $user->new_id;
295 //We have to recode the groupid field
296 $group = backup_getid($restore->backup_unique_code, 'groups', $discussion->groupid);
297 if ($group) {
298 $discussion->groupid = $group->new_id;
301 //We have to recode the usermodified field
302 $user = backup_getid($restore->backup_unique_code,"user",$discussion->usermodified);
303 if ($user) {
304 $discussion->usermodified = $user->new_id;
307 //The structure is equal to the db, so insert the forum_discussions
308 $newid = insert_record ("forum_discussions",$discussion);
310 //Do some output
311 if (($i+1) % 50 == 0) {
312 if (!defined('RESTORE_SILENTLY')) {
313 echo ".";
314 if (($i+1) % 1000 == 0) {
315 echo "<br />";
318 backup_flush(300);
321 if ($newid) {
322 //We have the newid, update backup_ids
323 backup_putid($restore->backup_unique_code,"forum_discussions",$oldid,
324 $newid);
325 //Restore forum_posts
326 $status = forum_posts_restore_mods ($forum_id,$newid,$dis_info,$restore);
327 //Now recalculate firstpost field
328 $old_firstpost = $discussion->firstpost;
329 //Get its new post_id from backup_ids table
330 $rec = backup_getid($restore->backup_unique_code,"forum_posts",$old_firstpost);
331 if ($rec) {
332 //Put its new firstpost
333 $discussion->firstpost = $rec->new_id;
334 if ($post = get_record("forum_posts", "id", $discussion->firstpost)) {
335 $discussion->userid = $post->userid;
337 } else {
338 $discussion->firstpost = 0;
339 $discussion->userid = 0;
341 //Create temp discussion record
342 $temp_discussion->id = $newid;
343 $temp_discussion->firstpost = $discussion->firstpost;
344 $temp_discussion->userid = $discussion->userid;
345 //Update discussion (only firstpost and userid will be changed)
346 $status = update_record("forum_discussions",$temp_discussion);
347 //echo "Updated firstpost ".$old_firstpost." to ".$temp_discussion->firstpost."<br />"; //Debug
348 } else {
349 $status = false;
353 return $status;
356 //This function restores the forum_read
357 function forum_read_restore_mods($forum_id,$info,$restore) {
359 global $CFG;
361 $status = true;
363 //Get the read array
364 $readposts = array();
365 if (isset($info['MOD']['#']['READPOSTS']['0']['#']['READ'])) {
366 $readposts = $info['MOD']['#']['READPOSTS']['0']['#']['READ'];
369 //Iterate over readposts
370 for($i = 0; $i < sizeof($readposts); $i++) {
371 $rea_info = $readposts[$i];
372 //traverse_xmlize($rea_info); //Debug
373 //print_object ($GLOBALS['traverse_array']); //Debug
374 //$GLOBALS['traverse_array']=""; //Debug
376 //We'll need this later!!
377 $oldid = backup_todb($rea_info['#']['ID']['0']['#']);
379 //Now, build the FORUM_READ record structure
380 $read->forumid = $forum_id;
381 $read->userid = backup_todb($rea_info['#']['USERID']['0']['#']);
382 $read->discussionid = backup_todb($rea_info['#']['DISCUSSIONID']['0']['#']);
383 $read->postid = backup_todb($rea_info['#']['POSTID']['0']['#']);
384 $read->firstread = backup_todb($rea_info['#']['FIRSTREAD']['0']['#']);
385 $read->lastread = backup_todb($rea_info['#']['LASTREAD']['0']['#']);
387 //Some recoding and check are performed now
388 $toinsert = true;
390 //We have to recode the userid field
391 $user = backup_getid($restore->backup_unique_code,"user",$read->userid);
392 if ($user) {
393 $read->userid = $user->new_id;
394 } else {
395 $toinsert = false;
398 //We have to recode the discussionid field
399 $discussion = backup_getid($restore->backup_unique_code,"forum_discussions",$read->discussionid);
400 if ($discussion) {
401 $read->discussionid = $discussion->new_id;
402 } else {
403 $toinsert = false;
406 //We have to recode the postid field
407 $post = backup_getid($restore->backup_unique_code,"forum_posts",$read->postid);
408 if ($post) {
409 $read->postid = $post->new_id;
410 } else {
411 $toinsert = false;
414 //The structure is equal to the db, so insert the forum_read
415 $newid = 0;
416 if ($toinsert) {
417 $newid = insert_record ("forum_read",$read);
420 //Do some output
421 if (($i+1) % 50 == 0) {
422 if (!defined('RESTORE_SILENTLY')) {
423 echo ".";
424 if (($i+1) % 1000 == 0) {
425 echo "<br />";
428 backup_flush(300);
431 if ($newid) {
432 //We have the newid, update backup_ids
433 backup_putid($restore->backup_unique_code,"forum_read",$oldid,
434 $newid);
435 } else {
436 $status = false;
440 return $status;
443 //This function restores the forum_posts
444 function forum_posts_restore_mods($new_forum_id,$discussion_id,$info,$restore) {
446 global $CFG;
448 $status = true;
450 //Get the posts array
451 $posts = $info['#']['POSTS']['0']['#']['POST'];
453 //Iterate over posts
454 for($i = 0; $i < sizeof($posts); $i++) {
455 $pos_info = $posts[$i];
456 //traverse_xmlize($pos_info); //Debug
457 //print_object ($GLOBALS['traverse_array']); //Debug
458 //$GLOBALS['traverse_array']=""; //Debug
460 //We'll need this later!!
461 $oldid = backup_todb($pos_info['#']['ID']['0']['#']);
462 $olduserid = backup_todb($pos_info['#']['USERID']['0']['#']);
464 //Now, build the FORUM_POSTS record structure
465 $post->discussion = $discussion_id;
466 $post->parent = backup_todb($pos_info['#']['PARENT']['0']['#']);
467 $post->userid = backup_todb($pos_info['#']['USERID']['0']['#']);
468 $post->created = backup_todb($pos_info['#']['CREATED']['0']['#']);
469 $post->created += $restore->course_startdateoffset;
470 $post->modified = backup_todb($pos_info['#']['MODIFIED']['0']['#']);
471 $post->modified += $restore->course_startdateoffset;
472 $post->mailed = backup_todb($pos_info['#']['MAILED']['0']['#']);
473 $post->subject = backup_todb($pos_info['#']['SUBJECT']['0']['#']);
474 $post->message = backup_todb($pos_info['#']['MESSAGE']['0']['#']);
475 $post->format = backup_todb($pos_info['#']['FORMAT']['0']['#']);
476 $post->attachment = backup_todb($pos_info['#']['ATTACHMENT']['0']['#']);
477 $post->totalscore = backup_todb($pos_info['#']['TOTALSCORE']['0']['#']);
478 $post->mailnow = backup_todb($pos_info['#']['MAILNOW']['0']['#']);
480 //We have to recode the userid field
481 $user = backup_getid($restore->backup_unique_code,"user",$post->userid);
482 if ($user) {
483 $post->userid = $user->new_id;
486 //The structure is equal to the db, so insert the forum_posts
487 $newid = insert_record ("forum_posts",$post);
489 //Do some output
490 if (($i+1) % 50 == 0) {
491 if (!defined('RESTORE_SILENTLY')) {
492 echo ".";
493 if (($i+1) % 1000 == 0) {
494 echo "<br />";
497 backup_flush(300);
500 if ($newid) {
501 //We have the newid, update backup_ids
502 backup_putid($restore->backup_unique_code,"forum_posts",$oldid,
503 $newid);
505 //Get old forum id from backup_ids
506 $rec = get_record("backup_ids","backup_code",$restore->backup_unique_code,
507 "table_name","forum",
508 "new_id",$new_forum_id);
509 //Now copy moddata associated files
510 $status = forum_restore_files ($rec->old_id, $new_forum_id,
511 $oldid, $newid, $restore);
513 //Now restore post ratings
514 $status = forum_ratings_restore_mods($newid,$pos_info,$restore);
516 } else {
517 $status = false;
521 //Now we get every post in this discussion_id and recalculate its parent post
522 $posts = get_records ("forum_posts","discussion",$discussion_id);
523 if ($posts) {
524 //Iterate over each post
525 foreach ($posts as $post) {
526 //Get its parent
527 $old_parent = $post->parent;
528 //Get its new post_id from backup_ids table
529 $rec = backup_getid($restore->backup_unique_code,"forum_posts",$old_parent);
530 if ($rec) {
531 //Put its new parent
532 $post->parent = $rec->new_id;
533 } else {
534 $post->parent = 0;
536 //Create temp post record
537 $temp_post->id = $post->id;
538 $temp_post->parent = $post->parent;
539 //echo "Updated parent ".$old_parent." to ".$temp_post->parent."<br />"; //Debug
540 //Update post (only parent will be changed)
541 $status = update_record("forum_posts",$temp_post);
545 return $status;
548 //This function copies the forum related info from backup temp dir to course moddata folder,
549 //creating it if needed and recoding everything (forum id and post id)
550 function forum_restore_files ($oldforid, $newforid, $oldpostid, $newpostid, $restore) {
552 global $CFG;
554 $status = true;
555 $todo = false;
556 $moddata_path = "";
557 $forum_path = "";
558 $temp_path = "";
560 //First, we check to "course_id" exists and create is as necessary
561 //in CFG->dataroot
562 $dest_dir = $CFG->dataroot."/".$restore->course_id;
563 $status = check_dir_exists($dest_dir,true);
565 //First, locate course's moddata directory
566 $moddata_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
568 //Check it exists and create it
569 $status = check_dir_exists($moddata_path,true);
571 //Now, locate forum directory
572 if ($status) {
573 $forum_path = $moddata_path."/forum";
574 //Check it exists and create it
575 $status = check_dir_exists($forum_path,true);
578 //Now locate the temp dir we are restoring from
579 if ($status) {
580 $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
581 "/moddata/forum/".$oldforid."/".$oldpostid;
582 //Check it exists
583 if (is_dir($temp_path)) {
584 $todo = true;
588 //If todo, we create the neccesary dirs in course moddata/forum
589 if ($status and $todo) {
590 //First this forum id
591 $this_forum_path = $forum_path."/".$newforid;
592 $status = check_dir_exists($this_forum_path,true);
593 //Now this post id
594 $post_forum_path = $this_forum_path."/".$newpostid;
595 //And now, copy temp_path to post_forum_path
596 $status = backup_copy_file($temp_path, $post_forum_path);
599 return $status;
602 //This function restores the forum_ratings
603 function forum_ratings_restore_mods($new_post_id,$info,$restore) {
605 global $CFG;
607 $status = true;
609 //Get the ratings array
610 $ratings = array();
611 if (isset($info['#']['RATINGS']['0']['#']['RATING'])) {
612 $ratings = $info['#']['RATINGS']['0']['#']['RATING'];
615 //Iterate over ratings
616 for($i = 0; $i < sizeof($ratings); $i++) {
617 $rat_info = $ratings[$i];
618 //traverse_xmlize($rat_info); //Debug
619 //print_object ($GLOBALS['traverse_array']); //Debug
620 //$GLOBALS['traverse_array']=""; //Debug
622 //We'll need this later!!
623 $oldid = backup_todb($rat_info['#']['ID']['0']['#']);
624 $olduserid = backup_todb($rat_info['#']['USERID']['0']['#']);
626 //Now, build the FORM_RATINGS record structure
627 $rating->post = $new_post_id;
628 $rating->userid = backup_todb($rat_info['#']['USERID']['0']['#']);
629 $rating->time = backup_todb($rat_info['#']['TIME']['0']['#']);
630 $rating->rating = backup_todb($rat_info['#']['POST_RATING']['0']['#']);
632 //We have to recode the userid field
633 $user = backup_getid($restore->backup_unique_code,"user",$rating->userid);
634 if ($user) {
635 $rating->userid = $user->new_id;
638 //The structure is equal to the db, so insert the forum_ratings
639 $newid = insert_record ("forum_ratings",$rating);
641 //Do some output
642 if (($i+1) % 50 == 0) {
643 if (!defined('RESTORE_SILENTLY')) {
644 echo ".";
645 if (($i+1) % 1000 == 0) {
646 echo "<br />";
649 backup_flush(300);
652 if ($newid) {
653 //We have the newid, update backup_ids
654 backup_putid($restore->backup_unique_code,"forum_ratings",$oldid,
655 $newid);
656 } else {
657 $status = false;
661 return $status;
664 //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
665 //some texts in the module
666 function forum_restore_wiki2markdown ($restore) {
668 global $CFG;
670 $status = true;
672 //Convert forum_posts->message
673 if ($records = get_records_sql ("SELECT p.id, p.message, p.format
674 FROM {$CFG->prefix}forum_posts p,
675 {$CFG->prefix}forum_discussions d,
676 {$CFG->prefix}forum f,
677 {$CFG->prefix}backup_ids b
678 WHERE d.id = p.discussion AND
679 f.id = d.forum AND
680 f.course = $restore->course_id AND
681 p.format = ".FORMAT_WIKI. " AND
682 b.backup_code = $restore->backup_unique_code AND
683 b.table_name = 'forum_posts' AND
684 b.new_id = p.id")) {
685 foreach ($records as $record) {
686 //Rebuild wiki links
687 $record->message = restore_decode_wiki_content($record->message, $restore);
688 //Convert to Markdown
689 $wtm = new WikiToMarkdown();
690 $record->message = $wtm->convert($record->message, $restore->course_id);
691 $record->format = FORMAT_MARKDOWN;
692 $status = update_record('forum_posts', addslashes_object($record));
693 //Do some output
694 $i++;
695 if (($i+1) % 1 == 0) {
696 if (!defined('RESTORE_SILENTLY')) {
697 echo ".";
698 if (($i+1) % 20 == 0) {
699 echo "<br />";
702 backup_flush(300);
707 return $status;
710 //This function returns a log record with all the necessay transformations
711 //done. It's used by restore_log_module() to restore modules log.
712 function forum_restore_logs($restore,$log) {
714 $status = false;
716 //Depending of the action, we recode different things
717 switch ($log->action) {
718 case "add":
719 if ($log->cmid) {
720 //Get the new_id of the module (to recode the info field)
721 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
722 if ($mod) {
723 $log->url = "view.php?id=".$log->cmid;
724 $log->info = $mod->new_id;
725 $status = true;
728 break;
729 case "mark read":
730 if ($log->cmid) {
731 //Get the new_id of the module (to recode the url and info fields)
732 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
733 if ($mod) {
734 $log->url = "view.php?f=".$mod->new_id;
735 $log->info = $mod->new_id;
736 $status = true;
739 break;
740 case "start tracking":
741 if ($log->cmid) {
742 //Get the new_id of the module (to recode the url and info fields)
743 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
744 if ($mod) {
745 $log->url = "view.php?f=".$mod->new_id;
746 $log->info = $mod->new_id;
747 $status = true;
750 break;
751 case "update":
752 if ($log->cmid) {
753 //Get the new_id of the module (to recode the info field)
754 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
755 if ($mod) {
756 $log->url = "view.php?id=".$log->cmid;
757 $log->info = $mod->new_id;
758 $status = true;
761 break;
762 case "view forum":
763 if ($log->cmid) {
764 //Get the new_id of the module (to recode the info field)
765 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
766 if ($mod) {
767 $log->url = "view.php?id=".$log->cmid;
768 $log->info = $mod->new_id;
769 $status = true;
772 break;
773 case "view forums":
774 $log->url = "index.php?id=".$log->course;
775 $status = true;
776 break;
777 case "subscribe":
778 if ($log->cmid) {
779 //Get the new_id of the module (to recode the info and url field)
780 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
781 if ($mod) {
782 $log->url = "view.php?f=".$mod->new_id;
783 $log->info = $mod->new_id;
784 $status = true;
787 break;
788 case "view subscriber":
789 if ($log->cmid) {
790 //Get the new_id of the module (to recode the info and field)
791 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
792 if ($mod) {
793 $log->url = "subscribers.php?id=".$mod->new_id;
794 $log->info = $mod->new_id;
795 $status = true;
798 break;
799 case "unsubscribe":
800 if ($log->cmid) {
801 //Get the new_id of the module (to recode the info and url field)
802 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
803 if ($mod) {
804 $log->url = "view.php?f=".$mod->new_id;
805 $log->info = $mod->new_id;
806 $status = true;
809 break;
810 case "add discussion":
811 if ($log->cmid) {
812 //Get the new_id of the discussion (to recode the info and url field)
813 $dis = backup_getid($restore->backup_unique_code,"forum_discussions",$log->info);
814 if ($dis) {
815 $log->url = "discuss.php?d=".$dis->new_id;
816 $log->info = $dis->new_id;
817 $status = true;
820 break;
821 case "view discussion":
822 if ($log->cmid) {
823 //Get the new_id of the discussion (to recode the info and url field)
824 $dis = backup_getid($restore->backup_unique_code,"forum_discussions",$log->info);
825 if ($dis) {
826 $log->url = "discuss.php?d=".$dis->new_id;
827 $log->info = $dis->new_id;
828 $status = true;
831 break;
832 case "move discussion":
833 if ($log->cmid) {
834 //Get the new_id of the discussion (to recode the info and url field)
835 $dis = backup_getid($restore->backup_unique_code,"forum_discussions",$log->info);
836 if ($dis) {
837 $log->url = "discuss.php?d=".$dis->new_id;
838 $log->info = $dis->new_id;
839 $status = true;
842 break;
843 case "delete discussi":
844 if ($log->cmid) {
845 //Get the new_id of the module (to recode the info field)
846 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
847 if ($mod) {
848 $log->url = "view.php?id=".$log->cmid;
849 $log->info = $mod->new_id;
850 $status = true;
853 break;
854 case "add post":
855 if ($log->cmid) {
856 //Get the new_id of the post (to recode the url and info field)
857 $pos = backup_getid($restore->backup_unique_code,"forum_posts",$log->info);
858 if ($pos) {
859 //Get the post record from database
860 $dbpos = get_record("forum_posts","id","$pos->new_id");
861 if ($dbpos) {
862 $log->url = "discuss.php?d=".$dbpos->discussion."&parent=".$pos->new_id;
863 $log->info = $pos->new_id;
864 $status = true;
868 break;
869 case "prune post":
870 if ($log->cmid) {
871 //Get the new_id of the post (to recode the url and info field)
872 $pos = backup_getid($restore->backup_unique_code,"forum_posts",$log->info);
873 if ($pos) {
874 //Get the post record from database
875 $dbpos = get_record("forum_posts","id","$pos->new_id");
876 if ($dbpos) {
877 $log->url = "discuss.php?d=".$dbpos->discussion;
878 $log->info = $pos->new_id;
879 $status = true;
883 break;
884 case "update post":
885 if ($log->cmid) {
886 //Get the new_id of the post (to recode the url and info field)
887 $pos = backup_getid($restore->backup_unique_code,"forum_posts",$log->info);
888 if ($pos) {
889 //Get the post record from database
890 $dbpos = get_record("forum_posts","id","$pos->new_id");
891 if ($dbpos) {
892 $log->url = "discuss.php?d=".$dbpos->discussion."&parent=".$pos->new_id;
893 $log->info = $pos->new_id;
894 $status = true;
898 break;
899 case "delete post":
900 if ($log->cmid) {
901 //Extract the discussion id from the url field
902 $disid = substr(strrchr($log->url,"="),1);
903 //Get the new_id of the discussion (to recode the url field)
904 $dis = backup_getid($restore->backup_unique_code,"quiz_discussions",$disid);
905 if ($dis) {
906 $log->url = "discuss.php?d=".$dis->new_id;
907 $status = true;
910 break;
911 case "search":
912 $log->url = "search.php?id=".$log->course."&search=".urlencode($log->info);
913 $status = true;
914 break;
915 default:
916 if (!defined('RESTORE_SILENTLY')) {
917 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
919 break;
922 if ($status) {
923 $status = $log;
925 return $status;
928 //Return a content decoded to support interactivities linking. Every module
929 //should have its own. They are called automatically from
930 //forum_decode_content_links_caller() function in each module
931 //in the restore process
932 function forum_decode_content_links ($content,$restore) {
934 global $CFG;
936 $result = $content;
938 //Link to the list of forums
940 $searchstring='/\$@(FORUMINDEX)\*([0-9]+)@\$/';
941 //We look for it
942 preg_match_all($searchstring,$content,$foundset);
943 //If found, then we are going to look for its new id (in backup tables)
944 if ($foundset[0]) {
945 //print_object($foundset); //Debug
946 //Iterate over foundset[2]. They are the old_ids
947 foreach($foundset[2] as $old_id) {
948 //We get the needed variables here (course id)
949 $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
950 //Personalize the searchstring
951 $searchstring='/\$@(FORUMINDEX)\*('.$old_id.')@\$/';
952 //If it is a link to this course, update the link to its new location
953 if($rec->new_id) {
954 //Now replace it
955 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/index.php?id='.$rec->new_id,$result);
956 } else {
957 //It's a foreign link so leave it as original
958 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/forum/index.php?id='.$old_id,$result);
963 //Link to forum view by moduleid
965 $searchstring='/\$@(FORUMVIEWBYID)\*([0-9]+)@\$/';
966 //We look for it
967 preg_match_all($searchstring,$result,$foundset);
968 //If found, then we are going to look for its new id (in backup tables)
969 if ($foundset[0]) {
970 //print_object($foundset); //Debug
971 //Iterate over foundset[2]. They are the old_ids
972 foreach($foundset[2] as $old_id) {
973 //We get the needed variables here (course_modules id)
974 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
975 //Personalize the searchstring
976 $searchstring='/\$@(FORUMVIEWBYID)\*('.$old_id.')@\$/';
977 //If it is a link to this course, update the link to its new location
978 if($rec->new_id) {
979 //Now replace it
980 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/view.php?id='.$rec->new_id,$result);
981 } else {
982 //It's a foreign link so leave it as original
983 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/forum/view.php?id='.$old_id,$result);
988 //Link to forum view by forumid
990 $searchstring='/\$@(FORUMVIEWBYF)\*([0-9]+)@\$/';
991 //We look for it
992 preg_match_all($searchstring,$result,$foundset);
993 //If found, then we are going to look for its new id (in backup tables)
994 if ($foundset[0]) {
995 //print_object($foundset); //Debug
996 //Iterate over foundset[2]. They are the old_ids
997 foreach($foundset[2] as $old_id) {
998 //We get the needed variables here (forum id)
999 $rec = backup_getid($restore->backup_unique_code,"forum",$old_id);
1000 //Personalize the searchstring
1001 $searchstring='/\$@(FORUMVIEWBYF)\*('.$old_id.')@\$/';
1002 //If it is a link to this course, update the link to its new location
1003 if($rec->new_id) {
1004 //Now replace it
1005 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/view.php?f='.$rec->new_id,$result);
1006 } else {
1007 //It's a foreign link so leave it as original
1008 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/forum/view.php?f='.$old_id,$result);
1013 //Link to forum discussion by discussionid
1015 $searchstring='/\$@(FORUMDISCUSSIONVIEW)\*([0-9]+)@\$/';
1016 //We look for it
1017 preg_match_all($searchstring,$result,$foundset);
1018 //If found, then we are going to look for its new id (in backup tables)
1019 if ($foundset[0]) {
1020 //print_object($foundset); //Debug
1021 //Iterate over foundset[2]. They are the old_ids
1022 foreach($foundset[2] as $old_id) {
1023 //We get the needed variables here (discussion id)
1024 $rec = backup_getid($restore->backup_unique_code,"forum_discussions",$old_id);
1025 //Personalize the searchstring
1026 $searchstring='/\$@(FORUMDISCUSSIONVIEW)\*('.$old_id.')@\$/';
1027 //If it is a link to this course, update the link to its new location
1028 if($rec->new_id) {
1029 //Now replace it
1030 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/discuss.php?d='.$rec->new_id,$result);
1031 } else {
1032 //It's a foreign link so leave it as original
1033 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/forum/discuss.php?d='.$old_id,$result);
1038 //Link to forum discussion with parent syntax
1040 $searchstring='/\$@(FORUMDISCUSSIONVIEWPARENT)\*([0-9]+)\*([0-9]+)@\$/';
1041 //We look for it
1042 preg_match_all($searchstring,$result,$foundset);
1043 //If found, then we are going to look for its new id (in backup tables)
1044 if ($foundset[0]) {
1045 //print_object($foundset); //Debug
1046 //Iterate over foundset[2] and foundset[3]. They are the old_ids
1047 foreach($foundset[2] as $key => $old_id) {
1048 $old_id2 = $foundset[3][$key];
1049 //We get the needed variables here (discussion id and post id)
1050 $rec = backup_getid($restore->backup_unique_code,"forum_discussions",$old_id);
1051 $rec2 = backup_getid($restore->backup_unique_code,"forum_posts",$old_id2);
1052 //Personalize the searchstring
1053 $searchstring='/\$@(FORUMDISCUSSIONVIEWPARENT)\*('.$old_id.')\*('.$old_id2.')@\$/';
1054 //If it is a link to this course, update the link to its new location
1055 if($rec->new_id && $rec2->new_id) {
1056 //Now replace it
1057 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/discuss.php?d='.$rec->new_id.'&parent='.$rec2->new_id,$result);
1058 } else {
1059 //It's a foreign link so leave it as original
1060 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/forum/discuss.php?d='.$old_id.'&parent='.$old_id2,$result);
1065 //Link to forum discussion with relative syntax
1067 $searchstring='/\$@(FORUMDISCUSSIONVIEWINSIDE)\*([0-9]+)\*([0-9]+)@\$/';
1068 //We look for it
1069 preg_match_all($searchstring,$result,$foundset);
1070 //If found, then we are going to look for its new id (in backup tables)
1071 if ($foundset[0]) {
1072 //print_object($foundset); //Debug
1073 //Iterate over foundset[2] and foundset[3]. They are the old_ids
1074 foreach($foundset[2] as $key => $old_id) {
1075 $old_id2 = $foundset[3][$key];
1076 //We get the needed variables here (discussion id and post id)
1077 $rec = backup_getid($restore->backup_unique_code,"forum_discussions",$old_id);
1078 $rec2 = backup_getid($restore->backup_unique_code,"forum_posts",$old_id2);
1079 //Personalize the searchstring
1080 $searchstring='/\$@(FORUMDISCUSSIONVIEWINSIDE)\*('.$old_id.')\*('.$old_id2.')@\$/';
1081 //If it is a link to this course, update the link to its new location
1082 if($rec->new_id && $rec2->new_id) {
1083 //Now replace it
1084 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/discuss.php?d='.$rec->new_id.'#'.$rec2->new_id,$result);
1085 } else {
1086 //It's a foreign link so leave it as original
1087 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/forum/discuss.php?d='.$old_id.'#'.$old_id2,$result);
1092 return $result;
1095 //This function makes all the necessary calls to xxxx_decode_content_links()
1096 //function in each module, passing them the desired contents to be decoded
1097 //from backup format to destination site/course in order to mantain inter-activities
1098 //working in the backup/restore process. It's called from restore_decode_content_links()
1099 //function in restore process
1100 function forum_decode_content_links_caller($restore) {
1101 global $CFG;
1102 $status = true;
1104 //Process every POST (message) in the course
1105 if ($posts = get_records_sql ("SELECT p.id, p.message
1106 FROM {$CFG->prefix}forum_posts p,
1107 {$CFG->prefix}forum_discussions d
1108 WHERE d.course = $restore->course_id AND
1109 p.discussion = d.id")) {
1110 //Iterate over each post->message
1111 $i = 0; //Counter to send some output to the browser to avoid timeouts
1112 foreach ($posts as $post) {
1113 //Increment counter
1114 $i++;
1115 $content = $post->message;
1116 $result = restore_decode_content_links_worker($content,$restore);
1117 if ($result != $content) {
1118 //Update record
1119 $post->message = addslashes($result);
1120 $status = update_record("forum_posts",$post);
1121 if (debugging()) {
1122 if (!defined('RESTORE_SILENTLY')) {
1123 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
1127 //Do some output
1128 if (($i+1) % 5 == 0) {
1129 if (!defined('RESTORE_SILENTLY')) {
1130 echo ".";
1131 if (($i+1) % 100 == 0) {
1132 echo "<br />";
1135 backup_flush(300);
1140 //Process every FORUM (intro) in the course
1141 if ($forums = get_records_sql ("SELECT f.id, f.intro
1142 FROM {$CFG->prefix}forum f
1143 WHERE f.course = $restore->course_id")) {
1144 //Iterate over each forum->intro
1145 $i = 0; //Counter to send some output to the browser to avoid timeouts
1146 foreach ($forums as $forum) {
1147 //Increment counter
1148 $i++;
1149 $content = $forum->intro;
1150 $result = restore_decode_content_links_worker($content,$restore);
1151 if ($result != $content) {
1152 //Update record
1153 $forum->intro = addslashes($result);
1154 $status = update_record("forum",$forum);
1155 if (debugging()) {
1156 if (!defined('RESTORE_SILENTLY')) {
1157 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
1161 //Do some output
1162 if (($i+1) % 5 == 0) {
1163 if (!defined('RESTORE_SILENTLY')) {
1164 echo ".";
1165 if (($i+1) % 100 == 0) {
1166 echo "<br />";
1169 backup_flush(300);
1174 return $status;