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
 * Container for structuring data, usually from a database. Supports PHP 8.1 and above.
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\data;
26
 
27
use context;
28
 
29
/**
30
 * Container for structuring data, usually from a database.
31
 *
32
 * @package block_dash
33
 */
34
class data_collection_new implements data_collection_interface, \ArrayAccess {
35
 
36
    /**
37
     * @var field_interface[]
38
     */
39
    private $data = [];
40
 
41
    /**
42
     * @var array type => [array of collections]
43
     */
44
    private $children = [];
45
 
46
    /**
47
     * @var context
48
     */
49
    private $context;
50
 
51
    /**
52
     * Check if data collection is active.
53
     *
54
     * @return bool
55
     */
56
    public function is_active() {
57
        global $PAGE;
58
 
59
        if ($PAGE->context && $context = $this->get_context()) {
60
            return $PAGE->context->id == $context->id;
61
        }
62
 
63
        return false;
64
    }
65
 
66
    /**
67
     * Set data context.
68
     * @param context $context
69
     */
70
    public function set_context(context $context) {
71
        $this->context = $context;
72
    }
73
 
74
    /**
75
     * Get data context.
76
     * @return context|null
77
     */
78
    public function get_context() {
79
        return $this->context;
80
    }
81
 
82
    /**
83
     * Get all fields in this data collection.
84
     *
85
     * @return field_interface[]
86
     */
87
    public function get_data() {
88
        return array_values($this->data);
89
    }
90
 
91
    /**
92
     * Add data to data collection.
93
     *
94
     * @param field_interface $field
95
     */
96
    public function add_data(field_interface $field) {
97
        $this->data[$field->get_name()] = $field;
98
    }
99
 
100
    /**
101
     * Add raw data to collection.
102
     *
103
     * @param array $data Associative array of data
104
     * @deprecated use add_data instead.
105
     * @throws \coding_exception
106
     */
107
    public function add_data_associative($data) {
108
        throw new \coding_exception('data_collection::add_data_associative is deprecated');
109
    }
110
 
111
    /**
112
     * Get child data collections.
113
     *
114
     * @param string $type Name of collection type to return. Null returns all.
115
     * @return data_collection_interface[]
116
     */
117
    public function get_child_collections($type = null) {
118
        if ($type) {
119
            if (isset($this->children[$type])) {
120
                return $this->children[$type];
121
            }
122
        } else {
123
            return array_values($this->children);
124
        }
125
 
126
        return [];
127
    }
128
 
129
    /**
130
     * Add a child data collection.
131
     *
132
     * @param string $type Name of collection type.
133
     * @param data_collection_interface $collection
134
     */
135
    public function add_child_collection($type, data_collection_interface $collection) {
136
        if (!isset($this->children[$type])) {
137
            $this->children[$type] = [];
138
        }
139
        $this->children[$type][] = $collection;
140
    }
141
 
142
    /**
143
     * Check if this collection contains any child collection of data.
144
     *
145
     * @return bool
146
     */
147
    public function has_child_collections() {
148
        return count($this->children) > 0;
149
    }
150
 
151
    /**
152
     * Get first child data collection.
153
     *
154
     * @return data_collection_interface
155
     */
156
    public function first_child() {
157
        if ($type = reset($this->children)) {
158
            return reset($type);
159
        }
160
 
161
        return null;
162
    }
163
 
164
    /**
165
     * Returns the first field of the data array.
166
     *
167
     * @return field_interface|null
168
     */
169
    public function first_data() {
170
        return isset(array_values($this->data)[0]) ? array_values($this->data)[0] : null;
171
    }
172
 
173
    /**
174
     * Returns the second field of the data array.
175
     *
176
     * @return field_interface|null
177
     */
178
    public function second_data() {
179
        return isset(array_values($this->data)[1]) ? array_values($this->data)[1] : null;
180
    }
181
 
182
    /**
183
     * Returns true if data collection has no data or child collections.
184
     *
185
     * @return bool
186
     */
187
    public function is_empty() {
188
        return empty($this->data) && !$this->has_child_collections();
189
    }
190
 
191
    /**
192
     * Whether a offset exists
193
     * @link https://php.net/manual/en/arrayaccess.offsetexists.php
194
     * @param mixed $offset <p>
195
     * An offset to check for.
196
     * </p>
197
     * @return boolean true on success or false on failure.
198
     * </p>
199
     * <p>
200
     * The return value will be casted to boolean if non-boolean was returned.
201
     * @since 5.0.0
202
     */
203
    public function offsetExists($offset) : bool {
204
        if ($offset == 'data') {
205
            return true;
206
        }
207
        return isset($this->data[$offset]) || isset($this->children[$offset]);
208
    }
209
 
210
    /**
211
     * Offset to retrieve
212
     * @link https://php.net/manual/en/arrayaccess.offsetget.php
213
     * @param mixed $offset <p>
214
     * The offset to retrieve.
215
     * </p>
216
     * @return mixed Can return all value types.
217
     * @since 5.0.0
218
     */
219
    public function offsetGet(mixed $offset): mixed {
220
        if ($offset == 'data') {
221
            return $this->get_data();
222
        }
223
 
224
        if (isset($this->data[$offset])) {
225
            return $this->data[$offset]->get_value();
226
        } else {
227
            return $this->children[$offset];
228
        }
229
    }
230
 
231
    /**
232
     * Offset to set
233
     * @link https://php.net/manual/en/arrayaccess.offsetset.php
234
     * @param mixed $offset <p>
235
     * The offset to assign the value to.
236
     * </p>
237
     * @param mixed $value <p>
238
     * The value to set.
239
     * </p>
240
     * @return void
241
     * @since 5.0.0
242
     */
243
    public function offsetSet(mixed $offset, mixed $value): void {
244
        throw new \coding_exception('Setting data not supported with array access.');
245
    }
246
 
247
    /**
248
     * Offset to unset
249
     * @link https://php.net/manual/en/arrayaccess.offsetunset.php
250
     * @param mixed $offset <p>
251
     * The offset to unset.
252
     * </p>
253
     * @return void
254
     * @since 5.0.0
255
     */
256
    public function offsetUnset(mixed $offset): void {
257
        throw new \coding_exception('Unsetting data not supported with array access.');
258
    }
259
}