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 userfield's core interaction API.
19
 *
20
 * @package    customcertelement_userfield
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 customcertelement_userfield;
26
 
27
use core_user\fields;
28
 
29
/**
30
 * The customcert element userfield's core interaction API.
31
 *
32
 * @package    customcertelement_userfield
33
 * @copyright  2013 Mark Nelson <markn@moodle.com>
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class element extends \mod_customcert\element {
37
 
38
    /**
39
     * This function renders the form elements when adding a customcert element.
40
     *
41
     * @param \MoodleQuickForm $mform the edit_form instance
42
     */
43
    public function render_form_elements($mform) {
44
        // Get the user profile fields.
45
        $userfields = [
46
            'firstname' => fields::get_display_name('firstname'),
47
            'lastname' => fields::get_display_name('lastname'),
48
            'username' => fields::get_display_name('username'),
49
            'email' => fields::get_display_name('email'),
50
            'city' => fields::get_display_name('city'),
51
            'country' => fields::get_display_name('country'),
52
            'url' => fields::get_display_name('url'),
53
            'idnumber' => fields::get_display_name('idnumber'),
54
            'institution' => fields::get_display_name('institution'),
55
            'department' => fields::get_display_name('department'),
56
            'phone1' => fields::get_display_name('phone1'),
57
            'phone2' => fields::get_display_name('phone2'),
58
            'address' => fields::get_display_name('address'),
59
        ];
60
        // Get the user custom fields.
61
        $arrcustomfields = \availability_profile\condition::get_custom_profile_fields();
62
        $customfields = [];
63
        foreach ($arrcustomfields as $key => $customfield) {
64
            $customfields[$customfield->id] = $customfield->name;
65
        }
66
        // Combine the two.
67
        $fields = $userfields + $customfields;
68
        \core_collator::asort($fields);
69
 
70
        // Create the select box where the user field is selected.
71
        $mform->addElement('select', 'userfield', get_string('userfield', 'customcertelement_userfield'), $fields);
72
        $mform->setType('userfield', PARAM_ALPHANUM);
73
        $mform->addHelpButton('userfield', 'userfield', 'customcertelement_userfield');
74
 
75
        parent::render_form_elements($mform);
76
    }
77
 
78
    /**
79
     * This will handle how form data will be saved into the data column in the
80
     * customcert_elements table.
81
     *
82
     * @param \stdClass $data the form data
83
     * @return string the text
84
     */
85
    public function save_unique_data($data) {
86
        return $data->userfield;
87
    }
88
 
89
    /**
90
     * Handles rendering the element on the pdf.
91
     *
92
     * @param \pdf $pdf the pdf object
93
     * @param bool $preview true if it is a preview, false otherwise
94
     * @param \stdClass $user the user we are rendering this for
95
     */
96
    public function render($pdf, $preview, $user) {
97
        \mod_customcert\element_helper::render_content($pdf, $this, $this->get_user_field_value($user, $preview));
98
    }
99
 
100
    /**
101
     * Render the element in html.
102
     *
103
     * This function is used to render the element when we are using the
104
     * drag and drop interface to position it.
105
     */
106
    public function render_html() {
107
        global $USER;
108
 
109
        return \mod_customcert\element_helper::render_html_content($this, $this->get_user_field_value($USER, true));
110
    }
111
 
112
    /**
113
     * Sets the data on the form when editing an element.
114
     *
115
     * @param \MoodleQuickForm $mform the edit_form instance
116
     */
117
    public function definition_after_data($mform) {
118
        if (!empty($this->get_data())) {
119
            $element = $mform->getElement('userfield');
120
            $element->setValue($this->get_data());
121
        }
122
        parent::definition_after_data($mform);
123
    }
124
 
125
    /**
126
     * Helper function that returns the text.
127
     *
128
     * @param \stdClass $user the user we are rendering this for
129
     * @param bool $preview Is this a preview?
130
     * @return string
131
     */
132
    protected function get_user_field_value(\stdClass $user, bool $preview) : string {
133
        global $CFG, $DB;
134
 
135
        // The user field to display.
136
        $field = $this->get_data();
137
        // The value to display - we always want to show a value here so it can be repositioned.
138
        if ($preview) {
139
            $value = $field;
140
        } else {
141
            $value = '';
142
        }
143
        if (is_number($field)) { // Must be a custom user profile field.
144
            if ($field = $DB->get_record('user_info_field', ['id' => $field])) {
145
                // Found the field name, let's update the value to display.
146
                $value = $field->name;
147
                $file = $CFG->dirroot . '/user/profile/field/' . $field->datatype . '/field.class.php';
148
                if (file_exists($file)) {
149
                    require_once($CFG->dirroot . '/user/profile/lib.php');
150
                    require_once($file);
151
                    $class = "profile_field_{$field->datatype}";
152
                    $field = new $class($field->id, $user->id);
153
                    $value = $field->display_data();
154
                }
155
            }
156
        } else if (!empty($user->$field)) { // Field in the user table.
157
            $value = $user->$field;
158
        }
159
 
160
        $context = \mod_customcert\element_helper::get_context($this->get_id());
161
        return format_string($value, true, ['context' => $context]);
162
    }
163
}