function schedule_inc_backup_course_execute($preferences, $starttime = 0)
{
    global $CFG;
    $status = true;
    //Some parts of the backup doesn't know about $preferences, so we
    //put a copy of it inside that CFG (always global) to be able to
    //use it. Then, when needed I search for preferences inside CFG
    //Used to avoid some problems in full_tag() when preferences isn't
    //set globally (i.e. in scheduled backups)
    $CFG->backup_preferences = $preferences;
    //Check for temp and backup and backup_unique_code directory
    //Create them as needed
    backup_add_to_log($starttime, $preferences->backup_course, "    checking temp structures", 'incrementalbackup');
    $status = check_and_create_backup_dir($preferences->backup_unique_code);
    //Empty backup dir
    if ($status) {
        backup_add_to_log($starttime, $preferences->backup_course, "    cleaning current dir", 'incrementalbackup');
        $status = clear_backup_dir($preferences->backup_unique_code);
    }
    //Create the moodle.xml file
    if ($status) {
        backup_add_to_log($starttime, $preferences->backup_course, "    creating backup file", 'incrementalbackup');
        //Obtain the xml file (create and open) and print prolog information
        $backup_file = backup_open_xml($preferences->backup_unique_code);
        //Prints general info about backup to file
        if ($backup_file) {
            backup_add_to_log($starttime, $preferences->backup_course, "      general info", 'incrementalbackup');
            $status = backup_general_info($backup_file, $preferences);
        } else {
            $status = false;
        }
        //Prints course start (tag and general info)
        if ($status) {
            $status = backup_course_start($backup_file, $preferences);
        }
        //Metacourse information
        if ($status && $preferences->backup_metacourse) {
            backup_add_to_log($starttime, $preferences->backup_course, "      metacourse info", 'incrementalbackup');
            $status = backup_course_metacourse($backup_file, $preferences);
        }
        //Block info
        if ($status) {
            backup_add_to_log($starttime, $preferences->backup_course, "      blocks info", 'incrementalbackup');
            $status = backup_course_blocks($backup_file, $preferences);
        }
        //Section info
        if ($status) {
            backup_add_to_log($starttime, $preferences->backup_course, "      sections info", 'incrementalbackup');
            $status = backup_course_sections($backup_file, $preferences);
        }
        //User info
        if ($status) {
            backup_add_to_log($starttime, $preferences->backup_course, "      user info", 'incrementalbackup');
            $status = backup_user_info($backup_file, $preferences);
        }
        //If we have selected to backup messages and we are
        //doing a SITE backup, let's do it
        if ($status && $preferences->backup_messages && $preferences->backup_course == SITEID) {
            backup_add_to_log($starttime, $preferences->backup_course, "      messages", 'incrementalbackup');
            if (!($status = backup_messages($backup_file, $preferences))) {
                notify("An error occurred while backing up messages");
            }
        }
        //If we have selected to backup blogs and we are
        //doing a SITE backup, let's do it
        if ($status && isset($preferences->backup_blogs) && isset($preferences->backup_course) && $preferences->backup_blogs && $preferences->backup_course == SITEID) {
            schedule_backup_log($starttime, $preferences->backup_course, "      blogs", 'incrementalbackup');
            $status = backup_blogs($backup_file, $preferences);
        }
        //If we have selected to backup quizzes, backup categories and
        //questions structure (step 1). See notes on mod/quiz/backuplib.php
        if ($status and isset($preferences->mods['quiz']->backup)) {
            backup_add_to_log($starttime, $preferences->backup_course, "      categories & questions", 'incrementalbackup');
            $status = backup_question_categories($backup_file, $preferences);
        }
        //Print logs if selected
        if ($status) {
            if ($preferences->backup_logs) {
                backup_add_to_log($starttime, $preferences->backup_course, "      logs", 'incrementalbackup');
                $status = backup_log_info($backup_file, $preferences);
            }
        }
        //Print scales info
        if ($status) {
            backup_add_to_log($starttime, $preferences->backup_course, "      scales", 'incrementalbackup');
            $status = backup_scales_info($backup_file, $preferences);
        }
        //Print groups info
        if ($status) {
            backup_add_to_log($starttime, $preferences->backup_course, "      groups", 'incrementalbackup');
            $status = backup_groups_info($backup_file, $preferences);
        }
        //Print groupings info
        if ($status) {
            backup_add_to_log($starttime, $preferences->backup_course, "      groupings", 'incrementalbackup');
            $status = backup_groupings_info($backup_file, $preferences);
        }
        //Print groupings_groups info
        if ($status) {
            backup_add_to_log($starttime, $preferences->backup_course, "      groupings_groups", 'incrementalbackup');
            $status = backup_groupings_groups_info($backup_file, $preferences);
        }
        //Print events info
        if ($status) {
            backup_add_to_log($starttime, $preferences->backup_course, "      events", 'incrementalbackup');
            $status = backup_events_info($backup_file, $preferences);
        }
        //Print gradebook info
        if ($status) {
            backup_add_to_log($starttime, $preferences->backup_course, "      gradebook", 'incrementalbackup');
            $status = backup_gradebook_info($backup_file, $preferences);
        }
        //Module info, this unique function makes all the work!!
        //db export and module fileis copy
        if ($status) {
            $mods_to_backup = false;
            //Check if we have any mod to backup
            if (!empty($preferences->mods)) {
                foreach ($preferences->mods as $module) {
                    if ($module->backup) {
                        $mods_to_backup = true;
                    }
                }
            }
            //If we have to backup some module
            if ($mods_to_backup) {
                backup_add_to_log($starttime, $preferences->backup_course, "      modules", 'incrementalbackup');
                //Start modules tag
                $status = backup_modules_start($backup_file, $preferences);
                //Iterate over modules and call backup
                foreach ($preferences->mods as $module) {
                    if ($module->backup and $status) {
                        backup_add_to_log($starttime, $preferences->backup_course, "        {$module->name}", 'incrementalbackup');
                        $status = backup_module($backup_file, $preferences, $module->name);
                    }
                }
                //Close modules tag
                $status = backup_modules_end($backup_file, $preferences);
            }
        }
        //Backup course format data, if any.
        if ($status) {
            backup_add_to_log($starttime, $preferences->backup_course, "      course format data", 'incrementalbackup');
            $status = backup_format_data($backup_file, $preferences);
        }
        //Prints course end
        if ($status) {
            $status = backup_course_end($backup_file, $preferences);
        }
        //Close the xml file and xml data
        if ($backup_file) {
            backup_close_xml($backup_file);
        }
    }
    //Now, if selected, copy user files
    if ($status) {
        if ($preferences->backup_user_files) {
            backup_add_to_log($starttime, $preferences->backup_course, "    copying user files", 'incrementalbackup');
            $status = backup_copy_user_files($preferences);
        }
    }
    //Now, if selected, copy course files
    if ($status) {
        if ($preferences->backup_course_files) {
            backup_add_to_log($starttime, $preferences->backup_course, "    copying course files", 'incrementalbackup');
            $status = backup_copy_course_files($preferences);
        }
    }
    //Now, if selected, copy site files
    if ($status) {
        if ($preferences->backup_site_files) {
            backup_add_to_log($starttime, $preferences->backup_course, "    copying site files", 'incrementalbackup');
            $status = backup_copy_site_files($preferences);
        }
    }
    //Now, zip all the backup directory contents
    if ($status) {
        backup_add_to_log($starttime, $preferences->backup_course, "    zipping files", 'incrementalbackup');
        $status = backup_zip($preferences);
    }
    //Now, copy the zip file to course directory
    if ($status) {
        backup_add_to_log($starttime, $preferences->backup_course, "    copying backup", 'incrementalbackup');
        $status = copy_zip_to_course_dir($preferences);
    }
    //Now, clean temporary data (db and filesystem)
    if ($status) {
        backup_add_to_log($starttime, $preferences->backup_course, "    cleaning temp data", 'incrementalbackup');
        $status = clean_temp_data($preferences);
    }
    //Unset CFG->backup_preferences only needed in scheduled backups
    unset($CFG->backup_preferences);
    return $status;
}
function backup_execute(&$preferences, &$errorstr)
{
    global $CFG;
    $status = true;
    //Check for temp and backup and backup_unique_code directory
    //Create them as needed
    if (!defined('BACKUP_SILENTLY')) {
        echo "<li>" . get_string("creatingtemporarystructures") . '</li>';
    }
    $status = check_and_create_backup_dir($preferences->backup_unique_code);
    //Empty dir
    if ($status) {
        $status = clear_backup_dir($preferences->backup_unique_code);
    }
    //Delete old_entries from backup tables
    if (!defined('BACKUP_SILENTLY')) {
        echo "<li>" . get_string("deletingolddata") . '</li>';
    }
    $status = backup_delete_old_data();
    if (!$status) {
        if (!defined('BACKUP_SILENTLY')) {
            notify("An error occurred deleting old backup data");
        } else {
            $errorstr = "An error occurred deleting old backup data";
            return false;
        }
    }
    //Create the moodle.xml file
    if ($status) {
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("creatingxmlfile");
            //Begin a new list to xml contents
            echo "<ul>";
            echo "<li>" . get_string("writingheader") . '</li>';
        }
        //Obtain the xml file (create and open) and print prolog information
        $backup_file = backup_open_xml($preferences->backup_unique_code);
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("writinggeneralinfo") . '</li>';
        }
        //Prints general info about backup to file
        if ($backup_file) {
            if (!($status = backup_general_info($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up general info");
                } else {
                    $errorstr = "An error occurred while backing up general info";
                    return false;
                }
            }
        }
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("writingcoursedata");
            //Start new ul (for course)
            echo "<ul>";
            echo "<li>" . get_string("courseinfo") . '</li>';
        }
        //Prints course start (tag and general info)
        if ($status) {
            if (!($status = backup_course_start($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up course start");
                } else {
                    $errorstr = "An error occurred while backing up course start";
                    return false;
                }
            }
        }
        //Metacourse information
        if ($status && $preferences->backup_metacourse) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("metacourse") . '</li>';
            }
            if (!($status = backup_course_metacourse($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up metacourse info");
                } else {
                    $errorstr = "An error occurred while backing up metacourse info";
                    return false;
                }
            }
        }
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("blocks") . '</li>';
        }
        //Blocks information
        if ($status) {
            if (!($status = backup_course_blocks($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up course blocks");
                } else {
                    $errorstr = "An error occurred while backing up course blocks";
                    return false;
                }
            }
        }
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("sections") . '</li>';
        }
        //Section info
        if ($status) {
            if (!($status = backup_course_sections($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up course sections");
                } else {
                    $errorstr = "An error occurred while backing up course sections";
                    return false;
                }
            }
        }
        //End course contents (close ul)
        if (!defined('BACKUP_SILENTLY')) {
            echo "</ul></li>";
        }
        //User info
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writinguserinfo") . '</li>';
            }
            if (!($status = backup_user_info($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up user info");
                } else {
                    $errorstr = "An error occurred while backing up user info";
                    return false;
                }
            }
        }
        //If we have selected to backup messages and we are
        //doing a SITE backup, let's do it
        if ($status && $preferences->backup_messages && $preferences->backup_course == SITEID) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writingmessagesinfo") . '</li>';
            }
            if (!($status = backup_messages($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up messages");
                } else {
                    $errorstr = "An error occurred while backing up messages";
                    return false;
                }
            }
        }
        //If we have selected to backup blogs and we are
        //doing a SITE backup, let's do it
        if ($status && $preferences->backup_blogs && $preferences->backup_course == SITEID) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writingblogsinfo") . '</li>';
            }
            if (!($status = backup_blogs($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up blogs");
                } else {
                    $errorstr = "An error occurred while backing up blogs";
                    return false;
                }
            }
        }
        //If we have selected to backup quizzes or other modules that use questions
        //we've already added ids of categories and questions to backup to backup_ids table
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writingcategoriesandquestions") . '</li>';
            }
            require_once $CFG->dirroot . '/question/backuplib.php';
            if (!($status = backup_question_categories($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up quiz categories");
                } else {
                    $errorstr = "An error occurred while backing up quiz categories";
                    return false;
                }
            }
        }
        //Print logs if selected
        if ($status) {
            if ($preferences->backup_logs) {
                if (!defined('BACKUP_SILENTLY')) {
                    echo "<li>" . get_string("writingloginfo") . '</li>';
                }
                if (!($status = backup_log_info($backup_file, $preferences))) {
                    if (!defined('BACKUP_SILENTLY')) {
                        notify("An error occurred while backing up log info");
                    } else {
                        $errorstr = "An error occurred while backing up log info";
                        return false;
                    }
                }
            }
        }
        //Print scales info
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writingscalesinfo") . '</li>';
            }
            if (!($status = backup_scales_info($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up scales");
                } else {
                    $errorstr = "An error occurred while backing up scales";
                    return false;
                }
            }
        }
        //Print groups info
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writinggroupsinfo") . '</li>';
            }
            if (!($status = backup_groups_info($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up groups");
                } else {
                    $errostr = "An error occurred while backing up groups";
                    return false;
                }
            }
        }
        //Print groupings info
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writinggroupingsinfo") . '</li>';
            }
            if (!($status = backup_groupings_info($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up groupings");
                } else {
                    $errorstr = "An error occurred while backing up groupings";
                    return false;
                }
            }
        }
        //Print groupings_groups info
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writinggroupingsgroupsinfo") . '</li>';
            }
            if (!($status = backup_groupings_groups_info($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up groupings groups");
                } else {
                    $errorstr = "An error occurred while backing up groupings groups";
                    return false;
                }
            }
        }
        //Print events info
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writingeventsinfo") . '</li>';
            }
            if (!($status = backup_events_info($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up events");
                } else {
                    $errorstr = "An error occurred while backing up events";
                    return false;
                }
            }
        }
        //Print gradebook info
        if ($status) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("writinggradebookinfo") . '</li>';
            }
            if (!($status = backup_gradebook_info($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up gradebook");
                } else {
                    $errorstr = "An error occurred while backing up gradebook";
                    return false;
                }
            }
        }
        //Module info, this unique function makes all the work!!
        //db export and module fileis copy
        if ($status) {
            $mods_to_backup = false;
            //Check if we have any mod to backup
            foreach ($preferences->mods as $module) {
                if ($module->backup) {
                    $mods_to_backup = true;
                }
            }
            //If we have to backup some module
            if ($mods_to_backup) {
                if (!defined('BACKUP_SILENTLY')) {
                    echo "<li>" . get_string("writingmoduleinfo");
                }
                //Start modules tag
                if (!($status = backup_modules_start($backup_file, $preferences))) {
                    if (!defined('BACKUP_SILENTLY')) {
                        notify("An error occurred while backing up module info");
                    } else {
                        $errorstr = "An error occurred while backing up module info";
                        return false;
                    }
                }
                //Open ul for module list
                if (!defined('BACKUP_SILENTLY')) {
                    echo "<ul>";
                }
                //Iterate over modules and call backup
                foreach ($preferences->mods as $module) {
                    if ($module->backup and $status) {
                        if (!defined('BACKUP_SILENTLY')) {
                            echo "<li>" . get_string("modulenameplural", $module->name) . '</li>';
                        }
                        if (!($status = backup_module($backup_file, $preferences, $module->name))) {
                            if (!defined('BACKUP_SILENTLY')) {
                                notify("An error occurred while backing up '{$module->name}'");
                            } else {
                                $errorstr = "An error occurred while backing up '{$module->name}'";
                                return false;
                            }
                        }
                    }
                }
                //Close ul for module list
                if (!defined('BACKUP_SILENTLY')) {
                    echo "</ul></li>";
                }
                //Close modules tag
                if (!($status = backup_modules_end($backup_file, $preferences))) {
                    if (!defined('BACKUP_SILENTLY')) {
                        notify("An error occurred while finishing the module backups");
                    } else {
                        $errorstr = "An error occurred while finishing the module backups";
                        return false;
                    }
                }
            }
        }
        //Backup course format data, if any.
        if (!defined('BACKUP_SILENTLY')) {
            echo '<li>' . get_string("courseformatdata") . '</li>';
        }
        if ($status) {
            if (!($status = backup_format_data($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while backing up the course format data");
                } else {
                    $errorstr = "An error occurred while backing up the course format data";
                    return false;
                }
            }
        }
        //Prints course end
        if ($status) {
            if (!($status = backup_course_end($backup_file, $preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while closing the course backup");
                } else {
                    $errorstr = "An error occurred while closing the course backup";
                    return false;
                }
            }
        }
        //Close the xml file and xml data
        if ($backup_file) {
            backup_close_xml($backup_file);
        }
        //End xml contents (close ul)
        if (!defined('BACKUP_SILENTLY')) {
            echo "</ul></li>";
        }
    }
    //Now, if selected, copy user files
    if ($status) {
        if ($preferences->backup_user_files) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("copyinguserfiles") . '</li>';
            }
            if (!($status = backup_copy_user_files($preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while copying user files");
                } else {
                    $errorstr = "An error occurred while copying user files";
                    return false;
                }
            }
        }
    }
    //Now, if selected, copy course files
    if ($status) {
        if ($preferences->backup_course_files) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("copyingcoursefiles") . '</li>';
            }
            if (!($status = backup_copy_course_files($preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while copying course files");
                } else {
                    $errorstr = "An error occurred while copying course files";
                    return false;
                }
            }
        }
    }
    //Now, if selected, copy site files
    if ($status) {
        if ($preferences->backup_site_files) {
            if (!defined('BACKUP_SILENTLY')) {
                echo "<li>" . get_string("copyingsitefiles") . '</li>';
            }
            if (!($status = backup_copy_site_files($preferences))) {
                if (!defined('BACKUP_SILENTLY')) {
                    notify("An error occurred while copying site files");
                } else {
                    $errorstr = "An error occurred while copying site files";
                    return false;
                }
            }
        }
    }
    //Now, zip all the backup directory contents
    if ($status) {
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("zippingbackup") . '</li>';
        }
        if (!($status = backup_zip($preferences))) {
            if (!defined('BACKUP_SILENTLY')) {
                notify("An error occurred while zipping the backup");
            } else {
                $errorstr = "An error occurred while zipping the backup";
                return false;
            }
        }
    }
    //Now, copy the zip file to course directory
    if ($status) {
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("copyingzipfile") . '</li>';
        }
        if (!($status = copy_zip_to_course_dir($preferences))) {
            if (!defined('BACKUP_SILENTLY')) {
                notify("An error occurred while copying the zip file to the course directory");
            } else {
                $errorstr = "An error occurred while copying the zip file to the course directory";
                return false;
            }
        }
    }
    //Now, clean temporary data (db and filesystem)
    if ($status) {
        if (!defined('BACKUP_SILENTLY')) {
            echo "<li>" . get_string("cleaningtempdata") . '</li>';
        }
        if (!($status = clean_temp_data($preferences))) {
            if (!defined('BACKUP_SILENTLY')) {
                notify("An error occurred while cleaning up temporary data");
            } else {
                $errorstr = "An error occurred while cleaning up temporary data";
                return false;
            }
        }
    }
    return $status;
}
 protected function xmlSetCourse($xml)
 {
     if (!backup_course_start($xml, $this->prefs)) {
         throw new SharingCart_XmlException('course start');
     }
     if (!empty($this->prefs->backup_metacourse)) {
         if (!backup_course_metacourse($xml, $this->prefs)) {
             throw new SharingCart_XmlException('course metacourse');
         }
     }
     // セクション
     $this->xmlSetSection($xml);
     if (!backup_user_info($xml, $this->prefs)) {
         throw new SharingCart_XmlException('user info');
     }
     // モジュール
     $this->xmlSetModules($xml);
     // 問題バンク
     $this->xmlSetQuestions($xml);
     if (!backup_scales_info($xml, $this->prefs)) {
         throw new SharingCart_XmlException('scales info');
     }
     if (!backup_groups_info($xml, $this->prefs) || !backup_groupings_info($xml, $this->prefs) || !backup_groupings_groups_info($xml, $this->prefs)) {
         throw new SharingCart_XmlException('groups');
     }
     if (!backup_events_info($xml, $this->prefs)) {
         throw new SharingCart_XmlException('events info');
     }
     if (!backup_format_data($xml, $this->prefs)) {
         throw new SharingCart_XmlException('format data');
     }
     if (!backup_course_end($xml, $this->prefs)) {
         throw new SharingCart_XmlException('course end');
     }
 }