From a306fd7a18ca8ab778773e99f016f0771d6d5b21 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Tue, 16 Feb 2021 10:45:25 +0100 Subject: [PATCH 1/3] MDL-70911 core_badges: Move "External backpack" to badges settings Setting "External backpack connection" has been moved from "Backpack settings" to "Badges settings" in order to make easier to find and configure it. Some extra tests have been added to cover existing behaviour and confirm is still working as expected. --- admin/settings/badges.php | 8 ++++---- badges/tests/behat/backpack.feature | 28 ++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/admin/settings/badges.php b/admin/settings/badges.php index a97097fea3b..6e4ca98be78 100644 --- a/admin/settings/badges.php +++ b/admin/settings/badges.php @@ -71,6 +71,10 @@ if (($hassiteconfig || has_any_capability(array( new lang_string('allowcoursebadges', 'badges'), new lang_string('allowcoursebadges_desc', 'badges'), 1)); + $globalsettings->add(new admin_setting_configcheckbox('badges_allowexternalbackpack', + new lang_string('allowexternalbackpack', 'badges'), + new lang_string('allowexternalbackpack_desc', 'badges'), 1)); + $ADMIN->add('badges', $globalsettings); $ADMIN->add('badges', @@ -100,10 +104,6 @@ if (($hassiteconfig || has_any_capability(array( $backpacksettings = new admin_settingpage('backpacksettings', new lang_string('backpacksettings', 'badges'), array('moodle/badges:manageglobalsettings'), empty($CFG->enablebadges)); - $backpacksettings->add(new admin_setting_configcheckbox('badges_allowexternalbackpack', - new lang_string('allowexternalbackpack', 'badges'), - new lang_string('allowexternalbackpack_desc', 'badges'), 1)); - $backpacksettings->add(new admin_setting_configselect('badges_site_backpack', new lang_string('sitebackpack', 'badges'), new lang_string('sitebackpack_help', 'badges'), diff --git a/badges/tests/behat/backpack.feature b/badges/tests/behat/backpack.feature index 3a979f6def0..1462fe9d1cd 100644 --- a/badges/tests/behat/backpack.feature +++ b/badges/tests/behat/backpack.feature @@ -17,13 +17,34 @@ Feature: Backpack badges And I set the field "Badge issuer email address" to "testuser@example.com" And I log out + @javascript + Scenario: If external backpack connection is disabled, backpack settings should not be displayed + Given I am on homepage + And I log in as "admin" + And I navigate to "Badges > Badges settings" in site administration + And I set the following fields to these values: + | External backpack connection | 0 | + And I press "Save changes" + When I navigate to "Badges" in site administration + Then I should not see "Manage backpacks" + And I navigate to "Badges > Badges settings" in site administration + And I set the following fields to these values: + | External backpack connection | 1 | + And I press "Save changes" + And I am on homepage + And I navigate to "Badges" in site administration + And I should see "Manage backpacks" + @javascript Scenario: Verify backback settings Given I am on homepage And I log in as "admin" - And I navigate to "Badges > Backpack settings" in site administration + And I navigate to "Badges > Badges settings" in site administration And I set the following fields to these values: | External backpack connection | 1 | + And I press "Save changes" + And I navigate to "Badges > Backpack settings" in site administration + And I set the following fields to these values: | Active external backpack | https://dc.imsglobal.org | And I press "Save changes" And I navigate to "Badges > Add a new badge" in site administration @@ -57,9 +78,12 @@ Feature: Backpack badges Scenario: User has been connected backpack Given I am on homepage And I log in as "admin" - And I navigate to "Badges > Backpack settings" in site administration + And I navigate to "Badges > Badges settings" in site administration And I set the following fields to these values: | External backpack connection | 1 | + And I press "Save changes" + And I navigate to "Badges > Backpack settings" in site administration + And I set the following fields to these values: | Active external backpack | https://dc.imsglobal.org | And I press "Save changes" And I navigate to "Badges > Add a new badge" in site administration From 066e998400151f8430cc1a3559a60d0c0795b5e6 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Wed, 17 Feb 2021 12:22:00 +0100 Subject: [PATCH 2/3] MDL-70911 core_badges: Add support to backpacks sortorder Before removing $CFG->badges_site_backpack setting, admins should be able to re-order the existing site-backpacks (because then, the first one will be treated as the default one). This patch adds the sort order feature to the backpack list. --- badges/backpacks.php | 11 +++ .../output/external_backpacks_page.php | 6 ++ .../external_backpacks_page.mustache | 25 +++++- badges/tests/badgeslib_test.php | 78 +++++++++++++++++++ badges/tests/behat/backpack.feature | 21 ++++- lib/badgeslib.php | 37 +++++++++ 6 files changed, 173 insertions(+), 5 deletions(-) diff --git a/badges/backpacks.php b/badges/backpacks.php index 635a1645cf8..84db40180db 100644 --- a/badges/backpacks.php +++ b/badges/backpacks.php @@ -56,6 +56,17 @@ if ($action == 'delete' && $confirm && confirm_sesskey()) { } else { $msg = get_string('sitebackpacknotdeleted', 'badges'); } +} else if ($action == 'moveup' || $action == 'movedown') { + // If no backpack has been selected, there isn't anything to move. + if (empty($id)) { + redirect($url); + } + + $direction = BACKPACK_MOVE_DOWN; + if ($action == 'moveup') { + $direction = BACKPACK_MOVE_UP; + } + badges_change_sortorder_backpacks($id, $direction); } if ($action == 'edit') { diff --git a/badges/classes/output/external_backpacks_page.php b/badges/classes/output/external_backpacks_page.php index 32c38db0a54..aa230cb27d7 100644 --- a/badges/classes/output/external_backpacks_page.php +++ b/badges/classes/output/external_backpacks_page.php @@ -60,6 +60,9 @@ class external_backpacks_page implements \renderable { $PAGE->requires->js_call_amd('core_badges/backpackactions', 'init'); + $rownumber = 0; + $rowcount = count($this->backpacks); + $data = new \stdClass(); $data->baseurl = $this->url; $data->backpacks = array(); @@ -69,8 +72,11 @@ class external_backpacks_page implements \renderable { $backpack = $exporter->export($output); $backpack->cantest = ($backpack->apiversion == OPEN_BADGES_V2); $backpack->iscurrent = ($backpack->id == $CFG->badges_site_backpack); + $backpack->canmoveup = $rownumber > 0; + $backpack->canmovedown = $rownumber < $rowcount - 1; $data->backpacks[] = $backpack; + $rownumber++; } return $data; diff --git a/badges/templates/external_backpacks_page.mustache b/badges/templates/external_backpacks_page.mustache index bfdcc5ec06c..5209ae46072 100644 --- a/badges/templates/external_backpacks_page.mustache +++ b/badges/templates/external_backpacks_page.mustache @@ -25,7 +25,13 @@ Example context (json): { "backpacks": [ - {"backpackweburl": "http://localhost/", "sitebackpack": true, "cantest": true} + { + "backpackweburl": "http://localhost/", + "sitebackpack": true, + "cantest": true, + "canmoveup": true, + "canmovedown": false + } ] } }} @@ -41,6 +47,7 @@ {{#str}}backpackweburl, core_badges{{/str}} {{#str}}sitebackpack, core_badges{{/str}} + {{#str}}order{{/str}} {{#str}}actions, core_badges{{/str}} @@ -49,6 +56,20 @@ {{{backpackweburl}}} {{#sitebackpack}}Yes{{/sitebackpack}} + + {{#canmoveup}} + {{#pix}}t/up, core,{{#str}}moveup{{/str}}{{/pix}} + {{/canmoveup}} + {{^canmoveup}} + {{#pix}}spacer, moodle{{/pix}} + {{/canmoveup}} + {{#canmovedown}} + {{#pix}}t/down, core,{{#str}}movedown{{/str}}{{/pix}} + {{/canmovedown}} + {{^canmovedown}} + {{#pix}}spacer, moodle{{/pix}} + {{/canmovedown}} + {{#pix}}t/edit, core,{{#str}}editsettings{{/str}}{{/pix}} {{^iscurrent}} @@ -63,4 +84,4 @@ {{/backpacks}} - \ No newline at end of file + diff --git a/badges/tests/badgeslib_test.php b/badges/tests/badgeslib_test.php index 2c0968446b2..49c557e2153 100644 --- a/badges/tests/badgeslib_test.php +++ b/badges/tests/badgeslib_test.php @@ -1344,6 +1344,84 @@ class core_badges_badgeslib_testcase extends advanced_testcase { ]; } + /** + * Test badges_change_sortorder_backpacks(). + * + * @dataProvider badges_change_sortorder_backpacks_provider + * @covers ::badges_change_sortorder_backpacks + * + * @param int $backpacktomove Backpack index to move (from 0 to 5). + * @param int $direction Direction to move the backpack. + * @param int|null $expectedsortorder Expected sortorder or null if an exception is expected. + */ + public function test_badges_change_sortorder_backpacks(int $backpacktomove, int $direction, ?int $expectedsortorder): void { + global $DB; + + $this->resetAfterTest(); + $this->setAdminUser(); + + // Create 5 more backpacks. + for ($i = 0; $i < 5; $i++) { + $data = new \stdClass(); + $data->apiversion = OPEN_BADGES_V2P1; + $data->backpackapiurl = "https://myurl$i.cat/ob/v2p1"; + $data->backpackweburl = "https://myurl$i.cat"; + badges_create_site_backpack($data); + } + + // Check there are 6 backpacks (1 pre-existing + 5 news). + $total = $DB->count_records('badge_external_backpack'); + $this->assertEquals(6, $total); + $backpacks = array_values(badges_get_site_backpacks()); + + if (is_null($expectedsortorder)) { + $this->expectException('moodle_exception'); + } + + // Move the backpack. + badges_change_sortorder_backpacks($backpacks[$backpacktomove]->id, $direction); + + if (!is_null($expectedsortorder)) { + $backpack = badges_get_site_backpack($backpacks[$backpacktomove]->id); + $this->assertEquals($expectedsortorder, $backpack->sortorder); + } + } + + /** + * Provider for test_badges_change_sortorder_backpacks. + * + * @return array + */ + public function badges_change_sortorder_backpacks_provider(): array { + return [ + "Test up" => [ + 'backpacktomove' => 1, + 'direction' => BACKPACK_MOVE_UP, + 'expectedsortorder' => 1, + ], + "Test down" => [ + 'backpacktomove' => 1, + 'direction' => BACKPACK_MOVE_DOWN, + 'expectedsortorder' => 3, + ], + "Test up the very first element" => [ + 'backpacktomove' => 0, + 'direction' => BACKPACK_MOVE_UP, + 'expectedsortorder' => 1, + ], + "Test down the very last element" => [ + 'backpacktomove' => 5, + 'direction' => BACKPACK_MOVE_DOWN, + 'expectedsortorder' => 6, + ], + "Test with an invalid direction value" => [ + 'backpacktomove' => 1, + 'direction' => 10, + 'expectedsortorder' => null, + ], + ]; + } + /** * Test the Badgr URL generator function * diff --git a/badges/tests/behat/backpack.feature b/badges/tests/behat/backpack.feature index 1462fe9d1cd..944c81f5050 100644 --- a/badges/tests/behat/backpack.feature +++ b/badges/tests/behat/backpack.feature @@ -5,9 +5,9 @@ Feature: Backpack badges Background: Given the following "badge external backpacks" exist: - | backpackapiurl | backpackweburl | apiversion | - | https://dc.imsglobal.org/obchost/ims/ob/v2p1 | https://dc.imsglobal.org | 2.1 | - | https://test.com/ | https://test.com/ | 2 | + | backpackapiurl | backpackweburl | apiversion | sortorder | + | https://dc.imsglobal.org/obchost/ims/ob/v2p1 | https://dc.imsglobal.org | 2.1 | 2 | + | https://test.com/ | https://test.com/ | 2 | 3 | And the following "users" exist: | username | firstname | lastname | email | | student1 | Student | 1 | student1@example.com | @@ -146,6 +146,21 @@ Feature: Backpack badges And I should not see "https://dc.imsglobal.org" And "Delete" "button" should not exist + @javascript + Scenario: Move up and down site backpack + Given I am on homepage + And I log in as "admin" + And I navigate to "Badges > Manage backpacks" in site administration + And "Move up" "icon" should exist in the "https://dc.imsglobal.org" "table_row" + And "Move down" "icon" should exist in the "https://dc.imsglobal.org" "table_row" + When I click on "Move up" "link" in the "https://dc.imsglobal.org" "table_row" + Then "Move up" "icon" should not exist in the "https://dc.imsglobal.org" "table_row" + And "Move down" "icon" should exist in the "https://dc.imsglobal.org" "table_row" + And I click on "Move down" "link" in the "https://dc.imsglobal.org" "table_row" + And I click on "Move down" "link" in the "https://dc.imsglobal.org" "table_row" + And "Move up" "icon" should exist in the "https://dc.imsglobal.org" "table_row" + And "Move down" "icon" should not exist in the "https://dc.imsglobal.org" "table_row" + @javascript Scenario: Add a new site backpack with authentication details checkbox Given I am on homepage diff --git a/lib/badgeslib.php b/lib/badgeslib.php index d67c1deeeb6..c88f9ea29dd 100644 --- a/lib/badgeslib.php +++ b/lib/badgeslib.php @@ -127,6 +127,9 @@ define('OPEN_BADGES_V2_TYPE_ISSUER', 'Issuer'); define('OPEN_BADGES_V2_TYPE_ENDORSEMENT', 'Endorsement'); define('OPEN_BADGES_V2_TYPE_AUTHOR', 'Author'); +define('BACKPACK_MOVE_UP', -1); +define('BACKPACK_MOVE_DOWN', 1); + // Global badge class has been moved to the component namespace. class_alias('\core_badges\badge', 'badge'); @@ -996,6 +999,40 @@ function badges_get_site_backpacks() { return $all; } +/** + * Moves the backpack in the list one position up or down. + * + * @param int $backpackid The backpack identifier to be moved. + * @param int $direction The direction (BACKPACK_MOVE_UP/BACKPACK_MOVE_DOWN) where to move the backpack. + * + * @throws \moodle_exception if attempting to use invalid direction value. + */ +function badges_change_sortorder_backpacks(int $backpackid, int $direction): void { + global $DB; + + if ($direction != BACKPACK_MOVE_UP && $direction != BACKPACK_MOVE_DOWN) { + throw new \coding_exception( + 'Must use a valid backpack API move direction constant (BACKPACK_MOVE_UP or BACKPACK_MOVE_DOWN)'); + } + + $backpacks = badges_get_site_backpacks(); + $backpacktoupdate = $backpacks[$backpackid]; + + $currentsortorder = $backpacktoupdate->sortorder; + $targetsortorder = $currentsortorder + $direction; + if ($targetsortorder > 0 && $targetsortorder <= count($backpacks) ) { + foreach ($backpacks as $backpack) { + if ($backpack->sortorder == $targetsortorder) { + $backpack->sortorder = $backpack->sortorder - $direction; + $DB->update_record('badge_external_backpack', $backpack); + break; + } + } + $backpacktoupdate->sortorder = $targetsortorder; + $DB->update_record('badge_external_backpack', $backpacktoupdate); + } +} + /** * List the supported badges api versions. * From 49c1d41a606d774a103a3d275cf249c9dfc91ab8 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Wed, 17 Feb 2021 16:33:20 +0100 Subject: [PATCH 3/3] MDL-70911 core_badges: Remove $CFG->badges_site_backpack The $CFG->badges_site_backpack setting has been completely removed because it's not required anymore. From now, the primary site backpack will be the first one in the "Manage backpacks" list (so, the one with lower sortorder value). --- admin/settings/badges.php | 19 ----------- badges/classes/form/backpack.php | 3 +- .../output/external_backpacks_page.php | 3 +- .../external_backpacks_page.mustache | 6 ++-- badges/tests/badgeslib_test.php | 30 +++++++++++++++-- badges/tests/behat/backpack.feature | 12 +++---- badges/upgrade.txt | 4 +++ badges/upgradelib.php | 3 -- lang/en/badges.php | 5 +-- lang/en/deprecated.txt | 2 ++ lib/badgeslib.php | 33 +++++++++++-------- lib/db/upgrade.php | 8 +++++ version.php | 2 +- 13 files changed, 74 insertions(+), 56 deletions(-) diff --git a/admin/settings/badges.php b/admin/settings/badges.php index 6e4ca98be78..997808e9e53 100644 --- a/admin/settings/badges.php +++ b/admin/settings/badges.php @@ -57,16 +57,6 @@ if (($hassiteconfig || has_any_capability(array( new lang_string('badgesalt_desc', 'badges'), 'badges' . $SITE->timecreated, PARAM_ALPHANUM)); - $backpacks = badges_get_site_backpacks(); - $choices = array(); - $defaultchoice = 0; - foreach ($backpacks as $backpack) { - $choices[$backpack->id] = $backpack->backpackweburl; - if ($backpack->backpackweburl == BADGRIO_BACKPACKWEBURL) { - $defaultchoice = $backpack->id; - } - } - $globalsettings->add(new admin_setting_configcheckbox('badges_allowcoursebadges', new lang_string('allowcoursebadges', 'badges'), new lang_string('allowcoursebadges_desc', 'badges'), 1)); @@ -101,15 +91,6 @@ if (($hassiteconfig || has_any_capability(array( array('moodle/badges:createbadge'), empty($CFG->enablebadges) ) ); - $backpacksettings = new admin_settingpage('backpacksettings', new lang_string('backpacksettings', 'badges'), - array('moodle/badges:manageglobalsettings'), empty($CFG->enablebadges)); - - $backpacksettings->add(new admin_setting_configselect('badges_site_backpack', - new lang_string('sitebackpack', 'badges'), - new lang_string('sitebackpack_help', 'badges'), - $defaultchoice, $choices)); - - $ADMIN->add('badges', $backpacksettings); $ADMIN->add('badges', new admin_externalpage('managebackpacks', diff --git a/badges/classes/form/backpack.php b/badges/classes/form/backpack.php index 6310b4cdd9a..e7341543b3a 100644 --- a/badges/classes/form/backpack.php +++ b/badges/classes/form/backpack.php @@ -78,7 +78,8 @@ class backpack extends external_backpack { } $mform->addElement('select', 'externalbackpackid', get_string('backpackprovider', 'badges'), $choices); $mform->setType('externalbackpackid', PARAM_INT); - $mform->setDefault('externalbackpackid', $CFG->badges_site_backpack); + $defaultbackpack = badges_get_site_primary_backpack(); + $mform->setDefault('externalbackpackid', $defaultbackpack->id); $mform->hideIf('password', 'externalbackpackid', 'in', $restrictedoptions); $mform->hideIf('backpackemail', 'externalbackpackid', 'in', $restrictedoptions); diff --git a/badges/classes/output/external_backpacks_page.php b/badges/classes/output/external_backpacks_page.php index aa230cb27d7..195658ce6c6 100644 --- a/badges/classes/output/external_backpacks_page.php +++ b/badges/classes/output/external_backpacks_page.php @@ -56,7 +56,7 @@ class external_backpacks_page implements \renderable { * @return stdClass */ public function export_for_template(\renderer_base $output) { - global $CFG, $PAGE; + global $PAGE; $PAGE->requires->js_call_amd('core_badges/backpackactions', 'init'); @@ -71,7 +71,6 @@ class external_backpacks_page implements \renderable { $exporter = new backpack_exporter($backpack); $backpack = $exporter->export($output); $backpack->cantest = ($backpack->apiversion == OPEN_BADGES_V2); - $backpack->iscurrent = ($backpack->id == $CFG->badges_site_backpack); $backpack->canmoveup = $rownumber > 0; $backpack->canmovedown = $rownumber < $rowcount - 1; diff --git a/badges/templates/external_backpacks_page.mustache b/badges/templates/external_backpacks_page.mustache index 5209ae46072..92eb673a2cc 100644 --- a/badges/templates/external_backpacks_page.mustache +++ b/badges/templates/external_backpacks_page.mustache @@ -46,7 +46,6 @@ {{#str}}backpackweburl, core_badges{{/str}} - {{#str}}sitebackpack, core_badges{{/str}} {{#str}}order{{/str}} {{#str}}actions, core_badges{{/str}} @@ -55,7 +54,6 @@ {{#backpacks}} {{{backpackweburl}}} - {{#sitebackpack}}Yes{{/sitebackpack}} {{#canmoveup}} {{#pix}}t/up, core,{{#str}}moveup{{/str}}{{/pix}} @@ -72,11 +70,11 @@ {{#pix}}t/edit, core,{{#str}}editsettings{{/str}}{{/pix}} - {{^iscurrent}} + {{^sitebackpack}} {{#pix}}t/delete, core,{{#str}}delete{{/str}}{{/pix}} - {{/iscurrent}} + {{/sitebackpack}} {{#cantest}} {{#pix}}t/check, core,{{#str}}testsettings, core_badges{{/str}}{{/pix}} {{/cantest}} diff --git a/badges/tests/badgeslib_test.php b/badges/tests/badgeslib_test.php index 49c557e2153..475f818658f 100644 --- a/badges/tests/badgeslib_test.php +++ b/badges/tests/badgeslib_test.php @@ -1196,6 +1196,7 @@ class core_badges_badgeslib_testcase extends advanced_testcase { 'apiversion' => 2, 'backpackapiurl' => 'https://api.ca.badgr.io/v2', 'backpackweburl' => 'https://ca.badgr.io', + 'sortorder' => 2, ]; // Given a complete set of unique data, a new backpack and auth records should exist in the tables. @@ -1204,10 +1205,13 @@ class core_badges_badgeslib_testcase extends advanced_testcase { $backpack1 = badges_save_external_backpack((object) $data); $data['backpackweburl'] = 'https://eu.badgr.io'; $data['backpackapiurl'] = 'https://api.eu.badgr.io/v2'; - $data['apiversion'] = 2.1; + $data['apiversion'] = '2.1'; + $data['sortorder'] = 3; $backpack2 = badges_save_external_backpack((object) $data); - set_config('badges_site_backpack', $backpack2); + // Move backpack2 to the first position to set it as primary site backpack. + $this->move_backpack_to_first_position($backpack2); + // The default response should check the default site backpack api version. $this->assertEquals(2.1, badges_open_badges_backpack_api()); // Check the api version for the other backpack created. @@ -1308,6 +1312,7 @@ class core_badges_badgeslib_testcase extends advanced_testcase { 'apiversion' => '2', 'backpackapiurl' => 'https://api.ca.badgr.io/v2', 'backpackweburl' => 'https://ca.badgr.io', + 'sortorder' => '2', ]; if ($withauth) { $data = array_merge($data, [ @@ -1317,7 +1322,13 @@ class core_badges_badgeslib_testcase extends advanced_testcase { } $backpack = badges_save_external_backpack((object) $data); - set_config('badges_site_backpack', $backpack); + // Check the backpack created is not the primary one. + $sitebackpack = badges_get_site_primary_backpack(); + $this->assertNotEquals($backpack, $sitebackpack->id); + + // Move backpack to the first position to set it as primary site backpack. + $this->move_backpack_to_first_position($backpack); + $sitebackpack = badges_get_site_primary_backpack(); $this->assertEquals($backpack, $sitebackpack->id); @@ -1512,4 +1523,17 @@ class core_badges_badgeslib_testcase extends advanced_testcase { ] ]; } + + /** + * Move the backpack to the first position, to set it as primary site backpack. + * + * @param int $backpackid The backpack identifier. + */ + private function move_backpack_to_first_position(int $backpackid): void { + $backpack = badges_get_site_backpack($backpackid); + while ($backpack->sortorder > 1) { + badges_change_sortorder_backpacks($backpackid, BACKPACK_MOVE_UP); + $backpack = badges_get_site_backpack($backpackid); + } + } } diff --git a/badges/tests/behat/backpack.feature b/badges/tests/behat/backpack.feature index 944c81f5050..b75d0cc0e91 100644 --- a/badges/tests/behat/backpack.feature +++ b/badges/tests/behat/backpack.feature @@ -43,10 +43,8 @@ Feature: Backpack badges And I set the following fields to these values: | External backpack connection | 1 | And I press "Save changes" - And I navigate to "Badges > Backpack settings" in site administration - And I set the following fields to these values: - | Active external backpack | https://dc.imsglobal.org | - And I press "Save changes" + And I navigate to "Badges > Manage backpacks" in site administration + And I click on "Move up" "link" in the "https://dc.imsglobal.org" "table_row" And I navigate to "Badges > Add a new badge" in site administration And I set the following fields to these values: | Name | Test badge verify backpack | @@ -82,10 +80,8 @@ Feature: Backpack badges And I set the following fields to these values: | External backpack connection | 1 | And I press "Save changes" - And I navigate to "Badges > Backpack settings" in site administration - And I set the following fields to these values: - | Active external backpack | https://dc.imsglobal.org | - And I press "Save changes" + And I navigate to "Badges > Manage backpacks" in site administration + And I click on "Move up" "link" in the "https://dc.imsglobal.org" "table_row" And I navigate to "Badges > Add a new badge" in site administration And I set the following fields to these values: | Name | Test badge verify backpack | diff --git a/badges/upgrade.txt b/badges/upgrade.txt index 2ca56f3e77c..8ee4fb70b8e 100644 --- a/badges/upgrade.txt +++ b/badges/upgrade.txt @@ -1,6 +1,10 @@ This files describes API changes in /badges/*, information provided here is intended especially for developers. +=== 3.11 === +* $CFG->badges_site_backpack setting has been completely removed because it's not required anymore. From now, the primary +site backpack will be the first one in the "Manage backpacks" list (so, the one with lower sortorder value). + === 3.10 === * Users can now specify a backpack that differs from the site backpack. In order to do this, connection details need to be set in 'Manage backpacks' with OR without auth details. diff --git a/badges/upgradelib.php b/badges/upgradelib.php index 7ce5b001566..61b97ca7696 100644 --- a/badges/upgradelib.php +++ b/badges/upgradelib.php @@ -47,9 +47,6 @@ function badges_install_default_backpacks() { $bpid = $DB->insert_record('badge_external_backpack', $record); } - set_config('badges_site_backpack', $bpid); - // Set external backpack to v2. $DB->set_field('badge_backpack', 'externalbackpackid', $bpid); } - diff --git a/lang/en/badges.php b/lang/en/badges.php index ccdfd367f88..9276ef7b814 100644 --- a/lang/en/badges.php +++ b/lang/en/badges.php @@ -514,8 +514,6 @@ $string['selectgroup_end'] = 'Only public collections are shown. $string['selectgroup_start'] = 'Select collections from your backpack to display on this site:'; $string['selecting'] = 'With selected badges...'; $string['setup'] = 'Set up connection'; -$string['sitebackpack'] = 'Active external backpack'; -$string['sitebackpack_help'] = 'The external backpack that users can connect to from this site. Note that changing this setting after users have connected their backpacks will require each user to go to their backpack settings page and disconnect then reconnect.'; $string['sitebackpackdeleted'] = 'The site backpack has been deleted.'; $string['sitebackpacknotdeleted'] = 'This backpack couldn\'t be deleted because it\'s currently the site default.'; $string['sitebackpackwarning'] = 'Could not connect to backpack.

Check that the "Badge issuer email address" admin setting is the valid email for an account on the backpack website.

Check that the "Badge issuer password" on the
site backpack settings page, is the correct password for the account on the backpack website.

The backpack returned: "{$a->warning}"'; @@ -579,3 +577,6 @@ $string['sitebackpackverify'] = 'Backpack connection'; // Deprecated since Moodle 3.10. $string['backpackneedsupdate'] = 'The backpack connected to this profile does not match the backpack for the site. You need to disconnect and reconnect the backpack.'; +// Deprecated since Moodle 3.11. +$string['sitebackpack'] = 'Active external backpack'; +$string['sitebackpack_help'] = 'The external backpack that users can connect to from this site. Note that changing this setting after users have connected their backpacks will require each user to go to their backpack settings page and disconnect then reconnect.'; diff --git a/lang/en/deprecated.txt b/lang/en/deprecated.txt index 79f960dcdef..415402af298 100644 --- a/lang/en/deprecated.txt +++ b/lang/en/deprecated.txt @@ -127,3 +127,5 @@ modeloutputdirinfo,core_analytics backpackneedsupdate,core_badges hidepicture,core_group hidepicture,core_moodle +sitebackpack,core_badges +sitebackpack_help,core_badges diff --git a/lib/badgeslib.php b/lib/badgeslib.php index c88f9ea29dd..ec7b60c2042 100644 --- a/lib/badgeslib.php +++ b/lib/badgeslib.php @@ -808,13 +808,14 @@ function badges_update_site_backpack($id, $data) { * @return boolean */ function badges_delete_site_backpack($id) { - global $DB, $CFG; + global $DB; $context = context_system::instance(); require_capability('moodle/badges:manageglobalsettings', $context); // Only remove site backpack if it's not the default one. - if ($CFG->badges_site_backpack != $id && $DB->record_exists('badge_external_backpack', ['id' => $id])) { + $defaultbackpack = badges_get_site_primary_backpack(); + if ($defaultbackpack->id != $id && $DB->record_exists('badge_external_backpack', ['id' => $id])) { $transaction = $DB->start_delegated_transaction(); // Remove connections for users to this backpack. @@ -917,11 +918,11 @@ function badges_save_backpack_credentials(stdClass $data) { */ function badges_open_badges_backpack_api(?int $backpackid = null) { if (!$backpackid) { - global $CFG; - $backpackid = $CFG->badges_site_backpack; + $backpack = badges_get_site_primary_backpack(); + } else { + $backpack = badges_get_site_backpack($backpackid); } - $backpack = badges_get_site_backpack($backpackid); if (empty($backpack->apiversion)) { return OPEN_BADGES_V2; } @@ -974,9 +975,15 @@ function badges_get_user_backpack(?int $userid = 0) { * @return array(stdClass) */ function badges_get_site_primary_backpack() { - global $CFG; + global $DB; - return badges_get_site_backpack($CFG->badges_site_backpack); + $sql = 'SELECT * + FROM {badge_external_backpack} + WHERE sortorder = (SELECT MIN(sortorder) + FROM {badge_external_backpack} b2)'; + $firstbackpack = $DB->get_record_sql($sql, null, MUST_EXIST); + + return badges_get_site_backpack($firstbackpack->id); } /** @@ -985,17 +992,18 @@ function badges_get_site_primary_backpack() { * @return array(stdClass) */ function badges_get_site_backpacks() { - global $DB, $CFG; + global $DB; + $defaultbackpack = badges_get_site_primary_backpack(); $all = $DB->get_records('badge_external_backpack', null, 'sortorder ASC'); - foreach ($all as $key => $bp) { - if ($bp->id == $CFG->badges_site_backpack) { + if ($bp->id == $defaultbackpack->id) { $all[$key]->sitebackpack = true; } else { $all[$key]->sitebackpack = false; } } + return $all; } @@ -1333,9 +1341,8 @@ function badge_assemble_notification(stdClass $badge) { * @return string */ function badges_verify_site_backpack() { - global $CFG; - - return badges_verify_backpack($CFG->badges_site_backpack); + $defaultbackpack = badges_get_site_primary_backpack(); + return badges_verify_backpack($defaultbackpack->id); } /** diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 9e49c7f8be1..c41b5bfd67c 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2483,5 +2483,13 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2021052500.65); } + if ($oldversion < 2021052500.67) { + // The $CFG->badges_site_backpack setting has been removed because it's not required anymore. From now, the default backpack + // will be the one with lower sortorder value. + unset_config('badges_site_backpack'); + + upgrade_main_savepoint(true, 2021052500.67); + } + return true; } diff --git a/version.php b/version.php index 48ccc5c2da4..5aeb022f53c 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2021052500.66; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2021052500.67; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. $release = '4.0dev (Build: 20210305)'; // Human-friendly version name