New translation added.
[moodle.git] / backup / backuplib.php
blobab4e2680be97a0dc806c94ab0b1728d031f75191
1 <?PHP //$Id$
2 //This file contains all the function needed in the backup utility
3 //except the mod-related funtions that are into every backuplib.php inside
4 //every mod directory
6 //Insert necessary category ids to backup_ids table
7 function insert_category_ids ($course,$backup_unique_code) {
8 global $CFG;
9 $status = true;
10 $status = execute_sql("INSERT INTO {$CFG->prefix}backup_ids
11 (backup_code, table_name, old_id)
12 SELECT DISTINCT '$backup_unique_code','quiz_categories',t.category
13 FROM {$CFG->prefix}quiz_questions t,
14 {$CFG->prefix}quiz_question_grades g,
15 {$CFG->prefix}quiz q
16 WHERE q.course = '$course' AND
17 g.quiz = q.id AND
18 g.question = t.id",false);
19 return $status;
22 //Delete category ids from backup_ids table
23 function delete_category_ids ($backup_unique_code) {
24 global $CFG;
25 $status = true;
26 $status = execute_sql("DELETE FROM {$CFG->prefix}backup_ids
27 WHERE backup_code = '$backup_unique_code'",false);
28 return $status;
31 //Calculate the number of users to backup and put their ids in backup_ids
32 //Return an array of info (name,value)
33 function user_check_backup($course,$backup_unique_code,$backup_users) {
34 //$backup_users=0-->all
35 // 1-->course
36 // 2-->needed-->NOT IMPLEMEMTED
38 global $CFG;
39 global $db;
41 $count_users = 0;
43 //Select all users from user (only id)
44 //If there are a lot of users and we retrieve all the info->memory shortage !!
45 $users = get_records ("user","","","","id,id");
46 //If we have users
47 if ($users) {
48 //Iterate over users putting their roles
49 foreach ($users as $user) {
50 $user->info = "";
51 //Is Admin in tables (not is_admin()) !!
52 if (record_exists("user_admins","userid",$user->id)) {
53 $user->info .= "admin";
54 $user->role_admin = true;
56 //Is Course Creator in tables (not is_coursecreator()) !!
57 if (record_exists("user_coursecreators","userid",$user->id)) {
58 $user->info .= "coursecreator";
59 $user->role_coursecreator = true;
61 //Is Teacher in tables (not is_teacher()) !!
62 if (record_exists("user_teachers","course",$course,"userid",$user->id)) {
63 $user->info .= "teacher";
64 $user->role_teacher = true;
66 //Is Student in tables (not is_student()) !!
67 if (record_exists("user_students","course",$course,"userid",$user->id)) {
68 $user->info .= "student";
69 $user->role_student = true;
71 //Now create the backup_id record
72 $backupids_rec->backup_code = $backup_unique_code;
73 $backupids_rec->table_name = "user";
74 $backupids_rec->old_id = $user->id;
75 $backupids_rec->info = $user->info;
77 //Insert the record id. backup_users decide it.
78 //When all users
79 if ($backup_users == 0) {
80 $status = insert_record("backup_ids",$backupids_rec,false);
81 $count_users++;
82 //When course users
83 } else if ($backup_users == 1) {
84 //Only if user has any role
85 if ($backupids_rec->info) {
86 $status = insert_record("backup_ids",$backupids_rec,false);
87 $count_users++;
91 //Do some output
92 backup_flush(30);
95 //Prepare Info
96 //Gets the user data
97 $info[0][0] = get_string("users");
98 $info[0][1] = $count_users;
100 return $info;
103 //Calculate the number of log entries to backup
104 //Return an array of info (name,value)
105 function log_check_backup($course) {
107 global $CFG;
109 //Now execute the count
110 $ids = count_records("log","course",$course);
112 //Gets the user data
113 $info[0][0] = get_string("logs");
114 if ($ids) {
115 $info[0][1] = $ids;
116 } else {
117 $info[0][1] = 0;
120 return $info;
123 //Calculate the number of user files to backup
124 //Under $CFG->dataroot/users
125 //and put them (their path) in backup_ids
126 //Return an array of info (name,value)
127 function user_files_check_backup($course,$backup_unique_code) {
129 global $CFG;
131 $rootdir = $CFG->dataroot."/users";
132 //Check if directory exists
133 if (is_dir($rootdir)) {
134 $coursedirs = get_directory_list($rootdir);
135 foreach ($coursedirs as $dir) {
136 //Extracts user id from file path
137 $tok = strtok($dir,"/");
138 if ($tok) {
139 $userid = $tok;
140 } else {
141 $tok = "";
143 //Insert them into backup_files
144 $status = execute_sql("INSERT INTO {$CFG->prefix}backup_files
145 (backup_code, file_type, path, old_id)
146 VALUES
147 ('$backup_unique_code','user','$dir','$userid')",false);
148 //Do some output
149 backup_flush(30);
153 //Now execute the select
154 $ids = get_records_sql("SELECT DISTINCT b.path, b.old_id
155 FROM {$CFG->prefix}backup_files b
156 WHERE backup_code = '$backup_unique_code' AND
157 file_type = 'user'");
158 //Gets the user data
159 $info[0][0] = get_string("files");
160 if ($ids) {
161 $info[0][1] = count($ids);
162 } else {
163 $info[0][1] = 0;
166 return $info;
169 //Calculate the number of course files to backup
170 //under $CFG->dataroot/$course, except $CFG->moddata, and get_string("backupdir)
171 //and put them (their path) in backup_ids
172 //Return an array of info (name,value)
173 function course_files_check_backup($course,$backup_unique_code) {
175 global $CFG;
177 $rootdir = $CFG->dataroot."/$course";
178 //Check if directory exists
179 if (is_dir($rootdir)) {
180 $coursedirs = get_directory_list($rootdir,$CFG->moddata);
181 $backupdir = get_string("backupdir");
182 foreach ($coursedirs as $dir) {
183 //Check it isn't backupdir
184 if (strpos($dir,$backupdir)!==0) {
185 //Insert them into backup_files
186 $status = execute_sql("INSERT INTO {$CFG->prefix}backup_files
187 (backup_code, file_type, path)
188 VALUES
189 ('$backup_unique_code','course','$dir')",false);
191 //Do some output
192 backup_flush(30);
196 //Now execute the select
197 $ids = get_records_sql("SELECT DISTINCT b.path, b.old_id
198 FROM {$CFG->prefix}backup_files b
199 WHERE backup_code = '$backup_unique_code' AND
200 file_type = 'course'");
201 //Gets the user data
202 $info[0][0] = get_string("files");
203 if ($ids) {
204 $info[0][1] = count($ids);
205 } else {
206 $info[0][1] = 0;
209 return $info;
212 //Function to check and create the needed moddata dir to
213 //save all the mod backup files. We always name it moddata
214 //to be able to restore it, but in restore we check for
215 //$CFG->moddata !!
216 function check_and_create_moddata_dir($backup_unique_code) {
218 global $CFG;
220 $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/moddata",true);
222 return $status;
225 //Function to check and create the "user_files" dir to
226 //save all the user files we need from "users" dir
227 function check_and_create_user_files_dir($backup_unique_code) {
229 global $CFG;
231 $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/user_files",true);
233 return $status;
236 //Function to check and create the "course_files" dir to
237 //save all the course files we need from "CFG->datadir/course" dir
238 function check_and_create_course_files_dir($backup_unique_code) {
240 global $CFG;
242 $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/course_files",true);
244 return $status;
247 //Function to create, open and write header of the xml file
248 function backup_open_xml($backup_unique_code) {
250 global $CFG;
252 $status = true;
254 //Open for writing
256 $file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
257 $backup_file = fopen($file,"w");
258 //Writes the header
259 $status = fwrite ($backup_file,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
260 if ($status) {
261 $status = fwrite ($backup_file,start_tag("MOODLE_BACKUP",0,true));
263 if ($status) {
264 return $backup_file;
265 } else {
266 return false;
270 //Close the file
271 function backup_close_xml($backup_file) {
272 $status = fwrite ($backup_file,end_tag("MOODLE_BACKUP",0,true));
273 return fclose($backup_file);
276 //Return the xml start tag
277 function start_tag($tag,$level=0,$endline=false) {
278 if ($endline) {
279 $endchar = "\n";
280 } else {
281 $endchar = "";
283 return str_repeat(" ",$level*2)."<".strtoupper($tag).">".$endchar;
286 //Return the xml end tag
287 function end_tag($tag,$level=0,$endline=true) {
288 if ($endline) {
289 $endchar = "\n";
290 } else {
291 $endchar = "";
293 return str_repeat(" ",$level*2)."</".strtoupper($tag).">".$endchar;
296 //Return the start tag, the contents and the end tag
297 function full_tag($tag,$level=0,$endline=true,$content,$to_utf=true) {
298 $st = start_tag($tag,$level,$endline);
299 $co="";
300 if ($to_utf) {
301 $co = preg_replace("/\r\n|\r/", "\n", utf8_encode(htmlspecialchars($content)));
302 } else {
303 $co = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
305 $et = end_tag($tag,0,true);
306 return $st.$co.$et;
309 //Prints General info about the course
310 //name, moodle_version (internal and release), backup_version, date, info in file...
311 function backup_general_info ($bf,$preferences) {
313 global $CFG;
315 fwrite ($bf,start_tag("INFO",1,true));
317 //The name of the backup
318 fwrite ($bf,full_tag("NAME",2,false,$preferences->backup_name));
319 //The moodle_version
320 fwrite ($bf,full_tag("MOODLE_VERSION",2,false,$preferences->moodle_version));
321 fwrite ($bf,full_tag("MOODLE_RELEASE",2,false,$preferences->moodle_release));
322 //The backup_version
323 fwrite ($bf,full_tag("BACKUP_VERSION",2,false,$preferences->backup_version));
324 fwrite ($bf,full_tag("BACKUP_RELEASE",2,false,$preferences->backup_release));
325 //The date
326 fwrite ($bf,full_tag("DATE",2,false,$preferences->backup_unique_code));
327 //Te includes tag
328 fwrite ($bf,start_tag("DETAILS",2,true));
329 //Now, go to mod element of preferences to print its status
330 foreach ($preferences->mods as $element) {
331 //Calculate info
332 $included = "false";
333 $userinfo = "false";
334 if ($element->backup) {
335 $included = "true";
336 if ($element->userinfo) {
337 $userinfo = "true";
340 //Prints the mod start
341 fwrite ($bf,start_tag("MOD",3,true));
342 fwrite ($bf,full_tag("NAME",4,false,$element->name));
343 fwrite ($bf,full_tag("INCLUDED",4,false,$included));
344 fwrite ($bf,full_tag("USERINFO",4,false,$userinfo));
346 //Print the end
347 fwrite ($bf,end_tag("MOD",3,true));
349 //The user in backup
350 if ($preferences->backup_users == 1) {
351 fwrite ($bf,full_tag("USERS",3,false,"course"));
352 } else {
353 fwrite ($bf,full_tag("USERS",3,false,"all"));
355 //The logs in backup
356 if ($preferences->backup_logs == 1) {
357 fwrite ($bf,full_tag("LOGS",3,false,"true"));
358 } else {
359 fwrite ($bf,full_tag("LOGS",3,false,"false"));
361 //The user files
362 if ($preferences->backup_user_files == 1) {
363 fwrite ($bf,full_tag("USERFILES",3,false,"true"));
364 } else {
365 fwrite ($bf,full_tag("USERFILES",3,false,"false"));
367 //The course files
368 if ($preferences->backup_course_files == 1) {
369 fwrite ($bf,full_tag("COURSEFILES",3,false,"true"));
370 } else {
371 fwrite ($bf,full_tag("COURSEFILES",3,false,"false"));
374 fwrite ($bf,end_tag("DETAILS",2,true));
377 $status = fwrite ($bf,end_tag("INFO",1,true));
379 return $status;
382 //Prints course's general info (table course)
383 function backup_course_start ($bf,$preferences) {
385 global $CFG;
387 $status = true;
389 //Course open tag
390 fwrite ($bf,start_tag("COURSE",1,true));
391 //Header open tag
392 fwrite ($bf,start_tag("HEADER",2,true));
394 //Get info from course
395 $course=false;
396 if ($courses = get_records("course","id",$preferences->backup_course)) {
397 $course = $courses[$preferences->backup_course];
399 if ($course) {
400 //Prints course info
401 fwrite ($bf,full_tag("ID",3,false,$course->id));
402 //Obtain the category
403 $category = false;
404 if ($categories = get_records("course_categories","id","$course->category")) {
405 $category = $categories[$course->category];
407 if ($category) {
408 //Prints category info
409 fwrite ($bf,start_tag("CATEGORY",3,true));
410 fwrite ($bf,full_tag("ID",4,false,$course->category));
411 fwrite ($bf,full_tag("NAME",4,false,$category->name));
412 fwrite ($bf,end_tag("CATEGORY",3,true));
414 //Continues with the course
415 fwrite ($bf,full_tag("PASSWORD",3,false,$course->password));
416 fwrite ($bf,full_tag("FULLNAME",3,false,$course->fullname));
417 fwrite ($bf,full_tag("SHORTNAME",3,false,$course->shortname));
418 fwrite ($bf,full_tag("SUMMARY",3,false,$course->summary));
419 fwrite ($bf,full_tag("FORMAT",3,false,$course->format));
420 fwrite ($bf,full_tag("NEWSITEMS",3,false,$course->newsitems));
421 fwrite ($bf,full_tag("TEACHER",3,false,$course->teacher));
422 fwrite ($bf,full_tag("TEACHERS",3,false,$course->teachers));
423 fwrite ($bf,full_tag("STUDENT",3,false,$course->student));
424 fwrite ($bf,full_tag("STUDENTS",3,false,$course->students));
425 fwrite ($bf,full_tag("GUEST",3,false,$course->guest));
426 fwrite ($bf,full_tag("STARDATE",3,false,$course->stardate));
427 fwrite ($bf,full_tag("NUMSECTIONS",3,false,$course->numsections));
428 fwrite ($bf,full_tag("SHOWRECENT",3,false,$course->showrecent));
429 fwrite ($bf,full_tag("MARKER",3,false,$course->marker));
430 fwrite ($bf,full_tag("VISIBLE",3,false,$course->visible));
431 fwrite ($bf,full_tag("TIMECREATED",3,false,$course->timecreated));
432 $status = fwrite ($bf,full_tag("TIMEMODIFIED",3,false,$course->timemodified));
433 //Print header end
434 fwrite ($bf,end_tag("HEADER",2,true));
435 } else {
436 $status = false;
439 return $status;
442 //Prints course's end tag
443 function backup_course_end ($bf,$preferences) {
445 //Course end tag
446 $status = fwrite ($bf,end_tag("COURSE",1,true));
448 return $status;
452 //Prints course's sections info (table course_sections)
453 function backup_course_sections ($bf,$preferences) {
455 global $CFG;
457 $status = true;
460 //Get info from sections
461 $section=false;
462 if ($sections = get_records("course_sections","course",$preferences->backup_course,"section")) {
463 //Section open tag
464 fwrite ($bf,start_tag("SECTIONS",2,true));
465 //Iterate over every section (ordered by section)
466 foreach ($sections as $section) {
467 //Begin Section
468 fwrite ($bf,start_tag("SECTION",3,true));
469 fwrite ($bf,full_tag("ID",4,false,$section->id));
470 fwrite ($bf,full_tag("NUMBER",4,false,$section->section));
471 fwrite ($bf,full_tag("SUMMARY",4,false,$section->summary));
472 fwrite ($bf,full_tag("VISIBLE",4,false,$section->visible));
473 //Now print the mods in section
474 backup_course_modules ($bf,$preferences,$section);
475 //End section
476 fwrite ($bf,end_tag("SECTION",3,true));
478 //Section close tag
479 $status = fwrite ($bf,end_tag("SECTIONS",2,true));
482 return $status;
486 //Prints course's modules info (table course_modules)
487 //Only for selected mods in preferences
488 function backup_course_modules ($bf,$preferences,$section) {
490 global $CFG;
492 $status = true;
494 $first_record = true;
496 //Now print the mods in section
497 //Extracts mod id from sequence
498 $tok = strtok($section->sequence,",");
499 while ($tok) {
500 //Get module's type
501 $moduletype = get_module_type ($preferences->backup_course,$tok);
502 //Check if we've selected to backup that type
503 if ($moduletype and $preferences->mods[$moduletype]->backup) {
504 $selected = true;
505 } else {
506 $selected = false;
509 if ($selected) {
510 //Gets course_module data from db
511 $course_module = get_records ("course_modules","id",$tok);
512 //If it's the first, pring MODS tag
513 if ($first_record) {
514 fwrite ($bf,start_tag("MODS",4,true));
515 $first_record = false;
517 //Print mod info from course_modules
518 fwrite ($bf,start_tag("MOD",5,true));
519 //Save neccesary info to backup_ids
520 fwrite ($bf,full_tag("ID",6,false,$tok));
521 fwrite ($bf,full_tag("TYPE",6,false,$moduletype));
522 fwrite ($bf,full_tag("INSTANCE",6,false,$course_module[$tok]->instance));
523 fwrite ($bf,full_tag("ADDED",6,false,$course_module[$tok]->added));
524 fwrite ($bf,full_tag("DELETED",6,false,$course_module[$tok]->deleted));
525 fwrite ($bf,full_tag("SCORE",6,false,$course_module[$tok]->score));
526 fwrite ($bf,full_tag("VISIBLE",6,false,$course_module[$tok]->visible));
527 fwrite ($bf,end_tag("MOD",5,true));
529 //check for next
530 $tok = strtok(",");
533 //Si ha habido modulos, final de MODS
534 if (!$first_record) {
535 $status =fwrite ($bf,end_tag("MODS",4,true));
538 return $status;
541 //Print users to xml
542 //Only users previously calculated in backup_ids will output
544 function backup_user_info ($bf,$preferences) {
546 global $CFG;
548 $status = true;
550 $users = get_records_sql("SELECT u.old_id, u.table_name,u.info
551 FROM {$CFG->prefix}backup_ids u
552 WHERE u.backup_code = '$preferences->backup_unique_code' AND
553 u.table_name = 'user'");
555 //If we have users to backup
556 if ($users) {
557 //Begin Users tag
558 fwrite ($bf,start_tag("USERS",2,true));
559 $counter = 0;
560 //With every user
561 foreach ($users as $user) {
562 //Get user data from table
563 $user_data = get_record("user","id",$user->old_id);
564 //Begin User tag
565 fwrite ($bf,start_tag("USER",3,true));
566 //Output all user data
567 fwrite ($bf,full_tag("ID",4,false,$user_data->id));
568 fwrite ($bf,full_tag("CONFIRMED",4,false,$user_data->confirmed));
569 fwrite ($bf,full_tag("DELETED",4,false,$user_data->deleted));
570 fwrite ($bf,full_tag("USERNAME",4,false,$user_data->username));
571 fwrite ($bf,full_tag("PASSWORD",4,false,$user_data->password));
572 fwrite ($bf,full_tag("IDNUMBER",4,false,$user_data->idnumber));
573 fwrite ($bf,full_tag("FIRSTNAME",4,false,$user_data->firstname));
574 fwrite ($bf,full_tag("LASTNAME",4,false,$user_data->lastname));
575 fwrite ($bf,full_tag("EMAIL",4,false,$user_data->email));
576 fwrite ($bf,full_tag("ICQ",4,false,$user_data->icq));
577 fwrite ($bf,full_tag("PHONE1",4,false,$user_data->phone1));
578 fwrite ($bf,full_tag("PHONE2",4,false,$user_data->phone2));
579 fwrite ($bf,full_tag("INSTITUTION",4,false,$user_data->institution));
580 fwrite ($bf,full_tag("DEPARTMENT",4,false,$user_data->department));
581 fwrite ($bf,full_tag("ADDRESS",4,false,$user_data->address));
582 fwrite ($bf,full_tag("CITY",4,false,$user_data->city));
583 fwrite ($bf,full_tag("COUNTRY",4,false,$user_data->country));
584 fwrite ($bf,full_tag("LANG",4,false,$user_data->lang));
585 fwrite ($bf,full_tag("TIMEZONE",4,false,$user_data->timezone));
586 fwrite ($bf,full_tag("FIRSTACCESS",4,false,$user_data->firstaccess));
587 fwrite ($bf,full_tag("LASTACCESS",4,false,$user_data->lastaccess));
588 fwrite ($bf,full_tag("LASTLOGIN",4,false,$user_data->lastlogin));
589 fwrite ($bf,full_tag("CURRENTLOGIN",4,false,$user_data->currentlogin));
590 fwrite ($bf,full_tag("LASTIP",4,false,$user_data->lastIP));
591 fwrite ($bf,full_tag("SECRET",4,false,$user_data->secret));
592 fwrite ($bf,full_tag("PICTURE",4,false,$user_data->picture));
593 fwrite ($bf,full_tag("URL",4,false,$user_data->url));
594 fwrite ($bf,full_tag("DESCRIPTION",4,false,$user_data->description));
595 fwrite ($bf,full_tag("MAILFORMAT",4,false,$user_data->mailformat));
596 fwrite ($bf,full_tag("MAILDISPLAY",4,false,$user_data->maildisplay));
597 fwrite ($bf,full_tag("HTMLEDITOR",4,false,$user_data->htmleditor));
598 fwrite ($bf,full_tag("AUTOSUBSCRIBE",4,false,$user_data->autosubscribe));
599 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$user_data->timemodified));
601 //Output every user role (with its associated info)
602 $user->isadmin = strpos($user->info,"admin");
603 $user->iscoursecreator = strpos($user->info,"coursecreator");
604 $user->isteacher = strpos($user->info,"teacher");
605 $user->isstudent = strpos($user->info,"student");
606 if ($user->isadmin!==false or
607 $user->iscoursecreator!==false or
608 $user->isteacher!==false or
609 $user->isstudent!==false) {
610 //Begin ROLES tag
611 fwrite ($bf,start_tag("ROLES",4,true));
612 //PRINT ROLE INFO
613 //Admins
614 if ($user->isadmin!==false) {
615 //Print ROLE start
616 fwrite ($bf,start_tag("ROLE",5,true));
617 //Print Role info
618 fwrite ($bf,full_tag("TYPE",6,false,"admin"));
619 //Print ROLE end
620 fwrite ($bf,end_tag("ROLE",5,true));
622 //CourseCreator
623 if ($user->iscoursecreator!==false) {
624 //Print ROLE start
625 fwrite ($bf,start_tag("ROLE",5,true));
626 //Print Role info
627 fwrite ($bf,full_tag("TYPE",6,false,"coursecreator"));
628 //Print ROLE end
629 fwrite ($bf,end_tag("ROLE",5,true));
631 //Teacher
632 if ($user->isteacher!==false) {
633 //Print ROLE start
634 fwrite ($bf,start_tag("ROLE",5,true));
635 //Print Role info
636 fwrite ($bf,full_tag("TYPE",6,false,"teacher"));
637 //Get specific info for teachers
638 $tea = get_record("user_teachers","userid",$user->old_id,"course",$preferences->backup_course);
639 fwrite ($bf,full_tag("AUTHORITY",6,false,$tea->authority));
640 fwrite ($bf,full_tag("TEA_ROLE",6,false,$tea->role));
641 //Print ROLE end
642 fwrite ($bf,end_tag("ROLE",5,true));
644 //Student
645 if ($user->isstudent!==false) {
646 //Print ROLE start
647 fwrite ($bf,start_tag("ROLE",5,true));
648 //Print Role info
649 fwrite ($bf,full_tag("TYPE",6,false,"student"));
650 //Get specific info for students
651 $stu = get_record("user_students","userid",$user->old_id,"course",$preferences->backup_course);
652 fwrite ($bf,full_tag("TIMESTART",6,false,$stu->timestart));
653 fwrite ($bf,full_tag("TIMEEND",6,false,$stu->timeend));
654 fwrite ($bf,full_tag("TIME",6,false,$stu->time));
655 //Print ROLE end
656 fwrite ($bf,end_tag("ROLE",5,true));
660 //End ROLES tag
661 fwrite ($bf,end_tag("ROLES",4,true));
663 //End User tag
664 fwrite ($bf,end_tag("USER",3,true));
665 //Do some output
666 $counter++;
667 if ($counter % 10 == 0) {
668 echo ".";
669 if ($counter % 200 == 0) {
670 echo "<br>";
672 backup_flush(300);
675 //End Users tag
676 fwrite ($bf,end_tag("USERS",2,true));
677 } else {
678 //There isn't users. Impossible !!
679 $status = false;
682 return $status;
685 //Backup log info (time ordered)
686 function backup_log_info($bf,$preferences) {
688 global $CFG;
690 $status = true;
692 $logs = get_records ("log","course",$preferences->backup_course,"time");
694 //We have logs
695 if ($logs) {
696 //Pring logs header
697 fwrite ($bf,start_tag("LOGS",2,true));
698 $counter = 0;
699 //Iterate
700 foreach ($logs as $log) {
701 //See if it is a valid module to backup
702 if ($log->module == "course" or
703 $log->module == "user" or
704 $preferences->mods[$log->module]->backup == 1) {
705 //Begin log tag
706 fwrite ($bf,start_tag("LOG",3,true));
708 //Output log tag
709 fwrite ($bf,full_tag("ID",4,false,$log->id));
710 fwrite ($bf,full_tag("TIME",4,false,$log->time));
711 fwrite ($bf,full_tag("USERID",4,false,$log->userid));
712 fwrite ($bf,full_tag("IP",4,false,$log->ip));
713 fwrite ($bf,full_tag("MODULE",4,false,$log->module));
714 fwrite ($bf,full_tag("ACTION",4,false,$log->action));
715 fwrite ($bf,full_tag("URL",4,false,$log->url));
716 fwrite ($bf,full_tag("INFO",4,false,$log->info));
718 //End log tag
719 fwrite ($bf,end_tag("LOG",3,true));
721 //Do some output
722 $counter++;
723 if ($counter % 10 == 0) {
724 echo ".";
725 if ($counter % 200 == 0) {
726 echo "<br>";
728 backup_flush(300);
731 //End logs tag
732 $status = fwrite ($bf,end_tag("LOGS",2,true));
734 return $status;
737 //Start the modules tag
738 function backup_modules_start ($bf,$preferences) {
740 return fwrite ($bf,start_tag("MODULES",2,true));
743 //End the modules tag
744 function backup_modules_end ($bf,$preferences) {
746 return fwrite ($bf,end_tag("MODULES",2,true));
749 //This function makes all the necesary calls to every mod
750 //to export itself and its files !!!
751 function backup_module($bf,$preferences,$module) {
753 global $CFG;
755 $status = true;
757 //First, re-check if necessary functions exists
758 $modbackup = $module."_backup_mods";
759 if (function_exists($modbackup)) {
760 //Call the function
761 $status = $modbackup($bf,$preferences);
762 } else {
763 //Something was wrong. Function should exist.
764 $status = false;
767 return $status;
771 //This function copies all the needed files under the "users" directory to the "user_files"
772 //directory under temp/backup
773 function backup_copy_user_files ($preferences) {
775 global $CFG;
777 $status = true;
779 //First we check to "user_files" exists and create it as necessary
780 //in temp/backup/$backup_code dir
781 $status = check_and_create_user_files_dir($preferences->backup_unique_code);
783 //Now iterate over directories under "users" to check if that user must be
784 //copied to backup
786 $rootdir = $CFG->dataroot."/users";
787 //Check if directory exists
788 if (is_dir($rootdir)) {
789 $list = list_directories ($rootdir);
790 if ($list) {
791 //Iterate
792 foreach ($list as $dir) {
793 //Look for dir like username in backup_ids
794 $data = get_record ("backup_ids","backup_code",$preferences->backup_unique_code,
795 "table_name","user",
796 "old_id",$dir);
797 //If exists, copy it
798 if ($data) {
799 $status = backup_copy_file($rootdir."/".$dir,
800 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/user_files/".$dir);
805 return $status;
808 //This function copies all the course files under the course directory (except the moddata
809 //directory to the "course_files" directory under temp/backup
810 function backup_copy_course_files ($preferences) {
812 global $CFG;
814 $status = true;
816 //First we check to "course_files" exists and create it as necessary
817 //in temp/backup/$backup_code dir
818 $status = check_and_create_course_files_dir($preferences->backup_unique_code);
820 //Now iterate over files and directories except $CFG->moddata and get_string("backupdir") to be
821 //copied to backup
823 $rootdir = $CFG->dataroot."/".$preferences->backup_course;
825 $name_moddata = $CFG->moddata;
826 $name_backupdata = get_string("backupdir");
827 //Check if directory exists
828 if (is_dir($rootdir)) {
829 $list = list_directories_and_files ($rootdir);
830 if ($list) {
831 //Iterate
832 foreach ($list as $dir) {
833 if ($dir !== $name_moddata and $dir !== $name_backupdata) {
834 $status = backup_copy_file($rootdir."/".$dir,
835 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/course_files/".$dir);
840 return $status;
843 //This function creates the zip file containing all the backup info
844 //moodle.xml, moddata, user_files, course_files.
845 //The zipped file is created in the backup directory and named with
846 //the "oficial" name of the backup
847 //It uses "pclzip" if available or system "zip" (unix only)
848 function backup_zip ($preferences,$moodle_home) {
850 global $CFG;
852 $status = true;
854 //Base dir where everything happens
855 $basedir = $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code;
856 //Backup zip file name
857 $name = $preferences->backup_name;
858 //List base_dir files and directories
859 $filelist = list_directories_and_files ($basedir);
861 if (empty($CFG->zip)) { // Use built-in php-based zip function
862 $files = array();
863 foreach ($filelist as $file) {
864 //If directory, append "/"
865 if (is_dir($basedir."/".$file)) {
866 $file = $file."/";
868 //Include into array
869 $files[] = $basedir."/".$file;
871 include_once($moodle_home."/lib/pclzip/pclzip.lib.php");
872 $archive = new PclZip("$basedir/$name");
873 if (($list = $archive->create($files,PCLZIP_OPT_REMOVE_PATH,$basedir)) == 0) {
874 error($archive->errorInfo(true));
875 $status = false;
877 } else { // Use external zip program
878 $files = "";
879 foreach ($filelist as $file) {
880 $files .= basename($file);
881 $files .= " ";
883 $command = "cd $basedir ; $CFG->zip -r $name $files";
884 $status = Exec($command);
887 return $status;
891 //This function copies the final zip to the course dir
892 function copy_zip_to_course_dir ($preferences) {
894 global $CFG;
896 //Define zip location (from)
897 $from_zip_file = $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/".$preferences->backup_name;
899 //Define zip destination (course dir)
900 $to_zip_file = $CFG->dataroot."/".$preferences->backup_course;
902 //echo "<p>From: ".$from_zip_file."<br>"; //Debug
904 //echo "<p>Checking: ".$to_zip_file."<br>"; //Debug
906 //Checks course dir exists
907 $status = check_dir_exists($to_zip_file,true);
909 //Define zip destination (backup dir)
910 $to_zip_file = $to_zip_file."/".get_string("backupdir");
912 //echo "<p>Checking: ".$to_zip_file."<br>"; //Debug
914 //Checks backup dir exists
915 $status = check_dir_exists($to_zip_file,true);
917 //Define zip destination (zip file)
918 $to_zip_file = $to_zip_file."/".$preferences->backup_name;
920 //echo "<p>To: ".$to_zip_file."<br>"; //Debug
922 //Copy zip file
923 if ($status) {
924 $status = backup_copy_file ($from_zip_file,$to_zip_file);
927 return $status;