2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Optionally award a badge and redirect to the my badges page.
20 * @package core_badges
21 * @copyright 2019 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once(__DIR__
. '/../config.php');
26 require_once($CFG->libdir
. '/badgeslib.php');
28 if (badges_open_badges_backpack_api() != OPEN_BADGES_V2
) {
29 throw new coding_exception('No backpacks support Open Badges V2.');
34 $id = required_param('hash', PARAM_ALPHANUM
);
36 $PAGE->set_url('/badges/backpack-add.php', array('hash' => $id));
37 $PAGE->set_context(context_system
::instance());
38 $output = $PAGE->get_renderer('core', 'badges');
40 $issuedbadge = new \core_badges\output\
issued_badge($id);
41 if (!empty($issuedbadge->recipient
->id
)) {
42 // The flow for issuing a badge is:
45 // * Create assertion (Award the badge!)
48 $badgeid = $issuedbadge->badgeid
;
49 $badge = new badge($badgeid);
50 $backpack = $DB->get_record('badge_backpack', array('userid' => $USER->id
));
51 $sitebackpack = badges_get_site_backpack($backpack->externalbackpackid
);
52 $assertion = new core_badges_assertion($id, $sitebackpack->apiversion
);
53 $api = new \core_badges\backpack_api
($sitebackpack);
57 $issuer = $assertion->get_issuer();
58 if (!($issuerentityid = badges_external_get_mapping($sitebackpack->id
, OPEN_BADGES_V2_TYPE_ISSUER
, $issuer['email']))) {
59 $response = $api->put_issuer($issuer);
61 throw new moodle_exception('invalidrequest', 'error');
63 $issuerentityid = $response->id
;
64 badges_external_create_mapping($sitebackpack->id
, OPEN_BADGES_V2_TYPE_ISSUER
, $issuer['email'], $issuerentityid);
67 $badge = $assertion->get_badge_class(false);
68 $badgeid = $assertion->get_badge_id();
69 if (!($badgeentityid = badges_external_get_mapping($sitebackpack->id
, OPEN_BADGES_V2_TYPE_BADGE
, $badgeid))) {
70 $response = $api->put_badgeclass($issuerentityid, $badge);
72 throw new moodle_exception('invalidrequest', 'error');
74 $badgeentityid = $response->id
;
75 badges_external_create_mapping($sitebackpack->id
, OPEN_BADGES_V2_TYPE_BADGE
, $badgeid, $badgeentityid);
78 // Create assertion (Award the badge!).
79 $assertiondata = $assertion->get_badge_assertion(false, false);
81 $assertionid = $assertion->get_assertion_hash();
83 if (!($assertionentityid = badges_external_get_mapping($sitebackpack->id
, OPEN_BADGES_V2_TYPE_ASSERTION
, $assertionid))) {
84 $response = $api->put_badgeclass_assertion($badgeentityid, $assertiondata);
86 throw new moodle_exception('invalidrequest', 'error');
88 $assertionentityid = $response->id
;
89 badges_external_create_mapping($sitebackpack->id
, OPEN_BADGES_V2_TYPE_ASSERTION
, $assertionid, $assertionentityid);
90 $response = ['success' => 'addedtobackpack'];
92 $response = ['warning' => 'existsinbackpack'];
94 redirect(new moodle_url('/badges/mybadges.php', $response));
96 redirect(new moodle_url('/badges/mybadges.php'));