Skip to content

Commit

Permalink
Merge branch 'MDL-31500_master' of git://github.com/dmonllao/moodle
Browse files Browse the repository at this point in the history
Conflicts:
	backup/util/ui/renderer.php
  • Loading branch information
danpoltawski committed Nov 24, 2014
2 parents d148da7 + 03c39da commit bcc4dc3
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 16 deletions.
61 changes: 58 additions & 3 deletions backup/moodle2/backup_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,14 @@ protected function define_structure() {
*/
class backup_enrolments_structure_step extends backup_structure_step {

/**
* Skip enrolments on the front page.
* @return bool
*/
protected function execute_condition() {
return ($this->get_courseid() != SITEID);
}

protected function define_structure() {

// To know if we are including users
Expand Down Expand Up @@ -921,7 +929,12 @@ class backup_gradebook_structure_step extends backup_structure_step {
* the module gradeitems have been already included in backup
*/
protected function execute_condition() {
return backup_plan_dbops::require_gradebook_backup($this->get_courseid(), $this->get_backupid());
$courseid = $this->get_courseid();
if ($courseid == SITEID) {
return false;
}

return backup_plan_dbops::require_gradebook_backup($courseid, $this->get_backupid());
}

protected function define_structure() {
Expand Down Expand Up @@ -1035,7 +1048,12 @@ class backup_grade_history_structure_step extends backup_structure_step {
* because we do not want to save the history of items which are not backed up. At least for now.
*/
protected function execute_condition() {
return backup_plan_dbops::require_gradebook_backup($this->get_courseid(), $this->get_backupid());
$courseid = $this->get_courseid();
if ($courseid == SITEID) {
return false;
}

return backup_plan_dbops::require_gradebook_backup($courseid, $this->get_backupid());
}

protected function define_structure() {
Expand Down Expand Up @@ -1088,6 +1106,14 @@ protected function define_structure() {
*/
class backup_userscompletion_structure_step extends backup_structure_step {

/**
* Skip completion on the front page.
* @return bool
*/
protected function execute_condition() {
return ($this->get_courseid() != SITEID);
}

protected function define_structure() {

// Define each element separated
Expand Down Expand Up @@ -1617,6 +1643,7 @@ protected function define_structure() {
$info['original_site_identifier_hash'] = md5(get_site_identifier());
$info['original_course_id'] = $this->get_courseid();
$originalcourseinfo = backup_controller_dbops::backup_get_original_course_info($this->get_courseid());
$info['original_course_format'] = $originalcourseinfo->format;
$info['original_course_fullname'] = $originalcourseinfo->fullname;
$info['original_course_shortname'] = $originalcourseinfo->shortname;
$info['original_course_startdate'] = $originalcourseinfo->startdate;
Expand All @@ -1634,7 +1661,7 @@ protected function define_structure() {
$information = new backup_nested_element('information', null, array(
'name', 'moodle_version', 'moodle_release', 'backup_version',
'backup_release', 'backup_date', 'mnet_remoteusers', 'include_files', 'include_file_references_to_external_content', 'original_wwwroot',
'original_site_identifier_hash', 'original_course_id',
'original_site_identifier_hash', 'original_course_id', 'original_course_format',
'original_course_fullname', 'original_course_shortname', 'original_course_startdate',
'original_course_contextid', 'original_system_contextid'));

Expand Down Expand Up @@ -2102,6 +2129,12 @@ class backup_activity_grading_structure_step extends backup_structure_step {
* Include the grading.xml only if the module supports advanced grading
*/
protected function execute_condition() {

// No grades on the front page.
if ($this->get_courseid() == SITEID) {
return false;
}

return plugin_supports('mod', $this->get_task()->get_modulename(), FEATURE_ADVANCED_GRADING, false);
}

Expand Down Expand Up @@ -2175,6 +2208,14 @@ protected function define_structure() {
*/
class backup_activity_grades_structure_step extends backup_structure_step {

/**
* No grades on the front page.
* @return bool
*/
protected function execute_condition() {
return ($this->get_courseid() != SITEID);
}

protected function define_structure() {

// To know if we are including userinfo
Expand Down Expand Up @@ -2259,6 +2300,14 @@ protected function define_structure() {
*/
class backup_activity_grade_history_structure_step extends backup_structure_step {

/**
* No grades on the front page.
* @return bool
*/
protected function execute_condition() {
return ($this->get_courseid() != SITEID);
}

protected function define_structure() {

// Settings to use.
Expand Down Expand Up @@ -2306,6 +2355,12 @@ protected function define_structure() {
class backup_course_completion_structure_step extends backup_structure_step {

protected function execute_condition() {

// No completion on front page.
if ($this->get_courseid() == SITEID) {
return false;
}

// Check that all activities have been included
if ($this->task->is_excluding_activities()) {
return false;
Expand Down
45 changes: 45 additions & 0 deletions backup/moodle2/restore_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ class restore_gradebook_structure_step extends restore_structure_step {
protected function execute_condition() {
global $CFG, $DB;

if ($this->get_courseid() == SITEID) {
return false;
}

// No gradebook info found, don't execute
$fullpath = $this->task->get_taskbasepath();
$fullpath = rtrim($fullpath, '/') . '/' . $this->filename;
Expand Down Expand Up @@ -464,6 +468,10 @@ class restore_grade_history_structure_step extends restore_structure_step {
protected function execute_condition() {
global $CFG, $DB;

if ($this->get_courseid() == SITEID) {
return false;
}

// No gradebook info found, don't execute.
$fullpath = $this->task->get_taskbasepath();
$fullpath = rtrim($fullpath, '/') . '/' . $this->filename;
Expand Down Expand Up @@ -1814,9 +1822,15 @@ public function process_override($data) {
* If no instances yet add default enrol methods the same way as when creating new course in UI.
*/
class restore_default_enrolments_step extends restore_execution_step {

public function define_execution() {
global $DB;

// No enrolments in front page.
if ($this->get_courseid() == SITEID) {
return;
}

$course = $DB->get_record('course', array('id'=>$this->get_courseid()), '*', MUST_EXIST);

if ($DB->record_exists('enrol', array('courseid'=>$this->get_courseid(), 'enrol'=>'manual'))) {
Expand Down Expand Up @@ -1853,6 +1867,10 @@ class restore_enrolments_structure_step extends restore_structure_step {
*/
protected function execute_condition() {

if ($this->get_courseid() == SITEID) {
return false;
}

// Check it is included in the backup
$fullpath = $this->task->get_taskbasepath();
$fullpath = rtrim($fullpath, '/') . '/' . $this->filename;
Expand Down Expand Up @@ -2444,6 +2462,11 @@ protected function execute_condition() {
return false;
}

// No course completion on the front page.
if ($this->get_courseid() == SITEID) {
return false;
}

// Check it is included in the backup
$fullpath = $this->task->get_taskbasepath();
$fullpath = rtrim($fullpath, '/') . '/' . $this->filename;
Expand Down Expand Up @@ -2791,6 +2814,10 @@ class restore_activity_grading_structure_step extends restore_structure_step {
*/
protected function execute_condition() {

if ($this->get_courseid() == SITEID) {
return false;
}

$fullpath = $this->task->get_taskbasepath();
$fullpath = rtrim($fullpath, '/') . '/' . $this->filename;
if (!file_exists($fullpath)) {
Expand Down Expand Up @@ -2925,6 +2952,14 @@ protected function after_execute() {
*/
class restore_activity_grades_structure_step extends restore_structure_step {

/**
* No grades in front page.
* @return bool
*/
protected function execute_condition() {
return ($this->get_courseid() != SITEID);
}

protected function define_structure() {

$paths = array();
Expand Down Expand Up @@ -3061,6 +3096,11 @@ class restore_activity_grade_history_structure_step extends restore_structure_st
* This step is executed only if the grade history file is present.
*/
protected function execute_condition() {

if ($this->get_courseid() == SITEID) {
return false;
}

$fullpath = $this->task->get_taskbasepath();
$fullpath = rtrim($fullpath, '/') . '/' . $this->filename;
if (!file_exists($fullpath)) {
Expand Down Expand Up @@ -3447,6 +3487,11 @@ protected function execute_condition() {
return false;
}

// No completion on the front page.
if ($this->get_courseid() == SITEID) {
return false;
}

// No user completion info found, don't execute
$fullpath = $this->task->get_taskbasepath();
$fullpath = rtrim($fullpath, '/') . '/' . $this->filename;
Expand Down
75 changes: 75 additions & 0 deletions backup/moodle2/tests/moodle2_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,81 @@ public function test_restore_dates() {
'assign', 'allowsubmissionsfromdate', array('id' => $newassign->instance)));
}

/**
* Test front page backup/restore and duplicate activities
* @return void
*/
public function test_restore_frontpage() {
global $DB, $CFG, $USER;

$this->resetAfterTest(true);
$this->setAdminUser();
$generator = $this->getDataGenerator();

$frontpage = $DB->get_record('course', array('id' => SITEID));
$forum = $generator->create_module('forum', array('course' => $frontpage->id));

// Activities can be duplicated.
$this->duplicate($frontpage, $forum->cmid);

$modinfo = get_fast_modinfo($frontpage);
$this->assertEquals(2, count($modinfo->get_instances_of('forum')));

// Front page backup.
$frontpagebc = new backup_controller(backup::TYPE_1COURSE, $frontpage->id,
backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT,
$USER->id);
$frontpagebackupid = $frontpagebc->get_backupid();
$frontpagebc->execute_plan();
$frontpagebc->destroy();

$course = $generator->create_course();
$newcourseid = restore_dbops::create_new_course(
$course->fullname . ' 2', $course->shortname . '_2', $course->category);

// Other course backup.
$bc = new backup_controller(backup::TYPE_1COURSE, $course->id,
backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT,
$USER->id);
$otherbackupid = $bc->get_backupid();
$bc->execute_plan();
$bc->destroy();

// We can only restore a front page over the front page.
$rc = new restore_controller($frontpagebackupid, $course->id,
backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id,
backup::TARGET_CURRENT_ADDING);
$this->assertFalse($rc->execute_precheck());
$rc->destroy();

$rc = new restore_controller($frontpagebackupid, $newcourseid,
backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id,
backup::TARGET_NEW_COURSE);
$this->assertFalse($rc->execute_precheck());
$rc->destroy();

$rc = new restore_controller($frontpagebackupid, $frontpage->id,
backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id,
backup::TARGET_CURRENT_ADDING);
$this->assertTrue($rc->execute_precheck());
$rc->execute_plan();
$rc->destroy();

// We can't restore a non-front page course on the front page course.
$rc = new restore_controller($otherbackupid, $frontpage->id,
backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id,
backup::TARGET_CURRENT_ADDING);
$this->assertFalse($rc->execute_precheck());
$rc->destroy();

$rc = new restore_controller($otherbackupid, $newcourseid,
backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id,
backup::TARGET_NEW_COURSE);
$this->assertTrue($rc->execute_precheck());
$rc->execute_plan();
$rc->destroy();
}

/**
* Backs a course up and restores it.
*
Expand Down
2 changes: 1 addition & 1 deletion backup/util/dbops/backup_controller_dbops.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ public static function backup_includes_file_references($backupid) {
*/
public static function backup_get_original_course_info($courseid) {
global $DB;
return $DB->get_record('course', array('id' => $courseid), 'fullname, shortname, startdate');
return $DB->get_record('course', array('id' => $courseid), 'fullname, shortname, startdate, format');
}

/**
Expand Down
5 changes: 5 additions & 0 deletions backup/util/helper/backup_general_helper.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ public static function get_backup_information($tempdir) {
} else {
$info->include_file_references_to_external_content = 0;
}
// Introduced in Moodle 2.9.
$info->original_course_format = '';
if (!empty($infoarr['original_course_format'])) {
$info->original_course_format = $infoarr['original_course_format'];
}
// include_files is a new setting in 2.6.
if (isset($infoarr['include_files'])) {
$info->include_files = $infoarr['include_files'];
Expand Down
22 changes: 18 additions & 4 deletions backup/util/helper/restore_prechecks_helper.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,24 @@ public static function execute_prechecks(restore_controller $controller, $dropte
$warnings[] = get_string('noticenewerbackup','',$message);
}

// Error if restoring over frontpage
// TODO: Review the whole restore process in order to transform this into one warning (see 1.9)
if ($controller->get_courseid() == SITEID) {
$errors[] = get_string('errorrestorefrontpage', 'backup');
// The original_course_format var was introduced in Moodle 2.9.
$originalcourseformat = null;
if (!empty($controller->get_info()->original_course_format)) {
$originalcourseformat = $controller->get_info()->original_course_format;
}

// We can't restore other course's backups on the front page.
if ($controller->get_courseid() == SITEID &&
$originalcourseformat != 'site' &&
$controller->get_type() == backup::TYPE_1COURSE) {
$errors[] = get_string('errorrestorefrontpagebackup', 'backup');
}

// We can't restore front pages over other courses.
if ($controller->get_courseid() != SITEID &&
$originalcourseformat == 'site' &&
$controller->get_type() == backup::TYPE_1COURSE) {
$errors[] = get_string('errorrestorefrontpagebackup', 'backup');
}

// If restoring to different site and restoring users and backup has mnet users warn/error
Expand Down
6 changes: 4 additions & 2 deletions backup/util/ui/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ public function course_selector(moodle_url $nextstageurl, $wholecourse = true, r
$hasrestoreoption = false;

$html = html_writer::start_tag('div', array('class'=>'backup-course-selector backup-restore'));
if ($wholecourse && !empty($categories) && ($categories->get_count() > 0 || $categories->get_search())) {
if ($wholecourse && !empty($categories) && ($categories->get_count() > 0 || $categories->get_search()) &&
$currentcourse != SITEID) {

// New course
$hasrestoreoption = true;
$html .= $form;
Expand Down Expand Up @@ -306,7 +308,7 @@ public function course_selector(moodle_url $nextstageurl, $wholecourse = true, r
$courses->invalidate_results(); // Clean list of courses.
$courses->set_include_currentcourse();
}
if (!empty($courses) && ($courses->get_count() > 0 || $courses->get_search())) {
if (!empty($courses) && ($courses->get_count() > 0 || $courses->get_search()) && $currentcourse != SITEID) {
// Existing course
$hasrestoreoption = true;
$html .= $form;
Expand Down
Loading

0 comments on commit bcc4dc3

Please sign in to comment.