Compare commits
232
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
040d7540d7 | ||
|
|
9c2eefcaa5 | ||
|
|
98aecd35ce | ||
|
|
c42e13d182 | ||
|
|
7dc023b05a | ||
|
|
3a865453a4 | ||
|
|
747055f9d1 | ||
|
|
ed0ce52060 | ||
|
|
6b18cffc97 | ||
|
|
0cbc05d52b | ||
|
|
ae2f31261d | ||
|
|
46711c9942 | ||
|
|
e84ac6859a | ||
|
|
e550237b4b | ||
|
|
db2d98d210 | ||
|
|
5b0dba4980 | ||
|
|
d441f44698 | ||
|
|
cb6d5c7e13 | ||
|
|
da306feae3 | ||
|
|
9a1b781107 | ||
|
|
8bf6ed8122 | ||
|
|
e2627eaf86 | ||
|
|
eb4b9b69f8 | ||
|
|
bde135499d | ||
|
|
4806fad728 | ||
|
|
654eade42b | ||
|
|
3f50d21a7f | ||
|
|
08a54b0e9f | ||
|
|
6f6d50776c | ||
|
|
7ae31a6eee | ||
|
|
0fe3b14f29 | ||
|
|
0cdfc1b1cf | ||
|
|
a2bc4b735c | ||
|
|
0a44411f98 | ||
|
|
b8e61c44ac | ||
|
|
6c31017c8d | ||
|
|
00309285ba | ||
|
|
ad5fb98070 | ||
|
|
09c54f6627 | ||
|
|
a01ad5f97c | ||
|
|
d8f5be6670 | ||
|
|
99c835a63d | ||
|
|
14e5a16a33 | ||
|
|
1352b5ee79 | ||
|
|
f3b5ab5616 | ||
|
|
f28ff05598 | ||
|
|
7892ecc2fd | ||
|
|
5115697a62 | ||
|
|
68a1928a4c | ||
|
|
6e82b7cc19 | ||
|
|
36ddf415c4 | ||
|
|
67cf87d17c | ||
|
|
dc4bc38f80 | ||
|
|
73f45578b3 | ||
|
|
c18d7107b9 | ||
|
|
1701c9cf97 | ||
|
|
d0e7af84ff | ||
|
|
aae3ea70b2 | ||
|
|
16305051a9 | ||
|
|
5d942ccbb2 | ||
|
|
05ce3e0664 | ||
|
|
17b9cfde78 | ||
|
|
270c04a603 | ||
|
|
2194bc4a01 | ||
|
|
5884a58f54 | ||
|
|
472f85850a | ||
|
|
816110da62 | ||
|
|
295da67a0a | ||
|
|
7d2e288b70 | ||
|
|
fa86c0329f | ||
|
|
9ee0af7e28 | ||
|
|
9006e6b9dd | ||
|
|
2b791958a7 | ||
|
|
4833a44b51 | ||
|
|
d7185f5925 | ||
|
|
975c6f8d20 | ||
|
|
092d0436e8 | ||
|
|
27e7f48f36 | ||
|
|
41a6a89ac8 | ||
|
|
279fca5ed1 | ||
|
|
5036ef261c | ||
|
|
6aed41d675 | ||
|
|
25f1756835 | ||
|
|
490b8bb170 | ||
|
|
95c54f8262 | ||
|
|
6a83be960e | ||
|
|
82842e5ed8 | ||
|
|
98c33a639a | ||
|
|
98e5a6470d | ||
|
|
e8b59e7b0d | ||
|
|
46b6022269 | ||
|
|
2bd5e80b10 | ||
|
|
af4224e8db | ||
|
|
79fe101e1b | ||
|
|
b12c850a2f | ||
|
|
57a870f56f | ||
|
|
639276c33d | ||
|
|
0d9fe62ec6 | ||
|
|
2b7e419f8f | ||
|
|
adb9a1e69c | ||
|
|
2c013b3230 | ||
|
|
f3dc13ac91 | ||
|
|
4d69ca0da6 | ||
|
|
f220e057bd | ||
|
|
422cbfd57d | ||
|
|
e0a43273a3 | ||
|
|
cce50b316d | ||
|
|
55b1a6b5a9 | ||
|
|
6a711bfc48 | ||
|
|
9ada0cfe74 | ||
|
|
0a07c2ab22 | ||
|
|
0e94634c49 | ||
|
|
d29ee546a2 | ||
|
|
27fe9e412b | ||
|
|
9cf4fc575c | ||
|
|
f3c2ebbe4d | ||
|
|
8f25379f32 | ||
|
|
90897b0e7d | ||
|
|
685d9ee15e | ||
|
|
db500cddce | ||
|
|
85bcb64e37 | ||
|
|
1736128dbf | ||
|
|
5cdd8de9e3 | ||
|
|
b6ddc8bd40 | ||
|
|
f10786bfb0 | ||
|
|
899b129d23 | ||
|
|
dd19065313 | ||
|
|
3a2bddeea4 | ||
|
|
a41dbbb950 | ||
|
|
a68882140b | ||
|
|
43f0bf98d2 | ||
|
|
95c4cf061a | ||
|
|
a1099245b5 | ||
|
|
66b5928e6e | ||
|
|
4f75d9c5c8 | ||
|
|
29bdbc7456 | ||
|
|
ebf76795da | ||
|
|
53c49b69de | ||
|
|
f392fd42d9 | ||
|
|
67f8d73a15 | ||
|
|
22b7569f71 | ||
|
|
fa74622268 | ||
|
|
9b7426fb20 | ||
|
|
283c64f62e | ||
|
|
17ae01ab71 | ||
|
|
28e1169be8 | ||
|
|
2126708201 | ||
|
|
0041667754 | ||
|
|
91bf79f030 | ||
|
|
7d1a0b9fec | ||
|
|
51007a0fb5 | ||
|
|
2eaa622161 | ||
|
|
3d4b78bd8b | ||
|
|
41185c6db4 | ||
|
|
7e1284bfe2 | ||
|
|
785a541d1c | ||
|
|
6e3bf072af | ||
|
|
2e57e732ba | ||
|
|
499f36d118 | ||
|
|
49516bea82 | ||
|
|
a6c6081139 | ||
|
|
9560267833 | ||
|
|
2057a1efb6 | ||
|
|
0cbf2bbb87 | ||
|
|
3e054dd94e | ||
|
|
54fb29c5ea | ||
|
|
78e9e435cc | ||
|
|
97711bb186 | ||
|
|
999ad9f472 | ||
|
|
82c7463e4a | ||
|
|
e0e9322644 | ||
|
|
7278ee951b | ||
|
|
01e2ba9f2f | ||
|
|
99868c158a | ||
|
|
bdaf1d35b2 | ||
|
|
7cfdf29600 | ||
|
|
bec8dcdc08 | ||
|
|
2ae949bbc3 | ||
|
|
de3c3a6553 | ||
|
|
05512c5440 | ||
|
|
91754489b0 | ||
|
|
f8b85a270c | ||
|
|
4bf6f61131 | ||
|
|
3da34ec7f1 | ||
|
|
4f342659d4 | ||
|
|
0bc809ba07 | ||
|
|
03d22ef096 | ||
|
|
1c87dae035 | ||
|
|
5639dc7b0f | ||
|
|
1ad4527c1d | ||
|
|
57cac60ab3 | ||
|
|
d4d105c018 | ||
|
|
3ea8f29329 | ||
|
|
c1077bc9c1 | ||
|
|
bc02d0dce2 | ||
|
|
93d788f046 | ||
|
|
29c12fa971 | ||
|
|
1e8cbe7f09 | ||
|
|
ad07800be9 | ||
|
|
f28b408b1e | ||
|
|
91fd140bd4 | ||
|
|
463aeaf043 | ||
|
|
97140dccf7 | ||
|
|
bf2cef3ea0 | ||
|
|
7b56375c05 | ||
|
|
b61e5a08be | ||
|
|
4a470056a7 | ||
|
|
688448b350 | ||
|
|
35358ea7cc | ||
|
|
97296b795a | ||
|
|
0fcec2738d | ||
|
|
e1372c6d34 | ||
|
|
098514660f | ||
|
|
07970f36e3 | ||
|
|
1e81988bf5 | ||
|
|
217c79d867 | ||
|
|
4c63223050 | ||
|
|
41a1acc36b | ||
|
|
9e1c2cca4e | ||
|
|
74c106aa87 | ||
|
|
700833b532 | ||
|
|
31ecf15e95 | ||
|
|
8afc94b51c | ||
|
|
2885dddccc | ||
|
|
0e4b7bc4d9 | ||
|
|
d6d13338e3 | ||
|
|
43ee70c01e | ||
|
|
6dfe28dab5 | ||
|
|
04e60b112b | ||
|
|
9e4c2dcbae | ||
|
|
1160a52753 | ||
|
|
af6ac9fb22 |
+31
-47
@@ -1,35 +1,41 @@
|
||||
language: php
|
||||
sudo: true
|
||||
|
||||
sudo: false
|
||||
addons:
|
||||
firefox: "latest-esr"
|
||||
postgresql: "9.6"
|
||||
|
||||
os: "linux"
|
||||
apt:
|
||||
packages:
|
||||
- "mysql-client-5.6"
|
||||
- "mysql-client-core-5.6"
|
||||
- "mysql-server-5.6"
|
||||
- "openjdk-8-jre-headless"
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- "$HOME/.composer/cache"
|
||||
- "$HOME/.npm"
|
||||
|
||||
php:
|
||||
- 5.6
|
||||
- 7.1
|
||||
- "7.0"
|
||||
- "7.1"
|
||||
- "7.2"
|
||||
|
||||
env:
|
||||
- DB=pgsql MOODLE_BRANCH=MOODLE_26_STABLE
|
||||
- DB=pgsql MOODLE_BRANCH=MOODLE_27_STABLE
|
||||
- DB=pgsql MOODLE_BRANCH=MOODLE_28_STABLE
|
||||
- DB=pgsql MOODLE_BRANCH=MOODLE_29_STABLE
|
||||
- DB=pgsql MOODLE_BRANCH=MOODLE_30_STABLE
|
||||
- DB=pgsql MOODLE_BRANCH=MOODLE_31_STABLE
|
||||
- DB=pgsql MOODLE_BRANCH=MOODLE_32_STABLE
|
||||
- DB=pgsql MOODLE_BRANCH=MOODLE_33_STABLE
|
||||
- DB=mysqli MOODLE_BRANCH=MOODLE_26_STABLE
|
||||
- DB=mysqli MOODLE_BRANCH=MOODLE_27_STABLE
|
||||
- DB=mysqli MOODLE_BRANCH=MOODLE_28_STABLE
|
||||
- DB=mysqli MOODLE_BRANCH=MOODLE_29_STABLE
|
||||
- DB=mysqli MOODLE_BRANCH=MOODLE_30_STABLE
|
||||
- DB=mysqli MOODLE_BRANCH=MOODLE_31_STABLE
|
||||
- DB=mysqli MOODLE_BRANCH=MOODLE_32_STABLE
|
||||
- DB=mysqli MOODLE_BRANCH=MOODLE_33_STABLE
|
||||
matrix:
|
||||
- MOODLE_BRANCH=MOODLE_35_STABLE DB=mysqli
|
||||
- MOODLE_BRANCH=MOODLE_35_STABLE DB=pgsql
|
||||
- MOODLE_BRANCH=MOODLE_36_STABLE DB=mysqli
|
||||
- MOODLE_BRANCH=MOODLE_36_STABLE DB=pgsql
|
||||
|
||||
before_install:
|
||||
- phpenv config-rm xdebug.ini
|
||||
- nvm install 8.9
|
||||
- nvm use 8.9
|
||||
- cd ../..
|
||||
- composer selfupdate
|
||||
- composer create-project -n --no-dev moodlerooms/moodle-plugin-ci ci ^1
|
||||
- composer create-project -n --no-dev --prefer-dist moodlerooms/moodle-plugin-ci ci ^2
|
||||
- export PATH="$(cd ci/bin; pwd):$(cd ci/vendor/bin; pwd):$PATH"
|
||||
|
||||
install:
|
||||
@@ -40,31 +46,9 @@ script:
|
||||
- moodle-plugin-ci phpcpd
|
||||
- moodle-plugin-ci phpmd
|
||||
- moodle-plugin-ci codechecker
|
||||
- moodle-plugin-ci csslint
|
||||
- moodle-plugin-ci shifter
|
||||
- moodle-plugin-ci jshint
|
||||
- moodle-plugin-ci validate
|
||||
- moodle-plugin-ci savepoints
|
||||
- moodle-plugin-ci mustache
|
||||
- moodle-plugin-ci grunt
|
||||
- moodle-plugin-ci phpunit
|
||||
- moodle-plugin-ci behat
|
||||
|
||||
jobs:
|
||||
include:
|
||||
stage: deploy
|
||||
|
||||
env:
|
||||
- DB=mysqli MOODLE_BRANCH=MOODLE_33_STABLE
|
||||
|
||||
php: 7.1
|
||||
|
||||
before_deploy:
|
||||
- cd $TRAVIS_REPO_SLUG
|
||||
- git stash --all
|
||||
- /bin/sh build.sh
|
||||
|
||||
deploy:
|
||||
provider: releases
|
||||
skip_cleanup: true
|
||||
file: "../local_webhooks.zip"
|
||||
email: "info@valentineus.link"
|
||||
api_key: $GITHUB_PROJECT_TOKEN
|
||||
on:
|
||||
tags: true
|
||||
@@ -20,27 +20,11 @@ Features:
|
||||
* Use a secret phrase to authenticate requests;
|
||||
* [JSON](https://en.wikipedia.org/wiki/JSON) - Format of outgoing data;
|
||||
|
||||
## Installation
|
||||
## Documentation
|
||||
|
||||
Get the installation package in any of the available methods:
|
||||
|
||||
* [GitHub Releases](https://github.com/valentineus/moodle-webhooks/releases).
|
||||
* [Compilation from the source code](#build).
|
||||
|
||||
## Build
|
||||
|
||||
Self-assembly package is as follows:
|
||||
|
||||
* Clone the repository:
|
||||
```bash
|
||||
git clone https://github.com/valentineus/moodle-webhooks.git moodle-webhooks
|
||||
```
|
||||
|
||||
* Run the build script:
|
||||
```bash
|
||||
cd ./moodle-webhooks
|
||||
/bin/sh build.sh
|
||||
```
|
||||
* [Install the plugin](docs/getting-started.md#installation).
|
||||
* [User guide](docs/getting-started.md#user-guide).
|
||||
* [Request format](docs/getting-started.md#request-format).
|
||||
|
||||
## License
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
<?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\event;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
use core\event\base as event;
|
||||
use core\task\manager;
|
||||
use local_webhooks\task\notify;
|
||||
use function defined;
|
||||
|
||||
/**
|
||||
* Class observer.
|
||||
*
|
||||
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package local_webhooks\event
|
||||
*/
|
||||
final class observer {
|
||||
/**
|
||||
* Main handler for events.
|
||||
*
|
||||
* @param \core\event\base $event
|
||||
*/
|
||||
public static function handler(event $event) {
|
||||
$task = new notify();
|
||||
$task->set_custom_data($event->get_data());
|
||||
manager::queue_adhoc_task($task);
|
||||
}
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* The event handler.
|
||||
*
|
||||
* @package local_webhooks
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace local_webhooks;
|
||||
|
||||
defined("MOODLE_INTERNAL") || die();
|
||||
|
||||
require_once($CFG->libdir . "/filelib.php");
|
||||
|
||||
use curl;
|
||||
|
||||
/**
|
||||
* Defines how to work with events.
|
||||
*
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class events {
|
||||
/**
|
||||
* External handler.
|
||||
*
|
||||
* @param object $event
|
||||
*/
|
||||
public static function handler($event) {
|
||||
$enable = get_config("local_webhooks", "enable");
|
||||
|
||||
if (boolval($enable)) {
|
||||
$data = $event->get_data();
|
||||
self::transmitter($data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transmitter, processing event and services.
|
||||
*
|
||||
* @param array $data
|
||||
*/
|
||||
private static function transmitter($data) {
|
||||
global $DB;
|
||||
|
||||
$callbacks = $DB->get_recordset("local_webhooks_service");
|
||||
|
||||
if ($callbacks->valid()) {
|
||||
foreach ($callbacks as $callback) {
|
||||
self::handler_callback($data, $callback);
|
||||
}
|
||||
}
|
||||
|
||||
$callbacks->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes each callback.
|
||||
*
|
||||
* @param array $data
|
||||
* @param object $callback
|
||||
*/
|
||||
private static function handler_callback($data, $callback) {
|
||||
if ($callback->enable) {
|
||||
if (!empty($callback->events)) {
|
||||
$events = unserialize(gzuncompress(base64_decode($callback->events)));
|
||||
}
|
||||
|
||||
if (!empty($events[$data["eventname"]])) {
|
||||
if (boolval($callback->token)) {
|
||||
$data["token"] = $callback->token;
|
||||
}
|
||||
|
||||
self::send($data, $callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sending data to the node.
|
||||
*
|
||||
* @param array $data
|
||||
* @param object $callback
|
||||
*/
|
||||
private static function send($data, $callback) {
|
||||
$curl = new curl();
|
||||
$curl->setHeader(array("Content-Type: application/$callback->type"));
|
||||
$curl->post($callback->url, json_encode($data));
|
||||
return $curl->getResponse();
|
||||
}
|
||||
}
|
||||
@@ -1,115 +0,0 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Defines the form of editing the service.
|
||||
*
|
||||
* @package local_webhooks
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace local_webhooks;
|
||||
|
||||
defined("MOODLE_INTERNAL") || die();
|
||||
|
||||
require_once($CFG->libdir . "/formslib.php");
|
||||
|
||||
use report_eventlist_list_generator;
|
||||
use lang_string;
|
||||
use moodleform;
|
||||
|
||||
/**
|
||||
* Description editing form definition.
|
||||
*
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class service_edit_form extends moodleform {
|
||||
/**
|
||||
* @param string $baseurl
|
||||
*/
|
||||
public function __construct($baseurl) {
|
||||
parent::__construct($baseurl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpacks data for display.
|
||||
*
|
||||
* @param object $record
|
||||
*/
|
||||
public function set_data($record) {
|
||||
if (!empty($record->events)) {
|
||||
$record->events = unserialize(gzuncompress(base64_decode($record->events)));
|
||||
}
|
||||
|
||||
return parent::set_data($record);
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the standard structure of the form.
|
||||
*/
|
||||
protected function definition() {
|
||||
$mform =& $this->_form;
|
||||
|
||||
/* Form heading */
|
||||
$mform->addElement("header", "editserviceheader", new lang_string("service", "webservice"));
|
||||
|
||||
/* Name of the service */
|
||||
$mform->addElement("text", "title", new lang_string("name", "moodle"), array("size" => 60));
|
||||
$mform->addRule("title", null, "required");
|
||||
$mform->setType("title", PARAM_NOTAGS);
|
||||
|
||||
/* Callback address */
|
||||
$mform->addElement("text", "url", new lang_string("url", "moodle"), array("size" => 60));
|
||||
$mform->addRule("url", null, "required");
|
||||
$mform->setType("url", PARAM_URL);
|
||||
|
||||
/* Enabling the service */
|
||||
$mform->addElement("advcheckbox", "enable", new lang_string("enable", "moodle"));
|
||||
$mform->setType("enable", PARAM_BOOL);
|
||||
$mform->setDefault("enable", 1);
|
||||
$mform->setAdvanced("enable");
|
||||
|
||||
/* Token */
|
||||
$mform->addElement("text", "token", new lang_string("token", "webservice"), array("size" => 60));
|
||||
$mform->setType("token", PARAM_NOTAGS);
|
||||
|
||||
/* Content type */
|
||||
$mform->addElement("select", "type", "Content type", array("json" => "application/json", "x-www-form-urlencoded" => "application/x-www-form-urlencoded"));
|
||||
$mform->setAdvanced("type");
|
||||
|
||||
/* Form heading */
|
||||
$mform->addElement("header", "editserviceheaderevent", new lang_string("edulevel", "moodle"));
|
||||
|
||||
/* List of events */
|
||||
$eventlist = report_eventlist_list_generator::get_all_events_list(true);
|
||||
$events = array();
|
||||
|
||||
/* Formation of the list of elements */
|
||||
foreach ($eventlist as $event) {
|
||||
$events[$event["component"]][] =& $mform->createElement("checkbox", $event["eventname"], $event["eventname"]);
|
||||
}
|
||||
|
||||
/* Displays groups of items */
|
||||
foreach ($events as $key => $event) {
|
||||
$mform->addGroup($event, "events", $key, "<br />", true);
|
||||
}
|
||||
|
||||
/* Control Panel */
|
||||
$this->add_action_buttons(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,361 @@
|
||||
<?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 del_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';
|
||||
$events = [];
|
||||
$files = self::get_file_list($directory);
|
||||
|
||||
if (isset($files['unknown_logged'])) {
|
||||
unset($files['unknown_logged']);
|
||||
}
|
||||
|
||||
foreach (array_keys($files) as $file) {
|
||||
$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;
|
||||
|
||||
$files = [];
|
||||
$root = $CFG->dirroot;
|
||||
|
||||
if (is_dir($directory) && is_readable($directory)) {
|
||||
$handle = opendir($directory);
|
||||
|
||||
if ($handle) {
|
||||
foreach (scandir($directory, SCANDIR_SORT_NONE) as $file) {
|
||||
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 (array_keys(core_component::get_plugin_types()) as $type) {
|
||||
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 (array_keys($files) as $file) {
|
||||
$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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
<?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();
|
||||
|
||||
use stdClass;
|
||||
use function defined;
|
||||
|
||||
/**
|
||||
* It's a class description record.
|
||||
*
|
||||
* @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 record extends stdClass {
|
||||
/**
|
||||
* List of some events.
|
||||
*
|
||||
* @var array|null
|
||||
*/
|
||||
public $events;
|
||||
|
||||
/**
|
||||
* Type of the package.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $header;
|
||||
|
||||
/**
|
||||
* A unique identifier of the service.
|
||||
*
|
||||
* @var int|null
|
||||
*/
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* Name of the service.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* Url's an endpoint to send notifications.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $point;
|
||||
|
||||
/**
|
||||
* Status activity of the service.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $status;
|
||||
|
||||
/**
|
||||
* Secret key of the service.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $token;
|
||||
|
||||
/**
|
||||
* Classes constructor.
|
||||
*
|
||||
* @param array|null $conditions
|
||||
*/
|
||||
public function __construct(array $conditions = null) {
|
||||
$this->events = $conditions['events'] ?? null;
|
||||
$this->header = $conditions['header'] ?? null;
|
||||
$this->id = $conditions['id'] ?? null;
|
||||
$this->name = $conditions['name'] ?? null;
|
||||
$this->point = $conditions['point'] ?? null;
|
||||
$this->status = $conditions['status'] ?? null;
|
||||
$this->token = $conditions['token'] ?? null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
<?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\output;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
|
||||
require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php');
|
||||
require_once($CFG->libdir . '/tablelib.php');
|
||||
|
||||
use html_writer;
|
||||
use lang_string;
|
||||
use local_webhooks\local\api;
|
||||
use moodle_url;
|
||||
use pix_icon;
|
||||
use stdClass;
|
||||
use table_sql;
|
||||
use function defined;
|
||||
use function is_int;
|
||||
|
||||
/**
|
||||
* Class table for list services.
|
||||
*
|
||||
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package local_webhooks\output
|
||||
*/
|
||||
final class services_table extends table_sql {
|
||||
/**
|
||||
* URL editor page.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private static $editorpage = '/local/webhooks/service.php';
|
||||
|
||||
/**
|
||||
* URL main page.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private static $mainpage = '/local/webhooks/index.php';
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param string $uniqueid
|
||||
*
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public function __construct(string $uniqueid) {
|
||||
parent::__construct($uniqueid);
|
||||
|
||||
$this->define_table_columns();
|
||||
$this->define_table_configs();
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the action column.
|
||||
*
|
||||
* @param \stdClass $row
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \moodle_exception
|
||||
*/
|
||||
public function col_actions(stdClass $row): string {
|
||||
global $OUTPUT;
|
||||
|
||||
$deletelink = new moodle_url(self::$mainpage, ['deleteid' => $row->id, 'sesskey' => sesskey()]);
|
||||
$deleteitem = $OUTPUT->action_icon($deletelink, new pix_icon('t/delete', new lang_string('delete', 'moodle')));
|
||||
|
||||
$editlink = new moodle_url(self::$editorpage, ['serviceid' => $row->id, 'sesskey' => sesskey()]);
|
||||
$edititem = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', new lang_string('edit', 'moodle')));
|
||||
|
||||
$hideshowlink = new moodle_url(self::$mainpage, ['hideshowid' => $row->id, 'sesskey' => sesskey()]);
|
||||
$hideshowitem = $OUTPUT->action_icon($hideshowlink, new pix_icon($row->status ? 't/hide' : 't/show', $row->status
|
||||
? new lang_string('disable', 'moodle')
|
||||
: new lang_string('enable', 'moodle')
|
||||
));
|
||||
|
||||
return $hideshowitem . $edititem . $deleteitem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the events column.
|
||||
*
|
||||
* @param \stdClass $row
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function col_events(stdClass $row): int {
|
||||
$total = count($row->events);
|
||||
|
||||
return is_int($total) ? $total : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the name column.
|
||||
*
|
||||
* @param \stdClass $row
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws \moodle_exception
|
||||
*/
|
||||
public function col_name(stdClass $row): string {
|
||||
$link = new moodle_url(self::$editorpage, [
|
||||
'serviceid' => $row->id,
|
||||
'sesskey' => sesskey(),
|
||||
]);
|
||||
|
||||
return html_writer::link($link, $row->name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Config table's columns.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public function define_table_columns() {
|
||||
$this->define_columns([
|
||||
'name',
|
||||
'point',
|
||||
'events',
|
||||
'actions',
|
||||
]);
|
||||
|
||||
$this->define_headers([
|
||||
new lang_string('name', 'moodle'),
|
||||
new lang_string('url', 'moodle'),
|
||||
new lang_string('edulevel', 'moodle'),
|
||||
new lang_string('actions', 'moodle'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Config table.
|
||||
*/
|
||||
public function define_table_configs() {
|
||||
$this->collapsible(false);
|
||||
$this->is_downloadable(false);
|
||||
$this->no_sorting('actions');
|
||||
$this->no_sorting('events');
|
||||
$this->pageable(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* The query for the database.
|
||||
*
|
||||
* @param int $pagesize
|
||||
* @param bool $useinitialsbar
|
||||
*
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public function query_db($pagesize, $useinitialsbar = true) {
|
||||
$this->pagesize($pagesize, $pagesize + 1);
|
||||
|
||||
$sort = (string) $this->get_sql_sort();
|
||||
$this->rawdata = api::get_services(null, $sort, $this->get_page_start(), $this->get_page_size());
|
||||
|
||||
$total = api::get_total_count();
|
||||
$this->pagesize($pagesize, $total);
|
||||
|
||||
if ($useinitialsbar) {
|
||||
$this->initialbars($total > $pagesize);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
<?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\task;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
|
||||
require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php');
|
||||
|
||||
use core\task\adhoc_task;
|
||||
use curl;
|
||||
use local_webhooks\local\api;
|
||||
use local_webhooks\local\record;
|
||||
use function defined;
|
||||
use function is_object;
|
||||
use function is_string;
|
||||
|
||||
/**
|
||||
* Class for processing events.
|
||||
*
|
||||
* @package local_webhooks\task
|
||||
*/
|
||||
final class notify extends adhoc_task {
|
||||
/**
|
||||
* Debug information.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $debug;
|
||||
|
||||
/**
|
||||
* Process an event.
|
||||
*
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public function execute() {
|
||||
$event = $this->get_custom_data();
|
||||
|
||||
if (!is_object($event) || !isset($event->eventname) || !is_string($event->eventname)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (api::get_services_by_event($event->eventname) as $service) {
|
||||
if (!is_object($service)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$service->status) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->post($service, array_merge((array) $event, [
|
||||
'token' => $service->token,
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a request for the service.
|
||||
*
|
||||
* @param \local_webhooks\local\record $service
|
||||
* @param array $data
|
||||
*/
|
||||
public function post(record $service, array $data) {
|
||||
$curl = new curl();
|
||||
$curl->setHeader(['Content-Type: ' . $service->header]);
|
||||
$curl->post($service->point, json_encode($data));
|
||||
|
||||
if (defined('PHPUNIT_TEST') && PHPUNIT_TEST) {
|
||||
$this->debug = array_merge($this->debug ?? [], [
|
||||
compact('data', 'service'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Defines forms.
|
||||
*
|
||||
* @copyright 2018 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package local_webhooks
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once($CFG->dirroot . '/local/webhooks/lib.php');
|
||||
require_once($CFG->libdir . '/formslib.php');
|
||||
|
||||
/**
|
||||
* Description editing form definition.
|
||||
*
|
||||
* @copyright 2018 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package local_webhooks
|
||||
*/
|
||||
class local_webhooks_service_edit_form extends moodleform {
|
||||
/**
|
||||
* @param string $baseurl
|
||||
*/
|
||||
public function __construct($baseurl) {
|
||||
parent::__construct($baseurl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the standard structure of the form.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
protected function definition() {
|
||||
$mform =& $this->_form;
|
||||
$size = array('size' => 60);
|
||||
|
||||
/* Form heading */
|
||||
$mform->addElement('header', 'edit-service-header-main', new lang_string('service', 'webservice'));
|
||||
|
||||
/* Name of the service */
|
||||
$mform->addElement('text', 'name', new lang_string('name', 'moodle'), $size);
|
||||
$mform->addRule('name', null, 'required');
|
||||
$mform->setType('name', PARAM_RAW);
|
||||
|
||||
/* Callback address */
|
||||
$mform->addElement('text', 'point', new lang_string('url', 'moodle'), $size);
|
||||
$mform->addRule('point', null, 'required');
|
||||
$mform->setType('point', PARAM_URL);
|
||||
|
||||
/* Enabling the service */
|
||||
$mform->addElement('advcheckbox', 'status', new lang_string('enable', 'moodle'));
|
||||
$mform->setType('status', PARAM_BOOL);
|
||||
$mform->setDefault('status', 1);
|
||||
$mform->setAdvanced('status');
|
||||
|
||||
/* Token */
|
||||
$mform->addElement('text', 'token', new lang_string('token', 'webservice'), $size);
|
||||
$mform->addRule('token', null, 'required');
|
||||
$mform->setType('token', PARAM_RAW);
|
||||
|
||||
/* Content type */
|
||||
$contenttype = array(
|
||||
'application/json' => 'application/json',
|
||||
'application/x-www-form-urlencoded' => 'application/x-www-form-urlencoded',
|
||||
);
|
||||
|
||||
$mform->addElement('select', 'header', 'Content-Type', $contenttype);
|
||||
$mform->setAdvanced('header');
|
||||
|
||||
/* Form heading */
|
||||
$mform->addElement('header', 'edit-service-header-event', new lang_string('edulevel', 'moodle'));
|
||||
|
||||
/* List of events */
|
||||
$eventlist = report_eventlist_list_generator::get_all_events_list(true);
|
||||
|
||||
$events = array();
|
||||
foreach ($eventlist as $event) {
|
||||
$events[$event['component']][] =& $mform->createElement('checkbox', $event['eventname'], $event['eventname']);
|
||||
}
|
||||
|
||||
foreach ($events as $key => $event) {
|
||||
$mform->addGroup($event, 'events', $key, '<br />', true);
|
||||
}
|
||||
|
||||
/* Control Panel */
|
||||
$this->add_action_buttons(true);
|
||||
}
|
||||
}
|
||||
+9
-13
@@ -15,21 +15,17 @@
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Interception of all events in the system.
|
||||
* Registration of observers for events.
|
||||
*
|
||||
* @package local_webhooks
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined("MOODLE_INTERNAL") || die();
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$observers = array(
|
||||
array(
|
||||
"eventname" => "*",
|
||||
"callback" => "\local_webhooks\\events::handler",
|
||||
"includefile" => null,
|
||||
"priority" => 200,
|
||||
"internal" => true
|
||||
)
|
||||
);
|
||||
$observers = [
|
||||
[
|
||||
'callback' => '\local_webhooks\event\observer::handler',
|
||||
'eventname' => '*',
|
||||
],
|
||||
];
|
||||
+22
-8
@@ -1,19 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<XMLDB PATH="blocks/local_webhooks/db" VERSION="20171026" COMMENT="XMLDB file for Moodle" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd">
|
||||
<XMLDB PATH="local/webhooks/db" VERSION="2018090700" COMMENT="XMLDB file for Moodle local/webhooks"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd">
|
||||
|
||||
<TABLES>
|
||||
<TABLE NAME="local_webhooks_service" COMMENT="A table for storing callback services.">
|
||||
<TABLE NAME="local_webhooks_service" COMMENT="Storing created services.">
|
||||
<FIELDS>
|
||||
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" />
|
||||
<FIELD NAME="enable" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" />
|
||||
<FIELD NAME="title" TYPE="text" NOTNULL="true" SEQUENCE="false" />
|
||||
<FIELD NAME="url" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" />
|
||||
<FIELD NAME="type" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" />
|
||||
<FIELD NAME="token" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" />
|
||||
<FIELD NAME="events" TYPE="text" NOTNULL="false" SEQUENCE="false" />
|
||||
<FIELD NAME="header" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" DEFAULT="application/json" COMMENT="Type of outgoing header." />
|
||||
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="Name of the service." />
|
||||
<FIELD NAME="point" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="Point of delivery of notifications." />
|
||||
<FIELD NAME="status" TYPE="int" LENGTH="1" NOTNULL="true" SEQUENCE="false" DEFAULT="0" COMMENT="Current status of the service." />
|
||||
<FIELD NAME="token" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="Token for verification of requests." />
|
||||
</FIELDS>
|
||||
<KEYS>
|
||||
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
|
||||
</KEYS>
|
||||
</TABLE>
|
||||
|
||||
<TABLE NAME="local_webhooks_events" COMMENT="Storing of viewed events.">
|
||||
<FIELDS>
|
||||
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" />
|
||||
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="Name of the service." />
|
||||
<FIELD NAME="serviceid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Service ID." />
|
||||
</FIELDS>
|
||||
<KEYS>
|
||||
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
|
||||
</KEYS>
|
||||
</TABLE>
|
||||
|
||||
</TABLES>
|
||||
</XMLDB>
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* This file registers the plugin's external functions.
|
||||
*
|
||||
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$functions = [
|
||||
'local_webhooks_add_service' => [
|
||||
'classname' => 'local_webhooks_external',
|
||||
'classpath' => 'local/webhooks/externallib.php',
|
||||
'description' => 'Add a new service.',
|
||||
'methodname' => 'add_service',
|
||||
'type' => 'write',
|
||||
],
|
||||
|
||||
'local_webhooks_del_service' => [
|
||||
'classname' => 'local_webhooks_external',
|
||||
'classpath' => 'local/webhooks/externallib.php',
|
||||
'description' => 'Delete the existing service.',
|
||||
'methodname' => 'del_service',
|
||||
'type' => 'write',
|
||||
],
|
||||
|
||||
'local_webhooks_get_events' => [
|
||||
'classname' => 'local_webhooks_external',
|
||||
'classpath' => 'local/webhooks/externallib.php',
|
||||
'description' => 'Get the event\'s list.',
|
||||
'methodname' => 'get_events',
|
||||
'type' => 'read',
|
||||
],
|
||||
|
||||
'local_webhooks_get_service' => [
|
||||
'classname' => 'local_webhooks_external',
|
||||
'classpath' => 'local/webhooks/externallib.php',
|
||||
'description' => 'Get data by service.',
|
||||
'methodname' => 'get_service',
|
||||
'type' => 'read',
|
||||
],
|
||||
|
||||
'local_webhooks_get_services' => [
|
||||
'classname' => 'local_webhooks_external',
|
||||
'classpath' => 'local/webhooks/externallib.php',
|
||||
'description' => 'Get the service\'s list.',
|
||||
'methodname' => 'get_services',
|
||||
'type' => 'read',
|
||||
],
|
||||
|
||||
'local_webhooks_set_service' => [
|
||||
'classname' => 'local_webhooks_external',
|
||||
'classpath' => 'local/webhooks/externallib.php',
|
||||
'description' => 'Update the existing service.',
|
||||
'methodname' => 'set_service',
|
||||
'type' => 'write',
|
||||
],
|
||||
];
|
||||
@@ -0,0 +1,28 @@
|
||||
<?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/>.
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Function to upgrade 'local_webhooks'.
|
||||
*
|
||||
* @param int $oldversion
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function xmldb_local_webhooks_upgrade(int $oldversion) {
|
||||
return true;
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Service editor.
|
||||
*
|
||||
* @package local_webhooks
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
require_once(__DIR__ . "/../../config.php");
|
||||
require_once($CFG->libdir . "/adminlib.php");
|
||||
require_once(__DIR__ . "/classes/forms.php");
|
||||
|
||||
admin_externalpage_setup("managelocalplugins");
|
||||
|
||||
$serviceid = optional_param("serviceid", 0, PARAM_INT);
|
||||
|
||||
require_login();
|
||||
|
||||
/* Link generation */
|
||||
$urlparameters = array("serviceid" => $serviceid);
|
||||
$managerservice = new moodle_url("/local/webhooks/managerservice.php", $urlparameters);
|
||||
$baseurl = new moodle_url("/local/webhooks/editservice.php", $urlparameters);
|
||||
$PAGE->set_url($baseurl, $urlparameters);
|
||||
|
||||
/* Configure the context of the page */
|
||||
$context = context_system::instance();
|
||||
$PAGE->set_context($context);
|
||||
|
||||
/* Preparing a template for data */
|
||||
$titlepage = new lang_string("externalservice", "webservice");
|
||||
$servicerecord = new stdClass;
|
||||
|
||||
/* Create an editing form */
|
||||
$mform = new \local_webhooks\service_edit_form($PAGE->url);
|
||||
|
||||
/* Cancel processing */
|
||||
if ($mform->is_cancelled()) {
|
||||
redirect($managerservice);
|
||||
}
|
||||
|
||||
/* Getting the data */
|
||||
if ($idediting = boolval($serviceid)) {
|
||||
$servicerecord = $DB->get_record("local_webhooks_service", array("id" => $serviceid), "*", MUST_EXIST);
|
||||
$mform->set_data($servicerecord);
|
||||
}
|
||||
|
||||
/* Processing of received data */
|
||||
if ($data = $mform->get_data()) {
|
||||
if (!empty($data->events)) {
|
||||
$data->events = base64_encode(gzcompress(serialize($data->events), 9));
|
||||
}
|
||||
|
||||
if (boolval($idediting)) {
|
||||
$data->id = $serviceid;
|
||||
$DB->update_record("local_webhooks_service", $data);
|
||||
redirect($managerservice, new lang_string("eventwebserviceserviceupdated", "webservice"));
|
||||
} else {
|
||||
$DB->insert_record("local_webhooks_service", $data);
|
||||
redirect($managerservice, new lang_string("eventwebserviceservicecreated", "webservice"));
|
||||
}
|
||||
}
|
||||
|
||||
/* Page template */
|
||||
$PAGE->set_pagelayout("admin");
|
||||
$PAGE->set_heading($titlepage);
|
||||
$PAGE->set_title($titlepage);
|
||||
|
||||
/* The page title */
|
||||
$PAGE->navbar->add(new lang_string("pluginname", "local_webhooks"), new moodle_url("/admin/settings.php", array("section" => "local_webhooks")));
|
||||
$PAGE->navbar->add(new lang_string("externalservices", "webservice"), $managerservice);
|
||||
$PAGE->navbar->add($titlepage);
|
||||
echo $OUTPUT->header();
|
||||
|
||||
/* Displays the form */
|
||||
$mform->display();
|
||||
|
||||
echo $OUTPUT->footer();
|
||||
+366
@@ -0,0 +1,366 @@
|
||||
<?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/>.
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
|
||||
require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php');
|
||||
require_once($CFG->libdir . '/externallib.php');
|
||||
|
||||
use local_webhooks\local\api;
|
||||
use local_webhooks\local\record;
|
||||
|
||||
/**
|
||||
* WebHooks external functions.
|
||||
*
|
||||
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
final class local_webhooks_external extends external_api {
|
||||
/**
|
||||
* Add a new service.
|
||||
*
|
||||
* @param array $conditions
|
||||
*
|
||||
* @return int
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public static function add_service(array $conditions): int {
|
||||
$parameters = self::validate_parameters(self::add_service_parameters(), [
|
||||
'record' => array_filter($conditions),
|
||||
]);
|
||||
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
$parameters = array_filter($parameters['record']);
|
||||
$record = new record($parameters);
|
||||
|
||||
return api::add_service($record);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method parameters.
|
||||
*
|
||||
* @return \external_function_parameters
|
||||
*/
|
||||
public static function add_service_parameters(): external_function_parameters {
|
||||
return new external_function_parameters([
|
||||
'record' => new external_single_structure([
|
||||
'events' => new external_multiple_structure(
|
||||
new external_value(PARAM_RAW, 'The event\'s name.'), 'The service\'s list events.', false
|
||||
),
|
||||
'header' => new external_value(PARAM_RAW, 'The request\'s header or type', false, 'application/json'),
|
||||
'name' => new external_value(PARAM_RAW, 'The service\'s name.'),
|
||||
'point' => new external_value(PARAM_URL, 'The service\'s endpoint.'),
|
||||
'status' => new external_value(PARAM_BOOL, 'The service\'s status.', false, true),
|
||||
'token' => new external_value(PARAM_RAW, 'The service\'s secret key.'),
|
||||
], 'The new service\'s data.'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method result value.
|
||||
*
|
||||
* @return \external_value
|
||||
*/
|
||||
public static function add_service_returns(): external_value {
|
||||
return new external_value(PARAM_INT, 'The service\'s ID.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the existing service.
|
||||
*
|
||||
* @param int $serviceid
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public static function del_service(int $serviceid): bool {
|
||||
$parameters = self::validate_parameters(self::del_service_parameters(), [
|
||||
'serviceid' => $serviceid,
|
||||
]);
|
||||
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
return api::del_service($parameters['serviceid']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method parameters.
|
||||
*
|
||||
* @return \external_function_parameters
|
||||
*/
|
||||
public static function del_service_parameters(): external_function_parameters {
|
||||
return new external_function_parameters([
|
||||
'serviceid' => new external_value(PARAM_INT, 'The service\'s ID.'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method result value.
|
||||
*
|
||||
* @return \external_value
|
||||
*/
|
||||
public static function del_service_returns(): external_value {
|
||||
return new external_value(PARAM_BOOL, 'The result operation.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the event's list.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public static function get_events(): array {
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
return api::get_events();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method parameters.
|
||||
*
|
||||
* @return \external_function_parameters
|
||||
*/
|
||||
public static function get_events_parameters(): external_function_parameters {
|
||||
return new external_function_parameters([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method result value.
|
||||
*
|
||||
* @return \external_multiple_structure
|
||||
*/
|
||||
public static function get_events_returns(): external_multiple_structure {
|
||||
return new external_multiple_structure(
|
||||
new external_single_structure([
|
||||
'action' => new external_value(PARAM_ALPHANUMEXT, 'The action name.'),
|
||||
'component' => new external_value(PARAM_COMPONENT, 'The component name.'),
|
||||
'crud' => new external_value(PARAM_ALPHA, 'The transaction type.'),
|
||||
'edulevel' => new external_value(PARAM_INT, 'The level of educational value.'),
|
||||
'eventname' => new external_value(PARAM_RAW, 'The event name.'),
|
||||
'objecttable' => new external_value(PARAM_RAW, 'The database table name.'),
|
||||
'target' => new external_value(PARAM_RAW, 'The target on which the action.'),
|
||||
], 'The event\'s data.'), 'The event\'s list.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get data by service.
|
||||
*
|
||||
* @param int $serviceid
|
||||
*
|
||||
* @return \local_webhooks\local\record
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public static function get_service(int $serviceid): record {
|
||||
$parameters = self::validate_parameters(self::get_service_parameters(), [
|
||||
'serviceid' => $serviceid,
|
||||
]);
|
||||
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
return api::get_service($parameters['serviceid']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method parameters.
|
||||
*
|
||||
* @return \external_function_parameters
|
||||
*/
|
||||
public static function get_service_parameters(): external_function_parameters {
|
||||
return new external_function_parameters([
|
||||
'serviceid' => new external_value(PARAM_INT, 'The service\'s ID.'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method result value.
|
||||
*
|
||||
* @return \external_single_structure
|
||||
*/
|
||||
public static function get_service_returns(): external_single_structure {
|
||||
return new external_single_structure([
|
||||
'events' => new external_multiple_structure(
|
||||
new external_value(PARAM_RAW, 'The event\'s name.'), 'The service\'s list events.'
|
||||
),
|
||||
'header' => new external_value(PARAM_RAW, 'The request\'s header or type'),
|
||||
'id' => new external_value(PARAM_INT, 'The service\'s ID.'),
|
||||
'name' => new external_value(PARAM_RAW, 'The service\'s name.'),
|
||||
'point' => new external_value(PARAM_URL, 'The service\'s endpoint.'),
|
||||
'status' => new external_value(PARAM_BOOL, 'The service\'s status.'),
|
||||
'token' => new external_value(PARAM_RAW, 'The service\'s secret key.'),
|
||||
], 'The service\'s data.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the service's list.
|
||||
*
|
||||
* @param array|null $conditions
|
||||
* @param string|null $sort
|
||||
* @param int|null $from
|
||||
* @param int|null $limit
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public static function get_services(array $conditions = null, string $sort = null, int $from = null, int $limit = null): array {
|
||||
$parameters = self::validate_parameters(self::get_services_parameters(), [
|
||||
'conditions' => $conditions ?? [],
|
||||
'from' => $from,
|
||||
'limit' => $limit,
|
||||
'sort' => $sort,
|
||||
]);
|
||||
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
return api::get_services(
|
||||
array_filter($parameters['conditions']),
|
||||
$parameters['sort'],
|
||||
$parameters['from'],
|
||||
$parameters['limit']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method parameters.
|
||||
*
|
||||
* @return \external_function_parameters
|
||||
*/
|
||||
public static function get_services_parameters(): external_function_parameters {
|
||||
return new external_function_parameters([
|
||||
'conditions' => new external_single_structure([
|
||||
'header' => new external_value(PARAM_RAW, 'The request\'s header or type', false),
|
||||
'name' => new external_value(PARAM_RAW, 'The service\'s name.', false),
|
||||
'point' => new external_value(PARAM_URL, 'The service\'s endpoint.', false),
|
||||
'status' => new external_value(PARAM_BOOL, 'The service\'s status.', false),
|
||||
'token' => new external_value(PARAM_RAW, 'The service\'s secret key.', false),
|
||||
], 'SQL conditions.', false),
|
||||
'sort' => new external_value(PARAM_RAW, 'SQL sort parameters.', false),
|
||||
'from' => new external_value(PARAM_INT, 'The start index.', false),
|
||||
'limit' => new external_value(PARAM_INT, 'The count elements.', false),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method result value.
|
||||
*
|
||||
* @return \external_multiple_structure
|
||||
*/
|
||||
public static function get_services_returns(): external_multiple_structure {
|
||||
return new external_multiple_structure(
|
||||
new external_single_structure([
|
||||
'events' => new external_multiple_structure(
|
||||
new external_value(PARAM_RAW, 'The event\'s name.'), 'The service\'s list events.'
|
||||
),
|
||||
'header' => new external_value(PARAM_RAW, 'The request\'s header or type'),
|
||||
'id' => new external_value(PARAM_INT, 'The service\'s ID.'),
|
||||
'name' => new external_value(PARAM_RAW, 'The service\'s name.'),
|
||||
'point' => new external_value(PARAM_URL, 'The service\'s endpoint.'),
|
||||
'status' => new external_value(PARAM_BOOL, 'The service\'s status.'),
|
||||
'token' => new external_value(PARAM_RAW, 'The service\'s secret key.'),
|
||||
], 'The service\'s data.'), 'The service\'s list.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the existing service.
|
||||
*
|
||||
* @param array $conditions
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public static function set_service(array $conditions): bool {
|
||||
$conditions = array_filter($conditions);
|
||||
$conditions['events'] = $conditions['events'] ?? [];
|
||||
|
||||
$parameters = self::validate_parameters(self::set_service_parameters(), [
|
||||
'record' => $conditions,
|
||||
]);
|
||||
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
$parameters = array_filter($parameters['record']);
|
||||
$record = api::get_service($parameters['id']);
|
||||
|
||||
foreach ($parameters as $index => $value) {
|
||||
if (property_exists($record, $index)) {
|
||||
$record->$index = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return api::set_service($record);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method parameters.
|
||||
*
|
||||
* @return \external_function_parameters
|
||||
*/
|
||||
public static function set_service_parameters(): external_function_parameters {
|
||||
return new external_function_parameters([
|
||||
'record' => new external_single_structure([
|
||||
'events' => new external_multiple_structure(
|
||||
new external_value(PARAM_RAW, 'The event\'s name.'), 'The service\'s list events.', false
|
||||
),
|
||||
'header' => new external_value(PARAM_RAW, 'The request\'s header or type', false),
|
||||
'id' => new external_value(PARAM_INT, 'The service\'s ID.'),
|
||||
'name' => new external_value(PARAM_RAW, 'The service\'s name.', false),
|
||||
'point' => new external_value(PARAM_URL, 'The service\'s endpoint.', false),
|
||||
'status' => new external_value(PARAM_BOOL, 'The service\'s status.', false),
|
||||
'token' => new external_value(PARAM_RAW, 'The service\'s secret key.', false),
|
||||
], 'The new service\'s data.'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method result value.
|
||||
*
|
||||
* @return \external_value
|
||||
*/
|
||||
public static function set_service_returns(): external_value {
|
||||
return new external_value(PARAM_BOOL, 'The result operation.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?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/>.
|
||||
|
||||
require_once(dirname(__DIR__, 2) . '/config.php');
|
||||
|
||||
global $CFG;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php');
|
||||
require_once($CFG->dirroot . '/local/webhooks/classes/output/services_table.php');
|
||||
require_once($CFG->libdir . '/adminlib.php');
|
||||
|
||||
use local_webhooks\local\api;
|
||||
use local_webhooks\output\services_table;
|
||||
|
||||
$deleteid = optional_param('deleteid', 0, PARAM_INT);
|
||||
$hideshowid = optional_param('hideshowid', 0, PARAM_INT);
|
||||
|
||||
$editpage = '/local/webhooks/service.php';
|
||||
$mainpage = '/local/webhooks/index.php';
|
||||
|
||||
$baseurl = new moodle_url($mainpage);
|
||||
$context = context_system::instance();
|
||||
|
||||
admin_externalpage_setup('local_webhooks', '', ['contextid' => $context->contextlevel], $baseurl, []);
|
||||
|
||||
// Deleting the existing service.
|
||||
if ($deleteid !== 0 && confirm_sesskey()) {
|
||||
api::del_service($deleteid);
|
||||
|
||||
redirect($PAGE->url, new lang_string('deleted', 'moodle'));
|
||||
}
|
||||
|
||||
// Setting status to existing service.
|
||||
if ($hideshowid !== 0 && confirm_sesskey()) {
|
||||
$service = api::get_service($hideshowid);
|
||||
$service->status = !filter_var($service->status, FILTER_VALIDATE_BOOLEAN);
|
||||
api::set_service($service);
|
||||
|
||||
redirect($PAGE->url, new lang_string('changessaved', 'moodle'));
|
||||
}
|
||||
|
||||
// Page header.
|
||||
$titlepage = new lang_string('pluginname', 'local_webhooks');
|
||||
$PAGE->set_heading($titlepage);
|
||||
$PAGE->set_title($titlepage);
|
||||
echo $OUTPUT->header();
|
||||
|
||||
// Adds the add button.
|
||||
$addserviceurl = new moodle_url($editpage, ['sesskey' => sesskey()]);
|
||||
echo $OUTPUT->single_button($addserviceurl, new lang_string('add', 'moodle'));
|
||||
|
||||
// Separation.
|
||||
echo html_writer::empty_tag('br');
|
||||
|
||||
// Displays the table.
|
||||
$table = new services_table('local-webhooks-services');
|
||||
$table->define_baseurl($baseurl);
|
||||
$table->out(25, true);
|
||||
|
||||
// Footer.
|
||||
echo $OUTPUT->footer();
|
||||
@@ -15,11 +15,11 @@
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Strings for component "local_webhooks", language "en".
|
||||
* Strings for component 'local_webhooks', language 'en'.
|
||||
*
|
||||
* @package local_webhooks
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @copyright 2018 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package local_webhooks
|
||||
*/
|
||||
|
||||
$string["pluginname"] = "WebHooks";
|
||||
$string['pluginname'] = 'WebHooks';
|
||||
@@ -1,122 +0,0 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Service Management Manager.
|
||||
*
|
||||
* @package local_webhooks
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
require_once(__DIR__ . "/../../config.php");
|
||||
require_once($CFG->libdir . "/tablelib.php");
|
||||
require_once($CFG->libdir . "/adminlib.php");
|
||||
|
||||
admin_externalpage_setup("managelocalplugins");
|
||||
|
||||
$hideshowid = optional_param("hideshowid", 0, PARAM_INT);
|
||||
$deleteid = optional_param("deleteid", 0, PARAM_INT);
|
||||
|
||||
require_login();
|
||||
|
||||
/* Link generation */
|
||||
$managerservice = "/local/webhooks/managerservice.php";
|
||||
$editservice = "/local/webhooks/editservice.php";
|
||||
$baseurl = new moodle_url($managerservice);
|
||||
$PAGE->set_url($baseurl);
|
||||
|
||||
/* Configure the context of the page */
|
||||
$context = context_system::instance();
|
||||
$PAGE->set_context($context);
|
||||
|
||||
/* Delete the service */
|
||||
if (boolval($deleteid) && confirm_sesskey()) {
|
||||
$DB->delete_records("local_webhooks_service", array("id" => $deleteid));
|
||||
redirect($PAGE->url, new lang_string("eventwebserviceservicedeleted", "webservice"));
|
||||
}
|
||||
|
||||
/* Retrieving a list of services */
|
||||
$callbacks = $DB->get_records_select("local_webhooks_service", null, null, $DB->sql_order_by_text("id"));
|
||||
|
||||
/* Switching the status of the service */
|
||||
if (boolval($hideshowid) && confirm_sesskey()) {
|
||||
$callback = $callbacks[$hideshowid];
|
||||
|
||||
if (!empty($callback)) {
|
||||
$callback->enable = !boolval($callback->enable);
|
||||
$DB->update_record("local_webhooks_service", $callback);
|
||||
redirect($PAGE->url, new lang_string("eventwebserviceserviceupdated", "webservice"));
|
||||
}
|
||||
}
|
||||
|
||||
/* Page template */
|
||||
$titlepage = new lang_string("externalservices", "webservice");
|
||||
$PAGE->set_pagelayout("admin");
|
||||
$PAGE->set_title($titlepage);
|
||||
$PAGE->set_heading($titlepage);
|
||||
|
||||
/* The page title */
|
||||
$PAGE->navbar->add(new lang_string("pluginname", "local_webhooks"), new moodle_url("/admin/settings.php", array("section" => "local_webhooks")));
|
||||
$PAGE->navbar->add($titlepage, $baseurl);
|
||||
echo $OUTPUT->header();
|
||||
|
||||
/* Table declaration */
|
||||
$table = new flexible_table("webhooks-service-table");
|
||||
|
||||
/* Customize the table */
|
||||
$table->define_columns(array("title", "url", "actions"));
|
||||
$table->define_headers(array(new lang_string("name", "moodle"), new lang_string("url", "moodle"), new lang_string("actions", "moodle")));
|
||||
$table->define_baseurl($baseurl);
|
||||
$table->setup();
|
||||
|
||||
foreach ($callbacks as $callback) {
|
||||
/* Filling of information columns */
|
||||
$titlecallback = html_writer::div($callback->title, "title");
|
||||
$urlcallback = html_writer::div($callback->url, "url");
|
||||
|
||||
/* Defining service status */
|
||||
$hideshowicon = "t/show";
|
||||
$hideshowstring = new lang_string("enable", "moodle");
|
||||
if (boolval($callback->enable)) {
|
||||
$hideshowicon = "t/hide";
|
||||
$hideshowstring = new lang_string("disable", "moodle");
|
||||
}
|
||||
|
||||
/* Link to enable / disable the service */
|
||||
$hideshowlink = new moodle_url($managerservice, array("hideshowid" => $callback->id, "sesskey" => sesskey()));
|
||||
$hideshowitem = $OUTPUT->action_icon($hideshowlink, new pix_icon($hideshowicon, $hideshowstring));
|
||||
|
||||
/* Link for editing */
|
||||
$editlink = new moodle_url($editservice, array("serviceid" => $callback->id));
|
||||
$edititem = $OUTPUT->action_icon($editlink, new pix_icon("t/edit", new lang_string("edit", "moodle")));
|
||||
|
||||
/* Link to remove */
|
||||
$deletelink = new moodle_url($managerservice, array("deleteid" => $callback->id, "sesskey" => sesskey()));
|
||||
$deleteitem = $OUTPUT->action_icon($deletelink, new pix_icon("t/delete", new lang_string("delete", "moodle")));
|
||||
|
||||
/* Adding data to the table */
|
||||
$table->add_data(array($titlecallback, $urlcallback, $hideshowitem . $edititem . $deleteitem));
|
||||
}
|
||||
|
||||
/* Display the table */
|
||||
$table->print_html();
|
||||
|
||||
/* Add service button */
|
||||
$addurl = new moodle_url("/local/webhooks/editservice.php");
|
||||
echo $OUTPUT->single_button($addurl, new lang_string("addaservice", "webservice"), "get");
|
||||
|
||||
echo $OUTPUT->footer();
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Page of the service editor.
|
||||
*
|
||||
* @copyright 2018 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package local_webhooks
|
||||
*/
|
||||
|
||||
require_once(__DIR__ . '/../../config.php');
|
||||
|
||||
require_once($CFG->dirroot . '/local/webhooks/classes/ui_forms_plugin.php');
|
||||
require_once($CFG->dirroot . '/local/webhooks/lib.php');
|
||||
require_once($CFG->libdir . '/adminlib.php');
|
||||
|
||||
$serviceid = optional_param('serviceid', 0, PARAM_INT);
|
||||
|
||||
$urlparameters = array('serviceid' => $serviceid);
|
||||
$baseurl = new moodle_url('/local/webhooks/service.php', $urlparameters);
|
||||
$mainpage = new moodle_url('/local/webhooks/index.php');
|
||||
|
||||
admin_externalpage_setup('local_webhooks', '', null, $baseurl, array());
|
||||
$context = context_system::instance();
|
||||
|
||||
$mform = new local_webhooks_service_edit_form($PAGE->url);
|
||||
$formdata = (array) $mform->get_data();
|
||||
|
||||
/* Cancel */
|
||||
if ($mform->is_cancelled()) {
|
||||
redirect($mainpage);
|
||||
}
|
||||
|
||||
/* Updating the data */
|
||||
if (!empty($formdata) && confirm_sesskey()) {
|
||||
if (isset($formdata['events'])) {
|
||||
$formdata['events'] = array_keys($formdata['events']);
|
||||
}
|
||||
|
||||
if (!empty($serviceid)) {
|
||||
$formdata['id'] = $serviceid;
|
||||
local_webhooks_api::update_service($formdata);
|
||||
} else {
|
||||
local_webhooks_api::create_service($formdata);
|
||||
}
|
||||
|
||||
redirect($mainpage, new lang_string('changessaved', 'moodle'));
|
||||
}
|
||||
|
||||
/* Loading service data */
|
||||
if (!empty($serviceid)) {
|
||||
$service = local_webhooks_api::get_service($serviceid);
|
||||
$service->events = array_fill_keys($service->events, 1);
|
||||
$mform->set_data($service);
|
||||
}
|
||||
|
||||
/* The page title */
|
||||
$titlepage = new lang_string('externalservice', 'webservice');
|
||||
$PAGE->navbar->add($titlepage);
|
||||
$PAGE->set_heading($titlepage);
|
||||
$PAGE->set_title($titlepage);
|
||||
echo $OUTPUT->header();
|
||||
|
||||
/* Displays the form */
|
||||
$mform->display();
|
||||
|
||||
/* Footer */
|
||||
echo $OUTPUT->footer();
|
||||
+12
-14
@@ -17,23 +17,21 @@
|
||||
/**
|
||||
* Settings of the plugin.
|
||||
*
|
||||
* @package local_webhooks
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @copyright 2018 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package local_webhooks
|
||||
*/
|
||||
|
||||
defined("MOODLE_INTERNAL") || die();
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/* @var \admin_root $ADMIN */
|
||||
|
||||
$settings = null;
|
||||
if ($hassiteconfig) {
|
||||
$settings = new admin_settingpage("local_webhooks", new lang_string("pluginname", "local_webhooks"));
|
||||
$ADMIN->add("localplugins", $settings);
|
||||
|
||||
/* The switch of the interceptor of events */
|
||||
$settings->add(new admin_setting_configcheckbox("local_webhooks/enable", new lang_string("enable", "moodle"), new lang_string("enablews", "webservice"), false));
|
||||
|
||||
/* Link to the service manager */
|
||||
$linktext = new lang_string("externalservices", "webservice");
|
||||
$linkurl = new moodle_url("/local/webhooks/managerservice.php");
|
||||
$settings->add(new admin_setting_heading("local_webhooks_managerservice", null, html_writer::link($linkurl, $linktext)));
|
||||
$ADMIN->add(
|
||||
'server', new admin_externalpage(
|
||||
'local_webhooks',
|
||||
new lang_string('pluginname', 'local_webhooks'),
|
||||
new moodle_url('/local/webhooks/index.php')
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,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/>.
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
|
||||
require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php');
|
||||
|
||||
use local_webhooks\local\api;
|
||||
use local_webhooks\local\record;
|
||||
|
||||
/**
|
||||
* Testing the API plugin class.
|
||||
*
|
||||
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
final class local_webhooks_api_testcase extends advanced_testcase {
|
||||
/**
|
||||
* Generate random an event's list.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
private static function get_random_events(): array {
|
||||
$result = array_rand(api::get_events(), random_int(2, 10));
|
||||
|
||||
return is_array($result) ? $result : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a random record.
|
||||
*
|
||||
* @return \local_webhooks\local\record
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
private static function get_random_record(): record {
|
||||
$record = new record();
|
||||
|
||||
$record->events = self::get_random_events();
|
||||
$record->header = 'application/json';
|
||||
$record->name = uniqid('', false);
|
||||
$record->point = 'http://example.org/' . urlencode($record->name);
|
||||
$record->status = true;
|
||||
$record->token = generate_uuid();
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing creation of the service.
|
||||
*
|
||||
* @group local_webhooks
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \moodle_exception
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function test_adding() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
|
||||
$events = $DB->get_records(LW_TABLE_EVENTS);
|
||||
$services = $DB->get_records(LW_TABLE_SERVICES);
|
||||
|
||||
self::assertCount(1, $services);
|
||||
$service = array_shift($services);
|
||||
|
||||
self::assertEquals($record->header, $service->header);
|
||||
self::assertEquals($record->id, $service->id);
|
||||
self::assertEquals($record->name, $service->name);
|
||||
self::assertEquals($record->point, $service->point);
|
||||
self::assertEquals($record->status, (bool) $service->status);
|
||||
self::assertEquals($record->token, $service->token);
|
||||
|
||||
self::assertCount(count($record->events), $events);
|
||||
foreach ($events as $event) {
|
||||
self::assertContains($event->name, $record->events);
|
||||
self::assertEquals($record->id, $event->serviceid);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deletion of the service.
|
||||
*
|
||||
* @group local_webhooks
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \moodle_exception
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function test_deleting() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Testing correct delete record of the database.
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
|
||||
self::assertTrue(api::del_service($record->id));
|
||||
self::assertCount(0, $DB->get_records(LW_TABLE_EVENTS));
|
||||
self::assertCount(0, $DB->get_records(LW_TABLE_SERVICES));
|
||||
|
||||
$ids = [];
|
||||
$total = random_int(5, 20);
|
||||
|
||||
// Testing correct delete record of the record's list.
|
||||
for ($i = 0; $i < $total; $i++) {
|
||||
$record = self::get_random_record();
|
||||
$ids[] = api::add_service($record);
|
||||
}
|
||||
|
||||
self::assertEquals(count($ids), api::get_total_count());
|
||||
self::assertTrue(api::del_service($ids[array_rand($ids, 1)]));
|
||||
self::assertEquals(count($ids) - 1, api::get_total_count());
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing get an event's list.
|
||||
*
|
||||
* @group local_webhooks
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function test_get_events() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$events = api::get_events();
|
||||
|
||||
self::assertNotCount(0, $events);
|
||||
|
||||
foreach ($events as $event) {
|
||||
self::assertInternalType('array', $event);
|
||||
|
||||
self::assertEquals([
|
||||
'eventname', 'component', 'target', 'action', 'crud', 'edulevel', 'objecttable',
|
||||
], array_keys($event));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing get to a service.
|
||||
*
|
||||
* @group local_webhooks
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \moodle_exception
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function test_get_service() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
$service = api::get_service($record->id);
|
||||
|
||||
self::assertEquals($record->header, $service->header);
|
||||
self::assertEquals($record->id, $service->id);
|
||||
self::assertEquals($record->name, $service->name);
|
||||
self::assertEquals($record->point, $service->point);
|
||||
self::assertEquals($record->status, (bool) $service->status);
|
||||
self::assertEquals($record->token, $service->token);
|
||||
|
||||
self::assertInternalType('array', $service->events);
|
||||
self::assertCount(count($record->events), $service->events);
|
||||
foreach ($service->events as $event) {
|
||||
self::assertContains($event, $record->events);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing get to the list services.
|
||||
*
|
||||
* @group local_webhooks
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \moodle_exception
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function test_get_services() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$records = [];
|
||||
$total = random_int(5, 20);
|
||||
|
||||
for ($i = 0; $i < $total; $i++) {
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
$records[$record->id] = $record;
|
||||
}
|
||||
|
||||
$services = api::get_services();
|
||||
self::assertCount(count($records), $services);
|
||||
|
||||
foreach ($services as $service) {
|
||||
$record = $records[$service->id];
|
||||
|
||||
self::assertEquals($record->header, $service->header);
|
||||
self::assertEquals($record->id, $service->id);
|
||||
self::assertEquals($record->name, $service->name);
|
||||
self::assertEquals($record->point, $service->point);
|
||||
self::assertEquals($record->status, $service->status);
|
||||
self::assertEquals($record->token, $service->token);
|
||||
|
||||
self::assertInternalType('array', $service->events);
|
||||
self::assertCount(count($record->events), $service->events);
|
||||
foreach ($service->events as $event) {
|
||||
self::assertContains($event, $record->events);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing get to the list services by event name.
|
||||
*
|
||||
* @group local_webhooks
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \moodle_exception
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function test_get_services_by_event() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$eventname = generate_uuid();
|
||||
$limit = random_int(1, 5);
|
||||
$total = random_int(5, 20);
|
||||
|
||||
$ids = [];
|
||||
|
||||
for ($i = 0; $i < $total; $i++) {
|
||||
$record = self::get_random_record();
|
||||
$record->events[] = $i < $limit ? $eventname : '';
|
||||
$ids[] = api::add_service($record);
|
||||
}
|
||||
|
||||
self::assertEquals(count($ids), api::get_total_count());
|
||||
$services = api::get_services_by_event($eventname);
|
||||
self::assertCount($limit, $services);
|
||||
|
||||
foreach ($services as $service) {
|
||||
self::assertContains($service->id, $ids);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing get to the list services with conditions.
|
||||
*
|
||||
* @group local_webhooks
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \moodle_exception
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function test_get_services_with_conditions() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$records = [];
|
||||
$total = random_int(5, 20);
|
||||
$limit = intdiv($total, 2);
|
||||
|
||||
// Creating some records.
|
||||
for ($i = 0; $i < $total; $i++) {
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
$records[] = $record;
|
||||
}
|
||||
|
||||
// Testing condition fields.
|
||||
$record = $records[array_rand($records, 1)];
|
||||
self::assertCount(1, api::get_services([
|
||||
'point' => $record->point,
|
||||
]));
|
||||
|
||||
// Testing limit fields.
|
||||
self::assertCount($limit, api::get_services([], null, 1, $limit));
|
||||
|
||||
// Testing sort fields.
|
||||
$service1 = api::get_services(null, 'id asc')[0];
|
||||
$service2 = api::get_services(null, 'id desc')[0];
|
||||
self::assertNotEquals($service1->id, $service2->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing get a total count of existing records.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function test_total() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$total = random_int(5, 20);
|
||||
|
||||
for ($i = 0; $i < $total; $i++) {
|
||||
$record = self::get_random_record();
|
||||
api::add_service($record);
|
||||
}
|
||||
|
||||
self::assertEquals($total, api::get_total_count());
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing of the service update.
|
||||
*
|
||||
* @group local_webhooks
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \moodle_exception
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function test_updating() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
$record1 = self::get_random_record();
|
||||
$record2 = self::get_random_record();
|
||||
|
||||
$record2->id = api::add_service($record1);
|
||||
self::assertTrue(api::set_service($record2));
|
||||
|
||||
$events = $DB->get_records(LW_TABLE_EVENTS);
|
||||
$services = $DB->get_records(LW_TABLE_SERVICES);
|
||||
|
||||
self::assertCount(1, $services);
|
||||
$service = array_shift($services);
|
||||
|
||||
self::assertEquals($record2->header, $service->header);
|
||||
self::assertEquals($record2->id, $service->id);
|
||||
self::assertEquals($record2->name, $service->name);
|
||||
self::assertEquals($record2->point, $service->point);
|
||||
self::assertEquals($record2->status, (bool) $service->status);
|
||||
self::assertEquals($record2->token, $service->token);
|
||||
|
||||
self::assertCount(count($record2->events), $events);
|
||||
foreach ($events as $event) {
|
||||
self::assertContains($event->name, $record2->events);
|
||||
self::assertEquals($record2->id, $event->serviceid);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,386 @@
|
||||
<?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/>.
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
|
||||
require_once($CFG->dirroot . '/local/webhooks/externallib.php');
|
||||
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
|
||||
|
||||
use local_webhooks\local\api;
|
||||
use local_webhooks\local\record;
|
||||
|
||||
/**
|
||||
* Testing external functions.
|
||||
*
|
||||
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
final class local_webhooks_external_testcase extends externallib_advanced_testcase {
|
||||
/**
|
||||
* Generate random an event's list.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
private static function get_random_events(): array {
|
||||
$result = array_rand(api::get_events(), random_int(2, 10));
|
||||
|
||||
return is_array($result) ? $result : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a random record.
|
||||
*
|
||||
* @return \local_webhooks\local\record
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
private static function get_random_record(): record {
|
||||
$record = new record();
|
||||
|
||||
$record->events = self::get_random_events();
|
||||
$record->header = 'application/json';
|
||||
$record->name = uniqid('', false);
|
||||
$record->point = 'http://example.org/' . urlencode($record->name);
|
||||
$record->status = true;
|
||||
$record->token = generate_uuid();
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing external add service.
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \invalid_response_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public function test_adding() {
|
||||
$this->resetAfterTest();
|
||||
self::setAdminUser();
|
||||
|
||||
$record = self::get_random_record();
|
||||
$return = local_webhooks_external::add_service((array) $record);
|
||||
$return = external_api::clean_returnvalue(local_webhooks_external::add_service_returns(), $return);
|
||||
|
||||
self::assertInternalType('integer', $return);
|
||||
self::assertEquals(1, api::get_total_count());
|
||||
|
||||
$service = api::get_service($return);
|
||||
self::assertEquals($record->header, $service->header);
|
||||
self::assertEquals($record->name, $service->name);
|
||||
self::assertEquals($record->point, $service->point);
|
||||
self::assertEquals($record->status, $service->status);
|
||||
self::assertEquals($record->token, $service->token);
|
||||
self::assertEquals($return, $service->id);
|
||||
|
||||
self::assertInternalType('array', $service->events);
|
||||
self::assertCount(count($record->events), $service->events);
|
||||
foreach ($service->events as $event) {
|
||||
self::assertContains($event, $record->events);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing the external delete service.
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \invalid_response_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public function test_deleting() {
|
||||
$this->resetAfterTest();
|
||||
self::setAdminUser();
|
||||
|
||||
// Testing correct delete record of the database.
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
|
||||
$return = local_webhooks_external::del_service($record->id);
|
||||
$return = external_api::clean_returnvalue(local_webhooks_external::del_service_returns(), $return);
|
||||
|
||||
self::assertEquals(0, api::get_total_count());
|
||||
self::assertInternalType('bool', $return);
|
||||
|
||||
$ids = [];
|
||||
$total = random_int(5, 20);
|
||||
|
||||
// Testing correct delete record of the record's list.
|
||||
for ($i = 0; $i < $total; $i++) {
|
||||
$record = self::get_random_record();
|
||||
$ids[] = api::add_service($record);
|
||||
}
|
||||
|
||||
self::assertEquals(count($ids), api::get_total_count());
|
||||
$return = local_webhooks_external::del_service($ids[array_rand($ids, 1)]);
|
||||
$return = external_api::clean_returnvalue(local_webhooks_external::del_service_returns(), $return);
|
||||
|
||||
self::assertEquals(count($ids) - 1, api::get_total_count());
|
||||
self::assertInternalType('bool', $return);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing external function get the event's list.
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \invalid_response_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public function test_get_events() {
|
||||
$this->resetAfterTest();
|
||||
self::setAdminUser();
|
||||
|
||||
$return = local_webhooks_external::get_events();
|
||||
$return = external_api::clean_returnvalue(local_webhooks_external::get_events_returns(), $return);
|
||||
|
||||
self::assertInternalType('array', $return);
|
||||
self::assertCount(count(api::get_events()), $return);
|
||||
|
||||
foreach ($return as $item) {
|
||||
self::assertInternalType('array', $item);
|
||||
|
||||
self::assertEquals([
|
||||
'action', 'component', 'crud', 'edulevel', 'eventname', 'objecttable', 'target',
|
||||
], array_keys($item));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing external get record's data.
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \invalid_response_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public function test_get_service() {
|
||||
$this->resetAfterTest();
|
||||
self::setAdminUser();
|
||||
|
||||
// Creating a new record.
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
|
||||
$return = local_webhooks_external::get_service($record->id);
|
||||
$return = external_api::clean_returnvalue(local_webhooks_external::get_service_returns(), $return);
|
||||
self::assertInternalType('array', $return);
|
||||
|
||||
// Testing the main fields.
|
||||
self::assertEquals($record->header, $return['header']);
|
||||
self::assertEquals($record->id, $return['id']);
|
||||
self::assertEquals($record->name, $return['name']);
|
||||
self::assertEquals($record->point, $return['point']);
|
||||
self::assertEquals($record->status, (int) $return['status']);
|
||||
self::assertEquals($record->token, $return['token']);
|
||||
|
||||
// Testing an event's list.
|
||||
self::assertInternalType('array', $return['events']);
|
||||
self::assertNotCount(0, $return['events']);
|
||||
foreach ($return['events'] as $event) {
|
||||
self::assertContains($event, $record->events);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing external get the record's list.
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \invalid_response_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public function test_get_services() {
|
||||
$this->resetAfterTest();
|
||||
self::setAdminUser();
|
||||
|
||||
$records = [];
|
||||
$total = random_int(5, 10);
|
||||
|
||||
// Creating some records.
|
||||
for ($i = 0; $i < $total; $i++) {
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
$records[$record->id] = $record;
|
||||
}
|
||||
|
||||
self::assertEquals(count($records), api::get_total_count());
|
||||
|
||||
$return = local_webhooks_external::get_services();
|
||||
$return = external_api::clean_returnvalue(local_webhooks_external::get_services_returns(), $return);
|
||||
|
||||
// Testing received item's list.
|
||||
self::assertInternalType('array', $return);
|
||||
self::assertCount(count($records), $return);
|
||||
|
||||
foreach ($return as $item) {
|
||||
self::assertInternalType('array', $item);
|
||||
|
||||
$record = $records[$item['id']];
|
||||
|
||||
self::assertEquals($record->header, $item['header']);
|
||||
self::assertEquals($record->id, $item['id']);
|
||||
self::assertEquals($record->name, $item['name']);
|
||||
self::assertEquals($record->point, $item['point']);
|
||||
self::assertEquals($record->status, (int) $item['status']);
|
||||
self::assertEquals($record->token, $item['token']);
|
||||
|
||||
self::assertInternalType('array', $item['events']);
|
||||
self::assertNotCount(0, $item['events']);
|
||||
foreach ($item['events'] as $event) {
|
||||
self::assertContains($event, $record->events);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing external get to the list services with conditions.
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \invalid_response_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public function test_get_services_with_conditions() {
|
||||
$this->resetAfterTest();
|
||||
self::setAdminUser();
|
||||
|
||||
$records = [];
|
||||
$total = random_int(5, 10);
|
||||
$limit = intdiv($total, 2);
|
||||
|
||||
// Creating some records.
|
||||
for ($i = 0; $i < $total; $i++) {
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
$records[$record->id] = $record;
|
||||
}
|
||||
|
||||
self::assertEquals(count($records), api::get_total_count());
|
||||
|
||||
// Testing condition fields.
|
||||
$record = $records[array_rand($records, 1)];
|
||||
$return = local_webhooks_external::get_services(['point' => $record->point]);
|
||||
$return = external_api::clean_returnvalue(local_webhooks_external::get_services_returns(), $return);
|
||||
|
||||
self::assertCount(1, $return);
|
||||
self::assertEquals($return[0]['id'], $record->id);
|
||||
|
||||
// Testing limit fields.
|
||||
$return = local_webhooks_external::get_services(null, null, 1, $limit);
|
||||
$return = external_api::clean_returnvalue(local_webhooks_external::get_services_returns(), $return);
|
||||
self::assertCount($limit, $return);
|
||||
|
||||
// Testing sort fields.
|
||||
$return = local_webhooks_external::get_services(null, 'id asc');
|
||||
$return = external_api::clean_returnvalue(local_webhooks_external::get_services_returns(), $return);
|
||||
$service1 = array_shift($return);
|
||||
|
||||
$return = local_webhooks_external::get_services(null, 'id desc');
|
||||
$return = external_api::clean_returnvalue(local_webhooks_external::get_services_returns(), $return);
|
||||
$service2 = array_shift($return);
|
||||
|
||||
self::assertNotEquals($service1['id'], $service2['id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing full update parameters of the service.
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public function test_updating_full() {
|
||||
$this->resetAfterTest();
|
||||
self::setAdminUser();
|
||||
|
||||
$record = self::get_random_record();
|
||||
$record2 = self::get_random_record();
|
||||
$record2->id = api::add_service($record);
|
||||
|
||||
$return = local_webhooks_external::set_service((array) $record2);
|
||||
$return = external_api::validate_parameters(local_webhooks_external::set_service_returns(), $return);
|
||||
self::assertInternalType('bool', $return);
|
||||
|
||||
$service = api::get_service($record2->id);
|
||||
self::assertEquals($record2->id, $service->id);
|
||||
self::assertEquals($record2->header, $service->header);
|
||||
self::assertEquals($record2->name, $service->name);
|
||||
self::assertEquals($record2->point, $service->point);
|
||||
self::assertEquals($record2->status, $service->status);
|
||||
self::assertEquals($record2->token, $service->token);
|
||||
|
||||
self::assertInternalType('array', $service->events);
|
||||
self::assertCount(count($record2->events), $service->events);
|
||||
foreach ($service->events as $event) {
|
||||
self::assertContains($event, $record2->events);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing selective update parameters of the service.
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public function test_updating_short() {
|
||||
$this->resetAfterTest();
|
||||
self::setAdminUser();
|
||||
|
||||
$record = self::get_random_record();
|
||||
$record2 = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
|
||||
$return = local_webhooks_external::set_service(['id' => $record->id, 'name' => $record2->name, 'point' => $record2->point]);
|
||||
$return = external_api::validate_parameters(local_webhooks_external::set_service_returns(), $return);
|
||||
self::assertInternalType('bool', $return);
|
||||
|
||||
$service = api::get_service($record->id);
|
||||
self::assertEquals($record->header, $service->header);
|
||||
self::assertEquals($record->id, $service->id);
|
||||
self::assertEquals($record->status, $service->status);
|
||||
self::assertEquals($record->token, $service->token);
|
||||
self::assertEquals($record2->name, $service->name);
|
||||
self::assertEquals($record2->point, $service->point);
|
||||
|
||||
self::assertInternalType('array', $service->events);
|
||||
self::assertCount(count($record->events), $service->events);
|
||||
foreach ($service->events as $event) {
|
||||
self::assertContains($event, $record->events);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
<?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/>.
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
|
||||
require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php');
|
||||
|
||||
use core\event\course_created;
|
||||
use core\task\manager;
|
||||
use local_webhooks\local\api;
|
||||
use local_webhooks\local\record;
|
||||
use local_webhooks\task\notify;
|
||||
|
||||
/**
|
||||
* Class local_webhooks_task_testcase.
|
||||
*
|
||||
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
final class local_webhooks_task_testcase extends advanced_testcase {
|
||||
const EVENTNAME = '\\' . course_created::class;
|
||||
|
||||
/**
|
||||
* Generate a random record.
|
||||
*
|
||||
* @return \local_webhooks\local\record
|
||||
*/
|
||||
private static function get_random_record(): record {
|
||||
$record = new record();
|
||||
|
||||
$record->events = [self::EVENTNAME];
|
||||
$record->header = 'application/json';
|
||||
$record->name = uniqid('', false);
|
||||
$record->point = 'http://example.org/' . urlencode($record->name);
|
||||
$record->status = true;
|
||||
$record->token = generate_uuid();
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing add a task to the queue.
|
||||
*/
|
||||
public function test_add_task() {
|
||||
global $DB;
|
||||
|
||||
$DB->delete_records('task_adhoc');
|
||||
$this->resetAfterTest();
|
||||
|
||||
manager::queue_adhoc_task(new notify());
|
||||
|
||||
$tasks = manager::get_adhoc_tasks(notify::class);
|
||||
|
||||
self::assertCount(1, $tasks);
|
||||
self::assertInstanceOf(notify::class, array_shift($tasks));
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing disabled processing service.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public function test_execute_disabled() {
|
||||
global $DB;
|
||||
|
||||
$DB->delete_records('task_adhoc');
|
||||
curl::mock_response('{}');
|
||||
$this->resetAfterTest();
|
||||
|
||||
$record = self::get_random_record();
|
||||
$record->status = false;
|
||||
$record->id = api::add_service($record);
|
||||
|
||||
$task = new notify();
|
||||
$task->set_custom_data(['eventname' => self::EVENTNAME]);
|
||||
$task->execute();
|
||||
|
||||
self::assertNull($task->debug);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing enabled processing service.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public function test_execute_enabled() {
|
||||
global $DB;
|
||||
|
||||
$DB->delete_records('task_adhoc');
|
||||
curl::mock_response('{}');
|
||||
$this->resetAfterTest();
|
||||
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
|
||||
$task = new notify();
|
||||
$task->set_custom_data(['eventname' => self::EVENTNAME]);
|
||||
$task->execute();
|
||||
|
||||
self::assertCount(1, $task->debug);
|
||||
self::assertInternalType('array', $task->debug);
|
||||
|
||||
$element = array_shift($task->debug);
|
||||
self::assertInternalType('array', $element['data']);
|
||||
self::assertInternalType('object', $element['service']);
|
||||
|
||||
self::assertEquals($record->events[0], $element['data']['eventname']);
|
||||
self::assertEquals($record->id, $element['service']->id);
|
||||
self::assertEquals($record->token, $element['data']['token']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing count creating tasks.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public function test_observer_multiple() {
|
||||
global $DB;
|
||||
|
||||
$DB->delete_records('task_adhoc');
|
||||
curl::mock_response('{}');
|
||||
$this->resetAfterTest();
|
||||
|
||||
$generator = self::getDataGenerator();
|
||||
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
|
||||
$total = random_int(5, 20);
|
||||
for ($i = 0; $i < $total; $i++) {
|
||||
$generator->create_course();
|
||||
}
|
||||
|
||||
$debug = [];
|
||||
foreach (manager::get_adhoc_tasks(notify::class) as $event) {
|
||||
self::assertInstanceOf(notify::class, $event);
|
||||
/** @var \local_webhooks\task\notify $event */
|
||||
|
||||
$event->execute();
|
||||
|
||||
if (isset($event->debug) && is_array($event->debug)) {
|
||||
$debug[] = array_merge(...$event->debug);
|
||||
}
|
||||
}
|
||||
|
||||
self::assertCount($total, $debug);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing structure a creating task.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public function test_observer_single() {
|
||||
global $DB;
|
||||
|
||||
$DB->delete_records('task_adhoc');
|
||||
curl::mock_response('{}');
|
||||
$this->resetAfterTest();
|
||||
|
||||
$generator = self::getDataGenerator();
|
||||
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
|
||||
$course = $generator->create_course();
|
||||
|
||||
$debug = [];
|
||||
foreach (manager::get_adhoc_tasks(notify::class) as $event) {
|
||||
self::assertInstanceOf(notify::class, $event);
|
||||
/** @var \local_webhooks\task\notify $event */
|
||||
|
||||
$event->execute();
|
||||
|
||||
if (isset($event->debug) && is_array($event->debug)) {
|
||||
$debug[] = array_merge(...$event->debug);
|
||||
}
|
||||
}
|
||||
|
||||
self::assertCount(1, $debug);
|
||||
$element = array_shift($debug);
|
||||
|
||||
self::assertInternalType('array', $element['data']);
|
||||
self::assertEquals($course->id, $element['data']['courseid']);
|
||||
}
|
||||
}
|
||||
+6
-13
@@ -14,17 +14,10 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Information about the version of the plugin.
|
||||
*
|
||||
* @package local_webhooks
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
defined("MOODLE_INTERNAL") || die();
|
||||
$plugin->release = "0.5.0 (Build: 2017102630)";
|
||||
$plugin->version = 2017102630;
|
||||
$plugin->requires = 2016112900;
|
||||
$plugin->component = "local_webhooks";
|
||||
$plugin->maturity = MATURITY_RC;
|
||||
$plugin->component = 'local_webhooks';
|
||||
$plugin->maturity = MATURITY_ALPHA;
|
||||
$plugin->release = '5.0.0-rc.1 (Build: 2019040100)';
|
||||
$plugin->requires = 2016052300;
|
||||
$plugin->version = 2019040100;
|
||||
Reference in New Issue
Block a user