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
6 //Calculate the number of users to backup and put their ids in backup_ids
7 //Return an array of info (name,value)
8 function user_check_backup($course,$backup_unique_code,$backup_users,$backup_messages) {
9 //$backup_users=0-->all
10 // 1-->course (needed + enrolled)
16 $context = get_context_instance(CONTEXT_COURSE
, $course);
19 //If we've selected none, simply return 0
20 if ($backup_users == 0 or $backup_users == 1) {
22 //Calculate needed users (calling every xxxx_get_participants function + scales users)
23 $needed_users = backup_get_needed_users($course, $backup_messages);
25 //Calculate enrolled users (students + teachers)
26 $enrolled_users = backup_get_enrolled_users($course);
28 //Calculate all users (every record in users table)
29 $all_users = backup_get_all_users();
31 //Calculate course users (needed + enrolled)
33 $course_users = $needed_users;
36 if ($enrolled_users) {
37 foreach ($enrolled_users as $enrolled_user) {
38 $course_users[$enrolled_user->id
]->id
= $enrolled_user->id
;
42 //Now, depending of parameters, create $backupable_users
43 if ($backup_users == 0) {
44 $backupable_users = $all_users;
46 $backupable_users = $course_users;
49 //If we have backupable users
50 if ($backupable_users) {
51 //Iterate over users putting their roles
52 foreach ($backupable_users as $backupable_user) {
53 $backupable_user->info
= "";
55 //Is needed user (exists in needed_users)
56 if (isset($needed_users[$backupable_user->id
])) {
57 $backupable_user->info
.= "needed";
58 } else if (isset($course_users[$backupable_user->id
])) {
59 $backupable_user->info
.= "needed";
60 } // Yu: also needed because they can view course
61 // might need another variable
63 //Now create the backup_id record
64 $backupids_rec->backup_code
= $backup_unique_code;
65 $backupids_rec->table_name
= "user";
66 $backupids_rec->old_id
= $backupable_user->id
;
67 $backupids_rec->info
= $backupable_user->info
;
69 //Insert the record id. backup_users decide it.
71 $status = insert_record('backup_ids', $backupids_rec, false);
81 $info[0][0] = get_string("users");
82 $info[0][1] = $count_users;
87 //Returns every needed user (participant) in a course
88 //It uses the xxxx_get_participants() function
89 //plus users needed to backup scales.
90 //WARNING: It returns only NEEDED users, not every
91 // every student and teacher in the course, so it
92 //must be merged with backup_get_enrrolled_users !!
94 function backup_get_needed_users ($courseid, $includemessages=false) {
100 $course_modules = get_records_sql ("SELECT cm.id, m.name, cm.instance
101 FROM {$CFG->prefix}modules m,
102 {$CFG->prefix}course_modules cm
103 WHERE m.id = cm.module and
104 cm.course = '$courseid'");
106 if ($course_modules) {
107 //Iterate over each module
108 foreach ($course_modules as $course_module) {
109 $modlib = "$CFG->dirroot/mod/$course_module->name/lib.php";
110 $modgetparticipants = $course_module->name
."_get_participants";
111 if (file_exists($modlib)) {
112 include_once($modlib);
113 if (function_exists($modgetparticipants)) {
114 $module_participants = $modgetparticipants($course_module->instance
);
116 if ($module_participants) {
117 foreach ($module_participants as $module_participant) {
118 $result[$module_participant->id
]->id
= $module_participant->id
;
126 //Now, add scale users (from site and course scales)
128 $scaleusers = get_records_sql("SELECT DISTINCT userid,userid
129 FROM {$CFG->prefix}scale
130 WHERE courseid = '0' or courseid = '$courseid'");
131 //Add scale users to results
133 foreach ($scaleusers as $scaleuser) {
135 if ($scaleuser->userid
!= 0) {
136 $result[$scaleuser->userid
]->id
= $scaleuser->userid
;
141 //Now, add message users if necessary
142 if ($includemessages) {
143 include_once("$CFG->dirroot/message/lib.php");
145 $messageusers = message_get_participants();
146 //Add message users to results
148 foreach ($messageusers as $messageuser) {
150 if ($messageuser->id
!=0) {
151 $result[$messageuser->id
]->id
= $messageuser->id
;
161 //Returns every enrolled user (student and teacher) in a course
163 function backup_get_enrolled_users ($courseid) {
167 // get all users with moodle/course:view capability, this will include people
168 // assigned at cat level, or site level
169 // but it should be ok if they have no direct assignment at course, mod, block level
170 return get_users_by_capability(get_context_instance(CONTEXT_COURSE
, $courseid), 'moodle/course:view');
173 //Returns all users ids (every record in users table)
174 function backup_get_all_users() {
176 return get_records('user', '', '', '', 'id, id');
179 //Calculate the number of log entries to backup
180 //Return an array of info (name,value)
181 function log_check_backup($course) {
185 //Now execute the count
186 $ids = count_records("log","course",$course);
189 $info[0][0] = get_string("logs");
199 //Calculate the number of user files to backup
200 //Under $CFG->dataroot/users
201 //and put them (their path) in backup_ids
202 //Return an array of info (name,value)
203 function user_files_check_backup($course,$backup_unique_code) {
207 $rootdir = $CFG->dataroot
."/users";
208 //Check if directory exists
209 if (is_dir($rootdir)) {
210 //Get directories without descend
211 $userdirs = get_directory_list($rootdir,"",false,true,false);
212 foreach ($userdirs as $dir) {
213 //Extracts user id from file path
214 $tok = strtok($dir,"/");
218 //We were getting $dir='0', so continue (WAS: $tok = "";)
221 //Look it is a backupable user
222 $data = get_record ("backup_ids","backup_code","$backup_unique_code",
226 //Insert them into backup_files
227 $status = execute_sql("INSERT INTO {$CFG->prefix}backup_files
228 (backup_code, file_type, path, old_id)
230 ('$backup_unique_code','user','".addslashes($dir)."','$userid')",false);
237 //Now execute the select
238 $ids = get_records_sql("SELECT DISTINCT b.path, b.old_id
239 FROM {$CFG->prefix}backup_files b
240 WHERE backup_code = '$backup_unique_code' AND
241 file_type = 'user'");
243 $info[0][0] = get_string("files");
245 $info[0][1] = count($ids);
253 //Calculate the number of course files to backup
254 //under $CFG->dataroot/$course, except $CFG->moddata, and backupdata
255 //and put them (their path) in backup_ids
256 //Return an array of info (name,value)
257 function course_files_check_backup($course,$backup_unique_code) {
261 $rootdir = $CFG->dataroot
."/$course";
262 //Check if directory exists
263 if (is_dir($rootdir)) {
264 //Get files and directories without descend
265 $coursedirs = get_directory_list($rootdir,$CFG->moddata
,false,true,true);
266 $backupdata_dir = "backupdata";
267 foreach ($coursedirs as $dir) {
268 //Check it isn't backupdata_dir
269 if (strpos($dir,$backupdata_dir)!==0) {
270 //Insert them into backup_files
271 $status = execute_sql("INSERT INTO {$CFG->prefix}backup_files
272 (backup_code, file_type, path)
274 ('$backup_unique_code','course','".addslashes($dir)."')",false);
281 //Now execute the select
282 $ids = get_records_sql("SELECT DISTINCT b.path, b.old_id
283 FROM {$CFG->prefix}backup_files b
284 WHERE backup_code = '$backup_unique_code' AND
285 file_type = 'course'");
287 $info[0][0] = get_string("files");
289 $info[0][1] = count($ids);
297 //Function to check and create the needed moddata dir to
298 //save all the mod backup files. We always name it moddata
299 //to be able to restore it, but in restore we check for
301 function check_and_create_moddata_dir($backup_unique_code) {
305 $status = check_dir_exists($CFG->dataroot
."/temp/backup/".$backup_unique_code."/moddata",true);
310 //Function to check and create the "user_files" dir to
311 //save all the user files we need from "users" dir
312 function check_and_create_user_files_dir($backup_unique_code) {
316 $status = check_dir_exists($CFG->dataroot
."/temp/backup/".$backup_unique_code."/user_files",true);
321 //Function to check and create the "group_files" dir to
322 //save all the user files we need from "groups" dir
323 function check_and_create_group_files_dir($backup_unique_code) {
327 $status = check_dir_exists($CFG->dataroot
."/temp/backup/".$backup_unique_code."/group_files",true);
332 //Function to check and create the "course_files" dir to
333 //save all the course files we need from "CFG->datadir/course" dir
334 function check_and_create_course_files_dir($backup_unique_code) {
338 $status = check_dir_exists($CFG->dataroot
."/temp/backup/".$backup_unique_code."/course_files",true);
343 //Function to create, open and write header of the xml file
344 function backup_open_xml($backup_unique_code) {
352 $file = $CFG->dataroot
."/temp/backup/".$backup_unique_code."/moodle.xml";
353 $backup_file = fopen($file,"w");
355 $status = fwrite ($backup_file,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
357 $status = fwrite ($backup_file,start_tag("MOODLE_BACKUP",0,true));
367 function backup_close_xml($backup_file) {
368 $status = fwrite ($backup_file,end_tag("MOODLE_BACKUP",0,true));
369 return fclose($backup_file);
372 //Return the xml start tag
373 function start_tag($tag,$level=0,$endline=false,$attributes=null) {
380 if (!empty($attributes) && is_array($attributes)) {
381 foreach ($attributes as $key => $value) {
382 $attrstring .= " ".xml_tag_safe_content($key)."=\"".
383 xml_tag_safe_content($value)."\"";
386 return str_repeat(" ",$level*2)."<".strtoupper($tag).$attrstring.">".$endchar;
389 //Return the xml end tag
390 function end_tag($tag,$level=0,$endline=true) {
396 return str_repeat(" ",$level*2)."</".strtoupper($tag).">".$endchar;
399 //Return the start tag, the contents and the end tag
400 function full_tag($tag,$level=0,$endline=true,$content,$attributes=null) {
404 //Here we encode absolute links
405 $content = backup_encode_absolute_links($content);
407 $st = start_tag($tag,$level,$endline,$attributes);
409 $co = xml_tag_safe_content($content);
411 $et = end_tag($tag,0,true);
417 function xml_tag_safe_content($content) {
419 //If enabled, we strip all the control chars (\x0-\x1f) from the text but tabs (\x9),
420 //newlines (\xa) and returns (\xd). The delete control char (\x7f) is also included.
421 //because they are forbiden in XML 1.0 specs. The expression below seems to be
422 //UTF-8 safe too because it simply ignores the rest of characters.
423 $content = preg_replace("/[\x-\x8\xb-\xc\xe-\x1f\x7f]/is","",$content);
424 $content = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
428 //Prints General info about the course
429 //name, moodle_version (internal and release), backup_version, date, info in file...
430 function backup_general_info ($bf,$preferences) {
434 fwrite ($bf,start_tag("INFO",1,true));
436 //The name of the backup
437 fwrite ($bf,full_tag("NAME",2,false,$preferences->backup_name
));
439 fwrite ($bf,full_tag("MOODLE_VERSION",2,false,$preferences->moodle_version
));
440 fwrite ($bf,full_tag("MOODLE_RELEASE",2,false,$preferences->moodle_release
));
442 fwrite ($bf,full_tag("BACKUP_VERSION",2,false,$preferences->backup_version
));
443 fwrite ($bf,full_tag("BACKUP_RELEASE",2,false,$preferences->backup_release
));
445 fwrite ($bf,full_tag("DATE",2,false,$preferences->backup_unique_code
));
446 //The original site wwwroot
447 fwrite ($bf,full_tag("ORIGINAL_WWWROOT",2,false,$CFG->wwwroot
));
448 //The zip method used
449 if (!empty($CFG->zip
)) {
450 $zipmethod = 'external';
452 $zipmethod = 'internal';
454 //Indicate if it includes external MNET users
455 $sql = "SELECT b.old_id
456 FROM {$CFG->prefix}backup_ids b
457 JOIN {$CFG->prefix}user u ON b.old_id=u.id
458 WHERE b.backup_code = '$preferences->backup_unique_code'
459 AND b.table_name = 'user' AND u.mnethostid != '{$CFG->mnet_localhost_id}'";
460 if (record_exists_sql($sql)) {
461 fwrite ($bf,full_tag("MNET_REMOTEUSERS",2,false,'true'));
463 fwrite ($bf,full_tag("ZIP_METHOD",2,false,$zipmethod));
465 fwrite ($bf,start_tag("DETAILS",2,true));
466 //Now, go to mod element of preferences to print its status
467 foreach ($preferences->mods
as $element) {
471 if ($element->backup
) {
473 if ($element->userinfo
) {
477 //Prints the mod start
478 fwrite ($bf,start_tag("MOD",3,true));
479 fwrite ($bf,full_tag("NAME",4,false,$element->name
));
480 fwrite ($bf,full_tag("INCLUDED",4,false,$included));
481 fwrite ($bf,full_tag("USERINFO",4,false,$userinfo));
483 if (isset($preferences->mods
[$element->name
]->instances
)
484 && is_array($preferences->mods
[$element->name
]->instances
)
485 && count($preferences->mods
[$element->name
]->instances
)) {
486 fwrite ($bf, start_tag("INSTANCES",4,true));
487 foreach ($preferences->mods
[$element->name
]->instances
as $id => $object) {
488 if (!empty($object->backup
)) {
492 if ($object->backup
) {
494 if ($object->userinfo
) {
498 fwrite ($bf, start_tag("INSTANCE",5,true));
499 fwrite ($bf, full_tag("ID",5,false,$id));
500 fwrite ($bf, full_tag("NAME",5,false,$object->name
));
501 fwrite ($bf, full_tag("INCLUDED",5,false,$included)) ;
502 fwrite ($bf, full_tag("USERINFO",5,false,$userinfo));
503 fwrite ($bf, end_tag("INSTANCE",5,true));
506 fwrite ($bf, end_tag("INSTANCES",4,true));
511 fwrite ($bf,end_tag("MOD",3,true));
513 //The metacourse in backup
514 if ($preferences->backup_metacourse
== 1) {
515 fwrite ($bf,full_tag("METACOURSE",3,false,"true"));
517 fwrite ($bf,full_tag("METACOURSE",3,false,"false"));
520 if ($preferences->backup_users
== 1) {
521 fwrite ($bf,full_tag("USERS",3,false,"course"));
522 } else if ($preferences->backup_users
== 0) {
523 fwrite ($bf,full_tag("USERS",3,false,"all"));
525 fwrite ($bf,full_tag("USERS",3,false,"none"));
528 if ($preferences->backup_logs
== 1) {
529 fwrite ($bf,full_tag("LOGS",3,false,"true"));
531 fwrite ($bf,full_tag("LOGS",3,false,"false"));
534 if ($preferences->backup_user_files
== 1) {
535 fwrite ($bf,full_tag("USERFILES",3,false,"true"));
537 fwrite ($bf,full_tag("USERFILES",3,false,"false"));
540 if ($preferences->backup_course_files
== 1) {
541 fwrite ($bf,full_tag("COURSEFILES",3,false,"true"));
543 fwrite ($bf,full_tag("COURSEFILES",3,false,"false"));
545 //The messages in backup
546 if ($preferences->backup_messages
== 1 && $preferences->backup_course
== SITEID
) {
547 fwrite ($bf,full_tag("MESSAGES",3,false,"true"));
549 fwrite ($bf,full_tag("MESSAGES",3,false,"false"));
551 //The mode of writing the block data
552 fwrite ($bf,full_tag('BLOCKFORMAT',3,false,'instances'));
553 fwrite ($bf,end_tag("DETAILS",2,true));
555 $status = fwrite ($bf,end_tag("INFO",1,true));
557 ///Roles stuff goes in here
559 fwrite ($bf, start_tag('ROLES', 1, true));
560 $roles = backup_fetch_roles($preferences);
562 $sitecontext = get_context_instance(CONTEXT_SYSTEM
, SITEID
);
564 foreach ($roles as $role) {
565 fwrite ($bf,start_tag('ROLE',2,true));
566 fwrite ($bf,full_tag('ID', 3, false, $role->id
));
567 fwrite ($bf,full_tag('NAME',3,false,$role->name
));
568 fwrite ($bf,full_tag('SHORTNAME',3,false,$role->shortname
));
569 // find and write all default capabilities
570 fwrite ($bf,start_tag('CAPABILITIES',3,true));
571 // pull out all default (site context) capabilities
572 if ($capabilities = role_context_capabilities($role->id
, $sitecontext)) {
573 foreach ($capabilities as $capability=>$value) {
574 fwrite ($bf,start_tag('CAPABILITY',4,true));
575 fwrite ($bf,full_tag('NAME', 5, false, $capability));
576 fwrite ($bf,full_tag('PERMISSION', 5, false, $value));
577 // use this to pull out the other info (timemodified and modifierid)
578 $cap = get_record_sql("SELECT *
579 FROM {$CFG->prefix}role_capabilities
580 WHERE capability = '$capability'
581 AND contextid = $sitecontext->id
582 AND roleid = $role->id");
583 fwrite ($bf, full_tag("TIMEMODIFIED", 5, false, $cap->timemodified
));
584 fwrite ($bf, full_tag("MODIFIERID", 5, false, $cap->modifierid
));
585 fwrite ($bf,end_tag('CAPABILITY',4,true));
588 fwrite ($bf,end_tag('CAPABILITIES',3,true));
589 fwrite ($bf,end_tag('ROLE',2,true));
591 fwrite ($bf,end_tag('ROLES', 1, true));
595 //Prints course's general info (table course)
596 function backup_course_start ($bf,$preferences) {
603 fwrite ($bf,start_tag("COURSE",1,true));
605 fwrite ($bf,start_tag("HEADER",2,true));
607 //Get info from course
608 $course = get_record("course","id",$preferences->backup_course
);
609 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
612 fwrite ($bf,full_tag("ID",3,false,$course->id
));
613 //Obtain the category
614 $category = get_record("course_categories","id","$course->category");
616 //Prints category info
617 fwrite ($bf,start_tag("CATEGORY",3,true));
618 fwrite ($bf,full_tag("ID",4,false,$course->category
));
619 fwrite ($bf,full_tag("NAME",4,false,$category->name
));
620 fwrite ($bf,end_tag("CATEGORY",3,true));
622 //Continues with the course
623 fwrite ($bf,full_tag("PASSWORD",3,false,$course->password
));
624 fwrite ($bf,full_tag("FULLNAME",3,false,$course->fullname
));
625 fwrite ($bf,full_tag("SHORTNAME",3,false,$course->shortname
));
626 fwrite ($bf,full_tag("IDNUMBER",3,false,$course->idnumber
));
627 fwrite ($bf,full_tag("SUMMARY",3,false,$course->summary
));
628 fwrite ($bf,full_tag("FORMAT",3,false,$course->format
));
629 fwrite ($bf,full_tag("SHOWGRADES",3,false,$course->showgrades
));
630 fwrite ($bf,full_tag("NEWSITEMS",3,false,$course->newsitems
));
631 fwrite ($bf,full_tag("TEACHER",3,false,$course->teacher
));
632 fwrite ($bf,full_tag("TEACHERS",3,false,$course->teachers
));
633 fwrite ($bf,full_tag("STUDENT",3,false,$course->student
));
634 fwrite ($bf,full_tag("STUDENTS",3,false,$course->students
));
635 fwrite ($bf,full_tag("GUEST",3,false,$course->guest
));
636 fwrite ($bf,full_tag("STARTDATE",3,false,$course->startdate
));
637 fwrite ($bf,full_tag("NUMSECTIONS",3,false,$course->numsections
));
638 //fwrite ($bf,full_tag("SHOWRECENT",3,false,$course->showrecent)); INFO: This is out in 1.3
639 fwrite ($bf,full_tag("MAXBYTES",3,false,$course->maxbytes
));
640 fwrite ($bf,full_tag("SHOWREPORTS",3,false,$course->showreports
));
641 fwrite ($bf,full_tag("GROUPMODE",3,false,$course->groupmode
));
642 fwrite ($bf,full_tag("GROUPMODEFORCE",3,false,$course->groupmodeforce
));
643 fwrite ($bf,full_tag("LANG",3,false,$course->lang
));
644 fwrite ($bf,full_tag("THEME",3,false,$course->theme
));
645 fwrite ($bf,full_tag("COST",3,false,$course->cost
));
646 fwrite ($bf,full_tag("CURRENCY",3,false,$course->currency
));
647 fwrite ($bf,full_tag("MARKER",3,false,$course->marker
));
648 fwrite ($bf,full_tag("VISIBLE",3,false,$course->visible
));
649 fwrite ($bf,full_tag("HIDDENSECTIONS",3,false,$course->hiddensections
));
650 fwrite ($bf,full_tag("TIMECREATED",3,false,$course->timecreated
));
651 fwrite ($bf,full_tag("TIMEMODIFIED",3,false,$course->timemodified
));
652 //If not selected, force metacourse to 0
653 if (!$preferences->backup_metacourse
) {
654 $status = fwrite ($bf,full_tag("METACOURSE",3,false,'0'));
655 //else, export the field as is in DB
657 $status = fwrite ($bf,full_tag("METACOURSE",3,false,$course->metacourse
));
659 fwrite ($bf,full_tag("EXPIRENOTIFY",3,false,$course->expirynotify
));
660 fwrite ($bf,full_tag("NOTIFYSTUDENTS",3,false,$course->notifystudents
));
661 fwrite ($bf,full_tag("EXPIRYTHRESHOLD",3,false,$course->expirythreshold
));
662 fwrite ($bf,full_tag("ENROLLABLE",3,false,$course->enrollable
));
663 fwrite ($bf,full_tag("ENROLSTARTDATE",3,false,$course->enrolstartdate
));
664 fwrite ($bf,full_tag("ENROLENDDATE",3,false,$course->enrolenddate
));
665 fwrite ($bf,full_tag("ENROLPERIOD",3,false,$course->enrolperiod
));
667 /// write local course overrides here?
668 write_role_overrides_xml($bf, $context, 3);
669 /// write role_assign code here
670 write_role_assignments_xml($bf, $context, 3);
672 fwrite ($bf,end_tag("HEADER",2,true));
680 //Prints course's end tag
681 function backup_course_end ($bf,$preferences) {
684 $status = fwrite ($bf,end_tag("COURSE",1,true));
690 //Prints course's metacourse info (table course_meta)
691 function backup_course_metacourse ($bf,$preferences) {
698 $parents = get_records_sql ("SELECT m.*, c.idnumber, c.shortname
699 FROM {$CFG->prefix}course_meta m,
700 {$CFG->prefix}course c
701 WHERE m.child_course = '$preferences->backup_course' AND
702 m.parent_course = c.id");
703 $childs = get_records_sql ("SELECT m.*, c.idnumber, c.shortname
704 FROM {$CFG->prefix}course_meta m,
705 {$CFG->prefix}course c
706 WHERE m.parent_course = '$preferences->backup_course' AND
707 m.child_course = c.id");
709 if ($parents ||
$childs) {
710 //metacourse open tag
711 fwrite ($bf,start_tag("METACOURSE",2,true));
713 fwrite($bf, start_tag("PARENTS",3,true));
714 //Iterate over every parent
715 foreach ($parents as $parent) {
717 fwrite ($bf,start_tag("PARENT",4,true));
718 fwrite ($bf,full_tag("ID",5,false,$parent->parent_course
));
719 fwrite ($bf,full_tag("IDNUMBER",5,false,$parent->idnumber
));
720 fwrite ($bf,full_tag("SHORTNAME",5,false,$parent->shortname
));
722 fwrite ($bf,end_tag("PARENT",4,true));
724 fwrite ($bf,end_tag("PARENTS",3,true));
727 fwrite($bf, start_tag("CHILDS",3,true));
728 //Iterate over every child
729 foreach ($childs as $child) {
731 fwrite ($bf,start_tag("CHILD",4,true));
732 fwrite ($bf,full_tag("ID",5,false,$child->child_course
));
733 fwrite ($bf,full_tag("IDNUMBER",5,false,$child->idnumber
));
734 fwrite ($bf,full_tag("SHORTNAME",5,false,$child->shortname
));
736 fwrite ($bf,end_tag("CHILD",4,true));
738 fwrite ($bf,end_tag("CHILDS",3,true));
740 //metacourse close tag
741 $status = fwrite ($bf,end_tag("METACOURSE",3,true));
748 //Prints course's messages info (tables message, message_read and message_contacts)
749 function backup_messages ($bf,$preferences) {
755 //Get info from messages
756 $unreads = get_records ('message');
757 $reads = get_records ('message_read');
758 $contacts= get_records ('message_contacts');
760 if ($unreads ||
$reads ||
$contacts) {
763 fwrite ($bf,start_tag("MESSAGES",2,true));
765 //Iterate over every unread
766 foreach ($unreads as $unread) {
768 fwrite($bf, start_tag("MESSAGE",3,true));
769 fwrite ($bf,full_tag("ID",4,false,$unread->id
));
770 fwrite ($bf,full_tag("STATUS",4,false,"UNREAD"));
771 fwrite ($bf,full_tag("USERIDFROM",4,false,$unread->useridfrom
));
772 fwrite ($bf,full_tag("USERIDTO",4,false,$unread->useridto
));
773 fwrite ($bf,full_tag("MESSAGE",4,false,$unread->message
));
774 fwrite ($bf,full_tag("FORMAT",4,false,$unread->format
));
775 fwrite ($bf,full_tag("TIMECREATED",4,false,$unread->timecreated
));
776 fwrite ($bf,full_tag("MESSAGETYPE",4,false,$unread->messagetype
));
778 fwrite ($bf,end_tag("MESSAGE",3,true));
782 if ($counter %
20 == 0) {
784 if ($counter %
400 == 0) {
793 //Iterate over every read
794 foreach ($reads as $read) {
796 fwrite($bf, start_tag("MESSAGE",3,true));
797 fwrite ($bf,full_tag("ID",4,false,$read->id
));
798 fwrite ($bf,full_tag("STATUS",4,false,"READ"));
799 fwrite ($bf,full_tag("USERIDFROM",4,false,$read->useridfrom
));
800 fwrite ($bf,full_tag("USERIDTO",4,false,$read->useridto
));
801 fwrite ($bf,full_tag("MESSAGE",4,false,$read->message
));
802 fwrite ($bf,full_tag("FORMAT",4,false,$read->format
));
803 fwrite ($bf,full_tag("TIMECREATED",4,false,$read->timecreated
));
804 fwrite ($bf,full_tag("MESSAGETYPE",4,false,$read->messagetype
));
805 fwrite ($bf,full_tag("TIMEREAD",4,false,$read->timeread
));
806 fwrite ($bf,full_tag("MAILED",4,false,$read->mailed
));
808 fwrite ($bf,end_tag("MESSAGE",3,true));
812 if ($counter %
20 == 0) {
814 if ($counter %
400 == 0) {
823 fwrite($bf, start_tag("CONTACTS",3,true));
824 //Iterate over every contact
825 foreach ($contacts as $contact) {
827 fwrite($bf, start_tag("CONTACT",4,true));
828 fwrite ($bf,full_tag("ID",5,false,$contact->id
));
829 fwrite ($bf,full_tag("USERID",5,false,$contact->userid
));
830 fwrite ($bf,full_tag("CONTACTID",5,false,$contact->contactid
));
831 fwrite ($bf,full_tag("BLOCKED",5,false,$contact->blocked
));
833 fwrite ($bf,end_tag("CONTACT",4,true));
837 if ($counter %
20 == 0) {
839 if ($counter %
400 == 0) {
845 fwrite($bf, end_tag("CONTACTS",3,true));
848 $status = fwrite ($bf,end_tag("MESSAGES",2,true));
855 //Prints course's blocks info (table block_instance)
856 function backup_course_blocks ($bf,$preferences) {
862 // Read all of the block table
863 $blocks = blocks_get_record();
866 $pages[] = page_create_object(PAGE_COURSE_VIEW
, $preferences->backup_course
);
868 // Let's see if we have to backup blocks from modules
869 $modulerecords = get_records_sql('SELECT name, id FROM '.$CFG->prefix
.'modules');
871 foreach($preferences->mods
as $module) {
872 if(!$module->backup
) {
876 $cmods = get_records_select('course_modules', 'course = '.$preferences->backup_course
.' AND module = '.$modulerecords[$module->name
]->id
);
881 $pagetypes = page_import_types('mod/'.$module->name
.'/');
882 if(empty($pagetypes)) {
886 foreach($pagetypes as $pagetype) {
887 foreach($cmods as $cmod) {
888 $pages[] = page_create_object($pagetype, $cmod->instance
);
894 fwrite ($bf,start_tag('BLOCKS',2,true));
896 while($page = array_pop($pages)) {
897 if ($instances = blocks_get_by_page($page)) {
898 //Iterate over every block
899 foreach ($instances as $position) {
900 foreach ($position as $instance) {
902 //If we somehow have a block with an invalid id, skip it
903 if(empty($blocks[$instance->blockid
]->name
)) {
908 fwrite ($bf,start_tag('BLOCK',3,true));
909 fwrite ($bf,full_tag('ID', 4, false,$instance->id
));
910 fwrite ($bf,full_tag('NAME',4,false,$blocks[$instance->blockid
]->name
));
911 fwrite ($bf,full_tag('PAGEID',4,false,$instance->pageid
));
912 fwrite ($bf,full_tag('PAGETYPE',4,false,$instance->pagetype
));
913 fwrite ($bf,full_tag('POSITION',4,false,$instance->position
));
914 fwrite ($bf,full_tag('WEIGHT',4,false,$instance->weight
));
915 fwrite ($bf,full_tag('VISIBLE',4,false,$instance->visible
));
916 fwrite ($bf,full_tag('CONFIGDATA',4,false,$instance->configdata
));
918 $context = get_context_instance(CONTEXT_BLOCK
, $instance->id
);
919 write_role_overrides_xml($bf, $context, 4);
920 /// write role_assign code here
921 write_role_assignments_xml($bf, $context, 4);
923 fwrite ($bf,end_tag('BLOCK',3,true));
930 $status = fwrite ($bf,end_tag('BLOCKS',2,true));
936 //Prints course's sections info (table course_sections)
937 function backup_course_sections ($bf,$preferences) {
944 //Get info from sections
946 if ($sections = get_records("course_sections","course",$preferences->backup_course
,"section")) {
948 fwrite ($bf,start_tag("SECTIONS",2,true));
949 //Iterate over every section (ordered by section)
950 foreach ($sections as $section) {
952 fwrite ($bf,start_tag("SECTION",3,true));
953 fwrite ($bf,full_tag("ID",4,false,$section->id
));
954 fwrite ($bf,full_tag("NUMBER",4,false,$section->section
));
955 fwrite ($bf,full_tag("SUMMARY",4,false,$section->summary
));
956 fwrite ($bf,full_tag("VISIBLE",4,false,$section->visible
));
957 //Now print the mods in section
958 backup_course_modules ($bf,$preferences,$section);
960 fwrite ($bf,end_tag("SECTION",3,true));
963 $status = fwrite ($bf,end_tag("SECTIONS",2,true));
970 //Prints course's format data (any data the format might want to save).
971 function backup_format_data ($bf,$preferences) {
974 // Check course format
975 if(!($format=get_field('course','format','id',$preferences->backup_course
))) {
978 // Write appropriate tag. Note that we always put this tag there even if
979 // blank, it makes parsing easier
980 fwrite ($bf,start_tag("FORMATDATA",2,true));
982 $file=$CFG->dirroot
."/course/format/$format/backuplib.php";
983 if(file_exists($file)) {
984 // If the file is there, the function must be or it's an error.
986 $function=$format.'_backup_format_data';
987 if(!function_exists($function)) {
990 if(!$function($bf,$preferences)) {
995 // This last return just checks the file writing has been ok (ish)
996 return fwrite ($bf,end_tag("FORMATDATA",2,true));
999 //Prints course's modules info (table course_modules)
1000 //Only for selected mods in preferences
1001 function backup_course_modules ($bf,$preferences,$section) {
1007 $first_record = true;
1009 //Now print the mods in section
1010 //Extracts mod id from sequence
1011 $tok = strtok($section->sequence
,",");
1014 $moduletype = get_module_type ($preferences->backup_course
,$tok);
1015 //Check if we've selected to backup that type
1016 if ($moduletype and $preferences->mods
[$moduletype]->backup
) {
1023 $context = get_context_instance(CONTEXT_MODULE
, $tok);
1024 //Gets course_module data from db
1025 $course_module = get_records ("course_modules","id",$tok);
1026 //If it's the first, pring MODS tag
1027 if ($first_record) {
1028 fwrite ($bf,start_tag("MODS",4,true));
1029 $first_record = false;
1031 // if we're doing selected instances, check that too.
1032 if (is_array($preferences->mods
[$moduletype]->instances
)
1033 && count($preferences->mods
[$moduletype]->instances
)
1034 && (!array_key_exists($course_module[$tok]->instance
,$preferences->mods
[$moduletype]->instances
)
1035 ||
empty($preferences->mods
[$moduletype]->instances
[$course_module[$tok]->instance
]->backup
))) {
1040 // find all role values that has an override in this context
1041 $roles = get_records('role_capabilities', 'contextid', $context->id
);
1043 //Print mod info from course_modules
1044 fwrite ($bf,start_tag("MOD",5,true));
1045 //Save neccesary info to backup_ids
1046 fwrite ($bf,full_tag("ID",6,false,$tok));
1047 fwrite ($bf,full_tag("TYPE",6,false,$moduletype));
1048 fwrite ($bf,full_tag("INSTANCE",6,false,$course_module[$tok]->instance
));
1049 fwrite ($bf,full_tag("ADDED",6,false,$course_module[$tok]->added
));
1050 fwrite ($bf,full_tag("SCORE",6,false,$course_module[$tok]->score
));
1051 fwrite ($bf,full_tag("INDENT",6,false,$course_module[$tok]->indent
));
1052 fwrite ($bf,full_tag("VISIBLE",6,false,$course_module[$tok]->visible
));
1053 fwrite ($bf,full_tag("GROUPMODE",6,false,$course_module[$tok]->groupmode
));
1054 // get all the role_capabilities overrides in this mod
1055 write_role_overrides_xml($bf, $context, 6);
1056 /// write role_assign code here
1057 write_role_assignments_xml($bf, $context, 6);
1058 /// write role_assign code here
1060 fwrite ($bf,end_tag("MOD",5,true));
1066 //Si ha habido modulos, final de MODS
1067 if (!$first_record) {
1068 $status =fwrite ($bf,end_tag("MODS",4,true));
1074 //Print users to xml
1075 //Only users previously calculated in backup_ids will output
1077 function backup_user_info ($bf,$preferences) {
1083 // Use a recordset to for the memory handling on to
1084 // the DB and run faster
1085 $users = get_recordset_sql("SELECT b.old_id, b.table_name, b.info,
1087 FROM {$CFG->prefix}backup_ids b
1088 JOIN {$CFG->prefix}user u ON b.old_id=u.id
1089 JOIN {$CFG->prefix}mnet_host m ON u.mnethostid=m.id
1090 WHERE b.backup_code = '$preferences->backup_unique_code' AND
1091 b.table_name = 'user'");
1093 //If we have users to backup
1094 if ($users && $users->RecordCount()) {
1096 fwrite ($bf,start_tag("USERS",2,true));
1099 while ($user = $users->FetchNextObj()) {
1101 fwrite ($bf,start_tag("USER",3,true));
1102 //Output all user data
1103 fwrite ($bf,full_tag("ID",4,false,$user->id
));
1104 fwrite ($bf,full_tag("AUTH",4,false,$user->auth
));
1105 fwrite ($bf,full_tag("CONFIRMED",4,false,$user->confirmed
));
1106 fwrite ($bf,full_tag("POLICYAGREED",4,false,$user->policyagreed
));
1107 fwrite ($bf,full_tag("DELETED",4,false,$user->deleted
));
1108 fwrite ($bf,full_tag("USERNAME",4,false,$user->username
));
1109 fwrite ($bf,full_tag("PASSWORD",4,false,$user->password
));
1110 fwrite ($bf,full_tag("IDNUMBER",4,false,$user->idnumber
));
1111 fwrite ($bf,full_tag("FIRSTNAME",4,false,$user->firstname
));
1112 fwrite ($bf,full_tag("LASTNAME",4,false,$user->lastname
));
1113 fwrite ($bf,full_tag("EMAIL",4,false,$user->email
));
1114 fwrite ($bf,full_tag("EMAILSTOP",4,false,$user->emailstop
));
1115 fwrite ($bf,full_tag("ICQ",4,false,$user->icq
));
1116 fwrite ($bf,full_tag("SKYPE",4,false,$user->skype
));
1117 fwrite ($bf,full_tag("YAHOO",4,false,$user->yahoo
));
1118 fwrite ($bf,full_tag("AIM",4,false,$user->aim
));
1119 fwrite ($bf,full_tag("MSN",4,false,$user->msn
));
1120 fwrite ($bf,full_tag("PHONE1",4,false,$user->phone1
));
1121 fwrite ($bf,full_tag("PHONE2",4,false,$user->phone2
));
1122 fwrite ($bf,full_tag("INSTITUTION",4,false,$user->institution
));
1123 fwrite ($bf,full_tag("DEPARTMENT",4,false,$user->department
));
1124 fwrite ($bf,full_tag("ADDRESS",4,false,$user->address
));
1125 fwrite ($bf,full_tag("CITY",4,false,$user->city
));
1126 fwrite ($bf,full_tag("COUNTRY",4,false,$user->country
));
1127 fwrite ($bf,full_tag("LANG",4,false,$user->lang
));
1128 fwrite ($bf,full_tag("THEME",4,false,$user->theme
));
1129 fwrite ($bf,full_tag("TIMEZONE",4,false,$user->timezone
));
1130 fwrite ($bf,full_tag("FIRSTACCESS",4,false,$user->firstaccess
));
1131 fwrite ($bf,full_tag("LASTACCESS",4,false,$user->lastaccess
));
1132 fwrite ($bf,full_tag("LASTLOGIN",4,false,$user->lastlogin
));
1133 fwrite ($bf,full_tag("CURRENTLOGIN",4,false,$user->currentlogin
));
1134 fwrite ($bf,full_tag("LASTIP",4,false,$user->lastip
));
1135 fwrite ($bf,full_tag("SECRET",4,false,$user->secret
));
1136 fwrite ($bf,full_tag("PICTURE",4,false,$user->picture
));
1137 fwrite ($bf,full_tag("URL",4,false,$user->url
));
1138 fwrite ($bf,full_tag("DESCRIPTION",4,false,$user->description
));
1139 fwrite ($bf,full_tag("MAILFORMAT",4,false,$user->mailformat
));
1140 fwrite ($bf,full_tag("MAILDIGEST",4,false,$user->maildigest
));
1141 fwrite ($bf,full_tag("MAILDISPLAY",4,false,$user->maildisplay
));
1142 fwrite ($bf,full_tag("HTMLEDITOR",4,false,$user->htmleditor
));
1143 fwrite ($bf,full_tag("AJAX",4,false,$user->ajax
));
1144 fwrite ($bf,full_tag("AUTOSUBSCRIBE",4,false,$user->autosubscribe
));
1145 fwrite ($bf,full_tag("TRACKFORUMS",4,false,$user->trackforums
));
1146 if ($user->mnethostid
!= $CFG->mnet_localhost_id
) {
1147 fwrite ($bf,full_tag("MNETHOSTURL",4,false,$user->wwwroot
));
1149 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$user->timemodified
));
1151 /// write assign/override code for context_userid
1153 $user->isneeded
= strpos($user->info
,"needed");
1154 //Output every user role (with its associated info)
1156 $user->isadmin = strpos($user->info,"admin");
1157 $user->iscoursecreator = strpos($user->info,"coursecreator");
1158 $user->isteacher = strpos($user->info,"teacher");
1159 $user->isstudent = strpos($user->info,"student");
1162 if ($user->isadmin!==false or
1163 $user->iscoursecreator!==false or
1164 $user->isteacher!==false or
1165 $user->isstudent!==false or
1166 $user->isneeded!==false) {
1168 fwrite ($bf,start_tag("ROLES",4,true));
1169 if ($user->info
!= "needed" && $user->info
!="") {
1174 $roles = explode(",", $user->info
);
1175 foreach ($roles as $role) {
1176 if ($role!="" && $role!="needed") {
1177 fwrite ($bf,start_tag("ROLE",5,true));
1179 fwrite ($bf,full_tag("TYPE",6,false,$role));
1181 fwrite ($bf,end_tag("ROLE",5,true));
1186 if ($user->isadmin!==false) {
1188 fwrite ($bf,start_tag("ROLE",5,true));
1190 fwrite ($bf,full_tag("TYPE",6,false,"admin"));
1192 fwrite ($bf,end_tag("ROLE",5,true));
1195 if ($user->iscoursecreator!==false) {
1197 fwrite ($bf,start_tag("ROLE",5,true));
1199 fwrite ($bf,full_tag("TYPE",6,false,"coursecreator"));
1201 fwrite ($bf,end_tag("ROLE",5,true));
1204 if ($user->isteacher!==false) {
1206 fwrite ($bf,start_tag("ROLE",5,true));
1208 fwrite ($bf,full_tag("TYPE",6,false,"teacher"));
1209 //Get specific info for teachers
1210 $tea = get_record("user_teachers","userid",$user->old_id,"course",$preferences->backup_course);
1211 fwrite ($bf,full_tag("AUTHORITY",6,false,$tea->authority));
1212 fwrite ($bf,full_tag("TEA_ROLE",6,false,$tea->role));
1213 fwrite ($bf,full_tag("EDITALL",6,false,$tea->editall));
1214 fwrite ($bf,full_tag("TIMESTART",6,false,$tea->timestart));
1215 fwrite ($bf,full_tag("TIMEEND",6,false,$tea->timeend));
1216 fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$tea->timemodified));
1217 fwrite ($bf,full_tag("TIMEACCESS",6,false,$tea->timeaccess));
1218 fwrite ($bf,full_tag("ENROL",6,false,$tea->enrol));
1220 fwrite ($bf,end_tag("ROLE",5,true));
1223 if ($user->isstudent!==false) {
1225 fwrite ($bf,start_tag("ROLE",5,true));
1227 fwrite ($bf,full_tag("TYPE",6,false,"student"));
1228 //Get specific info for students
1229 $stu = get_record("user_students","userid",$user->old_id,"course",$preferences->backup_course);
1230 fwrite ($bf,full_tag("TIMESTART",6,false,$stu->timestart));
1231 fwrite ($bf,full_tag("TIMEEND",6,false,$stu->timeend));
1232 fwrite ($bf,full_tag("TIME",6,false,$stu->time));
1233 fwrite ($bf,full_tag("TIMEACCESS",6,false,$stu->timeaccess));
1234 fwrite ($bf,full_tag("ENROL",6,false,$stu->enrol));
1236 fwrite ($bf,end_tag("ROLE",5,true));
1242 if ($user->isneeded
!==false) {
1244 fwrite ($bf,start_tag("ROLE",5,true));
1246 fwrite ($bf,full_tag("TYPE",6,false,"needed"));
1248 fwrite ($bf,end_tag("ROLE",5,true));
1252 fwrite ($bf,end_tag("ROLES",4,true));
1254 //Check if we have user_preferences to backup
1255 if ($preferences_data = get_records("user_preferences","userid",$user->old_id
)) {
1256 //Start USER_PREFERENCES tag
1257 fwrite ($bf,start_tag("USER_PREFERENCES",4,true));
1258 //Write each user_preference
1259 foreach ($preferences_data as $user_preference) {
1260 fwrite ($bf,start_tag("USER_PREFERENCE",5,true));
1261 fwrite ($bf,full_tag("NAME",6,false,$user_preference->name
));
1262 fwrite ($bf,full_tag("VALUE",6,false,$user_preference->value
));
1263 fwrite ($bf,end_tag("USER_PREFERENCE",5,true));
1265 //End USER_PREFERENCES tag
1266 fwrite ($bf,end_tag("USER_PREFERENCES",4,true));
1269 $context = get_context_instance(CONTEXT_USER
, $user->old_id
);
1271 write_role_overrides_xml($bf, $context, 4);
1272 /// write role_assign code here
1273 write_role_assignments_xml($bf, $context, 4);
1275 fwrite ($bf,end_tag("USER",3,true));
1278 if ($counter %
10 == 0) {
1280 if ($counter %
200 == 0) {
1287 fwrite ($bf,end_tag("USERS",2,true));
1289 // There aren't any users.
1296 //Backup log info (time ordered)
1297 function backup_log_info($bf,$preferences) {
1301 //Number of records to get in every chunk
1302 $recordset_size = 1000;
1306 //Counter, points to current record
1310 $count_logs = count_records("log","course",$preferences->backup_course
);
1313 if ($count_logs > 0 ) {
1314 fwrite ($bf,start_tag("LOGS",2,true));
1316 while ($counter < $count_logs) {
1317 //Get a chunk of records
1318 $logs = get_records ("log","course",$preferences->backup_course
,"time","*",$counter,$recordset_size);
1323 foreach ($logs as $log) {
1324 //See if it is a valid module to backup
1325 if ($log->module
== "course" or
1326 $log->module
== "user" or
1327 (array_key_exists($log->module
, $preferences->mods
) and $preferences->mods
[$log->module
]->backup
== 1)) {
1328 // logs with 'upload' in module field are ignored, there is no restore code anyway
1330 fwrite ($bf,start_tag("LOG",3,true));
1333 fwrite ($bf,full_tag("ID",4,false,$log->id
));
1334 fwrite ($bf,full_tag("TIME",4,false,$log->time
));
1335 fwrite ($bf,full_tag("USERID",4,false,$log->userid
));
1336 fwrite ($bf,full_tag("IP",4,false,$log->ip
));
1337 fwrite ($bf,full_tag("MODULE",4,false,$log->module
));
1338 fwrite ($bf,full_tag("CMID",4,false,$log->cmid
));
1339 fwrite ($bf,full_tag("ACTION",4,false,$log->action
));
1340 fwrite ($bf,full_tag("URL",4,false,$log->url
));
1341 fwrite ($bf,full_tag("INFO",4,false,$log->info
));
1344 fwrite ($bf,end_tag("LOG",3,true));
1348 if ($counter %
20 == 0) {
1350 if ($counter %
400 == 0) {
1359 if ($count_logs > 0 ) {
1360 $status = fwrite ($bf,end_tag("LOGS",2,true));
1365 //Backup gradebook info
1366 function backup_gradebook_info($bf,$preferences) {
1372 // see if ALL grade items of type mod of this course are being backed up
1373 // if not, we do not need to backup grade category and associated grade items/grades
1376 if ($grade_items = get_records_sql("SELECT * FROM {$CFG->prefix}grade_items
1377 WHERE courseid = $preferences->backup_course
1378 ORDER BY sortorder ASC")) {
1379 foreach ($grade_items as $grade_item) {
1381 // do not restore if this grade_item is a mod, and
1382 if ($grade_item->itemtype
== 'mod') {
1384 // get module information
1385 // if no user data selected, we skip this grade_item
1386 if (!backup_userdata_selected($preferences,$grade_item->itemmodule
,$grade_item->iteminstance
)) {
1387 //print_object($grade_item);
1396 fwrite ($bf,start_tag("GRADEBOOK",2,true));
1398 // Now backup grade_item (inside grade_category)
1400 $status = backup_gradebook_category_info($bf,$preferences);
1402 $status = backup_gradebook_item_info($bf,$preferences, $backupall);
1403 $status = backup_gradebook_outcomes_info($bf, $preferences);
1404 // back up grade outcomes
1407 $status = fwrite ($bf,end_tag("GRADEBOOK",2,true));
1412 function backup_gradebook_category_info($bf,$preferences) {
1417 //Output grade_category
1419 // getting grade categories, but make sure parents come before children
1420 // because when we do restore, we need to recover the parents first
1421 // we do this by getting the lowest depth first
1422 $grade_categories = get_records_sql("SELECT * FROM {$CFG->prefix}grade_categories
1423 WHERE courseid = $preferences->backup_course
1424 ORDER BY depth ASC");
1425 if ($grade_categories) {
1426 //Begin grade_categories tag
1427 fwrite ($bf,start_tag("GRADE_CATEGORIES",3,true));
1428 //Iterate for each category
1429 foreach ($grade_categories as $grade_category) {
1430 //Begin grade_category
1431 fwrite ($bf,start_tag("GRADE_CATEGORY",4,true));
1432 //Output individual fields
1433 fwrite ($bf,full_tag("ID",5,false,$grade_category->id
));
1435 // not keeping path and depth because they can be derived
1436 fwrite ($bf,full_tag("PARENT",5,false,$grade_category->parent
));
1437 fwrite ($bf,full_tag("FULLNAME",5,false,$grade_category->fullname
));
1438 fwrite ($bf,full_tag("AGGREGATION",5,false,$grade_category->aggregation
));
1439 fwrite ($bf,full_tag("KEEPHIGH",5,false,$grade_category->keephigh
));
1440 fwrite ($bf,full_tag("DROPLOW",5,false,$grade_category->droplow
));
1441 fwrite ($bf,full_tag("HIDDEN",5,false,$grade_category->hidden
));
1443 //End grade_category
1444 fwrite ($bf,end_tag("GRADE_CATEGORY",4,true));
1446 //End grade_categories tag
1447 $status = fwrite ($bf,end_tag("GRADE_CATEGORIES",3,true));
1455 //Backup gradebook_item (called from backup_gradebook_info
1456 function backup_gradebook_item_info($bf,$preferences, $backupall) {
1462 // get all the grade_items, ordered by sort order since upon restoring, it is not always
1463 // possible to use the same sort order. We could at least preserve the sortorder by restoring
1464 // grade_items in the original sortorder
1465 if ($grade_items = get_records_sql("SELECT * FROM {$CFG->prefix}grade_items
1466 WHERE courseid = $preferences->backup_course
1467 ORDER BY sortorder ASC")) {
1469 //Begin grade_items tag
1470 fwrite ($bf,start_tag("GRADE_ITEMS",3,true));
1471 //Iterate for each item
1472 foreach ($grade_items as $grade_item) {
1474 // do not restore if this grade_item is a mod, and
1475 if ($grade_item->itemtype
== 'mod') {
1477 // if no user data selected, we skip this grade_item
1478 if (!backup_userdata_selected($preferences,$grade_item->itemmodule
,$grade_item->iteminstance
)) {
1481 } else if ($grade_item->itemtype
== 'category') {
1482 // if not all grade items are being backed up
1483 // we ignore this type of grade_item and grades associated
1490 fwrite ($bf,start_tag("GRADE_ITEM",4,true));
1491 //Output individual fields
1493 fwrite ($bf,full_tag("ID",5,false,$grade_item->id
));
1494 fwrite ($bf,full_tag("ITEMNAME",5,false,$grade_item->itemname
));
1495 fwrite ($bf,full_tag("ITEMTYPE",5,false,$grade_item->itemtype
));
1496 fwrite ($bf,full_tag("ITEMMODULE",5,false,$grade_item->itemmodule
));
1497 fwrite ($bf,full_tag("ITEMINSTANCE",5,false,$grade_item->iteminstance
));
1498 fwrite ($bf,full_tag("ITEMNUMBER",5,false,$grade_item->itemnumber
));
1499 fwrite ($bf,full_tag("ITEMINFO",5,false,$grade_item->iteminfo
));
1500 fwrite ($bf,full_tag("IDNUMBER",5,false,$grade_item->idnumber
));
1501 // use [idnumber] in calculation instead of [#giXXX#]
1502 fwrite ($bf,full_tag("CALCULATION",5,false,$grade_item->get_caclulation()));
1503 fwrite ($bf,full_tag("GRADEMAX",5,false,$grade_item->grademax
));
1504 fwrite ($bf,full_tag("GRADEMIN",5,false,$grade_item->grademin
));
1505 fwrite ($bf,full_tag("SCALEID",5,false,$grade_item->scaleid
));
1506 fwrite ($bf,full_tag("OUTCOMEID",5,false,$grade_item->outcomeid
));
1507 fwrite ($bf,full_tag("GRADEPASS",5,false,$grade_item->gradepass
));
1508 fwrite ($bf,full_tag("MULTFACTOR",5,false,$grade_item->multfactor
));
1509 fwrite ($bf,full_tag("PLUSFACTOR",5,false,$grade_item->plusfactor
));
1510 fwrite ($bf,full_tag("HIDDEN",5,false,$grade_item->hidden
));
1511 fwrite ($bf,full_tag("LOCKED",5,false,$grade_item->locked
));
1512 fwrite ($bf,full_tag("LOCKTIME",5,false,$grade_item->locktime
));
1514 // back up the other stuff here
1515 $status = backup_gradebook_grades_info($bf,$preferences,$grade_item->id
);
1516 $status = backup_gradebook_grades_text_info($bf,$preferences,$grade_item->id
);
1519 fwrite ($bf,end_tag("GRADE_ITEM",4,true));
1521 //End grade_items tag
1522 $status = fwrite ($bf,end_tag("GRADE_ITEMS",3,true));
1527 //Backup gradebook_item (called from backup_gradebook_info
1529 function backup_gradebook_outcomes_info($bf,$preferences) {
1535 // get all the grade_items
1536 // might need to do it using sortorder
1537 if ($grade_outcomes = get_records('grade_outcomes', 'courseid', $preferences->backup_course
)) {
1539 //Begin grade_items tag
1540 fwrite ($bf,start_tag("GRADE_OUTCOMES",3,true));
1541 //Iterate for each item
1542 foreach ($grade_outcomes as $grade_outcome) {
1544 fwrite ($bf,start_tag("GRADE_OUTCOME",4,true));
1545 //Output individual fields
1547 fwrite ($bf,full_tag("ID",5,false,$grade_outcome->id
));
1548 fwrite ($bf,full_tag("SHORTNAME",5,false,$grade_outcome->shortname
));
1549 fwrite ($bf,full_tag("FULLNAME",5,false,$grade_outcome->fullname
));
1550 fwrite ($bf,full_tag("SCALEID",5,false,$grade_outcome->scaleid
));
1551 fwrite ($bf,full_tag("USERMODIFIED",5,false,$grade_outcome->usermodified
));
1554 fwrite ($bf,end_tag("GRADE_OUTCOME",4,true));
1556 //End grade_items tag
1557 $status = fwrite ($bf,end_tag("GRADE_OUTCOMES",3,true));
1563 function backup_gradebook_grades_info($bf,$preferences, $itemid) {
1569 // find all grades belonging to this item
1570 if ($grades = get_records('grade_grades', 'itemid', $itemid)) {
1571 fwrite ($bf,start_tag("GRADE_GRADES",5,true));
1572 foreach ($grades as $grade) {
1573 fwrite ($bf,start_tag("GRADE",6,true));
1574 fwrite ($bf,full_tag("ID",7,false,$grade->id
));
1575 fwrite ($bf,full_tag("USERID",7,false,$grade->userid
));
1576 fwrite ($bf,full_tag("RAWGRADE",7,false,$grade->rawgrade
));
1577 fwrite ($bf,full_tag("RAWGRADEMAX",7,false,$grade->rawgrademax
));
1578 fwrite ($bf,full_tag("RAWGRADEMIN",7,false,$grade->rawgrademin
));
1579 fwrite ($bf,full_tag("RAWSCALEID",7,false,$grade->rawscaleid
));
1580 fwrite ($bf,full_tag("USERMODIFIED",7,false,$grade->usermodified
));
1581 fwrite ($bf,full_tag("FINALGRADE",7,false,$grade->finalgrade
));
1582 fwrite ($bf,full_tag("HIDDEN",7,false,$final->hidden
));
1583 fwrite ($bf,full_tag("LOCKED",7,false,$final->locked
));
1584 fwrite ($bf,full_tag("LOCKTIME",7,false,$final->locktime
));
1585 fwrite ($bf,full_tag("EXPORTED",7,false,$final->exported
));
1586 fwrite ($bf,full_tag("OVERRIDDEN",7,false,$final->overridden
));
1587 fwrite ($bf,full_tag("EXCLUDED",7,false,$final->excluded
));
1588 fwrite ($bf,end_tag("GRADE",6,true));
1590 $stauts = fwrite ($bf,end_tag("GRADE_GRADES",5,true));
1595 function backup_gradebook_grades_text_info($bf, $preferences, $itemid) {
1601 // find all grade texts belonging to this item
1602 if ($texts = get_records('grade_grades_text', 'itemid', $itemid)) {
1603 fwrite ($bf,start_tag("GRADE_GRADES_TEXT",5,true));
1604 foreach ($texts as $text) {
1605 fwrite ($bf,start_tag("GRADE_TEXT",6,true));
1606 fwrite ($bf,full_tag("ID",7,false,$text->id
));
1607 fwrite ($bf,full_tag("GRADEID",7,false,$text->gradeid
));
1608 fwrite ($bf,full_tag("INFORMATION",7,false,$text->information
));
1609 fwrite ($bf,full_tag("INFORMATIONFORMAT",7,false,$text->informationformat
));
1610 fwrite ($bf,full_tag("FEEDBACK",7,false,$text->feedback
));
1611 fwrite ($bf,full_tag("FEEDBACKFORMAT",7,false,$text->feedbackformat
));
1612 fwrite ($bf,end_tag("GRADE_TEXT",6,true));
1614 $stauts = fwrite ($bf,end_tag("GRADE_GRADES_TEXT",5,true));
1619 //Backup scales info (common and course scales)
1620 function backup_scales_info($bf,$preferences) {
1626 //Counter, points to current record
1629 //Get scales (common and course scales)
1630 $scales = get_records_sql("SELECT id, courseid, userid, name, scale, description, timemodified
1631 FROM {$CFG->prefix}scale
1632 WHERE courseid = '0' or courseid = $preferences->backup_course");
1634 //Copy only used scales to $backupscales. They will be in backup (unused no). See Bug 1223.
1635 $backupscales = array();
1637 foreach ($scales as $scale) {
1638 if (course_scale_used($preferences->backup_course
, $scale->id
)) {
1639 $backupscales[] = $scale;
1644 //Pring scales header
1645 if ($backupscales) {
1646 //Pring scales header
1647 fwrite ($bf,start_tag("SCALES",2,true));
1649 foreach ($backupscales as $scale) {
1651 fwrite ($bf,start_tag("SCALE",3,true));
1653 fwrite ($bf,full_tag("ID",4,false,$scale->id
));
1654 fwrite ($bf,full_tag("COURSEID",4,false,$scale->courseid
));
1655 fwrite ($bf,full_tag("USERID",4,false,$scale->userid
));
1656 fwrite ($bf,full_tag("NAME",4,false,$scale->name
));
1657 fwrite ($bf,full_tag("SCALETEXT",4,false,$scale->scale
));
1658 fwrite ($bf,full_tag("DESCRIPTION",4,false,$scale->description
));
1659 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$scale->timemodified
));
1661 fwrite ($bf,end_tag("SCALE",3,true));
1664 $status = fwrite ($bf,end_tag("SCALES",2,true));
1669 //Backup events info (course events)
1670 function backup_events_info($bf,$preferences) {
1676 //Counter, points to current record
1679 //Get events (course events)
1680 $events = get_records_select("event","courseid='$preferences->backup_course' AND instance='0'","id");
1682 //Pring events header
1684 //Pring events header
1685 fwrite ($bf,start_tag("EVENTS",2,true));
1687 foreach ($events as $event) {
1689 fwrite ($bf,start_tag("EVENT",3,true));
1691 fwrite ($bf,full_tag("ID",4,false,$event->id
));
1692 fwrite ($bf,full_tag("NAME",4,false,$event->name
));
1693 fwrite ($bf,full_tag("DESCRIPTION",4,false,$event->description
));
1694 fwrite ($bf,full_tag("FORMAT",4,false,$event->format
));
1695 fwrite ($bf,full_tag("GROUPID",4,false,$event->groupid
));
1696 fwrite ($bf,full_tag("USERID",4,false,$event->userid
));
1697 fwrite ($bf,full_tag("REPEATID",4,false,$event->repeatid
));
1698 fwrite ($bf,full_tag("EVENTTYPE",4,false,$event->eventtype
));
1699 fwrite ($bf,full_tag("MODULENAME",4,false,$event->modulename
));
1700 fwrite ($bf,full_tag("TIMESTART",4,false,$event->timestart
));
1701 fwrite ($bf,full_tag("TIMEDURATION",4,false,$event->timeduration
));
1702 fwrite ($bf,full_tag("VISIBLE",4,false,$event->visible
));
1703 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$event->timemodified
));
1705 fwrite ($bf,end_tag("EVENT",3,true));
1708 $status = fwrite ($bf,end_tag("EVENTS",2,true));
1713 //Backup groups info
1714 function backup_groups_info($bf,$preferences) {
1722 $groups = get_groups($preferences->backup_course
); //TODO:check.
1724 //Pring groups header
1726 //Pring groups header
1727 fwrite ($bf,start_tag("GROUPS",2,true));
1729 foreach ($groups as $group) {
1731 fwrite ($bf,start_tag("GROUP",3,true));
1732 //Output group contents
1733 fwrite ($bf,full_tag("ID",4,false,$group->id
));
1734 ///fwrite ($bf,full_tag("COURSEID",4,false,$group->courseid));
1735 fwrite ($bf,full_tag("NAME",4,false,$group->name
));
1736 fwrite ($bf,full_tag("DESCRIPTION",4,false,$group->description
));
1737 fwrite ($bf,full_tag("ENROLMENTKEY",4,false,$group->enrolmentkey
)); //TODO:
1738 fwrite ($bf,full_tag("LANG",4,false,$group->lang
));
1739 fwrite ($bf,full_tag("THEME",4,false,$group->theme
));
1740 fwrite ($bf,full_tag("PICTURE",4,false,$group->picture
));
1741 fwrite ($bf,full_tag("HIDEPICTURE",4,false,$group->hidepicture
));
1742 fwrite ($bf,full_tag("TIMECREATED",4,false,$group->timecreated
));
1743 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$group->timemodified
));
1745 //Now, backup groups_members, only if users are included
1746 if ($preferences->backup_users
!= 2) {
1747 $status2 = backup_groups_members_info($bf,$preferences,$group->id
);
1751 fwrite ($bf,end_tag("GROUP",3,true));
1754 $status = fwrite ($bf,end_tag("GROUPS",2,true));
1756 //Now save group_files
1757 if ($status && $status2) {
1758 $status2 = backup_copy_group_files($preferences);
1761 return ($status && $status2);
1764 //Backup groups_members info
1765 function backup_groups_members_info($bf,$preferences,$groupid) {
1771 //Get groups_members
1772 $groups_members = groups_get_member_records($groupid);
1774 //Pring groups_members header
1775 if ($groups_members) {
1776 //Pring groups_members header
1777 fwrite ($bf,start_tag("MEMBERS",4,true));
1779 foreach ($groups_members as $group_member) {
1780 //Begin group_member tag
1781 fwrite ($bf,start_tag("MEMBER",5,true));
1782 //Output group_member contents
1783 fwrite ($bf,full_tag("USERID",6,false,$group_member->userid
));
1784 fwrite ($bf,full_tag("TIMEADDED",6,false,$group_member->timeadded
));
1785 //End group_member tag
1786 fwrite ($bf,end_tag("MEMBER",5,true));
1788 //End groups_members tag
1789 $status = fwrite ($bf,end_tag("MEMBERS",4,true));
1794 //Backup groupings info
1795 function backup_groupings_info($bf,$preferences) {
1803 $groupings = groups_get_grouping_records($preferences->backup_course
);
1805 //Pring groups header
1807 //Pring groups header
1808 fwrite ($bf,start_tag("GROUPINGS",2,true));
1810 foreach ($groupings as $grouping) {
1812 fwrite ($bf,start_tag("GROUPING",3,true));
1813 //Output group contents
1814 fwrite ($bf,full_tag("ID",4,false,$grouping->id
));
1815 fwrite ($bf,full_tag("NAME",4,false,$grouping->name
));
1816 fwrite ($bf,full_tag("DESCRIPTION",4,false,$grouping->description
));
1817 fwrite ($bf,full_tag("TIMECREATED",4,false,$grouping->timecreated
));
1819 $status2 = backup_groupids_info($bf,$preferences,$grouping->id
);
1822 fwrite ($bf,end_tag("GROUPING",3,true));
1825 $status = fwrite ($bf,end_tag("GROUPINGS",2,true));
1827 //(Now save grouping_files)
1829 return ($status && $status2);
1832 //Backup groupings-groups info
1833 function backup_groupids_info($bf,$preferences,$groupingid) {
1839 //Get groups_members
1840 $grouping_groups = groups_get_groups_in_grouping_records($groupingid) ;
1842 //Pring groups_members header
1843 if ($grouping_groups) {
1844 //Pring groups_members header
1845 fwrite ($bf,start_tag("GROUPS",4,true));
1847 foreach ($grouping_groups as $group2) {
1849 fwrite ($bf,start_tag("GROUP",5,true));
1850 //Output group_member contents
1851 fwrite ($bf,full_tag("GROUPID",6,false,$group2->groupid
));
1852 fwrite ($bf,full_tag("TIMEADDED",6,false,$group2->timeadded
)); //TODO:
1854 fwrite ($bf,end_tag("GROUP",5,true));
1856 //End groups_members tag
1857 $status = fwrite ($bf,end_tag("GROUPS",4,true));
1862 //Start the modules tag
1863 function backup_modules_start ($bf,$preferences) {
1865 return fwrite ($bf,start_tag("MODULES",2,true));
1868 //End the modules tag
1869 function backup_modules_end ($bf,$preferences) {
1871 return fwrite ($bf,end_tag("MODULES",2,true));
1874 //This function makes all the necesary calls to every mod
1875 //to export itself and its files !!!
1876 function backup_module($bf,$preferences,$module) {
1882 require_once($CFG->dirroot
.'/mod/'.$module.'/backuplib.php');
1884 if (isset($preferences->mods
[$module]->instances
)
1885 && is_array($preferences->mods
[$module]->instances
)) {
1886 $onemodbackup = $module.'_backup_one_mod';
1887 if (function_exists($onemodbackup)) {
1888 foreach ($preferences->mods
[$module]->instances
as $instance => $object) {
1889 if (!empty($object->backup
)) {
1890 $status = $onemodbackup($bf,$preferences,$instance);
1896 } else { // whole module.
1897 //First, re-check if necessary functions exists
1898 $modbackup = $module."_backup_mods";
1899 if (function_exists($modbackup)) {
1901 $status = $modbackup($bf,$preferences);
1903 //Something was wrong. Function should exist.
1912 //This function encode things to make backup multi-site fully functional
1913 //It does this conversions:
1914 // - $CFG->wwwroot/file.php/courseid ------------------> $@FILEPHP@$ (slasharguments links)
1915 // - $CFG->wwwroot/file.php?file=/courseid ------------> $@FILEPHP@$ (non-slasharguments links)
1916 // - Every module xxxx_encode_content_links() is executed too
1918 function backup_encode_absolute_links($content) {
1920 global $CFG,$preferences;
1922 //Use one static variable to cache all the require_once calls that,
1923 //under PHP5 seems to increase load too much, and we are requiring
1924 //them here thousands of times (one per content). MDL-8700.
1925 //Once fixed by PHP, we'll delete this hack
1927 static $includedfiles;
1928 if (!isset($includedfiles)) {
1929 $includedfiles = array();
1932 //Check if preferences is ok. If it isn't set, we are
1933 //in a scheduled_backup to we are able to get a copy
1934 //from CFG->backup_preferences
1935 if (!isset($preferences)) {
1936 $mypreferences = $CFG->backup_preferences
;
1938 //We are in manual backups so global preferences must exist!!
1939 $mypreferences = $preferences;
1942 //First, we check for every call to file.php inside the course
1943 $search = array($CFG->wwwroot
.'/file.php/'.$mypreferences->backup_course
,
1944 $CFG->wwwroot
.'/file.php?file=/'.$mypreferences->backup_course
);
1946 $replace = array('$@FILEPHP@$','$@FILEPHP@$');
1948 $result = str_replace($search,$replace,$content);
1950 foreach ($mypreferences->mods
as $name => $info) {
1951 /// We only include the corresponding backuplib.php if it hasn't been included before
1952 /// This will save some load under PHP5. MDL-8700.
1953 /// Once fixed by PHP, we'll delete this hack
1954 if (!in_array($name, $includedfiles)) {
1955 include_once("$CFG->dirroot/mod/$name/backuplib.php");
1956 $includedfiles[] = $name;
1958 //Check if the xxxx_encode_content_links exists
1959 $function_name = $name."_encode_content_links";
1960 if (function_exists($function_name)) {
1961 $result = $function_name($result,$mypreferences);
1965 if ($result != $content) {
1966 debugging('<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />');
1972 //This function copies all the needed files under the "users" directory to the "user_files"
1973 //directory under temp/backup
1974 function backup_copy_user_files ($preferences) {
1980 //First we check to "user_files" exists and create it as necessary
1981 //in temp/backup/$backup_code dir
1982 $status = check_and_create_user_files_dir($preferences->backup_unique_code
);
1984 //Now iterate over directories under "users" to check if that user must be
1987 $rootdir = $CFG->dataroot
."/users";
1988 //Check if directory exists
1989 if (is_dir($rootdir)) {
1990 $list = list_directories ($rootdir);
1993 foreach ($list as $dir) {
1994 //Look for dir like username in backup_ids
1995 $data = get_record ("backup_ids","backup_code",$preferences->backup_unique_code
,
1996 "table_name","user",
1998 //If exists, copy it
2000 $status = backup_copy_file($rootdir."/".$dir,
2001 $CFG->dataroot
."/temp/backup/".$preferences->backup_unique_code
."/user_files/".$dir);
2010 //This function copies all the needed files under the "groups" directory to the "group_files"
2011 //directory under temp/backup
2012 function backup_copy_group_files ($preferences) {
2018 //First we check if "group_files" exists and create it as necessary
2019 //in temp/backup/$backup_code dir
2020 $status = check_and_create_group_files_dir($preferences->backup_unique_code
);
2022 //Now iterate over directories under "groups" to check if that user must be
2025 $rootdir = $CFG->dataroot
.'/groups';
2026 //Check if directory exists
2027 if (is_dir($rootdir)) {
2028 $list = list_directories ($rootdir);
2031 foreach ($list as $dir) {
2032 //Look for dir like group in groups table
2033 $data = groups_group_belongs_to_course($dir, $preferences->backup_course
);
2034 //TODO:check. get_record ('groups', 'courseid', $preferences->backup_course,'id',$dir);
2035 //If exists, copy it
2037 $status = backup_copy_file($rootdir."/".$dir,
2038 $CFG->dataroot
."/temp/backup/".$preferences->backup_unique_code
."/group_files/".$dir);
2046 //This function copies all the course files under the course directory (except the moddata
2047 //directory to the "course_files" directory under temp/backup
2048 function backup_copy_course_files ($preferences) {
2054 //First we check to "course_files" exists and create it as necessary
2055 //in temp/backup/$backup_code dir
2056 $status = check_and_create_course_files_dir($preferences->backup_unique_code
);
2058 //Now iterate over files and directories except $CFG->moddata and backupdata to be
2061 $rootdir = $CFG->dataroot
."/".$preferences->backup_course
;
2063 $name_moddata = $CFG->moddata
;
2064 $name_backupdata = "backupdata";
2065 //Check if directory exists
2066 if (is_dir($rootdir)) {
2067 $list = list_directories_and_files ($rootdir);
2070 foreach ($list as $dir) {
2071 if ($dir !== $name_moddata and $dir !== $name_backupdata) {
2072 $status = backup_copy_file($rootdir."/".$dir,
2073 $CFG->dataroot
."/temp/backup/".$preferences->backup_unique_code
."/course_files/".$dir);
2081 //This function creates the zip file containing all the backup info
2082 //moodle.xml, moddata, user_files, course_files.
2083 //The zipped file is created in the backup directory and named with
2084 //the "oficial" name of the backup
2085 //It uses "pclzip" if available or system "zip" (unix only)
2086 function backup_zip ($preferences) {
2092 //Base dir where everything happens
2093 $basedir = cleardoubleslashes($CFG->dataroot
."/temp/backup/".$preferences->backup_unique_code
);
2094 //Backup zip file name
2095 $name = $preferences->backup_name
;
2096 //List of files and directories
2097 $filelist = list_directories_and_files ($basedir);
2099 //Convert them to full paths
2101 foreach ($filelist as $file) {
2102 $files[] = "$basedir/$file";
2105 $status = zip_files($files, "$basedir/$name");
2107 //echo "<br/>Status: ".$status; //Debug
2112 //This function copies the final zip to the course dir
2113 function copy_zip_to_course_dir ($preferences) {
2119 //Define zip location (from)
2120 $from_zip_file = $CFG->dataroot
."/temp/backup/".$preferences->backup_unique_code
."/".$preferences->backup_name
;
2122 //Initialise $to_zip_file
2125 //If $preferences->backup_destination isn't empty, then copy to custom directory
2126 if (!empty($preferences->backup_destination
)) {
2127 $to_zip_file = $preferences->backup_destination
."/".$preferences->backup_name
;
2129 //Define zip destination (course dir)
2130 $to_zip_file = $CFG->dataroot
."/".$preferences->backup_course
;
2132 //echo "<p>From: ".$from_zip_file."<br />"; //Debug
2134 //echo "<p>Checking: ".$to_zip_file."<br />"; //Debug
2136 //Checks course dir exists
2137 $status = check_dir_exists($to_zip_file,true);
2139 //Define zip destination (backup dir)
2140 $to_zip_file = $to_zip_file."/backupdata";
2142 //echo "<p>Checking: ".$to_zip_file."<br />"; //Debug
2144 //Checks backup dir exists
2145 $status = check_dir_exists($to_zip_file,true);
2147 //Define zip destination (zip file)
2148 $to_zip_file = $to_zip_file."/".$preferences->backup_name
;
2151 //echo "<p>To: ".$to_zip_file."<br />"; //Debug
2155 $status = backup_copy_file ($from_zip_file,$to_zip_file);
2162 * compatibility function
2163 * with new granular backup
2166 function backup_userdata_selected($preferences,$modname,$modid) {
2167 return ((empty($preferences->mods
[$modname]->instances
)
2168 && !empty($preferences->mods
[$modname]->userinfo
))
2169 ||
(is_array($preferences->mods
[$modname]->instances
)
2170 && array_key_exists($modid,$preferences->mods
[$modname]->instances
)
2171 && !empty($preferences->mods
[$modname]->instances
[$modid]->userinfo
)));
2175 function backup_mod_selected($preferences,$modname,$modid) {
2176 return ((empty($preferences->mods
[$modname]->instances
)
2177 && !empty($preferences->mods
[$modname]->backup
))
2178 ||
(is_array($preferences->mods
[$modname]->instances
)
2179 && array_key_exists($modid,$preferences->mods
[$modname]->instances
)
2180 && !empty($preferences->mods
[$modname]->instances
[$modid]->backup
)));
2184 * Checks for the required files/functions to backup every mod
2185 * And check if there is data about it
2187 function backup_fetch_prefs_from_request(&$preferences,&$count,$course) {
2188 global $CFG,$SESSION;
2190 // check to see if it's in the session already
2191 if (!empty($SESSION->backupprefs
) && array_key_exists($course->id
,$SESSION->backupprefs
) && !empty($SESSION->backupprefs
[$course->id
])) {
2192 $sprefs = $SESSION->backupprefs
[$course->id
];
2193 $preferences = $sprefs;
2194 // refetch backup_name just in case.
2195 $bn = optional_param('backup_name','',PARAM_FILE
);
2197 $preferences->backup_name
= $bn;
2203 if ($allmods = get_records("modules") ) {
2204 foreach ($allmods as $mod) {
2205 $modname = $mod->name
;
2206 $modfile = "$CFG->dirroot/mod/$modname/backuplib.php";
2207 $modbackup = $modname."_backup_mods";
2208 $modbackupone = $modname."_backup_one_mod";
2209 $modcheckbackup = $modname."_check_backup_mods";
2210 if (!file_exists($modfile)) {
2213 include_once($modfile);
2214 if (!function_exists($modbackup) ||
!function_exists($modcheckbackup)) {
2217 $var = "exists_".$modname;
2218 $preferences->$var = true;
2220 // check that there are instances and we can back them up individually
2221 if (!count_records('course_modules','course',$course->id
,'module',$mod->id
) ||
!function_exists($modbackupone)) {
2224 $var = 'exists_one_'.$modname;
2225 $preferences->$var = true;
2226 $varname = $modname.'_instances';
2227 $preferences->$varname = get_all_instances_in_course($modname,$course);
2228 foreach ($preferences->$varname as $instance) {
2229 $preferences->mods
[$modname]->instances
[$instance->id
]->name
= $instance->name
;
2230 $var = 'backup_'.$modname.'_instance_'.$instance->id
;
2231 $
$var = optional_param($var,0);
2232 $preferences->$var = $
$var;
2233 $preferences->mods
[$modname]->instances
[$instance->id
]->backup
= $
$var;
2234 $var = 'backup_user_info_'.$modname.'_instance_'.$instance->id
;
2235 $
$var = optional_param($var,0);
2236 $preferences->$var = $
$var;
2237 $preferences->mods
[$modname]->instances
[$instance->id
]->userinfo
= $
$var;
2238 $var = 'backup_'.$modname.'_instances';
2239 $preferences->$var = 1; // we need this later to determine what to display in modcheckbackup.
2244 $preferences->mods
[$modname]->name
= $modname;
2246 $var = "backup_".$modname;
2247 $
$var = optional_param( $var,0);
2248 $preferences->$var = $
$var;
2249 $preferences->mods
[$modname]->backup
= $
$var;
2251 //Check include user info
2252 $var = "backup_user_info_".$modname;
2253 $
$var = optional_param( $var,0);
2254 $preferences->$var = $
$var;
2255 $preferences->mods
[$modname]->userinfo
= $
$var;
2260 //Check other parameters
2261 $preferences->backup_metacourse
= optional_param('backup_metacourse',1,PARAM_INT
);
2262 $preferences->backup_users
= optional_param('backup_users',1,PARAM_INT
);
2263 $preferences->backup_logs
= optional_param('backup_logs',0,PARAM_INT
);
2264 $preferences->backup_user_files
= optional_param('backup_user_files',1,PARAM_INT
);
2265 $preferences->backup_course_files
= optional_param('backup_course_files',1,PARAM_INT
);
2266 $preferences->backup_messages
= optional_param('backup_messages',1,PARAM_INT
);
2267 $preferences->backup_course
= $course->id
;
2268 $preferences->backup_name
= required_param('backup_name',PARAM_FILE
);
2269 $preferences->backup_unique_code
= required_param('backup_unique_code');
2271 // put it (back) in the session
2272 $SESSION->backupprefs
[$course->id
] = $preferences;
2275 /* Finds all related roles used in course, mod and blocks context
2276 * @param object $preferences
2277 * @param object $course
2278 * @return array of role objects
2280 function backup_fetch_roles($preferences) {
2283 $contexts = array();
2286 /// adding course context
2287 $coursecontext = get_context_instance(CONTEXT_COURSE
, $preferences->backup_course
);
2288 $contexts[$coursecontext->id
] = $coursecontext;
2290 /// adding mod contexts
2291 $mods = $preferences->mods
;
2292 foreach ($mods as $modname => $mod) {
2293 $instances = $mod->instances
;
2294 foreach ($instances as $id => $instance) {
2295 // if this type of mod is to be backed up
2296 if ($instance->backup
) {
2297 $cm = get_coursemodule_from_instance($modname, $id);
2298 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
2299 // put context in array keys
2300 $contexts[$context->id
] = $context;
2305 // add all roles assigned at user context
2306 if ($preferences->backup_users
) {
2307 if ($users = get_records_sql("SELECT u.old_id, u.table_name,u.info
2308 FROM {$CFG->prefix}backup_ids u
2309 WHERE u.backup_code = '$preferences->backup_unique_code' AND
2310 u.table_name = 'user'")) {
2311 foreach ($users as $user) {
2312 $context = get_context_instance(CONTEXT_USER
, $user->old_id
);
2313 $contexts[$context->id
] = $context;
2319 // add all roles assigned at block context
2320 if ($courseblocks = get_records_sql("SELECT *
2321 FROM {$CFG->prefix}block_instance
2322 WHERE pagetype = '".PAGE_COURSE_VIEW
."'
2323 AND pageid = {$preferences->backup_course}")) {
2325 foreach ($courseblocks as $courseblock) {
2327 $context = get_context_instance(CONTEXT_BLOCK
, $courseblock->id
);
2328 $contexts[$context->id
] = $context;
2332 // foreach context, call get_roles_on_exact_context insert into array
2333 foreach ($contexts as $context) {
2334 if ($proles = get_roles_on_exact_context($context)) {
2335 foreach ($proles as $prole) {
2336 $roles[$prole->id
] = $prole;
2344 /* function to print xml for overrides */
2345 function write_role_overrides_xml($bf, $context, $startlevel) {
2346 fwrite ($bf, start_tag("ROLES_OVERRIDES", $startlevel, true));
2347 if ($roles = get_roles_with_override_on_context($context)) {
2348 foreach ($roles as $role) {
2349 fwrite ($bf, start_tag("ROLE", $startlevel+
1, true));
2350 fwrite ($bf, full_tag("ID", $startlevel+
2, false, $role->id
));
2351 fwrite ($bf, full_tag("NAME", $startlevel+
2, false, $role->name
));
2352 fwrite ($bf, full_tag("SHORTNAME", $startlevel+
2, false, $role->shortname
));
2353 fwrite ($bf, start_tag("CAPABILITIES", $startlevel+
2, true));
2354 if ($capabilities = get_capabilities_from_role_on_context($role, $context)) {
2355 foreach ($capabilities as $capability) {
2356 fwrite ($bf, start_tag("CAPABILITY", $startlevel+
3, true));
2357 fwrite ($bf, full_tag("NAME", $startlevel+
4, false, $capability->capability
));
2358 fwrite ($bf, full_tag("PERMISSION", $startlevel+
4, false, $capability->permission
));
2359 fwrite ($bf, full_tag("TIMEMODIFIED", $startlevel+
4, false, $capability->timemodified
));
2360 if (!isset($capability->modifierid
)) {
2361 $capability->modifierid
= 0;
2363 fwrite ($bf, full_tag("MODIFIERID", $startlevel+
4, false, $capability->modifierid
));
2364 fwrite ($bf, end_tag("CAPABILITY", $startlevel+
3, true));
2367 fwrite ($bf, end_tag("CAPABILITIES", $startlevel+
2, true));
2368 fwrite ($bf, end_tag("ROLE", $startlevel+
1, true));
2371 fwrite ($bf, end_tag("ROLES_OVERRIDES", $startlevel, true));
2374 /* function to print xml for assignment */
2375 function write_role_assignments_xml($bf, $context, $startlevel) {
2376 /// write role_assign code here
2377 fwrite ($bf, start_tag("ROLES_ASSIGNMENTS", $startlevel, true));
2379 if ($roles = get_roles_with_assignment_on_context($context)) {
2380 foreach ($roles as $role) {
2381 fwrite ($bf, start_tag("ROLE", $startlevel+
1, true));
2382 fwrite ($bf, full_tag("ID", $startlevel+
2, false, $role->id
));
2383 fwrite ($bf, full_tag("NAME", $startlevel+
2, false, $role->name
));
2384 fwrite ($bf, full_tag("SHORTNAME", $startlevel+
2, false, $role->shortname
));
2385 fwrite ($bf, start_tag("ASSIGNMENTS", $startlevel+
2, true));
2386 if ($assignments = get_users_from_role_on_context($role, $context)) {
2387 foreach ($assignments as $assignment) {
2388 fwrite ($bf, start_tag("ASSIGNMENT", $startlevel+
3, true));
2389 fwrite ($bf, full_tag("USERID", $startlevel+
4, false, $assignment->userid
));
2390 fwrite ($bf, full_tag("HIDDEN", $startlevel+
4, false, $assignment->hidden
));
2391 fwrite ($bf, full_tag("TIMESTART", $startlevel+
4, false, $assignment->timestart
));
2392 fwrite ($bf, full_tag("TIMEEND", $startlevel+
4, false, $assignment->timeend
));
2393 fwrite ($bf, full_tag("TIMEMODIFIED", $startlevel+
4, false, $assignment->timemodified
));
2394 if (!isset($assignment->modifierid
)) {
2395 $assignment->modifierid
= 0;
2397 fwrite ($bf, full_tag("MODIFIERID", $startlevel+
4, false, $assignment->modifierid
));
2398 fwrite ($bf, full_tag("ENROL", $startlevel+
4, false, $assignment->enrol
));
2399 fwrite ($bf, full_tag("SORTORDER", $startlevel+
4, false, $assignment->sortorder
));
2400 fwrite ($bf, end_tag("ASSIGNMENT", $startlevel+
3, true));
2403 fwrite ($bf, end_tag("ASSIGNMENTS", $startlevel+
2, true));
2404 fwrite ($bf, end_tag("ROLE", $startlevel+
1, true));
2407 fwrite ($bf, end_tag("ROLES_ASSIGNMENTS", $startlevel, true));
2411 function backup_execute(&$preferences, &$errorstr) {
2415 //Check for temp and backup and backup_unique_code directory
2416 //Create them as needed
2417 if (!defined('BACKUP_SILENTLY')) {
2418 echo "<li>".get_string("creatingtemporarystructures").'</li>';
2421 $status = check_and_create_backup_dir($preferences->backup_unique_code
);
2424 $status = clear_backup_dir($preferences->backup_unique_code
);
2427 //Delete old_entries from backup tables
2428 if (!defined('BACKUP_SILENTLY')) {
2429 echo "<li>".get_string("deletingolddata").'</li>';
2431 $status = backup_delete_old_data();
2433 if (!defined('BACKUP_SILENTLY')) {
2434 error ("An error occurred deleting old backup data");
2437 $errorstr = "An error occurred deleting old backup data";
2442 //Create the moodle.xml file
2444 if (!defined('BACKUP_SILENTLY')) {
2445 echo "<li>".get_string("creatingxmlfile");
2446 //Begin a new list to xml contents
2448 echo "<li>".get_string("writingheader").'</li>';
2450 //Obtain the xml file (create and open) and print prolog information
2451 $backup_file = backup_open_xml($preferences->backup_unique_code
);
2452 if (!defined('BACKUP_SILENTLY')) {
2453 echo "<li>".get_string("writinggeneralinfo").'</li>';
2455 //Prints general info about backup to file
2457 if (!$status = backup_general_info($backup_file,$preferences)) {
2458 if (!defined('BACKUP_SILENTLY')) {
2459 notify("An error occurred while backing up general info");
2462 $errorstr = "An error occurred while backing up general info";
2467 if (!defined('BACKUP_SILENTLY')) {
2468 echo "<li>".get_string("writingcoursedata");
2469 //Start new ul (for course)
2471 echo "<li>".get_string("courseinfo").'</li>';
2473 //Prints course start (tag and general info)
2475 if (!$status = backup_course_start($backup_file,$preferences)) {
2476 if (!defined('BACKUP_SILENTLY')) {
2477 notify("An error occurred while backing up course start");
2480 $errorstr = "An error occurred while backing up course start";
2485 //Metacourse information
2486 if ($status && $preferences->backup_metacourse
) {
2487 if (!defined('BACKUP_SILENTLY')) {
2488 echo "<li>".get_string("metacourse").'</li>';
2490 if (!$status = backup_course_metacourse($backup_file,$preferences)) {
2491 if (!defined('BACKUP_SILENTLY')) {
2492 notify("An error occurred while backing up metacourse info");
2495 $errorstr = "An error occurred while backing up metacourse info";
2500 if (!defined('BACKUP_SILENTLY')) {
2501 echo "<li>".get_string("blocks").'</li>';
2503 //Blocks information
2505 if (!$status = backup_course_blocks($backup_file,$preferences)) {
2506 if (!defined('BACKUP_SILENTLY')) {
2507 notify("An error occurred while backing up course blocks");
2510 $errorstr = "An error occurred while backing up course blocks";
2515 if (!defined('BACKUP_SILENTLY')) {
2516 echo "<li>".get_string("sections").'</li>';
2520 if (!$status = backup_course_sections($backup_file,$preferences)) {
2521 if (!defined('BACKUP_SILENTLY')) {
2522 notify("An error occurred while backing up course sections");
2525 $errorstr = "An error occurred while backing up course sections";
2531 //End course contents (close ul)
2532 if (!defined('BACKUP_SILENTLY')) {
2538 if (!defined('BACKUP_SILENTLY')) {
2539 echo "<li>".get_string("writinguserinfo").'</li>';
2541 if (!$status = backup_user_info($backup_file,$preferences)) {
2542 if (!defined('BACKUP_SILENTLY')) {
2543 notify("An error occurred while backing up user info");
2546 $errorstr = "An error occurred while backing up user info";
2552 //If we have selected to backup messages and we are
2553 //doing a SITE backup, let's do it
2554 if ($status && $preferences->backup_messages
&& $preferences->backup_course
== SITEID
) {
2555 if (!defined('BACKUP_SILENTLY')) {
2556 echo "<li>".get_string("writingmessagesinfo").'</li>';
2558 if (!$status = backup_messages($backup_file,$preferences)) {
2559 if (!defined('BACKUP_SILENTLY')) {
2560 notify("An error occurred while backing up messages");
2563 $errorstr = "An error occurred while backing up messages";
2569 //If we have selected to backup quizzes, backup categories and
2570 //questions structure (step 1). See notes on mod/quiz/backuplib.php
2571 if ($status and !empty($preferences->mods
['quiz']->backup
)) {
2572 if (!defined('BACKUP_SILENTLY')) {
2573 echo "<li>".get_string("writingcategoriesandquestions").'</li>';
2575 require_once($CFG->dirroot
.'/mod/quiz/backuplib.php');
2576 if (!$status = backup_question_categories($backup_file,$preferences)) {
2577 if (!defined('BACKUP_SILENTLY')) {
2578 notify("An error occurred while backing up quiz categories");
2581 $errorstr = "An error occurred while backing up quiz categories";
2587 //Print logs if selected
2589 if ($preferences->backup_logs
) {
2590 if (!defined('BACKUP_SILENTLY')) {
2591 echo "<li>".get_string("writingloginfo").'</li>';
2593 if (!$status = backup_log_info($backup_file,$preferences)) {
2594 if (!defined('BACKUP_SILENTLY')) {
2595 notify("An error occurred while backing up log info");
2598 $errorstr = "An error occurred while backing up log info";
2607 if (!defined('BACKUP_SILENTLY')) {
2608 echo "<li>".get_string("writingscalesinfo").'</li>';
2610 if (!$status = backup_scales_info($backup_file,$preferences)) {
2611 if (!defined('BACKUP_SILENTLY')) {
2612 notify("An error occurred while backing up scales");
2615 $errorstr = "An error occurred while backing up scales";
2621 //Print groupings info
2623 if (!defined('BACKUP_SILENTLY')) {
2624 echo "<li>".get_string("writinggroupingsinfo").'</li>';
2626 if (!$status = backup_groupings_info($backup_file,$preferences)) {
2627 if (!defined('BACKUP_SILENTLY')) {
2628 notify("An error occurred while backing up groupings");
2631 $errorstr = "An error occurred while backing up groupings";
2639 if (!defined('BACKUP_SILENTLY')) {
2640 echo "<li>".get_string("writinggroupsinfo").'</li>';
2642 if (!$status = backup_groups_info($backup_file,$preferences)) {
2643 if (!defined('BACKUP_SILENTLY')) {
2644 notify("An error occurred while backing up groups");
2647 $errostr = "An error occurred while backing up groups";
2655 if (!defined('BACKUP_SILENTLY')) {
2656 echo "<li>".get_string("writingeventsinfo").'</li>';
2658 if (!$status = backup_events_info($backup_file,$preferences)) {
2659 if (!defined('BACKUP_SILENTLY')) {
2660 notify("An error occurred while backing up events");
2663 $errorstr = "An error occurred while backing up events";
2669 //Print gradebook info
2671 if (!defined('BACKUP_SILENTLY')) {
2672 echo "<li>".get_string("writinggradebookinfo").'</li>';
2674 if (!$status = backup_gradebook_info($backup_file,$preferences)) {
2675 if (!defined('BACKUP_SILENTLY')) {
2676 notify("An error occurred while backing up gradebook");
2679 $errorstr = "An error occurred while backing up gradebook";
2685 //Module info, this unique function makes all the work!!
2686 //db export and module fileis copy
2688 $mods_to_backup = false;
2689 //Check if we have any mod to backup
2690 foreach ($preferences->mods
as $module) {
2691 if ($module->backup
) {
2692 $mods_to_backup = true;
2695 //If we have to backup some module
2696 if ($mods_to_backup) {
2697 if (!defined('BACKUP_SILENTLY')) {
2698 echo "<li>".get_string("writingmoduleinfo");
2701 if (!$status = backup_modules_start ($backup_file,$preferences)) {
2702 if (!defined('BACKUP_SILENTLY')) {
2703 notify("An error occurred while backing up module info");
2706 $errorstr = "An error occurred while backing up module info";
2710 //Open ul for module list
2711 if (!defined('BACKUP_SILENTLY')) {
2714 //Iterate over modules and call backup
2715 foreach ($preferences->mods
as $module) {
2716 if ($module->backup
and $status) {
2717 if (!defined('BACKUP_SILENTLY')) {
2718 echo "<li>".get_string("modulenameplural",$module->name
).'</li>';
2720 if (!$status = backup_module($backup_file,$preferences,$module->name
)) {
2721 if (!defined('BACKUP_SILENTLY')) {
2722 notify("An error occurred while backing up '$module->name'");
2725 $errorstr = "An error occurred while backing up '$module->name'";
2731 //Close ul for module list
2732 if (!defined('BACKUP_SILENTLY')) {
2736 if (!$status = backup_modules_end ($backup_file,$preferences)) {
2737 if (!defined('BACKUP_SILENTLY')) {
2738 notify("An error occurred while finishing the module backups");
2741 $errorstr = "An error occurred while finishing the module backups";
2748 //Backup course format data, if any.
2749 if (!defined('BACKUP_SILENTLY')) {
2750 echo '<li>'.get_string("courseformatdata").'</li>';
2753 if (!$status = backup_format_data($backup_file,$preferences)) {
2754 if (!defined('BACKUP_SILENTLY')) {
2755 notify("An error occurred while backing up the course format data");
2758 $errorstr = "An error occurred while backing up the course format data";
2766 if (!$status = backup_course_end($backup_file,$preferences)) {
2767 if (!defined('BACKUP_SILENTLY')) {
2768 notify("An error occurred while closing the course backup");
2771 $errorstr = "An error occurred while closing the course backup";
2776 //Close the xml file and xml data
2778 backup_close_xml($backup_file);
2781 //End xml contents (close ul)
2782 if (!defined('BACKUP_SILENTLY')) {
2787 //Now, if selected, copy user files
2789 if ($preferences->backup_user_files
) {
2790 if (!defined('BACKUP_SILENTLY')) {
2791 echo "<li>".get_string("copyinguserfiles").'</li>';
2793 if (!$status = backup_copy_user_files ($preferences)) {
2794 if (!defined('BACKUP_SILENTLY')) {
2795 notify("An error occurred while copying user files");
2798 $errorstr = "An error occurred while copying user files";
2805 //Now, if selected, copy course files
2807 if ($preferences->backup_course_files
) {
2808 if (!defined('BACKUP_SILENTLY')) {
2809 echo "<li>".get_string("copyingcoursefiles").'</li>';
2811 if (!$status = backup_copy_course_files ($preferences)) {
2812 if (!defined('BACKUP_SILENTLY')) {
2813 notify("An error occurred while copying course files");
2816 $errorstr = "An error occurred while copying course files";
2823 //Now, zip all the backup directory contents
2825 if (!defined('BACKUP_SILENTLY')) {
2826 echo "<li>".get_string("zippingbackup").'</li>';
2828 if (!$status = backup_zip ($preferences)) {
2829 if (!defined('BACKUP_SILENTLY')) {
2830 notify("An error occurred while zipping the backup");
2833 $errorstr = "An error occurred while zipping the backup";
2839 //Now, copy the zip file to course directory
2841 if (!defined('BACKUP_SILENTLY')) {
2842 echo "<li>".get_string("copyingzipfile").'</li>';
2844 if (!$status = copy_zip_to_course_dir ($preferences)) {
2845 if (!defined('BACKUP_SILENTLY')) {
2846 notify("An error occurred while copying the zip file to the course directory");
2849 $errorstr = "An error occurred while copying the zip file to the course directory";
2855 //Now, clean temporary data (db and filesystem)
2857 if (!defined('BACKUP_SILENTLY')) {
2858 echo "<li>".get_string("cleaningtempdata").'</li>';
2860 if (!$status = clean_temp_data ($preferences)) {
2861 if (!defined('BACKUP_SILENTLY')) {
2862 notify("An error occurred while cleaning up temporary data");
2865 $errorstr = "An error occurred while cleaning up temporary data";
2875 * This function generates the default zipfile name for a backup
2876 * based on the course id and the unique code.
2878 * @param object $course course object
2879 * @param string $backup_unique_code (optional, if left out current timestamp used)
2882 * @return string filename (excluding path information)
2884 function backup_get_zipfile_name($course, $backup_unique_code='') {
2886 if (empty($backup_unique_code)) {
2887 $backup_unique_code = time();
2890 //Calculate the backup word
2891 //Take off some characters in the filename !!
2892 $takeoff = array(" ", ":", "/", "\\", "|");
2893 $backup_word = str_replace($takeoff,"_",moodle_strtolower(get_string("backupfilename")));
2894 //If non-translated, use "backup"
2895 if (substr($backup_word,0,1) == "[") {
2896 $backup_word= "backup";
2899 //Calculate the date format string
2900 $backup_date_format = str_replace(" ","_",get_string("backupnameformat"));
2901 //If non-translated, use "%Y%m%d-%H%M"
2902 if (substr($backup_date_format,0,1) == "[") {
2903 $backup_date_format = "%%Y%%m%%d-%%H%%M";
2906 //Calculate the shortname
2907 $backup_shortname = clean_filename($course->shortname
);
2908 if (empty($backup_shortname) or $backup_shortname == '_' ) {
2909 $backup_shortname = $course->id
;
2912 //Calculate the final backup filename
2914 $backup_name = $backup_word."-";
2916 $backup_name .= moodle_strtolower($backup_shortname)."-";
2918 $backup_name .= userdate(time(),$backup_date_format,99,false);
2920 $backup_name .= ".zip";
2921 //And finally, clean everything
2922 $backup_name = clean_filename($backup_name);
2924 return $backup_name;
2929 * This function adds on the standard items to the preferences
2930 * Like moodle version and backup version
2932 * @param object $preferences existing preferences object.
2933 * (passed by reference)
2935 function backup_add_static_preferences(&$preferences) {
2937 $preferences->moodle_version
= $CFG->version
;
2938 $preferences->moodle_release
= $CFG->release
;
2939 $preferences->backup_version
= $CFG->backup_version
;
2940 $preferences->backup_release
= $CFG->backup_release
;