|
25 | 25 | require_once(__DIR__ . '/../config.php');
|
26 | 26 | require_once($CFG->libdir . '/badgeslib.php');
|
27 | 27 |
|
| 28 | +use core_badges\backpack; |
28 | 29 | use core_badges\local\backpack\ob\api_base;
|
29 | 30 |
|
30 | 31 | require_login();
|
31 | 32 |
|
32 |
| -$userbackpack = badges_get_user_backpack(); |
33 |
| -if (badges_open_badges_backpack_api($userbackpack->id) != OPEN_BADGES_V2) { |
34 |
| - throw new coding_exception('No backpacks support Open Badges V2.'); |
| 33 | +// Check if badges and the external backpack are enabled. |
| 34 | +if (empty($CFG->badges_allowexternalbackpack) || empty($CFG->enablebadges)) { |
| 35 | + redirect($CFG->wwwroot); |
35 | 36 | }
|
36 | 37 |
|
37 |
| -$id = required_param('hash', PARAM_ALPHANUM); |
38 |
| - |
39 |
| -$PAGE->set_url('/badges/backpack-add.php', array('hash' => $id)); |
40 |
| -$PAGE->set_context(context_system::instance()); |
41 |
| -$output = $PAGE->get_renderer('core', 'badges'); |
42 |
| - |
43 |
| -$issuedbadge = new \core_badges\output\issued_badge($id); |
44 |
| -if (!empty($issuedbadge->recipient->id)) { |
45 |
| - // The flow for issuing a badge is: |
46 |
| - // * Create issuer |
47 |
| - // * Create badge |
48 |
| - // * Create assertion (Award the badge!) |
49 |
| - |
50 |
| - // With the introduction OBv2.1 and MDL-65959 to allow cross region Badgr imports the above (old) procedure will |
51 |
| - // only be completely performed if both the site and user backpacks conform to the same apiversion. |
52 |
| - // Else we will attempt at pushing the assertion to the user's backpack. In this case, the id set against the assertion |
53 |
| - // has to be a publicly accessible resource. |
54 |
| - |
55 |
| - // Get the backpack. |
56 |
| - $badgeid = $issuedbadge->badgeid; |
57 |
| - $badge = new badge($badgeid); |
58 |
| - $backpack = $DB->get_record('badge_backpack', array('userid' => $USER->id)); |
59 |
| - $userbackpack = badges_get_site_backpack($backpack->externalbackpackid, $USER->id); |
60 |
| - $assertion = new core_badges_assertion($id, OPEN_BADGES_V2); |
61 |
| - $assertiondata = $assertion->get_badge_assertion(false, false); |
62 |
| - $assertionid = $assertion->get_assertion_hash(); |
63 |
| - $assertionentityid = $assertiondata['id']; |
64 |
| - $badgeadded = false; |
65 |
| - if (badges_open_badges_backpack_api() == OPEN_BADGES_V2) { |
66 |
| - $sitebackpack = badges_get_site_primary_backpack(); |
67 |
| - $api = api_base::create_from_externalbackpack($userbackpack); |
68 |
| - $response = $api->authenticate(); |
69 |
| - |
70 |
| - // A numeric response indicates a valid successful authentication. Else an error object will be returned. |
71 |
| - if (is_numeric($response)) { |
72 |
| - // Create issuer. |
73 |
| - $issuer = $assertion->get_issuer(); |
74 |
| - if (!($issuerentityid = badges_external_get_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ISSUER, $issuer['email']))) { |
75 |
| - $response = $api->put_issuer($issuer); |
76 |
| - if (!$response) { |
77 |
| - throw new moodle_exception('invalidrequest', 'error'); |
78 |
| - } |
79 |
| - $issuerentityid = $response->id; |
80 |
| - badges_external_create_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ISSUER, $issuer['email'], |
81 |
| - $issuerentityid); |
82 |
| - } |
83 |
| - // Create badge. |
84 |
| - $badge = $assertion->get_badge_class(false); |
85 |
| - $badgeid = $assertion->get_badge_id(); |
86 |
| - if (!($badgeentityid = badges_external_get_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_BADGE, $badgeid))) { |
87 |
| - $response = $api->put_badgeclass($issuerentityid, $badge); |
88 |
| - if (!$response) { |
89 |
| - throw new moodle_exception('invalidrequest', 'error'); |
90 |
| - } |
91 |
| - $badgeentityid = $response->id; |
92 |
| - badges_external_create_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_BADGE, $badgeid, |
93 |
| - $badgeentityid); |
94 |
| - } |
| 38 | +// Check the user has a backpack. |
| 39 | +$backpack = backpack::get_user_backpack(); |
| 40 | +if (empty($backpack)) { |
| 41 | + throw new coding_exception('This user has no backpack associated with their account.'); |
| 42 | +} |
95 | 43 |
|
96 |
| - // Create assertion (Award the badge!). |
97 |
| - $assertionentityid = badges_external_get_mapping( |
98 |
| - $sitebackpack->id, |
99 |
| - OPEN_BADGES_V2_TYPE_ASSERTION, |
100 |
| - $assertionid |
101 |
| - ); |
| 44 | +$hash = required_param('hash', PARAM_ALPHANUM); |
102 | 45 |
|
103 |
| - if ($assertionentityid && strpos($sitebackpack->backpackapiurl, 'badgr')) { |
104 |
| - $assertionentityid = badges_generate_badgr_open_url( |
105 |
| - $sitebackpack, |
106 |
| - OPEN_BADGES_V2_TYPE_ASSERTION, |
107 |
| - $assertionentityid |
108 |
| - ); |
109 |
| - } |
| 46 | +$PAGE->set_url('/badges/backpack-add.php', ['hash' => $hash]); |
| 47 | +$PAGE->set_context(context_user::instance($USER->id)); |
| 48 | +$output = $PAGE->get_renderer('core', 'badges'); |
110 | 49 |
|
111 |
| - // Create an assertion for the recipient in the issuer's account. |
112 |
| - if (!$assertionentityid) { |
113 |
| - $response = $api->put_badgeclass_assertion($badgeentityid, $assertiondata); |
114 |
| - if (!$response) { |
115 |
| - throw new moodle_exception('invalidrequest', 'error'); |
116 |
| - } |
117 |
| - $assertionentityid = badges_generate_badgr_open_url($sitebackpack, OPEN_BADGES_V2_TYPE_ASSERTION, $response->id); |
118 |
| - $badgeadded = true; |
119 |
| - badges_external_create_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ASSERTION, $assertionid, |
120 |
| - $response->id); |
121 |
| - } else { |
122 |
| - // An assertion already exists. Make sure it's up to date. |
123 |
| - $internalid = badges_external_get_mapping( |
124 |
| - $sitebackpack->id, |
125 |
| - OPEN_BADGES_V2_TYPE_ASSERTION, |
126 |
| - $assertionid, |
127 |
| - 'externalid' |
128 |
| - ); |
129 |
| - $response = $api->update_assertion($internalid, $assertiondata); |
130 |
| - if (!$response) { |
131 |
| - throw new moodle_exception('invalidrequest', 'error'); |
132 |
| - } |
133 |
| - } |
134 |
| - } |
135 |
| - } |
| 50 | +// Check the assertion belongs to the current user. |
| 51 | +$assertion = new core_badges_assertion($hash, $backpack->apiversion); |
| 52 | +if ($assertion->get_userid() != $USER->id) { |
| 53 | + throw new coding_exception('This assertion does not belong to the current user.'); |
| 54 | +} |
136 | 55 |
|
137 |
| - // Now award/upload the badge to the user's account. |
138 |
| - // - If a user and site backpack have the same provider we can skip this as Badgr automatically maps recipients |
139 |
| - // based on email address. |
140 |
| - // - This is only needed when the backpacks are from different regions. |
141 |
| - if ($assertionentityid && !badges_external_get_mapping($userbackpack->id, OPEN_BADGES_V2_TYPE_ASSERTION, $assertionid)) { |
142 |
| - $userapi = api_base::create_from_externalbackpack($userbackpack); |
143 |
| - $userapi->authenticate(); |
144 |
| - $response = $userapi->import_badge_assertion($assertionentityid); |
145 |
| - if (!$response) { |
146 |
| - throw new moodle_exception('invalidrequest', 'error'); |
147 |
| - } |
148 |
| - $assertionentityid = $response->id; |
149 |
| - $badgeadded = true; |
150 |
| - badges_external_create_mapping($userbackpack->id, OPEN_BADGES_V2_TYPE_ASSERTION, $assertionid, |
151 |
| - $assertionentityid); |
152 |
| - } |
| 56 | +// Send the assertion to the backpack. |
| 57 | +$api = api_base::create_from_externalbackpack($backpack); |
| 58 | +$notify = $api->put_assertions($hash); |
153 | 59 |
|
154 |
| - $response = $badgeadded ? ['success' => 'addedtobackpack'] : ['warning' => 'existsinbackpack']; |
155 |
| - redirect(new moodle_url('/badges/mybadges.php', $response)); |
156 |
| -} else { |
157 |
| - redirect(new moodle_url('/badges/mybadges.php')); |
| 60 | +$redirecturl = new moodle_url('/badges/mybadges.php'); |
| 61 | +if (!empty($notify['status'])) { |
| 62 | + redirect($redirecturl, $notify['message'], null, $notify['status']); |
158 | 63 | }
|
| 64 | +redirect($redirecturl); |
0 commit comments