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 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
 * Represents a predefined field that can be added to a data grid.
19
 *
20
 * @package    block_dash
21
 * @copyright  2019 bdecent gmbh <https://bdecent.de>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace block_dash\local\data_grid\field;
26
 
27
use block_dash\local\data_grid\field\attribute\field_attribute_interface;
28
use block_dash\local\data_grid\field\field_definition_interface;
29
 
30
/**
31
 * Represents a predefined field that can be added to a data grid.
32
 *
33
 * Add basic functionality for field definitions.
34
 *
35
 * @package block_dash
36
 */
37
abstract class abstract_field_definition implements field_definition_interface {
38
 
39
    /**
40
     * @var string Unique name of field (e.g. u_firstname).
41
     */
42
    private $name;
43
 
44
    /**
45
     * @var string String identifier of human readable name of field (e.g. Firstname).
46
     */
47
    private $title;
48
 
49
    /**
50
     * @var int Visibility of the field (if it should be displayed to the user).
51
     */
52
    private $visibility;
53
 
54
    /**
55
     * @var array Arbitrary options belonging to this field.
56
     */
57
    private $options = [];
58
 
59
    /**
60
     * @var field_attribute_interface[]
61
     */
62
    private $attributes = [];
63
 
64
    /**
65
     * @var bool If field should be sorted.
66
     */
67
    private $sort = false;
68
 
69
    /**
70
     * @var string Direction of sort, if sorting.
71
     */
72
    private $sortdirection = 'asc';
73
 
74
    /**
75
     * @var string Optional sort select (ORDER BY <select>), useful for fields that can't sort based on their field name.
76
     */
77
    private $sortselect;
78
 
79
    /**
80
     * Constructor.
81
     *
82
     * @param string $name String identifier of human readable name of field (e.g. Firstname).
83
     * @param string $title String identifier of human readable name of field (e.g. Firstname).
84
     * @param int $visibility Visibility of the field (if it should be displayed to the user).
85
     * @param array $options Arbitrary options belonging to this field.
86
     */
87
    public function __construct($name, $title, $visibility = self::VISIBILITY_VISIBLE, $options = []) {
88
        $this->name = $name;
89
        $this->title = $title;
90
        $this->visibility = $visibility;
91
        $this->options = $options;
92
    }
93
 
94
    /**
95
     * After records are relieved from database each field has a chance to transform the data.
96
     * Example: Convert unix timestamp into a human readable date format
97
     *
98
     * @param mixed $data Raw data associated with this field definition.
99
     * @param \stdClass $record Full record from database.
100
     * @return mixed
101
     */
102
    final public function transform_data($data, \stdClass $record) {
103
        foreach ($this->attributes as $attribute) {
104
            $data = $attribute->transform_data($data, $record);
105
        }
106
 
107
        return $data;
108
    }
109
 
110
    // Region Property methods.
111
 
112
    /**
113
     * Get unique field name.
114
     *
115
     * @return string
116
     */
117
    public function get_name() {
118
        return $this->name;
119
    }
120
 
121
    /**
122
     * Get field title.
123
     *
124
     * @return string
125
     */
126
    public function get_title() {
127
        return $this->title;
128
    }
129
 
130
    /**
131
     * Override field title.
132
     *
133
     * @param string $title
134
     */
135
    public function set_title($title) {
136
        $this->title = $title;
137
    }
138
 
139
    /**
140
     * Get field visibility.
141
     *
142
     * @return int
143
     */
144
    public function get_visibility() {
145
        return $this->visibility;
146
    }
147
 
148
    /**
149
     * Set field visibility.
150
     *
151
     * @param int $visibility
152
     */
153
    public function set_visibility($visibility) {
154
        // Warn the developer if they have used an invalid visibility.
155
        // ...@ codeCoverageIgnoreStart.
156
        if (!in_array($visibility, [self::VISIBILITY_HIDDEN, self::VISIBILITY_VISIBLE])) {
157
            debugging('Invalid visibility set on field ' . get_class($this) . ': ' . $visibility, DEBUG_DEVELOPER);
158
            // So the application doesn't break, default to visible.
159
            $visibility = self::VISIBILITY_VISIBLE;
160
        }
161
        // ...@ codeCoverageIgnoreEnd.
162
        $this->visibility = $visibility;
163
    }
164
 
165
    // Endregion.
166
 
167
    // Region Attributes.
168
 
169
    /**
170
     * Add attribute to this field definition.
171
     *
172
     * @param field_attribute_interface $attribute
173
     */
174
    public function add_attribute(field_attribute_interface $attribute) {
175
        $attribute->set_field_definition($this);
176
        $this->attributes[] = $attribute;
177
    }
178
 
179
    /**
180
     * Remove attribute to this field definition.
181
     *
182
     * @param field_attribute_interface $attribute
183
     */
184
    public function remove_attribute(field_attribute_interface $attribute) {
185
        foreach ($this->attributes as $key => $searchattribute) {
186
            if ($searchattribute === $attribute) {
187
                unset($this->attributes[$key]);
188
            }
189
        }
190
    }
191
 
192
    /**
193
     * Get all attributes associated with this field definition.
194
     *
195
     * @return field_attribute_interface[]
196
     */
197
    public function get_attributes() {
198
        return array_values($this->attributes);
199
    }
200
 
201
    /**
202
     * Check if field has an attribute type.
203
     *
204
     * @param string $classname Full class path to attribute
205
     * @return bool
206
     */
207
    public function has_attribute($classname) {
208
        foreach ($this->get_attributes() as $attribute) {
209
            if (get_class($attribute) == $classname) {
210
                return true;
211
            }
212
        }
213
 
214
        return false;
215
    }
216
 
217
    // Endregion.
218
 
219
    // Region Options.
220
 
221
    /**
222
     * Get a single option.
223
     *
224
     * @param string $name
225
     * @return mixed|null
226
     */
227
    public function get_option($name) {
228
        return isset($this->options[$name]) ? $this->options[$name] : null;
229
    }
230
 
231
    /**
232
     * Set option on field.
233
     *
234
     * @param string $name
235
     * @param string $value
236
     */
237
    public function set_option($name, $value) {
238
        $this->options[$name] = $value;
239
    }
240
 
241
    /**
242
     * Set options on field.
243
     *
244
     * @param array $options
245
     */
246
    public function set_options($options) {
247
        foreach ($options as $name => $value) {
248
            $this->set_option($name, $value);
249
        }
250
    }
251
 
252
    /**
253
     * Get all options for this field.
254
     *
255
     * @return array
256
     */
257
    public function get_options() {
258
        return $this->options;
259
    }
260
 
261
    // Endregion.
262
 
263
    // Region Sorting.
264
 
265
    /**
266
     * Set if field should be sorted.
267
     *
268
     * @param bool $sort
269
     * @throws \Exception
270
     */
271
    public function set_sort($sort) {
272
        if (!is_bool($sort)) {
273
            throw new \Exception('Sort expected to be a bool.');
274
        }
275
 
276
        $this->sort = $sort;
277
    }
278
 
279
    /**
280
     * Is the field sorted.
281
     *
282
     * @return bool
283
     */
284
    public function get_sort() {
285
        return $this->sort;
286
    }
287
 
288
    /**
289
     * Set direction sort should happen for this field.
290
     *
291
     * @param string $direction
292
     * @throws \Exception
293
     */
294
    public function set_sort_direction($direction) {
295
        if (!in_array($direction, ['desc', 'asc'])) {
296
            throw new \Exception('Invalid sort direction: ' . $direction);
297
        }
298
        $this->sortdirection = $direction;
299
    }
300
 
301
    /**
302
     * Get sort direction.
303
     *
304
     * @return string
305
     */
306
    public function get_sort_direction() {
307
        return $this->sortdirection;
308
    }
309
 
310
    /**
311
     * Set optional sort select (ORDER BY <select>), useful for fields that can't sort based on their field name.
312
     *
313
     * @param string $select
314
     */
315
    public function set_sort_select($select) {
316
        $this->sortselect = $select;
317
    }
318
 
319
    /**
320
     * Return select for ORDER BY.
321
     *
322
     * @return string
323
     */
324
    public function get_sort_select() {
325
        if (!is_null($this->sortselect)) {
326
            return $this->sortselect;
327
        }
328
 
329
        return $this->get_name();
330
    }
331
 
332
    // Endregion.
333
 
334
    /**
335
     * Get custom form.
336
     *
337
     * @return string
338
     */
339
    public function get_custom_form() {
340
        $html = '<input type="hidden" name="available_field_definitions[' . $this->get_name()
341
            . '][enabled]" value="1">';
342
 
343
        $html .= '<input type="text" name="available_field_definitions[' . $this->get_name()
344
            . '][title_override]" placeholder="' . get_string('titleoverride', 'block_dash') . '"
345
            value="' . $this->get_title() . '">';
346
 
347
        return $html;
348
    }
349
 
350
    /**
351
     * When an object is cloned, PHP 5 will perform a shallow copy of all of the object's properties.
352
     * Any properties that are references to other variables, will remain references.
353
     * Once the cloning is complete, if a __clone() method is defined,
354
     * then the newly created object's __clone() method will be called, to allow any necessary properties that need to be changed.
355
     * NOT CALLABLE DIRECTLY.
356
     *
357
     * @return void
358
     * @link https://php.net/manual/en/language.oop5.cloning.php
359
     */
360
    public function __clone() {
361
        // Update attribute references.
362
        foreach ($this->get_attributes() as $attribute) {
363
            $attribute->set_field_definition($this);
364
        }
365
    }
366
}