weekly release 4.0dev
[moodle.git] / badges / classes / external / assertion_exporter.php
CommitLineData
aae219ac
DW
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.
13//
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/>.
16
17/**
18 * Contains class for displaying a assertion.
19 *
20 * @package core_badges
21 * @copyright 2019 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core_badges\external;
26
27defined('MOODLE_INTERNAL') || die();
28
29use core\external\exporter;
30use renderer_base;
31use stdClass;
32
33/**
34 * Class for displaying a badge competency.
35 *
36 * @package core_badges
37 * @copyright 2019 Damyon Wiese
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class assertion_exporter extends exporter {
41
42 /**
43 * Map from a request response data to the internal structure.
44 *
45 * @param stdClass $data The remote data.
46 * @param string $apiversion The backpack version used to communicate remotely.
47 * @return stdClass
48 */
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;
55 }
56 if (isset($data->entityId)) {
57 $mapped->id = $data->entityId;
58 } else {
59 $mapped->id = $data->id;
60 }
61 if (isset($data->issuedOn)) {
62 $mapped->issuedOn = $data->issuedOn;
63 }
64 if (isset($data->recipient)) {
65 $mapped->recipient = $data->recipient;
66 }
67 if (isset($data->badgeclass)) {
68 $mapped->badgeclass = $data->badgeclass;
69 }
70 $propname = '@context';
71 $mapped->$propname = 'https://w3id.org/openbadges/v2';
72 return $mapped;
73 }
74
1837b1d5
DW
75 /**
76 * Return the list of additional properties.
77 *
78 * @return array
79 */
aae219ac
DW
80 protected static function define_other_properties() {
81 return array(
82 'badge' => array(
83 'type' => badgeclass_exporter::read_properties_definition(),
84 'optional' => true
85 ),
86 'recipient' => array(
87 'type' => recipient_exporter::read_properties_definition(),
88 'optional' => true
89 ),
90 'verification' => array(
91 'type' => verification_exporter::read_properties_definition(),
92 'optional' => true
93 )
94 );
95 }
96
97 /**
98 * We map from related data passed as data to this exporter to clean exportable values.
1837b1d5
DW
99 *
100 * @param renderer_base $output
101 * @return array
aae219ac
DW
102 */
103 protected function get_other_values(renderer_base $output) {
104 global $DB;
105 $result = [];
106
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);
110 }
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);
114 }
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);
118 }
119 return $result;
120 }
121
122 /**
123 * Return the list of properties.
124 *
125 * @return array
126 */
127 protected static function define_properties() {
128 return [
129 'type' => [
130 'type' => PARAM_ALPHA,
131 'description' => 'Issuer',
132 ],
133 'id' => [
134 'type' => PARAM_URL,
135 'description' => 'Unique identifier for this assertion',
136 ],
137 'badgeclass' => [
138 'type' => PARAM_RAW,
139 'description' => 'Identifier of the badge for this assertion',
140 'optional' => true,
141 ],
142 'issuedOn' => [
143 'type' => PARAM_RAW,
144 'description' => 'Date this badge was issued',
145 ],
146 'expires' => [
147 'type' => PARAM_RAW,
148 'description' => 'Date this badge will expire',
149 'optional' => true,
150 ],
151 '@context' => [
152 'type' => PARAM_URL,
153 'description' => 'Badge version',
154 ],
155 ];
156 }
157
158 /**
159 * Returns a list of objects that are related.
160 *
161 * @return array
162 */
163 protected static function define_related() {
164 return array(
165 'context' => 'context'
166 );
167 }
168}