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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
<?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/>.
namespace local_webhooks\local;
defined('MOODLE_INTERNAL') || die();
define('LW_TABLE_EVENTS', 'local_webhooks_events');
define('LW_TABLE_SERVICES', 'local_webhooks_service');
global $CFG;
require_once($CFG->dirroot . '/local/webhooks/classes/local/record.php');
use coding_exception;
use core_component;
use ReflectionClass;
use function define;
use function defined;
use function is_array;
use function is_int;
use function is_object;
use function is_string;
use function strlen;
/**
* The main class for the plugin.
*
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package local_webhooks\local
*/
final class api {
/**
* Create a new record in the database.
*
* @param \local_webhooks\local\record $record
*
* @return int
*
* @throws \coding_exception
* @throws \dml_exception
*/
public static function add_service(record $record): int {
global $DB;
$id = $DB->insert_record(LW_TABLE_SERVICES, $record);
if (is_int($id) && is_array($record->events)) {
$DB->insert_records(LW_TABLE_EVENTS, array_map(static function (string $name) use ($id) {
return ['name' => $name, 'serviceid' => $id];
}, $record->events));
}
if (!is_int($id)) {
throw new coding_exception('Variable \'id\' must be type \'Integer\'');
}
return $id;
}
/**
* Delete an existing record in the database.
*
* @param int $id
*
* @return bool
*
* @throws \dml_exception
*/
public static function delete_service(int $id): bool {
global $DB;
$DB->delete_records(LW_TABLE_EVENTS, ['serviceid' => $id]);
return $DB->delete_records(LW_TABLE_SERVICES, ['id' => $id]);
}
/**
* Get an event's list.
*
* @return array
*
* @throws \ReflectionException
*/
public static function get_events(): array {
return array_merge(self::get_core_events_list(), self::get_non_core_event_list());
}
/**
* Get an existing record from the database.
*
* @param int $id
*
* @return \local_webhooks\local\record
*
* @throws \dml_exception
*/
public static function get_service(int $id): record {
global $DB;
$record = $DB->get_record(LW_TABLE_SERVICES, ['id' => $id], '*', MUST_EXIST);
$events = $DB->get_records(LW_TABLE_EVENTS, ['serviceid' => $id]);
$service = new record();
$service->events = array_column($events, 'name');
$service->header = $record->header;
$service->id = $record->id;
$service->name = $record->name;
$service->point = $record->point;
$service->status = $record->status;
$service->token = $record->token;
return $service;
}
/**
* Get list records from the database.
*
* @param array|null $conditions
* @param string|null $sort
* @param int|null $from
* @param int|null $limit
*
* @return array
*
* @throws \dml_exception
*/
public static function get_services(array $conditions = null, string $sort = null, int $from = null, int $limit = null): array {
global $DB;
$records = $DB->get_records(LW_TABLE_SERVICES, $conditions ?? [], $sort ?? '', '*', $from ?? 0, $limit ?? 0);
$services = [];
foreach ($records as $record) {
if (!is_object($record)) {
continue;
}
$events = $DB->get_records(LW_TABLE_EVENTS, ['serviceid' => $record->id]);
$service = new record();
$service->events = array_column($events, 'name');
$service->header = $record->header;
$service->id = $record->id;
$service->name = $record->name;
$service->point = $record->point;
$service->status = $record->status;
$service->token = $record->token;
$services[] = $service;
}
return $services;
}
/**
* Get list records from the database by the event's name.
*
* @param string $name
*
* @return \local_webhooks\local\record[]
*
* @throws \dml_exception
*/
public static function get_services_by_event(string $name): array {
global $DB;
$events = $DB->get_records(LW_TABLE_EVENTS, ['name' => $name]);
$services = [];
foreach ($events as $event) {
if (!is_object($event)) {
continue;
}
$services[] = self::get_service($event->serviceid);
}
return $services;
}
/**
* Get a total count of existing records.
*
* @return int
*
* @throws \dml_exception
*/
public static function get_total_count(): int {
global $DB;
return $DB->count_records(LW_TABLE_SERVICES);
}
/**
* Update an existing record in the database.
*
* @param \local_webhooks\local\record $service
*
* @return bool
*
* @throws \coding_exception
* @throws \dml_exception
*/
public static function set_service(record $service): bool {
global $DB;
$result = $DB->update_record(LW_TABLE_SERVICES, $service);
$DB->delete_records(LW_TABLE_EVENTS, ['serviceid' => $service->id]);
if ($result && is_array($service->events)) {
$DB->insert_records(LW_TABLE_EVENTS, array_map(static function (string $name) use ($service) {
return ['name' => $name, 'serviceid' => $service->id];
}, $service->events));
}
return $result;
}
/**
* Get a system's events list.
*
* @return array
*
* @throws \ReflectionException
*/
private static function get_core_events_list(): array {
global $CFG;
$debugdeveloper = $CFG->debugdeveloper;
$debugdisplay = $CFG->debugdisplay;
$debuglevel = $CFG->debug;
$CFG->debug = 0;
$CFG->debugdeveloper = false;
$CFG->debugdisplay = false;
$directory = $CFG->libdir . '/classes/event';
$files = self::get_file_list($directory);
if (isset($files['unknown_logged'])) {
unset($files['unknown_logged']);
}
$events = [];
foreach ($files as $file => $location) {
$name = '\\core\\event\\' . $file;
if (method_exists($name, 'get_static_info')) {
$class = new ReflectionClass($name);
if ($file !== 'manager' && !$class->isAbstract()) {
$events[$name] = $name::get_static_info();
}
}
}
$CFG->debug = $debuglevel;
$CFG->debugdeveloper = $debugdeveloper;
$CFG->debugdisplay = $debugdisplay;
return $events;
}
/**
* Get a file's list in the directory.
*
* @param string $directory
*
* @return array
*/
private static function get_file_list(string $directory): array {
global $CFG;
$root = $CFG->dirroot;
$files = [];
if (is_dir($directory) && is_readable($directory)) {
$handle = opendir($directory);
if ($handle) {
foreach (scandir($directory, SCANDIR_SORT_NONE) as $file) {
if (!is_string($file)) {
continue;
}
if ($file !== '.' && $file !== '..' && strrpos($directory, $root) !== false) {
$location = substr($directory, strlen($root));
$eventname = substr($file, 0, -4);
if (is_string($eventname)) {
$files[$eventname] = $location . '/' . $file;
}
}
}
}
}
return $files;
}
/**
* Get a plugins' events list.
*
* @return array
*
* @throws \ReflectionException
*/
private static function get_non_core_event_list(): array {
global $CFG;
$debugdeveloper = $CFG->debugdeveloper;
$debugdisplay = $CFG->debugdisplay;
$debuglevel = $CFG->debug;
$CFG->debug = 0;
$CFG->debugdeveloper = false;
$CFG->debugdisplay = false;
$events = [];
foreach (core_component::get_plugin_types() as $type => $unused) {
foreach (core_component::get_plugin_list($type) as $plugin => $directory) {
$directory .= '/classes/event';
$files = self::get_file_list($directory);
if (isset($files['unknown_logged'])) {
unset($files['unknown_logged']);
}
foreach ($files as $file => $location) {
$name = '\\' . $type . '_' . $plugin . '\\event\\' . $file;
if (method_exists($name, 'get_static_info')) {
$class = new ReflectionClass($name);
if ($type . '_' . $plugin !== 'logstore_legacy' && !$class->isAbstract()) {
$events[$name] = $name::get_static_info();
}
}
}
}
}
$CFG->debug = $debuglevel;
$CFG->debugdeveloper = $debugdeveloper;
$CFG->debugdisplay = $debugdisplay;
return $events;
}
}
|