MDL-63262 badges: Add badgr.io
[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
75 protected static function define_other_properties() {
76 return array(
77 'badge' => array(
78 'type' => badgeclass_exporter::read_properties_definition(),
79 'optional' => true
80 ),
81 'recipient' => array(
82 'type' => recipient_exporter::read_properties_definition(),
83 'optional' => true
84 ),
85 'verification' => array(
86 'type' => verification_exporter::read_properties_definition(),
87 'optional' => true
88 )
89 );
90 }
91
92 /**
93 * We map from related data passed as data to this exporter to clean exportable values.
94 */
95 protected function get_other_values(renderer_base $output) {
96 global $DB;
97 $result = [];
98
99 if (array_key_exists('related_badge', $this->data)) {
100 $exporter = new badgeclass_exporter($this->data['related_badge'], $this->related);
101 $result['badge'] = $exporter->export($output);
102 }
103 if (array_key_exists('related_recipient', $this->data)) {
104 $exporter = new recipient_exporter($this->data['related_recipient'], $this->related);
105 $result['recipient'] = $exporter->export($output);
106 }
107 if (array_key_exists('related_verify', $this->data)) {
108 $exporter = new verification_exporter($this->data['related_verify'], $this->related);
109 $result['verification'] = $exporter->export($output);
110 }
111 return $result;
112 }
113
114 /**
115 * Return the list of properties.
116 *
117 * @return array
118 */
119 protected static function define_properties() {
120 return [
121 'type' => [
122 'type' => PARAM_ALPHA,
123 'description' => 'Issuer',
124 ],
125 'id' => [
126 'type' => PARAM_URL,
127 'description' => 'Unique identifier for this assertion',
128 ],
129 'badgeclass' => [
130 'type' => PARAM_RAW,
131 'description' => 'Identifier of the badge for this assertion',
132 'optional' => true,
133 ],
134 'issuedOn' => [
135 'type' => PARAM_RAW,
136 'description' => 'Date this badge was issued',
137 ],
138 'expires' => [
139 'type' => PARAM_RAW,
140 'description' => 'Date this badge will expire',
141 'optional' => true,
142 ],
143 '@context' => [
144 'type' => PARAM_URL,
145 'description' => 'Badge version',
146 ],
147 ];
148 }
149
150 /**
151 * Returns a list of objects that are related.
152 *
153 * @return array
154 */
155 protected static function define_related() {
156 return array(
157 'context' => 'context'
158 );
159 }
160}