| 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 |
* Widgets extend class for new widgets.
|
|
|
19 |
*
|
|
|
20 |
* @package block_dash
|
|
|
21 |
* @copyright 2022 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\widget;
|
|
|
26 |
|
|
|
27 |
use block_dash\local\data_source\abstract_data_source;
|
|
|
28 |
use block_dash\local\data_source\data_source_interface;
|
|
|
29 |
use block_dash\local\data_source\form\preferences_form;
|
|
|
30 |
use block_dash\local\dash_framework\query_builder\builder;
|
|
|
31 |
use block_dash\local\data_grid\filter\filter_collection;
|
|
|
32 |
use block_dash\local\layout\layout_interface;
|
|
|
33 |
use renderer_base;
|
|
|
34 |
use block_dash\local\paginator;
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Widgets extend class for new widgets.
|
|
|
38 |
*/
|
|
|
39 |
abstract class abstract_widget extends abstract_data_source implements data_source_interface, widget_interface, \templatable {
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* List of data to generate widget template content.
|
|
|
43 |
*
|
|
|
44 |
* @var array
|
|
|
45 |
*/
|
|
|
46 |
public $data = [];
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Check the datasource is widget.
|
|
|
50 |
*
|
|
|
51 |
* @var bool
|
|
|
52 |
*/
|
|
|
53 |
public $iswidget = true;
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Constructor.
|
|
|
59 |
*
|
|
|
60 |
* @param \context $context
|
|
|
61 |
*/
|
|
|
62 |
public function __construct(\context $context) {
|
|
|
63 |
parent::__construct($context);
|
|
|
64 |
$this->set_widget_layout();
|
|
|
65 |
$this->set_widget_preference();
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Set widget preferences
|
|
|
70 |
*
|
|
|
71 |
* @return void
|
|
|
72 |
*/
|
|
|
73 |
public function set_widget_preference() {
|
|
|
74 |
$preferences = $this->widget_preferences();
|
|
|
75 |
$this->set_preferences($preferences);
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Set the widget layout class.
|
|
|
80 |
*
|
|
|
81 |
* @return void
|
|
|
82 |
*/
|
|
|
83 |
public function set_widget_layout() {
|
|
|
84 |
$layout = $this->layout();
|
|
|
85 |
$this->set_layout($layout);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Fetch the widget data if supports the query method.
|
|
|
90 |
*
|
|
|
91 |
* @return array
|
|
|
92 |
*/
|
|
|
93 |
public function get_widget_data() {
|
|
|
94 |
global $PAGE;
|
|
|
95 |
$querydata = ($this->supports_query()) ? $this->get_query_template()->query() : [];
|
|
|
96 |
$this->data = $querydata;
|
|
|
97 |
$this->build_widget();
|
|
|
98 |
return $this->data;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Build widget data from child widget classes..
|
|
|
103 |
*
|
|
|
104 |
* @return array
|
|
|
105 |
*/
|
|
|
106 |
public function build_widget() {
|
|
|
107 |
return $this->data;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* Check the widget contains any data to render.
|
|
|
112 |
*
|
|
|
113 |
* @return bool
|
|
|
114 |
*/
|
|
|
115 |
public function is_empty() {
|
|
|
116 |
$this->build_widget();
|
|
|
117 |
return (empty($this->data)) ? true : false;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* Prefence form for widget. We make the fields disable other than the general.
|
|
|
122 |
*
|
|
|
123 |
* @param \moodleform $form
|
|
|
124 |
* @param \MoodleQuickForm $mform
|
|
|
125 |
* @return void
|
|
|
126 |
*/
|
|
|
127 |
public function build_preferences_form(\moodleform $form, \MoodleQuickForm $mform) {
|
|
|
128 |
if ($form->get_tab() == preferences_form::TAB_GENERAL) {
|
|
|
129 |
parent::build_preferences_form($form, $mform);
|
|
|
130 |
$element = $mform->getElement('config_preferences[layout]');
|
|
|
131 |
if ($element) {
|
|
|
132 |
$mform->removeElement('config_preferences[layout]');
|
|
|
133 |
}
|
|
|
134 |
} else {
|
|
|
135 |
$mform->addElement('html', get_string('fieldalert', 'block_dash'), 'fieldalert');
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
* Empty builder to make sure supports the datasource method.
|
|
|
141 |
*
|
|
|
142 |
* @return builder
|
|
|
143 |
*/
|
|
|
144 |
public function get_query_template(): builder {
|
|
|
145 |
global $USER;
|
|
|
146 |
$builder = new builder();
|
|
|
147 |
return $builder;
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
/**
|
|
|
151 |
* Filter conditions are added to badges preference report.
|
|
|
152 |
*
|
|
|
153 |
* @return filter_collection
|
|
|
154 |
*/
|
|
|
155 |
public function build_filter_collection() {
|
|
|
156 |
$filtercollection = new filter_collection(get_class($this), $this->get_context());
|
|
|
157 |
return $filtercollection;
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Set the data source supports debug.
|
|
|
162 |
* @return bool;
|
|
|
163 |
*/
|
|
|
164 |
public function supports_debug() {
|
|
|
165 |
return false;
|
|
|
166 |
}
|
|
|
167 |
/**
|
|
|
168 |
* Widget supports builder method. If new widget is not supports then widget should return as false.
|
|
|
169 |
*
|
|
|
170 |
* @return void
|
|
|
171 |
*/
|
|
|
172 |
public function supports_query() {
|
|
|
173 |
return false;
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
/**
|
|
|
177 |
* Confirm the groups datasource is widget.
|
|
|
178 |
*
|
|
|
179 |
* @return bool
|
|
|
180 |
*/
|
|
|
181 |
public function is_widget() {
|
|
|
182 |
return true;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
/**
|
|
|
186 |
* Is the widget needs to load the js when it the content updated using JS.
|
|
|
187 |
*
|
|
|
188 |
* @return bool
|
|
|
189 |
*/
|
|
|
190 |
public function supports_currentscript() {
|
|
|
191 |
return false;
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
/**
|
|
|
195 |
* Update the block fetched data before render.
|
|
|
196 |
*
|
|
|
197 |
* @param array $data
|
|
|
198 |
* @return void
|
|
|
199 |
*/
|
|
|
200 |
public function update_data_before_render(&$data) {
|
|
|
201 |
return null;
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
/**
|
|
|
205 |
* Get widget count based on the data. Define the steps to check the count of records.
|
|
|
206 |
*
|
|
|
207 |
* @return int
|
|
|
208 |
*/
|
|
|
209 |
public function widget_data_count() {
|
|
|
210 |
return 0;
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
/**
|
|
|
214 |
* Get table pagination class.
|
|
|
215 |
* @return paginator
|
|
|
216 |
*/
|
|
|
217 |
public function get_paginator(): paginator {
|
|
|
218 |
|
|
|
219 |
if ($this->get_layout()->supports_pagination()) {
|
|
|
220 |
$perpage = (int) $this->get_preferences('perpage');
|
|
|
221 |
}
|
|
|
222 |
$perpage = isset($perpage) && !empty($perpage) ? $perpage : \block_dash\local\paginator::PER_PAGE_DEFAULT;
|
|
|
223 |
|
|
|
224 |
if (!isset($this->paginator) || $this->paginator == null) {
|
|
|
225 |
$this->paginator = new paginator(function () {
|
|
|
226 |
return $this->widget_data_count();
|
|
|
227 |
}, 0, $perpage);
|
|
|
228 |
}
|
|
|
229 |
return $this->paginator;
|
|
|
230 |
}
|
|
|
231 |
}
|