MDL-11510 added missing fields in new gradebook backup
[moodle-pu.git] / backup / backuplib.php
blobcc9b00453faf486beec0ce66d7bd2df507d58c25
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 //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)
11 // 2-->none
13 global $db, $CFG;
15 $context = get_context_instance(CONTEXT_COURSE, $course);
16 $count_users = 0;
18 //If we've selected none, simply return 0
19 if ($backup_users == 0 or $backup_users == 1) {
21 //Now, depending of parameters, create $backupable_users
22 if ($backup_users == 0) {
23 //Calculate all users (every record in users table)
24 $all_users = backup_get_all_users();
26 $backupable_users = $all_users;
27 } else {
28 //Calculate needed users (calling every xxxx_get_participants function + scales users)
29 $needed_users = backup_get_needed_users($course, $backup_messages);
31 //Calculate enrolled users (students + teachers)
32 $enrolled_users = backup_get_enrolled_users($course);
34 //Calculate course users (needed + enrolled)
35 //First, needed
36 $course_users = $needed_users;
38 //Now, enrolled
39 if ($enrolled_users) {
40 foreach ($enrolled_users as $enrolled_user) {
41 $course_users[$enrolled_user->id]->id = $enrolled_user->id;
45 $backupable_users = $course_users;
48 //If we have backupable users
49 if ($backupable_users) {
50 //Iterate over users putting their roles
51 foreach ($backupable_users as $backupable_user) {
52 $backupable_user->info = "";
54 //Is needed user (exists in needed_users)
55 if (isset($needed_users[$backupable_user->id])) {
56 $backupable_user->info .= "needed";
57 } else if (isset($course_users[$backupable_user->id])) {
58 $backupable_user->info .= "needed";
59 } // Yu: also needed because they can view course
60 // might need another variable
62 //Now create the backup_id record
63 $backupids_rec = new stdClass;
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.
70 //When all users
71 $status = insert_record('backup_ids', $backupids_rec, false);
72 $count_users++;
74 //Do some output
75 backup_flush(30);
79 //Prepare Info
80 //Gets the user data
81 $info[0][0] = get_string("users");
82 $info[0][1] = $count_users;
84 return $info;
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) {
96 global $CFG;
98 $result = 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);
115 //Add them to result
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)
127 //Get users
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
132 if ($scaleusers) {
133 foreach ($scaleusers as $scaleuser) {
134 //If userid != 0
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");
144 //Get users
145 $messageusers = message_get_participants();
146 //Add message users to results
147 if ($messageusers) {
148 foreach ($messageusers as $messageuser) {
149 //If id != 0
150 if ($messageuser->id !=0) {
151 $result[$messageuser->id]->id = $messageuser->id;
157 return $result;
161 //Returns every enrolled user (student and teacher) in a course
163 function backup_get_enrolled_users ($courseid) {
165 global $CFG;
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) {
183 global $CFG;
185 //Now execute the count
186 $ids = count_records("log","course",$course);
188 //Gets the user data
189 $info[0][0] = get_string("logs");
190 if ($ids) {
191 $info[0][1] = $ids;
192 } else {
193 $info[0][1] = 0;
196 return $info;
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) {
205 global $CFG;
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,"/");
215 if ($tok) {
216 $userid = $tok;
217 } else {
218 //We were getting $dir='0', so continue (WAS: $tok = "";)
219 continue;
221 //Look it is a backupable user
222 $data = get_record ("backup_ids","backup_code","$backup_unique_code",
223 "table_name","user",
224 "old_id",$userid);
225 if ($data) {
226 //Insert them into backup_files
227 $status = execute_sql("INSERT INTO {$CFG->prefix}backup_files
228 (backup_code, file_type, path, old_id)
229 VALUES
230 ('$backup_unique_code','user','".addslashes($dir)."','$userid')",false);
232 //Do some output
233 backup_flush(30);
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'");
242 //Gets the user data
243 $info[0][0] = get_string("files");
244 if ($ids) {
245 $info[0][1] = count($ids);
246 } else {
247 $info[0][1] = 0;
250 return $info;
254 * Calculate the number of course files to backup
255 * under $CFG->dataroot/$course, except $CFG->moddata, and backupdata
256 * and put them (their path) in backup_ids
257 * Return an array of info (name,value)
259 function course_files_check_backup($course, $backup_unique_code) {
261 global $CFG;
263 $rootdir = $CFG->dataroot."/$course";
264 //Check if directory exists
265 if (is_dir($rootdir)) {
266 //Get files and directories without descend
267 $coursedirs = get_directory_list($rootdir,$CFG->moddata,false,true,true);
268 $backupdata_dir = "backupdata";
269 foreach ($coursedirs as $dir) {
270 //Check it isn't backupdata_dir
271 if (strpos($dir,$backupdata_dir)!==0) {
272 //Insert them into backup_files
273 $status = execute_sql("INSERT INTO {$CFG->prefix}backup_files
274 (backup_code, file_type, path)
275 VALUES
276 ('$backup_unique_code','course','".addslashes($dir)."')",false);
278 //Do some output
279 backup_flush(30);
283 //Now execute the select
284 $ids = get_records_sql("SELECT DISTINCT b.path, b.old_id
285 FROM {$CFG->prefix}backup_files b
286 WHERE backup_code = '$backup_unique_code' AND
287 file_type = 'course'");
288 //Gets the user data
289 $info = array();
290 $info[0] = array();
291 $info[0][0] = get_string("files");
292 if ($ids) {
293 $info[0][1] = count($ids);
294 } else {
295 $info[0][1] = 0;
298 return $info;
302 * Calculate the number of site files to backup
303 * under $CFG->dataroot/SITEID
304 * Their path is already in backup_ids, put there by modules check_backup functions.
305 * Modules only put in paths of files that are used.
307 * Return an array of info (name,value)
309 function site_files_check_backup($course, $backup_unique_code) {
310 global $CFG;
312 //execute the select, records have been inserted by modules during their ****_check_backup_mods function.
313 $ids = get_records_sql("SELECT DISTINCT b.path
314 FROM {$CFG->prefix}backup_files b
315 WHERE backup_code = '$backup_unique_code' AND
316 file_type = 'site'");
317 //Gets the user data
318 $info = array();
319 $info[0] = array();
320 $info[0][0] = get_string('files');
321 if ($ids) {
322 $info[0][1] = count($ids);
323 } else {
324 $info[0][1] = 0;
327 return $info;
330 //Function to check and create the needed moddata dir to
331 //save all the mod backup files. We always name it moddata
332 //to be able to restore it, but in restore we check for
333 //$CFG->moddata !!
334 function check_and_create_moddata_dir($backup_unique_code) {
336 global $CFG;
338 $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/moddata",true);
340 return $status;
343 //Function to check and create the "user_files" dir to
344 //save all the user files we need from "users" dir
345 function check_and_create_user_files_dir($backup_unique_code) {
347 global $CFG;
349 $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/user_files",true);
351 return $status;
354 //Function to check and create the "group_files" dir to
355 //save all the user files we need from "groups" dir
356 function check_and_create_group_files_dir($backup_unique_code) {
358 global $CFG;
360 $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/group_files",true);
362 return $status;
365 //Function to check and create the "course_files" dir to
366 //save all the course files we need from "CFG->datadir/course" dir
367 function check_and_create_course_files_dir($backup_unique_code) {
369 global $CFG;
371 $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/course_files",true);
373 return $status;
376 //Function to check and create the "site_files" dir to
377 //save all the course files we need from "CFG->datadir/SITEID" dir
378 function check_and_create_site_files_dir($backup_unique_code) {
380 global $CFG;
382 $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code."/site_files",true);
384 return $status;
387 //Function to create, open and write header of the xml file
388 function backup_open_xml($backup_unique_code) {
390 global $CFG;
392 $status = true;
394 //Open for writing
396 $file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
397 $backup_file = fopen($file,"w");
398 //Writes the header
399 $status = fwrite ($backup_file,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
400 if ($status) {
401 $status = fwrite ($backup_file,start_tag("MOODLE_BACKUP",0,true));
403 if ($status) {
404 return $backup_file;
405 } else {
406 return false;
410 //Close the file
411 function backup_close_xml($backup_file) {
412 $status = fwrite ($backup_file,end_tag("MOODLE_BACKUP",0,true));
413 return fclose($backup_file);
416 //Return the xml start tag
417 function start_tag($tag,$level=0,$endline=false,$attributes=null) {
418 if ($endline) {
419 $endchar = "\n";
420 } else {
421 $endchar = "";
423 $attrstring = '';
424 if (!empty($attributes) && is_array($attributes)) {
425 foreach ($attributes as $key => $value) {
426 $attrstring .= " ".xml_tag_safe_content($key)."=\"".
427 xml_tag_safe_content($value)."\"";
430 return str_repeat(" ",$level*2)."<".strtoupper($tag).$attrstring.">".$endchar;
433 //Return the xml end tag
434 function end_tag($tag,$level=0,$endline=true) {
435 if ($endline) {
436 $endchar = "\n";
437 } else {
438 $endchar = "";
440 return str_repeat(" ",$level*2)."</".strtoupper($tag).">".$endchar;
443 //Return the start tag, the contents and the end tag
444 function full_tag($tag,$level=0,$endline=true,$content,$attributes=null) {
446 global $CFG;
447 //Here we encode absolute links
448 // MDL-10770
449 if (is_null($content)) {
450 $content = '$@NULL@$';
451 } else {
452 $content = backup_encode_absolute_links($content);
454 $st = start_tag($tag,$level,$endline,$attributes);
456 $co = xml_tag_safe_content($content);
458 $et = end_tag($tag,0,true);
460 return $st.$co.$et;
464 function xml_tag_safe_content($content) {
465 global $CFG;
466 //If enabled, we strip all the control chars (\x0-\x1f) from the text but tabs (\x9),
467 //newlines (\xa) and returns (\xd). The delete control char (\x7f) is also included.
468 //because they are forbiden in XML 1.0 specs. The expression below seems to be
469 //UTF-8 safe too because it simply ignores the rest of characters.
470 $content = preg_replace("/[\x-\x8\xb-\xc\xe-\x1f\x7f]/is","",$content);
471 $content = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
472 return $content;
475 //Prints General info about the course
476 //name, moodle_version (internal and release), backup_version, date, info in file...
477 function backup_general_info ($bf,$preferences) {
479 global $CFG;
481 fwrite ($bf,start_tag("INFO",1,true));
483 //The name of the backup
484 fwrite ($bf,full_tag("NAME",2,false,$preferences->backup_name));
485 //The moodle_version
486 fwrite ($bf,full_tag("MOODLE_VERSION",2,false,$preferences->moodle_version));
487 fwrite ($bf,full_tag("MOODLE_RELEASE",2,false,$preferences->moodle_release));
488 //The backup_version
489 fwrite ($bf,full_tag("BACKUP_VERSION",2,false,$preferences->backup_version));
490 fwrite ($bf,full_tag("BACKUP_RELEASE",2,false,$preferences->backup_release));
491 //The date
492 fwrite ($bf,full_tag("DATE",2,false,$preferences->backup_unique_code));
493 //The original site wwwroot
494 fwrite ($bf,full_tag("ORIGINAL_WWWROOT",2,false,$CFG->wwwroot));
495 //The zip method used
496 if (!empty($CFG->zip)) {
497 $zipmethod = 'external';
498 } else {
499 $zipmethod = 'internal';
501 //Indicate if it includes external MNET users
502 $sql = "SELECT b.old_id
503 FROM {$CFG->prefix}backup_ids b
504 JOIN {$CFG->prefix}user u ON b.old_id=u.id
505 WHERE b.backup_code = '$preferences->backup_unique_code'
506 AND b.table_name = 'user' AND u.mnethostid != '{$CFG->mnet_localhost_id}'";
507 if (record_exists_sql($sql)) {
508 fwrite ($bf,full_tag("MNET_REMOTEUSERS",2,false,'true'));
510 fwrite ($bf,full_tag("ZIP_METHOD",2,false,$zipmethod));
511 //Te includes tag
512 fwrite ($bf,start_tag("DETAILS",2,true));
513 //Now, go to mod element of preferences to print its status
514 foreach ($preferences->mods as $element) {
515 //Calculate info
516 $included = "false";
517 $userinfo = "false";
518 if ($element->backup) {
519 $included = "true";
520 if ($element->userinfo) {
521 $userinfo = "true";
524 //Prints the mod start
525 fwrite ($bf,start_tag("MOD",3,true));
526 fwrite ($bf,full_tag("NAME",4,false,$element->name));
527 fwrite ($bf,full_tag("INCLUDED",4,false,$included));
528 fwrite ($bf,full_tag("USERINFO",4,false,$userinfo));
530 if (isset($preferences->mods[$element->name]->instances)
531 && is_array($preferences->mods[$element->name]->instances)
532 && count($preferences->mods[$element->name]->instances)) {
533 fwrite ($bf, start_tag("INSTANCES",4,true));
534 foreach ($preferences->mods[$element->name]->instances as $id => $object) {
535 if (!empty($object->backup)) {
536 //Calculate info
537 $included = "false";
538 $userinfo = "false";
539 if ($object->backup) {
540 $included = "true";
541 if ($object->userinfo) {
542 $userinfo = "true";
545 fwrite ($bf, start_tag("INSTANCE",5,true));
546 fwrite ($bf, full_tag("ID",5,false,$id));
547 fwrite ($bf, full_tag("NAME",5,false,$object->name));
548 fwrite ($bf, full_tag("INCLUDED",5,false,$included)) ;
549 fwrite ($bf, full_tag("USERINFO",5,false,$userinfo));
550 fwrite ($bf, end_tag("INSTANCE",5,true));
553 fwrite ($bf, end_tag("INSTANCES",4,true));
557 //Print the end
558 fwrite ($bf,end_tag("MOD",3,true));
560 //The metacourse in backup
561 if ($preferences->backup_metacourse == 1) {
562 fwrite ($bf,full_tag("METACOURSE",3,false,"true"));
563 } else {
564 fwrite ($bf,full_tag("METACOURSE",3,false,"false"));
566 //The user in backup
567 if ($preferences->backup_users == 1) {
568 fwrite ($bf,full_tag("USERS",3,false,"course"));
569 } else if ($preferences->backup_users == 0) {
570 fwrite ($bf,full_tag("USERS",3,false,"all"));
571 } else {
572 fwrite ($bf,full_tag("USERS",3,false,"none"));
574 //The logs in backup
575 if ($preferences->backup_logs == 1) {
576 fwrite ($bf,full_tag("LOGS",3,false,"true"));
577 } else {
578 fwrite ($bf,full_tag("LOGS",3,false,"false"));
580 //The user files
581 if ($preferences->backup_user_files == 1) {
582 fwrite ($bf,full_tag("USERFILES",3,false,"true"));
583 } else {
584 fwrite ($bf,full_tag("USERFILES",3,false,"false"));
586 //The course files
587 if ($preferences->backup_course_files == 1) {
588 fwrite ($bf,full_tag("COURSEFILES",3,false,"true"));
589 } else {
590 fwrite ($bf,full_tag("COURSEFILES",3,false,"false"));
592 //The course files
593 if ($preferences->backup_site_files == 1) {
594 fwrite ($bf,full_tag("SITEFILES",3,false,"true"));
595 } else {
596 fwrite ($bf,full_tag("SITEFILES",3,false,"false"));
598 //The messages in backup
599 if ($preferences->backup_messages == 1 && $preferences->backup_course == SITEID) {
600 fwrite ($bf,full_tag("MESSAGES",3,false,"true"));
601 } else {
602 fwrite ($bf,full_tag("MESSAGES",3,false,"false"));
604 //The mode of writing the block data
605 fwrite ($bf,full_tag('BLOCKFORMAT',3,false,'instances'));
606 fwrite ($bf,end_tag("DETAILS",2,true));
608 $status = fwrite ($bf,end_tag("INFO",1,true));
610 ///Roles stuff goes in here
612 fwrite ($bf, start_tag('ROLES', 1, true));
613 $roles = backup_fetch_roles($preferences);
615 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
617 foreach ($roles as $role) {
618 fwrite ($bf,start_tag('ROLE',2,true));
619 fwrite ($bf,full_tag('ID', 3, false, $role->id));
620 fwrite ($bf,full_tag('NAME',3,false,$role->name));
621 fwrite ($bf,full_tag('SHORTNAME',3,false,$role->shortname));
622 // find and write all default capabilities
623 fwrite ($bf,start_tag('CAPABILITIES',3,true));
624 // pull out all default (site context) capabilities
625 if ($capabilities = role_context_capabilities($role->id, $sitecontext)) {
626 foreach ($capabilities as $capability=>$value) {
627 fwrite ($bf,start_tag('CAPABILITY',4,true));
628 fwrite ($bf,full_tag('NAME', 5, false, $capability));
629 fwrite ($bf,full_tag('PERMISSION', 5, false, $value));
630 // use this to pull out the other info (timemodified and modifierid)
631 $cap = get_record_sql("SELECT *
632 FROM {$CFG->prefix}role_capabilities
633 WHERE capability = '$capability'
634 AND contextid = $sitecontext->id
635 AND roleid = $role->id");
636 fwrite ($bf, full_tag("TIMEMODIFIED", 5, false, $cap->timemodified));
637 fwrite ($bf, full_tag("MODIFIERID", 5, false, $cap->modifierid));
638 fwrite ($bf,end_tag('CAPABILITY',4,true));
641 fwrite ($bf,end_tag('CAPABILITIES',3,true));
642 fwrite ($bf,end_tag('ROLE',2,true));
644 fwrite ($bf,end_tag('ROLES', 1, true));
645 return $status;
648 //Prints course's general info (table course)
649 function backup_course_start ($bf,$preferences) {
651 global $CFG;
653 $status = true;
655 //Course open tag
656 fwrite ($bf,start_tag("COURSE",1,true));
657 //Header open tag
658 fwrite ($bf,start_tag("HEADER",2,true));
660 //Get info from course
661 $course = get_record("course","id",$preferences->backup_course);
662 $context = get_context_instance(CONTEXT_COURSE, $course->id);
663 if ($course) {
664 //Prints course info
665 fwrite ($bf,full_tag("ID",3,false,$course->id));
666 //Obtain the category
667 $category = get_record("course_categories","id","$course->category");
668 if ($category) {
669 //Prints category info
670 fwrite ($bf,start_tag("CATEGORY",3,true));
671 fwrite ($bf,full_tag("ID",4,false,$course->category));
672 fwrite ($bf,full_tag("NAME",4,false,$category->name));
673 fwrite ($bf,end_tag("CATEGORY",3,true));
675 //Continues with the course
676 fwrite ($bf,full_tag("PASSWORD",3,false,$course->password));
677 fwrite ($bf,full_tag("FULLNAME",3,false,$course->fullname));
678 fwrite ($bf,full_tag("SHORTNAME",3,false,$course->shortname));
679 fwrite ($bf,full_tag("IDNUMBER",3,false,$course->idnumber));
680 fwrite ($bf,full_tag("SUMMARY",3,false,$course->summary));
681 fwrite ($bf,full_tag("FORMAT",3,false,$course->format));
682 fwrite ($bf,full_tag("SHOWGRADES",3,false,$course->showgrades));
683 fwrite ($bf,full_tag("NEWSITEMS",3,false,$course->newsitems));
684 fwrite ($bf,full_tag("TEACHER",3,false,$course->teacher));
685 fwrite ($bf,full_tag("TEACHERS",3,false,$course->teachers));
686 fwrite ($bf,full_tag("STUDENT",3,false,$course->student));
687 fwrite ($bf,full_tag("STUDENTS",3,false,$course->students));
688 fwrite ($bf,full_tag("GUEST",3,false,$course->guest));
689 fwrite ($bf,full_tag("STARTDATE",3,false,$course->startdate));
690 fwrite ($bf,full_tag("NUMSECTIONS",3,false,$course->numsections));
691 //fwrite ($bf,full_tag("SHOWRECENT",3,false,$course->showrecent)); INFO: This is out in 1.3
692 fwrite ($bf,full_tag("MAXBYTES",3,false,$course->maxbytes));
693 fwrite ($bf,full_tag("SHOWREPORTS",3,false,$course->showreports));
694 fwrite ($bf,full_tag("GROUPMODE",3,false,$course->groupmode));
695 fwrite ($bf,full_tag("GROUPMODEFORCE",3,false,$course->groupmodeforce));
696 fwrite ($bf,full_tag("DEFAULTGROUPINGID",3,false,$course->defaultgroupingid));
697 fwrite ($bf,full_tag("LANG",3,false,$course->lang));
698 fwrite ($bf,full_tag("THEME",3,false,$course->theme));
699 fwrite ($bf,full_tag("COST",3,false,$course->cost));
700 fwrite ($bf,full_tag("CURRENCY",3,false,$course->currency));
701 fwrite ($bf,full_tag("MARKER",3,false,$course->marker));
702 fwrite ($bf,full_tag("VISIBLE",3,false,$course->visible));
703 fwrite ($bf,full_tag("HIDDENSECTIONS",3,false,$course->hiddensections));
704 fwrite ($bf,full_tag("TIMECREATED",3,false,$course->timecreated));
705 fwrite ($bf,full_tag("TIMEMODIFIED",3,false,$course->timemodified));
706 //If not selected, force metacourse to 0
707 if (!$preferences->backup_metacourse) {
708 $status = fwrite ($bf,full_tag("METACOURSE",3,false,'0'));
709 //else, export the field as is in DB
710 } else {
711 $status = fwrite ($bf,full_tag("METACOURSE",3,false,$course->metacourse));
713 fwrite ($bf,full_tag("EXPIRENOTIFY",3,false,$course->expirynotify));
714 fwrite ($bf,full_tag("NOTIFYSTUDENTS",3,false,$course->notifystudents));
715 fwrite ($bf,full_tag("EXPIRYTHRESHOLD",3,false,$course->expirythreshold));
716 fwrite ($bf,full_tag("ENROLLABLE",3,false,$course->enrollable));
717 fwrite ($bf,full_tag("ENROLSTARTDATE",3,false,$course->enrolstartdate));
718 fwrite ($bf,full_tag("ENROLENDDATE",3,false,$course->enrolenddate));
719 fwrite ($bf,full_tag("ENROLPERIOD",3,false,$course->enrolperiod));
721 /// write local course overrides here?
722 write_role_overrides_xml($bf, $context, 3);
723 /// write role_assign code here
724 write_role_assignments_xml($bf, $context, 3, $preferences);
725 //Print header end
726 fwrite ($bf,end_tag("HEADER",2,true));
727 } else {
728 $status = false;
731 return $status;
734 //Prints course's end tag
735 function backup_course_end ($bf,$preferences) {
737 //Course end tag
738 $status = fwrite ($bf,end_tag("COURSE",1,true));
740 return $status;
744 //Prints course's metacourse info (table course_meta)
745 function backup_course_metacourse ($bf,$preferences) {
747 global $CFG;
749 $status = true;
751 //Get info from meta
752 $parents = get_records_sql ("SELECT m.*, c.idnumber, c.shortname
753 FROM {$CFG->prefix}course_meta m,
754 {$CFG->prefix}course c
755 WHERE m.child_course = '$preferences->backup_course' AND
756 m.parent_course = c.id");
757 $childs = get_records_sql ("SELECT m.*, c.idnumber, c.shortname
758 FROM {$CFG->prefix}course_meta m,
759 {$CFG->prefix}course c
760 WHERE m.parent_course = '$preferences->backup_course' AND
761 m.child_course = c.id");
763 if ($parents || $childs) {
764 //metacourse open tag
765 fwrite ($bf,start_tag("METACOURSE",2,true));
766 if ($parents) {
767 fwrite($bf, start_tag("PARENTS",3,true));
768 //Iterate over every parent
769 foreach ($parents as $parent) {
770 //Begin parent
771 fwrite ($bf,start_tag("PARENT",4,true));
772 fwrite ($bf,full_tag("ID",5,false,$parent->parent_course));
773 fwrite ($bf,full_tag("IDNUMBER",5,false,$parent->idnumber));
774 fwrite ($bf,full_tag("SHORTNAME",5,false,$parent->shortname));
775 //End parent
776 fwrite ($bf,end_tag("PARENT",4,true));
778 fwrite ($bf,end_tag("PARENTS",3,true));
780 if ($childs) {
781 fwrite($bf, start_tag("CHILDS",3,true));
782 //Iterate over every child
783 foreach ($childs as $child) {
784 //Begin parent
785 fwrite ($bf,start_tag("CHILD",4,true));
786 fwrite ($bf,full_tag("ID",5,false,$child->child_course));
787 fwrite ($bf,full_tag("IDNUMBER",5,false,$child->idnumber));
788 fwrite ($bf,full_tag("SHORTNAME",5,false,$child->shortname));
789 //End parent
790 fwrite ($bf,end_tag("CHILD",4,true));
792 fwrite ($bf,end_tag("CHILDS",3,true));
794 //metacourse close tag
795 $status = fwrite ($bf,end_tag("METACOURSE",3,true));
798 return $status;
802 //Prints course's messages info (tables message, message_read and message_contacts)
803 function backup_messages ($bf,$preferences) {
805 global $CFG;
807 $status = true;
809 //Get info from messages
810 $unreads = get_records ('message');
811 $reads = get_records ('message_read');
812 $contacts= get_records ('message_contacts');
814 if ($unreads || $reads || $contacts) {
815 $counter = 0;
816 //message open tag
817 fwrite ($bf,start_tag("MESSAGES",2,true));
818 if ($unreads) {
819 //Iterate over every unread
820 foreach ($unreads as $unread) {
821 //start message
822 fwrite($bf, start_tag("MESSAGE",3,true));
823 fwrite ($bf,full_tag("ID",4,false,$unread->id));
824 fwrite ($bf,full_tag("STATUS",4,false,"UNREAD"));
825 fwrite ($bf,full_tag("USERIDFROM",4,false,$unread->useridfrom));
826 fwrite ($bf,full_tag("USERIDTO",4,false,$unread->useridto));
827 fwrite ($bf,full_tag("MESSAGE",4,false,$unread->message));
828 fwrite ($bf,full_tag("FORMAT",4,false,$unread->format));
829 fwrite ($bf,full_tag("TIMECREATED",4,false,$unread->timecreated));
830 fwrite ($bf,full_tag("MESSAGETYPE",4,false,$unread->messagetype));
831 //end message
832 fwrite ($bf,end_tag("MESSAGE",3,true));
834 //Do some output
835 $counter++;
836 if ($counter % 20 == 0) {
837 echo ".";
838 if ($counter % 400 == 0) {
839 echo "<br />";
841 backup_flush(300);
846 if ($reads) {
847 //Iterate over every read
848 foreach ($reads as $read) {
849 //start message
850 fwrite($bf, start_tag("MESSAGE",3,true));
851 fwrite ($bf,full_tag("ID",4,false,$read->id));
852 fwrite ($bf,full_tag("STATUS",4,false,"READ"));
853 fwrite ($bf,full_tag("USERIDFROM",4,false,$read->useridfrom));
854 fwrite ($bf,full_tag("USERIDTO",4,false,$read->useridto));
855 fwrite ($bf,full_tag("MESSAGE",4,false,$read->message));
856 fwrite ($bf,full_tag("FORMAT",4,false,$read->format));
857 fwrite ($bf,full_tag("TIMECREATED",4,false,$read->timecreated));
858 fwrite ($bf,full_tag("MESSAGETYPE",4,false,$read->messagetype));
859 fwrite ($bf,full_tag("TIMEREAD",4,false,$read->timeread));
860 fwrite ($bf,full_tag("MAILED",4,false,$read->mailed));
861 //end message
862 fwrite ($bf,end_tag("MESSAGE",3,true));
864 //Do some output
865 $counter++;
866 if ($counter % 20 == 0) {
867 echo ".";
868 if ($counter % 400 == 0) {
869 echo "<br />";
871 backup_flush(300);
876 if ($contacts) {
877 fwrite($bf, start_tag("CONTACTS",3,true));
878 //Iterate over every contact
879 foreach ($contacts as $contact) {
880 //start contact
881 fwrite($bf, start_tag("CONTACT",4,true));
882 fwrite ($bf,full_tag("ID",5,false,$contact->id));
883 fwrite ($bf,full_tag("USERID",5,false,$contact->userid));
884 fwrite ($bf,full_tag("CONTACTID",5,false,$contact->contactid));
885 fwrite ($bf,full_tag("BLOCKED",5,false,$contact->blocked));
886 //end contact
887 fwrite ($bf,end_tag("CONTACT",4,true));
889 //Do some output
890 $counter++;
891 if ($counter % 20 == 0) {
892 echo ".";
893 if ($counter % 400 == 0) {
894 echo "<br />";
896 backup_flush(300);
899 fwrite($bf, end_tag("CONTACTS",3,true));
901 //messages close tag
902 $status = fwrite ($bf,end_tag("MESSAGES",2,true));
905 return $status;
909 //Prints course's blocks info (table block_instance)
910 function backup_course_blocks ($bf,$preferences) {
912 global $CFG;
914 $status = true;
916 // Read all of the block table
917 $blocks = blocks_get_record();
919 $pages = array();
920 $pages[] = page_create_object(PAGE_COURSE_VIEW, $preferences->backup_course);
922 // Let's see if we have to backup blocks from modules
923 $modulerecords = get_records_sql('SELECT name, id FROM '.$CFG->prefix.'modules');
925 foreach($preferences->mods as $module) {
926 if(!$module->backup) {
927 continue;
930 $cmods = get_records_select('course_modules', 'course = '.$preferences->backup_course.' AND module = '.$modulerecords[$module->name]->id);
931 if(empty($cmods)) {
932 continue;
935 $pagetypes = page_import_types('mod/'.$module->name.'/');
936 if(empty($pagetypes)) {
937 continue;
940 foreach($pagetypes as $pagetype) {
941 foreach($cmods as $cmod) {
942 $pages[] = page_create_object($pagetype, $cmod->instance);
947 //Blocks open tag
948 fwrite ($bf,start_tag('BLOCKS',2,true));
950 while($page = array_pop($pages)) {
951 if ($instances = blocks_get_by_page($page)) {
952 //Iterate over every block
953 foreach ($instances as $position) {
954 foreach ($position as $instance) {
956 //If we somehow have a block with an invalid id, skip it
957 if(empty($blocks[$instance->blockid]->name)) {
958 continue;
960 //Begin Block
962 fwrite ($bf,start_tag('BLOCK',3,true));
963 fwrite ($bf,full_tag('ID', 4, false,$instance->id));
964 fwrite ($bf,full_tag('NAME',4,false,$blocks[$instance->blockid]->name));
965 fwrite ($bf,full_tag('PAGEID',4,false,$instance->pageid));
966 fwrite ($bf,full_tag('PAGETYPE',4,false,$instance->pagetype));
967 fwrite ($bf,full_tag('POSITION',4,false,$instance->position));
968 fwrite ($bf,full_tag('WEIGHT',4,false,$instance->weight));
969 fwrite ($bf,full_tag('VISIBLE',4,false,$instance->visible));
970 fwrite ($bf,full_tag('CONFIGDATA',4,false,$instance->configdata));
972 $context = get_context_instance(CONTEXT_BLOCK, $instance->id);
973 write_role_overrides_xml($bf, $context, 4);
974 /// write role_assign code here
975 write_role_assignments_xml($bf, $context, 4, $preferences);
976 //End Block
977 fwrite ($bf,end_tag('BLOCK',3,true));
983 //Blocks close tag
984 $status = fwrite ($bf,end_tag('BLOCKS',2,true));
986 return $status;
990 //Prints course's sections info (table course_sections)
991 function backup_course_sections ($bf,$preferences) {
993 global $CFG;
995 $status = true;
998 //Get info from sections
999 $section=false;
1000 if ($sections = get_records("course_sections","course",$preferences->backup_course,"section")) {
1001 //Section open tag
1002 fwrite ($bf,start_tag("SECTIONS",2,true));
1003 //Iterate over every section (ordered by section)
1004 foreach ($sections as $section) {
1005 //Begin Section
1006 fwrite ($bf,start_tag("SECTION",3,true));
1007 fwrite ($bf,full_tag("ID",4,false,$section->id));
1008 fwrite ($bf,full_tag("NUMBER",4,false,$section->section));
1009 fwrite ($bf,full_tag("SUMMARY",4,false,$section->summary));
1010 fwrite ($bf,full_tag("VISIBLE",4,false,$section->visible));
1011 //Now print the mods in section
1012 backup_course_modules ($bf,$preferences,$section);
1013 //End section
1014 fwrite ($bf,end_tag("SECTION",3,true));
1016 //Section close tag
1017 $status = fwrite ($bf,end_tag("SECTIONS",2,true));
1020 return $status;
1024 //Prints course's format data (any data the format might want to save).
1025 function backup_format_data ($bf,$preferences) {
1026 global $CFG;
1028 // Check course format
1029 if(!($format=get_field('course','format','id',$preferences->backup_course))) {
1030 return false;
1032 // Write appropriate tag. Note that we always put this tag there even if
1033 // blank, it makes parsing easier
1034 fwrite ($bf,start_tag("FORMATDATA",2,true));
1036 $file=$CFG->dirroot."/course/format/$format/backuplib.php";
1037 if(file_exists($file)) {
1038 // If the file is there, the function must be or it's an error.
1039 require_once($file);
1040 $function=$format.'_backup_format_data';
1041 if(!function_exists($function)) {
1042 return false;
1044 if(!$function($bf,$preferences)) {
1045 return false;
1049 // This last return just checks the file writing has been ok (ish)
1050 return fwrite ($bf,end_tag("FORMATDATA",2,true));
1053 //Prints course's modules info (table course_modules)
1054 //Only for selected mods in preferences
1055 function backup_course_modules ($bf,$preferences,$section) {
1057 global $CFG;
1059 $status = true;
1061 $first_record = true;
1063 //Now print the mods in section
1064 //Extracts mod id from sequence
1065 $tok = strtok($section->sequence,",");
1066 while ($tok) {
1067 //Get module's type
1068 $moduletype = get_module_type ($preferences->backup_course,$tok);
1069 //Check if we've selected to backup that type
1070 if ($moduletype and $preferences->mods[$moduletype]->backup) {
1071 $selected = true;
1072 } else {
1073 $selected = false;
1076 if ($selected) {
1077 $context = get_context_instance(CONTEXT_MODULE, $tok);
1078 //Gets course_module data from db
1079 $course_module = get_records ("course_modules","id",$tok);
1080 //If it's the first, pring MODS tag
1081 if ($first_record) {
1082 fwrite ($bf,start_tag("MODS",4,true));
1083 $first_record = false;
1085 // if we're doing selected instances, check that too.
1086 if (is_array($preferences->mods[$moduletype]->instances)
1087 && count($preferences->mods[$moduletype]->instances)
1088 && (!array_key_exists($course_module[$tok]->instance,$preferences->mods[$moduletype]->instances)
1089 || empty($preferences->mods[$moduletype]->instances[$course_module[$tok]->instance]->backup))) {
1090 $tok = strtok(",");
1091 continue;
1094 // find all role values that has an override in this context
1095 $roles = get_records('role_capabilities', 'contextid', $context->id);
1097 //Print mod info from course_modules
1098 fwrite ($bf,start_tag("MOD",5,true));
1099 //Save neccesary info to backup_ids
1100 fwrite ($bf,full_tag("ID",6,false,$tok));
1101 fwrite ($bf,full_tag("TYPE",6,false,$moduletype));
1102 fwrite ($bf,full_tag("INSTANCE",6,false,$course_module[$tok]->instance));
1103 fwrite ($bf,full_tag("ADDED",6,false,$course_module[$tok]->added));
1104 fwrite ($bf,full_tag("SCORE",6,false,$course_module[$tok]->score));
1105 fwrite ($bf,full_tag("INDENT",6,false,$course_module[$tok]->indent));
1106 fwrite ($bf,full_tag("VISIBLE",6,false,$course_module[$tok]->visible));
1107 fwrite ($bf,full_tag("GROUPMODE",6,false,$course_module[$tok]->groupmode));
1108 fwrite ($bf,full_tag("GROUPINGID",6,false,$course_module[$tok]->groupingid));
1109 fwrite ($bf,full_tag("GROUPMEMBERSONLY",6,false,$course_module[$tok]->groupmembersonly));
1110 // get all the role_capabilities overrides in this mod
1111 write_role_overrides_xml($bf, $context, 6);
1112 /// write role_assign code here
1113 write_role_assignments_xml($bf, $context, 6, $preferences);
1114 /// write role_assign code here
1116 fwrite ($bf,end_tag("MOD",5,true));
1118 //check for next
1119 $tok = strtok(",");
1122 //Si ha habido modulos, final de MODS
1123 if (!$first_record) {
1124 $status =fwrite ($bf,end_tag("MODS",4,true));
1127 return $status;
1130 //Print users to xml
1131 //Only users previously calculated in backup_ids will output
1133 function backup_user_info ($bf,$preferences) {
1135 global $CFG;
1137 $status = true;
1139 // Use a recordset to for the memory handling on to
1140 // the DB and run faster
1141 $users = get_recordset_sql("SELECT b.old_id, b.table_name, b.info,
1142 u.*, m.wwwroot
1143 FROM {$CFG->prefix}backup_ids b
1144 JOIN {$CFG->prefix}user u ON b.old_id=u.id
1145 JOIN {$CFG->prefix}mnet_host m ON u.mnethostid=m.id
1146 WHERE b.backup_code = '$preferences->backup_unique_code' AND
1147 b.table_name = 'user'");
1149 //If we have users to backup
1150 if ($users && $users->RecordCount()) {
1151 //Begin Users tag
1152 fwrite ($bf,start_tag("USERS",2,true));
1153 $counter = 0;
1154 //With every user
1155 while ($user = $users->FetchNextObj()) {
1156 //Begin User tag
1157 fwrite ($bf,start_tag("USER",3,true));
1158 //Output all user data
1159 fwrite ($bf,full_tag("ID",4,false,$user->id));
1160 fwrite ($bf,full_tag("AUTH",4,false,$user->auth));
1161 fwrite ($bf,full_tag("CONFIRMED",4,false,$user->confirmed));
1162 fwrite ($bf,full_tag("POLICYAGREED",4,false,$user->policyagreed));
1163 fwrite ($bf,full_tag("DELETED",4,false,$user->deleted));
1164 fwrite ($bf,full_tag("USERNAME",4,false,$user->username));
1165 fwrite ($bf,full_tag("PASSWORD",4,false,$user->password));
1166 fwrite ($bf,full_tag("IDNUMBER",4,false,$user->idnumber));
1167 fwrite ($bf,full_tag("FIRSTNAME",4,false,$user->firstname));
1168 fwrite ($bf,full_tag("LASTNAME",4,false,$user->lastname));
1169 fwrite ($bf,full_tag("EMAIL",4,false,$user->email));
1170 fwrite ($bf,full_tag("EMAILSTOP",4,false,$user->emailstop));
1171 fwrite ($bf,full_tag("ICQ",4,false,$user->icq));
1172 fwrite ($bf,full_tag("SKYPE",4,false,$user->skype));
1173 fwrite ($bf,full_tag("YAHOO",4,false,$user->yahoo));
1174 fwrite ($bf,full_tag("AIM",4,false,$user->aim));
1175 fwrite ($bf,full_tag("MSN",4,false,$user->msn));
1176 fwrite ($bf,full_tag("PHONE1",4,false,$user->phone1));
1177 fwrite ($bf,full_tag("PHONE2",4,false,$user->phone2));
1178 fwrite ($bf,full_tag("INSTITUTION",4,false,$user->institution));
1179 fwrite ($bf,full_tag("DEPARTMENT",4,false,$user->department));
1180 fwrite ($bf,full_tag("ADDRESS",4,false,$user->address));
1181 fwrite ($bf,full_tag("CITY",4,false,$user->city));
1182 fwrite ($bf,full_tag("COUNTRY",4,false,$user->country));
1183 fwrite ($bf,full_tag("LANG",4,false,$user->lang));
1184 fwrite ($bf,full_tag("THEME",4,false,$user->theme));
1185 fwrite ($bf,full_tag("TIMEZONE",4,false,$user->timezone));
1186 fwrite ($bf,full_tag("FIRSTACCESS",4,false,$user->firstaccess));
1187 fwrite ($bf,full_tag("LASTACCESS",4,false,$user->lastaccess));
1188 fwrite ($bf,full_tag("LASTLOGIN",4,false,$user->lastlogin));
1189 fwrite ($bf,full_tag("CURRENTLOGIN",4,false,$user->currentlogin));
1190 fwrite ($bf,full_tag("LASTIP",4,false,$user->lastip));
1191 fwrite ($bf,full_tag("SECRET",4,false,$user->secret));
1192 fwrite ($bf,full_tag("PICTURE",4,false,$user->picture));
1193 fwrite ($bf,full_tag("URL",4,false,$user->url));
1194 fwrite ($bf,full_tag("DESCRIPTION",4,false,$user->description));
1195 fwrite ($bf,full_tag("MAILFORMAT",4,false,$user->mailformat));
1196 fwrite ($bf,full_tag("MAILDIGEST",4,false,$user->maildigest));
1197 fwrite ($bf,full_tag("MAILDISPLAY",4,false,$user->maildisplay));
1198 fwrite ($bf,full_tag("HTMLEDITOR",4,false,$user->htmleditor));
1199 fwrite ($bf,full_tag("AJAX",4,false,$user->ajax));
1200 fwrite ($bf,full_tag("AUTOSUBSCRIBE",4,false,$user->autosubscribe));
1201 fwrite ($bf,full_tag("TRACKFORUMS",4,false,$user->trackforums));
1202 if ($user->mnethostid != $CFG->mnet_localhost_id) {
1203 fwrite ($bf,full_tag("MNETHOSTURL",4,false,$user->wwwroot));
1205 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$user->timemodified));
1207 /// write assign/override code for context_userid
1209 $user->isneeded = strpos($user->info,"needed");
1210 //Output every user role (with its associated info)
1212 $user->isadmin = strpos($user->info,"admin");
1213 $user->iscoursecreator = strpos($user->info,"coursecreator");
1214 $user->isteacher = strpos($user->info,"teacher");
1215 $user->isstudent = strpos($user->info,"student");
1218 if ($user->isadmin!==false or
1219 $user->iscoursecreator!==false or
1220 $user->isteacher!==false or
1221 $user->isstudent!==false or
1222 $user->isneeded!==false) {
1224 fwrite ($bf,start_tag("ROLES",4,true));
1225 if ($user->info != "needed" && $user->info!="") {
1226 //Begin ROLES tag
1228 //PRINT ROLE INFO
1229 //Admins
1230 $roles = explode(",", $user->info);
1231 foreach ($roles as $role) {
1232 if ($role!="" && $role!="needed") {
1233 fwrite ($bf,start_tag("ROLE",5,true));
1234 //Print Role info
1235 fwrite ($bf,full_tag("TYPE",6,false,$role));
1236 //Print ROLE end
1237 fwrite ($bf,end_tag("ROLE",5,true));
1241 //Needed
1242 if ($user->isneeded!==false) {
1243 //Print ROLE start
1244 fwrite ($bf,start_tag("ROLE",5,true));
1245 //Print Role info
1246 fwrite ($bf,full_tag("TYPE",6,false,"needed"));
1247 //Print ROLE end
1248 fwrite ($bf,end_tag("ROLE",5,true));
1251 //End ROLES tag
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, $preferences);
1274 //End User tag
1275 fwrite ($bf,end_tag("USER",3,true));
1276 //Do some output
1277 $counter++;
1278 if ($counter % 10 == 0) {
1279 echo ".";
1280 if ($counter % 200 == 0) {
1281 echo "<br />";
1283 backup_flush(300);
1286 //End Users tag
1287 fwrite ($bf,end_tag("USERS",2,true));
1288 } else {
1289 // There aren't any users.
1290 $status = true;
1293 return $status;
1296 //Backup log info (time ordered)
1297 function backup_log_info($bf,$preferences) {
1299 global $CFG;
1301 //Number of records to get in every chunk
1302 $recordset_size = 1000;
1304 $status = true;
1306 //Counter, points to current record
1307 $counter = 0;
1309 //Count records
1310 $count_logs = count_records("log","course",$preferences->backup_course);
1312 //Pring logs header
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);
1320 //We have logs
1321 if ($logs) {
1322 //Iterate
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
1329 //Begin log tag
1330 fwrite ($bf,start_tag("LOG",3,true));
1332 //Output log tag
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));
1343 //End log tag
1344 fwrite ($bf,end_tag("LOG",3,true));
1346 //Do some output
1347 $counter++;
1348 if ($counter % 20 == 0) {
1349 echo ".";
1350 if ($counter % 400 == 0) {
1351 echo "<br />";
1353 backup_flush(300);
1358 //End logs tag
1359 if ($count_logs > 0 ) {
1360 $status = fwrite ($bf,end_tag("LOGS",2,true));
1362 return $status;
1365 //Backup gradebook info
1366 function backup_gradebook_info($bf,$preferences) {
1368 global $CFG;
1369 $status = true;
1371 // see if ALL grade items of type mod of this course are being backed up
1372 // if not, we do not need to backup grade category and associated grade items/grades
1373 $backupall = true;
1375 if ($grade_items = get_records_sql("SELECT * FROM {$CFG->prefix}grade_items
1376 WHERE courseid = $preferences->backup_course
1377 AND itemtype != '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);
1388 $backupall = false;
1389 break;
1395 //Gradebook header
1396 fwrite ($bf,start_tag("GRADEBOOK",2,true));
1398 // Now backup grade_item (inside grade_category)
1399 if ($backupall) {
1400 $status = backup_gradebook_category_info($bf,$preferences);
1403 $status = backup_gradebook_item_info($bf,$preferences, $backupall);
1404 $status = backup_gradebook_outcomes_info($bf, $preferences);
1405 $status = backup_gradebook_outcomes_courses_info($bf, $preferences);
1407 // backup gradebook histories
1408 if ($preferences->backup_gradebook_history) {
1409 $status = backup_gradebook_categories_history_info($bf, $preferences);
1410 $status = backup_gradebook_grades_history_info($bf, $preferences);
1411 $status = backup_gradebook_items_history_info($bf, $preferences);
1412 $status = backup_gradebook_outcomes_history($bf, $preferences);
1415 //Gradebook footer
1416 $status = fwrite ($bf,end_tag("GRADEBOOK",2,true));
1417 return $status;
1420 function backup_gradebook_category_info($bf,$preferences) {
1421 global $CFG;
1422 $status = true;
1424 // getting grade categories, but make sure parents come before children
1425 // because when we do restore, we need to recover the parents first
1426 // we do this by getting the lowest depth first
1427 $grade_categories = get_records_sql("SELECT * FROM {$CFG->prefix}grade_categories
1428 WHERE courseid = $preferences->backup_course
1429 AND depth > 1
1430 ORDER BY depth ASC");
1431 if ($grade_categories) {
1432 //Begin grade_categories tag
1433 fwrite ($bf,start_tag("GRADE_CATEGORIES",3,true));
1434 //Iterate for each category
1435 foreach ($grade_categories as $grade_category) {
1436 //Begin grade_category
1437 fwrite ($bf,start_tag("GRADE_CATEGORY",4,true));
1438 //Output individual fields
1439 fwrite ($bf,full_tag("ID",5,false,$grade_category->id));
1441 // not keeping path and depth because they can be derived
1442 fwrite ($bf,full_tag("PARENT",5,false,$grade_category->parent));
1443 fwrite ($bf,full_tag("FULLNAME",5,false,$grade_category->fullname));
1444 fwrite ($bf,full_tag("AGGREGATION",5,false,$grade_category->aggregation));
1445 fwrite ($bf,full_tag("KEEPHIGH",5,false,$grade_category->keephigh));
1446 fwrite ($bf,full_tag("DROPLOW",5,false,$grade_category->droplow));
1447 fwrite ($bf,full_tag("AGGREGATEONLYGRADED",5,false,$grade_category->aggregateonlygraded));
1448 fwrite ($bf,full_tag("AGGREGATEOUTCOMES",5,false,$grade_category->aggregateoutcomes));
1449 fwrite ($bf,full_tag("AGGREGATESUBCATS",5,false,$grade_category->aggregatesubcats));
1450 fwrite ($bf,full_tag("TIMECREATED",5,false,$grade_category->timecreated));
1451 fwrite ($bf,full_tag("TIMEMODIFIED",5,false,$grade_category->timemodified));
1453 //End grade_category
1454 fwrite ($bf,end_tag("GRADE_CATEGORY",4,true));
1456 //End grade_categories tag
1457 $status = fwrite ($bf,end_tag("GRADE_CATEGORIES",3,true));
1460 return $status;
1463 //Backup gradebook_item (called from backup_gradebook_info
1464 function backup_gradebook_item_info($bf,$preferences, $backupall) {
1466 global $CFG;
1467 require_once($CFG->libdir . '/gradelib.php');
1468 $status = true;
1469 // get all the grade_items, ordered by sort order since upon restoring, it is not always
1470 // possible to use the same sort order. We could at least preserve the sortorder by restoring
1471 // grade_items in the original sortorder
1472 if ($grade_items = get_records_sql("SELECT * FROM {$CFG->prefix}grade_items
1473 WHERE courseid = $preferences->backup_course
1474 ORDER BY sortorder ASC")) {
1476 //Begin grade_items tag
1477 fwrite ($bf,start_tag("GRADE_ITEMS",3,true));
1478 //Iterate for each item
1479 foreach ($grade_items as $grade_item) {
1480 // Instantiate a grade_item object, to access its methods
1481 $grade_item = grade_object::fetch_helper('grade_items', 'grade_item', $grade_item);
1483 // do not restore if this grade_item is a mod, and
1484 if ($grade_item->itemtype == 'mod') {
1485 // this still needs to be included, though grades can be ignored
1486 } else if ($grade_item->itemtype == 'category') {
1487 // if not all grade items are being backed up
1488 // we ignore this type of grade_item and grades associated
1489 if (!$backupall) {
1490 continue;
1494 //Begin grade_item
1495 fwrite ($bf,start_tag("GRADE_ITEM",4,true));
1496 //Output individual fields
1498 fwrite ($bf,full_tag("ID",5,false,$grade_item->id));
1499 fwrite ($bf,full_tag("CATEGORYID",5,false,$grade_item->categoryid));
1500 fwrite ($bf,full_tag("ITEMNAME",5,false,$grade_item->itemname));
1501 fwrite ($bf,full_tag("ITEMTYPE",5,false,$grade_item->itemtype));
1502 fwrite ($bf,full_tag("ITEMMODULE",5,false,$grade_item->itemmodule));
1503 fwrite ($bf,full_tag("ITEMINSTANCE",5,false,$grade_item->iteminstance));
1504 fwrite ($bf,full_tag("ITEMNUMBER",5,false,$grade_item->itemnumber));
1505 fwrite ($bf,full_tag("ITEMINFO",5,false,$grade_item->iteminfo));
1506 fwrite ($bf,full_tag("IDNUMBER",5,false,$grade_item->idnumber));
1507 // use [idnumber] in calculation instead of [#giXXX#]
1508 fwrite ($bf,full_tag("CALCULATION",5,false,$grade_item->get_calculation()));
1509 fwrite ($bf,full_tag("GRADETYPE",5,false,$grade_item->gradetype));
1510 fwrite ($bf,full_tag("GRADEMAX",5,false,$grade_item->grademax));
1511 fwrite ($bf,full_tag("GRADEMIN",5,false,$grade_item->grademin));
1512 fwrite ($bf,full_tag("SCALEID",5,false,$grade_item->scaleid));
1513 fwrite ($bf,full_tag("OUTCOMEID",5,false,$grade_item->outcomeid));
1514 fwrite ($bf,full_tag("GRADEPASS",5,false,$grade_item->gradepass));
1515 fwrite ($bf,full_tag("MULTFACTOR",5,false,$grade_item->multfactor));
1516 fwrite ($bf,full_tag("PLUSFACTOR",5,false,$grade_item->plusfactor));
1517 fwrite ($bf,full_tag("AGGREGATIONCOEF",5,false,$grade_item->aggregationcoef));
1518 fwrite ($bf,full_tag("DISPLAY",5,false,$grade_item->plusfactor));
1519 fwrite ($bf,full_tag("DECIMALS",5,false,$grade_item->plusfactor));
1520 fwrite ($bf,full_tag("HIDDEN",5,false,$grade_item->hidden));
1521 fwrite ($bf,full_tag("LOCKED",5,false,$grade_item->locked));
1522 fwrite ($bf,full_tag("LOCKTIME",5,false,$grade_item->locktime));
1523 fwrite ($bf,full_tag("NEEDSUPDATE",5,false,$grade_item->needsupdate));
1524 fwrite ($bf,full_tag("TIMECREATED",5,false,$grade_item->timecreated));
1525 fwrite ($bf,full_tag("TIMEMODIFIED",5,false,$grade_item->timemodified));
1527 // back up the other stuff here
1528 // mod grades should only be backed up if selected
1529 if ($grade_item->itemtype == 'mod' && !backup_userdata_selected($preferences,$grade_item->itemmodule,$grade_item->iteminstance)) {
1530 // do not write grades if a mod grade_item is being restored
1531 // but userdata is not selected
1532 } else {
1533 $status = backup_gradebook_grades_info($bf,$preferences,$grade_item->id);
1535 //End grade_item
1536 fwrite ($bf,end_tag("GRADE_ITEM",4,true));
1538 //End grade_items tag
1539 $status = fwrite ($bf,end_tag("GRADE_ITEMS",3,true));
1542 return $status;
1544 //Backup gradebook_item (called from backup_gradebook_info
1546 function backup_gradebook_outcomes_info($bf,$preferences) {
1548 global $CFG;
1549 $status = true;
1550 // only back up courses already in the grade_outcomes_courses table
1551 $grade_outcomes = get_records_sql('SELECT go.*
1552 FROM '.$CFG->prefix.'grade_outcomes_courses goc,
1553 '.$CFG->prefix.'grade_outcomes go
1554 WHERE goc.courseid = '.$preferences->backup_course.'
1555 AND goc.outcomeid = go.id');
1557 if (!empty($grade_outcomes)) {
1558 //Begin grade_outcomes tag
1559 fwrite ($bf,start_tag("GRADE_OUTCOMES",3,true));
1560 //Iterate for each outcome
1561 foreach ($grade_outcomes as $grade_outcome) {
1562 //Begin grade_outcome
1563 fwrite ($bf,start_tag("GRADE_OUTCOME",4,true));
1564 //Output individual fields
1566 fwrite ($bf,full_tag("ID",5,false,$grade_outcome->id));
1567 fwrite ($bf,full_tag("COURSEID",5,false,$grade_outcome->courseid));
1568 fwrite ($bf,full_tag("SHORTNAME",5,false,$grade_outcome->shortname));
1569 fwrite ($bf,full_tag("FULLNAME",5,false,$grade_outcome->fullname));
1570 fwrite ($bf,full_tag("SCALEID",5,false,$grade_outcome->scaleid));
1571 fwrite ($bf,full_tag("DESCRIPTION",5,false,$grade_outcome->description));
1572 fwrite ($bf,full_tag("USERMODIFIED",5,false,$grade_outcome->usermodified));
1574 //End grade_outcome
1575 fwrite ($bf,end_tag("GRADE_OUTCOME",4,true));
1577 //End grade_outcomes tag
1578 $status = fwrite ($bf,end_tag("GRADE_OUTCOMES",3,true));
1580 return $status;
1583 // outcomes assigned to this course
1584 function backup_gradebook_outcomes_courses_info($bf,$preferences) {
1586 global $CFG;
1588 $status = true;
1589 // get all global outcomes (used in this course)
1590 // and course specific outcomes
1591 // we don't need to backup all the outcomes in this case
1592 if ($outcomes_courses = get_records('grade_outcomes_courses', 'courseid', $preferences->backup_course)) {
1593 //Begin grade_outcomes tag
1594 fwrite ($bf,start_tag("GRADE_OUTCOMES_COURSES",3,true));
1595 //Iterate for each outcome
1596 foreach ($outcomes_courses as $outcomes_course) {
1597 //Begin grade_outcome
1598 fwrite ($bf,start_tag("GRADE_OUTCOMES_COURSE",4,true));
1599 //Output individual fields
1600 fwrite ($bf,full_tag("ID",5,false,$outcomes_course->id));
1601 fwrite ($bf,full_tag("OUTCOMEID",5,false,$outcomes_course->outcomeid));
1603 //End grade_outcome
1604 fwrite ($bf,end_tag("GRADE_OUTCOMES_COURSE",4,true));
1606 //End grade_outcomes tag
1607 $status = fwrite ($bf,end_tag("GRADE_OUTCOMES_COURSES",3,true));
1609 return $status;
1612 function backup_gradebook_grades_info($bf,$preferences, $itemid) {
1614 global $CFG;
1616 $status = true;
1618 // find all grades belonging to this item
1619 if ($grades = get_records('grade_grades', 'itemid', $itemid)) {
1620 fwrite ($bf,start_tag("GRADE_GRADES",5,true));
1621 foreach ($grades as $grade) {
1622 fwrite ($bf,start_tag("GRADE",6,true));
1623 fwrite ($bf,full_tag("ID",7,false,$grade->id));
1624 fwrite ($bf,full_tag("USERID",7,false,$grade->userid));
1625 fwrite ($bf,full_tag("RAWGRADE",7,false,$grade->rawgrade));
1626 fwrite ($bf,full_tag("RAWGRADEMAX",7,false,$grade->rawgrademax));
1627 fwrite ($bf,full_tag("RAWGRADEMIN",7,false,$grade->rawgrademin));
1628 fwrite ($bf,full_tag("RAWSCALEID",7,false,$grade->rawscaleid));
1629 fwrite ($bf,full_tag("USERMODIFIED",7,false,$grade->usermodified));
1630 fwrite ($bf,full_tag("FINALGRADE",7,false,$grade->finalgrade));
1631 fwrite ($bf,full_tag("HIDDEN",7,false,$grade->hidden));
1632 fwrite ($bf,full_tag("LOCKED",7,false,$grade->locked));
1633 fwrite ($bf,full_tag("LOCKTIME",7,false,$grade->locktime));
1634 fwrite ($bf,full_tag("EXPORTED",7,false,$grade->exported));
1635 fwrite ($bf,full_tag("OVERRIDDEN",7,false,$grade->overridden));
1636 fwrite ($bf,full_tag("EXCLUDED",7,false,$grade->excluded));
1637 fwrite ($bf,full_tag("FEEDBACK",7,false,$grade->feedback));
1638 fwrite ($bf,full_tag("FEEDBACKFORMAT",7,false,$grade->feedbackformat));
1639 fwrite ($bf,full_tag("INFORMATION",7,false,$grade->information));
1640 fwrite ($bf,full_tag("INFORMATIONFORMAT",7,false,$grade->informationformat));
1641 fwrite ($bf,full_tag("TIMECREATED",7,false,$grade->timecreated));
1642 fwrite ($bf,full_tag("TIMEMODIFIED",7,false,$grade->timemodified));
1643 fwrite ($bf,end_tag("GRADE",6,true));
1645 $status = fwrite ($bf,end_tag("GRADE_GRADES",5,true));
1647 return $status;
1650 function backup_gradebook_categories_history_info($bf, $preferences) {
1652 global $CFG;
1653 $status = true;
1655 // find all grade categories history
1656 if ($chs = get_records('grade_categories_history', 'courseid', $preferences->backup_course)) {
1657 fwrite ($bf,start_tag("GRADE_CATEGORIES_HISTORIES",5,true));
1658 foreach ($chs as $ch) {
1659 fwrite ($bf,start_tag("GRADE_CATEGORIES_HISTORY",6,true));
1660 fwrite ($bf,full_tag("ID",7,false,$ch->id));
1661 fwrite ($bf,full_tag("OLDID",7,false,$ch->oldid));
1662 fwrite ($bf,full_tag("ACTION",7,false,$ch->action));
1663 fwrite ($bf,full_tag("SOURCE",7,false,$ch->source));
1664 fwrite ($bf,full_tag("TIMEMODIFIED",7,false,$ch->timemodified));
1665 fwrite ($bf,full_tag("LOGGEDUSER",7,false,$ch->loggeduser));
1666 fwrite ($bf,full_tag("PARENT",7,false,$ch->parent));
1667 fwrite ($bf,full_tag("DEPTH",7,false,$ch->depth));
1668 fwrite ($bf,full_tag("PATH",7,false,$ch->path));
1669 fwrite ($bf,full_tag("FULLNAME",7,false,$ch->fullname));
1670 fwrite ($bf,full_tag("AGGRETGATION",7,false,$ch->aggregation));
1671 fwrite ($bf,full_tag("KEEPHIGH",7,false,$ch->keephigh));
1672 fwrite ($bf,full_tag("DROPLOW",7,false,$ch->droplow));
1673 fwrite ($bf,full_tag("AGGREGATEONLYGRADED",7,false,$ch->aggregateonlygraded));
1674 fwrite ($bf,full_tag("AGGREGATEOUTCOMES",7,false,$ch->aggregateoutcomes));
1675 fwrite ($bf,full_tag("AGGREGATESUBCATS",7,false,$ch->aggregatesubcats));
1676 fwrite ($bf,end_tag("GRADE_CATEGORIES_HISTORY",6,true));
1678 $status = fwrite ($bf,end_tag("GRADE_CATEGORIES_HISTORIES",5,true));
1680 return $status;
1683 function backup_gradebook_grades_history_info($bf, $preferences) {
1685 global $CFG;
1686 $status = true;
1688 // find all grade categories history
1689 if ($chs = get_records_sql("SELECT ggh.* FROM {$CFG->prefix}grade_grades_history ggh,
1690 {$CFG->prefix}grade_items gi
1691 WHERE gi.courseid = $preferences->backup_course
1692 AND ggh.itemid = gi.id")) {
1693 fwrite ($bf,start_tag("GRADE_GRADES_HISTORIES",5,true));
1694 foreach ($chs as $ch) {
1695 fwrite ($bf,start_tag("GRADE_GRADES_HISTORY",6,true));
1696 fwrite ($bf,full_tag("ID",7,false,$ch->id));
1697 fwrite ($bf,full_tag("OLDID",7,false,$ch->oldid));
1698 fwrite ($bf,full_tag("ACTION",7,false,$ch->action));
1699 fwrite ($bf,full_tag("SOURCE",7,false,$ch->source));
1700 fwrite ($bf,full_tag("TIMEMODIFIED",7,false,$ch->timemodified));
1701 fwrite ($bf,full_tag("LOGGEDUSER",7,false,$ch->loggeduser));
1702 fwrite ($bf,full_tag("ITEMID",7,false,$ch->itemid));
1703 fwrite ($bf,full_tag("USERID",7,false,$ch->userid));
1704 fwrite ($bf,full_tag("RAWGRADE",7,false,$ch->rawgrade));
1705 fwrite ($bf,full_tag("RAWGRADEMAX",7,false,$ch->rawgrademax));
1706 fwrite ($bf,full_tag("RAWGRADEMIN",7,false,$ch->rawgrademin));
1707 fwrite ($bf,full_tag("USERMODIFIED",7,false,$ch->usermodified));
1708 fwrite ($bf,full_tag("FINALGRADE",7,false,$ch->finalgrade));
1709 fwrite ($bf,full_tag("HIDDEN",7,false,$ch->hidden));
1710 fwrite ($bf,full_tag("LOCKED",7,false,$ch->locked));
1711 fwrite ($bf,full_tag("LOCKTIME",7,false,$ch->locktime));
1712 fwrite ($bf,full_tag("EXPORTED",7,false,$ch->exported));
1713 fwrite ($bf,full_tag("OVERRIDDEN",7,false,$ch->overridden));
1714 fwrite ($bf,full_tag("EXCLUDED",7,false,$ch->excluded));
1715 fwrite ($bf,full_tag("FEEDBACK",7,false,$ch->feedback));
1716 fwrite ($bf,full_tag("FEEDBACKFORMAT",7,false,$ch->feedbackformat));
1717 fwrite ($bf,full_tag("INFORMATION",7,false,$ch->information));
1718 fwrite ($bf,full_tag("INFORMATIONFORMAT",7,false,$ch->informationformat));
1719 fwrite ($bf,end_tag("GRADE_GRADES_HISTORY",6,true));
1721 $status = fwrite ($bf,end_tag("GRADE_GRADES_HISTORIES",5,true));
1723 return $status;
1726 function backup_gradebook_items_history_info($bf, $preferences) {
1728 global $CFG;
1729 $status = true;
1731 // find all grade categories history
1732 if ($chs = get_records('grade_items_history','courseid', $preferences->backup_course)) {
1733 fwrite ($bf,start_tag("GRADE_ITEM_HISTORIES",5,true));
1734 foreach ($chs as $ch) {
1735 fwrite ($bf,start_tag("GRADE_ITEM_HISTORY",6,true));
1736 fwrite ($bf,full_tag("ID",7,false,$ch->id));
1737 fwrite ($bf,full_tag("OLDID",7,false,$ch->oldid));
1738 fwrite ($bf,full_tag("ACTION",7,false,$ch->action));
1739 fwrite ($bf,full_tag("SOURCE",7,false,$ch->source));
1740 fwrite ($bf,full_tag("TIMEMODIFIED",7,false,$ch->timemodified));
1741 fwrite ($bf,full_tag("LOGGEDUSER",7,false,$ch->loggeduser));
1742 fwrite ($bf,full_tag("CATEGORYID",7,false,$ch->categoryid));
1743 fwrite ($bf,full_tag("ITEMNAME",7,false,$ch->itemname));
1744 fwrite ($bf,full_tag("ITEMTYPE",7,false,$ch->itemtype));
1745 fwrite ($bf,full_tag("ITEMMODULE",7,false,$ch->itemmodule));
1746 fwrite ($bf,full_tag("ITEMINSTANCE",7,false,$ch->iteminstance));
1747 fwrite ($bf,full_tag("ITEMNUMBER",7,false,$ch->itemnumber));
1748 fwrite ($bf,full_tag("ITEMINFO",7,false,$ch->iteminfo));
1749 fwrite ($bf,full_tag("IDNUMBER",7,false,$ch->idnumber));
1750 fwrite ($bf,full_tag("CALCULATION",7,false,$ch->calculation));
1751 fwrite ($bf,full_tag("GRADETYPE",7,false,$ch->gradetype));
1752 fwrite ($bf,full_tag("GRADEMAX",7,false,$ch->grademax));
1753 fwrite ($bf,full_tag("GRADEMIN",7,false,$ch->grademin));
1754 fwrite ($bf,full_tag("SCALEID",7,false,$ch->scaleid));
1755 fwrite ($bf,full_tag("OUTCOMEID",7,false,$ch->outcomeid));
1756 fwrite ($bf,full_tag("GRADEPASS",7,false,$ch->gradepass));
1757 fwrite ($bf,full_tag("MULTFACTOR",7,false,$ch->multfactor));
1758 fwrite ($bf,full_tag("PLUSFACTOR",7,false,$ch->plusfactor));
1759 fwrite ($bf,full_tag("AGGREGATIONCOEF",7,false,$ch->aggregationcoef));
1760 fwrite ($bf,full_tag("SORTORDER",7,false,$ch->sortorder));
1761 fwrite ($bf,full_tag("DISPLAY",7,false,$ch->plusfactor));
1762 fwrite ($bf,full_tag("DECIMALS",7,false,$ch->plusfactor));
1763 fwrite ($bf,full_tag("HIDDEN",7,false,$ch->hidden));
1764 fwrite ($bf,full_tag("LOCKED",7,false,$ch->locked));
1765 fwrite ($bf,full_tag("LOCKTIME",7,false,$ch->locktime));
1766 fwrite ($bf,full_tag("NEEDSUPDATE",7,false,$ch->needsupdate));
1767 fwrite ($bf,end_tag("GRADE_ITEM_HISTORY",6,true));
1769 $status = fwrite ($bf,end_tag("GRADE_ITEM_HISTORIES",5,true));
1772 return $status;
1775 function backup_gradebook_outcomes_history($bf, $preferences) {
1776 global $CFG;
1777 $status = true;
1779 // find all grade categories history
1780 if ($chs = get_records('grade_outcomes_history','courseid', $preferences->backup_course)) {
1781 fwrite ($bf,start_tag("GRADE_OUTCOME_HISTORIES",5,true));
1782 foreach ($chs as $ch) {
1783 fwrite ($bf,start_tag("GRADE_OUTCOME_HISTORY",6,true));
1784 fwrite ($bf,full_tag("ID",7,false,$ch->id));
1785 fwrite ($bf,full_tag("OLDID",7,false,$ch->oldid));
1786 fwrite ($bf,full_tag("ACTION",7,false,$ch->action));
1787 fwrite ($bf,full_tag("SOURCE",7,false,$ch->source));
1788 fwrite ($bf,full_tag("TIMEMODIFIED",7,false,$ch->timemodified));
1789 fwrite ($bf,full_tag("LOGGEDUSER",7,false,$ch->loggeduser));
1790 fwrite ($bf,full_tag("SHORTNAME",7,false,$ch->shortname));
1791 fwrite ($bf,full_tag("FULLNAME",7,false,$ch->fullname));
1792 fwrite ($bf,full_tag("SCALEID",7,false,$ch->scaleid));
1793 fwrite ($bf,full_tag("DESCRIPTION",7,false,$ch->description));
1794 fwrite ($bf,end_tag("GRADE_OUTCOME_HISTORY",6,true));
1796 $status = fwrite ($bf,end_tag("GRADE_OUTCOME_HISTORIES",5,true));
1798 return $status;
1801 //Backup scales info (common and course scales)
1802 function backup_scales_info($bf,$preferences) {
1804 global $CFG;
1806 $status = true;
1808 //Counter, points to current record
1809 $counter = 0;
1811 //Get scales (common and course scales)
1812 $scales = get_records_sql("SELECT id, courseid, userid, name, scale, description, timemodified
1813 FROM {$CFG->prefix}scale
1814 WHERE courseid = '0' or courseid = $preferences->backup_course");
1816 //Copy only used scales to $backupscales. They will be in backup (unused no). See Bug 1223.
1817 $backupscales = array();
1818 if ($scales) {
1819 foreach ($scales as $scale) {
1820 if (course_scale_used($preferences->backup_course, $scale->id)) {
1821 $backupscales[] = $scale;
1826 //Pring scales header
1827 if ($backupscales) {
1828 //Pring scales header
1829 fwrite ($bf,start_tag("SCALES",2,true));
1830 //Iterate
1831 foreach ($backupscales as $scale) {
1832 //Begin scale tag
1833 fwrite ($bf,start_tag("SCALE",3,true));
1834 //Output scale tag
1835 fwrite ($bf,full_tag("ID",4,false,$scale->id));
1836 fwrite ($bf,full_tag("COURSEID",4,false,$scale->courseid));
1837 fwrite ($bf,full_tag("USERID",4,false,$scale->userid));
1838 fwrite ($bf,full_tag("NAME",4,false,$scale->name));
1839 fwrite ($bf,full_tag("SCALETEXT",4,false,$scale->scale));
1840 fwrite ($bf,full_tag("DESCRIPTION",4,false,$scale->description));
1841 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$scale->timemodified));
1842 //End scale tag
1843 fwrite ($bf,end_tag("SCALE",3,true));
1845 //End scales tag
1846 $status = fwrite ($bf,end_tag("SCALES",2,true));
1848 return $status;
1851 //Backup events info (course events)
1852 function backup_events_info($bf,$preferences) {
1854 global $CFG;
1856 $status = true;
1858 //Counter, points to current record
1859 $counter = 0;
1861 //Get events (course events)
1862 $events = get_records_select("event","courseid='$preferences->backup_course' AND instance='0'","id");
1864 //Pring events header
1865 if ($events) {
1866 //Pring events header
1867 fwrite ($bf,start_tag("EVENTS",2,true));
1868 //Iterate
1869 foreach ($events as $event) {
1870 //Begin event tag
1871 fwrite ($bf,start_tag("EVENT",3,true));
1872 //Output event tag
1873 fwrite ($bf,full_tag("ID",4,false,$event->id));
1874 fwrite ($bf,full_tag("NAME",4,false,$event->name));
1875 fwrite ($bf,full_tag("DESCRIPTION",4,false,$event->description));
1876 fwrite ($bf,full_tag("FORMAT",4,false,$event->format));
1877 fwrite ($bf,full_tag("GROUPID",4,false,$event->groupid));
1878 fwrite ($bf,full_tag("USERID",4,false,$event->userid));
1879 fwrite ($bf,full_tag("REPEATID",4,false,$event->repeatid));
1880 fwrite ($bf,full_tag("EVENTTYPE",4,false,$event->eventtype));
1881 fwrite ($bf,full_tag("MODULENAME",4,false,$event->modulename));
1882 fwrite ($bf,full_tag("TIMESTART",4,false,$event->timestart));
1883 fwrite ($bf,full_tag("TIMEDURATION",4,false,$event->timeduration));
1884 fwrite ($bf,full_tag("VISIBLE",4,false,$event->visible));
1885 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$event->timemodified));
1886 //End event tag
1887 fwrite ($bf,end_tag("EVENT",3,true));
1889 //End events tag
1890 $status = fwrite ($bf,end_tag("EVENTS",2,true));
1892 return $status;
1895 //Backup groups info
1896 function backup_groups_info($bf,$preferences) {
1898 global $CFG;
1900 $status = true;
1901 $status2 = true;
1903 //Get groups
1904 $groups = get_records("groups","courseid",$preferences->backup_course);
1906 //Pring groups header
1907 if ($groups) {
1908 //Pring groups header
1909 fwrite ($bf,start_tag("GROUPS",2,true));
1910 //Iterate
1911 foreach ($groups as $group) {
1912 //Begin group tag
1913 fwrite ($bf,start_tag("GROUP",3,true));
1914 //Output group contents
1915 fwrite ($bf,full_tag("ID",4,false,$group->id));
1916 //fwrite ($bf,full_tag("COURSEID",4,false,$group->courseid));
1917 fwrite ($bf,full_tag("NAME",4,false,$group->name));
1918 fwrite ($bf,full_tag("DESCRIPTION",4,false,$group->description));
1919 fwrite ($bf,full_tag("ENROLMENTKEY",4,false,$group->enrolmentkey));
1920 fwrite ($bf,full_tag("PICTURE",4,false,$group->picture));
1921 fwrite ($bf,full_tag("HIDEPICTURE",4,false,$group->hidepicture));
1922 fwrite ($bf,full_tag("TIMECREATED",4,false,$group->timecreated));
1923 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$group->timemodified));
1925 //Now, backup groups_members, only if users are included
1926 if ($preferences->backup_users != 2) {
1927 $status2 = backup_groups_members_info($bf,$preferences,$group->id);
1930 //End group tag
1931 fwrite ($bf,end_tag("GROUP",3,true));
1933 //End groups tag
1934 $status = fwrite ($bf,end_tag("GROUPS",2,true));
1936 //Now save group_files
1937 if ($status && $status2) {
1938 $status2 = backup_copy_group_files($preferences);
1941 return ($status && $status2);
1944 //Backup groups_members info
1945 function backup_groups_members_info($bf,$preferences,$groupid) {
1947 global $CFG;
1949 $status = true;
1951 //Get groups_members
1952 $groups_members = get_records("groups_members","groupid",$groupid);
1954 //Pring groups_members header
1955 if ($groups_members) {
1956 //Pring groups_members header
1957 fwrite ($bf,start_tag("MEMBERS",4,true));
1958 //Iterate
1959 foreach ($groups_members as $group_member) {
1960 //Begin group_member tag
1961 fwrite ($bf,start_tag("MEMBER",5,true));
1962 //Output group_member contents
1963 fwrite ($bf,full_tag("GROUPID",6,false,$group_member->groupid));
1964 fwrite ($bf,full_tag("USERID",6,false,$group_member->userid));
1965 fwrite ($bf,full_tag("TIMEADDED",6,false,$group_member->timeadded));
1966 //End group_member tag
1967 fwrite ($bf,end_tag("MEMBER",5,true));
1969 //End groups_members tag
1970 $status = fwrite ($bf,end_tag("MEMBERS",4,true));
1972 return $status;
1975 //Backup groupings info
1976 function backup_groupings_info($bf,$preferences) {
1978 global $CFG;
1980 $status = true;
1982 //Get groups
1983 $groupings = get_records("groupings","courseid",$preferences->backup_course);
1985 //Pring groups header
1986 if ($groupings) {
1987 //Pring groups header
1988 fwrite ($bf,start_tag("GROUPINGS",2,true));
1989 //Iterate
1990 foreach ($groupings as $grouping) {
1991 //Begin group tag
1992 fwrite ($bf,start_tag("GROUPING",3,true));
1993 //Output group contents
1994 fwrite ($bf,full_tag("ID",4,false,$grouping->id));
1995 //fwrite ($bf,full_tag("COURSEID",4,false,$grouping->courseid));
1996 fwrite ($bf,full_tag("NAME",4,false,$grouping->name));
1997 fwrite ($bf,full_tag("DESCRIPTION",4,false,$grouping->description));
1998 fwrite ($bf,full_tag("CONFIGDATA",4,false,$grouping->configdata));
1999 fwrite ($bf,full_tag("TIMECREATED",4,false,$grouping->timecreated));
2000 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$grouping->timemodified));
2002 //End group tag
2003 fwrite ($bf,end_tag("GROUPING",3,true));
2005 //End groups tag
2006 $status = fwrite ($bf,end_tag("GROUPINGS",2,true));
2008 //(Now save grouping_files)
2010 return $status;
2013 //Backup groupings-groups info
2014 function backup_groupings_groups_info($bf,$preferences) {
2016 global $CFG;
2018 $status = true;
2020 //Get grouping_groups
2021 $courseid = $preferences->backup_course;
2022 $sql = "SELECT gg.* FROM {$CFG->prefix}groupings g, {$CFG->prefix}groupings_groups gg
2023 WHERE g.courseid=$courseid AND g.id=gg.groupingid";
2024 $grouping_groups = get_records_sql($sql);
2026 //Pring grouping_groups header
2027 if ($grouping_groups) {
2028 //Pring grouping_groups header
2029 fwrite ($bf,start_tag("GROUPINGSGROUPS",2,true));
2030 //Iterate
2031 foreach ($grouping_groups as $members) {
2032 //Begin grouping_group tag
2033 fwrite ($bf,start_tag("GROUPINGGROUP",3,true));
2034 //Output group_member contents
2035 fwrite ($bf,full_tag("ID",4,false,$members->id));
2036 fwrite ($bf,full_tag("GROUPINGID",4,false,$members->groupingid));
2037 fwrite ($bf,full_tag("GROUPID",4,false,$members->groupid));
2038 fwrite ($bf,full_tag("TIMEADDED",4,false,$members->timeadded));
2039 //End grouping_group tag
2040 fwrite ($bf,end_tag("GROUPINGGROUP",3,true));
2042 //End grouping_groups tag
2043 $status = fwrite ($bf,end_tag("GROUPINGSGROUPS",2,true));
2045 return $status;
2048 //Start the modules tag
2049 function backup_modules_start ($bf,$preferences) {
2051 return fwrite ($bf,start_tag("MODULES",2,true));
2054 //End the modules tag
2055 function backup_modules_end ($bf,$preferences) {
2057 return fwrite ($bf,end_tag("MODULES",2,true));
2060 //This function makes all the necesary calls to every mod
2061 //to export itself and its files !!!
2062 function backup_module($bf,$preferences,$module) {
2064 global $CFG;
2066 $status = true;
2068 require_once($CFG->dirroot.'/mod/'.$module.'/backuplib.php');
2070 if (isset($preferences->mods[$module]->instances)
2071 && is_array($preferences->mods[$module]->instances)) {
2072 $onemodbackup = $module.'_backup_one_mod';
2073 if (function_exists($onemodbackup)) {
2074 foreach ($preferences->mods[$module]->instances as $instance => $object) {
2075 if (!empty($object->backup)) {
2076 $status = $onemodbackup($bf,$preferences,$instance);
2079 } else {
2080 $status = false;
2082 } else { // whole module.
2083 //First, re-check if necessary functions exists
2084 $modbackup = $module."_backup_mods";
2085 if (function_exists($modbackup)) {
2086 //Call the function
2087 $status = $modbackup($bf,$preferences);
2088 } else {
2089 //Something was wrong. Function should exist.
2090 $status = false;
2094 return $status;
2098 //This function encode things to make backup multi-site fully functional
2099 //It does this conversions:
2100 // - $CFG->wwwroot/file.php/courseid ------------------> $@FILEPHP@$ (slasharguments links)
2101 // - $CFG->wwwroot/file.php?file=/courseid ------------> $@FILEPHP@$ (non-slasharguments links)
2102 // - Every module xxxx_encode_content_links() is executed too
2104 function backup_encode_absolute_links($content) {
2106 global $CFG,$preferences;
2108 //Use one static variable to cache all the require_once calls that,
2109 //under PHP5 seems to increase load too much, and we are requiring
2110 //them here thousands of times (one per content). MDL-8700.
2111 //Once fixed by PHP, we'll delete this hack
2113 static $includedfiles;
2114 if (!isset($includedfiles)) {
2115 $includedfiles = array();
2118 //Check if preferences is ok. If it isn't set, we are
2119 //in a scheduled_backup to we are able to get a copy
2120 //from CFG->backup_preferences
2121 if (!isset($preferences)) {
2122 $mypreferences = $CFG->backup_preferences;
2123 } else {
2124 //We are in manual backups so global preferences must exist!!
2125 $mypreferences = $preferences;
2128 //First, we check for every call to file.php inside the course
2129 $search = array($CFG->wwwroot.'/file.php/'.$mypreferences->backup_course,
2130 $CFG->wwwroot.'/file.php?file=/'.$mypreferences->backup_course);
2132 $replace = array('$@FILEPHP@$','$@FILEPHP@$');
2134 $result = str_replace($search,$replace,$content);
2136 foreach ($mypreferences->mods as $name => $info) {
2137 /// We only include the corresponding backuplib.php if it hasn't been included before
2138 /// This will save some load under PHP5. MDL-8700.
2139 /// Once fixed by PHP, we'll delete this hack
2140 if (!in_array($name, $includedfiles)) {
2141 include_once("$CFG->dirroot/mod/$name/backuplib.php");
2142 $includedfiles[] = $name;
2144 //Check if the xxxx_encode_content_links exists
2145 $function_name = $name."_encode_content_links";
2146 if (function_exists($function_name)) {
2147 $result = $function_name($result,$mypreferences);
2151 if ($result != $content) {
2152 debugging('<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />');
2155 return $result;
2158 //This function copies all the needed files under the "users" directory to the "user_files"
2159 //directory under temp/backup
2160 function backup_copy_user_files ($preferences) {
2162 global $CFG;
2164 $status = true;
2166 //First we check to "user_files" exists and create it as necessary
2167 //in temp/backup/$backup_code dir
2168 $status = check_and_create_user_files_dir($preferences->backup_unique_code);
2170 //Now iterate over directories under "users" to check if that user must be
2171 //copied to backup
2173 $rootdir = $CFG->dataroot."/users";
2174 //Check if directory exists
2175 if (is_dir($rootdir)) {
2176 $list = list_directories ($rootdir);
2177 if ($list) {
2178 //Iterate
2179 foreach ($list as $dir) {
2180 //Look for dir like username in backup_ids
2181 $data = get_record ("backup_ids","backup_code",$preferences->backup_unique_code,
2182 "table_name","user",
2183 "old_id",$dir);
2184 //If exists, copy it
2185 if ($data) {
2186 $status = backup_copy_file($rootdir."/".$dir,
2187 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/user_files/".$dir);
2193 return $status;
2196 //This function copies all the needed files under the "groups" directory to the "group_files"
2197 //directory under temp/backup
2198 function backup_copy_group_files ($preferences) {
2200 global $CFG;
2202 $status = true;
2204 //First we check if "group_files" exists and create it as necessary
2205 //in temp/backup/$backup_code dir
2206 $status = check_and_create_group_files_dir($preferences->backup_unique_code);
2208 //Now iterate over directories under "groups" to check if that user must be
2209 //copied to backup
2211 $rootdir = $CFG->dataroot.'/groups';
2212 //Check if directory exists
2213 if (is_dir($rootdir)) {
2214 $list = list_directories ($rootdir);
2215 if ($list) {
2216 //Iterate
2217 foreach ($list as $dir) {
2218 //Look for dir like group in groups table
2219 $data = get_record ('groups', 'courseid', $preferences->backup_course,
2220 'id',$dir);
2221 //If exists, copy it
2222 if ($data) {
2223 $status = backup_copy_file($rootdir."/".$dir,
2224 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/group_files/".$dir);
2229 return $status;
2232 //This function copies all the course files under the course directory (except the moddata
2233 //directory to the "course_files" directory under temp/backup
2234 function backup_copy_course_files ($preferences) {
2236 global $CFG;
2238 $status = true;
2240 //First we check to "course_files" exists and create it as necessary
2241 //in temp/backup/$backup_code dir
2242 $status = check_and_create_course_files_dir($preferences->backup_unique_code);
2244 //Now iterate over files and directories except $CFG->moddata and backupdata to be
2245 //copied to backup
2247 $rootdir = $CFG->dataroot."/".$preferences->backup_course;
2249 $name_moddata = $CFG->moddata;
2250 $name_backupdata = "backupdata";
2251 //Check if directory exists
2252 if (is_dir($rootdir)) {
2253 $list = list_directories_and_files ($rootdir);
2254 if ($list) {
2255 //Iterate
2256 foreach ($list as $dir) {
2257 if ($dir !== $name_moddata and $dir !== $name_backupdata) {
2258 $status = backup_copy_file($rootdir."/".$dir,
2259 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/course_files/".$dir);
2264 return $status;
2267 * This function copies all the site files under the site directory (except the moddata and backupdata
2268 * directories to the "site_files" directory under temp/backup
2270 function backup_copy_site_files ($preferences) {
2272 global $CFG;
2274 $status = true;
2276 if ($preferences->backup_course == SITEID){
2277 return $status;
2280 //First we check to "site_files" exists and create it as necessary
2281 //in temp/backup/$backup_code dir
2282 $status = $status && check_and_create_site_files_dir($preferences->backup_unique_code);
2284 $rootdir = $CFG->dataroot."/".SITEID;
2288 $files = get_records_select('backup_files',
2289 "backup_code = '$preferences->backup_unique_code' AND file_type = 'site'");
2290 if ($files) {
2291 //Iterate
2292 foreach ($files as $fileobj) {
2293 //check for dir structure and create recursively
2294 $file = $fileobj->path;
2295 $status = $status && check_dir_exists(dirname($CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/site_files/".$file), true, true);
2296 $status = $status && backup_copy_file($rootdir."/".$file,
2297 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/site_files/".$file);
2300 return $status;
2302 //This function creates the zip file containing all the backup info
2303 //moodle.xml, moddata, user_files, course_files.
2304 //The zipped file is created in the backup directory and named with
2305 //the "oficial" name of the backup
2306 //It uses "pclzip" if available or system "zip" (unix only)
2307 function backup_zip ($preferences) {
2309 global $CFG;
2311 $status = true;
2313 //Base dir where everything happens
2314 $basedir = cleardoubleslashes($CFG->dataroot."/temp/backup/".$preferences->backup_unique_code);
2315 //Backup zip file name
2316 $name = $preferences->backup_name;
2317 //List of files and directories
2318 $filelist = list_directories_and_files ($basedir);
2320 //Convert them to full paths
2321 $files = array();
2322 foreach ($filelist as $file) {
2323 $files[] = "$basedir/$file";
2326 $status = zip_files($files, "$basedir/$name");
2328 //echo "<br/>Status: ".$status; //Debug
2329 return $status;
2333 //This function copies the final zip to the course dir
2334 function copy_zip_to_course_dir ($preferences) {
2336 global $CFG;
2338 $status = true;
2340 //Define zip location (from)
2341 $from_zip_file = $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/".$preferences->backup_name;
2343 //Initialise $to_zip_file
2344 $to_zip_file="";
2346 //If $preferences->backup_destination isn't empty, then copy to custom directory
2347 if (!empty($preferences->backup_destination)) {
2348 $to_zip_file = $preferences->backup_destination."/".$preferences->backup_name;
2349 } else {
2350 //Define zip destination (course dir)
2351 $to_zip_file = $CFG->dataroot."/".$preferences->backup_course;
2353 //echo "<p>From: ".$from_zip_file."<br />"; //Debug
2355 //echo "<p>Checking: ".$to_zip_file."<br />"; //Debug
2357 //Checks course dir exists
2358 $status = check_dir_exists($to_zip_file,true);
2360 //Define zip destination (backup dir)
2361 $to_zip_file = $to_zip_file."/backupdata";
2363 //echo "<p>Checking: ".$to_zip_file."<br />"; //Debug
2365 //Checks backup dir exists
2366 $status = check_dir_exists($to_zip_file,true);
2368 //Define zip destination (zip file)
2369 $to_zip_file = $to_zip_file."/".$preferences->backup_name;
2372 //echo "<p>To: ".$to_zip_file."<br />"; //Debug
2374 //Copy zip file
2375 if ($status) {
2376 $status = backup_copy_file ($from_zip_file,$to_zip_file);
2379 return $status;
2383 * compatibility function
2384 * with new granular backup
2385 * we need to know
2387 function backup_userdata_selected($preferences,$modname,$modid) {
2388 return ((empty($preferences->mods[$modname]->instances)
2389 && !empty($preferences->mods[$modname]->userinfo))
2390 || (is_array($preferences->mods[$modname]->instances)
2391 && array_key_exists($modid,$preferences->mods[$modname]->instances)
2392 && !empty($preferences->mods[$modname]->instances[$modid]->userinfo)));
2396 function backup_mod_selected($preferences,$modname,$modid) {
2397 return ((empty($preferences->mods[$modname]->instances)
2398 && !empty($preferences->mods[$modname]->backup))
2399 || (is_array($preferences->mods[$modname]->instances)
2400 && array_key_exists($modid,$preferences->mods[$modname]->instances)
2401 && !empty($preferences->mods[$modname]->instances[$modid]->backup)));
2405 * Checks for the required files/functions to backup every mod
2406 * And check if there is data about it
2408 function backup_fetch_prefs_from_request(&$preferences,&$count,$course) {
2409 global $CFG,$SESSION;
2411 // check to see if it's in the session already
2412 if (!empty($SESSION->backupprefs) && array_key_exists($course->id,$SESSION->backupprefs) && !empty($SESSION->backupprefs[$course->id])) {
2413 $sprefs = $SESSION->backupprefs[$course->id];
2414 $preferences = $sprefs;
2415 // refetch backup_name just in case.
2416 $bn = optional_param('backup_name','',PARAM_FILE);
2417 if (!empty($bn)) {
2418 $preferences->backup_name = $bn;
2420 $count = 1;
2421 return true;
2424 if ($allmods = get_records("modules") ) {
2425 foreach ($allmods as $mod) {
2426 $modname = $mod->name;
2427 $modfile = "$CFG->dirroot/mod/$modname/backuplib.php";
2428 $modbackup = $modname."_backup_mods";
2429 $modbackupone = $modname."_backup_one_mod";
2430 $modcheckbackup = $modname."_check_backup_mods";
2431 if (!file_exists($modfile)) {
2432 continue;
2434 include_once($modfile);
2435 if (!function_exists($modbackup) || !function_exists($modcheckbackup)) {
2436 continue;
2438 $var = "exists_".$modname;
2439 $preferences->$var = true;
2440 $count++;
2441 // check that there are instances and we can back them up individually
2442 if (!count_records('course_modules','course',$course->id,'module',$mod->id) || !function_exists($modbackupone)) {
2443 continue;
2445 $var = 'exists_one_'.$modname;
2446 $preferences->$var = true;
2447 $varname = $modname.'_instances';
2448 $preferences->$varname = get_all_instances_in_course($modname, $course, NULL, true);
2449 foreach ($preferences->$varname as $instance) {
2450 $preferences->mods[$modname]->instances[$instance->id]->name = $instance->name;
2451 $var = 'backup_'.$modname.'_instance_'.$instance->id;
2452 $$var = optional_param($var,0);
2453 $preferences->$var = $$var;
2454 $preferences->mods[$modname]->instances[$instance->id]->backup = $$var;
2455 $var = 'backup_user_info_'.$modname.'_instance_'.$instance->id;
2456 $$var = optional_param($var,0);
2457 $preferences->$var = $$var;
2458 $preferences->mods[$modname]->instances[$instance->id]->userinfo = $$var;
2459 $var = 'backup_'.$modname.'_instances';
2460 $preferences->$var = 1; // we need this later to determine what to display in modcheckbackup.
2463 //Check data
2464 //Check module info
2465 $preferences->mods[$modname]->name = $modname;
2467 $var = "backup_".$modname;
2468 $$var = optional_param( $var,0);
2469 $preferences->$var = $$var;
2470 $preferences->mods[$modname]->backup = $$var;
2472 //Check include user info
2473 $var = "backup_user_info_".$modname;
2474 $$var = optional_param( $var,0);
2475 $preferences->$var = $$var;
2476 $preferences->mods[$modname]->userinfo = $$var;
2481 //Check other parameters
2482 $preferences->backup_metacourse = optional_param('backup_metacourse',1,PARAM_INT);
2483 $preferences->backup_users = optional_param('backup_users',1,PARAM_INT);
2484 $preferences->backup_logs = optional_param('backup_logs',0,PARAM_INT);
2485 $preferences->backup_user_files = optional_param('backup_user_files',1,PARAM_INT);
2486 $preferences->backup_course_files = optional_param('backup_course_files',1,PARAM_INT);
2487 $preferences->backup_gradebook_history = optional_param('backup_gradebook_history', 1, PARAM_INT);
2488 $preferences->backup_site_files = optional_param('backup_site_files',1,PARAM_INT);
2489 $preferences->backup_messages = optional_param('backup_messages',1,PARAM_INT);
2490 $preferences->backup_course = $course->id;
2491 $preferences->backup_name = required_param('backup_name',PARAM_FILE);
2492 $preferences->backup_unique_code = required_param('backup_unique_code');
2494 $roles = get_records('role', '', '', 'sortorder');
2495 $preferences->backuproleassignments = array();
2496 foreach ($roles as $role) {
2497 if (optional_param('backupassignments_' . $role->shortname, 0, PARAM_INT)) {
2498 $preferences->backuproleassignments[$role->id] = $role;
2502 // put it (back) in the session
2503 $SESSION->backupprefs[$course->id] = $preferences;
2506 /* Finds all related roles used in course, mod and blocks context
2507 * @param object $preferences
2508 * @param object $course
2509 * @return array of role objects
2511 function backup_fetch_roles($preferences) {
2513 global $CFG;
2514 $contexts = array();
2515 $roles = array();
2517 /// adding course context
2518 $coursecontext = get_context_instance(CONTEXT_COURSE, $preferences->backup_course);
2519 $contexts[$coursecontext->id] = $coursecontext;
2521 /// adding mod contexts
2522 $mods = $preferences->mods;
2523 foreach ($mods as $modname => $mod) {
2524 $instances = $mod->instances;
2525 foreach ($instances as $id => $instance) {
2526 // if this type of mod is to be backed up
2527 if ($instance->backup) {
2528 $cm = get_coursemodule_from_instance($modname, $id);
2529 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
2530 // put context in array keys
2531 $contexts[$context->id] = $context;
2536 // add all roles assigned at user context
2537 if ($preferences->backup_users) {
2538 if ($users = get_records_sql("SELECT u.old_id, u.table_name,u.info
2539 FROM {$CFG->prefix}backup_ids u
2540 WHERE u.backup_code = '$preferences->backup_unique_code' AND
2541 u.table_name = 'user'")) {
2542 foreach ($users as $user) {
2543 $context = get_context_instance(CONTEXT_USER, $user->old_id);
2544 $contexts[$context->id] = $context;
2550 // add all roles assigned at block context
2551 if ($courseblocks = get_records_sql("SELECT *
2552 FROM {$CFG->prefix}block_instance
2553 WHERE pagetype = '".PAGE_COURSE_VIEW."'
2554 AND pageid = {$preferences->backup_course}")) {
2556 foreach ($courseblocks as $courseblock) {
2558 $context = get_context_instance(CONTEXT_BLOCK, $courseblock->id);
2559 $contexts[$context->id] = $context;
2563 // foreach context, call get_roles_on_exact_context insert into array
2564 foreach ($contexts as $context) {
2565 if ($proles = get_roles_on_exact_context($context)) {
2566 foreach ($proles as $prole) {
2567 $roles[$prole->id] = $prole;
2572 return $roles;
2575 /* function to print xml for overrides */
2576 function write_role_overrides_xml($bf, $context, $startlevel) {
2577 fwrite ($bf, start_tag("ROLES_OVERRIDES", $startlevel, true));
2578 if ($roles = get_roles_with_override_on_context($context)) {
2579 foreach ($roles as $role) {
2580 fwrite ($bf, start_tag("ROLE", $startlevel+1, true));
2581 fwrite ($bf, full_tag("ID", $startlevel+2, false, $role->id));
2582 fwrite ($bf, full_tag("NAME", $startlevel+2, false, $role->name));
2583 fwrite ($bf, full_tag("SHORTNAME", $startlevel+2, false, $role->shortname));
2584 fwrite ($bf, start_tag("CAPABILITIES", $startlevel+2, true));
2585 if ($capabilities = get_capabilities_from_role_on_context($role, $context)) {
2586 foreach ($capabilities as $capability) {
2587 fwrite ($bf, start_tag("CAPABILITY", $startlevel+3, true));
2588 fwrite ($bf, full_tag("NAME", $startlevel+4, false, $capability->capability));
2589 fwrite ($bf, full_tag("PERMISSION", $startlevel+4, false, $capability->permission));
2590 fwrite ($bf, full_tag("TIMEMODIFIED", $startlevel+4, false, $capability->timemodified));
2591 if (!isset($capability->modifierid)) {
2592 $capability->modifierid = 0;
2594 fwrite ($bf, full_tag("MODIFIERID", $startlevel+4, false, $capability->modifierid));
2595 fwrite ($bf, end_tag("CAPABILITY", $startlevel+3, true));
2598 fwrite ($bf, end_tag("CAPABILITIES", $startlevel+2, true));
2599 fwrite ($bf, end_tag("ROLE", $startlevel+1, true));
2602 fwrite ($bf, end_tag("ROLES_OVERRIDES", $startlevel, true));
2605 /* function to print xml for assignment */
2606 function write_role_assignments_xml($bf, $context, $startlevel, $preferences) {
2607 /// write role_assign code here
2608 fwrite ($bf, start_tag("ROLES_ASSIGNMENTS", $startlevel, true));
2610 if ($roles = get_roles_with_assignment_on_context($context)) {
2611 foreach ($roles as $role) {
2612 if (!isset($preferences->backuproleassignments[$role->id])) {
2613 continue;
2615 fwrite ($bf, start_tag("ROLE", $startlevel+1, true));
2616 fwrite ($bf, full_tag("ID", $startlevel+2, false, $role->id));
2617 fwrite ($bf, full_tag("NAME", $startlevel+2, false, $role->name));
2618 fwrite ($bf, full_tag("SHORTNAME", $startlevel+2, false, $role->shortname));
2619 fwrite ($bf, start_tag("ASSIGNMENTS", $startlevel+2, true));
2620 if ($assignments = get_users_from_role_on_context($role, $context)) {
2621 foreach ($assignments as $assignment) {
2622 fwrite ($bf, start_tag("ASSIGNMENT", $startlevel+3, true));
2623 fwrite ($bf, full_tag("USERID", $startlevel+4, false, $assignment->userid));
2624 fwrite ($bf, full_tag("HIDDEN", $startlevel+4, false, $assignment->hidden));
2625 fwrite ($bf, full_tag("TIMESTART", $startlevel+4, false, $assignment->timestart));
2626 fwrite ($bf, full_tag("TIMEEND", $startlevel+4, false, $assignment->timeend));
2627 fwrite ($bf, full_tag("TIMEMODIFIED", $startlevel+4, false, $assignment->timemodified));
2628 if (!isset($assignment->modifierid)) {
2629 $assignment->modifierid = 0;
2631 fwrite ($bf, full_tag("MODIFIERID", $startlevel+4, false, $assignment->modifierid));
2632 fwrite ($bf, full_tag("ENROL", $startlevel+4, false, $assignment->enrol));
2633 fwrite ($bf, full_tag("SORTORDER", $startlevel+4, false, $assignment->sortorder));
2634 fwrite ($bf, end_tag("ASSIGNMENT", $startlevel+3, true));
2637 fwrite ($bf, end_tag("ASSIGNMENTS", $startlevel+2, true));
2638 fwrite ($bf, end_tag("ROLE", $startlevel+1, true));
2641 fwrite ($bf, end_tag("ROLES_ASSIGNMENTS", $startlevel, true));
2645 function backup_execute(&$preferences, &$errorstr) {
2646 global $CFG;
2647 $status = true;
2649 //Check for temp and backup and backup_unique_code directory
2650 //Create them as needed
2651 if (!defined('BACKUP_SILENTLY')) {
2652 echo "<li>".get_string("creatingtemporarystructures").'</li>';
2655 $status = check_and_create_backup_dir($preferences->backup_unique_code);
2656 //Empty dir
2657 if ($status) {
2658 $status = clear_backup_dir($preferences->backup_unique_code);
2661 //Delete old_entries from backup tables
2662 if (!defined('BACKUP_SILENTLY')) {
2663 echo "<li>".get_string("deletingolddata").'</li>';
2665 $status = backup_delete_old_data();
2666 if (!$status) {
2667 if (!defined('BACKUP_SILENTLY')) {
2668 error ("An error occurred deleting old backup data");
2670 else {
2671 $errorstr = "An error occurred deleting old backup data";
2672 return false;
2676 //Create the moodle.xml file
2677 if ($status) {
2678 if (!defined('BACKUP_SILENTLY')) {
2679 echo "<li>".get_string("creatingxmlfile");
2680 //Begin a new list to xml contents
2681 echo "<ul>";
2682 echo "<li>".get_string("writingheader").'</li>';
2684 //Obtain the xml file (create and open) and print prolog information
2685 $backup_file = backup_open_xml($preferences->backup_unique_code);
2686 if (!defined('BACKUP_SILENTLY')) {
2687 echo "<li>".get_string("writinggeneralinfo").'</li>';
2689 //Prints general info about backup to file
2690 if ($backup_file) {
2691 if (!$status = backup_general_info($backup_file,$preferences)) {
2692 if (!defined('BACKUP_SILENTLY')) {
2693 notify("An error occurred while backing up general info");
2695 else {
2696 $errorstr = "An error occurred while backing up general info";
2697 return false;
2701 if (!defined('BACKUP_SILENTLY')) {
2702 echo "<li>".get_string("writingcoursedata");
2703 //Start new ul (for course)
2704 echo "<ul>";
2705 echo "<li>".get_string("courseinfo").'</li>';
2707 //Prints course start (tag and general info)
2708 if ($status) {
2709 if (!$status = backup_course_start($backup_file,$preferences)) {
2710 if (!defined('BACKUP_SILENTLY')) {
2711 notify("An error occurred while backing up course start");
2713 else {
2714 $errorstr = "An error occurred while backing up course start";
2715 return false;
2719 //Metacourse information
2720 if ($status && $preferences->backup_metacourse) {
2721 if (!defined('BACKUP_SILENTLY')) {
2722 echo "<li>".get_string("metacourse").'</li>';
2724 if (!$status = backup_course_metacourse($backup_file,$preferences)) {
2725 if (!defined('BACKUP_SILENTLY')) {
2726 notify("An error occurred while backing up metacourse info");
2728 else {
2729 $errorstr = "An error occurred while backing up metacourse info";
2730 return false;
2734 if (!defined('BACKUP_SILENTLY')) {
2735 echo "<li>".get_string("blocks").'</li>';
2737 //Blocks information
2738 if ($status) {
2739 if (!$status = backup_course_blocks($backup_file,$preferences)) {
2740 if (!defined('BACKUP_SILENTLY')) {
2741 notify("An error occurred while backing up course blocks");
2743 else {
2744 $errorstr = "An error occurred while backing up course blocks";
2745 return false;
2749 if (!defined('BACKUP_SILENTLY')) {
2750 echo "<li>".get_string("sections").'</li>';
2752 //Section info
2753 if ($status) {
2754 if (!$status = backup_course_sections($backup_file,$preferences)) {
2755 if (!defined('BACKUP_SILENTLY')) {
2756 notify("An error occurred while backing up course sections");
2758 else {
2759 $errorstr = "An error occurred while backing up course sections";
2760 return false;
2765 //End course contents (close ul)
2766 if (!defined('BACKUP_SILENTLY')) {
2767 echo "</ul></li>";
2770 //User info
2771 if ($status) {
2772 if (!defined('BACKUP_SILENTLY')) {
2773 echo "<li>".get_string("writinguserinfo").'</li>';
2775 if (!$status = backup_user_info($backup_file,$preferences)) {
2776 if (!defined('BACKUP_SILENTLY')) {
2777 notify("An error occurred while backing up user info");
2779 else {
2780 $errorstr = "An error occurred while backing up user info";
2781 return false;
2786 //If we have selected to backup messages and we are
2787 //doing a SITE backup, let's do it
2788 if ($status && $preferences->backup_messages && $preferences->backup_course == SITEID) {
2789 if (!defined('BACKUP_SILENTLY')) {
2790 echo "<li>".get_string("writingmessagesinfo").'</li>';
2792 if (!$status = backup_messages($backup_file,$preferences)) {
2793 if (!defined('BACKUP_SILENTLY')) {
2794 notify("An error occurred while backing up messages");
2796 else {
2797 $errorstr = "An error occurred while backing up messages";
2798 return false;
2803 //If we have selected to backup quizzes or other modules that use questions
2804 //we've already added ids of categories and questions to backup to backup_ids table
2805 if ($status) {
2806 if (!defined('BACKUP_SILENTLY')) {
2807 echo "<li>".get_string("writingcategoriesandquestions").'</li>';
2809 require_once($CFG->dirroot.'/question/backuplib.php');
2810 if (!$status = backup_question_categories($backup_file, $preferences)) {
2811 if (!defined('BACKUP_SILENTLY')) {
2812 notify("An error occurred while backing up quiz categories");
2814 else {
2815 $errorstr = "An error occurred while backing up quiz categories";
2816 return false;
2821 //Print logs if selected
2822 if ($status) {
2823 if ($preferences->backup_logs) {
2824 if (!defined('BACKUP_SILENTLY')) {
2825 echo "<li>".get_string("writingloginfo").'</li>';
2827 if (!$status = backup_log_info($backup_file,$preferences)) {
2828 if (!defined('BACKUP_SILENTLY')) {
2829 notify("An error occurred while backing up log info");
2831 else {
2832 $errorstr = "An error occurred while backing up log info";
2833 return false;
2839 //Print scales info
2840 if ($status) {
2841 if (!defined('BACKUP_SILENTLY')) {
2842 echo "<li>".get_string("writingscalesinfo").'</li>';
2844 if (!$status = backup_scales_info($backup_file,$preferences)) {
2845 if (!defined('BACKUP_SILENTLY')) {
2846 notify("An error occurred while backing up scales");
2848 else {
2849 $errorstr = "An error occurred while backing up scales";
2850 return false;
2855 //Print groups info
2856 if ($status) {
2857 if (!defined('BACKUP_SILENTLY')) {
2858 echo "<li>".get_string("writinggroupsinfo").'</li>';
2860 if (!$status = backup_groups_info($backup_file,$preferences)) {
2861 if (!defined('BACKUP_SILENTLY')) {
2862 notify("An error occurred while backing up groups");
2864 else {
2865 $errostr = "An error occurred while backing up groups";
2866 return false;
2871 //Print groupings info
2872 if ($status) {
2873 if (!defined('BACKUP_SILENTLY')) {
2874 echo "<li>".get_string("writinggroupingsinfo").'</li>';
2876 if (!$status = backup_groupings_info($backup_file,$preferences)) {
2877 if (!defined('BACKUP_SILENTLY')) {
2878 notify("An error occurred while backing up groupings");
2880 else {
2881 $errorstr = "An error occurred while backing up groupings";
2882 return false;
2887 //Print groupings_groups info
2888 if ($status) {
2889 if (!defined('BACKUP_SILENTLY')) {
2890 echo "<li>".get_string("writinggroupingsgroupsinfo").'</li>';
2892 if (!$status = backup_groupings_groups_info($backup_file,$preferences)) {
2893 if (!defined('BACKUP_SILENTLY')) {
2894 notify("An error occurred while backing up groupings groups");
2896 else {
2897 $errorstr = "An error occurred while backing up groupings groups";
2898 return false;
2903 //Print events info
2904 if ($status) {
2905 if (!defined('BACKUP_SILENTLY')) {
2906 echo "<li>".get_string("writingeventsinfo").'</li>';
2908 if (!$status = backup_events_info($backup_file,$preferences)) {
2909 if (!defined('BACKUP_SILENTLY')) {
2910 notify("An error occurred while backing up events");
2912 else {
2913 $errorstr = "An error occurred while backing up events";
2914 return false;
2919 //Print gradebook info
2920 if ($status) {
2921 if (!defined('BACKUP_SILENTLY')) {
2922 echo "<li>".get_string("writinggradebookinfo").'</li>';
2924 if (!$status = backup_gradebook_info($backup_file,$preferences)) {
2925 if (!defined('BACKUP_SILENTLY')) {
2926 notify("An error occurred while backing up gradebook");
2928 else {
2929 $errorstr = "An error occurred while backing up gradebook";
2930 return false;
2935 //Module info, this unique function makes all the work!!
2936 //db export and module fileis copy
2937 if ($status) {
2938 $mods_to_backup = false;
2939 //Check if we have any mod to backup
2940 foreach ($preferences->mods as $module) {
2941 if ($module->backup) {
2942 $mods_to_backup = true;
2945 //If we have to backup some module
2946 if ($mods_to_backup) {
2947 if (!defined('BACKUP_SILENTLY')) {
2948 echo "<li>".get_string("writingmoduleinfo");
2950 //Start modules tag
2951 if (!$status = backup_modules_start ($backup_file,$preferences)) {
2952 if (!defined('BACKUP_SILENTLY')) {
2953 notify("An error occurred while backing up module info");
2955 else {
2956 $errorstr = "An error occurred while backing up module info";
2957 return false;
2960 //Open ul for module list
2961 if (!defined('BACKUP_SILENTLY')) {
2962 echo "<ul>";
2964 //Iterate over modules and call backup
2965 foreach ($preferences->mods as $module) {
2966 if ($module->backup and $status) {
2967 if (!defined('BACKUP_SILENTLY')) {
2968 echo "<li>".get_string("modulenameplural",$module->name).'</li>';
2970 if (!$status = backup_module($backup_file,$preferences,$module->name)) {
2971 if (!defined('BACKUP_SILENTLY')) {
2972 notify("An error occurred while backing up '$module->name'");
2974 else {
2975 $errorstr = "An error occurred while backing up '$module->name'";
2976 return false;
2981 //Close ul for module list
2982 if (!defined('BACKUP_SILENTLY')) {
2983 echo "</ul></li>";
2985 //Close modules tag
2986 if (!$status = backup_modules_end ($backup_file,$preferences)) {
2987 if (!defined('BACKUP_SILENTLY')) {
2988 notify("An error occurred while finishing the module backups");
2990 else {
2991 $errorstr = "An error occurred while finishing the module backups";
2992 return false;
2998 //Backup course format data, if any.
2999 if (!defined('BACKUP_SILENTLY')) {
3000 echo '<li>'.get_string("courseformatdata").'</li>';
3002 if($status) {
3003 if (!$status = backup_format_data($backup_file,$preferences)) {
3004 if (!defined('BACKUP_SILENTLY')) {
3005 notify("An error occurred while backing up the course format data");
3007 else {
3008 $errorstr = "An error occurred while backing up the course format data";
3009 return false;
3014 //Prints course end
3015 if ($status) {
3016 if (!$status = backup_course_end($backup_file,$preferences)) {
3017 if (!defined('BACKUP_SILENTLY')) {
3018 notify("An error occurred while closing the course backup");
3020 else {
3021 $errorstr = "An error occurred while closing the course backup";
3022 return false;
3026 //Close the xml file and xml data
3027 if ($backup_file) {
3028 backup_close_xml($backup_file);
3031 //End xml contents (close ul)
3032 if (!defined('BACKUP_SILENTLY')) {
3033 echo "</ul></li>";
3037 //Now, if selected, copy user files
3038 if ($status) {
3039 if ($preferences->backup_user_files) {
3040 if (!defined('BACKUP_SILENTLY')) {
3041 echo "<li>".get_string("copyinguserfiles").'</li>';
3043 if (!$status = backup_copy_user_files ($preferences)) {
3044 if (!defined('BACKUP_SILENTLY')) {
3045 notify("An error occurred while copying user files");
3047 else {
3048 $errorstr = "An error occurred while copying user files";
3049 return false;
3055 //Now, if selected, copy course files
3056 if ($status) {
3057 if ($preferences->backup_course_files) {
3058 if (!defined('BACKUP_SILENTLY')) {
3059 echo "<li>".get_string("copyingcoursefiles").'</li>';
3061 if (!$status = backup_copy_course_files ($preferences)) {
3062 if (!defined('BACKUP_SILENTLY')) {
3063 notify("An error occurred while copying course files");
3065 else {
3066 $errorstr = "An error occurred while copying course files";
3067 return false;
3072 //Now, if selected, copy site files
3073 if ($status) {
3074 if ($preferences->backup_site_files) {
3075 if (!defined('BACKUP_SILENTLY')) {
3076 echo "<li>".get_string("copyingsitefiles").'</li>';
3078 if (!$status = backup_copy_site_files ($preferences)) {
3079 if (!defined('BACKUP_SILENTLY')) {
3080 notify("An error occurred while copying site files");
3082 else {
3083 $errorstr = "An error occurred while copying site files";
3084 return false;
3089 //Now, zip all the backup directory contents
3090 if ($status) {
3091 if (!defined('BACKUP_SILENTLY')) {
3092 echo "<li>".get_string("zippingbackup").'</li>';
3094 if (!$status = backup_zip ($preferences)) {
3095 if (!defined('BACKUP_SILENTLY')) {
3096 notify("An error occurred while zipping the backup");
3098 else {
3099 $errorstr = "An error occurred while zipping the backup";
3100 return false;
3105 //Now, copy the zip file to course directory
3106 if ($status) {
3107 if (!defined('BACKUP_SILENTLY')) {
3108 echo "<li>".get_string("copyingzipfile").'</li>';
3110 if (!$status = copy_zip_to_course_dir ($preferences)) {
3111 if (!defined('BACKUP_SILENTLY')) {
3112 notify("An error occurred while copying the zip file to the course directory");
3114 else {
3115 $errorstr = "An error occurred while copying the zip file to the course directory";
3116 return false;
3121 //Now, clean temporary data (db and filesystem)
3122 if ($status) {
3123 if (!defined('BACKUP_SILENTLY')) {
3124 echo "<li>".get_string("cleaningtempdata").'</li>';
3126 if (!$status = clean_temp_data ($preferences)) {
3127 if (!defined('BACKUP_SILENTLY')) {
3128 notify("An error occurred while cleaning up temporary data");
3130 else {
3131 $errorstr = "An error occurred while cleaning up temporary data";
3132 return false;
3137 return $status;
3141 * This function generates the default zipfile name for a backup
3142 * based on the course id and the unique code.
3144 * @param object $course course object
3145 * @param string $backup_unique_code (optional, if left out current timestamp used)
3148 * @return string filename (excluding path information)
3150 function backup_get_zipfile_name($course, $backup_unique_code='') {
3152 if (empty($backup_unique_code)) {
3153 $backup_unique_code = time();
3156 //Calculate the backup word
3157 //Take off some characters in the filename !!
3158 $takeoff = array(" ", ":", "/", "\\", "|");
3159 $backup_word = str_replace($takeoff,"_",moodle_strtolower(get_string("backupfilename")));
3160 //If non-translated, use "backup"
3161 if (substr($backup_word,0,1) == "[") {
3162 $backup_word= "backup";
3165 //Calculate the date format string
3166 $backup_date_format = str_replace(" ","_",get_string("backupnameformat"));
3167 //If non-translated, use "%Y%m%d-%H%M"
3168 if (substr($backup_date_format,0,1) == "[") {
3169 $backup_date_format = "%%Y%%m%%d-%%H%%M";
3172 //Calculate the shortname
3173 $backup_shortname = clean_filename($course->shortname);
3174 if (empty($backup_shortname) or $backup_shortname == '_' ) {
3175 $backup_shortname = $course->id;
3178 //Calculate the final backup filename
3179 //The backup word
3180 $backup_name = $backup_word."-";
3181 //The shortname
3182 $backup_name .= moodle_strtolower($backup_shortname)."-";
3183 //The date format
3184 $backup_name .= userdate(time(),$backup_date_format,99,false);
3185 //The extension
3186 $backup_name .= ".zip";
3187 //And finally, clean everything
3188 $backup_name = clean_filename($backup_name);
3190 return $backup_name;
3195 * This function adds on the standard items to the preferences
3196 * Like moodle version and backup version
3198 * @param object $preferences existing preferences object.
3199 * (passed by reference)
3201 function backup_add_static_preferences(&$preferences) {
3202 global $CFG;
3203 $preferences->moodle_version = $CFG->version;
3204 $preferences->moodle_release = $CFG->release;
3205 $preferences->backup_version = $CFG->backup_version;
3206 $preferences->backup_release = $CFG->backup_release;