2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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
;
32 use core_badges\external\endorsement_exporter
;
33 use core_badges\external\alignment_exporter
;
34 use core_badges\external\related_info_exporter
;
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
{
46 * Return the list of properties.
50 protected static function define_properties() {
54 'description' => 'Badge id',
59 'description' => 'Badge name',
62 'type' => PARAM_NOTAGS
,
63 'description' => 'Badge description',
64 'null' => NULL_ALLOWED
,
68 'description' => 'Time created',
74 'description' => 'Time modified',
80 'description' => 'User created',
85 'description' => 'User modified',
89 'type' => PARAM_NOTAGS
,
90 'description' => 'Issuer name',
94 'description' => 'Issuer URL',
98 'description' => 'Issuer contact',
99 'null' => NULL_ALLOWED
,
103 'description' => 'Expire date',
105 'null' => NULL_ALLOWED
,
109 'description' => 'Expire period',
111 'null' => NULL_ALLOWED
,
115 'description' => 'Type',
121 'description' => 'Course id',
123 'null' => NULL_ALLOWED
,
127 'description' => 'Message',
130 'messagesubject' => [
131 'type' => PARAM_TEXT
,
132 'description' => 'Message subject',
137 'description' => 'Attachment',
143 'description' => 'Whether to notify when badge is awarded',
149 'description' => 'Next cron',
151 'null' => NULL_ALLOWED
,
155 'description' => 'Status',
161 'description' => 'Issued id',
165 'type' => PARAM_ALPHANUM
,
166 'description' => 'Unique hash',
170 'description' => 'Date issued',
175 'description' => 'Date expire',
176 'null' => NULL_ALLOWED
,
180 'description' => 'Visible',
185 'type' => PARAM_TEXT
,
186 'description' => 'User email',
190 'type' => PARAM_TEXT
,
191 'description' => 'Version',
193 'null' => NULL_ALLOWED
,
196 'type' => PARAM_NOTAGS
,
197 'description' => 'Language',
199 'null' => NULL_ALLOWED
,
201 'imageauthorname' => [
202 'type' => PARAM_TEXT
,
203 'description' => 'Name of the image author',
205 'null' => NULL_ALLOWED
,
207 'imageauthoremail' => [
208 'type' => PARAM_TEXT
,
209 'description' => 'Email of the image author',
211 'null' => NULL_ALLOWED
,
213 'imageauthorurl' => [
215 'description' => 'URL of the image author',
217 'null' => NULL_ALLOWED
,
220 'type' => PARAM_TEXT
,
221 'description' => 'Caption of the image',
223 'null' => NULL_ALLOWED
,
229 * Returns a list of objects that are related.
233 protected static function define_related() {
235 'context' => 'context',
236 'endorsement' => 'stdClass?',
237 'alignment' => 'stdClass[]',
238 'relatedbadges' => 'stdClass[]',
243 * Return the list of additional properties.
247 protected static function define_other_properties() {
251 'description' => 'Badge URL',
254 'type' => endorsement_exporter
::read_properties_definition(),
255 'description' => 'Badge endorsement',
259 'type' => alignment_exporter
::read_properties_definition(),
260 'description' => 'Badge alignments',
264 'type' => related_info_exporter
::read_properties_definition(),
265 'description' => 'Related badges',
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'];
284 'badgeurl' => moodle_url
::make_webservice_pluginfile_url($context->id
, 'badges', 'badgeimage', $this->data
->id
, '/',
286 'alignment' => array(),
287 'relatedbadges' => array(),
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);