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
 * This file contains the customcert element digitial signature's core interaction API.
19
 *
20
 * @package    customcertelement_digitalsignature
21
 * @copyright  2017 Mark Nelson <markn@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace customcertelement_digitalsignature;
26
 
27
/**
28
 * The customcert element digital signature's core interaction API.
29
 *
30
 * @package    customcertelement_digitalsignature
31
 * @copyright  2017 Mark Nelson <markn@moodle.com>
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class element extends \customcertelement_image\element {
35
 
36
    /**
37
     * @var array The file manager options for the certificate.
38
     */
39
    protected $signaturefilemanageroptions = [];
40
 
41
    /**
42
     * Constructor.
43
     *
44
     * @param \stdClass $element the element data
45
     */
46
    public function __construct($element) {
47
        global $COURSE;
48
 
49
        $this->signaturefilemanageroptions = [
50
            'maxbytes' => $COURSE->maxbytes,
51
            'subdirs' => 1,
52
            'accepted_types' => ['.crt'],
53
        ];
54
 
55
        parent::__construct($element);
56
    }
57
 
58
    /**
59
     * This function renders the form elements when adding a customcert element.
60
     *
61
     * @param \MoodleQuickForm $mform the edit_form instance
62
     */
63
    public function render_form_elements($mform) {
64
        $mform->addElement('select', 'fileid', get_string('image', 'customcertelement_image'), self::get_images());
65
 
66
        $mform->addElement('select', 'signaturefileid', get_string('digitalsignature', 'customcertelement_digitalsignature'),
67
            self::get_signatures());
68
 
69
        $mform->addElement('text', 'signaturename', get_string('signaturename', 'customcertelement_digitalsignature'));
70
        $mform->setType('signaturename', PARAM_TEXT);
71
        $mform->setDefault('signaturename', '');
72
 
73
        $mform->addElement('passwordunmask', 'signaturepassword',
74
            get_string('signaturepassword', 'customcertelement_digitalsignature'));
75
        $mform->setType('signaturepassword', PARAM_TEXT);
76
        $mform->setDefault('signaturepassword', '');
77
 
78
        $mform->addElement('text', 'signaturelocation', get_string('signaturelocation', 'customcertelement_digitalsignature'));
79
        $mform->setType('signaturelocation', PARAM_TEXT);
80
        $mform->setDefault('signaturelocation', '');
81
 
82
        $mform->addElement('text', 'signaturereason', get_string('signaturereason', 'customcertelement_digitalsignature'));
83
        $mform->setType('signaturereason', PARAM_TEXT);
84
        $mform->setDefault('signaturereason', '');
85
 
86
        $mform->addElement('text', 'signaturecontactinfo',
87
            get_string('signaturecontactinfo', 'customcertelement_digitalsignature'));
88
        $mform->setType('signaturecontactinfo', PARAM_TEXT);
89
        $mform->setDefault('signaturecontactinfo', '');
90
 
91
        \mod_customcert\element_helper::render_form_element_width($mform);
92
 
93
        \mod_customcert\element_helper::render_form_element_height($mform);
94
 
95
        if (get_config('customcert', 'showposxy')) {
96
            \mod_customcert\element_helper::render_form_element_position($mform);
97
        }
98
 
99
        $mform->addElement('filemanager', 'customcertimage', get_string('uploadimage', 'customcert'), '',
100
            $this->filemanageroptions);
101
 
102
        $mform->addElement('filemanager', 'digitalsignature',
103
            get_string('uploaddigitalsignature', 'customcertelement_digitalsignature'), '',
104
            $this->signaturefilemanageroptions);
105
    }
106
 
107
    /**
108
     * Handles saving the form elements created by this element.
109
     * Can be overridden if more functionality is needed.
110
     *
111
     * @param \stdClass $data the form data
112
     * @return bool true of success, false otherwise.
113
     */
114
    public function save_form_elements($data) {
115
        global $COURSE, $SITE;
116
 
117
        // Set the context.
118
        if ($COURSE->id == $SITE->id) {
119
            $context = \context_system::instance();
120
        } else {
121
            $context = \context_course::instance($COURSE->id);
122
        }
123
 
124
        // Handle file uploads.
125
        \mod_customcert\certificate::upload_files($data->customcertimage, $context->id);
126
 
127
        // Handle file certificate uploads.
128
        \mod_customcert\certificate::upload_files($data->digitalsignature, $context->id, 'signature');
129
 
130
        return parent::save_form_elements($data);
131
    }
132
 
133
    /**
134
     * This will handle how form data will be saved into the data column in the
135
     * customcert_elements table.
136
     *
137
     * @param \stdClass $data the form data
138
     * @return string the json encoded array
139
     */
140
    public function save_unique_data($data) {
141
        $arrtostore = [
142
            'signaturename' => $data->signaturename,
143
            'signaturepassword' => $data->signaturepassword,
144
            'signaturelocation' => $data->signaturelocation,
145
            'signaturereason' => $data->signaturereason,
146
            'signaturecontactinfo' => $data->signaturecontactinfo,
147
            'width' => !empty($data->width) ? (int) $data->width : 0,
148
            'height' => !empty($data->height) ? (int) $data->height : 0,
149
        ];
150
 
151
        // Array of data we will be storing in the database.
152
        $fs = get_file_storage();
153
 
154
        if (!empty($data->fileid)) {
155
            if ($file = $fs->get_file_by_id($data->fileid)) {
156
                $arrtostore += [
157
                    'contextid' => $file->get_contextid(),
158
                    'filearea' => $file->get_filearea(),
159
                    'itemid' => $file->get_itemid(),
160
                    'filepath' => $file->get_filepath(),
161
                    'filename' => $file->get_filename(),
162
                ];
163
            }
164
        }
165
 
166
        if (!empty($data->signaturefileid)) {
167
            if ($signaturefile = $fs->get_file_by_id($data->signaturefileid)) {
168
                $arrtostore += [
169
                    'signaturecontextid' => $signaturefile->get_contextid(),
170
                    'signaturefilearea' => $signaturefile->get_filearea(),
171
                    'signatureitemid' => $signaturefile->get_itemid(),
172
                    'signaturefilepath' => $signaturefile->get_filepath(),
173
                    'signaturefilename' => $signaturefile->get_filename(),
174
                ];
175
            }
176
        }
177
 
178
        return json_encode($arrtostore);
179
    }
180
 
181
    /**
182
     * Handles rendering the element on the pdf.
183
     *
184
     * @param \pdf $pdf the pdf object
185
     * @param bool $preview true if it is a preview, false otherwise
186
     * @param \stdClass $user the user we are rendering this for
187
     */
188
    public function render($pdf, $preview, $user) {
189
        // If there is no element data, we have nothing to display.
190
        if (empty($this->get_data())) {
191
            return;
192
        }
193
 
194
        $imageinfo = json_decode($this->get_data());
195
 
196
        // If there is no file, we have nothing to display.
197
        if (empty($imageinfo->filename)) {
198
            return;
199
        }
200
 
201
        // If there is no signature file, we have nothing to display.
202
        if (empty($imageinfo->signaturefilename)) {
203
            return;
204
        }
205
 
206
        if ($file = $this->get_file()) {
207
            $location = make_request_directory() . '/target';
208
            $file->copy_content_to($location);
209
 
210
            $mimetype = $file->get_mimetype();
211
            if ($mimetype == 'image/svg+xml') {
212
                $pdf->ImageSVG($location, $this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height);
213
            } else {
214
                $pdf->Image($location, $this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height);
215
            }
216
        }
217
 
218
        if ($signaturefile = $this->get_signature_file()) {
219
            $location = make_request_directory() . '/target';
220
            $signaturefile->copy_content_to($location);
221
            $info = [
222
                'Name' => $imageinfo->signaturename,
223
                'Location' => $imageinfo->signaturelocation,
224
                'Reason' => $imageinfo->signaturereason,
225
                'ContactInfo' => $imageinfo->signaturecontactinfo,
226
            ];
227
            $pdf->setSignature('file://' . $location, '', $imageinfo->signaturepassword, '', 2, $info);
228
            $pdf->setSignatureAppearance($this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height);
229
        }
230
    }
231
 
232
    /**
233
     * Sets the data on the form when editing an element.
234
     *
235
     * @param \MoodleQuickForm $mform the edit_form instance
236
     */
237
    public function definition_after_data($mform) {
238
        global $COURSE, $SITE;
239
 
240
        // Set the context.
241
        if ($COURSE->id == $SITE->id) {
242
            $context = \context_system::instance();
243
        } else {
244
            $context = \context_course::instance($COURSE->id);
245
        }
246
 
247
        if (!empty($this->get_data())) {
248
            $imageinfo = json_decode($this->get_data());
249
 
250
            $element = $mform->getElement('signaturename');
251
            $element->setValue($imageinfo->signaturename);
252
 
253
            $element = $mform->getElement('signaturepassword');
254
            $element->setValue($imageinfo->signaturepassword);
255
 
256
            $element = $mform->getElement('signaturelocation');
257
            $element->setValue($imageinfo->signaturelocation);
258
 
259
            $element = $mform->getElement('signaturereason');
260
            $element->setValue($imageinfo->signaturereason);
261
 
262
            $element = $mform->getElement('signaturecontactinfo');
263
            $element->setValue($imageinfo->signaturecontactinfo);
264
 
265
            if (!empty($imageinfo->signaturefilename)) {
266
                if ($signaturefile = $this->get_signature_file()) {
267
                    $element = $mform->getElement('signaturefileid');
268
                    $element->setValue($signaturefile->get_id());
269
                }
270
            }
271
        }
272
 
273
        // Editing existing instance - copy existing files into draft area.
274
        $draftitemid = file_get_submitted_draft_itemid('digitalsignature');
275
        file_prepare_draft_area($draftitemid, $context->id, 'mod_customcert', 'signature', 0,
276
            $this->signaturefilemanageroptions);
277
        $element = $mform->getElement('digitalsignature');
278
        $element->setValue($draftitemid);
279
 
280
        parent::definition_after_data($mform);
281
    }
282
 
283
    /**
284
     * Return the list of possible images to use.
285
     *
286
     * @return array the list of images that can be used
287
     */
288
    public static function get_signatures() {
289
        global $COURSE;
290
 
291
        // Create file storage object.
292
        $fs = get_file_storage();
293
 
294
        // The array used to store the digital signatures.
295
        $arrfiles = [];
296
        // Loop through the files uploaded in the system context.
297
        if ($files = $fs->get_area_files(\context_system::instance()->id, 'mod_customcert', 'signature', false,
298
                'filename', false)) {
299
            foreach ($files as $hash => $file) {
300
                $arrfiles[$file->get_id()] = $file->get_filename();
301
            }
302
        }
303
        // Loop through the files uploaded in the course context.
304
        if ($files = $fs->get_area_files(\context_course::instance($COURSE->id)->id, 'mod_customcert', 'signature', false,
305
                'filename', false)) {
306
            foreach ($files as $hash => $file) {
307
                $arrfiles[$file->get_id()] = $file->get_filename();
308
            }
309
        }
310
 
311
        \core_collator::asort($arrfiles);
312
        $arrfiles = ['0' => get_string('nosignature', 'customcertelement_digitalsignature')] + $arrfiles;
313
 
314
        return $arrfiles;
315
    }
316
 
317
    /**
318
     * Fetch stored file.
319
     *
320
     * @return \stored_file|bool stored_file instance if exists, false if not
321
     */
322
    public function get_signature_file() {
323
        $imageinfo = json_decode($this->get_data());
324
 
325
        $fs = get_file_storage();
326
 
327
        return $fs->get_file($imageinfo->signaturecontextid, 'mod_customcert', $imageinfo->signaturefilearea,
328
            $imageinfo->signatureitemid, $imageinfo->signaturefilepath, $imageinfo->signaturefilename);
329
    }
330
}