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
, $forum);
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 = new object();
276 $discussion->forum
= $forum_id;
277 $discussion->course
= $restore->course_id
;
278 $discussion->name
= backup_todb($dis_info['#']['NAME']['0']['#']);
279 $discussion->firstpost
= backup_todb($dis_info['#']['FIRSTPOST']['0']['#']);
280 $discussion->userid
= backup_todb($dis_info['#']['USERID']['0']['#']);
281 $discussion->groupid
= backup_todb($dis_info['#']['GROUPID']['0']['#']);
282 $discussion->assessed
= backup_todb($dis_info['#']['ASSESSED']['0']['#']);
283 $discussion->timemodified
= backup_todb($dis_info['#']['TIMEMODIFIED']['0']['#']);
284 $discussion->timemodified +
= $restore->course_startdateoffset
;
285 $discussion->usermodified
= backup_todb($dis_info['#']['USERMODIFIED']['0']['#']);
286 $discussion->timestart
= backup_todb($dis_info['#']['TIMESTART']['0']['#']);
287 $discussion->timestart +
= $restore->course_startdateoffset
;
288 $discussion->timeend
= backup_todb($dis_info['#']['TIMEEND']['0']['#']);
289 $discussion->timeend +
= $restore->course_startdateoffset
;
290 //We have to recode the userid field
291 $user = backup_getid($restore->backup_unique_code
,"user",$discussion->userid
);
293 $discussion->userid
= $user->new_id
;
296 //We have to recode the groupid field
297 $group = restore_group_getid($restore, $discussion->groupid
);
299 $discussion->groupid
= $group->new_id
;
302 //We have to recode the usermodified field
303 $user = backup_getid($restore->backup_unique_code
,"user",$discussion->usermodified
);
305 $discussion->usermodified
= $user->new_id
;
308 //The structure is equal to the db, so insert the forum_discussions
309 $newid = insert_record ("forum_discussions",$discussion);
312 if (($i+
1) %
50 == 0) {
313 if (!defined('RESTORE_SILENTLY')) {
315 if (($i+
1) %
1000 == 0) {
323 //We have the newid, update backup_ids
324 backup_putid($restore->backup_unique_code
,"forum_discussions",$oldid,
326 //Restore forum_posts
327 $status = forum_posts_restore_mods ($forum_id,$newid,$dis_info,$restore);
328 //Now recalculate firstpost field
329 $old_firstpost = $discussion->firstpost
;
330 //Get its new post_id from backup_ids table
331 $rec = backup_getid($restore->backup_unique_code
,"forum_posts",$old_firstpost);
333 //Put its new firstpost
334 $discussion->firstpost
= $rec->new_id
;
335 if ($post = get_record("forum_posts", "id", $discussion->firstpost
)) {
336 $discussion->userid
= $post->userid
;
339 $discussion->firstpost
= 0;
340 $discussion->userid
= 0;
342 //Create temp discussion record
343 $temp_discussion->id
= $newid;
344 $temp_discussion->firstpost
= $discussion->firstpost
;
345 $temp_discussion->userid
= $discussion->userid
;
346 //Update discussion (only firstpost and userid will be changed)
347 $status = update_record("forum_discussions",$temp_discussion);
348 //echo "Updated firstpost ".$old_firstpost." to ".$temp_discussion->firstpost."<br />"; //Debug
357 //This function restores the forum_read
358 function forum_read_restore_mods($forum_id,$info,$restore) {
365 $readposts = array();
366 if (isset($info['MOD']['#']['READPOSTS']['0']['#']['READ'])) {
367 $readposts = $info['MOD']['#']['READPOSTS']['0']['#']['READ'];
370 //Iterate over readposts
371 for($i = 0; $i < sizeof($readposts); $i++
) {
372 $rea_info = $readposts[$i];
373 //traverse_xmlize($rea_info); //Debug
374 //print_object ($GLOBALS['traverse_array']); //Debug
375 //$GLOBALS['traverse_array']=""; //Debug
377 //We'll need this later!!
378 $oldid = backup_todb($rea_info['#']['ID']['0']['#']);
380 //Now, build the FORUM_READ record structure
381 $read->forumid
= $forum_id;
382 $read->userid
= backup_todb($rea_info['#']['USERID']['0']['#']);
383 $read->discussionid
= backup_todb($rea_info['#']['DISCUSSIONID']['0']['#']);
384 $read->postid
= backup_todb($rea_info['#']['POSTID']['0']['#']);
385 $read->firstread
= backup_todb($rea_info['#']['FIRSTREAD']['0']['#']);
386 $read->lastread
= backup_todb($rea_info['#']['LASTREAD']['0']['#']);
388 //Some recoding and check are performed now
391 //We have to recode the userid field
392 $user = backup_getid($restore->backup_unique_code
,"user",$read->userid
);
394 $read->userid
= $user->new_id
;
399 //We have to recode the discussionid field
400 $discussion = backup_getid($restore->backup_unique_code
,"forum_discussions",$read->discussionid
);
402 $read->discussionid
= $discussion->new_id
;
407 //We have to recode the postid field
408 $post = backup_getid($restore->backup_unique_code
,"forum_posts",$read->postid
);
410 $read->postid
= $post->new_id
;
415 //The structure is equal to the db, so insert the forum_read
418 $newid = insert_record ("forum_read",$read);
422 if (($i+
1) %
50 == 0) {
423 if (!defined('RESTORE_SILENTLY')) {
425 if (($i+
1) %
1000 == 0) {
433 //We have the newid, update backup_ids
434 backup_putid($restore->backup_unique_code
,"forum_read",$oldid,
444 //This function restores the forum_posts
445 function forum_posts_restore_mods($new_forum_id,$discussion_id,$info,$restore) {
451 //Get the posts array
452 $posts = $info['#']['POSTS']['0']['#']['POST'];
455 for($i = 0; $i < sizeof($posts); $i++
) {
456 $pos_info = $posts[$i];
457 //traverse_xmlize($pos_info); //Debug
458 //print_object ($GLOBALS['traverse_array']); //Debug
459 //$GLOBALS['traverse_array']=""; //Debug
461 //We'll need this later!!
462 $oldid = backup_todb($pos_info['#']['ID']['0']['#']);
463 $olduserid = backup_todb($pos_info['#']['USERID']['0']['#']);
465 //Now, build the FORUM_POSTS record structure
466 $post->discussion
= $discussion_id;
467 $post->parent
= backup_todb($pos_info['#']['PARENT']['0']['#']);
468 $post->userid
= backup_todb($pos_info['#']['USERID']['0']['#']);
469 $post->created
= backup_todb($pos_info['#']['CREATED']['0']['#']);
470 $post->created +
= $restore->course_startdateoffset
;
471 $post->modified
= backup_todb($pos_info['#']['MODIFIED']['0']['#']);
472 $post->modified +
= $restore->course_startdateoffset
;
473 $post->mailed
= backup_todb($pos_info['#']['MAILED']['0']['#']);
474 $post->subject
= backup_todb($pos_info['#']['SUBJECT']['0']['#']);
475 $post->message
= backup_todb($pos_info['#']['MESSAGE']['0']['#']);
476 $post->format
= backup_todb($pos_info['#']['FORMAT']['0']['#']);
477 $post->attachment
= backup_todb($pos_info['#']['ATTACHMENT']['0']['#']);
478 $post->totalscore
= backup_todb($pos_info['#']['TOTALSCORE']['0']['#']);
479 $post->mailnow
= backup_todb($pos_info['#']['MAILNOW']['0']['#']);
481 //We have to recode the userid field
482 $user = backup_getid($restore->backup_unique_code
,"user",$post->userid
);
484 $post->userid
= $user->new_id
;
487 //The structure is equal to the db, so insert the forum_posts
488 $newid = insert_record ("forum_posts",$post);
491 if (($i+
1) %
50 == 0) {
492 if (!defined('RESTORE_SILENTLY')) {
494 if (($i+
1) %
1000 == 0) {
502 //We have the newid, update backup_ids
503 backup_putid($restore->backup_unique_code
,"forum_posts",$oldid,
506 //Get old forum id from backup_ids
507 $rec = get_record("backup_ids","backup_code",$restore->backup_unique_code
,
508 "table_name","forum",
509 "new_id",$new_forum_id);
510 //Now copy moddata associated files
511 $status = forum_restore_files ($rec->old_id
, $new_forum_id,
512 $oldid, $newid, $restore);
514 //Now restore post ratings
515 $status = forum_ratings_restore_mods($newid,$pos_info,$restore);
522 //Now we get every post in this discussion_id and recalculate its parent post
523 $posts = get_records ("forum_posts","discussion",$discussion_id);
525 //Iterate over each post
526 foreach ($posts as $post) {
528 $old_parent = $post->parent
;
529 //Get its new post_id from backup_ids table
530 $rec = backup_getid($restore->backup_unique_code
,"forum_posts",$old_parent);
533 $post->parent
= $rec->new_id
;
537 //Create temp post record
538 $temp_post->id
= $post->id
;
539 $temp_post->parent
= $post->parent
;
540 //echo "Updated parent ".$old_parent." to ".$temp_post->parent."<br />"; //Debug
541 //Update post (only parent will be changed)
542 $status = update_record("forum_posts",$temp_post);
549 //This function copies the forum related info from backup temp dir to course moddata folder,
550 //creating it if needed and recoding everything (forum id and post id)
551 function forum_restore_files ($oldforid, $newforid, $oldpostid, $newpostid, $restore) {
561 //First, we check to "course_id" exists and create is as necessary
563 $dest_dir = $CFG->dataroot
."/".$restore->course_id
;
564 $status = check_dir_exists($dest_dir,true);
566 //First, locate course's moddata directory
567 $moddata_path = $CFG->dataroot
."/".$restore->course_id
."/".$CFG->moddata
;
569 //Check it exists and create it
570 $status = check_dir_exists($moddata_path,true);
572 //Now, locate forum directory
574 $forum_path = $moddata_path."/forum";
575 //Check it exists and create it
576 $status = check_dir_exists($forum_path,true);
579 //Now locate the temp dir we are restoring from
581 $temp_path = $CFG->dataroot
."/temp/backup/".$restore->backup_unique_code
.
582 "/moddata/forum/".$oldforid."/".$oldpostid;
584 if (is_dir($temp_path)) {
589 //If todo, we create the neccesary dirs in course moddata/forum
590 if ($status and $todo) {
591 //First this forum id
592 $this_forum_path = $forum_path."/".$newforid;
593 $status = check_dir_exists($this_forum_path,true);
595 $post_forum_path = $this_forum_path."/".$newpostid;
596 //And now, copy temp_path to post_forum_path
597 $status = backup_copy_file($temp_path, $post_forum_path);
603 //This function restores the forum_ratings
604 function forum_ratings_restore_mods($new_post_id,$info,$restore) {
610 //Get the ratings array
612 if (isset($info['#']['RATINGS']['0']['#']['RATING'])) {
613 $ratings = $info['#']['RATINGS']['0']['#']['RATING'];
616 //Iterate over ratings
617 for($i = 0; $i < sizeof($ratings); $i++
) {
618 $rat_info = $ratings[$i];
619 //traverse_xmlize($rat_info); //Debug
620 //print_object ($GLOBALS['traverse_array']); //Debug
621 //$GLOBALS['traverse_array']=""; //Debug
623 //We'll need this later!!
624 $oldid = backup_todb($rat_info['#']['ID']['0']['#']);
625 $olduserid = backup_todb($rat_info['#']['USERID']['0']['#']);
627 //Now, build the FORM_RATINGS record structure
628 $rating->post
= $new_post_id;
629 $rating->userid
= backup_todb($rat_info['#']['USERID']['0']['#']);
630 $rating->time
= backup_todb($rat_info['#']['TIME']['0']['#']);
631 $rating->rating
= backup_todb($rat_info['#']['POST_RATING']['0']['#']);
633 //We have to recode the userid field
634 $user = backup_getid($restore->backup_unique_code
,"user",$rating->userid
);
636 $rating->userid
= $user->new_id
;
639 //The structure is equal to the db, so insert the forum_ratings
640 $newid = insert_record ("forum_ratings",$rating);
643 if (($i+
1) %
50 == 0) {
644 if (!defined('RESTORE_SILENTLY')) {
646 if (($i+
1) %
1000 == 0) {
654 //We have the newid, update backup_ids
655 backup_putid($restore->backup_unique_code
,"forum_ratings",$oldid,
665 //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
666 //some texts in the module
667 function forum_restore_wiki2markdown ($restore) {
673 //Convert forum_posts->message
674 if ($records = get_records_sql ("SELECT p.id, p.message, p.format
675 FROM {$CFG->prefix}forum_posts p,
676 {$CFG->prefix}forum_discussions d,
677 {$CFG->prefix}forum f,
678 {$CFG->prefix}backup_ids b
679 WHERE d.id = p.discussion AND
681 f.course = $restore->course_id AND
682 p.format = ".FORMAT_WIKI
. " AND
683 b.backup_code = $restore->backup_unique_code AND
684 b.table_name = 'forum_posts' AND
686 foreach ($records as $record) {
688 $record->message
= restore_decode_wiki_content($record->message
, $restore);
689 //Convert to Markdown
690 $wtm = new WikiToMarkdown();
691 $record->message
= $wtm->convert($record->message
, $restore->course_id
);
692 $record->format
= FORMAT_MARKDOWN
;
693 $status = update_record('forum_posts', addslashes_object($record));
696 if (($i+
1) %
1 == 0) {
697 if (!defined('RESTORE_SILENTLY')) {
699 if (($i+
1) %
20 == 0) {
711 //This function returns a log record with all the necessay transformations
712 //done. It's used by restore_log_module() to restore modules log.
713 function forum_restore_logs($restore,$log) {
717 //Depending of the action, we recode different things
718 switch ($log->action
) {
721 //Get the new_id of the module (to recode the info field)
722 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
724 $log->url
= "view.php?id=".$log->cmid
;
725 $log->info
= $mod->new_id
;
732 //Get the new_id of the module (to recode the url and info fields)
733 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
735 $log->url
= "view.php?f=".$mod->new_id
;
736 $log->info
= $mod->new_id
;
741 case "start tracking":
743 //Get the new_id of the module (to recode the url and info fields)
744 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
746 $log->url
= "view.php?f=".$mod->new_id
;
747 $log->info
= $mod->new_id
;
752 case "stop tracking":
754 //Get the new_id of the module (to recode the url and info fields)
755 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
757 $log->url
= "view.php?f=".$mod->new_id
;
758 $log->info
= $mod->new_id
;
765 //Get the new_id of the module (to recode the info field)
766 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
768 $log->url
= "view.php?id=".$log->cmid
;
769 $log->info
= $mod->new_id
;
776 //Get the new_id of the module (to recode the info field)
777 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
779 $log->url
= "view.php?id=".$log->cmid
;
780 $log->info
= $mod->new_id
;
786 $log->url
= "index.php?id=".$log->course
;
790 $log->url
= "index.php?id=".$log->course
;
793 case "unsubscribeall":
794 $log->url
= "index.php?id=".$log->course
;
799 //Get the new_id of the module (to recode the info and url field)
800 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
802 $log->url
= "view.php?f=".$mod->new_id
;
803 $log->info
= $mod->new_id
;
808 case "view subscriber":
809 case "view subscribers":
811 //Get the new_id of the module (to recode the info and field)
812 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
814 $log->url
= "subscribers.php?id=".$mod->new_id
;
815 $log->info
= $mod->new_id
;
822 //Get the new_id of the module (to recode the info and url field)
823 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
825 $log->url
= "view.php?f=".$mod->new_id
;
826 $log->info
= $mod->new_id
;
831 case "add discussion":
833 //Get the new_id of the discussion (to recode the info and url field)
834 $dis = backup_getid($restore->backup_unique_code
,"forum_discussions",$log->info
);
836 $log->url
= "discuss.php?d=".$dis->new_id
;
837 $log->info
= $dis->new_id
;
842 case "view discussion":
844 //Get the new_id of the discussion (to recode the info and url field)
845 $dis = backup_getid($restore->backup_unique_code
,"forum_discussions",$log->info
);
847 $log->url
= "discuss.php?d=".$dis->new_id
;
848 $log->info
= $dis->new_id
;
853 case "move discussion":
855 //Get the new_id of the discussion (to recode the info and url field)
856 $dis = backup_getid($restore->backup_unique_code
,"forum_discussions",$log->info
);
858 $log->url
= "discuss.php?d=".$dis->new_id
;
859 $log->info
= $dis->new_id
;
864 case "delete discussi":
865 case "delete discussion":
867 //Get the new_id of the module (to recode the info field)
868 $mod = backup_getid($restore->backup_unique_code
,$log->module
,$log->info
);
870 $log->url
= "view.php?id=".$log->cmid
;
871 $log->info
= $mod->new_id
;
878 //Get the new_id of the post (to recode the url and info field)
879 $pos = backup_getid($restore->backup_unique_code
,"forum_posts",$log->info
);
881 //Get the post record from database
882 $dbpos = get_record("forum_posts","id","$pos->new_id");
884 $log->url
= "discuss.php?d=".$dbpos->discussion
."&parent=".$pos->new_id
;
885 $log->info
= $pos->new_id
;
893 //Get the new_id of the post (to recode the url and info field)
894 $pos = backup_getid($restore->backup_unique_code
,"forum_posts",$log->info
);
896 //Get the post record from database
897 $dbpos = get_record("forum_posts","id","$pos->new_id");
899 $log->url
= "discuss.php?d=".$dbpos->discussion
;
900 $log->info
= $pos->new_id
;
908 //Get the new_id of the post (to recode the url and info field)
909 $pos = backup_getid($restore->backup_unique_code
,"forum_posts",$log->info
);
911 //Get the post record from database
912 $dbpos = get_record("forum_posts","id","$pos->new_id");
914 $log->url
= "discuss.php?d=".$dbpos->discussion
."&parent=".$pos->new_id
;
915 $log->info
= $pos->new_id
;
923 //Extract the discussion id from the url field
924 $disid = substr(strrchr($log->url
,"="),1);
925 //Get the new_id of the discussion (to recode the url field)
926 $dis = backup_getid($restore->backup_unique_code
,"quiz_discussions",$disid);
928 $log->url
= "discuss.php?d=".$dis->new_id
;
934 //Extract mode from url
935 $mode = substr(strrchr($log->url
,"="),1);
937 if ($use = backup_getid($restore->backup_unique_code
, 'user', $log->info
)) {
938 $log->url
= 'user.php?course=' . $log->course
. '&id=' . $use->new_id
. '&mode=' . $mode;
939 $log->info
= $use->new_id
;
944 $log->url
= "search.php?id=".$log->course
."&search=".urlencode($log->info
);
948 if (!defined('RESTORE_SILENTLY')) {
949 echo "action (".$log->module
."-".$log->action
.") unknown. Not restored<br />"; //Debug
960 //Return a content decoded to support interactivities linking. Every module
961 //should have its own. They are called automatically from
962 //forum_decode_content_links_caller() function in each module
963 //in the restore process
964 function forum_decode_content_links ($content,$restore) {
970 //Link to the list of forums
972 $searchstring='/\$@(FORUMINDEX)\*([0-9]+)@\$/';
974 preg_match_all($searchstring,$content,$foundset);
975 //If found, then we are going to look for its new id (in backup tables)
977 //print_object($foundset); //Debug
978 //Iterate over foundset[2]. They are the old_ids
979 foreach($foundset[2] as $old_id) {
980 //We get the needed variables here (course id)
981 $rec = backup_getid($restore->backup_unique_code
,"course",$old_id);
982 //Personalize the searchstring
983 $searchstring='/\$@(FORUMINDEX)\*('.$old_id.')@\$/';
984 //If it is a link to this course, update the link to its new location
987 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/forum/index.php?id='.$rec->new_id
,$result);
989 //It's a foreign link so leave it as original
990 $result= preg_replace($searchstring,$restore->original_wwwroot
.'/mod/forum/index.php?id='.$old_id,$result);
995 //Link to forum view by moduleid
997 $searchstring='/\$@(FORUMVIEWBYID)\*([0-9]+)@\$/';
999 preg_match_all($searchstring,$result,$foundset);
1000 //If found, then we are going to look for its new id (in backup tables)
1002 //print_object($foundset); //Debug
1003 //Iterate over foundset[2]. They are the old_ids
1004 foreach($foundset[2] as $old_id) {
1005 //We get the needed variables here (course_modules id)
1006 $rec = backup_getid($restore->backup_unique_code
,"course_modules",$old_id);
1007 //Personalize the searchstring
1008 $searchstring='/\$@(FORUMVIEWBYID)\*('.$old_id.')@\$/';
1009 //If it is a link to this course, update the link to its new location
1012 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/forum/view.php?id='.$rec->new_id
,$result);
1014 //It's a foreign link so leave it as original
1015 $result= preg_replace($searchstring,$restore->original_wwwroot
.'/mod/forum/view.php?id='.$old_id,$result);
1020 //Link to forum view by forumid
1022 $searchstring='/\$@(FORUMVIEWBYF)\*([0-9]+)@\$/';
1024 preg_match_all($searchstring,$result,$foundset);
1025 //If found, then we are going to look for its new id (in backup tables)
1027 //print_object($foundset); //Debug
1028 //Iterate over foundset[2]. They are the old_ids
1029 foreach($foundset[2] as $old_id) {
1030 //We get the needed variables here (forum id)
1031 $rec = backup_getid($restore->backup_unique_code
,"forum",$old_id);
1032 //Personalize the searchstring
1033 $searchstring='/\$@(FORUMVIEWBYF)\*('.$old_id.')@\$/';
1034 //If it is a link to this course, update the link to its new location
1037 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/forum/view.php?f='.$rec->new_id
,$result);
1039 //It's a foreign link so leave it as original
1040 $result= preg_replace($searchstring,$restore->original_wwwroot
.'/mod/forum/view.php?f='.$old_id,$result);
1045 //Link to forum discussion by discussionid
1047 $searchstring='/\$@(FORUMDISCUSSIONVIEW)\*([0-9]+)@\$/';
1049 preg_match_all($searchstring,$result,$foundset);
1050 //If found, then we are going to look for its new id (in backup tables)
1052 //print_object($foundset); //Debug
1053 //Iterate over foundset[2]. They are the old_ids
1054 foreach($foundset[2] as $old_id) {
1055 //We get the needed variables here (discussion id)
1056 $rec = backup_getid($restore->backup_unique_code
,"forum_discussions",$old_id);
1057 //Personalize the searchstring
1058 $searchstring='/\$@(FORUMDISCUSSIONVIEW)\*('.$old_id.')@\$/';
1059 //If it is a link to this course, update the link to its new location
1062 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/forum/discuss.php?d='.$rec->new_id
,$result);
1064 //It's a foreign link so leave it as original
1065 $result= preg_replace($searchstring,$restore->original_wwwroot
.'/mod/forum/discuss.php?d='.$old_id,$result);
1070 //Link to forum discussion with parent syntax
1072 $searchstring='/\$@(FORUMDISCUSSIONVIEWPARENT)\*([0-9]+)\*([0-9]+)@\$/';
1074 preg_match_all($searchstring,$result,$foundset);
1075 //If found, then we are going to look for its new id (in backup tables)
1077 //print_object($foundset); //Debug
1078 //Iterate over foundset[2] and foundset[3]. They are the old_ids
1079 foreach($foundset[2] as $key => $old_id) {
1080 $old_id2 = $foundset[3][$key];
1081 //We get the needed variables here (discussion id and post id)
1082 $rec = backup_getid($restore->backup_unique_code
,"forum_discussions",$old_id);
1083 $rec2 = backup_getid($restore->backup_unique_code
,"forum_posts",$old_id2);
1084 //Personalize the searchstring
1085 $searchstring='/\$@(FORUMDISCUSSIONVIEWPARENT)\*('.$old_id.')\*('.$old_id2.')@\$/';
1086 //If it is a link to this course, update the link to its new location
1087 if($rec->new_id
&& $rec2->new_id
) {
1089 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/forum/discuss.php?d='.$rec->new_id
.'&parent='.$rec2->new_id
,$result);
1091 //It's a foreign link so leave it as original
1092 $result= preg_replace($searchstring,$restore->original_wwwroot
.'/mod/forum/discuss.php?d='.$old_id.'&parent='.$old_id2,$result);
1097 //Link to forum discussion with relative syntax
1099 $searchstring='/\$@(FORUMDISCUSSIONVIEWINSIDE)\*([0-9]+)\*([0-9]+)@\$/';
1101 preg_match_all($searchstring,$result,$foundset);
1102 //If found, then we are going to look for its new id (in backup tables)
1104 //print_object($foundset); //Debug
1105 //Iterate over foundset[2] and foundset[3]. They are the old_ids
1106 foreach($foundset[2] as $key => $old_id) {
1107 $old_id2 = $foundset[3][$key];
1108 //We get the needed variables here (discussion id and post id)
1109 $rec = backup_getid($restore->backup_unique_code
,"forum_discussions",$old_id);
1110 $rec2 = backup_getid($restore->backup_unique_code
,"forum_posts",$old_id2);
1111 //Personalize the searchstring
1112 $searchstring='/\$@(FORUMDISCUSSIONVIEWINSIDE)\*('.$old_id.')\*('.$old_id2.')@\$/';
1113 //If it is a link to this course, update the link to its new location
1114 if($rec->new_id
&& $rec2->new_id
) {
1116 $result= preg_replace($searchstring,$CFG->wwwroot
.'/mod/forum/discuss.php?d='.$rec->new_id
.'#'.$rec2->new_id
,$result);
1118 //It's a foreign link so leave it as original
1119 $result= preg_replace($searchstring,$restore->original_wwwroot
.'/mod/forum/discuss.php?d='.$old_id.'#'.$old_id2,$result);
1127 //This function makes all the necessary calls to xxxx_decode_content_links()
1128 //function in each module, passing them the desired contents to be decoded
1129 //from backup format to destination site/course in order to mantain inter-activities
1130 //working in the backup/restore process. It's called from restore_decode_content_links()
1131 //function in restore process
1132 function forum_decode_content_links_caller($restore) {
1136 //Process every POST (message) in the course
1137 if ($posts = get_records_sql ("SELECT p.id, p.message
1138 FROM {$CFG->prefix}forum_posts p,
1139 {$CFG->prefix}forum_discussions d
1140 WHERE d.course = $restore->course_id AND
1141 p.discussion = d.id")) {
1142 //Iterate over each post->message
1143 $i = 0; //Counter to send some output to the browser to avoid timeouts
1144 foreach ($posts as $post) {
1147 $content = $post->message
;
1148 $result = restore_decode_content_links_worker($content,$restore);
1149 if ($result != $content) {
1151 $post->message
= addslashes($result);
1152 $status = update_record("forum_posts",$post);
1154 if (!defined('RESTORE_SILENTLY')) {
1155 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
1160 if (($i+
1) %
5 == 0) {
1161 if (!defined('RESTORE_SILENTLY')) {
1163 if (($i+
1) %
100 == 0) {
1172 //Process every FORUM (intro) in the course
1173 if ($forums = get_records_sql ("SELECT f.id, f.intro
1174 FROM {$CFG->prefix}forum f
1175 WHERE f.course = $restore->course_id")) {
1176 //Iterate over each forum->intro
1177 $i = 0; //Counter to send some output to the browser to avoid timeouts
1178 foreach ($forums as $forum) {
1181 $content = $forum->intro
;
1182 $result = restore_decode_content_links_worker($content,$restore);
1183 if ($result != $content) {
1185 $forum->intro
= addslashes($result);
1186 $status = update_record("forum",$forum);
1188 if (!defined('RESTORE_SILENTLY')) {
1189 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
1194 if (($i+
1) %
5 == 0) {
1195 if (!defined('RESTORE_SILENTLY')) {
1197 if (($i+
1) %
100 == 0) {