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 layouts 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\layout;
|
|
|
26 |
|
|
|
27 |
use block_dash\local\data_source\data_source_interface;
|
|
|
28 |
/**
|
|
|
29 |
* Responsible for creating layouts on request.
|
|
|
30 |
*
|
|
|
31 |
* @package block_dash
|
|
|
32 |
*/
|
|
|
33 |
class layout_factory implements layout_factory_interface {
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* @var array
|
|
|
37 |
*/
|
|
|
38 |
private static $layoutregistry;
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Build and get layout registry.
|
|
|
42 |
*
|
|
|
43 |
* @return array
|
|
|
44 |
*/
|
|
|
45 |
protected static function get_layout_registry() {
|
|
|
46 |
if (is_null(self::$layoutregistry)) {
|
|
|
47 |
self::$layoutregistry = [];
|
|
|
48 |
if ($pluginsfunction = get_plugins_with_function('register_layouts')) {
|
|
|
49 |
foreach ($pluginsfunction as $plugintype => $plugins) {
|
|
|
50 |
foreach ($plugins as $pluginfunction) {
|
|
|
51 |
foreach ($pluginfunction() as $layoutinfo) {
|
|
|
52 |
self::$layoutregistry[$layoutinfo['identifier']] = $layoutinfo;
|
|
|
53 |
}
|
|
|
54 |
}
|
|
|
55 |
}
|
|
|
56 |
}
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
return self::$layoutregistry;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Check if layout identifier exists.
|
|
|
64 |
*
|
|
|
65 |
* @param string $identifier
|
|
|
66 |
* @return bool
|
|
|
67 |
*/
|
|
|
68 |
public static function exists($identifier) {
|
|
|
69 |
return isset(self::get_layout_registry()[$identifier]);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Get layout info based on layout identifier.
|
|
|
74 |
*
|
|
|
75 |
* @param string $identifier
|
|
|
76 |
* @return array|null
|
|
|
77 |
*/
|
|
|
78 |
public static function get_layout_info($identifier) {
|
|
|
79 |
if (self::exists($identifier)) {
|
|
|
80 |
return self::get_layout_registry()[$identifier];
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
return null;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Get layout object with datasource.
|
|
|
88 |
*
|
|
|
89 |
* @param string $identifier
|
|
|
90 |
* @param data_source_interface $datasource
|
|
|
91 |
* @return data_source_interface
|
|
|
92 |
*/
|
|
|
93 |
public static function build_layout($identifier, data_source_interface $datasource) {
|
|
|
94 |
if (!self::exists($identifier)) {
|
|
|
95 |
return null;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
$layoutinfo = self::get_layout_info($identifier);
|
|
|
99 |
|
|
|
100 |
if (isset($layoutinfo['factory']) && $layoutinfo['factory'] != self::class) {
|
|
|
101 |
return $layoutinfo['factory']::build_layout($identifier, $datasource);
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
if (class_exists($identifier)) {
|
|
|
105 |
return new $identifier($datasource);
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
return null;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Get options array for select form fields.
|
|
|
113 |
*
|
|
|
114 |
* @return array
|
|
|
115 |
*/
|
|
|
116 |
public static function get_layout_form_options() {
|
|
|
117 |
$options = [];
|
|
|
118 |
|
|
|
119 |
foreach (self::get_layout_registry() as $identifier => $layoutinfo) {
|
|
|
120 |
$options[$identifier] = $layoutinfo['name'];
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
return $options;
|
|
|
124 |
}
|
|
|
125 |
}
|