MDL-10910 Corrected two errors committed while fixing MDL-10870
[moodle-pu.git] / backup / backuplib.php
blobd3534d7e448ab582705e791fd5cade6f4d7a2f9c
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);
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);
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);
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);
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_grades_text_history_info($bf, $preferences);
1412 $status = backup_gradebook_items_history_info($bf, $preferences);
1413 $status = backup_gradebook_outcomes_history($bf, $preferences);
1416 //Gradebook footer
1417 $status = fwrite ($bf,end_tag("GRADEBOOK",2,true));
1418 return $status;
1421 function backup_gradebook_category_info($bf,$preferences) {
1422 global $CFG;
1423 $status = true;
1425 // getting grade categories, but make sure parents come before children
1426 // because when we do restore, we need to recover the parents first
1427 // we do this by getting the lowest depth first
1428 $grade_categories = get_records_sql("SELECT * FROM {$CFG->prefix}grade_categories
1429 WHERE courseid = $preferences->backup_course
1430 AND depth > 1
1431 ORDER BY depth ASC");
1432 if ($grade_categories) {
1433 //Begin grade_categories tag
1434 fwrite ($bf,start_tag("GRADE_CATEGORIES",3,true));
1435 //Iterate for each category
1436 foreach ($grade_categories as $grade_category) {
1437 //Begin grade_category
1438 fwrite ($bf,start_tag("GRADE_CATEGORY",4,true));
1439 //Output individual fields
1440 fwrite ($bf,full_tag("ID",5,false,$grade_category->id));
1442 // not keeping path and depth because they can be derived
1443 fwrite ($bf,full_tag("PARENT",5,false,$grade_category->parent));
1444 fwrite ($bf,full_tag("FULLNAME",5,false,$grade_category->fullname));
1445 fwrite ($bf,full_tag("AGGREGATION",5,false,$grade_category->aggregation));
1446 fwrite ($bf,full_tag("KEEPHIGH",5,false,$grade_category->keephigh));
1447 fwrite ($bf,full_tag("DROPLOW",5,false,$grade_category->droplow));
1448 fwrite ($bf,full_tag("AGGREGATEOUTCOMES",5,false,$grade_category->aggregateoutcomes));
1450 //End grade_category
1451 fwrite ($bf,end_tag("GRADE_CATEGORY",4,true));
1453 //End grade_categories tag
1454 $status = fwrite ($bf,end_tag("GRADE_CATEGORIES",3,true));
1457 return $status;
1460 //Backup gradebook_item (called from backup_gradebook_info
1461 function backup_gradebook_item_info($bf,$preferences, $backupall) {
1463 global $CFG;
1464 require_once($CFG->libdir . '/gradelib.php');
1465 $status = true;
1466 // get all the grade_items, ordered by sort order since upon restoring, it is not always
1467 // possible to use the same sort order. We could at least preserve the sortorder by restoring
1468 // grade_items in the original sortorder
1469 if ($grade_items = get_records_sql("SELECT * FROM {$CFG->prefix}grade_items
1470 WHERE courseid = $preferences->backup_course
1471 ORDER BY sortorder ASC")) {
1473 //Begin grade_items tag
1474 fwrite ($bf,start_tag("GRADE_ITEMS",3,true));
1475 //Iterate for each item
1476 foreach ($grade_items as $grade_item) {
1477 // Instantiate a grade_item object, to access its methods
1478 $grade_item = grade_object::fetch_helper('grade_items', 'grade_item', $grade_item);
1480 // do not restore if this grade_item is a mod, and
1481 if ($grade_item->itemtype == 'mod') {
1482 // this still needs to be included, though grades and grades_text can be ignored
1483 } else if ($grade_item->itemtype == 'category') {
1484 // if not all grade items are being backed up
1485 // we ignore this type of grade_item and grades associated
1486 if (!$backupall) {
1487 continue;
1491 //Begin grade_item
1492 fwrite ($bf,start_tag("GRADE_ITEM",4,true));
1493 //Output individual fields
1495 fwrite ($bf,full_tag("ID",5,false,$grade_item->id));
1496 fwrite ($bf,full_tag("CATEGORYID",5,false,$grade_item->categoryid));
1497 fwrite ($bf,full_tag("ITEMNAME",5,false,$grade_item->itemname));
1498 fwrite ($bf,full_tag("ITEMTYPE",5,false,$grade_item->itemtype));
1499 fwrite ($bf,full_tag("ITEMMODULE",5,false,$grade_item->itemmodule));
1500 fwrite ($bf,full_tag("ITEMINSTANCE",5,false,$grade_item->iteminstance));
1501 fwrite ($bf,full_tag("ITEMNUMBER",5,false,$grade_item->itemnumber));
1502 fwrite ($bf,full_tag("ITEMINFO",5,false,$grade_item->iteminfo));
1503 fwrite ($bf,full_tag("IDNUMBER",5,false,$grade_item->idnumber));
1504 // use [idnumber] in calculation instead of [#giXXX#]
1505 fwrite ($bf,full_tag("CALCULATION",5,false,$grade_item->get_calculation()));
1506 fwrite ($bf,full_tag("GRADEMAX",5,false,$grade_item->grademax));
1507 fwrite ($bf,full_tag("GRADEMIN",5,false,$grade_item->grademin));
1508 fwrite ($bf,full_tag("SCALEID",5,false,$grade_item->scaleid));
1509 fwrite ($bf,full_tag("OUTCOMEID",5,false,$grade_item->outcomeid));
1510 fwrite ($bf,full_tag("GRADEPASS",5,false,$grade_item->gradepass));
1511 fwrite ($bf,full_tag("MULTFACTOR",5,false,$grade_item->multfactor));
1512 fwrite ($bf,full_tag("PLUSFACTOR",5,false,$grade_item->plusfactor));
1513 fwrite ($bf,full_tag("HIDDEN",5,false,$grade_item->hidden));
1514 fwrite ($bf,full_tag("LOCKED",5,false,$grade_item->locked));
1515 fwrite ($bf,full_tag("LOCKTIME",5,false,$grade_item->locktime));
1517 // back up the other stuff here
1518 // mod grades should only be backed up if selected
1519 if ($grade_item->itemtype == 'mod' && !backup_userdata_selected($preferences,$grade_item->itemmodule,$grade_item->iteminstance)) {
1520 // do not write grades if a mod grade_item is being restored
1521 // but userdata is not selected
1522 } else {
1523 $status = backup_gradebook_grades_info($bf,$preferences,$grade_item->id);
1524 $status = backup_gradebook_grades_text_info($bf,$preferences,$grade_item->id);
1526 //End grade_item
1527 fwrite ($bf,end_tag("GRADE_ITEM",4,true));
1529 //End grade_items tag
1530 $status = fwrite ($bf,end_tag("GRADE_ITEMS",3,true));
1533 return $status;
1535 //Backup gradebook_item (called from backup_gradebook_info
1537 function backup_gradebook_outcomes_info($bf,$preferences) {
1539 global $CFG;
1540 $status = true;
1541 // only back up courses already in the grade_outcomes_courses table
1542 $grade_outcomes = get_records_sql('SELECT go.*
1543 FROM '.$CFG->prefix.'grade_outcomes_courses goc,
1544 '.$CFG->prefix.'grade_outcomes go
1545 WHERE goc.courseid = '.$preferences->backup_course.'
1546 AND goc.outcomeid = go.id');
1548 if (!empty($grade_outcomes)) {
1549 //Begin grade_outcomes tag
1550 fwrite ($bf,start_tag("GRADE_OUTCOMES",3,true));
1551 //Iterate for each outcome
1552 foreach ($grade_outcomes as $grade_outcome) {
1553 //Begin grade_outcome
1554 fwrite ($bf,start_tag("GRADE_OUTCOME",4,true));
1555 //Output individual fields
1557 fwrite ($bf,full_tag("ID",5,false,$grade_outcome->id));
1558 fwrite ($bf,full_tag("COURSEID",5,false,$grade_outcome->courseid));
1559 fwrite ($bf,full_tag("SHORTNAME",5,false,$grade_outcome->shortname));
1560 fwrite ($bf,full_tag("FULLNAME",5,false,$grade_outcome->fullname));
1561 fwrite ($bf,full_tag("SCALEID",5,false,$grade_outcome->scaleid));
1562 fwrite ($bf,full_tag("USERMODIFIED",5,false,$grade_outcome->usermodified));
1564 //End grade_outcome
1565 fwrite ($bf,end_tag("GRADE_OUTCOME",4,true));
1567 //End grade_outcomes tag
1568 $status = fwrite ($bf,end_tag("GRADE_OUTCOMES",3,true));
1570 return $status;
1573 // outcomes assigned to this course
1574 function backup_gradebook_outcomes_courses_info($bf,$preferences) {
1576 global $CFG;
1578 $status = true;
1579 // get all global outcomes (used in this course)
1580 // and course specific outcomes
1581 // we don't need to backup all the outcomes in this case
1582 if ($outcomes_courses = get_records('grade_outcomes_courses', 'courseid', $preferences->backup_course)) {
1583 //Begin grade_outcomes tag
1584 fwrite ($bf,start_tag("GRADE_OUTCOMES_COURSES",3,true));
1585 //Iterate for each outcome
1586 foreach ($outcomes_courses as $outcomes_course) {
1587 //Begin grade_outcome
1588 fwrite ($bf,start_tag("GRADE_OUTCOMES_COURSE",4,true));
1589 //Output individual fields
1590 fwrite ($bf,full_tag("ID",5,false,$outcomes_course->id));
1591 fwrite ($bf,full_tag("OUTCOMEID",5,false,$outcomes_course->outcomeid));
1593 //End grade_outcome
1594 fwrite ($bf,end_tag("GRADE_OUTCOMES_COURSE",4,true));
1596 //End grade_outcomes tag
1597 $status = fwrite ($bf,end_tag("GRADE_OUTCOMES_COURSES",3,true));
1599 return $status;
1602 function backup_gradebook_grades_info($bf,$preferences, $itemid) {
1604 global $CFG;
1606 $status = true;
1608 // find all grades belonging to this item
1609 if ($grades = get_records('grade_grades', 'itemid', $itemid)) {
1610 fwrite ($bf,start_tag("GRADE_GRADES",5,true));
1611 foreach ($grades as $grade) {
1612 fwrite ($bf,start_tag("GRADE",6,true));
1613 fwrite ($bf,full_tag("ID",7,false,$grade->id));
1614 fwrite ($bf,full_tag("USERID",7,false,$grade->userid));
1615 fwrite ($bf,full_tag("RAWGRADE",7,false,$grade->rawgrade));
1616 fwrite ($bf,full_tag("RAWGRADEMAX",7,false,$grade->rawgrademax));
1617 fwrite ($bf,full_tag("RAWGRADEMIN",7,false,$grade->rawgrademin));
1618 fwrite ($bf,full_tag("RAWSCALEID",7,false,$grade->rawscaleid));
1619 fwrite ($bf,full_tag("USERMODIFIED",7,false,$grade->usermodified));
1620 fwrite ($bf,full_tag("FINALGRADE",7,false,$grade->finalgrade));
1621 fwrite ($bf,full_tag("HIDDEN",7,false,$grade->hidden));
1622 fwrite ($bf,full_tag("LOCKED",7,false,$grade->locked));
1623 fwrite ($bf,full_tag("LOCKTIME",7,false,$grade->locktime));
1624 fwrite ($bf,full_tag("EXPORTED",7,false,$grade->exported));
1625 fwrite ($bf,full_tag("OVERRIDDEN",7,false,$grade->overridden));
1626 fwrite ($bf,full_tag("EXCLUDED",7,false,$grade->excluded));
1627 fwrite ($bf,end_tag("GRADE",6,true));
1629 $status = fwrite ($bf,end_tag("GRADE_GRADES",5,true));
1631 return $status;
1634 function backup_gradebook_grades_text_info($bf, $preferences, $itemid) {
1636 global $CFG;
1638 $status = true;
1640 // find all grade texts belonging to this item
1641 if ($grades = get_records('grade_grades', 'itemid', $itemid)) {
1642 fwrite ($bf,start_tag("GRADE_GRADES_TEXT",5,true));
1643 foreach ($grades as $grade) {
1644 if ($texts = get_records('grade_grades_text', 'gradeid', $grade->id)) {
1645 foreach ($texts as $text) {
1646 fwrite ($bf,start_tag("GRADE_TEXT",6,true));
1647 fwrite ($bf,full_tag("ID",7,false,$text->id));
1648 fwrite ($bf,full_tag("GRADEID",7,false,$text->gradeid));
1649 fwrite ($bf,full_tag("INFORMATION",7,false,$text->information));
1650 fwrite ($bf,full_tag("INFORMATIONFORMAT",7,false,$text->informationformat));
1651 fwrite ($bf,full_tag("FEEDBACK",7,false,$text->feedback));
1652 fwrite ($bf,full_tag("FEEDBACKFORMAT",7,false,$text->feedbackformat));
1653 fwrite ($bf,end_tag("GRADE_TEXT",6,true));
1657 $status = fwrite ($bf,end_tag("GRADE_GRADES_TEXT",5,true));
1659 return $status;
1662 function backup_gradebook_categories_history_info($bf, $preferences) {
1664 global $CFG;
1665 $status = true;
1667 // find all grade categories history
1668 if ($chs = get_records('grade_categories_history', 'courseid', $preferences->backup_course)) {
1669 fwrite ($bf,start_tag("GRADE_CATEGORIES_HISTORIES",5,true));
1670 foreach ($chs as $ch) {
1671 fwrite ($bf,start_tag("GRADE_CATEGORIES_HISTORY",6,true));
1672 fwrite ($bf,full_tag("ID",7,false,$ch->id));
1673 fwrite ($bf,full_tag("OLDID",7,false,$ch->oldid));
1674 fwrite ($bf,full_tag("ACTION",7,false,$ch->action));
1675 fwrite ($bf,full_tag("SOURCE",7,false,$ch->source));
1676 fwrite ($bf,full_tag("TIMEMODIFIED",7,false,$ch->timemodified));
1677 fwrite ($bf,full_tag("LOGGEDUSER",7,false,$ch->loggeduser));
1678 fwrite ($bf,full_tag("PARENT",7,false,$ch->parent));
1679 fwrite ($bf,full_tag("DEPTH",7,false,$ch->depth));
1680 fwrite ($bf,full_tag("PATH",7,false,$ch->path));
1681 fwrite ($bf,full_tag("FULLNAME",7,false,$ch->fullname));
1682 fwrite ($bf,full_tag("AGGRETGATION",7,false,$ch->aggregation));
1683 fwrite ($bf,full_tag("KEEPHIGH",7,false,$ch->keephigh));
1684 fwrite ($bf,full_tag("DROPLOW",7,false,$ch->droplow));
1685 fwrite ($bf,end_tag("GRADE_CATEGORIES_HISTORY",6,true));
1687 $status = fwrite ($bf,end_tag("GRADE_CATEGORIES_HISTORIES",5,true));
1689 return $status;
1692 function backup_gradebook_grades_history_info($bf, $preferences) {
1694 global $CFG;
1695 $status = true;
1697 // find all grade categories history
1698 if ($chs = get_records_sql("SELECT ggh.* FROM {$CFG->prefix}grade_grades_history ggh,
1699 {$CFG->prefix}grade_items gi
1700 WHERE gi.courseid = $preferences->backup_course
1701 AND ggh.itemid = gi.id")) {
1702 fwrite ($bf,start_tag("GRADE_GRADES_HISTORIES",5,true));
1703 foreach ($chs as $ch) {
1704 fwrite ($bf,start_tag("GRADE_GRADES_HISTORY",6,true));
1705 fwrite ($bf,full_tag("ID",7,false,$ch->id));
1706 fwrite ($bf,full_tag("OLDID",7,false,$ch->oldid));
1707 fwrite ($bf,full_tag("ACTION",7,false,$ch->action));
1708 fwrite ($bf,full_tag("SOURCE",7,false,$ch->source));
1709 fwrite ($bf,full_tag("TIMEMODIFIED",7,false,$ch->timemodified));
1710 fwrite ($bf,full_tag("LOGGEDUSER",7,false,$ch->loggeduser));
1711 fwrite ($bf,full_tag("ITEMID",7,false,$ch->itemid));
1712 fwrite ($bf,full_tag("USERID",7,false,$ch->userid));
1713 fwrite ($bf,full_tag("RAWGRADE",7,false,$ch->rawgrade));
1714 fwrite ($bf,full_tag("RAWGRADEMAX",7,false,$ch->rawgrademax));
1715 fwrite ($bf,full_tag("RAWGRADEMIN",7,false,$ch->rawgrademin));
1716 fwrite ($bf,full_tag("USERMODIFIED",7,false,$ch->usermodified));
1717 fwrite ($bf,full_tag("FINALGRADE",7,false,$ch->finalgrade));
1718 fwrite ($bf,full_tag("HIDDEN",7,false,$ch->hidden));
1719 fwrite ($bf,full_tag("LOCKED",7,false,$ch->locked));
1720 fwrite ($bf,full_tag("LOCKTIME",7,false,$ch->locktime));
1721 fwrite ($bf,full_tag("EXPORTED",7,false,$ch->exported));
1722 fwrite ($bf,full_tag("OVERRIDDEN",7,false,$ch->overridden));
1723 fwrite ($bf,full_tag("EXCLUDED",7,false,$ch->excluded));
1724 fwrite ($bf,end_tag("GRADE_GRADES_HISTORY",6,true));
1726 $status = fwrite ($bf,end_tag("GRADE_GRADES_HISTORIES",5,true));
1728 return $status;
1731 function backup_gradebook_grades_text_history_info($bf, $preferences) {
1733 global $CFG;
1734 $status = true;
1736 // find all grade categories history
1737 if ($chs = get_records_sql("SELECT ggth.* FROM {$CFG->prefix}grade_grades_text_history ggth,
1738 {$CFG->prefix}grade_grades gg,
1739 {$CFG->prefix}grade_items gi
1740 WHERE gi.courseid = $preferences->backup_course
1741 AND ggth.gradeid = gg.id
1742 AND gg.itemid = gi.id")) {
1744 fwrite ($bf,start_tag("GRADE_TEXT_HISTORIES",5,true));
1745 foreach ($chs as $ch) {
1746 fwrite ($bf,start_tag("GRADE_TEXT_HISTORY",6,true));
1747 fwrite ($bf,full_tag("ID",7,false,$ch->id));
1748 fwrite ($bf,full_tag("OLDID",7,false,$ch->oldid));
1749 fwrite ($bf,full_tag("ACTION",7,false,$ch->action));
1750 fwrite ($bf,full_tag("SOURCE",7,false,$ch->source));
1751 fwrite ($bf,full_tag("TIMEMODIFIED",7,false,$ch->timemodified));
1752 fwrite ($bf,full_tag("LOGGEDUSER",7,false,$ch->loggeduser));
1753 fwrite ($bf,full_tag("GRADEID",7,false,$ch->gradeid));
1754 fwrite ($bf,full_tag("INFORMATION",7,false,$ch->information));
1755 fwrite ($bf,full_tag("INFORMATIONFORMAT",7,false,$ch->informationformat));
1756 fwrite ($bf,full_tag("FEEDBACK",7,false,$ch->feedback));
1757 fwrite ($bf,full_tag("FEEDBACKFORMAT",7,false,$ch->feedbackformat));
1758 fwrite ($bf,full_tag("USERMODIFIED",7,false,$ch->usermodified));
1759 fwrite ($bf,end_tag("GRADE_TEXT_HISTORY",6,true));
1761 $status = fwrite ($bf,end_tag("GRADE_TEXT_HISTORIES",5,true));
1763 return $status;
1766 function backup_gradebook_items_history_info($bf, $preferences) {
1768 global $CFG;
1769 $status = true;
1771 // find all grade categories history
1772 if ($chs = get_records('grade_items_history','courseid', $preferences->backup_course)) {
1773 fwrite ($bf,start_tag("GRADE_ITEM_HISTORIES",5,true));
1774 foreach ($chs as $ch) {
1775 fwrite ($bf,start_tag("GRADE_ITEM_HISTORY",6,true));
1776 fwrite ($bf,full_tag("ID",7,false,$ch->id));
1777 fwrite ($bf,full_tag("OLDID",7,false,$ch->oldid));
1778 fwrite ($bf,full_tag("ACTION",7,false,$ch->action));
1779 fwrite ($bf,full_tag("SOURCE",7,false,$ch->source));
1780 fwrite ($bf,full_tag("TIMEMODIFIED",7,false,$ch->timemodified));
1781 fwrite ($bf,full_tag("LOGGEDUSER",7,false,$ch->loggeduser));
1782 fwrite ($bf,full_tag("CATEGORYID",7,false,$ch->categoryid));
1783 fwrite ($bf,full_tag("ITEMNAME",7,false,$ch->itemname));
1784 fwrite ($bf,full_tag("ITEMTYPE",7,false,$ch->itemtype));
1785 fwrite ($bf,full_tag("ITEMMODULE",7,false,$ch->itemmodule));
1786 fwrite ($bf,full_tag("ITEMINSTANCE",7,false,$ch->iteminstance));
1787 fwrite ($bf,full_tag("ITEMNUMBER",7,false,$ch->itemnumber));
1788 fwrite ($bf,full_tag("ITEMINFO",7,false,$ch->iteminfo));
1789 fwrite ($bf,full_tag("IDNUMBER",7,false,$ch->idnumber));
1790 fwrite ($bf,full_tag("CALCULATION",7,false,$ch->calculation));
1791 fwrite ($bf,full_tag("GRADETYPE",7,false,$ch->gradetype));
1792 fwrite ($bf,full_tag("GRADEMAX",7,false,$ch->grademax));
1793 fwrite ($bf,full_tag("GRADEMIN",7,false,$ch->grademin));
1794 fwrite ($bf,full_tag("SCALEID",7,false,$ch->scaleid));
1795 fwrite ($bf,full_tag("OUTCOMEID",7,false,$ch->outcomeid));
1796 fwrite ($bf,full_tag("GRADEPASS",7,false,$ch->gradepass));
1797 fwrite ($bf,full_tag("MULTFACTOR",7,false,$ch->multfactor));
1798 fwrite ($bf,full_tag("PLUSFACTOR",7,false,$ch->plusfactor));
1799 fwrite ($bf,full_tag("AGGREGATIONCOEF",7,false,$ch->aggregationcoef));
1800 fwrite ($bf,full_tag("SORTORDER",7,false,$ch->sortorder));
1801 fwrite ($bf,full_tag("HIDDEN",7,false,$ch->hidden));
1802 fwrite ($bf,full_tag("LOCKED",7,false,$ch->locked));
1803 fwrite ($bf,full_tag("LOCKTIME",7,false,$ch->locktime));
1804 fwrite ($bf,full_tag("NEEDSUPDATE",7,false,$ch->needsupdate));
1805 fwrite ($bf,end_tag("GRADE_ITEM_HISTORY",6,true));
1807 $status = fwrite ($bf,end_tag("GRADE_ITEM_HISTORIES",5,true));
1810 return $status;
1813 function backup_gradebook_outcomes_history($bf, $preferences) {
1814 global $CFG;
1815 $status = true;
1817 // find all grade categories history
1818 if ($chs = get_records('grade_outcomes_history','courseid', $preferences->backup_course)) {
1819 fwrite ($bf,start_tag("GRADE_OUTCOME_HISTORIES",5,true));
1820 foreach ($chs as $ch) {
1821 fwrite ($bf,start_tag("GRADE_OUTCOME_HISTORY",6,true));
1822 fwrite ($bf,full_tag("ID",7,false,$ch->id));
1823 fwrite ($bf,full_tag("OLDID",7,false,$ch->oldid));
1824 fwrite ($bf,full_tag("ACTION",7,false,$ch->action));
1825 fwrite ($bf,full_tag("SOURCE",7,false,$ch->source));
1826 fwrite ($bf,full_tag("TIMEMODIFIED",7,false,$ch->timemodified));
1827 fwrite ($bf,full_tag("LOGGEDUSER",7,false,$ch->loggeduser));
1828 fwrite ($bf,full_tag("SHORTNAME",7,false,$ch->shortname));
1829 fwrite ($bf,full_tag("FULLNAME",7,false,$ch->fullname));
1830 fwrite ($bf,full_tag("SCALEID",7,false,$ch->scaleid));
1831 fwrite ($bf,full_tag("DESCRIPTION",7,false,$ch->description));
1832 fwrite ($bf,end_tag("GRADE_OUTCOME_HISTORY",6,true));
1834 $status = fwrite ($bf,end_tag("GRADE_OUTCOME_HISTORIES",5,true));
1836 return $status;
1839 //Backup scales info (common and course scales)
1840 function backup_scales_info($bf,$preferences) {
1842 global $CFG;
1844 $status = true;
1846 //Counter, points to current record
1847 $counter = 0;
1849 //Get scales (common and course scales)
1850 $scales = get_records_sql("SELECT id, courseid, userid, name, scale, description, timemodified
1851 FROM {$CFG->prefix}scale
1852 WHERE courseid = '0' or courseid = $preferences->backup_course");
1854 //Copy only used scales to $backupscales. They will be in backup (unused no). See Bug 1223.
1855 $backupscales = array();
1856 if ($scales) {
1857 foreach ($scales as $scale) {
1858 if (course_scale_used($preferences->backup_course, $scale->id)) {
1859 $backupscales[] = $scale;
1864 //Pring scales header
1865 if ($backupscales) {
1866 //Pring scales header
1867 fwrite ($bf,start_tag("SCALES",2,true));
1868 //Iterate
1869 foreach ($backupscales as $scale) {
1870 //Begin scale tag
1871 fwrite ($bf,start_tag("SCALE",3,true));
1872 //Output scale tag
1873 fwrite ($bf,full_tag("ID",4,false,$scale->id));
1874 fwrite ($bf,full_tag("COURSEID",4,false,$scale->courseid));
1875 fwrite ($bf,full_tag("USERID",4,false,$scale->userid));
1876 fwrite ($bf,full_tag("NAME",4,false,$scale->name));
1877 fwrite ($bf,full_tag("SCALETEXT",4,false,$scale->scale));
1878 fwrite ($bf,full_tag("DESCRIPTION",4,false,$scale->description));
1879 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$scale->timemodified));
1880 //End scale tag
1881 fwrite ($bf,end_tag("SCALE",3,true));
1883 //End scales tag
1884 $status = fwrite ($bf,end_tag("SCALES",2,true));
1886 return $status;
1889 //Backup events info (course events)
1890 function backup_events_info($bf,$preferences) {
1892 global $CFG;
1894 $status = true;
1896 //Counter, points to current record
1897 $counter = 0;
1899 //Get events (course events)
1900 $events = get_records_select("event","courseid='$preferences->backup_course' AND instance='0'","id");
1902 //Pring events header
1903 if ($events) {
1904 //Pring events header
1905 fwrite ($bf,start_tag("EVENTS",2,true));
1906 //Iterate
1907 foreach ($events as $event) {
1908 //Begin event tag
1909 fwrite ($bf,start_tag("EVENT",3,true));
1910 //Output event tag
1911 fwrite ($bf,full_tag("ID",4,false,$event->id));
1912 fwrite ($bf,full_tag("NAME",4,false,$event->name));
1913 fwrite ($bf,full_tag("DESCRIPTION",4,false,$event->description));
1914 fwrite ($bf,full_tag("FORMAT",4,false,$event->format));
1915 fwrite ($bf,full_tag("GROUPID",4,false,$event->groupid));
1916 fwrite ($bf,full_tag("USERID",4,false,$event->userid));
1917 fwrite ($bf,full_tag("REPEATID",4,false,$event->repeatid));
1918 fwrite ($bf,full_tag("EVENTTYPE",4,false,$event->eventtype));
1919 fwrite ($bf,full_tag("MODULENAME",4,false,$event->modulename));
1920 fwrite ($bf,full_tag("TIMESTART",4,false,$event->timestart));
1921 fwrite ($bf,full_tag("TIMEDURATION",4,false,$event->timeduration));
1922 fwrite ($bf,full_tag("VISIBLE",4,false,$event->visible));
1923 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$event->timemodified));
1924 //End event tag
1925 fwrite ($bf,end_tag("EVENT",3,true));
1927 //End events tag
1928 $status = fwrite ($bf,end_tag("EVENTS",2,true));
1930 return $status;
1933 //Backup groups info
1934 function backup_groups_info($bf,$preferences) {
1936 global $CFG;
1938 $status = true;
1939 $status2 = true;
1941 //Get groups
1942 $groups = get_records("groups","courseid",$preferences->backup_course);
1944 //Pring groups header
1945 if ($groups) {
1946 //Pring groups header
1947 fwrite ($bf,start_tag("GROUPS",2,true));
1948 //Iterate
1949 foreach ($groups as $group) {
1950 //Begin group tag
1951 fwrite ($bf,start_tag("GROUP",3,true));
1952 //Output group contents
1953 fwrite ($bf,full_tag("ID",4,false,$group->id));
1954 //fwrite ($bf,full_tag("COURSEID",4,false,$group->courseid));
1955 fwrite ($bf,full_tag("NAME",4,false,$group->name));
1956 fwrite ($bf,full_tag("DESCRIPTION",4,false,$group->description));
1957 fwrite ($bf,full_tag("ENROLMENTKEY",4,false,$group->enrolmentkey));
1958 fwrite ($bf,full_tag("PICTURE",4,false,$group->picture));
1959 fwrite ($bf,full_tag("HIDEPICTURE",4,false,$group->hidepicture));
1960 fwrite ($bf,full_tag("TIMECREATED",4,false,$group->timecreated));
1961 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$group->timemodified));
1963 //Now, backup groups_members, only if users are included
1964 if ($preferences->backup_users != 2) {
1965 $status2 = backup_groups_members_info($bf,$preferences,$group->id);
1968 //End group tag
1969 fwrite ($bf,end_tag("GROUP",3,true));
1971 //End groups tag
1972 $status = fwrite ($bf,end_tag("GROUPS",2,true));
1974 //Now save group_files
1975 if ($status && $status2) {
1976 $status2 = backup_copy_group_files($preferences);
1979 return ($status && $status2);
1982 //Backup groups_members info
1983 function backup_groups_members_info($bf,$preferences,$groupid) {
1985 global $CFG;
1987 $status = true;
1989 //Get groups_members
1990 $groups_members = get_records("groups_members","groupid",$groupid);
1992 //Pring groups_members header
1993 if ($groups_members) {
1994 //Pring groups_members header
1995 fwrite ($bf,start_tag("MEMBERS",4,true));
1996 //Iterate
1997 foreach ($groups_members as $group_member) {
1998 //Begin group_member tag
1999 fwrite ($bf,start_tag("MEMBER",5,true));
2000 //Output group_member contents
2001 fwrite ($bf,full_tag("GROUPID",6,false,$group_member->groupid));
2002 fwrite ($bf,full_tag("USERID",6,false,$group_member->userid));
2003 fwrite ($bf,full_tag("TIMEADDED",6,false,$group_member->timeadded));
2004 //End group_member tag
2005 fwrite ($bf,end_tag("MEMBER",5,true));
2007 //End groups_members tag
2008 $status = fwrite ($bf,end_tag("MEMBERS",4,true));
2010 return $status;
2013 //Backup groupings info
2014 function backup_groupings_info($bf,$preferences) {
2016 global $CFG;
2018 $status = true;
2020 //Get groups
2021 $groupings = get_records("groupings","courseid",$preferences->backup_course);
2023 //Pring groups header
2024 if ($groupings) {
2025 //Pring groups header
2026 fwrite ($bf,start_tag("GROUPINGS",2,true));
2027 //Iterate
2028 foreach ($groupings as $grouping) {
2029 //Begin group tag
2030 fwrite ($bf,start_tag("GROUPING",3,true));
2031 //Output group contents
2032 fwrite ($bf,full_tag("ID",4,false,$grouping->id));
2033 //fwrite ($bf,full_tag("COURSEID",4,false,$grouping->courseid));
2034 fwrite ($bf,full_tag("NAME",4,false,$grouping->name));
2035 fwrite ($bf,full_tag("DESCRIPTION",4,false,$grouping->description));
2036 fwrite ($bf,full_tag("CONFIGDATA",4,false,$grouping->configdata));
2037 fwrite ($bf,full_tag("TIMECREATED",4,false,$grouping->timecreated));
2038 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$grouping->timemodified));
2040 //End group tag
2041 fwrite ($bf,end_tag("GROUPING",3,true));
2043 //End groups tag
2044 $status = fwrite ($bf,end_tag("GROUPINGS",2,true));
2046 //(Now save grouping_files)
2048 return $status;
2051 //Backup groupings-groups info
2052 function backup_groupings_groups_info($bf,$preferences) {
2054 global $CFG;
2056 $status = true;
2058 //Get grouping_groups
2059 $courseid = $preferences->backup_course;
2060 $sql = "SELECT gg.* FROM {$CFG->prefix}groupings g, {$CFG->prefix}groupings_groups gg
2061 WHERE g.courseid=$courseid AND g.id=gg.groupingid";
2062 $grouping_groups = get_records_sql($sql);
2064 //Pring grouping_groups header
2065 if ($grouping_groups) {
2066 //Pring grouping_groups header
2067 fwrite ($bf,start_tag("GROUPINGSGROUPS",2,true));
2068 //Iterate
2069 foreach ($grouping_groups as $members) {
2070 //Begin grouping_group tag
2071 fwrite ($bf,start_tag("GROUPINGGROUP",3,true));
2072 //Output group_member contents
2073 fwrite ($bf,full_tag("ID",4,false,$members->id));
2074 fwrite ($bf,full_tag("GROUPINGID",4,false,$members->groupingid));
2075 fwrite ($bf,full_tag("GROUPID",4,false,$members->groupid));
2076 fwrite ($bf,full_tag("TIMEADDED",4,false,$members->timeadded));
2077 //End grouping_group tag
2078 fwrite ($bf,end_tag("GROUPINGGROUP",3,true));
2080 //End grouping_groups tag
2081 $status = fwrite ($bf,end_tag("GROUPINGSGROUPS",2,true));
2083 return $status;
2086 //Start the modules tag
2087 function backup_modules_start ($bf,$preferences) {
2089 return fwrite ($bf,start_tag("MODULES",2,true));
2092 //End the modules tag
2093 function backup_modules_end ($bf,$preferences) {
2095 return fwrite ($bf,end_tag("MODULES",2,true));
2098 //This function makes all the necesary calls to every mod
2099 //to export itself and its files !!!
2100 function backup_module($bf,$preferences,$module) {
2102 global $CFG;
2104 $status = true;
2106 require_once($CFG->dirroot.'/mod/'.$module.'/backuplib.php');
2108 if (isset($preferences->mods[$module]->instances)
2109 && is_array($preferences->mods[$module]->instances)) {
2110 $onemodbackup = $module.'_backup_one_mod';
2111 if (function_exists($onemodbackup)) {
2112 foreach ($preferences->mods[$module]->instances as $instance => $object) {
2113 if (!empty($object->backup)) {
2114 $status = $onemodbackup($bf,$preferences,$instance);
2117 } else {
2118 $status = false;
2120 } else { // whole module.
2121 //First, re-check if necessary functions exists
2122 $modbackup = $module."_backup_mods";
2123 if (function_exists($modbackup)) {
2124 //Call the function
2125 $status = $modbackup($bf,$preferences);
2126 } else {
2127 //Something was wrong. Function should exist.
2128 $status = false;
2132 return $status;
2136 //This function encode things to make backup multi-site fully functional
2137 //It does this conversions:
2138 // - $CFG->wwwroot/file.php/courseid ------------------> $@FILEPHP@$ (slasharguments links)
2139 // - $CFG->wwwroot/file.php?file=/courseid ------------> $@FILEPHP@$ (non-slasharguments links)
2140 // - Every module xxxx_encode_content_links() is executed too
2142 function backup_encode_absolute_links($content) {
2144 global $CFG,$preferences;
2146 //Use one static variable to cache all the require_once calls that,
2147 //under PHP5 seems to increase load too much, and we are requiring
2148 //them here thousands of times (one per content). MDL-8700.
2149 //Once fixed by PHP, we'll delete this hack
2151 static $includedfiles;
2152 if (!isset($includedfiles)) {
2153 $includedfiles = array();
2156 //Check if preferences is ok. If it isn't set, we are
2157 //in a scheduled_backup to we are able to get a copy
2158 //from CFG->backup_preferences
2159 if (!isset($preferences)) {
2160 $mypreferences = $CFG->backup_preferences;
2161 } else {
2162 //We are in manual backups so global preferences must exist!!
2163 $mypreferences = $preferences;
2166 //First, we check for every call to file.php inside the course
2167 $search = array($CFG->wwwroot.'/file.php/'.$mypreferences->backup_course,
2168 $CFG->wwwroot.'/file.php?file=/'.$mypreferences->backup_course);
2170 $replace = array('$@FILEPHP@$','$@FILEPHP@$');
2172 $result = str_replace($search,$replace,$content);
2174 foreach ($mypreferences->mods as $name => $info) {
2175 /// We only include the corresponding backuplib.php if it hasn't been included before
2176 /// This will save some load under PHP5. MDL-8700.
2177 /// Once fixed by PHP, we'll delete this hack
2178 if (!in_array($name, $includedfiles)) {
2179 include_once("$CFG->dirroot/mod/$name/backuplib.php");
2180 $includedfiles[] = $name;
2182 //Check if the xxxx_encode_content_links exists
2183 $function_name = $name."_encode_content_links";
2184 if (function_exists($function_name)) {
2185 $result = $function_name($result,$mypreferences);
2189 if ($result != $content) {
2190 debugging('<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />');
2193 return $result;
2196 //This function copies all the needed files under the "users" directory to the "user_files"
2197 //directory under temp/backup
2198 function backup_copy_user_files ($preferences) {
2200 global $CFG;
2202 $status = true;
2204 //First we check to "user_files" exists and create it as necessary
2205 //in temp/backup/$backup_code dir
2206 $status = check_and_create_user_files_dir($preferences->backup_unique_code);
2208 //Now iterate over directories under "users" to check if that user must be
2209 //copied to backup
2211 $rootdir = $CFG->dataroot."/users";
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 username in backup_ids
2219 $data = get_record ("backup_ids","backup_code",$preferences->backup_unique_code,
2220 "table_name","user",
2221 "old_id",$dir);
2222 //If exists, copy it
2223 if ($data) {
2224 $status = backup_copy_file($rootdir."/".$dir,
2225 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/user_files/".$dir);
2231 return $status;
2234 //This function copies all the needed files under the "groups" directory to the "group_files"
2235 //directory under temp/backup
2236 function backup_copy_group_files ($preferences) {
2238 global $CFG;
2240 $status = true;
2242 //First we check if "group_files" exists and create it as necessary
2243 //in temp/backup/$backup_code dir
2244 $status = check_and_create_group_files_dir($preferences->backup_unique_code);
2246 //Now iterate over directories under "groups" to check if that user must be
2247 //copied to backup
2249 $rootdir = $CFG->dataroot.'/groups';
2250 //Check if directory exists
2251 if (is_dir($rootdir)) {
2252 $list = list_directories ($rootdir);
2253 if ($list) {
2254 //Iterate
2255 foreach ($list as $dir) {
2256 //Look for dir like group in groups table
2257 $data = get_record ('groups', 'courseid', $preferences->backup_course,
2258 'id',$dir);
2259 //If exists, copy it
2260 if ($data) {
2261 $status = backup_copy_file($rootdir."/".$dir,
2262 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/group_files/".$dir);
2267 return $status;
2270 //This function copies all the course files under the course directory (except the moddata
2271 //directory to the "course_files" directory under temp/backup
2272 function backup_copy_course_files ($preferences) {
2274 global $CFG;
2276 $status = true;
2278 //First we check to "course_files" exists and create it as necessary
2279 //in temp/backup/$backup_code dir
2280 $status = check_and_create_course_files_dir($preferences->backup_unique_code);
2282 //Now iterate over files and directories except $CFG->moddata and backupdata to be
2283 //copied to backup
2285 $rootdir = $CFG->dataroot."/".$preferences->backup_course;
2287 $name_moddata = $CFG->moddata;
2288 $name_backupdata = "backupdata";
2289 //Check if directory exists
2290 if (is_dir($rootdir)) {
2291 $list = list_directories_and_files ($rootdir);
2292 if ($list) {
2293 //Iterate
2294 foreach ($list as $dir) {
2295 if ($dir !== $name_moddata and $dir !== $name_backupdata) {
2296 $status = backup_copy_file($rootdir."/".$dir,
2297 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/course_files/".$dir);
2302 return $status;
2305 * This function copies all the site files under the site directory (except the moddata and backupdata
2306 * directories to the "site_files" directory under temp/backup
2308 function backup_copy_site_files ($preferences) {
2310 global $CFG;
2312 $status = true;
2314 if ($preferences->backup_course == SITEID){
2315 return $status;
2318 //First we check to "site_files" exists and create it as necessary
2319 //in temp/backup/$backup_code dir
2320 $status = $status && check_and_create_site_files_dir($preferences->backup_unique_code);
2322 $rootdir = $CFG->dataroot."/".SITEID;
2326 $files = get_records_select('backup_files',
2327 "backup_code = '$preferences->backup_unique_code' AND file_type = 'site'");
2328 if ($files) {
2329 //Iterate
2330 foreach ($files as $fileobj) {
2331 //check for dir structure and create recursively
2332 $file = $fileobj->path;
2333 $status = $status && check_dir_exists(dirname($CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/site_files/".$file), true, true);
2334 $status = $status && backup_copy_file($rootdir."/".$file,
2335 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/site_files/".$file);
2338 return $status;
2340 //This function creates the zip file containing all the backup info
2341 //moodle.xml, moddata, user_files, course_files.
2342 //The zipped file is created in the backup directory and named with
2343 //the "oficial" name of the backup
2344 //It uses "pclzip" if available or system "zip" (unix only)
2345 function backup_zip ($preferences) {
2347 global $CFG;
2349 $status = true;
2351 //Base dir where everything happens
2352 $basedir = cleardoubleslashes($CFG->dataroot."/temp/backup/".$preferences->backup_unique_code);
2353 //Backup zip file name
2354 $name = $preferences->backup_name;
2355 //List of files and directories
2356 $filelist = list_directories_and_files ($basedir);
2358 //Convert them to full paths
2359 $files = array();
2360 foreach ($filelist as $file) {
2361 $files[] = "$basedir/$file";
2364 $status = zip_files($files, "$basedir/$name");
2366 //echo "<br/>Status: ".$status; //Debug
2367 return $status;
2371 //This function copies the final zip to the course dir
2372 function copy_zip_to_course_dir ($preferences) {
2374 global $CFG;
2376 $status = true;
2378 //Define zip location (from)
2379 $from_zip_file = $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/".$preferences->backup_name;
2381 //Initialise $to_zip_file
2382 $to_zip_file="";
2384 //If $preferences->backup_destination isn't empty, then copy to custom directory
2385 if (!empty($preferences->backup_destination)) {
2386 $to_zip_file = $preferences->backup_destination."/".$preferences->backup_name;
2387 } else {
2388 //Define zip destination (course dir)
2389 $to_zip_file = $CFG->dataroot."/".$preferences->backup_course;
2391 //echo "<p>From: ".$from_zip_file."<br />"; //Debug
2393 //echo "<p>Checking: ".$to_zip_file."<br />"; //Debug
2395 //Checks course dir exists
2396 $status = check_dir_exists($to_zip_file,true);
2398 //Define zip destination (backup dir)
2399 $to_zip_file = $to_zip_file."/backupdata";
2401 //echo "<p>Checking: ".$to_zip_file."<br />"; //Debug
2403 //Checks backup dir exists
2404 $status = check_dir_exists($to_zip_file,true);
2406 //Define zip destination (zip file)
2407 $to_zip_file = $to_zip_file."/".$preferences->backup_name;
2410 //echo "<p>To: ".$to_zip_file."<br />"; //Debug
2412 //Copy zip file
2413 if ($status) {
2414 $status = backup_copy_file ($from_zip_file,$to_zip_file);
2417 return $status;
2421 * compatibility function
2422 * with new granular backup
2423 * we need to know
2425 function backup_userdata_selected($preferences,$modname,$modid) {
2426 return ((empty($preferences->mods[$modname]->instances)
2427 && !empty($preferences->mods[$modname]->userinfo))
2428 || (is_array($preferences->mods[$modname]->instances)
2429 && array_key_exists($modid,$preferences->mods[$modname]->instances)
2430 && !empty($preferences->mods[$modname]->instances[$modid]->userinfo)));
2434 function backup_mod_selected($preferences,$modname,$modid) {
2435 return ((empty($preferences->mods[$modname]->instances)
2436 && !empty($preferences->mods[$modname]->backup))
2437 || (is_array($preferences->mods[$modname]->instances)
2438 && array_key_exists($modid,$preferences->mods[$modname]->instances)
2439 && !empty($preferences->mods[$modname]->instances[$modid]->backup)));
2443 * Checks for the required files/functions to backup every mod
2444 * And check if there is data about it
2446 function backup_fetch_prefs_from_request(&$preferences,&$count,$course) {
2447 global $CFG,$SESSION;
2449 // check to see if it's in the session already
2450 if (!empty($SESSION->backupprefs) && array_key_exists($course->id,$SESSION->backupprefs) && !empty($SESSION->backupprefs[$course->id])) {
2451 $sprefs = $SESSION->backupprefs[$course->id];
2452 $preferences = $sprefs;
2453 // refetch backup_name just in case.
2454 $bn = optional_param('backup_name','',PARAM_FILE);
2455 if (!empty($bn)) {
2456 $preferences->backup_name = $bn;
2458 $count = 1;
2459 return true;
2462 if ($allmods = get_records("modules") ) {
2463 foreach ($allmods as $mod) {
2464 $modname = $mod->name;
2465 $modfile = "$CFG->dirroot/mod/$modname/backuplib.php";
2466 $modbackup = $modname."_backup_mods";
2467 $modbackupone = $modname."_backup_one_mod";
2468 $modcheckbackup = $modname."_check_backup_mods";
2469 if (!file_exists($modfile)) {
2470 continue;
2472 include_once($modfile);
2473 if (!function_exists($modbackup) || !function_exists($modcheckbackup)) {
2474 continue;
2476 $var = "exists_".$modname;
2477 $preferences->$var = true;
2478 $count++;
2479 // check that there are instances and we can back them up individually
2480 if (!count_records('course_modules','course',$course->id,'module',$mod->id) || !function_exists($modbackupone)) {
2481 continue;
2483 $var = 'exists_one_'.$modname;
2484 $preferences->$var = true;
2485 $varname = $modname.'_instances';
2486 $preferences->$varname = get_all_instances_in_course($modname,$course);
2487 foreach ($preferences->$varname as $instance) {
2488 $preferences->mods[$modname]->instances[$instance->id]->name = $instance->name;
2489 $var = 'backup_'.$modname.'_instance_'.$instance->id;
2490 $$var = optional_param($var,0);
2491 $preferences->$var = $$var;
2492 $preferences->mods[$modname]->instances[$instance->id]->backup = $$var;
2493 $var = 'backup_user_info_'.$modname.'_instance_'.$instance->id;
2494 $$var = optional_param($var,0);
2495 $preferences->$var = $$var;
2496 $preferences->mods[$modname]->instances[$instance->id]->userinfo = $$var;
2497 $var = 'backup_'.$modname.'_instances';
2498 $preferences->$var = 1; // we need this later to determine what to display in modcheckbackup.
2501 //Check data
2502 //Check module info
2503 $preferences->mods[$modname]->name = $modname;
2505 $var = "backup_".$modname;
2506 $$var = optional_param( $var,0);
2507 $preferences->$var = $$var;
2508 $preferences->mods[$modname]->backup = $$var;
2510 //Check include user info
2511 $var = "backup_user_info_".$modname;
2512 $$var = optional_param( $var,0);
2513 $preferences->$var = $$var;
2514 $preferences->mods[$modname]->userinfo = $$var;
2519 //Check other parameters
2520 $preferences->backup_metacourse = optional_param('backup_metacourse',1,PARAM_INT);
2521 $preferences->backup_users = optional_param('backup_users',1,PARAM_INT);
2522 $preferences->backup_logs = optional_param('backup_logs',0,PARAM_INT);
2523 $preferences->backup_user_files = optional_param('backup_user_files',1,PARAM_INT);
2524 $preferences->backup_course_files = optional_param('backup_course_files',1,PARAM_INT);
2525 $preferences->backup_gradebook_history = optional_param('backup_gradebook_history', 1, PARAM_INT);
2526 $preferences->backup_site_files = optional_param('backup_site_files',1,PARAM_INT);
2527 $preferences->backup_messages = optional_param('backup_messages',1,PARAM_INT);
2528 $preferences->backup_course = $course->id;
2529 $preferences->backup_name = required_param('backup_name',PARAM_FILE);
2530 $preferences->backup_unique_code = required_param('backup_unique_code');
2532 // put it (back) in the session
2533 $SESSION->backupprefs[$course->id] = $preferences;
2536 /* Finds all related roles used in course, mod and blocks context
2537 * @param object $preferences
2538 * @param object $course
2539 * @return array of role objects
2541 function backup_fetch_roles($preferences) {
2543 global $CFG;
2544 $contexts = array();
2545 $roles = array();
2547 /// adding course context
2548 $coursecontext = get_context_instance(CONTEXT_COURSE, $preferences->backup_course);
2549 $contexts[$coursecontext->id] = $coursecontext;
2551 /// adding mod contexts
2552 $mods = $preferences->mods;
2553 foreach ($mods as $modname => $mod) {
2554 $instances = $mod->instances;
2555 foreach ($instances as $id => $instance) {
2556 // if this type of mod is to be backed up
2557 if ($instance->backup) {
2558 $cm = get_coursemodule_from_instance($modname, $id);
2559 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
2560 // put context in array keys
2561 $contexts[$context->id] = $context;
2566 // add all roles assigned at user context
2567 if ($preferences->backup_users) {
2568 if ($users = get_records_sql("SELECT u.old_id, u.table_name,u.info
2569 FROM {$CFG->prefix}backup_ids u
2570 WHERE u.backup_code = '$preferences->backup_unique_code' AND
2571 u.table_name = 'user'")) {
2572 foreach ($users as $user) {
2573 $context = get_context_instance(CONTEXT_USER, $user->old_id);
2574 $contexts[$context->id] = $context;
2580 // add all roles assigned at block context
2581 if ($courseblocks = get_records_sql("SELECT *
2582 FROM {$CFG->prefix}block_instance
2583 WHERE pagetype = '".PAGE_COURSE_VIEW."'
2584 AND pageid = {$preferences->backup_course}")) {
2586 foreach ($courseblocks as $courseblock) {
2588 $context = get_context_instance(CONTEXT_BLOCK, $courseblock->id);
2589 $contexts[$context->id] = $context;
2593 // foreach context, call get_roles_on_exact_context insert into array
2594 foreach ($contexts as $context) {
2595 if ($proles = get_roles_on_exact_context($context)) {
2596 foreach ($proles as $prole) {
2597 $roles[$prole->id] = $prole;
2602 return $roles;
2605 /* function to print xml for overrides */
2606 function write_role_overrides_xml($bf, $context, $startlevel) {
2607 fwrite ($bf, start_tag("ROLES_OVERRIDES", $startlevel, true));
2608 if ($roles = get_roles_with_override_on_context($context)) {
2609 foreach ($roles as $role) {
2610 fwrite ($bf, start_tag("ROLE", $startlevel+1, true));
2611 fwrite ($bf, full_tag("ID", $startlevel+2, false, $role->id));
2612 fwrite ($bf, full_tag("NAME", $startlevel+2, false, $role->name));
2613 fwrite ($bf, full_tag("SHORTNAME", $startlevel+2, false, $role->shortname));
2614 fwrite ($bf, start_tag("CAPABILITIES", $startlevel+2, true));
2615 if ($capabilities = get_capabilities_from_role_on_context($role, $context)) {
2616 foreach ($capabilities as $capability) {
2617 fwrite ($bf, start_tag("CAPABILITY", $startlevel+3, true));
2618 fwrite ($bf, full_tag("NAME", $startlevel+4, false, $capability->capability));
2619 fwrite ($bf, full_tag("PERMISSION", $startlevel+4, false, $capability->permission));
2620 fwrite ($bf, full_tag("TIMEMODIFIED", $startlevel+4, false, $capability->timemodified));
2621 if (!isset($capability->modifierid)) {
2622 $capability->modifierid = 0;
2624 fwrite ($bf, full_tag("MODIFIERID", $startlevel+4, false, $capability->modifierid));
2625 fwrite ($bf, end_tag("CAPABILITY", $startlevel+3, true));
2628 fwrite ($bf, end_tag("CAPABILITIES", $startlevel+2, true));
2629 fwrite ($bf, end_tag("ROLE", $startlevel+1, true));
2632 fwrite ($bf, end_tag("ROLES_OVERRIDES", $startlevel, true));
2635 /* function to print xml for assignment */
2636 function write_role_assignments_xml($bf, $context, $startlevel) {
2637 /// write role_assign code here
2638 fwrite ($bf, start_tag("ROLES_ASSIGNMENTS", $startlevel, true));
2640 if ($roles = get_roles_with_assignment_on_context($context)) {
2641 foreach ($roles as $role) {
2642 fwrite ($bf, start_tag("ROLE", $startlevel+1, true));
2643 fwrite ($bf, full_tag("ID", $startlevel+2, false, $role->id));
2644 fwrite ($bf, full_tag("NAME", $startlevel+2, false, $role->name));
2645 fwrite ($bf, full_tag("SHORTNAME", $startlevel+2, false, $role->shortname));
2646 fwrite ($bf, start_tag("ASSIGNMENTS", $startlevel+2, true));
2647 if ($assignments = get_users_from_role_on_context($role, $context)) {
2648 foreach ($assignments as $assignment) {
2649 fwrite ($bf, start_tag("ASSIGNMENT", $startlevel+3, true));
2650 fwrite ($bf, full_tag("USERID", $startlevel+4, false, $assignment->userid));
2651 fwrite ($bf, full_tag("HIDDEN", $startlevel+4, false, $assignment->hidden));
2652 fwrite ($bf, full_tag("TIMESTART", $startlevel+4, false, $assignment->timestart));
2653 fwrite ($bf, full_tag("TIMEEND", $startlevel+4, false, $assignment->timeend));
2654 fwrite ($bf, full_tag("TIMEMODIFIED", $startlevel+4, false, $assignment->timemodified));
2655 if (!isset($assignment->modifierid)) {
2656 $assignment->modifierid = 0;
2658 fwrite ($bf, full_tag("MODIFIERID", $startlevel+4, false, $assignment->modifierid));
2659 fwrite ($bf, full_tag("ENROL", $startlevel+4, false, $assignment->enrol));
2660 fwrite ($bf, full_tag("SORTORDER", $startlevel+4, false, $assignment->sortorder));
2661 fwrite ($bf, end_tag("ASSIGNMENT", $startlevel+3, true));
2664 fwrite ($bf, end_tag("ASSIGNMENTS", $startlevel+2, true));
2665 fwrite ($bf, end_tag("ROLE", $startlevel+1, true));
2668 fwrite ($bf, end_tag("ROLES_ASSIGNMENTS", $startlevel, true));
2672 function backup_execute(&$preferences, &$errorstr) {
2673 global $CFG;
2674 $status = true;
2676 //Check for temp and backup and backup_unique_code directory
2677 //Create them as needed
2678 if (!defined('BACKUP_SILENTLY')) {
2679 echo "<li>".get_string("creatingtemporarystructures").'</li>';
2682 $status = check_and_create_backup_dir($preferences->backup_unique_code);
2683 //Empty dir
2684 if ($status) {
2685 $status = clear_backup_dir($preferences->backup_unique_code);
2688 //Delete old_entries from backup tables
2689 if (!defined('BACKUP_SILENTLY')) {
2690 echo "<li>".get_string("deletingolddata").'</li>';
2692 $status = backup_delete_old_data();
2693 if (!$status) {
2694 if (!defined('BACKUP_SILENTLY')) {
2695 error ("An error occurred deleting old backup data");
2697 else {
2698 $errorstr = "An error occurred deleting old backup data";
2699 return false;
2703 //Create the moodle.xml file
2704 if ($status) {
2705 if (!defined('BACKUP_SILENTLY')) {
2706 echo "<li>".get_string("creatingxmlfile");
2707 //Begin a new list to xml contents
2708 echo "<ul>";
2709 echo "<li>".get_string("writingheader").'</li>';
2711 //Obtain the xml file (create and open) and print prolog information
2712 $backup_file = backup_open_xml($preferences->backup_unique_code);
2713 if (!defined('BACKUP_SILENTLY')) {
2714 echo "<li>".get_string("writinggeneralinfo").'</li>';
2716 //Prints general info about backup to file
2717 if ($backup_file) {
2718 if (!$status = backup_general_info($backup_file,$preferences)) {
2719 if (!defined('BACKUP_SILENTLY')) {
2720 notify("An error occurred while backing up general info");
2722 else {
2723 $errorstr = "An error occurred while backing up general info";
2724 return false;
2728 if (!defined('BACKUP_SILENTLY')) {
2729 echo "<li>".get_string("writingcoursedata");
2730 //Start new ul (for course)
2731 echo "<ul>";
2732 echo "<li>".get_string("courseinfo").'</li>';
2734 //Prints course start (tag and general info)
2735 if ($status) {
2736 if (!$status = backup_course_start($backup_file,$preferences)) {
2737 if (!defined('BACKUP_SILENTLY')) {
2738 notify("An error occurred while backing up course start");
2740 else {
2741 $errorstr = "An error occurred while backing up course start";
2742 return false;
2746 //Metacourse information
2747 if ($status && $preferences->backup_metacourse) {
2748 if (!defined('BACKUP_SILENTLY')) {
2749 echo "<li>".get_string("metacourse").'</li>';
2751 if (!$status = backup_course_metacourse($backup_file,$preferences)) {
2752 if (!defined('BACKUP_SILENTLY')) {
2753 notify("An error occurred while backing up metacourse info");
2755 else {
2756 $errorstr = "An error occurred while backing up metacourse info";
2757 return false;
2761 if (!defined('BACKUP_SILENTLY')) {
2762 echo "<li>".get_string("blocks").'</li>';
2764 //Blocks information
2765 if ($status) {
2766 if (!$status = backup_course_blocks($backup_file,$preferences)) {
2767 if (!defined('BACKUP_SILENTLY')) {
2768 notify("An error occurred while backing up course blocks");
2770 else {
2771 $errorstr = "An error occurred while backing up course blocks";
2772 return false;
2776 if (!defined('BACKUP_SILENTLY')) {
2777 echo "<li>".get_string("sections").'</li>';
2779 //Section info
2780 if ($status) {
2781 if (!$status = backup_course_sections($backup_file,$preferences)) {
2782 if (!defined('BACKUP_SILENTLY')) {
2783 notify("An error occurred while backing up course sections");
2785 else {
2786 $errorstr = "An error occurred while backing up course sections";
2787 return false;
2792 //End course contents (close ul)
2793 if (!defined('BACKUP_SILENTLY')) {
2794 echo "</ul></li>";
2797 //User info
2798 if ($status) {
2799 if (!defined('BACKUP_SILENTLY')) {
2800 echo "<li>".get_string("writinguserinfo").'</li>';
2802 if (!$status = backup_user_info($backup_file,$preferences)) {
2803 if (!defined('BACKUP_SILENTLY')) {
2804 notify("An error occurred while backing up user info");
2806 else {
2807 $errorstr = "An error occurred while backing up user info";
2808 return false;
2813 //If we have selected to backup messages and we are
2814 //doing a SITE backup, let's do it
2815 if ($status && $preferences->backup_messages && $preferences->backup_course == SITEID) {
2816 if (!defined('BACKUP_SILENTLY')) {
2817 echo "<li>".get_string("writingmessagesinfo").'</li>';
2819 if (!$status = backup_messages($backup_file,$preferences)) {
2820 if (!defined('BACKUP_SILENTLY')) {
2821 notify("An error occurred while backing up messages");
2823 else {
2824 $errorstr = "An error occurred while backing up messages";
2825 return false;
2830 //If we have selected to backup quizzes or other modules that use questions
2831 //we've already added ids of categories and questions to backup to backup_ids table
2832 if ($status) {
2833 if (!defined('BACKUP_SILENTLY')) {
2834 echo "<li>".get_string("writingcategoriesandquestions").'</li>';
2836 require_once($CFG->dirroot.'/question/backuplib.php');
2837 if (!$status = backup_question_categories($backup_file, $preferences)) {
2838 if (!defined('BACKUP_SILENTLY')) {
2839 notify("An error occurred while backing up quiz categories");
2841 else {
2842 $errorstr = "An error occurred while backing up quiz categories";
2843 return false;
2848 //Print logs if selected
2849 if ($status) {
2850 if ($preferences->backup_logs) {
2851 if (!defined('BACKUP_SILENTLY')) {
2852 echo "<li>".get_string("writingloginfo").'</li>';
2854 if (!$status = backup_log_info($backup_file,$preferences)) {
2855 if (!defined('BACKUP_SILENTLY')) {
2856 notify("An error occurred while backing up log info");
2858 else {
2859 $errorstr = "An error occurred while backing up log info";
2860 return false;
2866 //Print scales info
2867 if ($status) {
2868 if (!defined('BACKUP_SILENTLY')) {
2869 echo "<li>".get_string("writingscalesinfo").'</li>';
2871 if (!$status = backup_scales_info($backup_file,$preferences)) {
2872 if (!defined('BACKUP_SILENTLY')) {
2873 notify("An error occurred while backing up scales");
2875 else {
2876 $errorstr = "An error occurred while backing up scales";
2877 return false;
2882 //Print groups info
2883 if ($status) {
2884 if (!defined('BACKUP_SILENTLY')) {
2885 echo "<li>".get_string("writinggroupsinfo").'</li>';
2887 if (!$status = backup_groups_info($backup_file,$preferences)) {
2888 if (!defined('BACKUP_SILENTLY')) {
2889 notify("An error occurred while backing up groups");
2891 else {
2892 $errostr = "An error occurred while backing up groups";
2893 return false;
2898 //Print groupings info
2899 if ($status) {
2900 if (!defined('BACKUP_SILENTLY')) {
2901 echo "<li>".get_string("writinggroupingsinfo").'</li>';
2903 if (!$status = backup_groupings_info($backup_file,$preferences)) {
2904 if (!defined('BACKUP_SILENTLY')) {
2905 notify("An error occurred while backing up groupings");
2907 else {
2908 $errorstr = "An error occurred while backing up groupings";
2909 return false;
2914 //Print groupings_groups info
2915 if ($status) {
2916 if (!defined('BACKUP_SILENTLY')) {
2917 echo "<li>".get_string("writinggroupingsgroupsinfo").'</li>';
2919 if (!$status = backup_groupings_groups_info($backup_file,$preferences)) {
2920 if (!defined('BACKUP_SILENTLY')) {
2921 notify("An error occurred while backing up groupings groups");
2923 else {
2924 $errorstr = "An error occurred while backing up groupings groups";
2925 return false;
2930 //Print events info
2931 if ($status) {
2932 if (!defined('BACKUP_SILENTLY')) {
2933 echo "<li>".get_string("writingeventsinfo").'</li>';
2935 if (!$status = backup_events_info($backup_file,$preferences)) {
2936 if (!defined('BACKUP_SILENTLY')) {
2937 notify("An error occurred while backing up events");
2939 else {
2940 $errorstr = "An error occurred while backing up events";
2941 return false;
2946 //Print gradebook info
2947 if ($status) {
2948 if (!defined('BACKUP_SILENTLY')) {
2949 echo "<li>".get_string("writinggradebookinfo").'</li>';
2951 if (!$status = backup_gradebook_info($backup_file,$preferences)) {
2952 if (!defined('BACKUP_SILENTLY')) {
2953 notify("An error occurred while backing up gradebook");
2955 else {
2956 $errorstr = "An error occurred while backing up gradebook";
2957 return false;
2962 //Module info, this unique function makes all the work!!
2963 //db export and module fileis copy
2964 if ($status) {
2965 $mods_to_backup = false;
2966 //Check if we have any mod to backup
2967 foreach ($preferences->mods as $module) {
2968 if ($module->backup) {
2969 $mods_to_backup = true;
2972 //If we have to backup some module
2973 if ($mods_to_backup) {
2974 if (!defined('BACKUP_SILENTLY')) {
2975 echo "<li>".get_string("writingmoduleinfo");
2977 //Start modules tag
2978 if (!$status = backup_modules_start ($backup_file,$preferences)) {
2979 if (!defined('BACKUP_SILENTLY')) {
2980 notify("An error occurred while backing up module info");
2982 else {
2983 $errorstr = "An error occurred while backing up module info";
2984 return false;
2987 //Open ul for module list
2988 if (!defined('BACKUP_SILENTLY')) {
2989 echo "<ul>";
2991 //Iterate over modules and call backup
2992 foreach ($preferences->mods as $module) {
2993 if ($module->backup and $status) {
2994 if (!defined('BACKUP_SILENTLY')) {
2995 echo "<li>".get_string("modulenameplural",$module->name).'</li>';
2997 if (!$status = backup_module($backup_file,$preferences,$module->name)) {
2998 if (!defined('BACKUP_SILENTLY')) {
2999 notify("An error occurred while backing up '$module->name'");
3001 else {
3002 $errorstr = "An error occurred while backing up '$module->name'";
3003 return false;
3008 //Close ul for module list
3009 if (!defined('BACKUP_SILENTLY')) {
3010 echo "</ul></li>";
3012 //Close modules tag
3013 if (!$status = backup_modules_end ($backup_file,$preferences)) {
3014 if (!defined('BACKUP_SILENTLY')) {
3015 notify("An error occurred while finishing the module backups");
3017 else {
3018 $errorstr = "An error occurred while finishing the module backups";
3019 return false;
3025 //Backup course format data, if any.
3026 if (!defined('BACKUP_SILENTLY')) {
3027 echo '<li>'.get_string("courseformatdata").'</li>';
3029 if($status) {
3030 if (!$status = backup_format_data($backup_file,$preferences)) {
3031 if (!defined('BACKUP_SILENTLY')) {
3032 notify("An error occurred while backing up the course format data");
3034 else {
3035 $errorstr = "An error occurred while backing up the course format data";
3036 return false;
3041 //Prints course end
3042 if ($status) {
3043 if (!$status = backup_course_end($backup_file,$preferences)) {
3044 if (!defined('BACKUP_SILENTLY')) {
3045 notify("An error occurred while closing the course backup");
3047 else {
3048 $errorstr = "An error occurred while closing the course backup";
3049 return false;
3053 //Close the xml file and xml data
3054 if ($backup_file) {
3055 backup_close_xml($backup_file);
3058 //End xml contents (close ul)
3059 if (!defined('BACKUP_SILENTLY')) {
3060 echo "</ul></li>";
3064 //Now, if selected, copy user files
3065 if ($status) {
3066 if ($preferences->backup_user_files) {
3067 if (!defined('BACKUP_SILENTLY')) {
3068 echo "<li>".get_string("copyinguserfiles").'</li>';
3070 if (!$status = backup_copy_user_files ($preferences)) {
3071 if (!defined('BACKUP_SILENTLY')) {
3072 notify("An error occurred while copying user files");
3074 else {
3075 $errorstr = "An error occurred while copying user files";
3076 return false;
3082 //Now, if selected, copy course files
3083 if ($status) {
3084 if ($preferences->backup_course_files) {
3085 if (!defined('BACKUP_SILENTLY')) {
3086 echo "<li>".get_string("copyingcoursefiles").'</li>';
3088 if (!$status = backup_copy_course_files ($preferences)) {
3089 if (!defined('BACKUP_SILENTLY')) {
3090 notify("An error occurred while copying course files");
3092 else {
3093 $errorstr = "An error occurred while copying course files";
3094 return false;
3099 //Now, if selected, copy site files
3100 if ($status) {
3101 if ($preferences->backup_site_files) {
3102 if (!defined('BACKUP_SILENTLY')) {
3103 echo "<li>".get_string("copyingsitefiles").'</li>';
3105 if (!$status = backup_copy_site_files ($preferences)) {
3106 if (!defined('BACKUP_SILENTLY')) {
3107 notify("An error occurred while copying site files");
3109 else {
3110 $errorstr = "An error occurred while copying site files";
3111 return false;
3116 //Now, zip all the backup directory contents
3117 if ($status) {
3118 if (!defined('BACKUP_SILENTLY')) {
3119 echo "<li>".get_string("zippingbackup").'</li>';
3121 if (!$status = backup_zip ($preferences)) {
3122 if (!defined('BACKUP_SILENTLY')) {
3123 notify("An error occurred while zipping the backup");
3125 else {
3126 $errorstr = "An error occurred while zipping the backup";
3127 return false;
3132 //Now, copy the zip file to course directory
3133 if ($status) {
3134 if (!defined('BACKUP_SILENTLY')) {
3135 echo "<li>".get_string("copyingzipfile").'</li>';
3137 if (!$status = copy_zip_to_course_dir ($preferences)) {
3138 if (!defined('BACKUP_SILENTLY')) {
3139 notify("An error occurred while copying the zip file to the course directory");
3141 else {
3142 $errorstr = "An error occurred while copying the zip file to the course directory";
3143 return false;
3148 //Now, clean temporary data (db and filesystem)
3149 if ($status) {
3150 if (!defined('BACKUP_SILENTLY')) {
3151 echo "<li>".get_string("cleaningtempdata").'</li>';
3153 if (!$status = clean_temp_data ($preferences)) {
3154 if (!defined('BACKUP_SILENTLY')) {
3155 notify("An error occurred while cleaning up temporary data");
3157 else {
3158 $errorstr = "An error occurred while cleaning up temporary data";
3159 return false;
3164 return $status;
3168 * This function generates the default zipfile name for a backup
3169 * based on the course id and the unique code.
3171 * @param object $course course object
3172 * @param string $backup_unique_code (optional, if left out current timestamp used)
3175 * @return string filename (excluding path information)
3177 function backup_get_zipfile_name($course, $backup_unique_code='') {
3179 if (empty($backup_unique_code)) {
3180 $backup_unique_code = time();
3183 //Calculate the backup word
3184 //Take off some characters in the filename !!
3185 $takeoff = array(" ", ":", "/", "\\", "|");
3186 $backup_word = str_replace($takeoff,"_",moodle_strtolower(get_string("backupfilename")));
3187 //If non-translated, use "backup"
3188 if (substr($backup_word,0,1) == "[") {
3189 $backup_word= "backup";
3192 //Calculate the date format string
3193 $backup_date_format = str_replace(" ","_",get_string("backupnameformat"));
3194 //If non-translated, use "%Y%m%d-%H%M"
3195 if (substr($backup_date_format,0,1) == "[") {
3196 $backup_date_format = "%%Y%%m%%d-%%H%%M";
3199 //Calculate the shortname
3200 $backup_shortname = clean_filename($course->shortname);
3201 if (empty($backup_shortname) or $backup_shortname == '_' ) {
3202 $backup_shortname = $course->id;
3205 //Calculate the final backup filename
3206 //The backup word
3207 $backup_name = $backup_word."-";
3208 //The shortname
3209 $backup_name .= moodle_strtolower($backup_shortname)."-";
3210 //The date format
3211 $backup_name .= userdate(time(),$backup_date_format,99,false);
3212 //The extension
3213 $backup_name .= ".zip";
3214 //And finally, clean everything
3215 $backup_name = clean_filename($backup_name);
3217 return $backup_name;
3222 * This function adds on the standard items to the preferences
3223 * Like moodle version and backup version
3225 * @param object $preferences existing preferences object.
3226 * (passed by reference)
3228 function backup_add_static_preferences(&$preferences) {
3229 global $CFG;
3230 $preferences->moodle_version = $CFG->version;
3231 $preferences->moodle_release = $CFG->release;
3232 $preferences->backup_version = $CFG->backup_version;
3233 $preferences->backup_release = $CFG->backup_release;