require_capability('moodle/restore:restorecourse', $context); if ($stage & restore_ui::STAGE_CONFIRM + restore_ui::STAGE_DESTINATION) { $restore = restore_ui::engage_independent_stage($stage, $contextid); } else { $restoreid = optional_param('restore', false, PARAM_ALPHANUM); $rc = restore_ui::load_controller($restoreid); if (!$rc) { $restore = restore_ui::engage_independent_stage($stage / 2, $contextid); if ($restore->process()) { $rc = new restore_controller($restore->get_filepath(), $restore->get_course_id(), backup::INTERACTIVE_YES, backup::MODE_GENERAL, $USER->id, $restore->get_target()); } } if ($rc) { // check if the format conversion must happen first if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) { $rc->convert(); } $restore = new restore_ui($rc, array('contextid' => $context->id)); } } $outcome = $restore->process(); if (!$restore->is_independent()) { if ($restore->get_stage() == restore_ui::STAGE_PROCESS && !$restore->requires_substage()) { try { $restore->execute(); } catch (Exception $e) { $restore->cleanup(); throw $e; } } else { $restore->save_controller();
/** * https://github.com/tmuras/moosh/blob/master/Moosh/Command/Moodle23/Course/CourseRestore.php * @param $bkpfile * @param $categoryId */ public static function execute($bkpfile, $categoryId) { global $CFG, $DB, $USER; require_once $CFG->dirroot . "/backup/util/includes/backup_includes.php"; require_once $CFG->dirroot . "/backup/util/includes/restore_includes.php"; if (empty($CFG->tempdir)) { $CFG->tempdir = $CFG->dataroot . DIRECTORY_SEPARATOR . 'temp'; } //unzip into $CFG->tempdir / "backup" / "auto_restore_" . $split[1]; $backupdir = "moosh_restore_" . uniqid(); $path = $CFG->tempdir . DIRECTORY_SEPARATOR . "backup" . DIRECTORY_SEPARATOR . $backupdir; /** @var $fp file_packer */ $fp = get_file_packer('application/vnd.moodle.backup'); $fp->extract_to_pathname($bkpfile, $path); //extract original full & short names $xmlfile = $path . DIRECTORY_SEPARATOR . "course" . DIRECTORY_SEPARATOR . "course.xml"; // Different XML file in Moodle 1.9 backup if (!file_exists($xmlfile)) { $xmlfile = $path . DIRECTORY_SEPARATOR . "moodle.xml"; } $xml = simplexml_load_file($xmlfile); $fullname = $xml->xpath('/course/fullname'); if (!$fullname) { $fullname = $xml->xpath('/MOODLE_BACKUP/COURSE/HEADER/FULLNAME'); } $shortname = $xml->xpath('/course/shortname'); if (!$shortname) { $shortname = $xml->xpath('/MOODLE_BACKUP/COURSE/HEADER/SHORTNAME'); } $fullname = (string) $fullname[0]; $shortname = (string) $shortname[0]; if (!$shortname) { cli_error('No shortname in the backup file.'); } if (!$fullname) { $fullname = $shortname; } $courseid = \restore_dbops::create_new_course($fullname, $shortname, $categoryId); $rc = new \restore_controller($backupdir, $courseid, \backup::INTERACTIVE_NO, \backup::MODE_GENERAL, 2, \backup::TARGET_NEW_COURSE); echo "Restoring (new course id,shortname,destination category): {$courseid}, {$shortname}," . $categoryId . "\n"; if ($rc->get_status() == \backup::STATUS_REQUIRE_CONV) { $rc->convert(); } $plan = $rc->get_plan(); //TODO: valider les options réquises. $restopt = array('activities' => 1, 'blocks' => 1, 'filters' => 1, 'users' => 0, 'role_assignments' => 1, 'comments' => 0, 'logs' => 0, 'grade_histories' => 0); foreach ($restopt as $name => $value) { $setting = $plan->get_setting($name); if ($setting->get_status() == \backup_setting::NOT_LOCKED) { $setting->set_value($value); } } $rc->execute_precheck(); $rc->execute_plan(); $rc->destroy(); echo "New course ID for '{$shortname}': {$courseid} in {$categoryId}\n"; // Ajouter le idnumber dans le nouveau cours. $c = $DB->get_record('course', array('id' => $courseid)); $c->idnumber = self::get_idnumber($shortname); $DB->update_record('course', $c); }
/** * Proceed with the import of the course. * * @return void */ public function proceed() { global $CFG, $USER; if (!$this->prepared) { throw new coding_exception('The course has not been prepared.'); } else { if ($this->has_errors()) { throw new moodle_exception('Cannot proceed, errors were detected.'); } else { if ($this->processstarted) { throw new coding_exception('The process has already been started.'); } } } $this->processstarted = true; if ($this->do === self::DO_DELETE) { if ($this->delete()) { $this->status('coursedeleted', new lang_string('coursedeleted', 'tool_uploadcourse')); } else { $this->error('errorwhiledeletingcourse', new lang_string('errorwhiledeletingcourse', 'tool_uploadcourse')); } return true; } else { if ($this->do === self::DO_CREATE) { $course = create_course((object) $this->data); $this->id = $course->id; $this->status('coursecreated', new lang_string('coursecreated', 'tool_uploadcourse')); } else { if ($this->do === self::DO_UPDATE) { $course = (object) $this->data; update_course($course); $this->id = $course->id; $this->status('courseupdated', new lang_string('courseupdated', 'tool_uploadcourse')); } else { // Strangely the outcome has not been defined, or is unknown! throw new coding_exception('Unknown outcome!'); } } } // Restore a course. if (!empty($this->restoredata)) { $rc = new restore_controller($this->restoredata, $course->id, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id, backup::TARGET_CURRENT_ADDING); // Check if the format conversion must happen first. if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) { $rc->convert(); } if ($rc->execute_precheck()) { $rc->execute_plan(); $this->status('courserestored', new lang_string('courserestored', 'tool_uploadcourse')); } else { $this->error('errorwhilerestoringcourse', new lang_string('errorwhilerestoringthecourse', 'tool_uploadcourse')); } $rc->destroy(); } // Proceed with enrolment data. $this->process_enrolment_data($course); // Reset the course. if ($this->importoptions['reset'] || $this->options['reset']) { if ($this->do === self::DO_UPDATE && $this->can_reset()) { $this->reset($course); $this->status('coursereset', new lang_string('coursereset', 'tool_uploadcourse')); } } // Mark context as dirty. $context = context_course::instance($course->id); $context->mark_dirty(); }
function restore_to_course($courseid, $backupid, $restoretarget, $admin) { global $CFG; // Check whether the backup directory still exists. If missing, something // went really wrong in backup, throw error. Note that backup::MODE_IMPORT // backups don't store resulting files ever $tempdestination = $CFG->tempdir . '/backup/' . $backupid; if (!file_exists($tempdestination) || !is_dir($tempdestination)) { print_error('unknownbackupexporterror'); // shouldn't happen ever } $rc = new restore_controller($backupid, $courseid, backup::INTERACTIVE_YES, backup::MODE_IMPORT,$admin->id,$restoretarget); // Convert the backup if required.... it should NEVER happed if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) { $rc->convert(); } // Mark the UI finished. $rc->finish_ui(); // Execute prechecks $rc->execute_precheck(); //if ($restoretarget == backup::TARGET_CURRENT_DELETING || $restoretarget == backup::TARGET_EXISTING_DELETING) { // restore_dbops::delete_course_content($courseid); //} // Execute the restore. $rc->execute_plan(); $rc->destroy(); unset($rc); // Delete the temp directory now fulldelete($tempdestination); }