From 597564d7b4c8187c62b5f92602b8d8059bba2fc0 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Fri, 20 Jan 2023 15:18:23 +0100 Subject: [PATCH] MDL-77086 badges: Improve debugging Display more information, like the status error, when the badge can't be sent to the backpack. --- badges/classes/backpack_api2p1.php | 13 ++++++++++++- lang/en/badges.php | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/badges/classes/backpack_api2p1.php b/badges/classes/backpack_api2p1.php index 055a3c393b9eb..d00521a37fc76 100644 --- a/badges/classes/backpack_api2p1.php +++ b/badges/classes/backpack_api2p1.php @@ -228,8 +228,19 @@ public function put_assertions($hash) { $msg['status'] = \core\output\notification::NOTIFY_SUCCESS; $msg['message'] = get_string('addedtobackpack', 'badges'); } else { + $statuserror = $response->status->error; + if (is_array($statuserror)) { + // Although the specification defines that status error is a string, some providers, like Badgr, are wrongly + // returning an array. It has been reported, but adding this extra check doesn't hurt, just in case. + $statuserror = implode($statuserror); + } + $data = [ + 'badgename' => $data['assertion']['badge']['name'], + 'error' => $statuserror, + ]; + $msg['status'] = \core\output\notification::NOTIFY_ERROR; - $msg['message'] = get_string('backpackexporterror', 'badges', $data['assertion']['badge']['name']); + $msg['message'] = get_string('backpackexporterrorwithinfo', 'badges', $data); } return $msg; } diff --git a/lang/en/badges.php b/lang/en/badges.php index a9970f2935e9d..b078a063fd44c 100644 --- a/lang/en/badges.php +++ b/lang/en/badges.php @@ -129,6 +129,7 @@ $string['backpackemailverifysuccess'] = 'Thanks for verifying your email address. You are now connected to your backpack.'; $string['backpackemailverifytokenmismatch'] = 'The token in the link you clicked does not match the stored token. Make sure you clicked the link in most recent email you received.'; $string['backpackexporterror'] = 'Can\'t export the badge to backpack'; +$string['backpackexporterrorwithinfo'] = 'Can\'t export the badge "{$a->badgename}" to backpack. Error: {$a->error}'; $string['backpackimport'] = 'Badge import settings'; $string['backpackimport_help'] = 'After the backpack connection is successfully established, badges from your backpack can be displayed on your badges page and your profile page.