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