diff --git a/badges/classes/backpack_api2p1.php b/badges/classes/backpack_api2p1.php index fe85d9a1d39..f3829089252 100644 --- a/badges/classes/backpack_api2p1.php +++ b/badges/classes/backpack_api2p1.php @@ -272,8 +272,34 @@ class backpack_api2p1 { $msg['status'] = \core\output\notification::NOTIFY_SUCCESS; $msg['message'] = get_string('addedtobackpack', 'badges'); } else { + if ($response) { + // 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 these extra checks doesn't hurt, just in case. + if ( + property_exists($response, 'status') && + is_object($response->status) && + property_exists($response->status, 'error') + ) { + $statuserror = $response->status->error; + if (is_array($statuserror)) { + $statuserror = implode($statuserror); + } + } else if (property_exists($response, 'error')) { + $statuserror = $response->error; + if (property_exists($response, 'message')) { + $statuserror .= '. Message: ' . $response->message; + } + } + } else { + $statuserror = 'Empty response'; + } + $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 ef2d17ea0a2..b42b0484161 100644 --- a/lang/en/badges.php +++ b/lang/en/badges.php @@ -130,6 +130,7 @@ $string['backpackemailverifypending'] = 'A verification email has been sent to < $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.