Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
declare(strict_types=1);
18
 
19
namespace customfield_number;
20
 
21
use core\context\system;
22
use core\context;
23
use html_writer;
24
use MoodleQuickForm;
25
 
26
/**
27
 * Field controller class
28
 *
29
 * @package    customfield_number
30
 * @copyright  2024 Paul Holden <paulh@moodle.com>
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class field_controller  extends \core_customfield\field_controller {
34
 
35
    /**
36
     * Add form elements for editing the custom field definition
37
     *
38
     * @param MoodleQuickForm $mform
39
     */
40
    public function config_form_definition(MoodleQuickForm $mform): void {
41
        $mform->addElement('header', 'specificsettings', get_string('specificsettings', 'customfield_number'));
42
        $mform->setExpanded('specificsettings');
43
 
44
        $providers = provider_base::get_all_providers($this);
45
        if (count($providers) > 0) {
46
            $this->add_field_type_select($mform, $providers);
47
            // Add form config elements for each provider.
48
            foreach ($providers as $provider) {
49
                $provider->config_form_definition($mform);
50
            }
51
        }
52
 
53
        // Default value.
54
        $mform->addElement('float', 'configdata[defaultvalue]', get_string('defaultvalue', 'core_customfield'));
55
        if ($this->get_configdata_property('defaultvalue') === null) {
56
            $mform->setDefault('configdata[defaultvalue]', '');
57
        }
58
 
59
        // Minimum value.
60
        $mform->addElement('float', 'configdata[minimumvalue]', get_string('minimumvalue', 'customfield_number'));
61
        if ($this->get_configdata_property('minimumvalue') === null) {
62
            $mform->setDefault('configdata[minimumvalue]', '');
63
        }
64
 
65
        // Maximum value.
66
        $mform->addElement('float', 'configdata[maximumvalue]', get_string('maximumvalue', 'customfield_number'));
67
        if ($this->get_configdata_property('maximumvalue') === null) {
68
            $mform->setDefault('configdata[maximumvalue]', '');
69
        }
70
 
71
        // Decimal places.
72
        $mform->addElement('text', 'configdata[decimalplaces]', get_string('decimalplaces', 'customfield_number'));
73
        if ($this->get_configdata_property('decimalplaces') === null) {
74
            $mform->setDefault('configdata[decimalplaces]', 0);
75
        }
76
        $mform->setType('configdata[decimalplaces]', PARAM_INT);
77
 
78
        // Display format settings.
79
        // TODO: Change this after MDL-82996 fixed.
80
        $randelname = 'str_display_format';
81
        $mform->addGroup([], $randelname, html_writer::tag('h4', get_string('headerdisplaysettings', 'customfield_number')));
82
 
83
        // Display template.
84
        $mform->addElement('text', 'configdata[display]', get_string('display', 'customfield_number'),
85
            ['size' => 50]);
86
        $mform->setType('configdata[display]', PARAM_TEXT);
87
        $mform->addHelpButton('configdata[display]', 'display', 'customfield_number');
88
        if ($this->get_configdata_property('display') === null) {
89
            $mform->setDefault('configdata[display]', '{value}');
90
        }
91
 
92
        // Display when zero.
93
        $mform->addElement('text', 'configdata[displaywhenzero]', get_string('displaywhenzero', 'customfield_number'),
94
            ['size' => 50]);
95
        $mform->setType('configdata[displaywhenzero]', PARAM_TEXT);
96
        $mform->addHelpButton('configdata[displaywhenzero]', 'displaywhenzero', 'customfield_number');
97
        if ($this->get_configdata_property('displaywhenzero') === null) {
98
            $mform->setDefault('configdata[displaywhenzero]', 0);
99
        }
100
    }
101
 
102
    /**
103
     * Adds selector to provider for field population.
104
     *
105
     * @param MoodleQuickForm $mform
106
     * @param provider_base[] $providers
107
     */
108
    protected function add_field_type_select(MoodleQuickForm $mform, array $providers): void {
109
        $autooptions = [];
110
        foreach ($providers as $provider) {
111
            $autooptions[get_class($provider)] = $provider->get_name();
112
        }
113
        $options = ['' => get_string('genericfield', 'customfield_number')];
114
        $options = array_merge($options, $autooptions);
115
        $mform->addElement('select', 'configdata[fieldtype]', get_string('fieldtype', 'customfield_number'), $options);
116
        $mform->addHelpButton('configdata[fieldtype]', 'fieldtype', 'customfield_number');
117
    }
118
 
119
    /**
120
     * Validate the data on the field configuration form
121
     *
122
     * @param array $data
123
     * @param array $files
124
     * @return array
125
     */
126
    public function config_form_validation(array $data, $files = []): array {
127
        $errors = parent::config_form_validation($data, $files);
128
 
129
        $display = $data['configdata']['display'];
130
        if (!preg_match('/\{value}/', $display)) {
131
            $errors['configdata[display]'] = get_string('displayvalueconfigerror', 'customfield_number');
132
        }
133
 
134
        // Each of these configuration fields are optional.
135
        $defaultvalue = $data['configdata']['defaultvalue'] ?? '';
136
        $minimumvalue = $data['configdata']['minimumvalue'] ?? '';
137
        $maximumvalue = $data['configdata']['maximumvalue'] ?? '';
138
 
139
        foreach (provider_base::get_all_providers($this) as $provider) {
140
            if (array_key_exists('fieldtype', $data["configdata"]) && $data["configdata"]["fieldtype"] == get_class($provider)) {
141
                $errors = array_merge($errors, $provider->config_form_validation($data, $files));
142
            }
143
        }
144
        // Early exit if neither maximum/minimum are specified.
145
        if ($minimumvalue === '' && $maximumvalue === '') {
146
            return $errors;
147
        }
148
 
149
        $minimumvaluefloat = (float) $minimumvalue;
150
        $maximumvaluefloat = (float) $maximumvalue;
151
 
152
        // If maximum is set, it must be greater than minimum.
153
        if ($maximumvalue !== '' && $minimumvaluefloat >= $maximumvaluefloat) {
154
            $errors['configdata[minimumvalue]'] = get_string('minimumvalueconfigerror', 'customfield_number');
155
        }
156
 
157
        // If default value is set, it must be in range of minimum and maximum.
158
        if ($defaultvalue !== '') {
159
            $defaultvaluefloat = (float) $defaultvalue;
160
 
161
            if ($defaultvaluefloat < $minimumvaluefloat || ($maximumvalue !== '' && $defaultvaluefloat > $maximumvaluefloat)) {
162
                $errors['configdata[defaultvalue]'] = get_string('defaultvalueconfigerror', 'customfield_number');
163
            }
164
        }
165
 
166
        return $errors;
167
    }
168
 
169
    /**
170
     * Prepares a value for export
171
     *
172
     * @param mixed $value
173
     * @param context|null $context
174
     * @return string|float|null
175
     */
176
    public function prepare_field_for_display(mixed $value, ?context $context = null): string|null|float {
177
        if ($provider = provider_base::instance($this)) {
178
            return $provider->prepare_export_value($value, $context);
179
        }
180
 
181
        if ($value === null) {
182
            return null;
183
        }
184
 
185
        $decimalplaces = (int) $this->get_configdata_property('decimalplaces');
186
        if (round((float) $value, $decimalplaces) == 0) {
187
            $value = $this->get_configdata_property('displaywhenzero');
188
            if ((string) $value === '') {
189
                return null;
190
            }
191
        } else {
192
            // Let's format the value.
193
            $value = format_float((float)$value, $decimalplaces);
194
 
195
            // Apply the display format.
196
            $format = $this->get_configdata_property('display') ?? '{value}';
197
            $value = str_replace('{value}', $value, $format);
198
        }
199
 
200
        return format_string($value, true, ['context' => $context ?? system::instance()]);
201
    }
202
 
203
    /**
204
     * Can the value of this field be manually editable in the edit forms
205
     *
206
     * @return bool
207
     */
208
    public function is_editable(): bool {
209
        return (string) $this->get_configdata_property('fieldtype') === '';
210
    }
211
}