function backup_copy_user_files($preferences) { global $CFG; $status = true; //First we check to "user_files" exists and create it as necessary //in temp/backup/$backup_code dir $status = check_and_create_user_files_dir($preferences->backup_unique_code); //Now iterate over directories under "user" to check if that user must be copied to backup // Method to get a list of userid=>array(basedir => $basedir, userfolder => $userfolder) $userlist = get_user_directories(); foreach ($userlist as $userid => $userinfo) { //Look for dir like username in backup_ids $data = count_records("backup_ids", "backup_code", $preferences->backup_unique_code, "table_name", "user", "old_id", $userid); //If exists, copy it if ($data) { $parts = explode('/', $userinfo['userfolder']); $status = true; if (is_array($parts)) { $group = $parts[0]; $userid = $parts[1]; // Create group dir first $status = check_dir_exists("{$CFG->dataroot}/temp/backup/{$preferences->backup_unique_code}/user_files/" . $group, true); } $status = $status && backup_copy_file($userinfo['basedir'] . '/' . $userinfo['userfolder'], "{$CFG->dataroot}/temp/backup/{$preferences->backup_unique_code}/user_files/{$userinfo['userfolder']}"); } } return $status; }
/** * This is a moodlelib method but it is used in backuplib, so it is tested here in that context, with typical backup data. */ function test_get_user_directories() { global $CFG; $dirlist = get_user_directories(); $this->assertEqual(4, count($dirlist)); foreach ($this->testfiles as $file) { $parts = explode('/', $file); $section = $parts[0]; $userid = $parts[1]; $this->assertEqual($file, $dirlist[$userid]['userfolder']); $this->assertEqual($this->userbasedir, $dirlist[$userid]['basedir']); } }