Example #1
0
    public function setUp() {
        global $CFG;

        $this->tempdir = convert_helper::generate_id('simpletest');
        check_dir_exists("$CFG->dataroot/temp/backup/$this->tempdir/course_files/sub1");
        check_dir_exists("$CFG->dataroot/temp/backup/$this->tempdir/moddata/unittest/4/7");
        copy(
            "$CFG->dirroot/backup/converter/moodle1/simpletest/files/moodle.xml",
            "$CFG->dataroot/temp/backup/$this->tempdir/moodle.xml"
        );
        copy(
            "$CFG->dirroot/backup/converter/moodle1/simpletest/files/icon.gif",
            "$CFG->dataroot/temp/backup/$this->tempdir/course_files/file1.gif"
        );
        copy(
            "$CFG->dirroot/backup/converter/moodle1/simpletest/files/icon.gif",
            "$CFG->dataroot/temp/backup/$this->tempdir/course_files/sub1/file2.gif"
        );
        copy(
            "$CFG->dirroot/backup/converter/moodle1/simpletest/files/icon.gif",
            "$CFG->dataroot/temp/backup/$this->tempdir/moddata/unittest/4/file1.gif"
        );
        copy(
            "$CFG->dirroot/backup/converter/moodle1/simpletest/files/icon.gif",
            "$CFG->dataroot/temp/backup/$this->tempdir/moddata/unittest/4/icon.gif"
        );
        copy(
            "$CFG->dirroot/backup/converter/moodle1/simpletest/files/icon.gif",
            "$CFG->dataroot/temp/backup/$this->tempdir/moddata/unittest/4/7/icon.gif"
        );
    }
Example #2
0
    protected function setUp() {
        global $CFG;

        $this->tempdir = convert_helper::generate_id('unittest');
        check_dir_exists("$CFG->tempdir/backup/$this->tempdir/course_files/sub1");
        check_dir_exists("$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/7");
        copy(
            "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/moodle.xml",
            "$CFG->tempdir/backup/$this->tempdir/moodle.xml"
        );
        copy(
            "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif",
            "$CFG->tempdir/backup/$this->tempdir/course_files/file1.gif"
        );
        copy(
            "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif",
            "$CFG->tempdir/backup/$this->tempdir/course_files/sub1/file2.gif"
        );
        copy(
            "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif",
            "$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/file1.gif"
        );
        copy(
            "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif",
            "$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/icon.gif"
        );
        $this->iconhash = sha1_file($CFG->tempdir.'/backup/'.$this->tempdir.'/moddata/unittest/4/icon.gif');
        copy(
            "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif",
            "$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/7/icon.gif"
        );
    }
Example #3
0
 /**
  * Constructor
  *
  * @param string $tempdir the relative path to the directory containing the unpacked backup to convert
  * @param null|base_logger logger to use during the conversion
  */
 public function __construct($tempdir, $logger = null)
 {
     $this->tempdir = $tempdir;
     $this->id = convert_helper::generate_id($tempdir);
     $this->workdir = $tempdir . '_' . $this->get_name() . '_' . $this->id;
     $this->set_logger($logger);
     $this->log('instantiating ' . $this->get_name() . ' converter ' . $this->get_id(), backup::LOG_DEBUG);
     $this->log('conversion source directory', backup::LOG_DEBUG, $this->tempdir);
     $this->log('conversion target directory', backup::LOG_DEBUG, $this->workdir);
     $this->init();
 }
 /**
  * Detects the format of the given unpacked backup directory
  *
  * @param string $tempdir the name of the backup directory
  * @return string one of backup::FORMAT_xxx constants
  */
 public static function detect_backup_format($tempdir)
 {
     global $CFG;
     require_once $CFG->dirroot . '/backup/util/helper/convert_helper.class.php';
     if (convert_helper::detect_moodle2_format($tempdir)) {
         return backup::FORMAT_MOODLE;
     }
     // see if a converter can identify the format
     $converters = convert_helper::available_converters();
     foreach ($converters as $name) {
         $classname = "{$name}_converter";
         if (!class_exists($classname)) {
             throw new coding_exception("available_converters() is supposed to load\n                    converter classes but class {$classname} not found");
         }
         $detected = call_user_func($classname . '::detect_format', $tempdir);
         if (!empty($detected)) {
             return $detected;
         }
     }
     return backup::FORMAT_UNKNOWN;
 }
 /**
  * Define the common setting that any backup type will have
  */
 protected function define_settings()
 {
     global $CFG;
     require_once $CFG->dirroot . '/backup/util/helper/convert_helper.class.php';
     // Define filename setting
     $filename = new backup_filename_setting('filename', base_setting::IS_FILENAME, 'backup.mbz');
     $filename->set_ui(get_string('filename', 'backup'), 'backup.mbz', array('size' => 50));
     $this->add_setting($filename);
     //Sample custom settings
     $converters = convert_helper::available_converters(false);
     foreach ($converters as $cnv) {
         $formatcnv = new backup_users_setting($cnv, base_setting::IS_BOOLEAN, false);
         $formatcnv->set_ui(new backup_setting_ui_checkbox($formatcnv, get_string('backupformat' . $cnv, 'backup')));
         $this->add_setting($formatcnv);
     }
     // Define users setting (keeping it on hand to define dependencies)
     $users = new backup_users_setting('users', base_setting::IS_BOOLEAN, true);
     $users->set_ui(new backup_setting_ui_checkbox($users, get_string('rootsettingusers', 'backup')));
     $this->add_setting($users);
     $this->converter_deps($users, $converters);
     // Define anonymize (dependent of users)
     $anonymize = new backup_anonymize_setting('anonymize', base_setting::IS_BOOLEAN, false);
     $anonymize->set_ui(new backup_setting_ui_checkbox($anonymize, get_string('rootsettinganonymize', 'backup')));
     $this->add_setting($anonymize);
     $users->add_dependency($anonymize);
     // Define role_assignments (dependent of users)
     $roleassignments = new backup_role_assignments_setting('role_assignments', base_setting::IS_BOOLEAN, true);
     $roleassignments->set_ui(new backup_setting_ui_checkbox($roleassignments, get_string('rootsettingroleassignments', 'backup')));
     $this->add_setting($roleassignments);
     $users->add_dependency($roleassignments);
     // Define user_files (dependent of users and anonymize)
     $userfiles = new backup_user_files_setting('user_files', base_setting::IS_BOOLEAN, true);
     $userfiles->set_ui(new backup_setting_ui_checkbox($userfiles, get_string('rootsettinguserfiles', 'backup')));
     $this->add_setting($userfiles);
     $users->add_dependency($userfiles);
     $anonymize->add_dependency($userfiles, setting_dependency::DISABLED_TRUE);
     // Define activities
     $activities = new backup_activities_setting('activities', base_setting::IS_BOOLEAN, true);
     $activities->set_ui(new backup_setting_ui_checkbox($activities, get_string('rootsettingactivities', 'backup')));
     $this->add_setting($activities);
     // Define blocks
     $blocks = new backup_generic_setting('blocks', base_setting::IS_BOOLEAN, true);
     $blocks->set_ui(new backup_setting_ui_checkbox($blocks, get_string('rootsettingblocks', 'backup')));
     $this->add_setting($blocks);
     $this->converter_deps($blocks, $converters);
     // Define filters
     $filters = new backup_generic_setting('filters', base_setting::IS_BOOLEAN, true);
     $filters->set_ui(new backup_setting_ui_checkbox($filters, get_string('rootsettingfilters', 'backup')));
     $this->add_setting($filters);
     $this->converter_deps($filters, $converters);
     // Define comments (dependent of users)
     $comments = new backup_comments_setting('comments', base_setting::IS_BOOLEAN, true);
     $comments->set_ui(new backup_setting_ui_checkbox($comments, get_string('rootsettingcomments', 'backup')));
     $this->add_setting($comments);
     $users->add_dependency($comments);
     // Define completion (dependent of users)
     $completion = new backup_userscompletion_setting('userscompletion', base_setting::IS_BOOLEAN, true);
     $completion->set_ui(new backup_setting_ui_checkbox($completion, get_string('rootsettinguserscompletion', 'backup')));
     $this->add_setting($completion);
     $users->add_dependency($completion);
     // Define logs (dependent of users)
     $logs = new backup_logs_setting('logs', base_setting::IS_BOOLEAN, true);
     $logs->set_ui(new backup_setting_ui_checkbox($logs, get_string('rootsettinglogs', 'backup')));
     $this->add_setting($logs);
     $users->add_dependency($logs);
     // Define grade_histories (dependent of users)
     $gradehistories = new backup_generic_setting('grade_histories', base_setting::IS_BOOLEAN, true);
     $gradehistories->set_ui(new backup_setting_ui_checkbox($gradehistories, get_string('rootsettinggradehistories', 'backup')));
     $this->add_setting($gradehistories);
     $users->add_dependency($gradehistories);
 }
 /**
  * Define the common setting that any backup type will have
  */
 protected function define_settings()
 {
     global $CFG;
     require_once $CFG->dirroot . '/backup/util/helper/convert_helper.class.php';
     // Define filename setting
     $filename = new backup_filename_setting('filename', base_setting::IS_FILENAME, 'backup.mbz');
     $filename->set_ui_filename(get_string('filename', 'backup'), 'backup.mbz', array('size' => 50));
     $this->add_setting($filename);
     // Present converter settings only in type course and mode general backup operations.
     $converters = array();
     if ($this->plan->get_type() == backup::TYPE_1COURSE and $this->plan->get_mode() == backup::MODE_GENERAL) {
         $converters = convert_helper::available_converters(false);
         foreach ($converters as $cnv) {
             $formatcnv = new backup_users_setting($cnv, base_setting::IS_BOOLEAN, false);
             $formatcnv->set_ui(new backup_setting_ui_checkbox($formatcnv, get_string('backupformat' . $cnv, 'backup')));
             $this->add_setting($formatcnv);
         }
     }
     // Define users setting (keeping it on hand to define dependencies)
     $users = new backup_users_setting('users', base_setting::IS_BOOLEAN, true);
     $users->set_ui(new backup_setting_ui_checkbox($users, get_string('rootsettingusers', 'backup')));
     $this->add_setting($users);
     $this->converter_deps($users, $converters);
     // Define anonymize (dependent of users)
     $anonymize = new backup_anonymize_setting('anonymize', base_setting::IS_BOOLEAN, false);
     $anonymize->set_ui(new backup_setting_ui_checkbox($anonymize, get_string('rootsettinganonymize', 'backup')));
     $this->add_setting($anonymize);
     $users->add_dependency($anonymize);
     // Define role_assignments (dependent of users)
     $roleassignments = new backup_role_assignments_setting('role_assignments', base_setting::IS_BOOLEAN, true);
     $roleassignments->set_ui(new backup_setting_ui_checkbox($roleassignments, get_string('rootsettingroleassignments', 'backup')));
     $this->add_setting($roleassignments);
     $users->add_dependency($roleassignments);
     // Define activities
     $activities = new backup_activities_setting('activities', base_setting::IS_BOOLEAN, true);
     $activities->set_ui(new backup_setting_ui_checkbox($activities, get_string('rootsettingactivities', 'backup')));
     $this->add_setting($activities);
     // Define blocks
     $blocks = new backup_generic_setting('blocks', base_setting::IS_BOOLEAN, true);
     $blocks->set_ui(new backup_setting_ui_checkbox($blocks, get_string('rootsettingblocks', 'backup')));
     $this->add_setting($blocks);
     $this->converter_deps($blocks, $converters);
     // Define filters
     $filters = new backup_generic_setting('filters', base_setting::IS_BOOLEAN, true);
     $filters->set_ui(new backup_setting_ui_checkbox($filters, get_string('rootsettingfilters', 'backup')));
     $this->add_setting($filters);
     $this->converter_deps($filters, $converters);
     // Define comments (dependent of users)
     $comments = new backup_comments_setting('comments', base_setting::IS_BOOLEAN, true);
     $comments->set_ui(new backup_setting_ui_checkbox($comments, get_string('rootsettingcomments', 'backup')));
     $this->add_setting($comments);
     $users->add_dependency($comments);
     // Define badges (dependent of activities).
     $badges = new backup_badges_setting('badges', base_setting::IS_BOOLEAN, true);
     $badges->set_ui(new backup_setting_ui_checkbox($badges, get_string('rootsettingbadges', 'backup')));
     $this->add_setting($badges);
     $activities->add_dependency($badges);
     $users->add_dependency($badges);
     // Define calendar events.
     $events = new backup_calendarevents_setting('calendarevents', base_setting::IS_BOOLEAN, true);
     $events->set_ui(new backup_setting_ui_checkbox($events, get_string('rootsettingcalendarevents', 'backup')));
     $this->add_setting($events);
     // Define completion (dependent of users)
     $completion = new backup_userscompletion_setting('userscompletion', base_setting::IS_BOOLEAN, true);
     $completion->set_ui(new backup_setting_ui_checkbox($completion, get_string('rootsettinguserscompletion', 'backup')));
     $this->add_setting($completion);
     $users->add_dependency($completion);
     // Define logs (dependent of users)
     $logs = new backup_logs_setting('logs', base_setting::IS_BOOLEAN, true);
     $logs->set_ui(new backup_setting_ui_checkbox($logs, get_string('rootsettinglogs', 'backup')));
     $this->add_setting($logs);
     $users->add_dependency($logs);
     // Define grade_histories (dependent of users)
     $gradehistories = new backup_generic_setting('grade_histories', base_setting::IS_BOOLEAN, true);
     $gradehistories->set_ui(new backup_setting_ui_checkbox($gradehistories, get_string('rootsettinggradehistories', 'backup')));
     $this->add_setting($gradehistories);
     $users->add_dependency($gradehistories);
     // The restore does not process the grade histories when some activities are ignored.
     // So let's define a dependency to prevent false expectations from our users.
     $activities->add_dependency($gradehistories);
     // Define question bank inclusion setting.
     $questionbank = new backup_generic_setting('questionbank', base_setting::IS_BOOLEAN, true);
     $questionbank->set_ui(new backup_setting_ui_checkbox($questionbank, get_string('rootsettingquestionbank', 'backup')));
     $this->add_setting($questionbank);
 }
 /**
  * Converts from current format to backup::MOODLE format
  */
 public function convert()
 {
     global $CFG;
     require_once $CFG->dirroot . '/backup/util/helper/convert_helper.class.php';
     // Basic/initial prevention against time/memory limits
     set_time_limit(1 * 60 * 60);
     // 1 hour for 1 course initially granted
     raise_memory_limit(MEMORY_EXTRA);
     if ($this->status != backup::STATUS_REQUIRE_CONV) {
         throw new restore_controller_exception('cannot_convert_not_required_status');
     }
     $this->log('backup format conversion required', backup::LOG_INFO);
     // Run conversion to the proper format
     if (!convert_helper::to_moodle2_format($this->get_tempdir(), $this->format, $this->get_logger())) {
         // todo - unable to find the conversion path, what to do now?
         // throwing the exception as a temporary solution
         throw new restore_controller_exception('unable_to_find_conversion_path');
     }
     $this->log('backup format conversion successful', backup::LOG_INFO);
     // If no exceptions were thrown, then we are in the proper format
     $this->format = backup::FORMAT_MOODLE;
     // Load plan, apply security and set status based on interactivity
     $this->load_plan();
     // Perform all initial security checks and apply (2nd param) them to settings automatically
     restore_check::check_security($this, true);
     if ($this->interactive == backup::INTERACTIVE_YES) {
         $this->set_status(backup::STATUS_SETTING_UI);
     } else {
         $this->set_status(backup::STATUS_NEED_PRECHECK);
     }
 }
 /**
  * Create all the steps that will be part of this task
  */
 public function build()
 {
     global $CFG;
     // Set the backup::VAR_CONTEXTID setting to course context as far as next steps require that
     $coursectxid = context_course::instance($this->get_courseid())->id;
     $this->add_setting(new backup_activity_generic_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $coursectxid));
     // Set the backup::VAR_COURSEID setting to course, we'll need that in some steps
     $courseid = $this->get_courseid();
     $this->add_setting(new backup_activity_generic_setting(backup::VAR_COURSEID, base_setting::IS_INTEGER, $courseid));
     // Generate the groups file with the final annotated groups and groupings
     // including membership based on setting
     $this->add_step(new backup_groups_structure_step('groups', 'groups.xml'));
     // Generate the questions file with the final annotated question_categories
     $this->add_step(new backup_questions_structure_step('questions', 'questions.xml'));
     // Annotate all the question files for the already annotated question
     // categories (this is performed here and not in the structure step because
     // it involves multiple contexts and as far as we are always backup-ing
     // complete question banks we don't need to restrict at all and can be
     // done in a single pass
     $this->add_step(new backup_annotate_all_question_files('question_files'));
     // Annotate all the user files (conditionally) (profile and icon files)
     // Because each user has its own context, we need a separate/specialised step here
     // This step also ensures that the contexts for all the users exist, so next
     // step can be safely executed (join between users and contexts)
     // Not executed if backup is without users of anonymized
     if ($this->get_setting_value('users') && !$this->get_setting_value('anonymize')) {
         $this->add_step(new backup_annotate_all_user_files('user_files'));
     }
     // Generate the users file (conditionally) with the final annotated users
     // including custom profile fields, preferences, tags, role assignments and
     // overrides
     if ($this->get_setting_value('users')) {
         $this->add_step(new backup_users_structure_step('users', 'users.xml'));
     }
     // Generate the top roles file with all the final annotated roles
     // that have been detected along the whole process. It's just
     // the list of role definitions (no assignments nor permissions)
     $this->add_step(new backup_final_roles_structure_step('roleslist', 'roles.xml'));
     // Generate the gradebook file with categories and course grade items. Do it conditionally, using
     // execute_condition() so only will be excuted if ALL module grade_items in course have been exported
     $this->add_step(new backup_gradebook_structure_step('course_gradebook', 'gradebook.xml'));
     // Generate the grade history file, conditionally.
     $this->add_step(new backup_grade_history_structure_step('course_grade_history', 'grade_history.xml'));
     // Generate the course completion
     $this->add_step(new backup_course_completion_structure_step('course_completion', 'completion.xml'));
     // Conditionally generate the badges file.
     if ($this->get_setting_value('badges')) {
         $this->add_step(new backup_badges_structure_step('course_badges', 'badges.xml'));
     }
     // Generate the scales file with all the (final) annotated scales
     $this->add_step(new backup_final_scales_structure_step('scaleslist', 'scales.xml'));
     // Generate the outcomes file with all the (final) annotated outcomes
     $this->add_step(new backup_final_outcomes_structure_step('outcomeslist', 'outcomes.xml'));
     // Migrate the pending annotations to final (prev steps may have added some files)
     // This must be executed before backup files
     $this->add_step(new move_inforef_annotations_to_final('migrate_inforef'));
     // Generate the files.xml file with all the (final) annotated files. At the same
     // time copy all the files from moodle storage to backup storage (uses custom
     // backup_nested_element for that)
     $this->add_step(new backup_final_files_structure_step('fileslist', 'files.xml'));
     // Write the main moodle_backup.xml file, with all the information related
     // to the backup, settings, license, versions and other useful information
     $this->add_step(new backup_main_structure_step('mainfile', 'moodle_backup.xml'));
     require_once $CFG->dirroot . '/backup/util/helper/convert_helper.class.php';
     // Look for converter steps only in type course and mode general backup operations.
     $conversion = false;
     if ($this->plan->get_type() == backup::TYPE_1COURSE and $this->plan->get_mode() == backup::MODE_GENERAL) {
         $converters = convert_helper::available_converters(false);
         foreach ($converters as $value) {
             if ($this->get_setting_value($value)) {
                 // Zip class.
                 $zip_contents = "{$value}_zip_contents";
                 $store_backup_file = "{$value}_store_backup_file";
                 $convert = "{$value}_backup_convert";
                 $this->add_step(new $convert("package_convert_{$value}"));
                 $this->add_step(new $zip_contents("zip_contents_{$value}"));
                 $this->add_step(new $store_backup_file("save_backupfile_{$value}"));
                 if (!$conversion) {
                     $conversion = true;
                 }
             }
         }
     }
     // On backup::MODE_IMPORT, we don't have to zip nor store the the file, skip these steps
     if ($this->plan->get_mode() != backup::MODE_IMPORT && !$conversion) {
         // Generate the zip file (mbz extension)
         $this->add_step(new backup_zip_contents('zip_contents'));
         // Copy the generated zip (.mbz) file to final destination
         $this->add_step(new backup_store_backup_file('save_backupfile'));
     }
     // Clean the temp dir (conditionally) and drop temp tables
     $cleanstep = new drop_and_clean_temp_stuff('drop_and_clean_temp_stuff');
     // Decide about to delete the temp dir (based on backup::MODE_IMPORT)
     $cleanstep->skip_cleaning_temp_dir($this->plan->get_mode() == backup::MODE_IMPORT);
     $this->add_step($cleanstep);
     $this->built = true;
 }
Example #9
0
 public static function choose_conversion_path($format, array $descriptions)
 {
     return parent::choose_conversion_path($format, $descriptions);
 }
<?php

define('CLI_SCRIPT', 1);
require_once '../../../config.php';
require_once $CFG->dirroot . '/backup/util/helper/convert_helper.class.php';
convert_helper::to_moodle2_format('refcourse', 'moodle1');