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 context;
|
|
|
22 |
use MoodleQuickForm;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Class provider_base
|
|
|
26 |
*
|
|
|
27 |
* @package customfield_number
|
|
|
28 |
* @author 2024 Marina Glancy
|
|
|
29 |
* @copyright 2024 Moodle Pty Ltd <support@moodle.com>
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
*/
|
|
|
32 |
abstract class provider_base {
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Constructor.
|
|
|
36 |
*
|
|
|
37 |
* @param field_controller $field A field controller.
|
|
|
38 |
*/
|
|
|
39 |
public function __construct(
|
|
|
40 |
/** @var field_controller the custom field controller */
|
|
|
41 |
protected field_controller $field,
|
|
|
42 |
) {
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Provider name
|
|
|
47 |
*/
|
|
|
48 |
abstract public function get_name(): string;
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* If provide is available for the current field.
|
|
|
52 |
*/
|
|
|
53 |
abstract public function is_available(): bool;
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Add provider specific fields for form.
|
|
|
57 |
*
|
|
|
58 |
* @param \MoodleQuickForm $mform
|
|
|
59 |
*/
|
|
|
60 |
public function config_form_definition(MoodleQuickForm $mform): void {
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Recalculate field value.
|
|
|
65 |
*
|
|
|
66 |
* @param int|null $instanceid
|
|
|
67 |
*/
|
|
|
68 |
public function recalculate(?int $instanceid = null): void {
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Default value if there is no value in the database (or there is a null)
|
|
|
73 |
*
|
|
|
74 |
* Usually returns either null or 0
|
|
|
75 |
*
|
|
|
76 |
* @return null|float
|
|
|
77 |
*/
|
|
|
78 |
public function get_default_value(): ?float {
|
|
|
79 |
return null;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* How the field should be displayed
|
|
|
84 |
*
|
|
|
85 |
* Called from {@see field_controller::prepare_field_for_display()}
|
|
|
86 |
* The return value may contain safe HTML but all user input must be passed through
|
|
|
87 |
* format_string/format_text functions
|
|
|
88 |
*
|
|
|
89 |
* @param mixed $value String or float
|
|
|
90 |
* @param context|null $context Context
|
|
|
91 |
* @return ?string null if the field should not be displayed or string representation of the field
|
|
|
92 |
*/
|
|
|
93 |
public function prepare_export_value(mixed $value, ?\context $context = null): ?string {
|
|
|
94 |
if ($value === null) {
|
|
|
95 |
return null;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
// By default assumes that configuration 'decimalplaces' and 'displaywhenzero' are
|
|
|
99 |
// present. If they are not used in this provider, override the method.
|
|
|
100 |
$decimalplaces = (int) $this->field->get_configdata_property('decimalplaces');
|
|
|
101 |
if (round((float) $value, $decimalplaces) == 0) {
|
|
|
102 |
$result = $this->field->get_configdata_property('displaywhenzero');
|
|
|
103 |
if ((string) $result === '') {
|
|
|
104 |
return null;
|
|
|
105 |
} else {
|
|
|
106 |
return format_string($result, true, ['context' => $context ?? \core\context\system::instance()]);
|
|
|
107 |
}
|
|
|
108 |
} else {
|
|
|
109 |
return format_float((float)$value, $decimalplaces);
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Returns a new provider instance.
|
|
|
115 |
*
|
|
|
116 |
* @param field_controller $field Field
|
|
|
117 |
*/
|
|
|
118 |
final public static function instance(\core_customfield\field_controller $field): ?self {
|
|
|
119 |
if ($field->get('type') !== 'number' || !($field instanceof field_controller)) {
|
|
|
120 |
return null;
|
|
|
121 |
}
|
|
|
122 |
$classname = $field->get_configdata_property('fieldtype');
|
|
|
123 |
if (!$classname) {
|
|
|
124 |
return null;
|
|
|
125 |
}
|
|
|
126 |
if (!class_exists($classname) || !is_a($classname, self::class, true)) {
|
|
|
127 |
return new missing_provider($field);
|
|
|
128 |
}
|
|
|
129 |
return new $classname($field);
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* List of applicable automatic providers for this field
|
|
|
134 |
*
|
|
|
135 |
* @param field_controller $field
|
|
|
136 |
* @return provider_base[]
|
|
|
137 |
*/
|
|
|
138 |
final public static function get_all_providers(field_controller $field): array {
|
|
|
139 |
/** @var provider_base[] $allproviders */
|
|
|
140 |
$allproviders = [
|
|
|
141 |
new \customfield_number\local\numberproviders\nofactivities($field),
|
|
|
142 |
];
|
|
|
143 |
|
|
|
144 |
// Custom providers.
|
|
|
145 |
$hook = new \customfield_number\hook\add_custom_providers($field);
|
|
|
146 |
|
|
|
147 |
// Dispatch the hook and collect custom providers.
|
|
|
148 |
\core\di::get(\core\hook\manager::class)->dispatch($hook);
|
|
|
149 |
|
|
|
150 |
$allproviders = array_merge($allproviders, $hook->get_providers());
|
|
|
151 |
|
|
|
152 |
return array_filter($allproviders, fn($p) => $p->is_available());
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
/**
|
|
|
156 |
* Validate the data on the field configuration form
|
|
|
157 |
*
|
|
|
158 |
* Providers can override it
|
|
|
159 |
*
|
|
|
160 |
* @param array $data
|
|
|
161 |
* @param array $files
|
|
|
162 |
* @return array associative array of error messages
|
|
|
163 |
*/
|
|
|
164 |
public function config_form_validation(array $data, array $files = []): array {
|
|
|
165 |
return [];
|
|
|
166 |
}
|
|
|
167 |
}
|