Skip to content

Commit

Permalink
backup MDL-22142 This is the user interface for backups, several mino…
Browse files Browse the repository at this point in the history
…r/cosmetic issues still to be ironed out. See the tracker for details
  • Loading branch information
Sam Hemelryk committed May 1, 2010
1 parent 9057a84 commit 1904e9b
Show file tree
Hide file tree
Showing 20 changed files with 2,177 additions and 203 deletions.
212 changes: 78 additions & 134 deletions backup/backup.php
Original file line number Diff line number Diff line change
@@ -1,136 +1,80 @@
<?php
//This script is used to configure and execute the backup proccess.

//Define some globals for all the script

require_once ("../config.php");
require_once ("lib.php");
require_once ("backuplib.php");
require_once ("$CFG->libdir/adminlib.php");

$id = optional_param('id', 0, PARAM_INT); // course id
$to = optional_param('to', 0, PARAM_INT); // id of course to import into afterwards.
$cancel = optional_param('cancel', '', PARAM_RAW);
$launch = optional_param('launch', '', PARAM_ACTION);

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

$loginurl = get_login_url();

if (!empty($id)) {
require_login($id);
if (!has_capability('moodle/backup:backupcourse', get_context_instance(CONTEXT_COURSE, $id))) {
print_error('cannotuseadminadminorteacher', 'error', $loginurl);
}
} else {
require_login();
if (!has_capability('moodle/backup:backupcourse', get_context_instance(CONTEXT_SYSTEM))) {
print_error('cannotuseadmin', 'error', $loginurl);
}
}

if (!empty($to)) {
if (!has_capability('moodle/backup:backupcourse', get_context_instance(CONTEXT_COURSE, $to))) {
print_error('cannotuseadminadminorteacher', 'error', $loginurl);
}
}

//Check site
$site = get_site();

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

//Get strings
if (empty($to)) {
$strcoursebackup = get_string("coursebackup");
}
else {
$strcoursebackup = get_string('importdata');
}
$stradministration = get_string("administration");

//If cancel has been selected, go back to course main page (bug 2817)
if ($cancel) {
if ($id) {
$redirecto = $CFG->wwwroot . '/course/view.php?id=' . $id; //Course page
} else {
$redirecto = $CFG->wwwroot.'/';
}
redirect ($redirecto, get_string('backupcancelled')); //Site page
exit;
}

//If no course has been selected, show a list of available courses
$PAGE->set_title("$site->shortname: $strcoursebackup");
$PAGE->set_heading($site->fullname);
if (!$id) {
$PAGE->navbar->add($stradministration, new moodle_url('/admin/index.php'));
$PAGE->navbar->add($strcoursebackup);
echo $OUTPUT->header();
if ($courses = get_courses('all','c.shortname','c.id,c.shortname,c.fullname')) {
echo $OUTPUT->heading(get_string("choosecourse"));
echo $OUTPUT->box_start();
foreach ($courses as $course) {
echo '<a href="backup.php?id='.$course->id.'">'.format_string($course->fullname).' ('.format_string($course->shortname).')</a><br />'."\n";
}
echo $OUTPUT->box_end();
} else {
echo $OUTPUT->heading(get_string("nocoursesyet"));
echo $OUTPUT->continue_button("$CFG->wwwroot/$CFG->admin/index.php");
}
echo $OUTPUT->footer();
exit;
}

//Get and check course
if (! $course = $DB->get_record("course", array("id"=>$id))) {
print_error('unknowncourseidnumber','error');
}

//Print header
if (has_capability('moodle/backup:backupcourse', get_context_instance(CONTEXT_SYSTEM))) {
$PAGE->navbar->add($stradministration, new moodle_url('/admin/index.php'));
$PAGE->navbar->add($strcoursebackup, new moodle_url('/backup/backup.php'));
$PAGE->navbar->add("$course->fullname ($course->shortname)");
echo $OUTPUT->header();
} else {
$PAGE->navbar->add($course->fullname, new moodle_url('/course/view.php', array('id'=>$course->id)));
$PAGE->navbar->add($strcoursebackup);
echo $OUTPUT->header();
}

//Print form
echo $OUTPUT->heading(format_string("$strcoursebackup: $course->fullname ($course->shortname)"));
echo $OUTPUT->box_start();

//Adjust some php variables to the execution of this script
@ini_set("max_execution_time","3000");
raise_memory_limit("192M");

//Call the form, depending the step we are
if (!$launch or !data_submitted() or !confirm_sesskey()) {
// if we're at the start, clear the cache of prefs
if (isset($SESSION->backupprefs[$course->id])) {
unset($SESSION->backupprefs[$course->id]);
}
include_once("backup_form.html");
} else if ($launch == "check") {
include_once("backup_check.html");
} else if ($launch == "execute") {
include_once("backup_execute.html");
}
echo $OUTPUT->box_end();

//Print footer
echo $OUTPUT->footer();
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

require_once('../config.php');
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_plan_builder.class.php');


$courseid = required_param('id', PARAM_INT);
$sectionid = optional_param('section', null, PARAM_INT);
$cmid = optional_param('cm', null, PARAM_INT);

$url = new moodle_url('/backup/backup.php', array('id'=>$courseid));
if ($sectionid !== null) {
$url->param('section', $sectionid);
}
if ($cmid !== null) {
$url->param('cm', $cmid);
}
$PAGE->set_url($url);

$id = $courseid;
$cm = null;
$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
$type = backup::TYPE_1COURSE;
if (!is_null($sectionid)) {
$section = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>$sectionid));
$type = backup::TYPE_1SECTION;
$id = $sectionid;
}
if (!is_null($cmid)) {
$cm = get_coursemodule_from_id(null, $cmid, $course->id, $sectionid, MUST_EXIST);
$type = backup::TYPE_1ACTIVITY;
$id = $cmid;
}
require_login($course, false, $cm);

if (!has_capability('moodle/backup:backupcourse', get_context_instance(CONTEXT_COURSE, $course->id))) {
print_error('cannotuseadminadminorteacher', 'error');
}

if (!($bc = backup_ui::load_controller())) {
$bc = new backup_controller($type, $id, backup::FORMAT_MOODLE,
backup::INTERACTIVE_YES, backup::MODE_GENERAL, $USER->id);
}
$backup = new backup_ui($bc);
$backup->process();
if ($backup->get_stage() == backup_ui::STAGE_FINAL) {
$backup->execute();
} else {
$backup->save_controller();
}

$PAGE->set_title(get_string('backup'));
$PAGE->set_heading(get_string('backup'));
$PAGE->navbar->add($backup->get_stage_name());
$PAGE->set_pagelayout('admin');

$renderer = $PAGE->get_renderer('core','backup');
echo $OUTPUT->header();
if ($backup->enforce_changed_dependencies()) {
echo $renderer->dependency_notification(get_string('dependenciesenforced','backup'));
}
echo $renderer->progress_bar($backup->get_progress_bar());
echo $backup->display();
echo $OUTPUT->footer();
1 change: 1 addition & 0 deletions backup/moodle2/backup_activity_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ protected function define_settings() {
// Define activity_userinfo (dependent of root users setting)
$settingname = $settingprefix . 'userinfo';
$activity_userinfo = new backup_activity_userinfo_setting($settingname, base_setting::IS_BOOLEAN, true);
$activity_userinfo->get_ui()->set_label(get_string('includeuserinfo','backup'));
$this->add_setting($activity_userinfo);
// Look for "users" root setting
$users = $this->plan->get_setting('users');
Expand Down
15 changes: 14 additions & 1 deletion backup/moodle2/backup_root_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,56 +48,69 @@ public function build() {
protected function define_settings() {

// Define filename setting
$this->add_setting(new backup_filename_setting('filename', base_setting::IS_FILENAME, 'backup.zip'));
$filename = new backup_filename_setting('filename', base_setting::IS_FILENAME, 'backup.zip');
$filename->set_ui(get_string('filename', 'backup'), 'backup.zip');
$this->add_setting($filename);

// 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_select($users, $users->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
$this->add_setting($users);

// Define anonymize (dependent of users)
$anonymize = new backup_anonymize_setting('anonymize', base_setting::IS_BOOLEAN, false);
$anonymize->set_ui(new backup_setting_ui_select($anonymize, $anonymize->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
$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_select($roleassignments, $roleassignments->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
$this->add_setting($roleassignments);
$users->add_dependency($roleassignments);

// Define user_files (dependent of users)
$userfiles = new backup_user_files_setting('user_files', base_setting::IS_BOOLEAN, true);
$userfiles->set_ui(new backup_setting_ui_select($userfiles, $userfiles->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
$this->add_setting($userfiles);
$users->add_dependency($userfiles);

// Define activitites
$activities = new backup_activities_setting('activities', base_setting::IS_BOOLEAN, true);
$activities->set_ui(new backup_setting_ui_select($activities, $activities->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
$this->add_setting($activities);

// Define blocks
$blocks = new backup_generic_setting('blocks', base_setting::IS_BOOLEAN, true);
$blocks->set_ui(new backup_setting_ui_select($blocks, $blocks->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
$this->add_setting($blocks);

// Define filters
$filters = new backup_generic_setting('filters', base_setting::IS_BOOLEAN, true);
$filters->set_ui(new backup_setting_ui_select($filters, $filters->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
$this->add_setting($filters);

// Define comments (dependent of users)
$comments = new backup_comments_setting('comments', base_setting::IS_BOOLEAN, true);
$comments->set_ui(new backup_setting_ui_select($comments, $comments->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
$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_select($completion, $completion->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
$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_select($logs, $logs->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
$this->add_setting($logs);
$users->add_dependency($logs);

// Define grade_histories
$gradehistories = new backup_generic_setting('grade_histories', base_setting::IS_BOOLEAN, true);
$gradehistories->set_ui(new backup_setting_ui_select($gradehistories, $gradehistories->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
$this->add_setting($gradehistories);
}
}
1 change: 1 addition & 0 deletions backup/moodle2/backup_section_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ protected function define_settings() {
// Define section_userinfo (dependent of root users setting)
$settingname = $settingprefix . 'userinfo';
$section_userinfo = new backup_section_userinfo_setting($settingname, base_setting::IS_BOOLEAN, true);
$section_userinfo->get_ui()->set_label(get_string('includeuserinfo','backup'));
$this->add_setting($section_userinfo);
// Look for "users" root setting
$users = $this->plan->get_setting('users');
Expand Down
9 changes: 9 additions & 0 deletions backup/moodle2/backup_settingslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ public function process_change($setting, $ctype, $oldv) {
* root setting to handle backup file names (no dependencies nor anything else)
*/
class backup_filename_setting extends backup_generic_setting {

public function __construct($name, $vtype, $value = null, $visibility = self::VISIBLE, $status = self::NOT_LOCKED) {
parent::__construct($name, $vtype, $value, $visibility, $status);
}

public function set_ui($label, $value, array $options = null) {
parent::make_ui(self::UI_HTML_TEXTFIELD, $label, null, $options);
$this->set_value($value);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion backup/util/factories/backup_factory.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public static function get_backup_section_task($format, $sectionid) {
throw new backup_task_exception('section_task_section_not_found', $sectionid);
}

return new backup_section_task($section->section, $sectionid);
return new backup_section_task(empty($section->name) ? $section->section : $section->name, $sectionid);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions backup/util/includes/backup_includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
require_once($CFG->dirroot . '/backup/util/loggers/file_logger.class.php');
require_once($CFG->dirroot . '/backup/util/loggers/database_logger.class.php');
require_once($CFG->dirroot . '/backup/util/loggers/output_indented_logger.class.php');
require_once($CFG->dirroot . '/backup/util/settings/setting_dependency.class.php');
require_once($CFG->dirroot . '/backup/util/settings/base_setting.class.php');
require_once($CFG->dirroot . '/backup/util/settings/backup_setting.class.php');
require_once($CFG->dirroot . '/backup/util/settings/root/root_backup_setting.class.php');
Expand All @@ -83,6 +84,10 @@
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/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');
require_once($CFG->dirroot . '/backup/util/ui/backup_ui_setting.class.php');

// And some moodle stuff too
require_once($CFG->libdir.'/gradelib.php');
21 changes: 15 additions & 6 deletions backup/util/settings/backup_setting.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,12 @@ public function get_level() {
return $this->level;
}

public function add_dependency($obj) {
if (! $obj instanceof backup_setting) {
throw new backup_setting_exception('dependency_is_not_backkup_setting');
}
public function add_dependency(backup_setting $dependentsetting, $type=setting_dependency::DISABLED_VALUE, $options=array()) {
// Check the dependency level is >= current level
if ($obj->get_level() < $this->level) {
if ($dependentsetting->get_level() < $this->level) {
throw new backup_setting_exception('cannot_add_upper_level_dependency');
}
parent::add_dependency($obj);
parent::add_dependency($dependentsetting, $type, $options);
}

// checksumable interface methods
Expand All @@ -64,6 +61,18 @@ public function calculate_checksum() {
public function is_checksum_correct($checksum) {
return $this->calculate_checksum() === $checksum;
}

public function get_dependencies() {
return $this->dependencies;
}

public function get_ui_name() {
return $this->uisetting->get_name();
}

public function get_ui_type() {
return $this->uisetting->get_type();
}
}

/*
Expand Down
Loading

0 comments on commit 1904e9b

Please sign in to comment.