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_backup_mods($bf,$preferences) {
41 //Iterate over forum table
42 $forums = get_records ("forum","course",$preferences->backup_course
,"id");
44 foreach ($forums as $forum) {
45 if (backup_mod_selected($preferences,'forum',$forum->id
)) {
46 $status = forum_backup_one_mod($bf,$preferences,$forum);
47 // backup files happens in backup_one_mod now too.
55 function forum_backup_one_mod($bf,$preferences,$forum) {
59 if (is_numeric($forum)) {
60 $forum = get_record('forum','id',$forum);
62 $instanceid = $forum->id
;
67 fwrite ($bf,start_tag("MOD",3,true));
70 fwrite ($bf,full_tag("ID",4,false,$forum->id
));
71 fwrite ($bf,full_tag("MODTYPE",4,false,"forum"));
72 fwrite ($bf,full_tag("TYPE",4,false,$forum->type
));
73 fwrite ($bf,full_tag("NAME",4,false,$forum->name
));
74 fwrite ($bf,full_tag("INTRO",4,false,$forum->intro
));
75 fwrite ($bf,full_tag("ASSESSED",4,false,$forum->assessed
));
76 fwrite ($bf,full_tag("ASSESSTIMESTART",4,false,$forum->assesstimestart
));
77 fwrite ($bf,full_tag("ASSESSTIMEFINISH",4,false,$forum->assesstimefinish
));
78 fwrite ($bf,full_tag("MAXBYTES",4,false,$forum->maxbytes
));
79 fwrite ($bf,full_tag("SCALE",4,false,$forum->scale
));
80 fwrite ($bf,full_tag("FORCESUBSCRIBE",4,false,$forum->forcesubscribe
));
81 fwrite ($bf,full_tag("TRACKINGTYPE",4,false,$forum->trackingtype
));
82 fwrite ($bf,full_tag("RSSTYPE",4,false,$forum->rsstype
));
83 fwrite ($bf,full_tag("RSSARTICLES",4,false,$forum->rssarticles
));
84 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$forum->timemodified
));
85 fwrite ($bf,full_tag("WARNAFTER",4,false,$forum->warnafter
));
86 fwrite ($bf,full_tag("BLOCKAFTER",4,false,$forum->blockafter
));
87 fwrite ($bf,full_tag("BLOCKPERIOD",4,false,$forum->blockperiod
));
89 //if we've selected to backup users info, then execute backup_forum_suscriptions and
90 //backup_forum_discussions
91 if (backup_userdata_selected($preferences,'forum',$forum->id
)) {
92 $status = backup_forum_subscriptions($bf,$preferences,$forum->id
);
94 $status = backup_forum_discussions($bf,$preferences,$forum->id
);
97 $status = backup_forum_read($bf,$preferences,$forum->id
);
100 $status = backup_forum_files_instance($bf,$preferences,$forum->id
);
104 $status =fwrite ($bf,end_tag("MOD",3,true));
109 function forum_check_backup_mods_instances($instance,$backup_unique_code) {
110 $info[$instance->id
.'0'][0] = '<b>'.$instance->name
.'</b>';
111 $info[$instance->id
.'0'][1] = '';
112 if (!empty($instance->userdata
)) {
113 $info[$instance->id
.'1'][0] = get_string("subscriptions","forum");
114 if ($ids = forum_subscription_ids_by_instance ($instance->id
)) {
115 $info[$instance->id
.'1'][1] = count($ids);
117 $info[$instance->id
.'1'][1] = 0;
120 $info[$instance->id
.'2'][0] = get_string("discussions","forum");
121 if ($ids = forum_discussion_ids_by_instance ($instance->id
)) {
122 $info[$instance->id
.'2'][1] = count($ids);
124 $info[$instance->id
.'2'][1] = 0;
127 $info[$instance->id
.'3'][0] = get_string("posts","forum");
128 if ($ids = forum_post_ids_by_instance ($instance->id
)) {
129 $info[$instance->id
.'3'][1] = count($ids);
131 $info[$instance->id
.'3'][1] = 0;
134 $info[$instance->id
.'4'][0] = get_string("ratings","forum");
135 if ($ids = forum_rating_ids_by_instance ($instance->id
)) {
136 $info[$instance->id
.'4'][1] = count($ids);
138 $info[$instance->id
.'4'][1] = 0;
144 //Backup forum_subscriptions contents (executed from forum_backup_mods)
145 function backup_forum_subscriptions ($bf,$preferences,$forum) {
151 $forum_subscriptions = get_records("forum_subscriptions","forum",$forum,"id");
152 //If there is subscriptions
153 if ($forum_subscriptions) {
155 $status =fwrite ($bf,start_tag("SUBSCRIPTIONS",4,true));
156 //Iterate over each answer
157 foreach ($forum_subscriptions as $for_sus) {
159 $status =fwrite ($bf,start_tag("SUBSCRIPTION",5,true));
160 //Print forum_subscriptions contents
161 fwrite ($bf,full_tag("ID",6,false,$for_sus->id
));
162 fwrite ($bf,full_tag("USERID",6,false,$for_sus->userid
));
164 $status =fwrite ($bf,end_tag("SUBSCRIPTION",5,true));
167 $status =fwrite ($bf,end_tag("SUBSCRIPTIONS",4,true));
172 //Backup forum_discussions contents (executed from forum_backup_mods)
173 function backup_forum_discussions ($bf,$preferences,$forum) {
179 $forum_discussions = get_records("forum_discussions","forum",$forum,"id");
180 //If there are discussions
181 if ($forum_discussions) {
183 $status =fwrite ($bf,start_tag("DISCUSSIONS",4,true));
184 //Iterate over each discussion
185 foreach ($forum_discussions as $for_dis) {
187 $status =fwrite ($bf,start_tag("DISCUSSION",5,true));
188 //Print forum_discussions contents
189 fwrite ($bf,full_tag("ID",6,false,$for_dis->id
));
190 fwrite ($bf,full_tag("NAME",6,false,$for_dis->name
));
191 fwrite ($bf,full_tag("FIRSTPOST",6,false,$for_dis->firstpost
));
192 fwrite ($bf,full_tag("USERID",6,false,$for_dis->userid
));
193 fwrite ($bf,full_tag("GROUPID",6,false,$for_dis->groupid
));
194 fwrite ($bf,full_tag("ASSESSED",6,false,$for_dis->assessed
));
195 fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$for_dis->timemodified
));
196 fwrite ($bf,full_tag("USERMODIFIED",6,false,$for_dis->usermodified
));
197 fwrite ($bf,full_tag("TIMESTART",6,false,$for_dis->timestart
));
198 fwrite ($bf,full_tag("TIMEEND",6,false,$for_dis->timeend
));
199 //Now print posts to xml
200 $status = backup_forum_posts($bf,$preferences,$for_dis->id
);
202 $status =fwrite ($bf,end_tag("DISCUSSION",5,true));
205 $status =fwrite ($bf,end_tag("DISCUSSIONS",4,true));
210 //Backup forum_read contents (executed from forum_backup_mods)
211 function backup_forum_read ($bf,$preferences,$forum) {
217 $forum_read = get_records("forum_read","forumid",$forum,"id");
221 $status =fwrite ($bf,start_tag("READPOSTS",4,true));
222 //Iterate over each read
223 foreach ($forum_read as $for_rea) {
225 $status =fwrite ($bf,start_tag("READ",5,true));
226 //Print forum_read contents
227 fwrite ($bf,full_tag("ID",6,false,$for_rea->id
));
228 fwrite ($bf,full_tag("USERID",6,false,$for_rea->userid
));
229 fwrite ($bf,full_tag("DISCUSSIONID",6,false,$for_rea->discussionid
));
230 fwrite ($bf,full_tag("POSTID",6,false,$for_rea->postid
));
231 fwrite ($bf,full_tag("FIRSTREAD",6,false,$for_rea->firstread
));
232 fwrite ($bf,full_tag("LASTREAD",6,false,$for_rea->lastread
));
234 $status =fwrite ($bf,end_tag("READ",5,true));
237 $status =fwrite ($bf,end_tag("READPOSTS",4,true));
242 //Backup forum_posts contents (executed from backup_forum_discussions)
243 function backup_forum_posts ($bf,$preferences,$discussion) {
249 $forum_posts = get_records("forum_posts","discussion",$discussion,"id");
253 $status =fwrite ($bf,start_tag("POSTS",6,true));
254 //Iterate over each post
255 foreach ($forum_posts as $for_pos) {
257 $status =fwrite ($bf,start_tag("POST",7,true));
258 //Print forum_posts contents
259 fwrite ($bf,full_tag("ID",8,false,$for_pos->id
));
260 fwrite ($bf,full_tag("PARENT",8,false,$for_pos->parent
));
261 fwrite ($bf,full_tag("USERID",8,false,$for_pos->userid
));
262 fwrite ($bf,full_tag("CREATED",8,false,$for_pos->created
));
263 fwrite ($bf,full_tag("MODIFIED",8,false,$for_pos->modified
));
264 fwrite ($bf,full_tag("MAILED",8,false,$for_pos->mailed
));
265 fwrite ($bf,full_tag("SUBJECT",8,false,$for_pos->subject
));
266 fwrite ($bf,full_tag("MESSAGE",8,false,$for_pos->message
));
267 fwrite ($bf,full_tag("FORMAT",8,false,$for_pos->format
));
268 fwrite ($bf,full_tag("ATTACHMENT",8,false,$for_pos->attachment
));
269 fwrite ($bf,full_tag("TOTALSCORE",8,false,$for_pos->totalscore
));
270 fwrite ($bf,full_tag("MAILNOW",8,false,$for_pos->mailnow
));
271 //Now print ratings to xml
272 $status = backup_forum_ratings($bf,$preferences,$for_pos->id
);
275 $status =fwrite ($bf,end_tag("POST",7,true));
278 $status =fwrite ($bf,end_tag("POSTS",6,true));
284 //Backup forum_ratings contents (executed from backup_forum_posts)
285 function backup_forum_ratings ($bf,$preferences,$post) {
291 $forum_ratings = get_records("forum_ratings","post",$post,"id");
292 //If there are ratings
293 if ($forum_ratings) {
295 $status =fwrite ($bf,start_tag("RATINGS",8,true));
296 //Iterate over each rating
297 foreach ($forum_ratings as $for_rat) {
299 $status =fwrite ($bf,start_tag("RATING",9,true));
300 //Print forum_rating contents
301 fwrite ($bf,full_tag("ID",10,false,$for_rat->id
));
302 fwrite ($bf,full_tag("USERID",10,false,$for_rat->userid
));
303 fwrite ($bf,full_tag("TIME",10,false,$for_rat->time
));
304 fwrite ($bf,full_tag("POST_RATING",10,false,$for_rat->rating
));
306 $status =fwrite ($bf,end_tag("RATING",9,true));
309 $status =fwrite ($bf,end_tag("RATINGS",8,true));
314 //Backup forum files because we've selected to backup user info
315 //and files are user info's level
316 function backup_forum_files($bf,$preferences) {
321 //First we check to moddata exists and create it as necessary
322 //in temp/backup/$backup_code dir
323 $status = check_and_create_moddata_dir($preferences->backup_unique_code
);
324 //Now copy the forum dir
326 //Only if it exists !! Thanks to Daniel Miksik.
327 if (is_dir($CFG->dataroot
."/".$preferences->backup_course
."/".$CFG->moddata
."/forum")) {
328 $handle = opendir($CFG->dataroot
."/".$preferences->backup_course
."/".$CFG->moddata
."/forum");
329 while (false!==($item = readdir($handle))) {
330 if ($item != '.' && $item != '..' && is_dir($CFG->dataroot
."/".$preferences->backup_course
."/".$CFG->moddata
."/forum/".$item)
331 && array_key_exists($item,$preferences->mods
['forum']->instances
)
332 && !empty($preferences->mods
['forum']->instances
[$item]->backup
)) {
333 $status = backup_copy_file($CFG->dataroot
."/".$preferences->backup_course
."/".$CFG->moddata
."/forum/".$item,
334 $CFG->dataroot
."/temp/backup/".$preferences->backup_unique_code
."/moddata/forum/",$item);
345 //Backup forum files because we've selected to backup user info
346 //and files are user info's level
347 function backup_forum_files_instance($bf,$preferences,$instanceid) {
352 //First we check to moddata exists and create it as necessary
353 //in temp/backup/$backup_code dir
354 $status = check_and_create_moddata_dir($preferences->backup_unique_code
);
355 $status = check_dir_exists($CFG->dataroot
."/temp/backup/".$preferences->backup_unique_code
."/moddata/forum/",true);
356 //Now copy the forum dir
358 //Only if it exists !! Thanks to Daniel Miksik.
359 if (is_dir($CFG->dataroot
."/".$preferences->backup_course
."/".$CFG->moddata
."/forum/".$instanceid)) {
360 $status = backup_copy_file($CFG->dataroot
."/".$preferences->backup_course
."/".$CFG->moddata
."/forum/".$instanceid,
361 $CFG->dataroot
."/temp/backup/".$preferences->backup_unique_code
."/moddata/forum/".$instanceid);
369 ////Return an array of info (name,value)
370 function forum_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
372 if (!empty($instances) && is_array($instances) && count($instances)) {
374 foreach ($instances as $id => $instance) {
375 $info +
= forum_check_backup_mods_instances($instance,$backup_unique_code);
379 //First the course data
380 $info[0][0] = get_string("modulenameplural","forum");
381 if ($ids = forum_ids ($course)) {
382 $info[0][1] = count($ids);
387 //Now, if requested, the user_data
390 $info[1][0] = get_string("subscriptions","forum");
391 if ($ids = forum_subscription_ids_by_course ($course)) {
392 $info[1][1] = count($ids);
397 $info[2][0] = get_string("discussions","forum");
398 if ($ids = forum_discussion_ids_by_course ($course)) {
399 $info[2][1] = count($ids);
404 $info[3][0] = get_string("posts","forum");
405 if ($ids = forum_post_ids_by_course ($course)) {
406 $info[3][1] = count($ids);
411 $info[4][0] = get_string("ratings","forum");
412 if ($ids = forum_rating_ids_by_course ($course)) {
413 $info[4][1] = count($ids);
421 //Return a content encoded to support interactivities linking. Every module
422 //should have its own. They are called automatically from the backup procedure.
423 function forum_encode_content_links ($content,$preferences) {
427 $base = preg_quote($CFG->wwwroot
,"/");
429 //Link to the list of forums
430 $buscar="/(".$base."\/mod\/forum\/index.php\?id\=)([0-9]+)/";
431 $result= preg_replace($buscar,'$@FORUMINDEX*$2@$',$content);
433 //Link to forum view by moduleid
434 $buscar="/(".$base."\/mod\/forum\/view.php\?id\=)([0-9]+)/";
435 $result= preg_replace($buscar,'$@FORUMVIEWBYID*$2@$',$result);
437 //Link to forum view by forumid
438 $buscar="/(".$base."\/mod\/forum\/view.php\?f\=)([0-9]+)/";
439 $result= preg_replace($buscar,'$@FORUMVIEWBYF*$2@$',$result);
441 //Link to forum discussion with parent syntax
442 $buscar="/(".$base."\/mod\/forum\/discuss.php\?d\=)([0-9]+)\&parent\=([0-9]+)/";
443 $result= preg_replace($buscar,'$@FORUMDISCUSSIONVIEWPARENT*$2*$3@$',$result);
445 //Link to forum discussion with relative syntax
446 $buscar="/(".$base."\/mod\/forum\/discuss.php\?d\=)([0-9]+)\#([0-9]+)/";
447 $result= preg_replace($buscar,'$@FORUMDISCUSSIONVIEWINSIDE*$2*$3@$',$result);
449 //Link to forum discussion by discussionid
450 $buscar="/(".$base."\/mod\/forum\/discuss.php\?d\=)([0-9]+)/";
451 $result= preg_replace($buscar,'$@FORUMDISCUSSIONVIEW*$2@$',$result);
456 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
458 //Returns an array of forums id
459 function forum_ids ($course) {
463 return get_records_sql ("SELECT a.id, a.course
464 FROM {$CFG->prefix}forum a
465 WHERE a.course = '$course'");
468 //Returns an array of forum subscriptions id
469 function forum_subscription_ids_by_course ($course) {
473 return get_records_sql ("SELECT s.id , s.forum
474 FROM {$CFG->prefix}forum_subscriptions s,
475 {$CFG->prefix}forum a
476 WHERE a.course = '$course' AND
480 //Returns an array of forum subscriptions id
481 function forum_subscription_ids_by_instance($instanceid) {
485 return get_records_sql ("SELECT s.id , s.forum
486 FROM {$CFG->prefix}forum_subscriptions s
487 WHERE s.forum = $instanceid");
490 //Returns an array of forum discussions id
491 function forum_discussion_ids_by_course ($course) {
495 return get_records_sql ("SELECT s.id , s.forum
496 FROM {$CFG->prefix}forum_discussions s,
497 {$CFG->prefix}forum a
498 WHERE a.course = '$course' AND
502 //Returns an array of forum discussions id
503 function forum_discussion_ids_by_instance ($instanceid) {
507 return get_records_sql ("SELECT s.id , s.forum
508 FROM {$CFG->prefix}forum_discussions s
509 WHERE s.forum = $instanceid");
512 //Returns an array of forum posts id
513 function forum_post_ids_by_course ($course) {
517 return get_records_sql ("SELECT p.id , p.discussion, s.forum
518 FROM {$CFG->prefix}forum_posts p,
519 {$CFG->prefix}forum_discussions s,
520 {$CFG->prefix}forum a
521 WHERE a.course = '$course' AND
523 p.discussion = s.id");
526 //Returns an array of forum posts id
527 function forum_post_ids_by_instance ($instanceid) {
531 return get_records_sql ("SELECT p.id , p.discussion, s.forum
532 FROM {$CFG->prefix}forum_posts p,
533 {$CFG->prefix}forum_discussions s
534 WHERE s.forum = $instanceid AND
535 p.discussion = s.id");
538 //Returns an array of ratings posts id
539 function forum_rating_ids_by_course ($course) {
543 return get_records_sql ("SELECT r.id, r.post, p.discussion, s.forum
544 FROM {$CFG->prefix}forum_ratings r,
545 {$CFG->prefix}forum_posts p,
546 {$CFG->prefix}forum_discussions s,
547 {$CFG->prefix}forum a
548 WHERE a.course = '$course' AND
550 p.discussion = s.id AND
554 //Returns an array of ratings posts id
555 function forum_rating_ids_by_instance ($instanceid) {
559 return get_records_sql ("SELECT r.id, r.post, p.discussion, s.forum
560 FROM {$CFG->prefix}forum_ratings r,
561 {$CFG->prefix}forum_posts p,
562 {$CFG->prefix}forum_discussions s
563 WHERE s.forum = $instanceid AND
564 p.discussion = s.id AND