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 MoodleQuickForm;
22
 
23
/**
24
 * Data controller class
25
 *
26
 * @package    customfield_number
27
 * @copyright  2024 Paul Holden <paulh@moodle.com>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class data_controller extends \core_customfield\data_controller {
31
 
32
    /**
33
     * Return the name of the field where the information is stored
34
     *
35
     * @return string
36
     */
37
    public function datafield(): string {
38
        return 'decvalue';
39
    }
40
 
41
    /**
42
     * Add form elements for editing the custom field instance
43
     *
44
     * @param MoodleQuickForm $mform
45
     */
46
    public function instance_form_definition(MoodleQuickForm $mform): void {
47
        global $OUTPUT;
48
 
49
        $elementname = $this->get_form_element_name();
50
 
51
        // If the field isn't editable (based on type/provider config), then display static element.
52
        $field = $this->get_field();
53
        if (!$field->is_editable()) {
54
            $instanceid = (int)$this->get('instanceid');
55
            $data = ['value' => $this->export_value(), 'fieldid' => $field->get('id'), 'instanceid' => $instanceid];
56
            $value = $OUTPUT->render_from_template('customfield_number/staticvalue', $data);
57
            $mform->addElement('static', $elementname . '_static', $this->get_field()->get_formatted_name(),
58
                $value);
59
            return;
60
        }
61
 
62
        $mform->addElement('float', $elementname, $this->get_field()->get_formatted_name());
63
        if (!$this->get('id')) {
64
            $mform->setDefault($elementname, $this->get_default_value());
65
        }
66
    }
67
 
68
    /**
69
     * Validate the data on the field instance form
70
     *
71
     * @param array $data
72
     * @param array $files
73
     * @return array
74
     */
75
    public function instance_form_validation(array $data, array $files): array {
76
        $errors = parent::instance_form_validation($data, $files);
77
 
78
        $elementname = $this->get_form_element_name();
79
        $elementvalue = '';
80
        // Providers calculate values automatically, so nothing to validate.
81
        if (!provider_base::instance($this->get_field())) {
82
            $elementvalue = $data[$elementname];
83
        }
84
        $minimumvalue = $this->get_field()->get_configdata_property('minimumvalue') ?? '';
85
        $maximumvalue = $this->get_field()->get_configdata_property('maximumvalue') ?? '';
86
 
87
        // Early exit if element value isn't specified, or neither maximum/minimum are specified.
88
        if ($elementvalue === '' || ($minimumvalue === '' && $maximumvalue === '')) {
89
            return $errors;
90
        }
91
 
92
        $elementvaluefloat = (float) $elementvalue;
93
        $minimumvaluefloat = (float) $minimumvalue;
94
        $maximumvaluefloat = (float) $maximumvalue;
95
 
96
        $decimalplaces = (int) $this->get_field()->get_configdata_property('decimalplaces');
97
 
98
        // Value must be greater than minimum. If maximum is set, value must not exceed it.
99
        if ($minimumvalue !== '' && $elementvaluefloat < $minimumvaluefloat) {
100
            $errors[$elementname] = get_string('minimumvalueerror', 'customfield_number',
101
                format_float($minimumvaluefloat, $decimalplaces));
102
        } else if ($maximumvalue !== '' && $elementvaluefloat > $maximumvaluefloat) {
103
            $errors[$elementname] = get_string('maximumvalueerror', 'customfield_number',
104
                format_float($maximumvaluefloat, $decimalplaces));
105
        }
106
 
107
        return $errors;
108
    }
109
 
110
    /**
111
     * Checks if the value is empty
112
     *
113
     * @param mixed $value
114
     * @return bool
115
     */
116
    protected function is_empty($value): bool {
117
        return (string) $value === '';
118
    }
119
 
120
    /**
121
     * Returns the default value in non human-readable format
122
     *
123
     * @return float|null
124
     */
125
    public function get_default_value(): ?float {
126
        // If a provider is available, use its default value.
127
        if ($provider = provider_base::instance($this->get_field())) {
128
            return $provider->get_default_value();
129
        }
130
        $defaultvalue = $this->get_field()->get_configdata_property('defaultvalue');
131
        if ($this->is_empty($defaultvalue)) {
132
            return null;
133
        }
134
        return (float) $defaultvalue;
135
    }
136
 
137
    /**
138
     * Returns value in a human-readable format
139
     *
140
     * @return string|float|null
141
     */
142
    public function export_value(): string|float|null {
143
        /** @var field_controller $field */
144
        $field = $this->get_field();
145
        return $field->prepare_field_for_display($this->get_value(), $this->get_context());
146
    }
147
}