Skip to content

Commit

Permalink
Merge branch 'MDL-27886-master' of git://github.com/tobiasreischmann/…
Browse files Browse the repository at this point in the history
…moodle
  • Loading branch information
Damyon Wiese committed Nov 20, 2017
2 parents 268e772 + 34eb2fa commit 18c02e9
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 20 deletions.
26 changes: 13 additions & 13 deletions backup/util/settings/setting_dependency.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ abstract public function enforce();
*/
abstract public function get_moodleform_properties();
/**
* Returns true if the dependent setting is locked.
* Returns true if the dependent setting is locked by this setting_dependency.
* @return bool
*/
abstract public function is_locked();
Expand Down Expand Up @@ -185,16 +185,16 @@ public function __construct(base_setting $setting, base_setting $dependentsettin
$this->value = ($value)?(string)$value:0;
}
/**
* Returns true if the dependent setting is locked.
* Returns true if the dependent setting is locked by this setting_dependency.
* @return bool
*/
public function is_locked() {
// If the setting is locked or the dependent setting should be locked then return true
if ($this->setting->get_status() !== base_setting::NOT_LOCKED || $this->setting->get_value() == $this->value) {
return true;
}
// Else return based upon the dependent settings status
return ($this->dependentsetting->get_status() !== base_setting::NOT_LOCKED);
// Else the dependent setting is not locked by this setting_dependency.
return false;
}
/**
* Processes a value change in the primary setting
Expand Down Expand Up @@ -343,16 +343,16 @@ public function __construct(base_setting $setting, base_setting $dependentsettin
$this->value = $value;
}
/**
* Returns true if the dependent setting is locked.
* Returns true if the dependent setting is locked by this setting_dependency.
* @return bool
*/
public function is_locked() {
// If the setting is locked or the dependent setting should be locked then return true
if ($this->setting->get_status() !== base_setting::NOT_LOCKED || in_array($this->setting->get_value(), $this->value)) {
return true;
}
// Else return based upon the dependent settings status
return ($this->dependentsetting->get_status() !== base_setting::NOT_LOCKED);
// Else the dependent setting is not locked by this setting_dependency.
return false;
}
/**
* Processes a value change in the primary setting
Expand Down Expand Up @@ -537,16 +537,16 @@ protected function process_value_change($oldvalue) {
}

/**
* Returns true if the dependent setting is locked.
* Returns true if the dependent setting is locked by this setting_dependency.
* @return bool
*/
public function is_locked() {
// If the setting is locked or the dependent setting should be locked then return true
if ($this->setting->get_status() !== base_setting::NOT_LOCKED || !empty($value)) {
return true;
}
// Else return based upon the dependent settings status
return ($this->dependentsetting->get_status() !== base_setting::NOT_LOCKED);
// Else the dependent setting is not locked by this setting_dependency.
return false;
}
}

Expand Down Expand Up @@ -601,15 +601,15 @@ protected function process_value_change($oldvalue) {
return ($prevalue != $this->dependentsetting->get_value());
}
/**
* Returns true if the dependent setting is locked.
* Returns true if the dependent setting is locked by this setting_dependency.
* @return bool
*/
public function is_locked() {
// If the setting is locked or the dependent setting should be locked then return true
if ($this->setting->get_status() !== base_setting::NOT_LOCKED || empty($value)) {
return true;
}
// Else return based upon the dependent settings status
return ($this->dependentsetting->get_status() !== base_setting::NOT_LOCKED);
// Else the dependent setting is not locked by this setting_dependency.
return false;
}
}
23 changes: 17 additions & 6 deletions backup/util/ui/backup_ui_setting.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,12 @@ public function get_label(base_task $task = null) {
* 2. The setting is locked but only by settings that are of the same level (same page)
*
* Condition 2 is really why we have this function
*
* @param int $level Optional, if provided only depedency_settings below or equal to this level are considered,
* when checking if the ui_setting is changeable. Although dependencies might cause a lock on this setting,
* they could be changeable in the same view.
* @return bool
*/
public function is_changeable() {
public function is_changeable($level = null) {
if ($this->setting->get_status() === backup_setting::NOT_LOCKED) {
// Its not locked so its chanegable.
return true;
Expand All @@ -319,6 +321,9 @@ public function is_changeable() {
return false;
} else if ($this->setting->has_dependencies_on_settings()) {
foreach ($this->setting->get_settings_depended_on() as $dependency) {
if ($level && $dependency->get_setting()->get_level() >= $level) {
continue;
}
if ($dependency->is_locked() && $dependency->get_setting()->get_level() !== $this->setting->get_level()) {
// Its not changeable because one or more dependancies arn't changeable.
return false;
Expand Down Expand Up @@ -458,13 +463,16 @@ public function get_static_value() {

/**
* Returns true if the setting is changeable
* @param int $level Optional, if provided only depedency_settings below or equal to this level are considered,
* when checking if the ui_setting is changeable. Although dependencies might cause a lock on this setting,
* they could be changeable in the same view.
* @return bool
*/
public function is_changeable() {
public function is_changeable($level = null) {
if ($this->changeable === false) {
return false;
} else {
return parent::is_changeable();
return parent::is_changeable($level);
}
}

Expand Down Expand Up @@ -639,13 +647,16 @@ public function get_static_value() {
/**
* Returns true if the setting is changeable, false otherwise
*
* @param int $level Optional, if provided only depedency_settings below or equal to this level are considered,
* when checking if the ui_setting is changeable. Although dependencies might cause a lock on this setting,
* they could be changeable in the same view.
* @return bool
*/
public function is_changeable() {
public function is_changeable($level = null) {
if (count($this->values) == 1) {
return false;
} else {
return parent::is_changeable();
return parent::is_changeable($level);
}
}

Expand Down
13 changes: 12 additions & 1 deletion backup/util/ui/base_moodleform.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,22 @@ public function add_setting(backup_setting $setting, base_task $task = null) {
public function add_settings(array $settingstasks) {
global $OUTPUT;

// Determine highest setting level, which is displayed in this stage. This is relevant for considering only
// locks of dependency settings for parent settings, which are not displayed in this stage.
$highestlevel = backup_setting::ACTIVITY_LEVEL;
foreach ($settingstasks as $st) {
list($setting, $task) = $st;
if ($setting->get_level() < $highestlevel) {
$highestlevel = $setting->get_level();
}
}

$defaults = array();
foreach ($settingstasks as $st) {
list($setting, $task) = $st;
// If the setting cant be changed or isn't visible then add it as a fixed setting.
if (!$setting->get_ui()->is_changeable() || $setting->get_visibility() != backup_setting::VISIBLE) {
if (!$setting->get_ui()->is_changeable($highestlevel) ||
$setting->get_visibility() != backup_setting::VISIBLE) {
$this->add_fixed_setting($setting, $task);
continue;
}
Expand Down
125 changes: 125 additions & 0 deletions backup/util/ui/tests/behat/restore_moodle2_courses_settings.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
@core @core_backup
Feature: Restore Moodle 2 course backups with different user data settings
In order to decide upon including user data during backup and restore of courses
As a teacher and an admin
I need to be able to set and override backup and restore settings

Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| student1 | Student | 1 | student1@example.com |
| teacher1 | Teacher | 1 | teacher1@example.com |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And the following "activities" exist:
| activity | name | intro | course | idnumber |
| data | Test database name | n | C1 | data1 |
And I log in as "teacher1"
And I am on "Course 1" course homepage
And I add a "Text input" field to "Test database name" database and I fill the form with:
| Field name | Test field name |
| Field description | Test field description |
And I follow "Templates"
And I wait until the page is ready
And I log out
And I log in as "student1"
And I am on "Course 1" course homepage
And I add an entry to "Test database name" database with:
| Test field name | Student entry |
And I press "Save and view"
And I log out
And I log in as "admin"
And I backup "Course 1" course using this options:
| Initial | Include enrolled users | 1 |
| Confirmation | Filename | test_backup.mbz |

@javascript
Scenario: Restore a backup with user data
# "User data" marks the user data field for the section
# "-" marks the user data field for the data activity
When I restore "test_backup.mbz" backup into a new course using this options:
| Settings | Include enrolled users | 1 |
| Schema | User data | 1 |
| Schema | - | 1 |
Then I should see "Test database name"
When I follow "Test database name"
Then I should see "Student entry"

@javascript
Scenario: Restore a backup without user data for data activity
# "User data" marks the user data field for the section
# "-" marks the user data field for the data activity
When I restore "test_backup.mbz" backup into a new course using this options:
| Settings | Include enrolled users | 1 |
| Schema | User data | 1 |
| Schema | - | 0 |
Then I should see "Test database name"
When I follow "Test database name"
Then I should not see "Student entry"

@javascript
Scenario: Restore a backup without user data for section and data activity
# "User data" marks the user data field for the section
# "-" marks the user data field for the data activity
When I restore "test_backup.mbz" backup into a new course using this options:
| Settings | Include enrolled users | 1 |
| Schema | User data | 0 |
| Schema | - | 0 |
Then I should see "Test database name"
When I follow "Test database name"
Then I should not see "Student entry"

@javascript
Scenario: Restore a backup without user data for section
# "User data" marks the user data field for the section
# "-" marks the user data field for the data activity
When I restore "test_backup.mbz" backup into a new course using this options:
| Settings | Include enrolled users | 1 |
| Schema | - | 1 |
| Schema | User data | 0 |
Then I should see "Test database name"
When I follow "Test database name"
Then I should not see "Student entry"

@javascript
Scenario: Restore a backup with user data with local config for including users set to 0
And I restore "test_backup.mbz" backup into a new course using this options:
| Settings | Include enrolled users | 0 |
Then I should see "Test database name"
When I follow "Test database name"
Then I should not see "Student entry"

@javascript
Scenario: Restore a backup with user data with site config for including users set to 0
Given I navigate to "General restore defaults" node in "Site administration > Courses > Backups"
And I set the field "s_restore_restore_general_users" to ""
And I press "Save changes"
And I am on "Course 1" course homepage
And I navigate to "Restore" node in "Course administration"
# "User data" marks the user data field for the section
# "-" marks the user data field for the data activity
And I restore "test_backup.mbz" backup into a new course using this options:
| Settings | Include enrolled users | 1 |
| Schema | User data | 1 |
| Schema | - | 1 |
Then I should see "Test database name"
When I follow "Test database name"
Then I should see "Student entry"

@javascript
Scenario: Restore a backup with user data with local and site config config for including users set to 0
Given I navigate to "General restore defaults" node in "Site administration > Courses > Backups"
And I set the field "s_restore_restore_general_users" to ""
And I press "Save changes"
And I am on "Course 1" course homepage
And I navigate to "Restore" node in "Course administration"
When I restore "test_backup.mbz" backup into a new course using this options:
| Settings | Include enrolled users | 0 |
Then I should see "Test database name"
When I follow "Test database name"
Then I should not see "Student entry"

0 comments on commit 18c02e9

Please sign in to comment.