diff --git a/admin/settings/badges.php b/admin/settings/badges.php index 24333ba7260..0ea51937c78 100644 --- a/admin/settings/badges.php +++ b/admin/settings/badges.php @@ -105,6 +105,13 @@ if (($hassiteconfig || has_any_capability(array( new lang_string('sitebackpack_help', 'badges'), 1, $choices)); + $warning = badges_verify_site_backpack(); + if (!empty($warning)) { + $backpacksettings->add(new admin_setting_description('badges_site_backpack_verify', + new lang_string('sitebackpackverify', 'badges'), + $warning)); + } + $ADMIN->add('badges', $backpacksettings); $ADMIN->add('badges', diff --git a/badges/classes/output/external_backpacks_page.php b/badges/classes/output/external_backpacks_page.php index 90086e4218e..aa9592fda20 100644 --- a/badges/classes/output/external_backpacks_page.php +++ b/badges/classes/output/external_backpacks_page.php @@ -70,6 +70,7 @@ class external_backpacks_page implements \renderable { } $data->backpacks[] = $backpack; } + $data->warning = badges_verify_site_backpack(); return $data; } diff --git a/badges/templates/external_backpacks_page.mustache b/badges/templates/external_backpacks_page.mustache index 5152c927497..2d84733279b 100644 --- a/badges/templates/external_backpacks_page.mustache +++ b/badges/templates/external_backpacks_page.mustache @@ -26,7 +26,8 @@ { "backpacks": [ {"backpackweburl": "http://localhost/", "sitebackpack": true, "canedit": false} - ] + ], + "warning": "Could not login" } }} @@ -54,3 +55,4 @@ {{/backpacks}}
+{{{warning}}} diff --git a/lang/en/badges.php b/lang/en/badges.php index 957c4d3ac2a..6d58937ba54 100644 --- a/lang/en/badges.php +++ b/lang/en/badges.php @@ -503,7 +503,9 @@ $string['selectgroup_start'] = 'Select collections from your backpack to display $string['selecting'] = 'With selected badges...'; $string['setup'] = 'Set up connection'; $string['sitebackpack'] = 'Active external backpack'; -$string['sitebackpack_help'] = 'An external backpack allows users to share their badges. Only one external backpack can be selected for the site. Changing this setting after users have connected their backpacks will require each user to disconnect and reconnect from their backpack settings page.'; +$string['sitebackpack_help'] = 'An external backpack allows users to share their badges. Only one external backpack can be active for the site. Changing this setting after users have connected their backpacks will require each user to disconnect and reconnect from their backpack settings page.'; +$string['sitebackpackverify'] = 'Backpack connection'; +$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}"'; $string['sitebadges'] = 'Site badges'; $string['sitebadges_help'] = 'Site badges can only be awarded to users for site-related activities. These include completing a set of courses or parts of user profiles. Site badges can also be issued manually by one user to another. diff --git a/lib/badgeslib.php b/lib/badgeslib.php index 811fb909390..cc7e3669e5b 100644 --- a/lib/badgeslib.php +++ b/lib/badgeslib.php @@ -1167,3 +1167,36 @@ function badge_assemble_notification(stdClass $badge) { message_send($eventdata); } } + +/** + * Attempt to authenticate with the site backpack credentials and return an error + * if the authentication fails. + * + * @return string + */ +function badges_verify_site_backpack() { + global $OUTPUT, $CFG; + + if (empty($CFG->badges_allowexternalbackpack)) { + return ''; + } + + $backpack = badges_get_site_backpack($CFG->badges_site_backpack); + + if (empty($backpack->apiversion) || ($backpack->apiversion == OPEN_BADGES_V2)) { + $backpackapi = new \core_badges\backpack_api($backpack); + + $result = $backpackapi->authenticate(); + if ($result === false || !empty($result->error)) { + $warning = $backpackapi->get_authentication_error(); + + $params = ['id' => $backpack->id, 'action' => 'edit']; + $backpackurl = (new moodle_url('/badges/backpacks.php', $params))->out(false); + + $message = get_string('sitebackpackwarning', 'badges', ['url' => $backpackurl, 'warning' => $warning]); + $icon = $OUTPUT->pix_icon('i/warning', get_string('warning', 'moodle')); + return $OUTPUT->container($icon . $message, 'text-error'); + } + } + return ''; +}