MDL-10234
[moodle-linuxchix.git] / mod / forum / backuplib.php
blob9819f5008fbdd2956ed740f34192c4d7bd9e932e
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_backup_mods($bf,$preferences) {
37 global $CFG;
39 $status = true;
41 //Iterate over forum table
42 $forums = get_records ("forum","course",$preferences->backup_course,"id");
43 if ($forums) {
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.
51 return $status;
55 function forum_backup_one_mod($bf,$preferences,$forum) {
57 global $CFG;
59 if (is_numeric($forum)) {
60 $forum = get_record('forum','id',$forum);
62 $instanceid = $forum->id;
64 $status = true;
66 //Start mod
67 fwrite ($bf,start_tag("MOD",3,true));
69 //Print forum data
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);
93 if ($status) {
94 $status = backup_forum_discussions($bf,$preferences,$forum->id);
96 if ($status) {
97 $status = backup_forum_read($bf,$preferences,$forum->id);
99 if ($status) {
100 $status = backup_forum_files_instance($bf,$preferences,$forum->id);
103 //End mod
104 $status =fwrite ($bf,end_tag("MOD",3,true));
105 return $status;
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);
116 } else {
117 $info[$instance->id.'1'][1] = 0;
119 //Discussions
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);
123 } else {
124 $info[$instance->id.'2'][1] = 0;
126 //Posts
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);
130 } else {
131 $info[$instance->id.'3'][1] = 0;
133 //Ratings
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);
137 } else {
138 $info[$instance->id.'4'][1] = 0;
141 return $info;
144 //Backup forum_subscriptions contents (executed from forum_backup_mods)
145 function backup_forum_subscriptions ($bf,$preferences,$forum) {
147 global $CFG;
149 $status = true;
151 $forum_subscriptions = get_records("forum_subscriptions","forum",$forum,"id");
152 //If there is subscriptions
153 if ($forum_subscriptions) {
154 //Write start tag
155 $status =fwrite ($bf,start_tag("SUBSCRIPTIONS",4,true));
156 //Iterate over each answer
157 foreach ($forum_subscriptions as $for_sus) {
158 //Start suscription
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));
163 //End subscription
164 $status =fwrite ($bf,end_tag("SUBSCRIPTION",5,true));
166 //Write end tag
167 $status =fwrite ($bf,end_tag("SUBSCRIPTIONS",4,true));
169 return $status;
172 //Backup forum_discussions contents (executed from forum_backup_mods)
173 function backup_forum_discussions ($bf,$preferences,$forum) {
175 global $CFG;
177 $status = true;
179 $forum_discussions = get_records("forum_discussions","forum",$forum,"id");
180 //If there are discussions
181 if ($forum_discussions) {
182 //Write start tag
183 $status =fwrite ($bf,start_tag("DISCUSSIONS",4,true));
184 //Iterate over each discussion
185 foreach ($forum_discussions as $for_dis) {
186 //Start discussion
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);
201 //End discussion
202 $status =fwrite ($bf,end_tag("DISCUSSION",5,true));
204 //Write end tag
205 $status =fwrite ($bf,end_tag("DISCUSSIONS",4,true));
207 return $status;
210 //Backup forum_read contents (executed from forum_backup_mods)
211 function backup_forum_read ($bf,$preferences,$forum) {
213 global $CFG;
215 $status = true;
217 $forum_read = get_records("forum_read","forumid",$forum,"id");
218 //If there are read
219 if ($forum_read) {
220 //Write start tag
221 $status =fwrite ($bf,start_tag("READPOSTS",4,true));
222 //Iterate over each read
223 foreach ($forum_read as $for_rea) {
224 //Start read
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));
233 //End read
234 $status =fwrite ($bf,end_tag("READ",5,true));
236 //Write end tag
237 $status =fwrite ($bf,end_tag("READPOSTS",4,true));
239 return $status;
242 //Backup forum_posts contents (executed from backup_forum_discussions)
243 function backup_forum_posts ($bf,$preferences,$discussion) {
245 global $CFG;
247 $status = true;
249 $forum_posts = get_records("forum_posts","discussion",$discussion,"id");
250 //If there are posts
251 if ($forum_posts) {
252 //Write start tag
253 $status =fwrite ($bf,start_tag("POSTS",6,true));
254 //Iterate over each post
255 foreach ($forum_posts as $for_pos) {
256 //Start post
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);
274 //End discussion
275 $status =fwrite ($bf,end_tag("POST",7,true));
277 //Write end tag
278 $status =fwrite ($bf,end_tag("POSTS",6,true));
280 return $status;
284 //Backup forum_ratings contents (executed from backup_forum_posts)
285 function backup_forum_ratings ($bf,$preferences,$post) {
287 global $CFG;
289 $status = true;
291 $forum_ratings = get_records("forum_ratings","post",$post,"id");
292 //If there are ratings
293 if ($forum_ratings) {
294 //Write start tag
295 $status =fwrite ($bf,start_tag("RATINGS",8,true));
296 //Iterate over each rating
297 foreach ($forum_ratings as $for_rat) {
298 //Start rating
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));
305 //End rating
306 $status =fwrite ($bf,end_tag("RATING",9,true));
308 //Write end tag
309 $status =fwrite ($bf,end_tag("RATINGS",8,true));
311 return $status;
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) {
317 global $CFG;
319 $status = true;
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
325 if ($status) {
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);
340 return $status;
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) {
348 global $CFG;
350 $status = true;
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
357 if ($status) {
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);
365 return $status;
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)) {
373 $info = array();
374 foreach ($instances as $id => $instance) {
375 $info += forum_check_backup_mods_instances($instance,$backup_unique_code);
377 return $info;
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);
383 } else {
384 $info[0][1] = 0;
387 //Now, if requested, the user_data
388 if ($user_data) {
389 //Subscriptions
390 $info[1][0] = get_string("subscriptions","forum");
391 if ($ids = forum_subscription_ids_by_course ($course)) {
392 $info[1][1] = count($ids);
393 } else {
394 $info[1][1] = 0;
396 //Discussions
397 $info[2][0] = get_string("discussions","forum");
398 if ($ids = forum_discussion_ids_by_course ($course)) {
399 $info[2][1] = count($ids);
400 } else {
401 $info[2][1] = 0;
403 //Posts
404 $info[3][0] = get_string("posts","forum");
405 if ($ids = forum_post_ids_by_course ($course)) {
406 $info[3][1] = count($ids);
407 } else {
408 $info[3][1] = 0;
410 //Ratings
411 $info[4][0] = get_string("ratings","forum");
412 if ($ids = forum_rating_ids_by_course ($course)) {
413 $info[4][1] = count($ids);
414 } else {
415 $info[4][1] = 0;
418 return $info;
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) {
425 global $CFG;
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);
453 return $result;
456 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
458 //Returns an array of forums id
459 function forum_ids ($course) {
461 global $CFG;
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) {
471 global $CFG;
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
477 s.forum = a.id");
480 //Returns an array of forum subscriptions id
481 function forum_subscription_ids_by_instance($instanceid) {
483 global $CFG;
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) {
493 global $CFG;
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
499 s.forum = a.id");
502 //Returns an array of forum discussions id
503 function forum_discussion_ids_by_instance ($instanceid) {
505 global $CFG;
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) {
515 global $CFG;
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
522 s.forum = a.id AND
523 p.discussion = s.id");
526 //Returns an array of forum posts id
527 function forum_post_ids_by_instance ($instanceid) {
529 global $CFG;
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) {
541 global $CFG;
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
549 s.forum = a.id AND
550 p.discussion = s.id AND
551 r.post = p.id");
554 //Returns an array of ratings posts id
555 function forum_rating_ids_by_instance ($instanceid) {
557 global $CFG;
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
565 r.post = p.id");