MDL-22449 help string update
[moodle.git] / backup / lib.php
blobec23bf0a9daad336d7b1c0b9a70c4afed4789e93
1 <?php
2 //This file contains all the general function needed (file manipulation...)
3 //not directly part of the backup/restore utility plus some constants
5 // Define "restoreto" options
6 define('RESTORETO_CURRENT_DELETING', 0);
7 define('RESTORETO_CURRENT_ADDING', 1);
8 define('RESTORETO_NEW_COURSE', 2);
9 define('RESTORETO_EXISTING_DELETING', 3);
10 define('RESTORETO_EXISTING_ADDING', 4);
12 require_once($CFG->libdir . '/completionlib.php');
14 //Sets a name/value pair in config_plugin table
15 function backup_set_config($name, $value) {
16 return set_config($name, $value, 'backup');
19 //Gets all the information from config_plugin table
20 function backup_get_config() {
21 $backup_config = get_config('backup');
22 return (object)$backup_config;
25 //Delete old data in backup tables (if exists)
26 //Four hours seem to be appropiate now that backup is stable
27 function backup_delete_old_data() {
28 global $CFG, $DB;
30 //Change this if you want !!
31 $hours = 4;
32 //End change this
33 $seconds = $hours * 60 * 60;
34 $delete_from = time()-$seconds;
35 //Now delete from tables
36 $status = $DB->execute("DELETE FROM {backup_ids}
37 WHERE backup_code < ?", array($delete_from));
38 if ($status) {
39 $status = $DB->execute("DELETE FROM {backup_files}
40 WHERE backup_code < ?", array($delete_from));
42 //Now, delete old directory (if exists)
43 if ($status) {
44 $status = backup_delete_old_dirs($delete_from);
46 return($status);
49 //Function to delete dirs/files into temp/backup directory
50 //older than $delete_from
51 function backup_delete_old_dirs($delete_from) {
53 global $CFG;
55 $status = true;
56 //Get files and directories in the temp backup dir witout descend
57 $list = get_directory_list($CFG->dataroot."/temp/backup", "", false, true, true);
58 foreach ($list as $file) {
59 $file_path = $CFG->dataroot."/temp/backup/".$file;
60 $moddate = filemtime($file_path);
61 if ($status && $moddate < $delete_from) {
62 //If directory, recurse
63 if (is_dir($file_path)) {
64 $status = delete_dir_contents($file_path);
65 //There is nothing, delete the directory itself
66 if ($status) {
67 $status = rmdir($file_path);
69 //If file
70 } else {
71 unlink("$file_path");
76 return $status;
79 //Function to check and create the needed dir to
80 //save all the backup
81 function check_and_create_backup_dir($backup_unique_code) {
82 global $CFG;
84 $status = check_dir_exists($CFG->dataroot."/temp",true);
85 if ($status) {
86 $status = check_dir_exists($CFG->dataroot."/temp/backup",true);
88 if ($status) {
89 $status = check_dir_exists($CFG->dataroot."/temp/backup/".$backup_unique_code,true);
92 return $status;
95 //Function to delete all the directory contents recursively
96 //it supports a excluded dit too
97 //Copied from the web !!
98 function delete_dir_contents ($dir,$excludeddir="") {
100 if (!is_dir($dir)) {
101 // if we've been given a directory that doesn't exist yet, return true.
102 // this happens when we're trying to clear out a course that has only just
103 // been created.
104 return true;
106 $slash = "/";
108 // Create arrays to store files and directories
109 $dir_files = array();
110 $dir_subdirs = array();
112 // Make sure we can delete it
113 chmod($dir, 0777);
115 if ((($handle = opendir($dir))) == FALSE) {
116 // The directory could not be opened
117 return false;
120 // Loop through all directory entries, and construct two temporary arrays containing files and sub directories
121 while(false !== ($entry = readdir($handle))) {
122 if (is_dir($dir. $slash .$entry) && $entry != ".." && $entry != "." && $entry != $excludeddir) {
123 $dir_subdirs[] = $dir. $slash .$entry;
125 else if ($entry != ".." && $entry != "." && $entry != $excludeddir) {
126 $dir_files[] = $dir. $slash .$entry;
130 // Delete all files in the curent directory return false and halt if a file cannot be removed
131 for($i=0; $i<count($dir_files); $i++) {
132 chmod($dir_files[$i], 0777);
133 if (((unlink($dir_files[$i]))) == FALSE) {
134 return false;
138 // Empty sub directories and then remove the directory
139 for($i=0; $i<count($dir_subdirs); $i++) {
140 chmod($dir_subdirs[$i], 0777);
141 if (delete_dir_contents($dir_subdirs[$i]) == FALSE) {
142 return false;
144 else {
145 if (remove_dir($dir_subdirs[$i]) == FALSE) {
146 return false;
151 // Close directory
152 closedir($handle);
154 // Success, every thing is gone return true
155 return true;
158 //Function to clear (empty) the contents of the backup_dir
159 function clear_backup_dir($backup_unique_code) {
160 global $CFG;
162 $rootdir = $CFG->dataroot."/temp/backup/".$backup_unique_code;
164 //Delete recursively
165 $status = delete_dir_contents($rootdir);
167 return $status;
170 //Returns the module type of a course_module's id in a course
171 function get_module_type ($courseid,$moduleid) {
172 global $DB;
174 $results = $DB->get_records_sql("SELECT cm.id, m.name
175 FROM {course_modules} cm, {modules} m
176 WHERE cm.course = ? AND cm.id = ? AND
177 m.id = cm.module", array($courseid, $moduleid));
179 if ($results) {
180 $name = $results[$moduleid]->name;
181 } else {
182 $name = false;
184 return $name;
187 //This function return the names of all directories under a give directory
188 //Not recursive
189 function list_directories ($rootdir) {
191 $results = null;
193 $dir = opendir($rootdir);
194 while (false !== ($file=readdir($dir))) {
195 if ($file=="." || $file=="..") {
196 continue;
198 if (is_dir($rootdir."/".$file)) {
199 $results[$file] = $file;
202 closedir($dir);
203 return $results;
206 //This function return the names of all directories and files under a give directory
207 //Not recursive
208 function list_directories_and_files ($rootdir) {
210 $results = "";
212 $dir = opendir($rootdir);
213 while (false !== ($file=readdir($dir))) {
214 if ($file=="." || $file=="..") {
215 continue;
217 $results[$file] = $file;
219 closedir($dir);
220 return $results;
223 //This function clean data from backup tables and
224 //delete all temp files used
225 function clean_temp_data ($preferences) {
226 global $CFG, $DB;
228 $status = true;
230 //true->do it, false->don't do it. To debug if necessary.
231 if (true) {
232 //Now delete from tables
233 $status = $DB->delete_records('backup_ids', array('backup_code'=>$preferences->backup_unique_code))
234 && $DB->delete_records('backup_files', array('backup_code'=>$preferences->backup_unique_code));
236 //Now, delete temp directory (if exists)
237 $file_path = $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code;
238 if (is_dir($file_path)) {
239 $status = delete_dir_contents($file_path);
240 //There is nothing, delete the directory itself
241 if ($status) {
242 $status = rmdir($file_path);
246 return $status;
249 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
250 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
251 //This functions are used to copy any file or directory ($from_file)
252 //to a new file or directory ($to_file). It works recursively and
253 //mantains file perms.
254 //I've copied it from: http://www.php.net/manual/en/function.copy.php
255 //Little modifications done
257 function backup_copy_file ($from_file,$to_file,$log_clam=false) {
258 global $CFG;
260 if (is_file($from_file)) {
261 //echo "<br />Copying ".$from_file." to ".$to_file; //Debug
262 //$perms=fileperms($from_file);
263 //return copy($from_file,$to_file) && chmod($to_file,$perms);
264 umask(0000);
265 if (copy($from_file,$to_file)) {
266 chmod($to_file,$CFG->directorypermissions);
267 if (!empty($log_clam)) {
268 //clam_log_upload($to_file,null,true);
270 return true;
272 return false;
274 else if (is_dir($from_file)) {
275 return backup_copy_dir($from_file,$to_file);
277 else{
278 //echo "<br />Error: not file or dir ".$from_file; //Debug
279 return false;
283 function backup_copy_dir($from_file,$to_file) {
284 global $CFG;
286 $status = true; // Initialize this, next code will change its value if needed
288 if (!is_dir($to_file)) {
289 //echo "<br />Creating ".$to_file; //Debug
290 umask(0000);
291 $status = mkdir($to_file,$CFG->directorypermissions);
293 $dir = opendir($from_file);
294 while (false !== ($file=readdir($dir))) {
295 if ($file=="." || $file=="..") {
296 continue;
298 $status = backup_copy_file ("$from_file/$file","$to_file/$file");
300 closedir($dir);
301 return $status;
303 ///Ends copy file/dirs functions
304 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
305 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
308 * Are we restoring a backup that was made on the same site that we are restoring to?
309 * This relies on some information that was only added to backup files in January 2009.
310 * For older backup files, fall back to guessing based on wwwroot. MDL-16614 explains
311 * when this guess could give the wrong answer.
312 * @return boolean true if the backup was made on the same site we are restoring to.
314 function backup_is_same_site(&$restore) {
315 global $CFG;
316 static $hashedsiteid = null;
317 if (is_null($hashedsiteid)) {
318 $hashedsiteid = md5(get_site_identifier());
320 if (!empty($restore->original_siteidentifier)) {
321 return $restore->original_siteidentifier == $hashedsiteid;
322 } else {
323 return $restore->original_wwwroot == $CFG->wwwroot;
327 //This function is used to insert records in the backup_ids table
328 //If the info field is greater than max_db_storage, then its info
329 //is saved to filesystem
330 function backup_putid($backup_unique_code, $table, $old_id, $new_id, $info="") {
331 global $CFG, $DB;
333 $max_db_storage = 128; //Max bytes to save to db, else save to file
335 $status = true;
337 //First delete to avoid PK duplicates
338 $status = backup_delid($backup_unique_code, $table, $old_id);
340 //Now, serialize info
341 $info_ser = serialize($info);
343 //Now, if the size of $info_ser > $max_db_storage, save it to filesystem and
344 //insert a "infile" in the info field
346 if (strlen($info_ser) > $max_db_storage) {
347 //Calculate filename (in current_backup_dir, $backup_unique_code_$table_$old_id.info)
348 $filename = $CFG->dataroot."/temp/backup/".$backup_unique_code."/".$backup_unique_code."_".$table."_".$old_id.".info";
349 //Save data to file
350 $status = backup_data2file($filename,$info_ser);
351 //Set info_to save
352 $info_to_save = "infile";
353 } else {
354 //Saving to db
355 $info_to_save = $info_ser;
358 //Now, insert the record
359 if ($status) {
360 //Build the record
361 $rec = new object();
362 $rec->backup_code = $backup_unique_code;
363 $rec->table_name = $table;
364 $rec->old_id = $old_id;
365 $rec->new_id = ($new_id === null? 0 : $new_id);
366 $rec->info = $info_to_save;
368 $DB->insert_record('backup_ids', $rec, false);
370 return $status;
373 //This function is used to delete recods from the backup_ids table
374 //If the info field is "infile" then the file is deleted too
375 function backup_delid ($backup_unique_code, $table, $old_id) {
376 global $DB;
377 return $DB->delete_records('backup_ids', array('backup_code'=>$backup_unique_code, 'table_name'=>$table, 'old_id'=>$old_id));
380 //This function is used to get a record from the backup_ids table
381 //If the info field is "infile" then its info
382 //is read from filesystem
383 function backup_getid ($backup_unique_code, $table, $old_id) {
384 global $CFG, $DB;
386 $status = true;
387 $status2 = true;
389 $status = $DB->get_record("backup_ids", array("backup_code"=>$backup_unique_code,
390 "table_name"=>$table, "old_id"=>$old_id));
392 //If info field = "infile", get file contents
393 if (!empty($status->info) && $status->info == "infile") {
394 $filename = $CFG->dataroot."/temp/backup/".$backup_unique_code."/".$backup_unique_code."_".$table."_".$old_id.".info";
395 //Read data from file
396 $status2 = backup_file2data($filename,$info);
397 if ($status2) {
398 //unserialize data
399 $status->info = unserialize($info);
400 } else {
401 $status = false;
403 } else {
404 //Only if status (record exists)
405 if (!empty($status->info)) {
406 if ($status->info === 'needed') {
407 // TODO: ugly hack - fix before 1.9.1
408 debugging('Incorrect string "needed" in $status->info, please fix the code (table:'.$table.'; old_id:'.$old_id.').', DEBUG_DEVELOPER);
409 } else {
410 ////First strip slashes
411 $temp = $status->info;
412 //Now unserialize
413 $status->info = unserialize($temp);
418 return $status;
421 //This function is used to add slashes (and decode from UTF-8 if needed)
422 //It's used intensivelly when restoring modules and saving them in db
423 function backup_todb ($data) {
424 // MDL-10770
425 if ($data === '$@NULL@$') {
426 return null;
427 } else {
428 return restore_decode_absolute_links($data);
432 //This function is used to check that every necessary function to
433 //backup/restore exists in the current php installation. Thanks to
434 //gregb@crowncollege.edu by the idea.
435 function backup_required_functions($justcheck=false) {
437 if(!function_exists('utf8_encode')) {
438 if (empty($justcheck)) {
439 print_error('needphpext', '', '', 'XML');
440 } else {
441 return false;
445 return true;
448 //This function send n white characters to the browser and flush the
449 //output buffer. Used to avoid browser timeouts and to show the progress.
450 function backup_flush($n=0,$time=false) {
451 if (defined('RESTORE_SILENTLY_NOFLUSH')) {
452 return;
454 if ($time) {
455 $ti = strftime("%X",time());
456 } else {
457 $ti = "";
459 echo str_repeat(" ", $n) . $ti . "\n";
460 flush();
463 //This function creates the filename and write data to it
464 //returning status as result
465 function backup_data2file ($file,&$data) {
467 $status = true;
468 $status2 = true;
470 $f = fopen($file,"w");
471 $status = fwrite($f,$data);
472 $status2 = fclose($f);
474 return ($status && $status2);
477 //This function read the filename and read data from it
478 function backup_file2data ($file,&$data) {
480 $status = true;
481 $status2 = true;
483 $f = fopen($file,"r");
484 $data = fread ($f,filesize($file));
485 $status2 = fclose($f);
487 return ($status && $status2);
490 /** this function will restore an entire backup.zip into the specified course
491 * using standard moodle backup/restore functions, but silently.
492 * @param string $pathtofile the absolute path to the backup file.
493 * @param int $destinationcourse the course id to restore to.
494 * @param boolean $emptyfirst whether to delete all coursedata first.
495 * @param boolean $userdata whether to include any userdata that may be in the backup file.
496 * @param array $preferences optional, 0 will be used. Can contain:
497 * metacourse
498 * logs
499 * course_files
500 * messages
502 function import_backup_file_silently($pathtofile,$destinationcourse,$emptyfirst=false,$userdata=false, $preferences=array()) {
503 global $CFG,$SESSION,$USER, $DB; // is there such a thing on cron? I guess so..
505 if (!defined('RESTORE_SILENTLY')) {
506 define('RESTORE_SILENTLY',true); // don't output all the stuff to us.
509 $debuginfo = 'import_backup_file_silently: ';
510 $cleanupafter = false;
511 $errorstr = ''; // passed by reference to restore_precheck to get errors from.
513 $course = null;
514 if ($destinationcourse && !$course = $DB->get_record('course', array('id'=>$destinationcourse))) {
515 mtrace($debuginfo.'Course with id $destinationcourse was not a valid course!');
516 return false;
519 // first check we have a valid file.
520 if (!file_exists($pathtofile) || !is_readable($pathtofile)) {
521 mtrace($debuginfo.'File '.$pathtofile.' either didn\'t exist or wasn\'t readable');
522 return false;
525 // now make sure it's a zip file
526 require_once($CFG->dirroot.'/lib/filelib.php');
527 $filename = substr($pathtofile,strrpos($pathtofile,'/')+1);
528 $mimetype = mimeinfo("type", $filename);
529 if ($mimetype != 'application/zip') {
530 mtrace($debuginfo.'File '.$pathtofile.' was of wrong mimetype ('.$mimetype.')' );
531 return false;
534 // restore_precheck wants this within dataroot, so lets put it there if it's not already..
535 if (strstr($pathtofile,$CFG->dataroot) === false) {
536 // first try and actually move it..
537 if (!check_dir_exists($CFG->dataroot.'/temp/backup/',true)) {
538 mtrace($debuginfo.'File '.$pathtofile.' outside of dataroot and couldn\'t move it! ');
539 return false;
541 if (!copy($pathtofile,$CFG->dataroot.'/temp/backup/'.$filename)) {
542 mtrace($debuginfo.'File '.$pathtofile.' outside of dataroot and couldn\'t move it! ');
543 return false;
544 } else {
545 $pathtofile = 'temp/backup/'.$filename;
546 $cleanupafter = true;
548 } else {
549 // it is within dataroot, so take it off the path for restore_precheck.
550 $pathtofile = substr($pathtofile,strlen($CFG->dataroot.'/'));
553 if (!backup_required_functions()) {
554 mtrace($debuginfo.'Required function check failed (see backup_required_functions)');
555 return false;
557 @ini_set("max_execution_time","3000");
558 if (empty($CFG->extramemorylimit)) {
559 raise_memory_limit('128M');
560 } else {
561 raise_memory_limit($CFG->extramemorylimit);
564 if (!$backup_unique_code = restore_precheck($destinationcourse,$pathtofile,$errorstr,true)) {
565 mtrace($debuginfo.'Failed restore_precheck (error was '.$errorstr.')');
566 return false;
569 global $restore; // ick
570 $restore = new StdClass;
571 // copy back over the stuff that gets set in restore_precheck
572 $restore->course_header = $SESSION->course_header;
573 $restore->info = $SESSION->info;
575 $xmlfile = "$CFG->dataroot/temp/backup/$backup_unique_code/moodle.xml";
576 $info = restore_read_xml_roles($xmlfile);
577 $restore->rolesmapping = array();
578 if (isset($info->roles) && is_array($info->roles)) {
579 foreach ($info->roles as $id => $info) {
580 if ($newroleid = get_field('role', 'id', 'shortname', $info->shortname)) {
581 $restore->rolesmapping[$id] = $newroleid;
586 // add on some extra stuff we need...
587 $restore->metacourse = (isset($preferences['restore_metacourse']) ? $preferences['restore_metacourse'] : 0);
588 $restore->course_id = $destinationcourse;
589 if ($destinationcourse) {
590 $restore->restoreto = RESTORETO_CURRENT_ADDING;
591 $restore->course_startdateoffset = $course->startdate - $restore->course_header->course_startdate;
592 } else {
593 $restore->restoreto = RESTORETO_NEW_COURSE;
594 $restore->restore_restorecatto = 0; // let this be handled by the headers
595 $restore->course_startdateoffset = 0;
599 $restore->users = $userdata;
600 $restore->user_files = $userdata;
601 $restore->deleting = $emptyfirst;
603 $restore->groups = (isset($preferences['restore_groups']) ? $preferences['restore_groups'] : RESTORE_GROUPS_NONE);
604 $restore->logs = (isset($preferences['restore_logs']) ? $preferences['restore_logs'] : 0);
605 $restore->messages = (isset($preferences['restore_messages']) ? $preferences['restore_messages'] : 0);
606 $restore->blogs = (isset($preferences['restore_blogs']) ? $preferences['restore_blogs'] : 0);
607 $restore->course_files = (isset($preferences['restore_course_files']) ? $preferences['restore_course_files'] : 0);
608 $restore->site_files = (isset($preferences['restore_site_files']) ? $preferences['restore_site_files'] : 0);
610 $restore->backup_version = $restore->info->backup_backup_version;
611 $restore->original_wwwroot = $restore->info->original_wwwroot;
613 // now copy what we have over to the session
614 // this needs to happen before restore_setup_for_check
615 // which for some reason reads the session
616 $SESSION->restore =& $restore;
617 // rename the things that are called differently
618 $SESSION->restore->restore_course_files = $restore->course_files;
619 $SESSION->restore->restore_site_files = $restore->site_files;
620 $SESSION->restore->backup_version = $restore->info->backup_backup_version;
622 restore_setup_for_check($restore, $backup_unique_code);
624 // maybe we need users (defaults to 2 (none) in restore_setup_for_check)
625 // so set this again here
626 if (!empty($userdata)) {
627 $restore->users = 1;
630 // we also need modules...
631 if ($allmods = $DB->get_records("modules")) {
632 foreach ($allmods as $mod) {
633 $modname = $mod->name;
634 //Now check that we have that module info in the backup file
635 if (isset($restore->info->mods[$modname]) && $restore->info->mods[$modname]->backup == "true") {
636 $restore->mods[$modname]->restore = true;
637 $restore->mods[$modname]->userinfo = $userdata;
639 else {
640 // avoid warnings
641 $restore->mods[$modname]->restore = false;
642 $restore->mods[$modname]->userinfo = false;
646 if (!$status = restore_execute($restore,$restore->info,$restore->course_header,$errorstr)) {
647 mtrace($debuginfo.'Failed restore_execute (error was '.$errorstr.')');
648 return false;
650 // now get out the new courseid and return that
651 if ($restore->restoreto = RESTORETO_NEW_COURSE) {
652 if (!empty($SESSION->restore->course_id)) {
653 return $SESSION->restore->course_id;
655 return false;
657 return true;
661 * Function to backup an entire course silently and create a zipfile.
663 * @param int $courseid the id of the course
664 * @param array $prefs see {@link backup_generate_preferences_artificially}
666 function backup_course_silently($courseid, $prefs, &$errorstring) {
667 global $CFG, $preferences, $DB; // global preferences here because something else wants it :(
668 if (!defined('BACKUP_SILENTLY')) {
669 define('BACKUP_SILENTLY', 1);
671 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
672 debugging("Couldn't find course with id $courseid in backup_course_silently");
673 return false;
675 $preferences = backup_generate_preferences_artificially($course, $prefs);
676 $preferences->destination = array_key_exists('destination', $prefs) ? $prefs['destination'] : 0;
677 if (backup_execute($preferences, $errorstring)) {
678 return $CFG->dataroot . '/' . $course->id . '/backupdata/' . $preferences->backup_name;
680 else {
681 return false;
686 * Function to generate the $preferences variable that
687 * backup uses. This will back up all modules and instances in a course.
689 * @param object $course course object
690 * @param array $prefs can contain:
691 backup_metacourse
692 backup_users
693 backup_logs
694 backup_user_files
695 backup_course_files
696 backup_site_files
697 backup_messages
698 userdata
699 * and if not provided, they will not be included.
702 function backup_generate_preferences_artificially($course, $prefs) {
703 global $CFG, $DB;
704 $preferences = new StdClass;
705 $preferences->backup_unique_code = time();
706 $preferences->backup_users = (isset($prefs['backup_users']) ? $prefs['backup_users'] : 0);
707 $preferences->backup_name = backup_get_zipfile_name($course, $preferences->backup_unique_code);
708 $preferences->mods = array();
709 $count = 0;
711 if ($allmods = $DB->get_records("modules") ) {
712 foreach ($allmods as $mod) {
713 $modname = $mod->name;
714 $modfile = "$CFG->dirroot/mod/$modname/backuplib.php";
715 $modbackup = $modname."_backup_mods";
716 $modbackupone = $modname."_backup_one_mod";
717 $modcheckbackup = $modname."_check_backup_mods";
718 if (!file_exists($modfile)) {
719 continue;
721 include_once($modfile);
722 if (!function_exists($modbackup) || !function_exists($modcheckbackup)) {
723 continue;
725 $var = "exists_".$modname;
726 $preferences->$var = true;
727 $count++;
728 // check that there are instances and we can back them up individually
729 if (!$DB->count_records('course_modules', array('course'=>$course->id), array('module'=>$mod->id)) || !function_exists($modbackupone)) {
730 continue;
732 $var = 'exists_one_'.$modname;
733 $preferences->$var = true;
734 $varname = $modname.'_instances';
735 $preferences->$varname = get_all_instances_in_course($modname, $course, NULL, true);
736 foreach ($preferences->$varname as $instance) {
737 $preferences->mods[$modname]->instances[$instance->id]->name = $instance->name;
738 $var = 'backup_'.$modname.'_instance_'.$instance->id;
739 $preferences->$var = true;
740 $preferences->mods[$modname]->instances[$instance->id]->backup = true;
741 $var = 'backup_user_info_'.$modname.'_instance_'.$instance->id;
742 $preferences->$var = (!array_key_exists('userdata', $prefs) || $prefs['userdata']);
743 $preferences->mods[$modname]->instances[$instance->id]->userinfo = $preferences->$var;
744 $var = 'backup_'.$modname.'_instances';
745 $preferences->$var = 1; // we need this later to determine what to display in modcheckbackup.
748 //Check data
749 //Check module info
750 $preferences->mods[$modname]->name = $modname;
752 $var = "backup_".$modname;
753 $preferences->$var = true;
754 $preferences->mods[$modname]->backup = true;
756 //Check include user info
757 $var = "backup_user_info_".$modname;
758 $preferences->$var = (!array_key_exists('userdata', $prefs) || $prefs['userdata']);
759 $preferences->mods[$modname]->userinfo = $preferences->$var;
761 //Call the check function to show more info
762 $modcheckbackup = $modname."_check_backup_mods";
763 $var = $modname.'_instances';
764 $instancestopass = array();
765 if (!empty($preferences->$var) && is_array($preferences->$var) && count($preferences->$var)) {
766 $table->data = array();
767 $countinstances = 0;
768 foreach ($preferences->$var as $instance) {
769 $var1 = 'backup_'.$modname.'_instance_'.$instance->id;
770 $var2 = 'backup_user_info_'.$modname.'_instance_'.$instance->id;
771 if (!empty($preferences->$var1)) {
772 $obj = new StdClass;
773 $obj->name = $instance->name;
774 $obj->userdata = $preferences->$var2;
775 $obj->id = $instance->id;
776 $instancestopass[$instance->id]= $obj;
777 $countinstances++;
782 $modcheckbackup($course->id,$preferences->$var,$preferences->backup_unique_code,$instancestopass);
786 //Check other parameters
787 $preferences->backup_metacourse = (isset($prefs['backup_metacourse']) ? $prefs['backup_metacourse'] : 0);
788 $preferences->backup_logs = (isset($prefs['backup_logs']) ? $prefs['backup_logs'] : 0);
789 $preferences->backup_user_files = (isset($prefs['backup_user_files']) ? $prefs['backup_user_files'] : 0);
790 $preferences->backup_course_files = (isset($prefs['backup_course_files']) ? $prefs['backup_course_files'] : 0);
791 $preferences->backup_site_files = (isset($prefs['backup_site_files']) ? $prefs['backup_site_files'] : 0);
792 $preferences->backup_messages = (isset($prefs['backup_messages']) ? $prefs['backup_messages'] : 0);
793 $preferences->backup_gradebook_history = (isset($prefs['backup_gradebook_history']) ? $prefs['backup_gradebook_history'] : 0);
794 $preferences->backup_blogs = (isset($prefs['backup_blogs']) ? $prefs['backup_blogs'] : 0);
795 $preferences->backup_course = $course->id;
797 //Check users
798 user_check_backup($course->id,$preferences->backup_unique_code,$preferences->backup_users,$preferences->backup_messages, $preferences->backup_blogs);
800 //Check logs
801 log_check_backup($course->id);
803 //Check user files
804 user_files_check_backup($course->id,$preferences->backup_unique_code);
806 //Check course files
807 course_files_check_backup($course->id,$preferences->backup_unique_code);
809 //Check site files
810 site_files_check_backup($course->id,$preferences->backup_unique_code);
812 //Role assignments
813 $roles = $DB->get_records('role', null, 'sortorder');
814 foreach ($roles as $role) {
815 $preferences->backuproleassignments[$role->id] = $role;
818 backup_add_static_preferences($preferences);
819 return $preferences;
821 function add_to_backup_log($starttime,$courseid,$message, $backuptype) {
822 global $DB;
823 $log = new object();
824 $log->courseid = $courseid;
825 $log->time = time();
826 $log->laststarttime = $starttime;
827 $log->info = $message;
828 $log->backuptype = $backuptype;
829 $DB->insert_record('backup_log', $log);