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\dash_framework\structure;
26
 
27
use block_dash\local\data_grid\field\attribute\field_attribute_interface;
28
use lang_string;
29
/**
30
 * Represents a predefined field that can be added to a data grid.
31
 *
32
 * Add basic functionality for field definitions.
33
 *
34
 * @package block_dash
35
 */
36
class field implements field_interface {
37
 
38
    /**
39
     * @var string The column name of the field as it appears in the table (e.g. firstname).
40
     */
41
    private $name;
42
 
43
    /**
44
     * @var lang_string Human readable name of field (e.g. Firstname).
45
     */
46
    private $title;
47
 
48
    /**
49
     * @var table
50
     */
51
    private $table;
52
 
53
    /**
54
     * @var string|array|null SQL select statement.
55
     * If left null the name will be used (table_alias.name).
56
     * An array of different selects based on dbtype is also possible ['select' => '', 'select_pgsql' => ''].
57
     */
58
    private $select;
59
 
60
    /**
61
     * @var int Visibility of the field (if it should be displayed to the user).
62
     */
63
    private $visibility;
64
 
65
    /**
66
     * @var array Arbitrary options belonging to this field.
67
     */
68
    private $options = [];
69
 
70
    /**
71
     * @var field_attribute_interface[]
72
     */
73
    private $attributes = [];
74
 
75
    /**
76
     * @var bool If field should be sorted.
77
     */
78
    private $sort = false;
79
 
80
    /**
81
     * @var string Direction of sort, if sorting.
82
     */
83
    private $sortdirection = 'asc';
84
 
85
    /**
86
     * @var string Optional sort select (ORDER BY <select>), useful for fields that can't sort based on their field name.
87
     */
88
    private $sortselect;
89
 
90
    /**
91
     * Constructor.
92
     *
93
     * @param string $name The column name of the field as it appears in the table (e.g. firstname).
94
     * @param lang_string $title Human readable name of field (e.g. Firstname).
95
     * @param table $table The table this field belongs to.
96
     * @param string|array|null $select SQL select statement.
97
     * If left null the name will be used (table_alias.name).
98
     * An array of different selects based on dbtype is also possible ['select' => '', 'select_pgsql' => ''].
99
     * @param array $attributes Field attributes to be added immediately.
100
     * @param array $options Arbitrary options belonging to this field.
101
     * @param int $visibility Visibility of the field (if it should be displayed to the user).
102
     * @param string $sortselect
103
     */
104
    public function __construct(string $name,
105
                                lang_string $title,
106
                                table $table,
107
                                $select = null,
108
                                array $attributes = [],
109
                                $options = [],
110
                                $visibility = self::VISIBILITY_VISIBLE,
111
                                $sortselect = null) {
112
        $this->name = $name;
113
        $this->title = $title;
114
        $this->table = $table;
115
        $this->select = $select;
116
        $this->visibility = $visibility;
117
        $this->options = $options;
118
        $this->sortselect = $sortselect;
119
 
120
        foreach ($attributes as $attribute) {
121
            $this->add_attribute($attribute);
122
        }
123
    }
124
 
125
    /**
126
     * After records are relieved from database each field has a chance to transform the data.
127
     * Example: Convert unix timestamp into a human readable date format
128
     *
129
     * @param mixed $data Raw data associated with this field definition.
130
     * @param \stdClass $record Full record from database.
131
     * @return mixed
132
     */
133
    final public function transform_data($data, \stdClass $record) {
134
        foreach ($this->attributes as $attribute) {
135
            $data = $attribute->transform_data($data, $record);
136
        }
137
 
138
        return $data;
139
    }
140
 
141
    // Region Property methods.
142
 
143
    /**
144
     * Get the column name of the field as it appears in the table (e.g. firstname).
145
     *
146
     * @return string
147
     */
148
    public function get_name(): string {
149
        return $this->name;
150
    }
151
 
152
    /**
153
     * Get field alias for query.
154
     *
155
     * @return string
156
     */
157
    public function get_alias(): string {
158
        return sprintf('%s_%s', $this->get_table()->get_alias(), $this->get_name());
159
    }
160
 
161
    /**
162
     * Human readable name of field (e.g. Firstname).
163
     *
164
     * @return lang_string
165
     */
166
    public function get_title(): lang_string {
167
        return $this->title;
168
    }
169
 
170
    /**
171
     * Override field title.
172
     *
173
     * @param lang_string $title
174
     */
175
    public function set_title(lang_string $title): void {
176
        $this->title = $title;
177
    }
178
 
179
    /**
180
     * Get table this field belongs to.
181
     *
182
     * @return table
183
     */
184
    public function get_table(): table {
185
        return $this->table;
186
    }
187
 
188
    /**
189
     * Get SQL select for this field, minus alias.
190
     *
191
     * @return string
192
     */
193
    public function get_select(): string {
194
        global $CFG;
195
 
196
        if (is_null($this->select)) {
197
            return sprintf('%s.%s', $this->get_table()->get_alias(), $this->get_name());
198
        }
199
 
200
        if (is_string($this->select)) {
201
            return $this->select;
202
        }
203
 
204
        if (isset($this->select['select_' . $CFG->dbtype])) {
205
            return $this->select['select_' . $CFG->dbtype];
206
        }
207
 
208
        return $this->select['select'];
209
    }
210
 
211
    /**
212
     * Get field visibility.
213
     *
214
     * @return int
215
     */
216
    public function get_visibility() {
217
        return $this->visibility;
218
    }
219
 
220
    /**
221
     * Set field visibility.
222
     *
223
     * @param int $visibility
224
     */
225
    public function set_visibility($visibility) {
226
        // Warn the developer if they have used an invalid visibility.
227
        // ...@ codeCoverageIgnoreStart.
228
        if (!in_array($visibility, [self::VISIBILITY_HIDDEN, self::VISIBILITY_VISIBLE])) {
229
            debugging('Invalid visibility set on field ' . get_class($this) . ': ' . $visibility, DEBUG_DEVELOPER);
230
            // So the application doesn't break, default to visible.
231
            $visibility = self::VISIBILITY_VISIBLE;
232
        }
233
        // ...@ codeCoverageIgnoreEnd.
234
        $this->visibility = $visibility;
235
    }
236
 
237
    // Endregion.
238
 
239
    // Region Attributes.
240
 
241
    /**
242
     * Add attribute to this field definition.
243
     *
244
     * @param field_attribute_interface $attribute
245
     */
246
    public function add_attribute(field_attribute_interface $attribute) {
247
        $attribute->set_field($this);
248
        $this->attributes[] = $attribute;
249
    }
250
 
251
    /**
252
     * Remove attribute to this field definition.
253
     *
254
     * @param field_attribute_interface $attribute
255
     */
256
    public function remove_attribute(field_attribute_interface $attribute) {
257
        foreach ($this->attributes as $key => $fsearchattribute) {
258
            if ($fsearchattribute === $attribute) {
259
                unset($this->attributes[$key]);
260
            }
261
        }
262
    }
263
 
264
    /**
265
     * Get all attributes associated with this field definition.
266
     *
267
     * @return field_attribute_interface[]
268
     */
269
    public function get_attributes() {
270
        return array_values($this->attributes);
271
    }
272
 
273
    /**
274
     * Check if field has an attribute type.
275
     *
276
     * @param string $classname Full class path to attribute
277
     * @return bool
278
     */
279
    public function has_attribute($classname) {
280
        foreach ($this->get_attributes() as $fattribute) {
281
            if (get_class($fattribute) == $classname) {
282
                return true;
283
            }
284
        }
285
 
286
        return false;
287
    }
288
 
289
    // Endregion.
290
 
291
    // Region Options.
292
 
293
    /**
294
     * Get a single option.
295
     *
296
     * @param string $optname
297
     * @return mixed|null
298
     */
299
    public function get_option($optname) {
300
        return isset($this->options[$optname]) ? $this->options[$optname] : null;
301
    }
302
 
303
    /**
304
     * Set option on field.
305
     *
306
     * @param string $name
307
     * @param string $optvalue
308
     */
309
    public function set_option($name, $optvalue) {
310
        $this->options[$name] = $optvalue;
311
    }
312
 
313
    /**
314
     * Set options on field.
315
     *
316
     * @param array $options
317
     */
318
    public function set_options($options) {
319
        foreach ($options as $optname => $value) {
320
            $this->set_option($optname, $value);
321
        }
322
    }
323
 
324
    /**
325
     * Get all options for this field.
326
     *
327
     * @return array
328
     */
329
    public function get_options() {
330
        return $this->options;
331
    }
332
 
333
    // Endregion.
334
 
335
    // Region Sorting.
336
 
337
    /**
338
     * Set if field should be sorted.
339
     *
340
     * @param bool $sort
341
     * @throws \Exception
342
     */
343
    public function set_sort($sort) {
344
 
345
        if (!is_bool($sort)) {
346
            throw new \Exception('Sort expected to be a boolean.');
347
        }
348
 
349
        $this->sort = $sort;
350
    }
351
 
352
    /**
353
     * Is the field sorted.
354
     *
355
     * @return bool
356
     */
357
    public function get_sort() {
358
        return $this->sort;
359
    }
360
 
361
    /**
362
     * Set direction sort should happen for this field.
363
     *
364
     * @param string $direction
365
     * @throws \Exception
366
     */
367
    public function set_sort_direction($direction) {
368
        if (!in_array($direction, ['desc', 'asc'])) {
369
            throw new \Exception('Invalid sort direction: ' . $direction);
370
        }
371
        $this->sortdirection = $direction;
372
    }
373
 
374
    /**
375
     * Get sort direction.
376
     *
377
     * @return string
378
     */
379
    public function get_sort_direction() {
380
        $dir = $this->sortdirection;
381
        return $dir;
382
    }
383
 
384
    /**
385
     * Set optional sort select (ORDER BY <select>), useful for fields that can't sort based on their field name.
386
     *
387
     * @param string $select
388
     */
389
    public function set_sort_select($select) {
390
        $this->sortselect = $select;
391
    }
392
 
393
    /**
394
     * Return select for ORDER BY.
395
     *
396
     * @return string
397
     */
398
    public function get_sort_select() {
399
        $sort = $this->sortselect;
400
        if (is_null($sort)) {
401
            return $this->get_select();
402
        }
403
        return empty($sort) ? $this->get_alias() : '';
404
    }
405
 
406
    // Endregion.
407
 
408
    /**
409
     * Get custom form.
410
     *
411
     * @return string
412
     */
413
    public function get_custom_form() {
414
        $html = '<input type="hidden" name="available_fields[' . $this->get_alias()
415
            . '][enabled]" value="1">';
416
 
417
        $html .= '<input type="text" name="available_fields[' . $this->get_alias()
418
            . '][title_override]" placeholder="' . get_string('titleoverride', 'block_dash') . '"
419
            value="' . $this->get_title() . '">';
420
 
421
        return $html;
422
    }
423
 
424
    /**
425
     * When an object is cloned, PHP 5 will perform a shallow copy of all of the object's properties.
426
     * Any properties that are references to other variables, will remain references.
427
     * Once the cloning is complete, if a __clone() method is defined,
428
     * then the newly created object's __clone() method will be called, to allow any necessary properties that need to be changed.
429
     * NOT CALLABLE DIRECTLY.
430
     *
431
     * @return void
432
     * @link https://php.net/manual/en/language.oop5.cloning.php
433
     */
434
    public function __clone() {
435
        // Update attribute references.
436
        foreach ($this->get_attributes() as $attribute) {
437
            $attribute->set_field($this);
438
        }
439
    }
440
}