/** * Function responsible for executing the tasks of any plan */ public function execute() { if ($this->controller->get_status() != backup::STATUS_AWAITING) { throw new restore_controller_exception('restore_not_executable_awaiting_required', $this->controller->get_status()); } $this->controller->set_status(backup::STATUS_EXECUTING); parent::execute(); $this->controller->set_status(backup::STATUS_FINISHED_OK); }
/** * Function responsible for executing the tasks of any plan */ public function execute() { if ($this->controller->get_status() != backup::STATUS_AWAITING) { throw new restore_controller_exception('restore_not_executable_awaiting_required', $this->controller->get_status()); } $this->controller->set_status(backup::STATUS_EXECUTING); parent::execute(); $this->controller->set_status(backup::STATUS_FINISHED_OK); events_trigger('course_restored', (object) array('courseid' => $this->get_courseid(), 'userid' => $this->get_userid(), 'type' => $this->controller->get_type(), 'target' => $this->controller->get_target(), 'mode' => $this->controller->get_mode(), 'operation' => $this->controller->get_operation(), 'samesite' => $this->controller->is_samesite())); }
/** * Function responsible for executing the tasks of any plan */ public function execute() { if ($this->controller->get_status() != backup::STATUS_AWAITING) { throw new restore_controller_exception('restore_not_executable_awaiting_required', $this->controller->get_status()); } $this->controller->set_status(backup::STATUS_EXECUTING); parent::execute(); $this->controller->set_status(backup::STATUS_FINISHED_OK); // Trigger a course restored event. $event = \core\event\course_restored::create(array('objectid' => $this->get_courseid(), 'userid' => $this->get_userid(), 'context' => context_course::instance($this->get_courseid()), 'other' => array('type' => $this->controller->get_type(), 'target' => $this->controller->get_target(), 'mode' => $this->controller->get_mode(), 'operation' => $this->controller->get_operation(), 'samesite' => $this->controller->is_samesite()))); $event->trigger(); }
/** * Send one restore controller to DB * * @param restore_controller $controller controller to send to DB * @param string $checksum hash of the controller to be checked * @param bool $includeobj to decide if the object itself must be updated (true) or no (false) * @param bool $cleanobj to decide if the object itself must be cleaned (true) or no (false) * @return int id of the controller record in the DB * @throws backup_controller_exception|restore_dbops_exception */ public static function save_controller($controller, $checksum, $includeobj = true, $cleanobj = false) { global $DB; // Check we are going to save one backup_controller if (!$controller instanceof restore_controller) { throw new backup_controller_exception('restore_controller_expected'); } // Check checksum is ok. Only if we are including object info. Sounds silly but it isn't ;-). if ($includeobj and !$controller->is_checksum_correct($checksum)) { throw new restore_dbops_exception('restore_controller_dbops_saving_checksum_mismatch'); } // Cannot request to $includeobj and $cleanobj at the same time. if ($includeobj and $cleanobj) { throw new restore_dbops_exception('restore_controller_dbops_saving_cannot_include_and_delete'); } // Get all the columns $rec = new stdclass(); $rec->backupid = $controller->get_restoreid(); $rec->operation = $controller->get_operation(); $rec->type = $controller->get_type(); $rec->itemid = $controller->get_courseid(); $rec->format = $controller->get_format(); $rec->interactive = $controller->get_interactive(); $rec->purpose = $controller->get_mode(); $rec->userid = $controller->get_userid(); $rec->status = $controller->get_status(); $rec->execution = $controller->get_execution(); $rec->executiontime = $controller->get_executiontime(); $rec->checksum = $checksum; // Serialize information if ($includeobj) { $rec->controller = base64_encode(serialize($controller)); } else { if ($cleanobj) { $rec->controller = ''; } } // Send it to DB if ($recexists = $DB->get_record('backup_controllers', array('backupid' => $rec->backupid))) { $rec->id = $recexists->id; $rec->timemodified = time(); $DB->update_record('backup_controllers', $rec); } else { $rec->timecreated = time(); $rec->timemodified = 0; $rec->id = $DB->insert_record('backup_controllers', $rec); } return $rec->id; }
/** * Function responsible for executing the tasks of any plan */ public function execute() { if ($this->controller->get_status() != backup::STATUS_AWAITING) { throw new restore_controller_exception('restore_not_executable_awaiting_required', $this->controller->get_status()); } $this->controller->set_status(backup::STATUS_EXECUTING); parent::execute(); $this->controller->set_status(backup::STATUS_FINISHED_OK); // Check if we are restoring a course. if ($this->controller->get_type() === backup::TYPE_1COURSE) { // Check to see if we are on the same site to pass original course info. $issamesite = $this->controller->is_samesite(); $otherarray = array('type' => $this->controller->get_type(), 'target' => $this->controller->get_target(), 'mode' => $this->controller->get_mode(), 'operation' => $this->controller->get_operation(), 'samesite' => $issamesite); if ($this->controller->is_samesite()) { $otherarray['originalcourseid'] = $this->controller->get_info()->original_course_id; } // Trigger a course restored event. $event = \core\event\course_restored::create(array('objectid' => $this->get_courseid(), 'userid' => $this->get_userid(), 'context' => context_course::instance($this->get_courseid()), 'other' => $otherarray)); $event->trigger(); } }
require_login($course, null, $cm); 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 {
/** * 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(); }
/** * 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); }
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); }