From 96423fc89baf54ce3044d3f58c93af666493bb59 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Mon, 10 Feb 2025 13:26:09 +0100 Subject: [PATCH] MDL-84416 backup: Add backup_import_badges setting While MDL-43938 introduced badge importing, two tasks were left incomplete. 1) Creating the backup_import_badges setting for default import behaviour. 2) Preventing the inclusion of badges during activity duplication. This commit implements them. --- admin/settings/courses.php | 6 ++++++ .../util/dbops/backup_controller_dbops.class.php | 15 ++++++++++++++- .../ui/tests/behat/duplicate_activities.feature | 11 +++++++++++ backup/util/ui/tests/behat/import_course.feature | 15 ++++++++++++++- 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/admin/settings/courses.php b/admin/settings/courses.php index c45cda39ff5..38db42c6680 100644 --- a/admin/settings/courses.php +++ b/admin/settings/courses.php @@ -459,6 +459,12 @@ if ($hassiteconfig or has_any_capability($capabilities, $systemcontext)) { $temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_import_activities', new lang_string('generalactivities','backup'), new lang_string('configgeneralactivities','backup'), array('value'=>1, 'locked'=>0))); $temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_import_blocks', new lang_string('generalblocks','backup'), new lang_string('configgeneralblocks','backup'), array('value'=>1, 'locked'=>0))); $temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_import_filters', new lang_string('generalfilters','backup'), new lang_string('configgeneralfilters','backup'), array('value'=>1, 'locked'=>0))); + $temp->add(new admin_setting_configcheckbox_with_lock( + 'backup/backup_import_badges', + new lang_string('generalbadges', 'backup'), + new lang_string('configgeneralbadges', 'backup'), + ['value' => 0, 'locked' => 0], + )); $temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_import_calendarevents', new lang_string('generalcalendarevents','backup'), new lang_string('configgeneralcalendarevents','backup'), array('value'=>1, 'locked'=>0))); $temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_import_questionbank', new lang_string('generalquestionbank','backup'), new lang_string('configgeneralquestionbank','backup'), array('value'=>1, 'locked'=>0))); $temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_import_groups', diff --git a/backup/util/dbops/backup_controller_dbops.class.php b/backup/util/dbops/backup_controller_dbops.class.php index b920e071f5e..324e55f70d5 100644 --- a/backup/util/dbops/backup_controller_dbops.class.php +++ b/backup/util/dbops/backup_controller_dbops.class.php @@ -585,6 +585,7 @@ abstract class backup_controller_dbops extends backup_dbops { 'backup_import_activities' => 'activities', 'backup_import_blocks' => 'blocks', 'backup_import_filters' => 'filters', + 'backup_import_badges' => 'badges', 'backup_import_calendarevents' => 'calendarevents', 'backup_import_permissions' => 'permissions', 'backup_import_questionbank' => 'questionbank', @@ -605,6 +606,8 @@ abstract class backup_controller_dbops extends backup_dbops { 'questionbank' ); self::force_enable_settings($controller, $settings); + // Badges are not included by default when duplicating activities. + self::force_settings($controller, ['badges'], false); } break; case backup::MODE_AUTOMATED: @@ -645,9 +648,19 @@ abstract class backup_controller_dbops extends backup_dbops { * @param array $settings a map from admin config names to setting names (Config name => Setting name) */ private static function force_enable_settings(backup_controller $controller, array $settings) { + self::force_settings($controller, $settings, true); + } + + /** + * Set these settings to the given $value. No defaults from admin settings. + * + * @param backup_controller $controller The backup controller. + * @param array $settings a map from admin config names to setting names (Config name => Setting name). + * @param mixed $value the value to set the settings to. + */ + private static function force_settings(backup_controller $controller, array $settings, $value) { $plan = $controller->get_plan(); foreach ($settings as $config => $settingname) { - $value = true; if ($plan->setting_exists($settingname)) { $setting = $plan->get_setting($settingname); // We do not allow this setting to be locked for a duplicate function. diff --git a/backup/util/ui/tests/behat/duplicate_activities.feature b/backup/util/ui/tests/behat/duplicate_activities.feature index 2d598e04cb7..5efa1983e71 100644 --- a/backup/util/ui/tests/behat/duplicate_activities.feature +++ b/backup/util/ui/tests/behat/duplicate_activities.feature @@ -21,6 +21,12 @@ Feature: Duplicate activities | data | Test database name | Test database description | C1 | database1 | 1 | And the following config values are set as admin: | backup_import_activities | 0 | backup | + And the following "core_badges > Badges" exist: + | name | course | description | image | status | type | + | My course badge | C1 | Badge description | badges/tests/behat/badge.png | active | 2 | + And the following "core_badges > Criterias" exist: + | badge | role | + | My course badge | editingteacher | And I log in as "teacher1" And I am on "Course 1" course homepage with editing mode on And I duplicate "Test database name" activity @@ -38,3 +44,8 @@ Feature: Duplicate activities Then I should see "Original database name" in the "Section 1" "section" And I should see "Duplicated database name" in the "Section 1" "section" And "Original database name" "link" should appear before "Duplicated database name" "link" + # Check that badges are not duplicated. If they are duplicated, they will appear as "Not available". + And I navigate to "Badges" in current page administration + And the following should not exist in the "reportbuilder-table" table: + | Name | Badge status | + | My course badge | Not available | diff --git a/backup/util/ui/tests/behat/import_course.feature b/backup/util/ui/tests/behat/import_course.feature index 5f849f5b711..81ae35b1633 100644 --- a/backup/util/ui/tests/behat/import_course.feature +++ b/backup/util/ui/tests/behat/import_course.feature @@ -68,7 +68,7 @@ Feature: Import course's contents into another course | Published course badge | editingteacher | | Unpublished course badge | editingteacher | When I import "Course 1" course into "Course 2" course using this options: - | Settings | Include badges | 1 | + | Initial | Include badges | 1 | And I navigate to "Badges" in current page administration Then I should see "Published course badge" And I should see "Unpublished course badge" @@ -77,3 +77,16 @@ Feature: Import course's contents into another course And I should not see "Criteria for this badge have not been set up yet" in the "Published course badge" "table_row" And I should not see "Criteria for this badge have not been set up yet" in the "Unpublished course badge" "table_row" And I should see "Criteria for this badge have not been set up yet" in the "Unpublished without criteria course badge" "table_row" + + Scenario: Import process should not include badges by default + Given I log in as "teacher1" + And the following "core_badges > Badges" exist: + | name | course | description | image | status | type | + | Published course badge | C1 | Badge description | badges/tests/behat/badge.png | active | 2 | + And the following "core_badges > Criterias" exist: + | badge | role | + | Published course badge | editingteacher | + When I import "Course 1" course into "Course 2" course using this options: + | Initial | Include badges | 0 | + And I navigate to "Badges" in current page administration + Then I should not see "Published course badge"