on-demand release 3.8dev+
[moodle.git] / badges / classes / external / assertion_exporter.php
blob4e1e393773f8ba5871cdef89a8729652df118e4c
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 * Map from a request response data to the internal structure.
45 * @param stdClass $data The remote data.
46 * @param string $apiversion The backpack version used to communicate remotely.
47 * @return stdClass
49 public static function map_external_data($data, $apiversion) {
50 $mapped = new \stdClass();
51 if (isset($data->entityType)) {
52 $mapped->type = $data->entityType;
53 } else {
54 $mapped->type = $data->type;
56 if (isset($data->entityId)) {
57 $mapped->id = $data->entityId;
58 } else {
59 $mapped->id = $data->id;
61 if (isset($data->issuedOn)) {
62 $mapped->issuedOn = $data->issuedOn;
64 if (isset($data->recipient)) {
65 $mapped->recipient = $data->recipient;
67 if (isset($data->badgeclass)) {
68 $mapped->badgeclass = $data->badgeclass;
70 $propname = '@context';
71 $mapped->$propname = 'https://w3id.org/openbadges/v2';
72 return $mapped;
75 /**
76 * Return the list of additional properties.
78 * @return array
80 protected static function define_other_properties() {
81 return array(
82 'badge' => array(
83 'type' => badgeclass_exporter::read_properties_definition(),
84 'optional' => true
86 'recipient' => array(
87 'type' => recipient_exporter::read_properties_definition(),
88 'optional' => true
90 'verification' => array(
91 'type' => verification_exporter::read_properties_definition(),
92 'optional' => true
97 /**
98 * We map from related data passed as data to this exporter to clean exportable values.
100 * @param renderer_base $output
101 * @return array
103 protected function get_other_values(renderer_base $output) {
104 global $DB;
105 $result = [];
107 if (array_key_exists('related_badge', $this->data)) {
108 $exporter = new badgeclass_exporter($this->data['related_badge'], $this->related);
109 $result['badge'] = $exporter->export($output);
111 if (array_key_exists('related_recipient', $this->data)) {
112 $exporter = new recipient_exporter($this->data['related_recipient'], $this->related);
113 $result['recipient'] = $exporter->export($output);
115 if (array_key_exists('related_verify', $this->data)) {
116 $exporter = new verification_exporter($this->data['related_verify'], $this->related);
117 $result['verification'] = $exporter->export($output);
119 return $result;
123 * Return the list of properties.
125 * @return array
127 protected static function define_properties() {
128 return [
129 'type' => [
130 'type' => PARAM_ALPHA,
131 'description' => 'Issuer',
133 'id' => [
134 'type' => PARAM_URL,
135 'description' => 'Unique identifier for this assertion',
137 'badgeclass' => [
138 'type' => PARAM_RAW,
139 'description' => 'Identifier of the badge for this assertion',
140 'optional' => true,
142 'issuedOn' => [
143 'type' => PARAM_RAW,
144 'description' => 'Date this badge was issued',
146 'expires' => [
147 'type' => PARAM_RAW,
148 'description' => 'Date this badge will expire',
149 'optional' => true,
151 '@context' => [
152 'type' => PARAM_URL,
153 'description' => 'Badge version',
159 * Returns a list of objects that are related.
161 * @return array
163 protected static function define_related() {
164 return array(
165 'context' => 'context'