| Línea 58... |
Línea 58... |
| 58 |
$assertion = new core_badges_assertion($id, OPEN_BADGES_V2);
|
58 |
$assertion = new core_badges_assertion($id, OPEN_BADGES_V2);
|
| 59 |
$assertiondata = $assertion->get_badge_assertion(false, false);
|
59 |
$assertiondata = $assertion->get_badge_assertion(false, false);
|
| 60 |
$assertionid = $assertion->get_assertion_hash();
|
60 |
$assertionid = $assertion->get_assertion_hash();
|
| 61 |
$assertionentityid = $assertiondata['id'];
|
61 |
$assertionentityid = $assertiondata['id'];
|
| 62 |
$badgeadded = false;
|
62 |
$badgeadded = false;
|
| - |
|
63 |
$issuerexists = false;
|
| 63 |
if (badges_open_badges_backpack_api() == OPEN_BADGES_V2) {
|
64 |
if (badges_open_badges_backpack_api() == OPEN_BADGES_V2) {
|
| 64 |
$sitebackpack = badges_get_site_primary_backpack();
|
65 |
$sitebackpack = badges_get_site_primary_backpack();
|
| 65 |
$api = new \core_badges\backpack_api($sitebackpack);
|
66 |
$api = new \core_badges\backpack_api($sitebackpack);
|
| 66 |
$response = $api->authenticate();
|
67 |
$response = $api->authenticate();
|
| Línea 69... |
Línea 70... |
| 69 |
if (is_numeric($response)) {
|
70 |
if (is_numeric($response)) {
|
| 70 |
// Create issuer.
|
71 |
// Create issuer.
|
| 71 |
$issuer = $assertion->get_issuer();
|
72 |
$issuer = $assertion->get_issuer();
|
| 72 |
if (!($issuerentityid = badges_external_get_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ISSUER, $issuer['email']))) {
|
73 |
if (!($issuerentityid = badges_external_get_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ISSUER, $issuer['email']))) {
|
| 73 |
$response = $api->put_issuer($issuer);
|
74 |
$response = $api->put_issuer($issuer);
|
| 74 |
if (!$response) {
|
75 |
if ($response) {
|
| - |
|
76 |
$issuerexists = true;
|
| - |
|
77 |
$issuerentityid = $response->id;
|
| 75 |
throw new moodle_exception('invalidrequest', 'error');
|
78 |
badges_external_create_mapping(
|
| - |
|
79 |
$sitebackpack->id,
|
| - |
|
80 |
OPEN_BADGES_V2_TYPE_ISSUER,
|
| - |
|
81 |
$issuer['email'],
|
| - |
|
82 |
$issuerentityid,
|
| - |
|
83 |
);
|
| 76 |
}
|
84 |
}
|
| 77 |
$issuerentityid = $response->id;
|
- |
|
| 78 |
badges_external_create_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ISSUER, $issuer['email'],
|
- |
|
| 79 |
$issuerentityid);
|
- |
|
| 80 |
}
|
85 |
}
|
| - |
|
86 |
if ($issuerexists) {
|
| 81 |
// Create badge.
|
87 |
// Create badge.
|
| 82 |
$badge = $assertion->get_badge_class(false);
|
88 |
$badge = $assertion->get_badge_class(false);
|
| 83 |
$badgeid = $assertion->get_badge_id();
|
89 |
$badgeid = $assertion->get_badge_id();
|
| 84 |
if (!($badgeentityid = badges_external_get_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_BADGE, $badgeid))) {
|
90 |
if (!($badgeentityid = badges_external_get_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_BADGE, $badgeid))) {
|
| 85 |
$response = $api->put_badgeclass($issuerentityid, $badge);
|
91 |
$response = $api->put_badgeclass($issuerentityid, $badge);
|
| 86 |
if (!$response) {
|
92 |
if ($response) {
|
| - |
|
93 |
$badgeentityid = $response->id;
|
| 87 |
throw new moodle_exception('invalidrequest', 'error');
|
94 |
badges_external_create_mapping(
|
| - |
|
95 |
$sitebackpack->id,
|
| - |
|
96 |
OPEN_BADGES_V2_TYPE_BADGE,
|
| - |
|
97 |
$badgeid,
|
| - |
|
98 |
$badgeentityid,
|
| - |
|
99 |
);
|
| - |
|
100 |
}
|
| 88 |
}
|
101 |
}
|
| 89 |
$badgeentityid = $response->id;
|
- |
|
| 90 |
badges_external_create_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_BADGE, $badgeid,
|
- |
|
| 91 |
$badgeentityid);
|
- |
|
| 92 |
}
|
- |
|
| Línea 93... |
Línea 102... |
| 93 |
|
102 |
|
| 94 |
// Create assertion (Award the badge!).
|
103 |
// Create assertion (Award the badge!).
|
| 95 |
$assertionentityid = badges_external_get_mapping(
|
- |
|
| 96 |
$sitebackpack->id,
|
- |
|
| 97 |
OPEN_BADGES_V2_TYPE_ASSERTION,
|
- |
|
| 98 |
$assertionid
|
- |
|
| 99 |
);
|
- |
|
| 100 |
|
- |
|
| 101 |
if ($assertionentityid && strpos($sitebackpack->backpackapiurl, 'badgr')) {
|
- |
|
| 102 |
$assertionentityid = badges_generate_badgr_open_url(
|
104 |
$assertionentityid = badges_external_get_mapping(
|
| 103 |
$sitebackpack,
|
105 |
$sitebackpack->id,
|
| 104 |
OPEN_BADGES_V2_TYPE_ASSERTION,
|
106 |
OPEN_BADGES_V2_TYPE_ASSERTION,
|
| 105 |
$assertionentityid
|
107 |
$assertionid
|
| 106 |
);
|
- |
|
| Línea -... |
Línea 108... |
| - |
|
108 |
);
|
| 107 |
}
|
109 |
|
| 108 |
|
110 |
if ($assertionentityid && strpos($sitebackpack->backpackapiurl, 'badgr')) {
|
| 109 |
// Create an assertion for the recipient in the issuer's account.
|
111 |
$assertionentityid = badges_generate_badgr_open_url(
|
| 110 |
if (!$assertionentityid) {
|
112 |
$sitebackpack,
|
| 111 |
$response = $api->put_badgeclass_assertion($badgeentityid, $assertiondata);
|
113 |
OPEN_BADGES_V2_TYPE_ASSERTION,
|
| 112 |
if (!$response) {
|
114 |
$assertionentityid
|
| - |
|
115 |
);
|
| - |
|
116 |
}
|
| - |
|
117 |
|
| - |
|
118 |
// Create an assertion for the recipient in the issuer's account.
|
| - |
|
119 |
if (!$assertionentityid) {
|
| 113 |
throw new moodle_exception('invalidrequest', 'error');
|
120 |
$response = $api->put_badgeclass_assertion($badgeentityid, $assertiondata);
|
| - |
|
121 |
if ($response) {
|
| - |
|
122 |
$assertionentityid = badges_generate_badgr_open_url(
|
| - |
|
123 |
$sitebackpack,
|
| - |
|
124 |
OPEN_BADGES_V2_TYPE_ASSERTION,
|
| 114 |
}
|
125 |
$response->id,
|
| - |
|
126 |
);
|
| - |
|
127 |
$badgeadded = true;
|
| 115 |
$assertionentityid = badges_generate_badgr_open_url($sitebackpack, OPEN_BADGES_V2_TYPE_ASSERTION, $response->id);
|
128 |
badges_external_create_mapping(
|
| - |
|
129 |
$sitebackpack->id,
|
| 116 |
$badgeadded = true;
|
130 |
OPEN_BADGES_V2_TYPE_ASSERTION,
|
| - |
|
131 |
$assertionid,
|
| - |
|
132 |
$response->id,
|
| 117 |
badges_external_create_mapping($sitebackpack->id, OPEN_BADGES_V2_TYPE_ASSERTION, $assertionid,
|
133 |
);
|
| 118 |
$response->id);
|
134 |
}
|
| 119 |
} else {
|
135 |
} else {
|
| 120 |
// An assertion already exists. Make sure it's up to date.
|
136 |
// An assertion already exists. Make sure it's up to date.
|
| 121 |
$internalid = badges_external_get_mapping(
|
137 |
$internalid = badges_external_get_mapping(
|
| 122 |
$sitebackpack->id,
|
138 |
$sitebackpack->id,
|
| 123 |
OPEN_BADGES_V2_TYPE_ASSERTION,
|
139 |
OPEN_BADGES_V2_TYPE_ASSERTION,
|
| 124 |
$assertionid,
|
140 |
$assertionid,
|
| 125 |
'externalid'
|
141 |
'externalid'
|
| 126 |
);
|
- |
|
| 127 |
$response = $api->update_assertion($internalid, $assertiondata);
|
- |
|
| 128 |
if (!$response) {
|
142 |
);
|
| 129 |
throw new moodle_exception('invalidrequest', 'error');
|
143 |
$response = $api->update_assertion($internalid, $assertiondata);
|
| 130 |
}
|
144 |
}
|
| 131 |
}
|
145 |
}
|
| Línea 132... |
Línea 146... |
| 132 |
}
|
146 |
}
|
| 133 |
}
|
147 |
}
|
| 134 |
|
148 |
|
| 135 |
// Now award/upload the badge to the user's account.
|
149 |
// Now award/upload the badge to the user's account.
|
| - |
|
150 |
// - If a user and site backpack have the same provider we can skip this as Badgr automatically maps recipients
|
| - |
|
151 |
// based on email address.
|
| 136 |
// - If a user and site backpack have the same provider we can skip this as Badgr automatically maps recipients
|
152 |
// - This is only needed when the backpacks are from different regions.
|
| - |
|
153 |
if (
|
| 137 |
// based on email address.
|
154 |
$assertionentityid
|
| 138 |
// - This is only needed when the backpacks are from different regions.
|
155 |
&& (!$issuerexists || !badges_external_get_mapping($userbackpack->id, OPEN_BADGES_V2_TYPE_ASSERTION, $assertionid))
|
| 139 |
if ($assertionentityid && !badges_external_get_mapping($userbackpack->id, OPEN_BADGES_V2_TYPE_ASSERTION, $assertionid)) {
|
156 |
) {
|
| 140 |
$userapi = new \core_badges\backpack_api($userbackpack, $backpack);
|
157 |
$userapi = new \core_badges\backpack_api($userbackpack, $backpack);
|
| - |
|
158 |
$userapi->authenticate();
|
| - |
|
159 |
$response = $userapi->import_badge_assertion($assertionentityid);
|
| - |
|
160 |
if ($response) {
|
| - |
|
161 |
$assertionentityid = $response->id;
|
| - |
|
162 |
$badgeadded = true;
|
| - |
|
163 |
badges_external_create_mapping(
|
| - |
|
164 |
$userbackpack->id,
|
| - |
|
165 |
OPEN_BADGES_V2_TYPE_ASSERTION,
|
| - |
|
166 |
$assertionid,
|
| - |
|
167 |
$assertionentityid,
|
| - |
|
168 |
);
|
| - |
|
169 |
}
|
| - |
|
170 |
}
|
| - |
|
171 |
|
| - |
|
172 |
if ($badgeadded) {
|
| - |
|
173 |
$message = get_string('addedtobackpack', 'badges');
|
| - |
|
174 |
$messagetype = \core\output\notification::NOTIFY_SUCCESS;
|
| - |
|
175 |
} else {
|
| - |
|
176 |
if (isset($userapi) && !empty($userapi->get_errors())) {
|
| - |
|
177 |
// If the api used to import the badge to the backpack has errors, show them to inform the user.
|
| - |
|
178 |
if (array_filter($userapi->get_errors(), fn($element) => str_contains($element, "DUPLICATE_BADGE"))) {
|
| - |
|
179 |
// Duplicated badges are displayed as a warning.
|
| - |
|
180 |
$message = get_string('existsinbackpack', 'badges');
|
| - |
|
181 |
$messagetype = \core\output\notification::NOTIFY_WARNING;
|
| - |
|
182 |
} else {
|
| - |
|
183 |
// If the userapi has any other errors, we will use those to inform the user.
|
| - |
|
184 |
$message = get_string(
|
| - |
|
185 |
'error:cannotsendtobackpack',
|
| - |
|
186 |
'badges',
|
| - |
|
187 |
implode($userapi->get_errors()),
|
| - |
|
188 |
);
|
| - |
|
189 |
$messagetype = \core\output\notification::NOTIFY_ERROR;
|
| 141 |
$userapi->authenticate();
|
190 |
}
|
| - |
|
191 |
} else if (isset($api) && !empty($api->get_errors())) {
|
| - |
|
192 |
// If the api used to create/update the issuer has errors, show them to inform the user.
|
| - |
|
193 |
$errors = $api->get_errors() ?? [get_string('invalidrequest', 'error')];
|
| - |
|
194 |
$message = get_string(
|
| - |
|
195 |
'error:cannotsendtobackpack',
|
| - |
|
196 |
'badges',
|
| 142 |
$response = $userapi->import_badge_assertion($assertionentityid);
|
197 |
implode($errors),
|
| 143 |
if (!$response) {
|
- |
|
| 144 |
throw new moodle_exception('invalidrequest', 'error');
|
- |
|
| 145 |
}
|
- |
|
| 146 |
$assertionentityid = $response->id;
|
- |
|
| 147 |
$badgeadded = true;
|
198 |
);
|
| Línea 148... |
Línea 199... |
| 148 |
badges_external_create_mapping($userbackpack->id, OPEN_BADGES_V2_TYPE_ASSERTION, $assertionid,
|
199 |
$messagetype = \core\output\notification::NOTIFY_ERROR;
|
| 149 |
$assertionentityid);
|
200 |
}
|
| - |
|
201 |
}
|
| - |
|
202 |
|
| - |
|
203 |
redirect(
|
| 150 |
}
|
204 |
url: new \core\url('/badges/mybadges.php'),
|
| 151 |
|
205 |
message: $message,
|
| 152 |
$response = $badgeadded ? ['success' => 'addedtobackpack'] : ['warning' => 'existsinbackpack'];
|
206 |
messagetype: $messagetype,
|