function flv_backup_files($bf, $preferences, $flv)
{
    global $CFG;
    $status = true;
    if (!file_exists($CFG->dataroot . '/' . $preferences->backup_course . '/' . $flv->flvfile)) {
        return true;
        // doesn't exist but we don't want to halt the entire process so still return true.
    }
    $status = $status && check_and_create_course_files_dir($preferences->backup_unique_code);
    // if this is somewhere deeply nested we need to do all the structure stuff first.....
    $bits = explode('/', $flv->flvfile);
    $newbit = '';
    for ($i = 0; $i < count($bits) - 1; $i++) {
        $newbit .= $bits[$i] . '/';
        $status = $status && check_dir_exists($CFG->dataroot . '/temp/backup/' . $preferences->backup_unique_code . '/course_files/' . $newbit, true);
    }
    if ($flv->flvfile === '') {
        $status = $status && backup_copy_course_files($preferences);
        // copy while ignoring backupdata and moddata!!!
    } else {
        if (strpos($flv->flvfile, 'backupdata') === 0 or strpos($flv->flvfile, $CFG->moddata) === 0) {
            // no copying - these directories must not be shared anyway!
        } else {
            $status = $status && backup_copy_file($CFG->dataroot . "/" . $preferences->backup_course . "/" . $flv->flvfile, $CFG->dataroot . "/temp/backup/" . $preferences->backup_unique_code . "/course_files/" . $flv->flvfile);
        }
    }
    // now, just in case we check moddata ( going forwards, flvs should use this )
    $status = $status && check_and_create_moddata_dir($preferences->backup_unique_code);
    $status = $status && check_dir_exists($CFG->dataroot . "/temp/backup/" . $preferences->backup_unique_code . "/" . $CFG->moddata . "/flv/", true);
    if ($status) {
        //Only if it exists !! Thanks to Daniel Miksik.
        $instanceid = $flv->id;
        if (is_dir($CFG->dataroot . "/" . $preferences->backup_course . "/" . $CFG->moddata . "/flv/" . $instanceid)) {
            $status = backup_copy_file($CFG->dataroot . "/" . $preferences->backup_course . "/" . $CFG->moddata . "/flv/" . $instanceid, $CFG->dataroot . "/temp/backup/" . $preferences->backup_unique_code . "/moddata/flv/" . $instanceid);
        }
    }
    return $status;
}
function backup_copy_course_files($preferences)
{
    global $CFG;
    $status = true;
    //First we check to "course_files" exists and create it as necessary
    //in temp/backup/$backup_code  dir
    $status = check_and_create_course_files_dir($preferences->backup_unique_code);
    //Now iterate over files and directories except $CFG->moddata and backupdata to be
    //copied to backup
    $rootdir = $CFG->dataroot . "/" . $preferences->backup_course;
    $name_moddata = $CFG->moddata;
    $name_backupdata = "backupdata";
    //Check if directory exists
    if (is_dir($rootdir)) {
        $list = list_directories_and_files($rootdir);
        if ($list) {
            //Iterate
            foreach ($list as $dir) {
                if ($dir !== $name_moddata and $dir !== $name_backupdata) {
                    $status = backup_copy_file($rootdir . "/" . $dir, $CFG->dataroot . "/temp/backup/" . $preferences->backup_unique_code . "/course_files/" . $dir);
                }
            }
        }
    }
    return $status;
}
/**
 * 対象のセクションのコースファイルのみをコピーする
 * 
 * @param object $preferences
 * @return bool
 */
function project_backup_copy_course_files($preferences)
{
    global $CFG;
    $status = true;
    // 対象のコース情報の取得
    if (!($course = get_record("course", "id", $preferences->backup_course))) {
        error("Course ID was incorrect (can't find it)");
    }
    // セクション情報の取得
    if (!($section = get_course_section($preferences->backup_section, $course->id))) {
        error("Section data was incorrect (can't find it)");
    }
    // セクションタイトルの取得
    if (!($sectiontitle = project_format_get_title($course, $section->id))) {
        error("Section title data was incorrect (can't find it)");
    }
    //First we check to "course_files" exists and create it as necessary
    //in temp/backup/$backup_code  dir
    $status = check_and_create_course_files_dir($preferences->backup_unique_code);
    //Now iterate over files and directories except $CFG->moddata and backupdata to be
    //copied to backup
    // 対象のセクションのディレクトリ名を取得
    $rootdir = $CFG->dataroot . "/" . $preferences->backup_course . '/' . $sectiontitle->directoryname;
    // ディレクトリをまるごとコピーする
    if (is_dir($rootdir)) {
        $status = backup_copy_file($rootdir, $CFG->dataroot . "/temp/backup/" . $preferences->backup_unique_code . "/course_files/" . $sectiontitle->directoryname);
    }
    return $status;
}