Skip to content

Commit

Permalink
backup MDL-23109 Implemented base restore UI within Moodle and linked…
Browse files Browse the repository at this point in the history
… through the navigation.
  • Loading branch information
Sam Hemelryk committed Jul 22, 2010
1 parent 003ffc8 commit 785d660
Show file tree
Hide file tree
Showing 25 changed files with 1,995 additions and 721 deletions.
15 changes: 14 additions & 1 deletion backup/controller/restore_controller.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ class restore_controller extends backup implements loggable {

protected $checksum; // Cache @checksumable results for lighter @is_checksum_correct() uses

/**
*
* @param string $tempdir Directory under dataroot/temp/backup awaiting restore
* @param int $courseid Course id where restore is going to happen
* @param bool $interactive backup::INTERACTIVE_YES[true] or backup::INTERACTIVE_NO[false]
* @param int $mode backup::MODE_[ GENERAL | HUB | IMPORT | SAMESITE ]
* @param int $userid
* @param int $target backup::TARGET_[ NEW_COURSE | CURRENT_ADDING | CURRENT_DELETING | EXISTING_ADDING | EXISTING_DELETING ]
*/
public function __construct($tempdir, $courseid, $interactive, $mode, $userid, $target){
$this->tempdir = $tempdir;
$this->courseid = $courseid;
Expand Down Expand Up @@ -128,7 +137,7 @@ public function __construct($tempdir, $courseid, $interactive, $mode, $userid, $

public function finish_ui() {
if ($this->status != backup::STATUS_SETTING_UI) {
throw new backup_controller_exception('cannot_finish_ui_if_not_setting_ui');
throw new restore_controller_exception('cannot_finish_ui_if_not_setting_ui');
}
$this->set_status(backup::STATUS_NEED_PRECHECK);
}
Expand Down Expand Up @@ -249,6 +258,10 @@ public function get_executiontime() {
return $this->executiontime;
}

/**
* Returns the restore plan
* @return restore_plan
*/
public function get_plan() {
return $this->plan;
}
Expand Down
14 changes: 14 additions & 0 deletions backup/moodle2/restore_course_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,23 @@ static public function decode_content_links($content) {
*/
protected function define_settings() {

//$name, $vtype, $value = null, $visibility = self::VISIBLE, $status = self::NOT_LOCKED
$fullname = new restore_course_generic_text_setting('course_fullname', base_setting::IS_TEXT, $this->get_info()->original_course_fullname);
$fullname->get_ui()->set_label(get_string('setting_course_fullname', 'backup'));
$this->add_setting($fullname);

$shortname = new restore_course_generic_text_setting('course_shortname', base_setting::IS_TEXT, $this->get_info()->original_course_shortname);
$shortname->get_ui()->set_label(get_string('setting_course_shortname', 'backup'));
$this->add_setting($shortname);

$startdate = new restore_course_generic_text_setting('course_startdate', base_setting::IS_INTEGER, $this->get_info()->original_course_startdate);
$startdate->set_ui(new backup_setting_ui_dateselector($startdate, get_string('setting_course_startdate', 'backup')));
$this->add_setting($startdate);

// Define overwrite_conf to decide if course configuration will be restored over existing one
$overwrite = new restore_course_overwrite_conf_setting('overwrite_conf', base_setting::IS_BOOLEAN, false);
$overwrite->set_ui(new backup_setting_ui_select($overwrite, $overwrite->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
$overwrite->get_ui()->set_label(get_string('setting_overwriteconf', 'backup'));
$this->add_setting($overwrite);

}
Expand Down
9 changes: 9 additions & 0 deletions backup/moodle2/restore_settingslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ class restore_course_generic_setting extends course_backup_setting {}
class restore_course_overwrite_conf_setting extends restore_course_generic_setting {}


class restore_course_generic_text_setting extends restore_course_generic_setting {

public function __construct($name, $vtype, $value = null, $visibility = self::VISIBLE, $status = self::NOT_LOCKED) {
parent::__construct($name, $vtype, $value, $visibility, $status);
$this->set_ui(new backup_setting_ui_text($this, $name));
}

}

// Section restore settings

/**
Expand Down
18 changes: 6 additions & 12 deletions backup/moodle2/restore_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,17 +543,10 @@ public function process_course($data) {
$coursetags = isset($data->tags['tag']) ? $data->tags['tag'] : array();
$coursemodules = isset($data->allowed_modules['module']) ? $data->allowed_modules['module'] : array();
$oldid = $data->id; // We'll need this later
debugging ('review the these lines of process_course() to change to settings once available', DEBUG_DEVELOPER);
// TODO: Get fullname, shortname, startdate and category from settings
// $fullname = $this->get_setting_value('course_fullname');
// $shortname = $this->get_setting_value('course_shortname');
// $category = $this->get_setting_value('course_category');
// $startdate = $this->get_setting_value('course_startdate');
// TODO: Delete this lines once we are getting the vars above from settings
$fullname = $this->task->get_info()->original_course_fullname;
$shortname= $this->task->get_info()->original_course_shortname;
$startdate= $this->task->get_info()->original_course_startdate;
$category = get_course_category()->id;

$fullname = $this->get_setting_value('course_fullname');
$shortname = $this->get_setting_value('course_shortname');
$startdate = $this->get_setting_value('course_startdate');

// Calculate final course names, to avoid dupes
list($fullname, $shortname) = restore_dbops::calculate_course_names($this->get_courseid(), $fullname, $shortname);
Expand All @@ -563,7 +556,8 @@ public function process_course($data) {
$data->fullname = $fullname;
$data->shortname= $shortname;
$data->idnumber = '';
$data->category = $category;
// TODO: Set category from the UI, its not a setting just a param
$data->category = get_course_category()->id;
$data->startdate= $this->apply_date_offset($data->startdate);
if ($data->defaultgroupingid) {
$data->defaultgroupingid = $this->get_mappingid('grouping', $data->defaultgroupingid);
Expand Down
223 changes: 46 additions & 177 deletions backup/restore.php
Original file line number Diff line number Diff line change
@@ -1,190 +1,59 @@
<?php
//This script is used to configure and execute the restore proccess.

//Define some globals for all the script
require_once('../config.php');
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_plan_builder.class.php');

//Units used
require_once("../config.php");
require_once("../lib/xmlize.php");
require_once("../course/lib.php");
require_once("lib.php");
require_once("restorelib.php");
require_once("bb/restore_bb.php");
require_once("$CFG->libdir/wiki_to_markdown.php" );
require_once("$CFG->libdir/adminlib.php");
$contextid = required_param('contextid', PARAM_INT);
$stage = optional_param('stage', restore_ui::STAGE_CONFIRM, PARAM_INT);

//Optional
$id = optional_param('id', 0, PARAM_INT);
$file = optional_param( 'file', 0, PARAM_PATH);
$cancel = optional_param('cancel', '', PARAM_RAW);
$launch = optional_param( 'launch', '', PARAM_ACTION);
$to = optional_param('to', '', PARAM_INT);
$method = optional_param('method', '', PARAM_ACTION);
$backup_unique_code = optional_param('backup_unique_code',0,PARAM_INT);
list($context, $course, $cm) = get_context_info_array($contextid);

$url = new moodle_url('/backup/restore.php');
if ($id !== 0) {
$url->param('id', $id);
}
if ($file !== 0) {
$url->param('file', $file);
}
if ($cancel !== '') {
$url->param('cancel', $cancel);
}
if ($launch !== '') {
$url->param('launch', $launch);
}
if ($to !== '') {
$url->param('to', $to);
}
if ($method !== '') {
$url->param('method', $method);
}
if ($backup_unique_code !== 0) {
$url->param('backup_unique_code', $backup_unique_code);
}
$PAGE->set_url($url);

$site = get_site();

/// With method=manual, we come from the FileManager so we delete all the backup/restore/import session structures
if ($method == 'manual') {
if (isset($SESSION->course_header)) {
unset ($SESSION->course_header);
}
if (isset($SESSION->info)) {
unset ($SESSION->info);
}
if (isset($SESSION->backupprefs)) {
unset ($SESSION->backupprefs);
}
if (isset($SESSION->restore)) {
unset ($SESSION->restore);
}
if (isset($SESSION->import_preferences)) {
unset ($SESSION->import_preferences);
}
}
$PAGE->set_url(new moodle_url('/backup/restore.php', array('contextid'=>$contextid)));
$PAGE->set_context($context);
$PAGE->set_pagelayout('standard');

if (!$to && isset($SESSION->restore->restoreto) && isset($SESSION->restore->importing) && isset($SESSION->restore->course_id)) {
$to = $SESSION->restore->course_id;
}
require_login($course, null, $cm);

$loginurl = get_login_url();
$isfrontpage = ($course->id == SITEID);

if (!empty($id)) {
require_login($id);
if (!has_capability('moodle/restore:restorecourse', get_context_instance(CONTEXT_COURSE, $id))) {
if (empty($to)) {
print_error("cannotuseadminadminorteacher", '', $loginurl);
} else {
if (!has_capability('moodle/restore:restorecourse', get_context_instance(CONTEXT_COURSE, $to))
&& !has_capability('moodle/restore:restoretargetimport', get_context_instance(CONTEXT_COURSE, $to))) {
print_error("cannotuseadminadminorteacher", '', $loginurl);
}
}
}
} else {
if (!has_capability('moodle/restore:restorecourse', get_context_instance(CONTEXT_SYSTEM))) {
print_error("cannotuseadmin", '', $loginurl);
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, backup::TARGET_NEW_COURSE);
}
}

//Check site
$site = get_site();

//Check necessary functions exists. Thanks to [email protected]
backup_required_functions();

//Get strings
if (empty($to)) {
$strcourserestore = get_string("courserestore");
} else {
$strcourserestore = get_string("importdata");
}
$stradministration = get_string("administration");

//If no file has been selected from the FileManager, inform and end
$PAGE->set_title("$site->shortname: $strcourserestore");
$PAGE->set_heading($site->fullname);
if (!$file) {
$PAGE->navbar->add($stradministration, new moodle_url('/admin/index.php'));
$PAGE->navbar->add($strcourserestore);
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string("nofilesselected"));
echo $OUTPUT->continue_button("$CFG->wwwroot/$CFG->admin/index.php");
echo $OUTPUT->footer();
exit;
}

//If cancel has been selected, inform and end
if ($cancel) {
$PAGE->navbar->add($stradministration, new moodle_url('/admin/index.php'));
$PAGE->navbar->add($strcourserestore);
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string("restorecancelled"));
echo $OUTPUT->continue_button("$CFG->wwwroot/course/view.php?id=".$id);
echo $OUTPUT->footer();
exit;
}

//We are here, so we have a file.

//Get and check course
if (! $course = $DB->get_record('course', array('id'=>$id))) {
error("Course ID was incorrect (can't find it)");
}

//Print header
if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
$PAGE->navbar->add(basename($file));
echo $OUTPUT->header();
} else {
$PAGE->navbar->add($course->shortname, new moodle_url('/course/view.php', array('id'=>$course->id)));
$PAGE->navbar->add($strcourserestore);
echo $OUTPUT->header();
if ($rc) {
$restore = new restore_ui($rc, array('contextid'=>$context->id));
}
//Print form
echo $OUTPUT->heading("$strcourserestore".((empty($to) ? ': '.basename($file) : '')));
echo $OUTPUT->box_start();

//Adjust some php variables to the execution of this script
@ini_set("max_execution_time","3000");
if (empty($CFG->extramemorylimit)) {
raise_memory_limit('128M');
}
$outcome = $restore->process();
if (!$restore->is_independent()) {
if ($restore->get_stage() == restore_ui::STAGE_PROCESS && !$restore->requires_substage()) {
$restore->execute();
} else {
raise_memory_limit($CFG->extramemorylimit);
}

//Call the form, depending the step we are

if (!$launch) {
include_once("restore_precheck.html");
} else if ($launch == "form") {
if (!empty($SESSION->restore->importing)) {
// set up all the config stuff and skip asking the user about it.
restore_setup_for_check($SESSION->restore,$backup_unique_code);
require_sesskey();
include_once("restore_execute.html");
} else {
include_once("restore_form.html");
}
} else if ($launch == "check") {
include_once("restore_check.html");
//To avoid multiple restore executions...
$SESSION->cancontinue = true;
} else if ($launch == "execute") {
//Prevent multiple restore executions...
if (empty($SESSION->cancontinue)) {
print_error('multiplerestorenotallow');
}
//Unset this for the future
unset($SESSION->cancontinue);
require_sesskey();
include_once("restore_execute.html");
}
echo $OUTPUT->box_end();

//Print footer
echo $OUTPUT->footer();
$restore->save_controller();
}
}
$heading = $course->fullname;

$PAGE->set_title($heading.': '.$restore->get_stage_name());
$PAGE->set_heading($heading);
$PAGE->settingsnav->get('courseadmin')->find('restore', navigation_node::TYPE_SETTING)->make_active();
$PAGE->navbar->add($restore->get_stage_name());

$renderer = $PAGE->get_renderer('core','backup');
echo $OUTPUT->header();
if (!$restore->is_independent() && $restore->enforce_changed_dependencies()) {
echo $renderer->dependency_notification(get_string('dependenciesenforced','backup'));
}
echo $renderer->progress_bar($restore->get_progress_bar());
echo $restore->display($renderer);
echo $OUTPUT->footer();
20 changes: 20 additions & 0 deletions backup/util/dbops/restore_dbops.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,26 @@ public static function set_course_role_names($restoreid, $courseid) {
}
$rs->close();
}

public static function create_new_course($fullname, $shortname, $categoryid) {
global $DB;
$category = $DB->get_record('course_categories', array('id'=>$categoryid), '*', MUST_EXIST);

$course = new stdClass;
$course->fullname = $fullname;
$course->shortname = $shortname;
$course->category = $category->id;
$course->sortorder = 0;
$course->timecreated = time();
$course->timemodified = $course->timecreated;
$course->visible = $category->visible;

return $DB->insert_record('course', $course);
}

public static function delete_course_content($courseid) {
return remove_course_contents($courseid);
}
}

/*
Expand Down
3 changes: 3 additions & 0 deletions backup/util/includes/backup_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
require_once($CFG->dirroot . '/backup/util/plan/backup_structure_step.class.php');
require_once($CFG->dirroot . '/backup/util/plan/backup_execution_step.class.php');
require_once($CFG->dirroot . '/backup/controller/backup_controller.class.php');
require_once($CFG->dirroot . '/backup/util/ui/base_moodleform.class.php');
require_once($CFG->dirroot . '/backup/util/ui/base_ui.class.php');
require_once($CFG->dirroot . '/backup/util/ui/base_ui_stage.class.php');
require_once($CFG->dirroot . '/backup/util/ui/backup_moodleform.class.php');
require_once($CFG->dirroot . '/backup/util/ui/backup_ui.class.php');
require_once($CFG->dirroot . '/backup/util/ui/backup_ui_stage.class.php');
Expand Down
8 changes: 6 additions & 2 deletions backup/util/includes/restore_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@
require_once($CFG->dirroot . '/backup/util/plan/restore_execution_step.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_plan_builder.class.php');
require_once($CFG->dirroot . '/backup/controller/restore_controller.class.php');
//require_once($CFG->dirroot . '/backup/util/ui/backup_ui.class.php');
//require_once($CFG->dirroot . '/backup/util/ui/backup_ui_stage.class.php');
require_once($CFG->dirroot . '/backup/util/ui/base_moodleform.class.php');
require_once($CFG->dirroot . '/backup/util/ui/base_ui.class.php');
require_once($CFG->dirroot . '/backup/util/ui/base_ui_stage.class.php');
require_once($CFG->dirroot . '/backup/util/ui/backup_ui_setting.class.php');
require_once($CFG->dirroot . '/backup/util/ui/restore_ui_stage.class.php');
require_once($CFG->dirroot . '/backup/util/ui/restore_ui.class.php');
require_once($CFG->dirroot . '/backup/util/ui/restore_moodleform.class.php');

// And some moodle stuff too
require_once ($CFG->dirroot . '/tag/lib.php');
Loading

0 comments on commit 785d660

Please sign in to comment.