Skip to content

Commit

Permalink
MDL-65518 badges: Admin settings warning
Browse files Browse the repository at this point in the history
If the backpack is changed to Open Badges V2 backpack,
attempt the authenticate with the site settings and return a warning
if it fails.
  • Loading branch information
Damyon Wiese committed May 9, 2019
1 parent 5c159cb commit d48a52d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
7 changes: 7 additions & 0 deletions admin/settings/badges.php
Expand Up @@ -105,6 +105,13 @@
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',
Expand Down
1 change: 1 addition & 0 deletions badges/classes/output/external_backpacks_page.php
Expand Up @@ -70,6 +70,7 @@ public function export_for_template(\renderer_base $output) {
}
$data->backpacks[] = $backpack;
}
$data->warning = badges_verify_site_backpack();

return $data;
}
Expand Down
4 changes: 3 additions & 1 deletion badges/templates/external_backpacks_page.mustache
Expand Up @@ -26,7 +26,8 @@
{
"backpacks": [
{"backpackweburl": "http://localhost/", "sitebackpack": true, "canedit": false}
]
],
"warning": "<span class='text-warning'>Could not login</span>"
}
}}
<table class="generaltable fullwidth">
Expand Down Expand Up @@ -54,3 +55,4 @@
{{/backpacks}}
</tbody>
</table>
{{{warning}}}
4 changes: 3 additions & 1 deletion lang/en/badges.php
Expand Up @@ -503,7 +503,9 @@
$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. <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}"';
$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.
Expand Down
33 changes: 33 additions & 0 deletions lib/badgeslib.php
Expand Up @@ -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 '';
}

0 comments on commit d48a52d

Please sign in to comment.