Skip to content

Commit

Permalink
MDL-69002 core_badges: add methods to support backpack validation
Browse files Browse the repository at this point in the history
A more generic method has been added to the API to validate the
backpack connection (for now, there was only one method for
validating current backpack).
Besides, a renderer has been added to display this information
depending on the backpackid.
  • Loading branch information
sarjona committed Jun 10, 2020
1 parent 24562c0 commit 3a6950c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
31 changes: 31 additions & 0 deletions badges/renderer.php
Expand Up @@ -1362,4 +1362,35 @@ public function render_external_backpacks_page(\core_badges\output\external_back
$data = $page->export_for_template($this);
return parent::render_from_template('core_badges/external_backpacks_page', $data);
}

/**
* Get the result of a backpack validation with its settings. It returns:
* - A informative message if the backpack version is different from OBv2.
* - A warning with the error if it's not possible to connect to this backpack.
* - A successful message if the connection has worked.
*
* @param int $backpackid The backpack identifier.
* @return string A message with the validation result.
*/
public function render_test_backpack_result(int $backpackid): string {
// Get the backpack.
$backpack = badges_get_site_backpack($backpackid);

// Add the header to the result.
$result = $this->heading(get_string('testbackpack', 'badges', $backpack->backpackweburl));

if ($backpack->apiversion != OPEN_BADGES_V2) {
// Only OBv2 supports this validation.
$result .= get_string('backpackconnectionnottested', 'badges');
} else {
$message = badges_verify_backpack($backpackid);
if (empty($message)) {
$result .= get_string('backpackconnectionok', 'badges');
} else {
$result .= $message;
}
}

return $result;
}
}
3 changes: 3 additions & 0 deletions lang/en/badges.php
Expand Up @@ -81,6 +81,8 @@
$string['awardoncron'] = 'Access to the badges was successfully enabled. Too many users can instantly earn this badge. To ensure site performance, this action will take some time to process.';
$string['awards'] = 'Recipients';
$string['backpackavailability'] = 'External badge verification';
$string['backpackconnectionok'] = 'Backpack connection successfully established';
$string['backpackconnectionnottested'] = 'Connection can not be tested for this backpack because only OBv2.0 backpacks support it.';
$string['backpackneedsupdate'] = 'The backpack connected to this profile does not match the backpack for the site. You need to disconnect and reconnect the backpack.';
$string['backpackavailability_help'] = 'For badge recipients to be able to prove they earned their badges from you, an external backpack service should be able to access your site and verify badges issued from it. Your site does not currently appear to be accessible, which means that badges you have already issued or will issue in the future cannot be verified.
Expand Down Expand Up @@ -536,6 +538,7 @@
$string['targetframework_help'] = 'The name of the external skill or standard framework.';
$string['targetcode'] = 'Code';
$string['targetcode_help'] = 'A unique string identifier for referencing the external skill or standard within its framework.';
$string['testbackpack'] = 'Test backpack \'{$a}\'';
$string['type'] = 'Type';
$string['variablesubstitution'] = 'Variable substitution in messages.';
$string['variablesubstitution_help'] = 'In a badge message, certain variables can be inserted into the subject and/or body of a message so that they will be replaced with real values when the message is sent. The variables should be inserted into the text exactly as they are shown below. The following variables can be used:
Expand Down
19 changes: 17 additions & 2 deletions lib/badgeslib.php
Expand Up @@ -1147,14 +1147,28 @@ function badge_assemble_notification(stdClass $badge) {
* @return string
*/
function badges_verify_site_backpack() {
global $CFG;

return badges_verify_backpack($CFG->badges_site_backpack);
}

/**
* Attempt to authenticate with a backpack credentials and return an error
* if the authentication fails.
* If external backpacks are not enabled or the backpack version is different
* from OBv2, this will not perform any test.
*
* @param int $backpackid Backpack identifier to verify.
* @return string The result of the verification process.
*/
function badges_verify_backpack(int $backpackid) {
global $OUTPUT, $CFG;

if (empty($CFG->badges_allowexternalbackpack)) {
return '';
}

$backpack = badges_get_site_backpack($CFG->badges_site_backpack);

$backpack = badges_get_site_backpack($backpackid);
if (empty($backpack->apiversion) || ($backpack->apiversion == OPEN_BADGES_V2)) {
$backpackapi = new \core_badges\backpack_api($backpack);

Expand All @@ -1174,5 +1188,6 @@ function badges_verify_site_backpack() {
return $OUTPUT->container($icon . $message, 'text-error');
}
}

return '';
}

0 comments on commit 3a6950c

Please sign in to comment.