weekly release 5.0dev
[moodle.git] / badges / classes / external / assertion_exporter.php
blob78e86b06462f26249266811e9f241866225fec37
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
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;
30 use renderer_base;
31 use stdClass;
33 /**
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 {
42 /**
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);
56 /**
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.
61 * @return stdClass
63 public static function map_external_data($data, $apiversion) {
64 $mapped = new \stdClass();
65 if (isset($data->entityType)) {
66 $mapped->type = $data->entityType;
67 } else {
68 $mapped->type = $data->type;
70 if (isset($data->entityId)) {
71 $mapped->id = $data->entityId;
72 } else {
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';
86 return $mapped;
89 /**
90 * Return the list of additional properties.
92 * @return array
94 protected static function define_other_properties() {
95 return array(
96 'badge' => array(
97 'type' => badgeclass_exporter::read_properties_definition(),
98 'optional' => true
100 'recipient' => array(
101 'type' => recipient_exporter::read_properties_definition(),
102 'optional' => true
104 'verification' => array(
105 'type' => verification_exporter::read_properties_definition(),
106 'optional' => true
112 * We map from related data passed as data to this exporter to clean exportable values.
114 * @param renderer_base $output
115 * @return array
117 protected function get_other_values(renderer_base $output) {
118 global $DB;
119 $result = [];
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);
133 return $result;
137 * Return the list of properties.
139 * @return array
141 protected static function define_properties() {
142 return [
143 'type' => [
144 'type' => PARAM_ALPHA,
145 'description' => 'Issuer',
147 'id' => [
148 'type' => PARAM_URL,
149 'description' => 'Unique identifier for this assertion',
151 'badgeclass' => [
152 'type' => PARAM_RAW,
153 'description' => 'Identifier of the badge for this assertion',
154 'optional' => true,
156 'issuedOn' => [
157 'type' => PARAM_RAW,
158 'description' => 'Date this badge was issued',
160 'expires' => [
161 'type' => PARAM_RAW,
162 'description' => 'Date this badge will expire',
163 'optional' => true,
165 '@context' => [
166 'type' => PARAM_URL,
167 'description' => 'Badge version',
173 * Returns a list of objects that are related.
175 * @return array
177 protected static function define_related() {
178 return array(
179 'context' => 'context'