Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / forum / restorelib.php
blobee7d177ddfe495ceae4f92a95ff3b5720a05c00f
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, $forum);
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 = 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);
292 if ($user) {
293 $discussion->userid = $user->new_id;
296 //We have to recode the groupid field
297 $group = restore_group_getid($restore, $discussion->groupid);
298 if ($group) {
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);
304 if ($user) {
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);
311 //Do some output
312 if (($i+1) % 50 == 0) {
313 if (!defined('RESTORE_SILENTLY')) {
314 echo ".";
315 if (($i+1) % 1000 == 0) {
316 echo "<br />";
319 backup_flush(300);
322 if ($newid) {
323 //We have the newid, update backup_ids
324 backup_putid($restore->backup_unique_code,"forum_discussions",$oldid,
325 $newid);
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);
332 if ($rec) {
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;
338 } else {
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
349 } else {
350 $status = false;
354 return $status;
357 //This function restores the forum_read
358 function forum_read_restore_mods($forum_id,$info,$restore) {
360 global $CFG;
362 $status = true;
364 //Get the read array
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
389 $toinsert = true;
391 //We have to recode the userid field
392 $user = backup_getid($restore->backup_unique_code,"user",$read->userid);
393 if ($user) {
394 $read->userid = $user->new_id;
395 } else {
396 $toinsert = false;
399 //We have to recode the discussionid field
400 $discussion = backup_getid($restore->backup_unique_code,"forum_discussions",$read->discussionid);
401 if ($discussion) {
402 $read->discussionid = $discussion->new_id;
403 } else {
404 $toinsert = false;
407 //We have to recode the postid field
408 $post = backup_getid($restore->backup_unique_code,"forum_posts",$read->postid);
409 if ($post) {
410 $read->postid = $post->new_id;
411 } else {
412 $toinsert = false;
415 //The structure is equal to the db, so insert the forum_read
416 $newid = 0;
417 if ($toinsert) {
418 $newid = insert_record ("forum_read",$read);
421 //Do some output
422 if (($i+1) % 50 == 0) {
423 if (!defined('RESTORE_SILENTLY')) {
424 echo ".";
425 if (($i+1) % 1000 == 0) {
426 echo "<br />";
429 backup_flush(300);
432 if ($newid) {
433 //We have the newid, update backup_ids
434 backup_putid($restore->backup_unique_code,"forum_read",$oldid,
435 $newid);
436 } else {
437 $status = false;
441 return $status;
444 //This function restores the forum_posts
445 function forum_posts_restore_mods($new_forum_id,$discussion_id,$info,$restore) {
447 global $CFG;
449 $status = true;
451 //Get the posts array
452 $posts = $info['#']['POSTS']['0']['#']['POST'];
454 //Iterate over posts
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);
483 if ($user) {
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);
490 //Do some output
491 if (($i+1) % 50 == 0) {
492 if (!defined('RESTORE_SILENTLY')) {
493 echo ".";
494 if (($i+1) % 1000 == 0) {
495 echo "<br />";
498 backup_flush(300);
501 if ($newid) {
502 //We have the newid, update backup_ids
503 backup_putid($restore->backup_unique_code,"forum_posts",$oldid,
504 $newid);
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);
517 } else {
518 $status = false;
522 //Now we get every post in this discussion_id and recalculate its parent post
523 $posts = get_records ("forum_posts","discussion",$discussion_id);
524 if ($posts) {
525 //Iterate over each post
526 foreach ($posts as $post) {
527 //Get its parent
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);
531 if ($rec) {
532 //Put its new parent
533 $post->parent = $rec->new_id;
534 } else {
535 $post->parent = 0;
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);
546 return $status;
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) {
553 global $CFG;
555 $status = true;
556 $todo = false;
557 $moddata_path = "";
558 $forum_path = "";
559 $temp_path = "";
561 //First, we check to "course_id" exists and create is as necessary
562 //in CFG->dataroot
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
573 if ($status) {
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
580 if ($status) {
581 $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
582 "/moddata/forum/".$oldforid."/".$oldpostid;
583 //Check it exists
584 if (is_dir($temp_path)) {
585 $todo = true;
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);
594 //Now this post id
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);
600 return $status;
603 //This function restores the forum_ratings
604 function forum_ratings_restore_mods($new_post_id,$info,$restore) {
606 global $CFG;
608 $status = true;
610 //Get the ratings array
611 $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);
635 if ($user) {
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);
642 //Do some output
643 if (($i+1) % 50 == 0) {
644 if (!defined('RESTORE_SILENTLY')) {
645 echo ".";
646 if (($i+1) % 1000 == 0) {
647 echo "<br />";
650 backup_flush(300);
653 if ($newid) {
654 //We have the newid, update backup_ids
655 backup_putid($restore->backup_unique_code,"forum_ratings",$oldid,
656 $newid);
657 } else {
658 $status = false;
662 return $status;
665 //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for
666 //some texts in the module
667 function forum_restore_wiki2markdown ($restore) {
669 global $CFG;
671 $status = true;
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
680 f.id = d.forum 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
685 b.new_id = p.id")) {
686 foreach ($records as $record) {
687 //Rebuild wiki links
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));
694 //Do some output
695 $i++;
696 if (($i+1) % 1 == 0) {
697 if (!defined('RESTORE_SILENTLY')) {
698 echo ".";
699 if (($i+1) % 20 == 0) {
700 echo "<br />";
703 backup_flush(300);
708 return $status;
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) {
715 $status = false;
717 //Depending of the action, we recode different things
718 switch ($log->action) {
719 case "add":
720 if ($log->cmid) {
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);
723 if ($mod) {
724 $log->url = "view.php?id=".$log->cmid;
725 $log->info = $mod->new_id;
726 $status = true;
729 break;
730 case "mark read":
731 if ($log->cmid) {
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);
734 if ($mod) {
735 $log->url = "view.php?f=".$mod->new_id;
736 $log->info = $mod->new_id;
737 $status = true;
740 break;
741 case "start tracking":
742 if ($log->cmid) {
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);
745 if ($mod) {
746 $log->url = "view.php?f=".$mod->new_id;
747 $log->info = $mod->new_id;
748 $status = true;
751 break;
752 case "stop tracking":
753 if ($log->cmid) {
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);
756 if ($mod) {
757 $log->url = "view.php?f=".$mod->new_id;
758 $log->info = $mod->new_id;
759 $status = true;
762 break;
763 case "update":
764 if ($log->cmid) {
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);
767 if ($mod) {
768 $log->url = "view.php?id=".$log->cmid;
769 $log->info = $mod->new_id;
770 $status = true;
773 break;
774 case "view forum":
775 if ($log->cmid) {
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);
778 if ($mod) {
779 $log->url = "view.php?id=".$log->cmid;
780 $log->info = $mod->new_id;
781 $status = true;
784 break;
785 case "view forums":
786 $log->url = "index.php?id=".$log->course;
787 $status = true;
788 break;
789 case "subscribeall":
790 $log->url = "index.php?id=".$log->course;
791 $status = true;
792 break;
793 case "unsubscribeall":
794 $log->url = "index.php?id=".$log->course;
795 $status = true;
796 break;
797 case "subscribe":
798 if ($log->cmid) {
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);
801 if ($mod) {
802 $log->url = "view.php?f=".$mod->new_id;
803 $log->info = $mod->new_id;
804 $status = true;
807 break;
808 case "view subscriber":
809 case "view subscribers":
810 if ($log->cmid) {
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);
813 if ($mod) {
814 $log->url = "subscribers.php?id=".$mod->new_id;
815 $log->info = $mod->new_id;
816 $status = true;
819 break;
820 case "unsubscribe":
821 if ($log->cmid) {
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);
824 if ($mod) {
825 $log->url = "view.php?f=".$mod->new_id;
826 $log->info = $mod->new_id;
827 $status = true;
830 break;
831 case "add discussion":
832 if ($log->cmid) {
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);
835 if ($dis) {
836 $log->url = "discuss.php?d=".$dis->new_id;
837 $log->info = $dis->new_id;
838 $status = true;
841 break;
842 case "view discussion":
843 if ($log->cmid) {
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);
846 if ($dis) {
847 $log->url = "discuss.php?d=".$dis->new_id;
848 $log->info = $dis->new_id;
849 $status = true;
852 break;
853 case "move discussion":
854 if ($log->cmid) {
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);
857 if ($dis) {
858 $log->url = "discuss.php?d=".$dis->new_id;
859 $log->info = $dis->new_id;
860 $status = true;
863 break;
864 case "delete discussi":
865 case "delete discussion":
866 if ($log->cmid) {
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);
869 if ($mod) {
870 $log->url = "view.php?id=".$log->cmid;
871 $log->info = $mod->new_id;
872 $status = true;
875 break;
876 case "add post":
877 if ($log->cmid) {
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);
880 if ($pos) {
881 //Get the post record from database
882 $dbpos = get_record("forum_posts","id","$pos->new_id");
883 if ($dbpos) {
884 $log->url = "discuss.php?d=".$dbpos->discussion."&parent=".$pos->new_id;
885 $log->info = $pos->new_id;
886 $status = true;
890 break;
891 case "prune post":
892 if ($log->cmid) {
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);
895 if ($pos) {
896 //Get the post record from database
897 $dbpos = get_record("forum_posts","id","$pos->new_id");
898 if ($dbpos) {
899 $log->url = "discuss.php?d=".$dbpos->discussion;
900 $log->info = $pos->new_id;
901 $status = true;
905 break;
906 case "update post":
907 if ($log->cmid) {
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);
910 if ($pos) {
911 //Get the post record from database
912 $dbpos = get_record("forum_posts","id","$pos->new_id");
913 if ($dbpos) {
914 $log->url = "discuss.php?d=".$dbpos->discussion."&parent=".$pos->new_id;
915 $log->info = $pos->new_id;
916 $status = true;
920 break;
921 case "delete post":
922 if ($log->cmid) {
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);
927 if ($dis) {
928 $log->url = "discuss.php?d=".$dis->new_id;
929 $status = true;
932 break;
933 case "user report":
934 //Extract mode from url
935 $mode = substr(strrchr($log->url,"="),1);
936 //Get new user id
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;
940 $status = true;
942 break;
943 case "search":
944 $log->url = "search.php?id=".$log->course."&search=".urlencode($log->info);
945 $status = true;
946 break;
947 default:
948 if (!defined('RESTORE_SILENTLY')) {
949 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
951 break;
954 if ($status) {
955 $status = $log;
957 return $status;
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) {
966 global $CFG;
968 $result = $content;
970 //Link to the list of forums
972 $searchstring='/\$@(FORUMINDEX)\*([0-9]+)@\$/';
973 //We look for it
974 preg_match_all($searchstring,$content,$foundset);
975 //If found, then we are going to look for its new id (in backup tables)
976 if ($foundset[0]) {
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
985 if($rec->new_id) {
986 //Now replace it
987 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/index.php?id='.$rec->new_id,$result);
988 } else {
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]+)@\$/';
998 //We look for it
999 preg_match_all($searchstring,$result,$foundset);
1000 //If found, then we are going to look for its new id (in backup tables)
1001 if ($foundset[0]) {
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
1010 if($rec->new_id) {
1011 //Now replace it
1012 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/view.php?id='.$rec->new_id,$result);
1013 } else {
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]+)@\$/';
1023 //We look for it
1024 preg_match_all($searchstring,$result,$foundset);
1025 //If found, then we are going to look for its new id (in backup tables)
1026 if ($foundset[0]) {
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
1035 if($rec->new_id) {
1036 //Now replace it
1037 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/view.php?f='.$rec->new_id,$result);
1038 } else {
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]+)@\$/';
1048 //We look for it
1049 preg_match_all($searchstring,$result,$foundset);
1050 //If found, then we are going to look for its new id (in backup tables)
1051 if ($foundset[0]) {
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
1060 if($rec->new_id) {
1061 //Now replace it
1062 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/discuss.php?d='.$rec->new_id,$result);
1063 } else {
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]+)@\$/';
1073 //We look for it
1074 preg_match_all($searchstring,$result,$foundset);
1075 //If found, then we are going to look for its new id (in backup tables)
1076 if ($foundset[0]) {
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) {
1088 //Now replace it
1089 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/discuss.php?d='.$rec->new_id.'&parent='.$rec2->new_id,$result);
1090 } else {
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]+)@\$/';
1100 //We look for it
1101 preg_match_all($searchstring,$result,$foundset);
1102 //If found, then we are going to look for its new id (in backup tables)
1103 if ($foundset[0]) {
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) {
1115 //Now replace it
1116 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/forum/discuss.php?d='.$rec->new_id.'#'.$rec2->new_id,$result);
1117 } else {
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);
1124 return $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) {
1133 global $CFG;
1134 $status = true;
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) {
1145 //Increment counter
1146 $i++;
1147 $content = $post->message;
1148 $result = restore_decode_content_links_worker($content,$restore);
1149 if ($result != $content) {
1150 //Update record
1151 $post->message = addslashes($result);
1152 $status = update_record("forum_posts",$post);
1153 if (debugging()) {
1154 if (!defined('RESTORE_SILENTLY')) {
1155 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
1159 //Do some output
1160 if (($i+1) % 5 == 0) {
1161 if (!defined('RESTORE_SILENTLY')) {
1162 echo ".";
1163 if (($i+1) % 100 == 0) {
1164 echo "<br />";
1167 backup_flush(300);
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) {
1179 //Increment counter
1180 $i++;
1181 $content = $forum->intro;
1182 $result = restore_decode_content_links_worker($content,$restore);
1183 if ($result != $content) {
1184 //Update record
1185 $forum->intro = addslashes($result);
1186 $status = update_record("forum",$forum);
1187 if (debugging()) {
1188 if (!defined('RESTORE_SILENTLY')) {
1189 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
1193 //Do some output
1194 if (($i+1) % 5 == 0) {
1195 if (!defined('RESTORE_SILENTLY')) {
1196 echo ".";
1197 if (($i+1) % 100 == 0) {
1198 echo "<br />";
1201 backup_flush(300);
1206 return $status;