Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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 user badge class for displaying a badge issued to a user.
19
 *
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
23
 */
24
 
25
namespace core_badges\external;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
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;
35
 
36
/**
37
 * Class for displaying a badge issued to a user.
38
 *
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
42
 */
43
class user_badge_exporter extends exporter {
44
 
45
    /**
46
     * Return the list of properties.
47
     *
48
     * @return array
49
     */
50
    protected static function define_properties() {
51
        return [
52
            'id' => [
53
                'type' => PARAM_INT,
54
                'description' => 'Badge id',
55
                'optional' => true,
56
            ],
57
            'name' => [
58
                'type' => PARAM_TEXT,
59
                'description' => 'Badge name',
60
            ],
61
            'description' => [
62
                'type' => PARAM_NOTAGS,
63
                'description' => 'Badge description',
64
                'null' => NULL_ALLOWED,
65
            ],
66
            'timecreated' => [
67
                'type' => PARAM_INT,
68
                'description' => 'Time created',
69
                'optional' => true,
70
                'default' => 0,
71
            ],
72
            'timemodified' => [
73
                'type' => PARAM_INT,
74
                'description' => 'Time modified',
75
                'optional' => true,
76
                'default' => 0,
77
            ],
78
            'usercreated' => [
79
                'type' => PARAM_INT,
80
                'description' => 'User created',
81
                'optional' => true,
82
            ],
83
            'usermodified' => [
84
                'type' => PARAM_INT,
85
                'description' => 'User modified',
86
                'optional' => true,
87
            ],
88
            'issuername' => [
89
                'type' => PARAM_TEXT,
90
                'description' => 'Issuer name',
91
            ],
92
            'issuerurl' => [
93
                'type' => PARAM_URL,
94
                'description' => 'Issuer URL',
95
            ],
96
            'issuercontact' => [
97
                'type' => PARAM_RAW,
98
                'description' => 'Issuer contact',
99
                'null' => NULL_ALLOWED,
100
            ],
101
            'expiredate' => [
102
                'type' => PARAM_INT,
103
                'description' => 'Expire date',
104
                'optional' => true,
105
                'null' => NULL_ALLOWED,
106
            ],
107
            'expireperiod' => [
108
                'type' => PARAM_INT,
109
                'description' => 'Expire period',
110
                'optional' => true,
111
                'null' => NULL_ALLOWED,
112
            ],
113
            'type' => [
114
                'type' => PARAM_INT,
115
                'description' => 'Type',
116
                'optional' => true,
117
                'default' => 1,
118
            ],
119
            'courseid' => [
120
                'type' => PARAM_INT,
121
                'description' => 'Course id',
122
                'optional' => true,
123
                'null' => NULL_ALLOWED,
124
            ],
1441 ariadna 125
            'coursefullname' => [
126
                'type' => PARAM_TEXT,
127
                'description' => 'Full name of the course',
128
                'optional' => true,
129
            ],
1 efrain 130
            'message' => [
131
                'type' => PARAM_RAW,
132
                'description' => 'Message',
133
                'optional' => true,
134
            ],
135
            'messagesubject' => [
136
                'type' => PARAM_TEXT,
137
                'description' => 'Message subject',
138
                'optional' => true,
139
            ],
140
            'attachment' => [
141
                'type' => PARAM_INT,
142
                'description' => 'Attachment',
143
                'optional' => true,
144
                'default' => 1,
145
            ],
146
            'notification' => [
147
                'type' => PARAM_INT,
148
                'description' => 'Whether to notify when badge is awarded',
149
                'optional' => true,
150
                'default' => 1,
151
            ],
152
            'nextcron' => [
153
                'type' => PARAM_INT,
154
                'description' => 'Next cron',
155
                'optional' => true,
156
                'null' => NULL_ALLOWED,
157
            ],
158
            'status' => [
159
                'type' => PARAM_INT,
160
                'description' => 'Status',
161
                'optional' => true,
162
                'default' => 0,
163
            ],
164
            'issuedid' => [
165
                'type' => PARAM_INT,
166
                'description' => 'Issued id',
167
                'optional' => true,
168
            ],
169
            'uniquehash' => [
170
                'type' => PARAM_ALPHANUM,
171
                'description' => 'Unique hash',
172
            ],
173
            'dateissued' => [
174
                'type' => PARAM_INT,
175
                'description' => 'Date issued',
176
                'default' => 0,
177
            ],
178
            'dateexpire' => [
179
                'type' => PARAM_INT,
180
                'description' => 'Date expire',
181
                'null' => NULL_ALLOWED,
182
            ],
183
            'visible' => [
184
                'type' => PARAM_INT,
185
                'description' => 'Visible',
186
                'optional' => true,
187
                'default' => 0,
188
            ],
1441 ariadna 189
            'recipientid' => [
190
                'type' => PARAM_INT,
191
                'description' => 'Id of the awarded user',
192
            ],
193
            'recipientfullname' => [
194
                'type' => PARAM_NOTAGS,
195
                'description' => 'Full name of the awarded user',
196
            ],
1 efrain 197
            'email' => [
198
                'type' => PARAM_TEXT,
199
                'description' => 'User email',
200
                'optional' => true,
201
            ],
202
            'version' => [
203
                'type' => PARAM_TEXT,
204
                'description' => 'Version',
205
                'optional' => true,
206
                'null' => NULL_ALLOWED,
207
            ],
208
            'language' => [
209
                'type' => PARAM_NOTAGS,
210
                'description' => 'Language',
211
                'optional' => true,
212
                'null' => NULL_ALLOWED,
213
            ],
214
            'imagecaption' => [
215
                'type' => PARAM_TEXT,
216
                'description' => 'Caption of the image',
217
                'optional' => true,
218
                'null' => NULL_ALLOWED,
219
            ],
220
        ];
221
    }
222
 
223
    /**
224
     * Returns a list of objects that are related.
225
     *
226
     * @return array
227
     */
228
    protected static function define_related() {
229
        return array(
230
            'context' => 'context',
231
            'endorsement' => 'stdClass?',
232
            'alignment' => 'stdClass[]',
233
            'relatedbadges' => 'stdClass[]',
234
        );
235
    }
236
 
237
    /**
238
     * Return the list of additional properties.
239
     *
240
     * @return array
241
     */
242
    protected static function define_other_properties() {
243
        return [
244
            'badgeurl' => [
245
                'type' => PARAM_URL,
246
                'description' => 'Badge URL',
247
            ],
248
            'endorsement' => [
249
                'type' => endorsement_exporter::read_properties_definition(),
250
                'description' => 'Badge endorsement',
251
                'optional' => true,
252
            ],
253
            'alignment' => [
254
                'type' => alignment_exporter::read_properties_definition(),
255
                'description' => 'Badge alignments',
256
                'multiple' => true,
257
            ],
258
            'relatedbadges' => [
259
                'type' => related_info_exporter::read_properties_definition(),
260
                'description' => 'Related badges',
261
                'multiple' => true,
262
            ]
263
        ];
264
    }
265
 
266
    /**
267
     * Get the additional values to inject while exporting.
268
     *
269
     * @param renderer_base $output The renderer.
270
     * @return array Keys are the property names, values are their values.
271
     */
272
    protected function get_other_values(renderer_base $output) {
273
        $context = $this->related['context'];
274
        $endorsement = $this->related['endorsement'];
275
        $alignments = $this->related['alignment'];
276
        $relatedbadges = $this->related['relatedbadges'];
277
 
278
        $values = array(
279
            'badgeurl' => moodle_url::make_webservice_pluginfile_url($context->id, 'badges', 'badgeimage', $this->data->id, '/',
280
                'f3')->out(false),
281
            'alignment' => array(),
282
            'relatedbadges' => array(),
283
        );
284
 
285
        if ($endorsement) {
286
            $endorsementexporter = new endorsement_exporter($endorsement, array('context' => $context));
287
            $values['endorsement'] = $endorsementexporter->export($output);
288
        }
289
 
290
        if (!empty($alignments)) {
291
            foreach ($alignments as $alignment) {
292
                $alignmentexporter = new alignment_exporter($alignment, array('context' => $context));
293
                $values['alignment'][] = $alignmentexporter->export($output);
294
            }
295
        }
296
 
297
        if (!empty($relatedbadges)) {
298
            foreach ($relatedbadges as $badge) {
299
                $relatedexporter = new related_info_exporter($badge, array('context' => $context));
300
                $values['relatedbadges'][] = $relatedexporter->export($output);
301
            }
302
        }
303
 
304
        return $values;
305
    }
306
}