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 userpicture's core interaction API.
19
 *
20
 * @package    customcertelement_userpicture
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_userpicture;
26
 
27
/**
28
 * The customcert element userpicture's core interaction API.
29
 *
30
 * @package    customcertelement_userpicture
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 \mod_customcert\element {
35
 
36
    /**
37
     * This function renders the form elements when adding a customcert element.
38
     *
39
     * @param \MoodleQuickForm $mform the edit_form instance
40
     */
41
    public function render_form_elements($mform) {
42
        \mod_customcert\element_helper::render_form_element_width($mform);
43
 
44
        \mod_customcert\element_helper::render_form_element_height($mform);
45
 
46
        if (get_config('customcert', 'showposxy')) {
47
            \mod_customcert\element_helper::render_form_element_position($mform);
48
        }
49
    }
50
 
51
    /**
52
     * Performs validation on the element values.
53
     *
54
     * @param array $data the submitted data
55
     * @param array $files the submitted files
56
     * @return array the validation errors
57
     */
58
    public function validate_form_elements($data, $files) {
59
        // Array to return the errors.
60
        $errors = [];
61
 
62
        // Validate the width.
63
        $errors += \mod_customcert\element_helper::validate_form_element_width($data);
64
 
65
        // Validate the height.
66
        $errors += \mod_customcert\element_helper::validate_form_element_height($data);
67
 
68
        // Validate the position.
69
        if (get_config('customcert', 'showposxy')) {
70
            $errors += \mod_customcert\element_helper::validate_form_element_position($data);
71
        }
72
 
73
        return $errors;
74
    }
75
 
76
    /**
77
     * This will handle how form data will be saved into the data column in the
78
     * customcert_elements table.
79
     *
80
     * @param \stdClass $data the form data
81
     * @return string the json encoded array
82
     */
83
    public function save_unique_data($data) {
84
        // Array of data we will be storing in the database.
85
        $arrtostore = [
86
            'width' => (int) $data->width,
87
            'height' => (int) $data->height,
88
        ];
89
 
90
        return json_encode($arrtostore);
91
    }
92
 
93
    /**
94
     * Handles rendering the element on the pdf.
95
     *
96
     * @param \pdf $pdf the pdf object
97
     * @param bool $preview true if it is a preview, false otherwise
98
     * @param \stdClass $user the user we are rendering this for
99
     */
100
    public function render($pdf, $preview, $user) {
101
        global $CFG;
102
 
103
        // If there is no element data, we have nothing to display.
104
        if (empty($this->get_data())) {
105
            return;
106
        }
107
 
108
        $imageinfo = json_decode($this->get_data());
109
 
110
        $context = \context_user::instance($user->id);
111
 
112
        // Get files in the user icon area.
113
        $fs = get_file_storage();
114
        $files = $fs->get_area_files($context->id, 'user', 'icon', 0);
115
 
116
        // Get the file we want to display.
117
        $file = null;
118
        foreach ($files as $filefound) {
119
            if (!$filefound->is_directory()) {
120
                $file = $filefound;
121
                break;
122
            }
123
        }
124
 
125
        // Show image if we found one.
126
        if ($file) {
127
            $location = make_request_directory() . '/target';
128
            $file->copy_content_to($location);
129
            $pdf->Image($location, $this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height);
130
        } else if ($preview) { // Can't find an image, but we are in preview mode then display default pic.
131
            $location = $CFG->dirroot . '/pix/u/f1.png';
132
            $pdf->Image($location, $this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height);
133
        }
134
    }
135
 
136
    /**
137
     * Render the element in html.
138
     *
139
     * This function is used to render the element when we are using the
140
     * drag and drop interface to position it.
141
     *
142
     * @return string the html
143
     */
144
    public function render_html() {
145
        global $PAGE, $USER;
146
 
147
        // If there is no element data, we have nothing to display.
148
        if (empty($this->get_data())) {
149
            return '';
150
        }
151
 
152
        $imageinfo = json_decode($this->get_data());
153
 
154
        // Get the image.
155
        $userpicture = new \user_picture($USER);
156
        $userpicture->size = 1;
157
        $url = $userpicture->get_url($PAGE)->out(false);
158
 
159
        // The size of the images to use in the CSS style.
160
        $style = '';
161
        if ($imageinfo->width === 0 && $imageinfo->height === 0) {
162
            // Put this in so code checker doesn't complain.
163
            $style .= '';
164
        } else if ($imageinfo->width === 0) { // Then the height must be set.
165
            $style .= 'width: ' . $imageinfo->height . 'mm; ';
166
            $style .= 'height: ' . $imageinfo->height . 'mm';
167
        } else if ($imageinfo->height === 0) { // Then the width must be set.
168
            $style .= 'width: ' . $imageinfo->width . 'mm; ';
169
            $style .= 'height: ' . $imageinfo->width . 'mm';
170
        } else { // Must both be set.
171
            $style .= 'width: ' . $imageinfo->width . 'mm; ';
172
            $style .= 'height: ' . $imageinfo->height . 'mm';
173
        }
174
 
175
        return \html_writer::tag('img', '', ['src' => $url, 'style' => $style]);
176
    }
177
 
178
    /**
179
     * Sets the data on the form when editing an element.
180
     *
181
     * @param \MoodleQuickForm $mform the edit_form instance
182
     */
183
    public function definition_after_data($mform) {
184
        // Set the image, width and height for this element.
185
        if (!empty($this->get_data())) {
186
            $imageinfo = json_decode($this->get_data());
187
 
188
            $element = $mform->getElement('width');
189
            $element->setValue($imageinfo->width);
190
 
191
            $element = $mform->getElement('height');
192
            $element->setValue($imageinfo->height);
193
        }
194
 
195
        parent::definition_after_data($mform);
196
    }
197
}