Skip to content

Commit d2d194a

Browse files
committed
MDL-83905 core_badges: Replace manual JSON with exporters
1 parent 31bdb5b commit d2d194a

File tree

2 files changed

+45
-152
lines changed

2 files changed

+45
-152
lines changed

badges/classes/assertion.php

+38-144
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
defined('MOODLE_INTERNAL') || die();
1818

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+
1924
/**
2025
* Open Badges Assertions specification 2.0
2126
* {@link https://www.imsglobal.org/sites/default/files/Badges/OBv2p0Final/index.html#Assertion}
@@ -24,12 +29,6 @@
2429
* - Badge Assertion (information regarding a specific badge that was awarded to a badge earner)
2530
* - Badge Class (general information about a badge and what it is intended to represent)
2631
* - 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.
3332
*
3433
* @package core_badges
3534
* @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
@@ -116,53 +115,11 @@ public function get_assertion_hash() {
116115
* @return array Badge assertion.
117116
*/
118117
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();
166123
}
167124

168125
/**
@@ -172,48 +129,13 @@ public function get_badge_assertion($issued = true, $usesalt = true) {
172129
* @return array Badge Class information.
173130
*/
174131
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();
217139
}
218140

219141
/**
@@ -222,11 +144,11 @@ public function get_badge_class($issued = true) {
222144
* @return array Issuer information.
223145
*/
224146
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();
230152
}
231153

232154
/**
@@ -236,20 +158,11 @@ public function get_issuer() {
236158
* @return array|bool List related badges.
237159
*/
238160
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();
253166
}
254167

255168
/**
@@ -270,18 +183,11 @@ public function get_endorsement() {
270183
* @return array|string Criteria information.
271184
*/
272185
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();
285191
}
286192

287193
/**
@@ -290,31 +196,19 @@ public function get_criteria_badge_class() {
290196
* @return array information.
291197
*/
292198
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();
311204
}
312205

313206
/**
314207
* Embed data of Open Badges Specification Version 2.0 to json.
315208
*
316209
* @param array $json for assertion, badges, issuer.
317210
* @param string $type Content type.
211+
* TODO: Deprecate this method.
318212
*/
319213
protected function embed_data_badge_version2(&$json, $type = OPEN_BADGES_V2_TYPE_ASSERTION) {
320214
// Specification Version 2.0.

badges/classes/badge.php

+7-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
namespace core_badges;
1818

19+
use core_badges\local\backpack\ob\exporter_base;
20+
1921
defined('MOODLE_INTERNAL') || die();
2022

2123
require_once($CFG->dirroot.'/lib/badgeslib.php');
@@ -946,14 +948,11 @@ public function markdown_badge_criteria() {
946948
* @return array Issuer informations of the badge.
947949
*/
948950
public function get_badge_issuer(?int $obversion = null) {
949-
return [
950-
'name' => $this->issuername,
951-
'url' => $this->issuerurl,
952-
'email' => $this->issuercontact,
953-
'@context' => OPEN_BADGES_V2_CONTEXT,
954-
'id' => (new moodle_url('/badges/issuer_json.php', ['id' => $this->id]))->out(false),
955-
'type' => OPEN_BADGES_V2_TYPE_ISSUER,
956-
];
951+
$issuerexporter = exporter_base::create_issuer_exporter_from_id(
952+
$this->id,
953+
exporter_base::convert_apiversion($obversion),
954+
);
955+
return $issuerexporter->export();
957956
}
958957

959958
/**

0 commit comments

Comments
 (0)