Skip to content

Commit

Permalink
MDL-82057 backup: Badges independent of user data and activities
Browse files Browse the repository at this point in the history
The dependency of badges on user data and activities has been
removed when copying, importing, or creating backups in courses.
Now, badges can be imported or copied even if user data and activities
are not selected:

- If user data is not selected (during backup or course copying), the badge
information (name, description, image, etc.) and criteria will be included,
but the users who have been awarded the badge will be excluded.

- If activities are not selected (during backup), the badge information
(name, description, image, etc.) will be copied, but the criteria and users
who have been awarded the badge will be excluded.
  • Loading branch information
sarjona committed Jul 9, 2024
1 parent 7d7a871 commit 092e2d1
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 24 deletions.
2 changes: 0 additions & 2 deletions backup/moodle2/backup_root_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ protected function define_settings() {
$badges = new backup_badges_setting('badges', base_setting::IS_BOOLEAN, true);
$badges->set_ui(new backup_setting_ui_checkbox($badges, get_string('rootsettingbadges', 'backup')));
$this->add_setting($badges);
$activities->add_dependency($badges);
$users->add_dependency($badges);

// Define calendar events.
$events = new backup_calendarevents_setting('calendarevents', base_setting::IS_BOOLEAN, true);
Expand Down
45 changes: 23 additions & 22 deletions backup/moodle2/backup_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -914,14 +914,6 @@ protected function define_structure() {
*/
class backup_badges_structure_step extends backup_structure_step {

protected function execute_condition() {
// Check that all activities have been included.
if ($this->task->is_excluding_activities()) {
return false;
}
return true;
}

protected function define_structure() {
global $CFG;

Expand Down Expand Up @@ -965,10 +957,16 @@ protected function define_structure() {
// Build the tree.

$badges->add_child($badge);
$badge->add_child($criteria);
$criteria->add_child($criterion);
$criterion->add_child($parameters);
$parameters->add_child($parameter);

// Have the activities been included? Only if that's the case, the criteria will be included too.
$activitiesincluded = !$this->task->is_excluding_activities();
if ($activitiesincluded) {
$badge->add_child($criteria);
$criteria->add_child($criterion);
$criterion->add_child($parameters);
$parameters->add_child($parameter);
}

$badge->add_child($endorsement);
$badge->add_child($alignments);
$alignments->add_child($alignment);
Expand All @@ -990,19 +988,20 @@ protected function define_structure() {
'courseid' => backup::VAR_COURSEID
];
$badge->set_source_sql($parametersql, $parameterparams);
$criterion->set_source_table('badge_criteria', array('badgeid' => backup::VAR_PARENTID));
if ($activitiesincluded) {
$criterion->set_source_table('badge_criteria', ['badgeid' => backup::VAR_PARENTID]);
$parametersql = 'SELECT cp.*, c.criteriatype
FROM {badge_criteria_param} cp JOIN {badge_criteria} c
ON cp.critid = c.id
WHERE critid = :critid';
$parameterparams = ['critid' => backup::VAR_PARENTID];
$parameter->set_source_sql($parametersql, $parameterparams);
}
$endorsement->set_source_table('badge_endorsement', array('badgeid' => backup::VAR_PARENTID));

$alignment->set_source_table('badge_alignment', array('badgeid' => backup::VAR_PARENTID));
$relatedbadge->set_source_table('badge_related', array('badgeid' => backup::VAR_PARENTID));

$parametersql = 'SELECT cp.*, c.criteriatype
FROM {badge_criteria_param} cp JOIN {badge_criteria} c
ON cp.critid = c.id
WHERE critid = :critid';
$parameterparams = array('critid' => backup::VAR_PARENTID);
$parameter->set_source_sql($parametersql, $parameterparams);

$manual_award->set_source_table('badge_manual_award', array('badgeid' => backup::VAR_PARENTID));

$tag->set_source_sql('SELECT t.id, t.name, t.rawname
Expand All @@ -1015,8 +1014,10 @@ protected function define_structure() {

$badge->annotate_ids('user', 'usercreated');
$badge->annotate_ids('user', 'usermodified');
$criterion->annotate_ids('badge', 'badgeid');
$parameter->annotate_ids('criterion', 'critid');
if ($activitiesincluded) {
$criterion->annotate_ids('badge', 'badgeid');
$parameter->annotate_ids('criterion', 'critid');
}
$endorsement->annotate_ids('badge', 'badgeid');
$alignment->annotate_ids('badge', 'badgeid');
$relatedbadge->annotate_ids('badge', 'badgeid');
Expand Down
22 changes: 22 additions & 0 deletions backup/util/ui/tests/behat/import_course.feature
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,25 @@ Feature: Import course's contents into another course
| Initial | Include permission overrides | 0 |
And I am on the "Course 2" "permissions" page
Then I should see "Non-editing teacher (0)"

Scenario: Import course badges to another course
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 |
| Unpublished course badge | C1 | Badge description | badges/tests/behat/badge.png | 0 | 2 |
| Unpublished without criteria course badge | C1 | Badge description | badges/tests/behat/badge.png | 0 | 2 |
And the following "core_badges > Criterias" exist:
| badge | role |
| 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 |
And I navigate to "Badges > Manage badges" in current page administration
Then I should see "Published course badge"
And I should see "Unpublished course badge"
And I should see "Unpublished without criteria course badge"
# Badges exist and the criteria have been restored too.
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"
43 changes: 43 additions & 0 deletions backup/util/ui/tests/behat/restore_moodle2_courses.feature
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,46 @@ Feature: Restore Moodle 2 course backups
| Settings | Include permission overrides | 0 |
Then I am on the "Course 1 copy 1" "permissions" page
And I should see "Non-editing teacher (0)"

@javascript @core_badges
Scenario Outline: Restore course badges
Given 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 |
| Unpublished course badge | C1 | Badge description | badges/tests/behat/badge.png | 0 | 2 |
| Unpublished without criteria course badge | C1 | Badge description | badges/tests/behat/badge.png | 0 | 2 |
And the following "core_badges > Criterias" exist:
| badge | role |
| Published course badge | editingteacher |
| Unpublished course badge | editingteacher |
And I backup "Course 1" course using this options:
| Initial | Include badges | 1 |
| Initial | Include activities and resources | <includeactivities> |
| Initial | Include enrolled users | 0 |
| Initial | Include blocks | 0 |
| Initial | Include files | 0 |
| Initial | Include filters | 0 |
| Initial | Include calendar events | 0 |
| Initial | Include question bank | 0 |
| Initial | Include groups and groupings | 0 |
| Initial | Include competencies | 0 |
| Initial | Include custom fields | 0 |
| Initial | Include calendar events | 0 |
| Initial | Include content bank content | 0 |
| Initial | Include legacy course files | 0 |
| Confirmation | Filename | test_backup.mbz |
When I restore "test_backup.mbz" backup into a new course using this options:
| Settings | Include badges | 1 |
And I navigate to "Badges > Manage badges" in current page administration
Then I should see "Published course badge"
And I should see "Unpublished course badge"
And I should see "Unpublished without criteria course badge"
# If activities were included, the criteria have been restored too; otherwise no criteria have been set up for badges.
And I <shouldornotsee> "Criteria for this badge have not been set up yet" in the "Published course badge" "table_row"
And I <shouldornotsee> "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"

Examples:
| includeactivities | shouldornotsee |
| 0 | should see |
| 1 | should not see |

0 comments on commit 092e2d1

Please sign in to comment.