2 //This php script contains all the stuff to backup/restore
5 //This is the "graphical" structure of the forum mod:
10 // ---------------------------------------------------
12 // subscriptions forum_discussions
13 //(UL,pk->id, fk->forum) ---------------(UL,pk->id, fk->forum)
18 // |-------------(UL,pk->id,fk->discussion,
19 // | nt->parent,files)
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) {
41 //Get record from backup_ids
42 $data = backup_getid($restore->backup_unique_code
,$mod->modtype
,$mod->id
);
45 //Now get completed xmlized object
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
));
90 $forum->scale
= -($scale->new_id
);
94 $newid = insert_record("forum", $forum);
98 if (!defined('RESTORE_SILENTLY')) {
99 echo "<li>".get_string("modulename","forum")." \"".format_string(stripslashes($forum->name
),true)."\"</li>";
104 //We have the newid, update backup_ids
105 backup_putid($restore->backup_unique_code
,$mod->modtype
,
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);
115 //Restore forum_discussions
116 $status = forum_discussions_restore_mods ($newid,$info,$restore);
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
127 if ($forum->type
== 'single' && !record_exists('forum_discussions', 'forum', $newid)) {
129 require_once ($CFG->dirroot
.'/mod/forum/lib.php');
130 // Calculate the default format
131 if (can_use_html_editor()) {
132 $defaultformat = FORMAT_HTML
;
134 $defaultformat = FORMAT_MOODLE
;
136 //Create discussion/post data
138 $sd->course
= $forum->course
;
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!
149 set_field ('forum_posts','mailed', '1', 'discussion', $sdid);
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
);
189 //This function restores the forum_subscriptions
190 function forum_subscriptions_restore_mods($forum_id,$info,$restore) {
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
);
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);
227 if (($i+
1) %
50 == 0) {
228 if (!defined('RESTORE_SILENTLY')) {
230 if (($i+
1) %
1000 == 0) {
238 //We have the newid, update backup_ids
239 backup_putid($restore->backup_unique_code
,"forum_subscriptions",$oldid,
249 //This function restores the forum_discussions
250 function forum_discussions_restore_mods($forum_id,$info,$restore) {
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
);
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
);
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
);
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);
311 if (($i+
1) %
50 == 0) {
312 if (!defined('RESTORE_SILENTLY')) {
314 if (($i+
1) %
1000 == 0) {
322 //We have the newid, update backup_ids
323 backup_putid($restore->backup_unique_code
,"forum_discussions",$oldid,
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);
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
;
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
356 //This function restores the forum_read
357 function forum_read_restore_mods($forum_id,$info,$restore) {
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
390 //We have to recode the userid field
391 $user = backup_getid($restore->backup_unique_code
,"user",$read->userid
);
393 $read->userid
= $user->new_id
;
398 //We have to recode the discussionid field
399 $discussion = backup_getid($restore->backup_unique_code
,"forum_discussions",$read->discussionid
);
401 $read->discussionid
= $discussion->new_id
;
406 //We have to recode the postid field
407 $post = backup_getid($restore->backup_unique_code
,"forum_posts",$read->postid
);
409 $read->postid
= $post->new_id
;
414 //The structure is equal to the db, so insert the forum_read
417 $newid = insert_record ("forum_read",$read);
421 if (($i+
1) %
50 == 0) {
422 if (!defined('RESTORE_SILENTLY')) {
424 if (($i+
1) %
1000 == 0) {
432 //We have the newid, update backup_ids
433 backup_putid($restore->backup_unique_code
,"forum_read",$oldid,
443 //This function restores the forum_posts
444 function forum_posts_restore_mods($new_forum_id,$discussion_id,$info,$restore) {
450 //Get the posts array
451 $posts = $info['#']['POSTS']['0']['#']['POST'];
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
);
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);
490 if (($i+
1) %
50 == 0) {
491 if (!defined('RESTORE_SILENTLY')) {
493 if (($i+
1) %
1000 == 0) {
501 //We have the newid, update backup_ids
502 backup_putid($restore->backup_unique_code
,"forum_posts",$oldid,
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);
521 //Now we get every post in this discussion_id and recalculate its parent post
522 $posts = get_records ("forum_posts","discussion",$discussion_id);
524 //Iterate over each post
525 foreach ($posts as $post) {
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);
532 $post->parent
= $rec->new_id
;
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);
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) {
560 //First, we check to "course_id" exists and create is as necessary
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
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
580 $temp_path = $CFG->dataroot
."/temp/backup/".$restore->backup_unique_code
.
581 "/moddata/forum/".$oldforid."/".$oldpostid;
583 if (is_dir($temp_path)) {
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);
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);
602 //This function restores the forum_ratings
603 function forum_ratings_restore_mods($new_post_id,$info,$restore) {
609 //Get the 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
);
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);
642 if (($i+
1) %
50 == 0) {
643 if (!defined('RESTORE_SILENTLY')) {
645 if (($i+
1) %
1000 == 0) {
653 //We have the newid, update backup_ids
654 backup_putid($restore->backup_unique_code
,"forum_ratings",$oldid,
664 //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
665 //some texts in the module
666 function forum_restore_wiki2markdown ($restore) {
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
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
685 foreach ($records as $record) {
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));
695 if (($i+
1) %
1 == 0) {
696 if (!defined('RESTORE_SILENTLY')) {
698 if (($i+
1) %
20 == 0) {
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) {
716 //Depending of the action, we recode different things
717 switch ($log->action
) {
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
);
723 $log->url
= "view.php?id=".$log->cmid
;
724 $log->info
= $mod->new_id
;
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
);
734 $log->url
= "view.php?f=".$mod->new_id
;
735 $log->info
= $mod->new_id
;
740 case "start tracking":
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
);
745 $log->url
= "view.php?f=".$mod->new_id
;
746 $log->info
= $mod->new_id
;
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
);
756 $log->url
= "view.php?id=".$log->cmid
;
757 $log->info
= $mod->new_id
;
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
);
767 $log->url
= "view.php?id=".$log->cmid
;
768 $log->info
= $mod->new_id
;
774 $log->url
= "index.php?id=".$log->course
;
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
);
782 $log->url
= "view.php?f=".$mod->new_id
;
783 $log->info
= $mod->new_id
;
788 case "view subscriber":
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
);
793 $log->url
= "subscribers.php?id=".$mod->new_id
;
794 $log->info
= $mod->new_id
;
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
);
804 $log->url
= "view.php?f=".$mod->new_id
;
805 $log->info
= $mod->new_id
;
810 case "add discussion":
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
);
815 $log->url
= "discuss.php?d=".$dis->new_id
;
816 $log->info
= $dis->new_id
;
821 case "view discussion":
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
);
826 $log->url
= "discuss.php?d=".$dis->new_id
;
827 $log->info
= $dis->new_id
;
832 case "move discussion":
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
);
837 $log->url
= "discuss.php?d=".$dis->new_id
;
838 $log->info
= $dis->new_id
;
843 case "delete discussi":
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
);
848 $log->url
= "view.php?id=".$log->cmid
;
849 $log->info
= $mod->new_id
;
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
);
859 //Get the post record from database
860 $dbpos = get_record("forum_posts","id","$pos->new_id");
862 $log->url
= "discuss.php?d=".$dbpos->discussion
."&parent=".$pos->new_id
;
863 $log->info
= $pos->new_id
;
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
);
874 //Get the post record from database
875 $dbpos = get_record("forum_posts","id","$pos->new_id");
877 $log->url
= "discuss.php?d=".$dbpos->discussion
;
878 $log->info
= $pos->new_id
;
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
);
889 //Get the post record from database
890 $dbpos = get_record("forum_posts","id","$pos->new_id");
892 $log->url
= "discuss.php?d=".$dbpos->discussion
."&parent=".$pos->new_id
;
893 $log->info
= $pos->new_id
;
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);
906 $log->url
= "discuss.php?d=".$dis->new_id
;
912 $log->url
= "search.php?id=".$log->course
."&search=".urlencode($log->info
);
916 if (!defined('RESTORE_SILENTLY')) {
917 echo "action (".$log->module
."-".$log->action
.") unknown. Not restored<br />"; //Debug
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) {
938 //Link to the list of forums
940 $searchstring='/\$@(FORUMINDEX)\*([0-9]+)@\$/';
942 preg_match_all($searchstring,$content,$foundset);
943 //If found, then we are going to look for its new id (in backup tables)
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
955 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/forum/index.php?id='.$rec->new_id
,$result);
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]+)@\$/';
967 preg_match_all($searchstring,$result,$foundset);
968 //If found, then we are going to look for its new id (in backup tables)
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
980 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/forum/view.php?id='.$rec->new_id
,$result);
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]+)@\$/';
992 preg_match_all($searchstring,$result,$foundset);
993 //If found, then we are going to look for its new id (in backup tables)
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
1005 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/forum/view.php?f='.$rec->new_id
,$result);
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]+)@\$/';
1017 preg_match_all($searchstring,$result,$foundset);
1018 //If found, then we are going to look for its new id (in backup tables)
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
1030 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/forum/discuss.php?d='.$rec->new_id
,$result);
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]+)@\$/';
1042 preg_match_all($searchstring,$result,$foundset);
1043 //If found, then we are going to look for its new id (in backup tables)
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
) {
1057 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/forum/discuss.php?d='.$rec->new_id
.'&parent='.$rec2->new_id
,$result);
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]+)@\$/';
1069 preg_match_all($searchstring,$result,$foundset);
1070 //If found, then we are going to look for its new id (in backup tables)
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
) {
1084 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/forum/discuss.php?d='.$rec->new_id
.'#'.$rec2->new_id
,$result);
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);
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) {
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) {
1115 $content = $post->message
;
1116 $result = restore_decode_content_links_worker($content,$restore);
1117 if ($result != $content) {
1119 $post->message
= addslashes($result);
1120 $status = update_record("forum_posts",$post);
1122 if (!defined('RESTORE_SILENTLY')) {
1123 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
1128 if (($i+
1) %
5 == 0) {
1129 if (!defined('RESTORE_SILENTLY')) {
1131 if (($i+
1) %
100 == 0) {
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) {
1149 $content = $forum->intro
;
1150 $result = restore_decode_content_links_worker($content,$restore);
1151 if ($result != $content) {
1153 $forum->intro
= addslashes($result);
1154 $status = update_record("forum",$forum);
1156 if (!defined('RESTORE_SILENTLY')) {
1157 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
1162 if (($i+
1) %
5 == 0) {
1163 if (!defined('RESTORE_SILENTLY')) {
1165 if (($i+
1) %
100 == 0) {