MDL-66504 lang: Apply cantblockuser changes to behat tests
[moodle.git] / badges / classes / external / user_badge_exporter.php
blob7146bebf7a4244b838dae4599d861be6a2cbef1d
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 user badge class for displaying a badge issued to a user.
20 * @package core_badges
21 * @copyright 2018 Dani Palou <dani@moodle.com>
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 moodle_url;
32 use core_badges\external\endorsement_exporter;
33 use core_badges\external\alignment_exporter;
34 use core_badges\external\related_info_exporter;
36 /**
37 * Class for displaying a badge issued to a user.
39 * @package core_badges
40 * @copyright 2018 Dani Palou <dani@moodle.com>
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 class user_badge_exporter extends exporter {
45 /**
46 * Return the list of properties.
48 * @return array
50 protected static function define_properties() {
51 return [
52 'id' => [
53 'type' => PARAM_INT,
54 'description' => 'Badge id',
55 'optional' => true,
57 'name' => [
58 'type' => PARAM_TEXT,
59 'description' => 'Badge name',
61 'description' => [
62 'type' => PARAM_NOTAGS,
63 'description' => 'Badge description',
64 'null' => NULL_ALLOWED,
66 'timecreated' => [
67 'type' => PARAM_INT,
68 'description' => 'Time created',
69 'optional' => true,
70 'default' => 0,
72 'timemodified' => [
73 'type' => PARAM_INT,
74 'description' => 'Time modified',
75 'optional' => true,
76 'default' => 0,
78 'usercreated' => [
79 'type' => PARAM_INT,
80 'description' => 'User created',
81 'optional' => true,
83 'usermodified' => [
84 'type' => PARAM_INT,
85 'description' => 'User modified',
86 'optional' => true,
88 'issuername' => [
89 'type' => PARAM_NOTAGS,
90 'description' => 'Issuer name',
92 'issuerurl' => [
93 'type' => PARAM_URL,
94 'description' => 'Issuer URL',
96 'issuercontact' => [
97 'type' => PARAM_RAW,
98 'description' => 'Issuer contact',
99 'null' => NULL_ALLOWED,
101 'expiredate' => [
102 'type' => PARAM_INT,
103 'description' => 'Expire date',
104 'optional' => true,
105 'null' => NULL_ALLOWED,
107 'expireperiod' => [
108 'type' => PARAM_INT,
109 'description' => 'Expire period',
110 'optional' => true,
111 'null' => NULL_ALLOWED,
113 'type' => [
114 'type' => PARAM_INT,
115 'description' => 'Type',
116 'optional' => true,
117 'default' => 1,
119 'courseid' => [
120 'type' => PARAM_INT,
121 'description' => 'Course id',
122 'optional' => true,
123 'null' => NULL_ALLOWED,
125 'message' => [
126 'type' => PARAM_RAW,
127 'description' => 'Message',
128 'optional' => true,
130 'messagesubject' => [
131 'type' => PARAM_TEXT,
132 'description' => 'Message subject',
133 'optional' => true,
135 'attachment' => [
136 'type' => PARAM_INT,
137 'description' => 'Attachment',
138 'optional' => true,
139 'default' => 1,
141 'notification' => [
142 'type' => PARAM_INT,
143 'description' => 'Whether to notify when badge is awarded',
144 'optional' => true,
145 'default' => 1,
147 'nextcron' => [
148 'type' => PARAM_INT,
149 'description' => 'Next cron',
150 'optional' => true,
151 'null' => NULL_ALLOWED,
153 'status' => [
154 'type' => PARAM_INT,
155 'description' => 'Status',
156 'optional' => true,
157 'default' => 0,
159 'issuedid' => [
160 'type' => PARAM_INT,
161 'description' => 'Issued id',
162 'optional' => true,
164 'uniquehash' => [
165 'type' => PARAM_ALPHANUM,
166 'description' => 'Unique hash',
168 'dateissued' => [
169 'type' => PARAM_INT,
170 'description' => 'Date issued',
171 'default' => 0,
173 'dateexpire' => [
174 'type' => PARAM_INT,
175 'description' => 'Date expire',
176 'null' => NULL_ALLOWED,
178 'visible' => [
179 'type' => PARAM_INT,
180 'description' => 'Visible',
181 'optional' => true,
182 'default' => 0,
184 'email' => [
185 'type' => PARAM_TEXT,
186 'description' => 'User email',
187 'optional' => true,
189 'version' => [
190 'type' => PARAM_TEXT,
191 'description' => 'Version',
192 'optional' => true,
193 'null' => NULL_ALLOWED,
195 'language' => [
196 'type' => PARAM_NOTAGS,
197 'description' => 'Language',
198 'optional' => true,
199 'null' => NULL_ALLOWED,
201 'imageauthorname' => [
202 'type' => PARAM_TEXT,
203 'description' => 'Name of the image author',
204 'optional' => true,
205 'null' => NULL_ALLOWED,
207 'imageauthoremail' => [
208 'type' => PARAM_TEXT,
209 'description' => 'Email of the image author',
210 'optional' => true,
211 'null' => NULL_ALLOWED,
213 'imageauthorurl' => [
214 'type' => PARAM_URL,
215 'description' => 'URL of the image author',
216 'optional' => true,
217 'null' => NULL_ALLOWED,
219 'imagecaption' => [
220 'type' => PARAM_TEXT,
221 'description' => 'Caption of the image',
222 'optional' => true,
223 'null' => NULL_ALLOWED,
229 * Returns a list of objects that are related.
231 * @return array
233 protected static function define_related() {
234 return array(
235 'context' => 'context',
236 'endorsement' => 'stdClass?',
237 'alignment' => 'stdClass[]',
238 'relatedbadges' => 'stdClass[]',
243 * Return the list of additional properties.
245 * @return array
247 protected static function define_other_properties() {
248 return [
249 'badgeurl' => [
250 'type' => PARAM_URL,
251 'description' => 'Badge URL',
253 'endorsement' => [
254 'type' => endorsement_exporter::read_properties_definition(),
255 'description' => 'Badge endorsement',
256 'optional' => true,
258 'alignment' => [
259 'type' => alignment_exporter::read_properties_definition(),
260 'description' => 'Badge alignments',
261 'multiple' => true,
263 'relatedbadges' => [
264 'type' => related_info_exporter::read_properties_definition(),
265 'description' => 'Related badges',
266 'multiple' => true,
272 * Get the additional values to inject while exporting.
274 * @param renderer_base $output The renderer.
275 * @return array Keys are the property names, values are their values.
277 protected function get_other_values(renderer_base $output) {
278 $context = $this->related['context'];
279 $endorsement = $this->related['endorsement'];
280 $alignments = $this->related['alignment'];
281 $relatedbadges = $this->related['relatedbadges'];
283 $values = array(
284 'badgeurl' => moodle_url::make_webservice_pluginfile_url($context->id, 'badges', 'badgeimage', $this->data->id, '/',
285 'f1')->out(false),
286 'alignment' => array(),
287 'relatedbadges' => array(),
290 if ($endorsement) {
291 $endorsementexporter = new endorsement_exporter($endorsement, array('context' => $context));
292 $values['endorsement'] = $endorsementexporter->export($output);
295 if (!empty($alignments)) {
296 foreach ($alignments as $alignment) {
297 $alignmentexporter = new alignment_exporter($alignment, array('context' => $context));
298 $values['alignment'][] = $alignmentexporter->export($output);
302 if (!empty($relatedbadges)) {
303 foreach ($relatedbadges as $badge) {
304 $relatedexporter = new related_info_exporter($badge, array('context' => $context));
305 $values['relatedbadges'][] = $relatedexporter->export($output);
309 return $values;