forked from eLearning-TUDarmstadt/moodle-tool_cleanupusers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.php
285 lines (255 loc) · 12 KB
/
renderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Renderer for the Web interface of tool_cleanupusers.
*
* @package tool_cleanupusers
* @copyright 2016/17 N Herrmann
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use tool_cleanupusers\archiveduser;
use tool_cleanupusers\helper;
use tool_cleanupusers\not_archive_filter_form;
use tool_cleanupusers\archive_filter_form;
use tool_cleanupusers\plugininfo\userstatus;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->libdir . '/tablelib.php');
require_once(__DIR__ . '/classes/not_archive_filter_form.php');
require_once(__DIR__ . '/classes/archive_filter_form.php');
/**
* Class of the tool_cleanupusers renderer.
*
* @package tool_cleanupusers
* @copyright 2016/17 N Herrmann
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_cleanupusers_renderer extends plugin_renderer_base {
public function render_subplugin_table(): string {
global $OUTPUT, $CFG;
// display strings
$txt = get_strings(array('authenticationplugins',
'settings', 'edit', 'name', 'enable', 'disable',
'up', 'down', 'none'));
$txt->updown = "$txt->up/$txt->down";
$txt->authmethod = get_string('authmethod', 'tool_cleanupusers');
$txt->condition = get_string('condition', 'tool_cleanupusers');
$txt->deletetime = get_string('deletetime', 'tool_cleanupusers');
$txt->suspendtime = get_string('suspendtime', 'tool_cleanupusers');
$txt->deleteifneverloggedin = get_string('deleteifneverloggedin', 'tool_cleanupusers');
$authsavailable = core_plugin_manager::instance()->get_plugins_of_type('userstatus');
$authsenabled = userstatus::get_enabled_plugins();
if (!$authsenabled) {
$authsenabled = [];
}
// construct the display array, with enabled pluginname plugins at the top, in order
$displayauths = [];
foreach ($authsenabled as $auth) {
$displayauths[$auth] = $auth;
}
foreach ($authsavailable as $auth => $dir) {
if (array_key_exists($auth, $displayauths)) {
continue; //already in the list
}
$displayauths[$auth] = $dir->displayname;
}
$return = $OUTPUT->heading(get_string('actpluginshdr', 'tool_cleanupusers'), 3, 'main');
$return .= $OUTPUT->box_start('generalbox authsui');
$table = new html_table();
$table->head = array($txt->name, $txt->condition, $txt->authmethod,
$txt->suspendtime, $txt->deletetime, $txt->deleteifneverloggedin,
$txt->enable, $txt->updown, $txt->settings);
$table->colclasses = array('leftalign', 'leftalign', 'centeralign', 'centeralign', 'centeralign', 'centeralign', 'centeralign');
$table->data = array();
$table->attributes['class'] = 'admintable generaltable';
$table->id = 'manageauthtable';
// iterate through plugins and add to the display table
$updowncount = 1;
$authcount = count($authsenabled);
$url = "index.php?sesskey=" . sesskey();
foreach ($displayauths as $pluginname => $name) {
$class = '';
$mysubpluginname = "\\userstatus_" . $pluginname . "\\" . $pluginname;
if (!class_exists($mysubpluginname)) {
// core\notification::warning($pluginname . ' does not exist');
continue;
}
$userstatuschecker = new $mysubpluginname();
// displayname
$displayname = $userstatuschecker->get_displayname();
// hide/show link
if (in_array($pluginname, $authsenabled)) {
$hideshow = "<a href=\"$url&action=disable&userstatus=$pluginname\">";
$hideshow .= $OUTPUT->pix_icon('t/hide', get_string('disable')) . '</a>';
$enabled = true;
}
else {
$hideshow = "<a href=\"$url&action=enable&userstatus=$pluginname\">";
$hideshow .= $OUTPUT->pix_icon('t/show', get_string('enable')) . '</a>';
$enabled = false;
$class = 'dimmed_text';
}
// Condition
$condition = $userstatuschecker->get_condition_text();
// Authentication method
$auths = \get_enabled_auth_plugins();
$authvalues = $userstatuschecker->get_authentication_method();
// look for keys in enabled auth methods and convert to json
$authvalue_sarray = explode(',', $authvalues);
$keylist = [];
foreach ($authvalue_sarray as $key => $authvalue) {
$keylist[] = array_search($authvalue, $auths);
}
$strkeylist = json_encode($keylist);
$authmethod = $OUTPUT->render(\tool_cleanupusers\helper::render_auth_editable($pluginname,
$authvalues, $strkeylist));
// Time to suspend
if ($userstatuschecker->needs_suspendtime()) {
$timetosuspend = $OUTPUT->render(\tool_cleanupusers\helper::render_suspendtime_editable(
$pluginname, $userstatuschecker->get_suspendtime()));
} else {
// No suspendtime input
$timetosuspend = '';
}
// Time to delete
$timetodelete = $OUTPUT->render(\tool_cleanupusers\helper::render_deletetime_editable(
$pluginname, $userstatuschecker->get_deletetime()));
// deleteifneverloggedin
$neverloggendin = $userstatuschecker->delete_if_never_logged_in_on_suspendtime();
$neverloggendin = $OUTPUT->render(\tool_cleanupusers\helper::render_no_login_editiable(
$pluginname, $neverloggendin));
// up/down link (only if pluginname is enabled)
$updown = '';
if ($enabled) {
if ($updowncount > 1) {
$updown .= "<a href=\"$url&action=up&userstatus=$pluginname\">";
$updown .= $OUTPUT->pix_icon('t/up', get_string('moveup')) . '</a> ';
}
else {
$updown .= $OUTPUT->spacer() . ' ';
}
if ($updowncount < $authcount) {
$updown .= "<a href=\"$url&action=down&userstatus=$pluginname\">";
$updown .= $OUTPUT->pix_icon('t/down', get_string('movedown')) . '</a> ';
}
else {
$updown .= $OUTPUT->spacer() . ' ';
}
++ $updowncount;
}
// settings link
if (file_exists( __DIR__ . '/userstatus/'.$pluginname.'/settings.php')) {
$settings = "<a href=\"{$CFG->wwwroot}/admin/settings.php?section=cleanupusers_userstatus$pluginname\">{$txt->settings}</a>";
} else {
$settings = '';
}
// Add a row to the table.
$row = new html_table_row(array($displayname, $condition, $authmethod, $timetosuspend, $timetodelete,
$neverloggendin, $hideshow, $updown, $settings));
if ($class) {
$row->attributes['class'] = $class;
}
$table->data[] = $row;
}
$return .= html_writer::table($table);
$return .= $OUTPUT->box_end();
return $return;
}
/**
* Function expects four arrays and renders them to separate tables.
*
* @param array $userstoreactivate
* @param array $userstosuspend
* @param array $usertodelete
* @param array $usersneverloggedin
* @return string html
*/
public function render_preview_page($userstoreactivate, $userstosuspend, $usertodelete,
$checker) {
global $PAGE;
$returnurl = new \moodle_url('/admin/tool/cleanupusers/pending.php');
$limit = 15;
// Checks if one of the given arrays is empty to prevent rendering empty arrays.
// If not empty renders the information needed.
// Renders the information for each array in a separate table.
$output = '';
if (!empty($userstoreactivate)) {
$url = new \moodle_url('/admin/tool/cleanupusers/archiveusers.php',
['action' => archive_filter_form::TO_BE_REACTIVATED, 'checker' => $checker]);
$limitedarray = array_slice($userstoreactivate, 0, $limit, true);
$sqlfilter = helper::users_to_sql_filter($limitedarray, 'a');
$archivetable = new \tool_cleanupusers\table\archive_table(
'tool_cleanupusers_pending_reactivate_table',
$sqlfilter, [], "reactivate", [], $returnurl, $checker);
$output .= $this->output_table($archivetable, $limit, $userstoreactivate,
get_string('willbereactivated', 'tool_cleanupusers'), $url);
}
if (!empty($userstosuspend)) {
$url = new \moodle_url('/admin/tool/cleanupusers/toarchive.php',
['action' => not_archive_filter_form::TO_BE_ARCHIVED, 'checker' => $checker]);
$limitedarray = array_slice($userstosuspend, 0, $limit, true);
$archivetable = new \tool_cleanupusers\table\users_table(
'tool_cleanupusers_pending_suspend_table',
helper::users_to_sql_filter($limitedarray), [], $checker, $returnurl, $checker);
$output .= $this->output_table($archivetable, $limit, $userstosuspend,
get_string('willbesuspended', 'tool_cleanupusers'), $url);
}
if (!empty($usertodelete)) {
$url = new \moodle_url('/admin/tool/cleanupusers/archiveusers.php',
['action' => archive_filter_form::TO_BE_DELETED, 'checker' => $checker]);
$limitedarray = array_slice($usertodelete, 0, $limit, true);
$sqlfilter = helper::users_to_sql_filter($limitedarray, 'a');
$archivetable = new \tool_cleanupusers\table\archive_table(
'tool_cleanupusers_pending_delete_table',
$sqlfilter, [], "delete", [], $returnurl, $checker);
$output .= $this->output_table($archivetable, $limit, $usertodelete,
get_string('willbedeleted', 'tool_cleanupusers'), $url);
}
return $output;
}
/**
* Functions returns the heading for the tool_cleanupusers.
*
* @return string
*/
public function get_heading($text = '') {
if (!empty($text)) {
return $this->heading($text);
} else {
return $this->heading(get_string('pluginname', 'tool_cleanupusers'));
}
}
/**
* @param \tool_cleanupusers\table\users_table $archivetable
* @param int $limit
* @param array $userarray
* @param string $title
* @param moodle_url $url
* @return string
* @throws coding_exception
*/
private function output_table($archivetable, int $limit, array $userarray, string $title, moodle_url $url): string
{
global $PAGE;
$archivetable->define_baseurl($PAGE->url);
$output = \html_writer::tag('h5', $title);
$output .= $archivetable->get_content($limit);
if (count($userarray) > $limit) {
$output .= \html_writer::link($url,
'... ' . get_string('showcompletetable', 'tool_cleanupusers'));
}
return $output;
}
}