Merge branch 'MDL-81472' of https://github.com/paulholden/moodle
[moodle.git] / badges / backpack-add.php
blob1ff7795ae7c5bbaabca3884b9a30636df83c07fb
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
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 require_login();
30 $userbackpack = badges_get_user_backpack();
31 if (badges_open_badges_backpack_api($userbackpack->id) != OPEN_BADGES_V2) {
32 throw new coding_exception('No backpacks support Open Badges V2.');
35 $id = required_param('hash', PARAM_ALPHANUM);
37 $PAGE->set_url('/badges/backpack-add.php', array('hash' => $id));
38 $PAGE->set_context(context_system::instance());
39 $output = $PAGE->get_renderer('core', 'badges');
41 $issuedbadge = new \core_badges\output\issued_badge($id);
42 if (!empty($issuedbadge->recipient->id)) {
43 // The flow for issuing a badge is:
44 // * Create issuer
45 // * Create badge
46 // * Create assertion (Award the badge!)
48 // With the introduction OBv2.1 and MDL-65959 to allow cross region Badgr imports the above (old) procedure will
49 // only be completely performed if both the site and user backpacks conform to the same apiversion.
50 // Else we will attempt at pushing the assertion to the user's backpack. In this case, the id set against the assertion
51 // has to be a publicly accessible resource.
53 // Get the backpack.
54 $badgeid = $issuedbadge->badgeid;
55 $badge = new badge($badgeid);
56 $backpack = $DB->get_record('badge_backpack', array('userid' => $USER->id));
57 $userbackpack = badges_get_site_backpack($backpack->externalbackpackid, $USER->id);
58 $assertion = new core_badges_assertion($id, OPEN_BADGES_V2);
59 $assertiondata = $assertion->get_badge_assertion(false, false);
60 $assertionid = $assertion->get_assertion_hash();
61 $assertionentityid = $assertiondata['id'];
62 $badgeadded = false;
63 if (badges_open_badges_backpack_api() == OPEN_BADGES_V2) {
64 $sitebackpack = badges_get_site_primary_backpack();
65 $api = new \core_badges\backpack_api($sitebackpack);
66 $response = $api->authenticate();
68 // A numeric response indicates a valid successful authentication. Else an error object will be returned.
69 if (is_numeric($response)) {
70 // Create issuer.
71 $issuer = $assertion->get_issuer();
72 if (!($issuerentityid = badges_external_get_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ISSUER, $issuer['email']))) {
73 $response = $api->put_issuer($issuer);
74 if (!$response) {
75 throw new moodle_exception('invalidrequest', 'error');
77 $issuerentityid = $response->id;
78 badges_external_create_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ISSUER, $issuer['email'],
79 $issuerentityid);
81 // Create badge.
82 $badge = $assertion->get_badge_class(false);
83 $badgeid = $assertion->get_badge_id();
84 if (!($badgeentityid = badges_external_get_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_BADGE, $badgeid))) {
85 $response = $api->put_badgeclass($issuerentityid, $badge);
86 if (!$response) {
87 throw new moodle_exception('invalidrequest', 'error');
89 $badgeentityid = $response->id;
90 badges_external_create_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_BADGE, $badgeid,
91 $badgeentityid);
94 // Create assertion (Award the badge!).
95 $assertionentityid = badges_external_get_mapping(
96 $sitebackpack->id,
97 OPEN_BADGES_V2_TYPE_ASSERTION,
98 $assertionid
101 if ($assertionentityid && strpos($sitebackpack->backpackapiurl, 'badgr')) {
102 $assertionentityid = badges_generate_badgr_open_url(
103 $sitebackpack,
104 OPEN_BADGES_V2_TYPE_ASSERTION,
105 $assertionentityid
109 // Create an assertion for the recipient in the issuer's account.
110 if (!$assertionentityid) {
111 $response = $api->put_badgeclass_assertion($badgeentityid, $assertiondata);
112 if (!$response) {
113 throw new moodle_exception('invalidrequest', 'error');
115 $assertionentityid = badges_generate_badgr_open_url($sitebackpack, OPEN_BADGES_V2_TYPE_ASSERTION, $response->id);
116 $badgeadded = true;
117 badges_external_create_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ASSERTION, $assertionid,
118 $response->id);
119 } else {
120 // An assertion already exists. Make sure it's up to date.
121 $internalid = badges_external_get_mapping(
122 $sitebackpack->id,
123 OPEN_BADGES_V2_TYPE_ASSERTION,
124 $assertionid,
125 'externalid'
127 $response = $api->update_assertion($internalid, $assertiondata);
128 if (!$response) {
129 throw new moodle_exception('invalidrequest', 'error');
135 // Now award/upload the badge to the user's account.
136 // - If a user and site backpack have the same provider we can skip this as Badgr automatically maps recipients
137 // based on email address.
138 // - This is only needed when the backpacks are from different regions.
139 if ($assertionentityid && !badges_external_get_mapping($userbackpack->id, OPEN_BADGES_V2_TYPE_ASSERTION, $assertionid)) {
140 $userapi = new \core_badges\backpack_api($userbackpack, $backpack);
141 $userapi->authenticate();
142 $response = $userapi->import_badge_assertion($assertionentityid);
143 if (!$response) {
144 throw new moodle_exception('invalidrequest', 'error');
146 $assertionentityid = $response->id;
147 $badgeadded = true;
148 badges_external_create_mapping($userbackpack->id, OPEN_BADGES_V2_TYPE_ASSERTION, $assertionid,
149 $assertionentityid);
152 $response = $badgeadded ? ['success' => 'addedtobackpack'] : ['warning' => 'existsinbackpack'];
153 redirect(new moodle_url('/badges/mybadges.php', $response));
154 } else {
155 redirect(new moodle_url('/badges/mybadges.php'));