Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of the customcert module for 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
 * The base class for the customcert elements.
19
 *
20
 * @package    mod_customcert
21
 * @copyright  2013 Mark Nelson <markn@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace mod_customcert;
26
 
27
/**
28
 * Class element
29
 *
30
 * All customcert element plugins are based on this class.
31
 *
32
 * @package    mod_customcert
33
 * @copyright  2013 Mark Nelson <markn@moodle.com>
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
abstract class element {
37
 
38
    /**
39
     * @var string The left alignment constant.
40
     */
41
    const ALIGN_LEFT = 'L';
42
 
43
    /**
44
     * @var string The centered alignment constant.
45
     */
46
    const ALIGN_CENTER = 'C';
47
 
48
    /**
49
     * @var string The right alignment constant.
50
     */
51
    const ALIGN_RIGHT = 'R';
52
 
53
    /**
54
     * @var \stdClass $element The data for the element we are adding - do not use, kept for legacy reasons.
55
     */
56
    protected $element;
57
 
58
    /**
59
     * @var int The id.
60
     */
61
    protected $id;
62
 
63
    /**
64
     * @var int The page id.
65
     */
66
    protected $pageid;
67
 
68
    /**
69
     * @var string The name.
70
     */
71
    protected $name;
72
 
73
    /**
74
     * @var mixed The data.
75
     */
76
    protected $data;
77
 
78
    /**
79
     * @var string The font name.
80
     */
81
    protected $font;
82
 
83
    /**
84
     * @var int The font size.
85
     */
86
    protected $fontsize;
87
 
88
    /**
89
     * @var string The font colour.
90
     */
91
    protected $colour;
92
 
93
    /**
94
     * @var int The position x.
95
     */
96
    protected $posx;
97
 
98
    /**
99
     * @var int The position y.
100
     */
101
    protected $posy;
102
 
103
    /**
104
     * @var int The width.
105
     */
106
    protected $width;
107
 
108
    /**
109
     * @var int The refpoint.
110
     */
111
    protected $refpoint;
112
 
113
    /**
114
     * @var string The alignment.
115
     */
116
    protected $alignment;
117
 
118
    /**
119
     * @var bool $showposxy Show position XY form elements?
120
     */
121
    protected $showposxy;
122
 
123
    /**
124
     * @var edit_element_form Element edit form instance.
125
     */
126
    private $editelementform;
127
 
128
    /**
129
     * Constructor.
130
     *
131
     * @param \stdClass $element the element data
132
     */
133
    public function __construct($element) {
134
        $showposxy = get_config('customcert', 'showposxy');
135
 
136
        // Keeping this for legacy reasons so we do not break third-party elements.
137
        $this->element = clone($element);
138
 
139
        $this->id = $element->id;
140
        $this->pageid = $element->pageid;
141
        $this->name = $element->name;
142
        $this->data = $element->data;
143
        $this->font = $element->font;
144
        $this->fontsize = $element->fontsize;
145
        $this->colour = $element->colour;
146
        $this->posx = $element->posx;
147
        $this->posy = $element->posy;
148
        $this->width = $element->width;
149
        $this->refpoint = $element->refpoint;
150
        $this->showposxy = isset($showposxy) && $showposxy;
151
        $this->set_alignment($element->alignment ?? self::ALIGN_LEFT);
152
    }
153
 
154
    /**
155
     * Returns the id.
156
     *
157
     * @return int
158
     */
159
    public function get_id() {
160
        return $this->id;
161
    }
162
 
163
    /**
164
     * Returns the page id.
165
     *
166
     * @return int
167
     */
168
    public function get_pageid() {
169
        return $this->pageid;
170
    }
171
 
172
    /**
173
     * Returns the name.
174
     *
175
     * @return int
176
     */
177
    public function get_name() {
178
        return $this->name;
179
    }
180
 
181
    /**
182
     * Returns the data.
183
     *
184
     * @return mixed
185
     */
186
    public function get_data() {
187
        return $this->data;
188
    }
189
 
190
    /**
191
     * Returns the font name.
192
     *
193
     * @return string
194
     */
195
    public function get_font() {
196
        return $this->font;
197
    }
198
 
199
    /**
200
     * Returns the font size.
201
     *
202
     * @return int
203
     */
204
    public function get_fontsize() {
205
        return $this->fontsize;
206
    }
207
 
208
    /**
209
     * Returns the font colour.
210
     *
211
     * @return string
212
     */
213
    public function get_colour() {
214
        return $this->colour;
215
    }
216
 
217
    /**
218
     * Returns the position x.
219
     *
220
     * @return int
221
     */
222
    public function get_posx() {
223
        return $this->posx;
224
    }
225
 
226
    /**
227
     * Returns the position y.
228
     *
229
     * @return int
230
     */
231
    public function get_posy() {
232
        return $this->posy;
233
    }
234
 
235
    /**
236
     * Returns the width.
237
     *
238
     * @return int
239
     */
240
    public function get_width() {
241
        return $this->width;
242
    }
243
 
244
    /**
245
     * Returns the refpoint.
246
     *
247
     * @return int
248
     */
249
    public function get_refpoint() {
250
        return $this->refpoint;
251
    }
252
 
253
    /**
254
     * Returns the alignment.
255
     *
256
     * @return string The current alignment value.
257
     */
258
    public function get_alignment() {
259
        return $this->alignment ?? self::ALIGN_LEFT;
260
    }
261
 
262
    /**
263
     * Sets the alignment.
264
     *
265
     * @param string $alignment The new alignment.
266
     *
267
     * @throws \InvalidArgumentException if the provided new alignment is not valid.
268
     */
269
    protected function set_alignment(string $alignment) {
270
        $validvalues = [self::ALIGN_LEFT, self::ALIGN_CENTER, self::ALIGN_RIGHT];
271
        if (!in_array($alignment, $validvalues)) {
272
            throw new \InvalidArgumentException("'$alignment' is not a valid alignment value. It has to be one of " .
273
                implode(', ', $validvalues));
274
        }
275
        $this->alignment = $alignment;
276
    }
277
 
278
    /**
279
     * This function renders the form elements when adding a customcert element.
280
     * Can be overridden if more functionality is needed.
281
     *
282
     * @param \MoodleQuickForm $mform the edit_form instance.
283
     */
284
    public function render_form_elements($mform) {
285
        // Render the common elements.
286
        element_helper::render_form_element_font($mform);
287
        element_helper::render_form_element_colour($mform);
288
        if ($this->showposxy) {
289
            element_helper::render_form_element_position($mform);
290
        }
291
        element_helper::render_form_element_width($mform);
292
        element_helper::render_form_element_refpoint($mform);
293
        element_helper::render_form_element_alignment($mform);
294
    }
295
 
296
    /**
297
     * Sets the data on the form when editing an element.
298
     * Can be overridden if more functionality is needed.
299
     *
300
     * @param edit_element_form $mform the edit_form instance
301
     */
302
    public function definition_after_data($mform) {
303
        // Loop through the properties of the element and set the values
304
        // of the corresponding form element, if it exists.
305
        $properties = [
306
            'name' => $this->name,
307
            'font' => $this->font,
308
            'fontsize' => $this->fontsize,
309
            'colour' => $this->colour,
310
            'posx' => $this->posx,
311
            'posy' => $this->posy,
312
            'width' => $this->width,
313
            'refpoint' => $this->refpoint,
314
            'alignment' => $this->get_alignment(),
315
        ];
316
        foreach ($properties as $property => $value) {
317
            if (!is_null($value) && $mform->elementExists($property)) {
318
                $element = $mform->getElement($property);
319
                $element->setValue($value);
320
            }
321
        }
322
    }
323
 
324
    /**
325
     * Performs validation on the element values.
326
     * Can be overridden if more functionality is needed.
327
     *
328
     * @param array $data the submitted data
329
     * @param array $files the submitted files
330
     * @return array the validation errors
331
     */
332
    public function validate_form_elements($data, $files) {
333
        // Array to return the errors.
334
        $errors = [];
335
 
336
        // Common validation methods.
337
        $errors += element_helper::validate_form_element_colour($data);
338
        if ($this->showposxy) {
339
            $errors += element_helper::validate_form_element_position($data);
340
        }
341
        $errors += element_helper::validate_form_element_width($data);
342
 
343
        return $errors;
344
    }
345
 
346
    /**
347
     * Handles saving the form elements created by this element.
348
     * Can be overridden if more functionality is needed.
349
     *
350
     * @param \stdClass $data the form data
351
     * @return bool true of success, false otherwise.
352
     */
353
    public function save_form_elements($data) {
354
        global $DB;
355
 
356
        // Get the data from the form.
357
        $element = new \stdClass();
358
        $element->name = $data->name;
359
        $element->data = $this->save_unique_data($data);
360
        $element->font = $data->font ?? null;
361
        $element->fontsize = $data->fontsize ?? null;
362
        $element->colour = $data->colour ?? null;
363
        if ($this->showposxy) {
364
            $element->posx = $data->posx ?? null;
365
            $element->posy = $data->posy ?? null;
366
        }
367
        $element->width = $data->width ?? null;
368
        $element->refpoint = $data->refpoint ?? null;
369
        $element->alignment = $data->alignment ?? self::ALIGN_LEFT;
370
        $element->timemodified = time();
371
 
372
        // Check if we are updating, or inserting a new element.
373
        if (!empty($this->id)) { // Must be updating a record in the database.
374
            $element->id = $this->id;
375
            $return = $DB->update_record('customcert_elements', $element);
376
 
377
            \mod_customcert\event\element_updated::create_from_element($this)->trigger();
378
 
379
            return $return;
380
        } else { // Must be adding a new one.
381
            $element->element = $data->element;
382
            $element->pageid = $data->pageid;
383
            $element->sequence = \mod_customcert\element_helper::get_element_sequence($element->pageid);
384
            $element->timecreated = time();
385
            $element->id = $DB->insert_record('customcert_elements', $element, true);
386
            $this->id = $element->id;
387
 
388
            \mod_customcert\event\element_created::create_from_element($this)->trigger();
389
 
390
            return $element->id;
391
        }
392
    }
393
 
394
    /**
395
     * This will handle how form data will be saved into the data column in the
396
     * customcert_elements table.
397
     * Can be overridden if more functionality is needed.
398
     *
399
     * @param \stdClass $data the form data
400
     * @return string the unique data to save
401
     */
402
    public function save_unique_data($data) {
403
        return '';
404
    }
405
 
406
    /**
407
     * This handles copying data from another element of the same type.
408
     * Can be overridden if more functionality is needed.
409
     *
410
     * @param \stdClass $data the form data
411
     * @return bool returns true if the data was copied successfully, false otherwise
412
     */
413
    public function copy_element($data) {
414
        return true;
415
    }
416
 
417
    /**
418
     * This defines if an element plugin can be added to a certificate.
419
     * Can be overridden if an element plugin wants to take over the control.
420
     *
421
     * @return bool returns true if the element can be added, false otherwise
422
     */
423
    public static function can_add() {
424
        return true;
425
    }
426
 
427
    /**
428
     * Handles rendering the element on the pdf.
429
     *
430
     * Must be overridden.
431
     *
432
     * @param \pdf $pdf the pdf object
433
     * @param bool $preview true if it is a preview, false otherwise
434
     * @param \stdClass $user the user we are rendering this for
435
     */
436
    abstract public function render($pdf, $preview, $user);
437
 
438
    /**
439
     * Render the element in html.
440
     *
441
     * Must be overridden.
442
     *
443
     * This function is used to render the element when we are using the
444
     * drag and drop interface to position it.
445
     *
446
     * @return string the html
447
     */
448
    abstract public function render_html();
449
 
450
    /**
451
     * Handles deleting any data this element may have introduced.
452
     * Can be overridden if more functionality is needed.
453
     *
454
     * @return bool success return true if deletion success, false otherwise
455
     */
456
    public function delete() {
457
        global $DB;
458
 
459
        $return = $DB->delete_records('customcert_elements', ['id' => $this->id]);
460
 
461
        \mod_customcert\event\element_deleted::create_from_element($this)->trigger();
462
 
463
        return $return;
464
    }
465
 
466
    /**
467
     * This function is responsible for handling the restoration process of the element.
468
     *
469
     * For example, the function may save data that is related to another course module, this
470
     * data will need to be updated if we are restoring the course as the course module id will
471
     * be different in the new course.
472
     *
473
     * @param \restore_customcert_activity_task $restore
474
     */
475
    public function after_restore($restore) {
476
 
477
    }
478
 
479
    /**
480
     * Magic getter for read only access.
481
     *
482
     * @param string $name
483
     */
484
    public function __get($name) {
485
        debugging('Please call the appropriate get_* function instead of relying on magic getters', DEBUG_DEVELOPER);
486
        if (property_exists($this->element, $name)) {
487
            return $this->element->$name;
488
        }
489
    }
490
 
491
    /**
492
     * Set edit form instance for the custom cert element.
493
     *
494
     * @param \mod_customcert\edit_element_form $editelementform
495
     */
496
    public function set_edit_element_form(edit_element_form $editelementform) {
497
        $this->editelementform = $editelementform;
498
    }
499
 
500
    /**
501
     * Get edit form instance for the custom cert element.
502
     *
503
     * @return \mod_customcert\edit_element_form
504
     */
505
    public function get_edit_element_form() {
506
        if (empty($this->editelementform)) {
507
            throw new \coding_exception('Edit element form instance is not set.');
508
        }
509
 
510
        return $this->editelementform;
511
    }
512
 
513
}