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 |
* Responsible for creating data sources on request.
|
|
|
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_source;
|
|
|
26 |
|
|
|
27 |
use block_dash\local\data_custom\abstract_custom_type;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Responsible for creating data sources on request.
|
|
|
31 |
*
|
|
|
32 |
* @package block_dash
|
|
|
33 |
*/
|
|
|
34 |
class data_source_factory implements data_source_factory_interface {
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Cache registered data sources so they are only retrieved once.
|
|
|
38 |
*
|
|
|
39 |
* @var array
|
|
|
40 |
*/
|
|
|
41 |
private static $datasourceregistry;
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Register and return data registry.
|
|
|
45 |
*
|
|
|
46 |
* @return array
|
|
|
47 |
*/
|
|
|
48 |
protected static function get_data_source_registry() {
|
|
|
49 |
if (is_null(self::$datasourceregistry)) {
|
|
|
50 |
|
|
|
51 |
self::$datasourceregistry = [];
|
|
|
52 |
if ($pluginsfunction = get_plugins_with_function('register_data_sources')) {
|
|
|
53 |
foreach ($pluginsfunction as $plugintype => $plugins) {
|
|
|
54 |
foreach ($plugins as $pluginfunction) {
|
|
|
55 |
foreach ($pluginfunction() as $datasourceinfo) {
|
|
|
56 |
self::$datasourceregistry[$datasourceinfo['identifier']] = $datasourceinfo;
|
|
|
57 |
}
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
}
|
|
|
61 |
$crd = \core_component::get_component_classes_in_namespace(null, 'local\\block_dash');
|
|
|
62 |
foreach ($crd as $fullclassname => $classpath) {
|
|
|
63 |
if (is_subclass_of($fullclassname, abstract_data_source::class)) {
|
|
|
64 |
self::$datasourceregistry[$fullclassname] = [
|
|
|
65 |
'identifier' => $fullclassname,
|
|
|
66 |
'name' => abstract_data_source::get_name_from_class($fullclassname),
|
|
|
67 |
'help' => abstract_data_source::get_name_from_class($fullclassname, true),
|
|
|
68 |
];
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
if ($pluginsfunction = get_plugins_with_function('register_widget')) {
|
|
|
73 |
foreach ($pluginsfunction as $plugintype => $plugins) {
|
|
|
74 |
foreach ($plugins as $pluginfunction) {
|
|
|
75 |
foreach ($pluginfunction() as $callback) {
|
|
|
76 |
self::$datasourceregistry[$callback['identifier']] = $callback + ['type' => 'widget'];
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
}
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
// Attach the custom type of feature. For now its content.
|
|
|
83 |
$crd = \core_component::get_component_classes_in_namespace(null, 'local\\block_dash');
|
|
|
84 |
foreach ($crd as $fullclassname => $classpath) {
|
|
|
85 |
if (is_subclass_of($fullclassname, abstract_custom_type::class)) {
|
|
|
86 |
self::$datasourceregistry[$fullclassname] = [
|
|
|
87 |
'identifier' => $fullclassname,
|
|
|
88 |
'name' => abstract_data_source::get_name_from_class($fullclassname),
|
|
|
89 |
'help' => abstract_data_source::get_name_from_class($fullclassname, true),
|
|
|
90 |
'type' => 'custom',
|
|
|
91 |
];
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
return self::$datasourceregistry;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Check if data source identifier exists.
|
|
|
101 |
*
|
|
|
102 |
* @param string $identifier
|
|
|
103 |
* @return bool
|
|
|
104 |
*/
|
|
|
105 |
public static function exists($identifier) {
|
|
|
106 |
return isset(self::get_data_source_registry()[$identifier]);
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Get data source info.
|
|
|
111 |
*
|
|
|
112 |
* @param string $identifier
|
|
|
113 |
* @return array|null
|
|
|
114 |
*/
|
|
|
115 |
public static function get_data_source_info($identifier) {
|
|
|
116 |
if (self::exists($identifier)) {
|
|
|
117 |
return self::get_data_source_registry()[$identifier];
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
return null;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* Build data source.
|
|
|
125 |
*
|
|
|
126 |
* @param string $identifier
|
|
|
127 |
* @param \context $context
|
|
|
128 |
* @return data_source_interface
|
|
|
129 |
*/
|
|
|
130 |
public static function build_data_source($identifier, \context $context) {
|
|
|
131 |
if (!self::exists($identifier)) {
|
|
|
132 |
return null;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
$datasourceinfo = self::get_data_source_info($identifier);
|
|
|
136 |
|
|
|
137 |
if (isset($datasourceinfo['factory']) && $datasourceinfo['factory'] != self::class) {
|
|
|
138 |
return $datasourceinfo['factory']::build_data_source($identifier, $context);
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
if (!class_exists($identifier)) {
|
|
|
142 |
return null;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
return new $identifier($context);
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
/**
|
|
|
149 |
* Get options array for select form fields.
|
|
|
150 |
*
|
|
|
151 |
* @param string $type
|
|
|
152 |
* @return array
|
|
|
153 |
*/
|
|
|
154 |
public static function get_data_source_form_options($type='') {
|
|
|
155 |
$options = [];
|
|
|
156 |
foreach (self::get_data_source_registry() as $identifier => $datasourceinfo) {
|
|
|
157 |
if ($type) {
|
|
|
158 |
if (isset($datasourceinfo['type']) && $datasourceinfo['type'] == $type) {
|
|
|
159 |
$options[$identifier] = [
|
|
|
160 |
'name' => $datasourceinfo['name'],
|
|
|
161 |
'help' => isset($datasourceinfo['help']) ? $datasourceinfo['help'] : '',
|
|
|
162 |
];
|
|
|
163 |
}
|
|
|
164 |
} else {
|
|
|
165 |
if (!isset($datasourceinfo['type'])) {
|
|
|
166 |
$options[$identifier] = [
|
|
|
167 |
'name' => $datasourceinfo['name'],
|
|
|
168 |
'help' => isset($datasourceinfo['help']) ? $datasourceinfo['help'] : '',
|
|
|
169 |
];
|
|
|
170 |
}
|
|
|
171 |
}
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
return $options;
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
}
|