--- /dev/null
+<?PHP //$Id$
+ //This php script contains all the stuff to backup/restore
+ //forum mods
+
+ //This is the "graphical" structure of the forum mod:
+ //
+ // forum
+ // (CL,pk->id)
+ // |
+ // -----------------------------------
+ // | |
+ // subscriptions forum_discussions
+ // (UL,pk->id, fk->forum) (UL,pk->id, fk->forum)
+ // |
+ // |
+ // |
+ // forum_posts
+ // (UL,pk->id,fk->discussion,nt->parent,files)
+ // |
+ // |
+ // |
+ // forum_ratings
+ // (UL,pk->id,fk->post)
+ //
+ // Meaning: pk->primary key field of the table
+ // fk->foreign key to link with parent
+ // nt->nested field (recursive data)
+ // CL->course level info
+ // UL->user level info
+ // files->table may have files)
+ //
+ //-----------------------------------------------------------
+
+ function forum_backup_mods($bf,$preferences) {
+
+ global $CFG;
+
+ $status = true;
+
+ //Iterate over forum table
+ $forums = get_records ("forum","course",$preferences->backup_course,"id");
+ if ($forums) {
+ foreach ($forums as $forum) {
+ //Start mod
+ fwrite ($bf,start_tag("MOD",3,true));
+ //Print forum data
+ fwrite ($bf,full_tag("ID",4,false,$forum->id));
+ fwrite ($bf,full_tag("MODTYPE",4,false,"forum"));
+ fwrite ($bf,full_tag("TYPE",4,false,$forum->type));
+ fwrite ($bf,full_tag("NAME",4,false,$forum->name));
+ fwrite ($bf,full_tag("INTRO",4,false,$forum->intro));
+ fwrite ($bf,full_tag("OPEN",4,false,$forum->open));
+ fwrite ($bf,full_tag("ASSESSED",4,false,$forum->assessed));
+ fwrite ($bf,full_tag("FORCESUBSCRIBE",4,false,$forum->forcesubscribe));
+ fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$forum->timemodified));
+
+ //if we've selected to backup users info, then execute backup_forum_suscriptions and
+ //backup_forum_discussions
+ if ($preferences->mods["forum"]->userinfo) {
+ $status = backup_forum_subscriptions($bf,$preferences,$forum->id);
+ if ($status) {
+ $status = backup_forum_discussions($bf,$preferences,$forum->id);
+ }
+ }
+ //End mod
+ $status =fwrite ($bf,end_tag("MOD",3,true));
+ }
+ }
+ //if we've selected to backup users info, then backup files too
+ if ($status) {
+ if ($preferences->mods["forum"]->userinfo) {
+ $status = backup_forum_files($bf,$preferences);
+ }
+ }
+ return $status;
+ }
+
+ //Backup forum_subscriptions contents (executed from forum_backup_mods)
+ function backup_forum_subscriptions ($bf,$preferences,$forum) {
+
+ global $CFG;
+
+ $status = true;
+
+ $forum_subscriptions = get_records("forum_subscriptions","forum",$forum,"id");
+ //If there is subscriptions
+ if ($forum_subscriptions) {
+ //Write start tag
+ $status =fwrite ($bf,start_tag("SUBSCRIPTIONS",4,true));
+ //Iterate over each answer
+ foreach ($forum_subscriptions as $for_sus) {
+ //Start suscription
+ $status =fwrite ($bf,start_tag("SUBSCRIPTION",5,true));
+ //Print forum_subscriptions contents
+ fwrite ($bf,full_tag("ID",6,false,$for_sus->id));
+ fwrite ($bf,full_tag("USERID",6,false,$for_sus->userid));
+ //End subscription
+ $status =fwrite ($bf,end_tag("SUBSCRIPTION",5,true));
+ }
+ //Write end tag
+ $status =fwrite ($bf,end_tag("SUBSCRIPTIONS",4,true));
+ }
+ return $status;
+ }
+
+ //Backup forum_discussions contents (executed from forum_backup_mods)
+ function backup_forum_discussions ($bf,$preferences,$forum) {
+
+ global $CFG;
+
+ $status = true;
+
+ $forum_discussions = get_records("forum_discussions","forum",$forum,"id");
+ //If there are discussions
+ if ($forum_discussions) {
+ //Write start tag
+ $status =fwrite ($bf,start_tag("DISCUSSIONS",4,true));
+ //Iterate over each discussion
+ foreach ($forum_discussions as $for_dis) {
+ //Start discussion
+ $status =fwrite ($bf,start_tag("DISCUSSION",5,true));
+ //Print forum_discussions contents
+ fwrite ($bf,full_tag("ID",6,false,$for_dis->id));
+ fwrite ($bf,full_tag("NAME",6,false,$for_dis->name));
+ fwrite ($bf,full_tag("FIRSTPOST",6,false,$for_dis->firstpost));
+ fwrite ($bf,full_tag("ASSESSED",6,false,$for_dis->assessed));
+ fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$for_dis->timemodified));
+ //Now print posts to xml
+ $status = backup_forum_posts($bf,$preferences,$for_dis->id);
+ //End discussion
+ $status =fwrite ($bf,end_tag("DISCUSSION",5,true));
+ }
+ //Write end tag
+ $status =fwrite ($bf,end_tag("DISCUSSIONS",4,true));
+ }
+ return $status;
+ }
+
+ //Backup forum_posts contents (executed from backup_forum_discussions)
+ function backup_forum_posts ($bf,$preferences,$discussion) {
+
+ global $CFG;
+
+ $status = true;
+
+ $forum_posts = get_records("forum_posts","discussion",$discussion,"id");
+ //If there are posts
+ if ($forum_posts) {
+ //Write start tag
+ $status =fwrite ($bf,start_tag("POSTS",6,true));
+ //Iterate over each post
+ foreach ($forum_posts as $for_pos) {
+ //Start post
+ $status =fwrite ($bf,start_tag("POST",7,true));
+ //Print forum_posts contents
+ fwrite ($bf,full_tag("ID",8,false,$for_pos->id));
+ fwrite ($bf,full_tag("PARENT",8,false,$for_pos->parent));
+ fwrite ($bf,full_tag("USERID",8,false,$for_pos->userid));
+ fwrite ($bf,full_tag("CREATED",8,false,$for_pos->created));
+ fwrite ($bf,full_tag("MODIFIED",8,false,$for_pos->modified));
+ fwrite ($bf,full_tag("MAILED",8,false,$for_pos->mailed));
+ fwrite ($bf,full_tag("SUBJECT",8,false,$for_pos->subject));
+ fwrite ($bf,full_tag("MESSAGE",8,false,$for_pos->message));
+ fwrite ($bf,full_tag("FORMAT",8,false,$for_pos->format));
+ fwrite ($bf,full_tag("ATTACHMENT",8,false,$for_pos->attachment));
+ fwrite ($bf,full_tag("TOTALSCORE",8,false,$for_pos->totalscore));
+ //Now print ratings to xml
+ $status = backup_forum_ratings($bf,$preferences,$for_pos->id);
+
+ //End discussion
+ $status =fwrite ($bf,end_tag("POST",7,true));
+ }
+ //Write end tag
+ $status =fwrite ($bf,end_tag("POSTS",6,true));
+ }
+ return $status;
+ }
+
+
+ //Backup forum_ratings contents (executed from backup_forum_posts)
+ function backup_forum_ratings ($bf,$preferences,$post) {
+
+ global $CFG;
+
+ $status = true;
+
+ $forum_ratings = get_records("forum_ratings","post",$post,"id");
+ //If there are ratings
+ if ($forum_ratings) {
+ //Write start tag
+ $status =fwrite ($bf,start_tag("RATINGS",8,true));
+ //Iterate over each rating
+ foreach ($forum_ratings as $for_rat) {
+ //Start rating
+ $status =fwrite ($bf,start_tag("RATING",9,true));
+ //Print forum_rating contents
+ fwrite ($bf,full_tag("ID",10,false,$for_rat->id));
+ fwrite ($bf,full_tag("USERID",10,false,$for_rat->userid));
+ fwrite ($bf,full_tag("TIME",10,false,$for_rat->time));
+ fwrite ($bf,full_tag("POST_RATING",10,false,$for_rat->rating));
+ //End rating
+ $status =fwrite ($bf,end_tag("RATING",9,true));
+ }
+ //Write end tag
+ $status =fwrite ($bf,end_tag("RATINGS",8,true));
+ }
+ return $status;
+ }
+
+ //Backup forum files because we've selected to backup user info
+ //and files are user info's level
+ function backup_forum_files($bf,$preferences) {
+
+ global $CFG;
+
+ $status = true;
+
+ //First we check to moddata exists and create it as necessary
+ //in temp/backup/$backup_code dir
+ $status = check_and_create_moddata_dir($preferences->backup_unique_code);
+ //Now copy the forum dir
+ if ($status) {
+ //Only if it exists !! Thanks to Daniel Miksik.
+ if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/forum")) {
+ $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/forum",
+ $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/forum");
+ }
+ }
+
+ return $status;
+
+ }
+
+
+ ////Return an array of info (name,value)
+ function forum_check_backup_mods($course,$user_data=false,$backup_unique_code) {
+ //First the course data
+ $info[0][0] = get_string("modulenameplural","forum");
+ if ($ids = forum_ids ($course)) {
+ $info[0][1] = count($ids);
+ } else {
+ $info[0][1] = 0;
+ }
+
+ //Now, if requested, the user_data
+ if ($user_data) {
+ //Subscriptions
+ $info[1][0] = get_string("subscriptions","forum");
+ if ($ids = forum_subscription_ids_by_course ($course)) {
+ $info[1][1] = count($ids);
+ } else {
+ $info[1][1] = 0;
+ }
+ //Discussions
+ $info[2][0] = get_string("discussions","forum");
+ if ($ids = forum_discussion_ids_by_course ($course)) {
+ $info[2][1] = count($ids);
+ } else {
+ $info[2][1] = 0;
+ }
+ //Posts
+ $info[3][0] = get_string("posts","forum");
+ if ($ids = forum_post_ids_by_course ($course)) {
+ $info[3][1] = count($ids);
+ } else {
+ $info[3][1] = 0;
+ }
+ //Ratings
+ $info[4][0] = get_string("ratings","forum");
+ if ($ids = forum_rating_ids_by_course ($course)) {
+ $info[4][1] = count($ids);
+ } else {
+ $info[4][1] = 0;
+ }
+ }
+ return $info;
+ }
+
+
+
+
+
+
+ // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
+
+ //Returns an array of forums id
+ function forum_ids ($course) {
+
+ global $CFG;
+
+ return get_records_sql ("SELECT a.id, a.course
+ FROM {$CFG->prefix}forum a
+ WHERE a.course = '$course'");
+ }
+
+ //Returns an array of forum subscriptions id
+ function forum_subscription_ids_by_course ($course) {
+
+ global $CFG;
+
+ return get_records_sql ("SELECT s.id , s.forum
+ FROM {$CFG->prefix}forum_subscriptions s,
+ {$CFG->prefix}forum a
+ WHERE a.course = '$course' AND
+ s.forum = a.id");
+ }
+
+ //Returns an array of forum discussions id
+ function forum_discussion_ids_by_course ($course) {
+
+ global $CFG;
+
+ return get_records_sql ("SELECT s.id , s.forum
+ FROM {$CFG->prefix}forum_discussions s,
+ {$CFG->prefix}forum a
+ WHERE a.course = '$course' AND
+ s.forum = a.id");
+ }
+
+ //Returns an array of forum posts id
+ function forum_post_ids_by_course ($course) {
+
+ global $CFG;
+
+ return get_records_sql ("SELECT p.id , p.discussion, s.forum
+ FROM {$CFG->prefix}forum_posts p,
+ {$CFG->prefix}forum_discussions s,
+ {$CFG->prefix}forum a
+ WHERE a.course = '$course' AND
+ s.forum = a.id AND
+ p.discussion = s.id");
+ }
+
+ //Returns an array of ratings posts id
+ function forum_rating_ids_by_course ($course) {
+
+ global $CFG;
+
+ return get_records_sql ("SELECT r.id, r.post, p.discussion, s.forum
+ FROM {$CFG->prefix}forum_ratings r,
+ {$CFG->prefix}forum_posts p,
+ {$CFG->prefix}forum_discussions s,
+ {$CFG->prefix}forum a
+ WHERE a.course = '$course' AND
+ s.forum = a.id AND
+ p.discussion = s.id AND
+ r.post = p.id");
+ }
+?>
--- /dev/null
+<?PHP //$Id$
+ //This php script contains all the stuff to backup/restore
+ //forum mods
+
+ //This is the "graphical" structure of the forum mod:
+ //
+ // forum
+ // (CL,pk->id)
+ // |
+ // -----------------------------------
+ // | |
+ // subscriptions forum_discussions
+ // (UL,pk->id, fk->forum) (UL,pk->id, fk->forum)
+ // |
+ // |
+ // |
+ // forum_posts
+ // (UL,pk->id,fk->discussion,nt->parent,files)
+ // |
+ // |
+ // |
+ // forum_ratings
+ // (UL,pk->id,fk->post)
+ //
+ // Meaning: pk->primary key field of the table
+ // fk->foreign key to link with parent
+ // nt->nested field (recursive data)
+ // CL->course level info
+ // UL->user level info
+ // files->table may have files)
+ //
+ //-----------------------------------------------------------
+
+ function forum_restore_mods($mod,$restore) {
+
+ global $CFG,$db;
+
+ $status = true;
+
+ //Get record from backup_ids
+ $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
+
+ if ($data) {
+ //Now get completed xmlized object
+ $info = $data->info;
+ //traverse_xmlize($info); //Debug
+ //print_object ($GLOBALS['traverse_array']); //Debug
+ //$GLOBALS['traverse_array']=""; //Debug
+
+ //Now, build the FORUM record structure
+ $forum->course = $restore->course_id;
+ $forum->type = backup_todb($info['MOD']['#']['TYPE']['0']['#']);
+ $forum->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
+ $forum->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
+ $forum->open = backup_todb($info['MOD']['#']['OPEN']['0']['#']);
+ $forum->assessed = backup_todb($info['MOD']['#']['ASSESSED']['0']['#']);
+ $forum->forcesubscribe = backup_todb($info['MOD']['#']['FORCESUBSCRIBE']['0']['#']);
+ $forum->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
+
+ //The structure is equal to the db, so insert the forum
+ $newid = insert_record ("forum",$forum);
+
+ //Do some output
+ echo "<ul><li>Forum \"".$forum->name."\"<br>";
+ backup_flush(300);
+
+ if ($newid) {
+ //We have the newid, update backup_ids
+ backup_putid($restore->backup_unique_code,$mod->modtype,
+ $mod->id, $newid);
+ //Now check if want to restore user data and do it.
+ if ($restore->mods[forum]->userinfo) {
+ //Restore forum_subscriptions
+ $status = forum_subscriptions_restore_mods ($newid,$info,$restore);
+ if ($status) {
+ //Restore forum_discussions
+ $status = forum_discussions_restore_mods ($newid,$info,$restore);
+ }
+ }
+ } else {
+ $status = false;
+ }
+
+ //Finalize ul
+ echo "</ul>";
+
+ } else {
+ $status = false;
+ }
+
+ return $status;
+ }
+
+ //This function restores the forum_subscriptions
+ function forum_subscriptions_restore_mods($forum_id,$info,$restore) {
+
+ global $CFG;
+
+ $status = true;
+
+ //Get the discussions array
+ $subscriptions = $info['MOD']['#']['SUBSCRIPTIONS']['0']['#']['SUBSCRIPTION'];
+
+ //Iterate over subscriptions
+ for($i = 0; $i < sizeof($subscriptions); $i++) {
+ $sus_info = $subscriptions[$i];
+ //traverse_xmlize($sus_info); //Debug
+ //print_object ($GLOBALS['traverse_array']); //Debug
+ //$GLOBALS['traverse_array']=""; //Debug
+
+ //We'll need this later!!
+ $oldid = backup_todb($sus_info['#']['ID']['0']['#']);
+ $olduserid = backup_todb($sus_info['#']['USERID']['0']['#']);
+
+ //Now, build the FORUM_SUBSCRIPTIONS record structure
+ $subscription->forum = $forum_id;
+ $subscription->userid = backup_todb($sus_info['#']['USERID']['0']['#']);
+
+ //We have to recode the userid field
+ $user = backup_getid($restore->backup_unique_code,"user",$subscription->userid);
+ if ($user) {
+ $subscription->userid = $user->new_id;
+ }
+
+ //The structure is equal to the db, so insert the forum_subscription
+ $newid = insert_record ("forum_subscriptions",$subscription);
+
+ //Do some output
+ if (($i+1) % 50 == 0) {
+ echo ".";
+ if (($i+1) % 1000 == 0) {
+ echo "<br>";
+ }
+ backup_flush(300);
+ }
+
+ if ($newid) {
+ //We have the newid, update backup_ids
+ backup_putid($restore->backup_unique_code,"forum_subscriptions",$oldid,
+ $newid);
+ } else {
+ $status = false;
+ }
+ }
+
+ return $status;
+ }
+
+ //This function restores the forum_discussions
+ function forum_discussions_restore_mods($forum_id,$info,$restore) {
+
+ global $CFG;
+
+ $status = true;
+
+ //Get the discussions array
+ $discussions = $info['MOD']['#']['DISCUSSIONS']['0']['#']['DISCUSSION'];
+
+ //Iterate over discussions
+ for($i = 0; $i < sizeof($discussions); $i++) {
+ $dis_info = $discussions[$i];
+ //traverse_xmlize($dis_info); //Debug
+ //print_object ($GLOBALS['traverse_array']); //Debug
+ //$GLOBALS['traverse_array']=""; //Debug
+
+ //We'll need this later!!
+ $oldid = backup_todb($dis_info['#']['ID']['0']['#']);
+ $olduserid = backup_todb($dis_info['#']['USERID']['0']['#']);
+
+ //Now, build the FORUM_DISCUSSIONS record structure
+ $discussion->forum = $forum_id;
+ $discussion->course = $restore->course_id;
+ $discussion->name = backup_todb($dis_info['#']['NAME']['0']['#']);
+ $discussion->firstpost = backup_todb($dis_info['#']['FIRSTPOST']['0']['#']);
+ $discussion->assessed = backup_todb($dis_info['#']['ASSESSED']['0']['#']);
+ $discussion->timemodified = backup_todb($dis_info['#']['TIMEMODIFIED']['0']['#']);
+
+ //The structure is equal to the db, so insert the forum_discussions
+ $newid = insert_record ("forum_discussions",$discussion);
+
+ //Do some output
+ if (($i+1) % 50 == 0) {
+ echo ".";
+ if (($i+1) % 1000 == 0) {
+ echo "<br>";
+ }
+ backup_flush(300);
+ }
+
+ if ($newid) {
+ //We have the newid, update backup_ids
+ backup_putid($restore->backup_unique_code,"forum_discussions",$oldid,
+ $newid);
+ //Restore forum_posts
+ $status = forum_posts_restore_mods ($forum_id,$newid,$dis_info,$restore);
+ //Now recalculate firstpost field
+ $old_firstpost = $discussion->firstpost;
+ //Get its new post_id from backup_ids table
+ $rec = backup_getid($restore->backup_unique_code,"forum_posts",$old_firstpost);
+ if ($rec) {
+ //Put its new firstpost
+ $discussion->firstpost = $rec->new_id;
+ } else {
+ $discussion->firstpost = 0;
+ }
+ //Create temp discussion record
+ $temp_discussion->id = $newid;
+ $temp_discussion->firstpost = $discussion->firstpost;
+ //Update discussion (only firstpost will be changed)
+ $status = update_record("forum_discussions",$temp_discussion);
+ //echo "Updated firstpost ".$old_firstpost." to ".$temp_discussion->firstpost."<br>"; //Debug
+ } else {
+ $status = false;
+ }
+ }
+
+ return $status;
+ }
+
+ //This function restores the forum_posts
+ function forum_posts_restore_mods($new_forum_id,$discussion_id,$info,$restore) {
+
+ global $CFG;
+
+ $status = true;
+
+ //Get the posts array
+ $posts = $info['#']['POSTS']['0']['#']['POST'];
+
+ //Iterate over posts
+ for($i = 0; $i < sizeof($posts); $i++) {
+ $pos_info = $posts[$i];
+ //traverse_xmlize($pos_info); //Debug
+ //print_object ($GLOBALS['traverse_array']); //Debug
+ //$GLOBALS['traverse_array']=""; //Debug
+
+ //We'll need this later!!
+ $oldid = backup_todb($pos_info['#']['ID']['0']['#']);
+ $olduserid = backup_todb($pos_info['#']['USERID']['0']['#']);
+
+ //Now, build the FORUM_POSTS record structure
+ $post->discussion = $discussion_id;
+ $post->parent = backup_todb($pos_info['#']['PARENT']['0']['#']);
+ $post->userid = backup_todb($pos_info['#']['USERID']['0']['#']);
+ $post->created = backup_todb($pos_info['#']['CREATED']['0']['#']);
+ $post->modified = backup_todb($pos_info['#']['MODIFIED']['0']['#']);
+ $post->mailed = backup_todb($pos_info['#']['MAILED']['0']['#']);
+ $post->subject = backup_todb($pos_info['#']['SUBJECT']['0']['#']);
+ $post->message = backup_todb($pos_info['#']['MESSAGE']['0']['#']);
+ $post->format = backup_todb($pos_info['#']['FORMAT']['0']['#']);
+ $post->attachment = backup_todb($pos_info['#']['ATTACHMENT']['0']['#']);
+ $post->totalscore = backup_todb($pos_info['#']['TOTALSCORE']['0']['#']);
+
+ //We have to recode the userid field
+ $user = backup_getid($restore->backup_unique_code,"user",$post->userid);
+ if ($user) {
+ $post->userid = $user->new_id;
+ }
+
+ //The structure is equal to the db, so insert the forum_posts
+ $newid = insert_record ("forum_posts",$post);
+
+ //Do some output
+ if (($i+1) % 50 == 0) {
+ echo ".";
+ if (($i+1) % 1000 == 0) {
+ echo "<br>";
+ }
+ backup_flush(300);
+ }
+
+ if ($newid) {
+ //We have the newid, update backup_ids
+ backup_putid($restore->backup_unique_code,"forum_posts",$oldid,
+ $newid);
+
+ //Get old forum id from backup_ids
+ $rec = get_record("backup_ids","backup_code",$restore->backup_unique_code,
+ "table_name","forum",
+ "new_id",$new_forum_id);
+ //Now copy moddata associated files
+ $status = forum_restore_files ($rec->old_id, $new_forum_id,
+ $oldid, $newid, $restore);
+
+ //Now restore post ratings
+ $status = forum_ratings_restore_mods($newid,$pos_info,$restore);
+
+ } else {
+ $status = false;
+ }
+ }
+
+ //Now we get every post in this discussion_id and recalculate its parent post
+ $posts = get_records ("forum_posts","discussion",$discussion_id);
+ if ($posts) {
+ //Iterate over each post
+ foreach ($posts as $post) {
+ //Get its parent
+ $old_parent = $post->parent;
+ //Get its new post_id from backup_ids table
+ $rec = backup_getid($restore->backup_unique_code,"forum_posts",$old_parent);
+ if ($rec) {
+ //Put its new parent
+ $post->parent = $rec->new_id;
+ } else {
+ $post->parent = 0;
+ }
+ //Create temp post record
+ $temp_post->id = $post->id;
+ $temp_post->parent = $post->parent;
+ //echo "Updated parent ".$old_parent." to ".$temp_post->parent."<br>"; //Debug
+ //Update post (only parent will be changed)
+ $status = update_record("forum_posts",$temp_post);
+ }
+ }
+
+ return $status;
+ }
+
+ //This function copies the forum related info from backup temp dir to course moddata folder,
+ //creating it if needed and recoding everything (forum id and post id)
+ function forum_restore_files ($oldforid, $newforid, $oldpostid, $newpostid, $restore) {
+
+ global $CFG;
+
+ $status = true;
+ $todo = false;
+ $moddata_path = "";
+ $forum_path = "";
+ $temp_path = "";
+
+ //First, locate course's moddata directory
+ $moddata_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
+
+ //Check it exists and create it
+ $status = check_dir_exists($moddata_path,true);
+
+ //Now, locate forum directory
+ if ($status) {
+ $forum_path = $moddata_path."/forum";
+ //Check it exists and create it
+ $status = check_dir_exists($forum_path,true);
+ }
+
+ //Now locate the temp dir we are restoring from
+ if ($status) {
+ $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
+ "/moddata/forum/".$oldforid."/".$oldpostid;
+ //Check it exists
+ if (is_dir($temp_path)) {
+ $todo = true;
+ }
+ }
+
+ //If todo, we create the neccesary dirs in course moddata/forum
+ if ($status and $todo) {
+ //First this forum id
+ $this_forum_path = $forum_path."/".$newforid;
+ $status = check_dir_exists($this_forum_path,true);
+ //Now this post id
+ $post_forum_path = $this_forum_path."/".$newpostid;
+ //And now, copy temp_path to post_forum_path
+ $status = backup_copy_file($temp_path, $post_forum_path);
+ }
+
+ return $status;
+ }
+
+ //This function restores the forum_ratings
+ function forum_ratings_restore_mods($new_post_id,$info,$restore) {
+
+ global $CFG;
+
+ $status = true;
+
+ //Get the ratings array
+ $ratings = $info['#']['RATINGS']['0']['#']['RATING'];
+
+ //Iterate over ratings
+ for($i = 0; $i < sizeof($ratings); $i++) {
+ $rat_info = $ratings[$i];
+ //traverse_xmlize($rat_info); //Debug
+ //print_object ($GLOBALS['traverse_array']); //Debug
+ //$GLOBALS['traverse_array']=""; //Debug
+
+ //We'll need this later!!
+ $oldid = backup_todb($rat_info['#']['ID']['0']['#']);
+ $olduserid = backup_todb($rat_info['#']['USERID']['0']['#']);
+
+ //Now, build the FORM_RATINGS record structure
+ $rating->post = $new_post_id;
+ $rating->userid = backup_todb($rat_info['#']['USERID']['0']['#']);
+ $rating->time = backup_todb($rat_info['#']['TIME']['0']['#']);
+ $rating->rating = backup_todb($rat_info['#']['POST_RATING']['0']['#']);
+
+ //We have to recode the userid field
+ $user = backup_getid($restore->backup_unique_code,"user",$rating->userid);
+ if ($user) {
+ $rating->userid = $user->new_id;
+ }
+
+ //The structure is equal to the db, so insert the forum_ratings
+ $newid = insert_record ("forum_ratings",$rating);
+
+ //Do some output
+ if (($i+1) % 50 == 0) {
+ echo ".";
+ if (($i+1) % 1000 == 0) {
+ echo "<br>";
+ }
+ backup_flush(300);
+ }
+
+ if ($newid) {
+ //We have the newid, update backup_ids
+ backup_putid($restore->backup_unique_code,"forum_ratings",$oldid,
+ $newid);
+ } else {
+ $status = false;
+ }
+ }
+
+ return $status;
+ }
+
+?>