2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Contains class for displaying a assertion.
20 * @package core_badges
21 * @copyright 2019 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_badges\external
;
27 defined('MOODLE_INTERNAL') ||
die();
29 use core\external\exporter
;
34 * Class for displaying a badge competency.
36 * @package core_badges
37 * @copyright 2019 Damyon Wiese
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class assertion_exporter
extends exporter
{
43 * Constructor - saves the persistent object, and the related objects.
45 * @param mixed $data - Either an stdClass or an array of values.
46 * @param array $related - An optional list of pre-loaded objects related to this object.
48 public function __construct($data, $related = array()) {
49 // Having mixed $data is causing some issues. As this class is treating $data as an object everywhere, it can be converted
50 // to object at this point, to avoid errors and get the expected behaviour always.
51 // $data is an array when this class is a request exporter in backpack_api_mapping, but it is an object when this is
52 // used as a response exporter.
53 parent
::__construct((object) $data, $related);
57 * Map from a request response data to the internal structure.
59 * @param stdClass $data The remote data.
60 * @param string $apiversion The backpack version used to communicate remotely.
63 public static function map_external_data($data, $apiversion) {
64 $mapped = new \
stdClass();
65 if (isset($data->entityType
)) {
66 $mapped->type
= $data->entityType
;
68 $mapped->type
= $data->type
;
70 if (isset($data->entityId
)) {
71 $mapped->id
= $data->entityId
;
73 $mapped->id
= $data->id
;
75 if (isset($data->issuedOn
)) {
76 $mapped->issuedOn
= $data->issuedOn
;
78 if (isset($data->recipient
)) {
79 $mapped->recipient
= $data->recipient
;
81 if (isset($data->badgeclass
)) {
82 $mapped->badgeclass
= $data->badgeclass
;
84 $propname = '@context';
85 $mapped->$propname = 'https://w3id.org/openbadges/v2';
90 * Return the list of additional properties.
94 protected static function define_other_properties() {
97 'type' => badgeclass_exporter
::read_properties_definition(),
100 'recipient' => array(
101 'type' => recipient_exporter
::read_properties_definition(),
104 'verification' => array(
105 'type' => verification_exporter
::read_properties_definition(),
112 * We map from related data passed as data to this exporter to clean exportable values.
114 * @param renderer_base $output
117 protected function get_other_values(renderer_base
$output) {
121 if (property_exists($this->data
, 'related_badge')) {
122 $exporter = new badgeclass_exporter($this->data
->related_badge
, $this->related
);
123 $result['badge'] = $exporter->export($output);
125 if (property_exists($this->data
, 'related_recipient')) {
126 $exporter = new recipient_exporter($this->data
->related_recipient
, $this->related
);
127 $result['recipient'] = $exporter->export($output);
129 if (property_exists($this->data
, 'related_verify')) {
130 $exporter = new verification_exporter($this->data
->related_verify
, $this->related
);
131 $result['verification'] = $exporter->export($output);
137 * Return the list of properties.
141 protected static function define_properties() {
144 'type' => PARAM_ALPHA
,
145 'description' => 'Issuer',
149 'description' => 'Unique identifier for this assertion',
153 'description' => 'Identifier of the badge for this assertion',
158 'description' => 'Date this badge was issued',
162 'description' => 'Date this badge will expire',
167 'description' => 'Badge version',
173 * Returns a list of objects that are related.
177 protected static function define_related() {
179 'context' => 'context'