Skip to content

Commit

Permalink
MDL-70911 core_badges: Remove $CFG->badges_site_backpack
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
sarjona committed Mar 10, 2021
1 parent 066e998 commit 49c1d41
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 56 deletions.
19 changes: 0 additions & 19 deletions admin/settings/badges.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,6 @@
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));
Expand Down Expand Up @@ -101,15 +91,6 @@
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',
Expand Down
3 changes: 2 additions & 1 deletion badges/classes/form/backpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public function definition() {
}
$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);

Expand Down
3 changes: 1 addition & 2 deletions badges/classes/output/external_backpacks_page.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function __construct(\moodle_url $url) {
* @return stdClass
*/
public function export_for_template(\renderer_base $output) {
global $CFG, $PAGE;
global $PAGE;

$PAGE->requires->js_call_amd('core_badges/backpackactions', 'init');

Expand All @@ -71,7 +71,6 @@ public function export_for_template(\renderer_base $output) {
$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;

Expand Down
6 changes: 2 additions & 4 deletions badges/templates/external_backpacks_page.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
<thead>
<tr>
<th scope="col">{{#str}}backpackweburl, core_badges{{/str}}</th>
<th scope="col">{{#str}}sitebackpack, core_badges{{/str}}</th>
<th scope="col">{{#str}}order{{/str}}</th>
<th scope="col">{{#str}}actions, core_badges{{/str}}</th>
</tr>
Expand All @@ -55,7 +54,6 @@
{{#backpacks}}
<tr data-backpackurl="{{{backpackweburl}}}">
<td> {{{backpackweburl}}} </td>
<td> {{#sitebackpack}}Yes{{/sitebackpack}} </td>
<td>
{{#canmoveup}}
<a href="{{baseurl}}?id={{id}}&action=moveup">{{#pix}}t/up, core,{{#str}}moveup{{/str}}{{/pix}}</a>
Expand All @@ -72,11 +70,11 @@
</td>
<td>
<a href="{{baseurl}}?id={{id}}&action=edit">{{#pix}}t/edit, core,{{#str}}editsettings{{/str}}{{/pix}}</a>
{{^iscurrent}}
{{^sitebackpack}}
<a href="{{baseurl}}?id={{id}}&action=delete" role="button" data-action="deletebackpack">
{{#pix}}t/delete, core,{{#str}}delete{{/str}}{{/pix}}
</a>
{{/iscurrent}}
{{/sitebackpack}}
{{#cantest}}
<a href="{{baseurl}}?id={{id}}&action=test">{{#pix}}t/check, core,{{#str}}testsettings, core_badges{{/str}}{{/pix}}</a>
{{/cantest}}
Expand Down
30 changes: 27 additions & 3 deletions badges/tests/badgeslib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,7 @@ public function test_badges_open_badges_backpack_api() {
'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.
Expand All @@ -1204,10 +1205,13 @@ public function test_badges_open_badges_backpack_api() {
$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.
Expand Down Expand Up @@ -1308,6 +1312,7 @@ public function test_badges_get_site_primary_backpack($withauth) {
'apiversion' => '2',
'backpackapiurl' => 'https://api.ca.badgr.io/v2',
'backpackweburl' => 'https://ca.badgr.io',
'sortorder' => '2',
];
if ($withauth) {
$data = array_merge($data, [
Expand All @@ -1317,7 +1322,13 @@ public function test_badges_get_site_primary_backpack($withauth) {
}
$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);

Expand Down Expand Up @@ -1512,4 +1523,17 @@ public function badges_external_get_mapping_provider() {
]
];
}

/**
* 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);
}
}
}
12 changes: 4 additions & 8 deletions badges/tests/behat/backpack.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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 |
Expand Down
4 changes: 4 additions & 0 deletions badges/upgrade.txt
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
3 changes: 0 additions & 3 deletions badges/upgradelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

5 changes: 3 additions & 2 deletions lang/en/badges.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,6 @@
$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. <br/><br/>Check that the "Badge issuer email address" admin setting is the valid email for an account on the backpack website. <br/><br/>Check that the "Badge issuer password" on the <a href="{$a->url}">site backpack settings page</a>, is the correct password for the account on the backpack website. <br/><br/>The backpack returned: "{$a->warning}"';
Expand Down Expand Up @@ -579,3 +577,6 @@

// 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.';
2 changes: 2 additions & 0 deletions lang/en/deprecated.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,5 @@ modeloutputdirinfo,core_analytics
backpackneedsupdate,core_badges
hidepicture,core_group
hidepicture,core_moodle
sitebackpack,core_badges
sitebackpack_help,core_badges
33 changes: 20 additions & 13 deletions lib/badgeslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;

$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($CFG->badges_site_backpack);
return badges_get_site_backpack($firstbackpack->id);
}

/**
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 49c1d41

Please sign in to comment.