16
16
17
17
defined ('MOODLE_INTERNAL ' ) || die ();
18
18
19
+ require_once ($ CFG ->libdir . '/badgeslib.php ' );
20
+ require_once ($ CFG ->dirroot . '/badges/renderer.php ' );
21
+
22
+ use core_badges \local \backpack \ob \exporter_base ;
23
+
19
24
/**
20
25
* Open Badges Assertions specification 2.0
21
26
* {@link https://www.imsglobal.org/sites/default/files/Badges/OBv2p0Final/index.html#Assertion}
24
29
* - Badge Assertion (information regarding a specific badge that was awarded to a badge earner)
25
30
* - Badge Class (general information about a badge and what it is intended to represent)
26
31
* - Issuer Class (general information of an issuing organisation)
27
- */
28
- require_once ($ CFG ->libdir . '/badgeslib.php ' );
29
- require_once ($ CFG ->dirroot . '/badges/renderer.php ' );
30
-
31
- /**
32
- * Class that represents badge assertion.
33
32
*
34
33
* @package core_badges
35
34
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
@@ -116,53 +115,11 @@ public function get_assertion_hash() {
116
115
* @return array Badge assertion.
117
116
*/
118
117
public function get_badge_assertion ($ issued = true , $ usesalt = true ) {
119
- global $ CFG ;
120
- $ assertion = array ();
121
- if ($ this ->_data ) {
122
- $ hash = $ this ->_data ->uniquehash ;
123
- $ email = empty ($ this ->_data ->backpackemail ) ? $ this ->_data ->email : $ this ->_data ->backpackemail ;
124
- $ assertionurl = new moodle_url ('/badges/assertion.php ' , array ('b ' => $ hash , 'obversion ' => $ this ->_obversion ));
125
-
126
- if ($ this ->_obversion >= OPEN_BADGES_V2 ) {
127
- $ classurl = new moodle_url ('/badges/badge_json.php ' , array ('id ' => $ this ->get_badge_id ()));
128
- } else {
129
- $ classurl = new moodle_url ('/badges/assertion.php ' , array ('b ' => $ hash , 'action ' => 1 ));
130
- }
131
-
132
- // Required.
133
- $ assertion ['uid ' ] = $ hash ;
134
- $ assertion ['recipient ' ] = array ();
135
- if ($ usesalt ) {
136
- $ assertion ['recipient ' ]['identity ' ] = 'sha256$ ' . hash ('sha256 ' , $ email . $ CFG ->badges_badgesalt );
137
- } else {
138
- $ assertion ['recipient ' ]['identity ' ] = $ email ;
139
- }
140
- $ assertion ['recipient ' ]['type ' ] = 'email ' ; // Currently the only supported type.
141
- $ assertion ['recipient ' ]['hashed ' ] = true ; // We are always hashing recipient.
142
- if ($ usesalt ) {
143
- $ assertion ['recipient ' ]['salt ' ] = $ CFG ->badges_badgesalt ;
144
- }
145
- if ($ issued ) {
146
- $ assertion ['badge ' ] = $ classurl ->out (false );
147
- }
148
- $ assertion ['verify ' ] = array ();
149
- $ assertion ['verify ' ]['type ' ] = 'hosted ' ; // 'Signed' is not implemented yet.
150
- $ assertion ['verify ' ]['url ' ] = $ assertionurl ->out (false );
151
- $ assertion ['issuedOn ' ] = $ this ->_data ->dateissued ;
152
- if ($ issued ) {
153
- $ assertion ['evidence ' ] = $ this ->_url ->out (false ); // Currently issued badge URL.
154
- }
155
- // Optional.
156
- if (!empty ($ this ->_data ->dateexpire )) {
157
- $ assertion ['expires ' ] = $ this ->_data ->dateexpire ;
158
- }
159
- $ tags = $ this ->get_tags ();
160
- if (is_array ($ tags ) && count ($ tags ) > 0 ) {
161
- $ assertion ['tags ' ] = $ tags ;
162
- }
163
- $ this ->embed_data_badge_version2 ($ assertion , OPEN_BADGES_V2_TYPE_ASSERTION );
164
- }
165
- return $ assertion ;
118
+ $ assertionexporter = exporter_base::create_assertion_exporter_from_hash (
119
+ $ this ->_data ->uniquehash ,
120
+ exporter_base::convert_apiversion (OPEN_BADGES_V2 ),
121
+ );
122
+ return $ assertionexporter ->export ();
166
123
}
167
124
168
125
/**
@@ -172,48 +129,13 @@ public function get_badge_assertion($issued = true, $usesalt = true) {
172
129
* @return array Badge Class information.
173
130
*/
174
131
public function get_badge_class ($ issued = true ) {
175
- $ class = [];
176
- if ($ this ->_data ) {
177
- if (empty ($ this ->_data ->courseid )) {
178
- $ context = context_system::instance ();
179
- } else {
180
- $ context = context_course::instance ($ this ->_data ->courseid );
181
- }
182
- // Required.
183
- $ class ['name ' ] = $ this ->_data ->name ;
184
- $ class ['description ' ] = $ this ->_data ->description ;
185
- $ storage = get_file_storage ();
186
- $ imagefile = $ storage ->get_file ($ context ->id , 'badges ' , 'badgeimage ' , $ this ->_data ->id , '/ ' , 'f3.png ' );
187
- if ($ imagefile ) {
188
- $ imagedata = base64_encode ($ imagefile ->get_content ());
189
- } else {
190
- if (defined ('PHPUNIT_TEST ' ) && PHPUNIT_TEST ) {
191
- // Unit tests the file might not exist yet.
192
- $ imagedata = '' ;
193
- } else {
194
- throw new coding_exception ('Image file does not exist. ' );
195
- }
196
- }
197
- $ class ['image ' ] = 'data:image/png;base64, ' . $ imagedata ;
198
-
199
- $ params = ['id ' => $ this ->get_badge_id ()];
200
- $ badgeurl = new moodle_url ('/badges/badgeclass.php ' , $ params );
201
- $ class ['criteria ' ] = $ badgeurl ->out (false ); // Currently badge URL.
202
- if ($ issued ) {
203
- $ params = ['id ' => $ this ->get_badge_id ()];
204
- $ issuerurl = new moodle_url ('/badges/issuer_json.php ' , $ params );
205
- $ class ['issuer ' ] = $ issuerurl ->out (false );
206
- }
207
- $ tags = $ this ->get_tags ();
208
- if (is_array ($ tags ) && count ($ tags ) > 0 ) {
209
- $ class ['tags ' ] = $ tags ;
210
- }
211
- $ this ->embed_data_badge_version2 ($ class , OPEN_BADGES_V2_TYPE_BADGE );
212
- if (!$ issued ) {
213
- unset($ class ['issuer ' ]);
214
- }
215
- }
216
- return $ class ;
132
+ // TODO: Should the image be added with the data:image/png;base64, prefix?
133
+ // TODO: This badge contains less information, for instance, version or language are not present).
134
+ $ badgeexporter = exporter_base::create_badge_exporter_from_id (
135
+ $ this ->get_badge_id (),
136
+ exporter_base::convert_apiversion (OPEN_BADGES_V2 ),
137
+ );
138
+ return $ badgeexporter ->export ();
217
139
}
218
140
219
141
/**
@@ -222,11 +144,11 @@ public function get_badge_class($issued = true) {
222
144
* @return array Issuer information.
223
145
*/
224
146
public function get_issuer () {
225
- $ badge = new badge ( $ this -> get_badge_id ());
226
- $ issuer = $ badge -> get_badge_issuer ();
227
-
228
- $ this -> embed_data_badge_version2 ( $ issuer , OPEN_BADGES_V2_TYPE_ISSUER );
229
- return $ issuer ;
147
+ $ issuerexporter = exporter_base:: create_issuer_exporter_from_id (
148
+ $ this -> get_badge_id (),
149
+ exporter_base:: convert_apiversion ( OPEN_BADGES_V2 ),
150
+ );
151
+ return $ issuerexporter -> export () ;
230
152
}
231
153
232
154
/**
@@ -236,20 +158,11 @@ public function get_issuer() {
236
158
* @return array|bool List related badges.
237
159
*/
238
160
public function get_related_badges (badge $ badge ) {
239
- global $ DB ;
240
- $ arraybadges = array ();
241
- $ relatedbadges = $ badge ->get_related_badges (true );
242
- if ($ relatedbadges ) {
243
- foreach ($ relatedbadges as $ rb ) {
244
- $ url = new moodle_url ('/badges/badge_json.php ' , array ('id ' => $ rb ->id ));
245
- $ arraybadges [] = array (
246
- 'id ' => $ url ->out (false ),
247
- 'version ' => $ rb ->version ,
248
- '@language ' => $ rb ->language
249
- );
250
- }
251
- }
252
- return $ arraybadges ;
161
+ $ badgeexporter = exporter_base::create_badge_exporter_from_id (
162
+ $ badge ->id ,
163
+ exporter_base::convert_apiversion (OPEN_BADGES_V2 ),
164
+ );
165
+ return $ badgeexporter ->export_related_badges ();
253
166
}
254
167
255
168
/**
@@ -270,18 +183,11 @@ public function get_endorsement() {
270
183
* @return array|string Criteria information.
271
184
*/
272
185
public function get_criteria_badge_class () {
273
- $ badge = new badge ($ this ->_data ->id );
274
- $ narrative = $ badge ->markdown_badge_criteria ();
275
- $ params = ['id ' => $ this ->get_badge_id ()];
276
- $ badgeurl = new moodle_url ('/badges/badgeclass.php ' , $ params );
277
- if (!empty ($ narrative )) {
278
- $ criteria = [];
279
- $ criteria ['id ' ] = $ badgeurl ->out (false );
280
- $ criteria ['narrative ' ] = $ narrative ;
281
- return $ criteria ;
282
- } else {
283
- return $ badgeurl ->out (false );
284
- }
186
+ $ badgeexporter = exporter_base::create_badge_exporter_from_id (
187
+ $ this ->get_badge_id (),
188
+ exporter_base::convert_apiversion (OPEN_BADGES_V2 ),
189
+ );
190
+ return $ badgeexporter ->export_criteria ();
285
191
}
286
192
287
193
/**
@@ -290,31 +196,19 @@ public function get_criteria_badge_class() {
290
196
* @return array information.
291
197
*/
292
198
public function get_alignments () {
293
- global $ DB ;
294
- $ badgeid = $ this ->_data ->id ;
295
- $ alignments = array ();
296
- $ items = $ DB ->get_records_select ('badge_alignment ' , 'badgeid = ? ' , array ($ badgeid ));
297
- foreach ($ items as $ item ) {
298
- $ alignment = array ('targetName ' => $ item ->targetname , 'targetUrl ' => $ item ->targeturl );
299
- if ($ item ->targetdescription ) {
300
- $ alignment ['targetDescription ' ] = $ item ->targetdescription ;
301
- }
302
- if ($ item ->targetframework ) {
303
- $ alignment ['targetFramework ' ] = $ item ->targetframework ;
304
- }
305
- if ($ item ->targetcode ) {
306
- $ alignment ['targetCode ' ] = $ item ->targetcode ;
307
- }
308
- $ alignments [] = $ alignment ;
309
- }
310
- return $ alignments ;
199
+ $ badgeexporter = exporter_base::create_badge_exporter_from_id (
200
+ $ this ->get_badge_id (),
201
+ exporter_base::convert_apiversion (OPEN_BADGES_V2 ),
202
+ );
203
+ return $ badgeexporter ->export_alignments ();
311
204
}
312
205
313
206
/**
314
207
* Embed data of Open Badges Specification Version 2.0 to json.
315
208
*
316
209
* @param array $json for assertion, badges, issuer.
317
210
* @param string $type Content type.
211
+ * TODO: Deprecate this method.
318
212
*/
319
213
protected function embed_data_badge_version2 (&$ json , $ type = OPEN_BADGES_V2_TYPE_ASSERTION ) {
320
214
// Specification Version 2.0.
0 commit comments