Compare commits
116
Commits
v0.2.0
...
v4.0.0-rc.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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 | ||
|
|
e1734746c0 | ||
|
|
21ad3135c8 | ||
|
|
f87b2c82c9 | ||
|
|
c25f4bada6 | ||
|
|
4f34a6db0f | ||
|
|
a7e41f77d2 | ||
|
|
6c4e47b395 | ||
|
|
3816d4128c | ||
|
|
fdaa591882 | ||
|
|
6b5c96a383 | ||
|
|
8ed2352fbe | ||
|
|
f613301f24 | ||
|
|
f51bb5714e | ||
|
|
065426b026 | ||
|
|
3a24fc34ca | ||
|
|
a95df3c6b1 | ||
|
|
2744e4fb80 | ||
|
|
5179a7ec05 | ||
|
|
18a5d23d07 | ||
|
|
f43b3f2fd3 | ||
|
|
ea30d3ca56 | ||
|
|
e86542d8f7 | ||
|
|
dab55b9562 | ||
|
|
c2cd361171 | ||
|
|
eaee6b7c81 | ||
|
|
282e3bcaf1 | ||
|
|
c037edbc58 | ||
|
|
2c4c05acd6 |
@@ -2,34 +2,29 @@
|
||||
[](https://github.com/valentineus/moodle-webhooks/releases)
|
||||
[](https://travis-ci.org/valentineus/moodle-webhooks)
|
||||
[](https://www.codacy.com/app/valentineus/moodle-webhooks)
|
||||
[](https://gitter.im/valentineus/auth-link)
|
||||
[](https://gitter.im/valentineus/moodle-webhooks)
|
||||
|
||||
Webhooks allow applications to subscribe to Moodle events.
|
||||
Adding Webhooks to Moodle.
|
||||
The Webhooks feature allows applications to receive real-time notification of changes to certain data.
|
||||
|
||||
**Attention!**:
|
||||
This plugin is in active development.
|
||||
Using Webhooks, your application will be able to receive notifications of changes to selected topics and their fields.
|
||||
Because of this, you do not have to rely on continuous or even periodic requests to check for updates.
|
||||
Notifications about Webhooks updates are sent as POST requests to the callback URL you specified.
|
||||
Notifications can indicate the very fact of a field change or include a new value.
|
||||
|
||||
## Installation
|
||||
Features:
|
||||
|
||||
Get the installation package in any of the available methods:
|
||||
* Use any number of services for notification;
|
||||
* Customizing each external service;
|
||||
* Interception of all events in the Moodle system;
|
||||
* Use a secret phrase to authenticate requests;
|
||||
* [JSON](https://en.wikipedia.org/wiki/JSON) - Format of outgoing data;
|
||||
|
||||
* [GitHub Releases](https://github.com/valentineus/moodle-webhooks/releases).
|
||||
* [Compilation from the source code](#build).
|
||||
## Documentation
|
||||
|
||||
## 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,65 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Registration of the system of events.
|
||||
*
|
||||
* @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\event;
|
||||
|
||||
defined("MOODLE_INTERNAL") || die();
|
||||
|
||||
/**
|
||||
* 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 backup_performed extends \core\event\base {
|
||||
/**
|
||||
* Init method.
|
||||
*/
|
||||
protected function init() {
|
||||
$this->data["crud"] = "c";
|
||||
$this->data["edulevel"] = self::LEVEL_OTHER;
|
||||
$this->data["objecttable"] = "local_webhooks_service";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return localised event name.
|
||||
*/
|
||||
public static function get_name() {
|
||||
return new \lang_string("create", "moodle");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of what happened.
|
||||
*/
|
||||
public function get_description() {
|
||||
return new \lang_string("backup", "moodle");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL related to the action.
|
||||
*/
|
||||
public function get_url() {
|
||||
return new \moodle_url("/local/webhooks/index.php");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Registration of the system of events.
|
||||
*
|
||||
* @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\event;
|
||||
|
||||
defined("MOODLE_INTERNAL") || die();
|
||||
|
||||
/**
|
||||
* 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 backup_restored extends \core\event\base {
|
||||
/**
|
||||
* Init method.
|
||||
*/
|
||||
protected function init() {
|
||||
$this->data["crud"] = "u";
|
||||
$this->data["edulevel"] = self::LEVEL_OTHER;
|
||||
$this->data["objecttable"] = "local_webhooks_service";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return localised event name.
|
||||
*/
|
||||
public static function get_name() {
|
||||
return new \lang_string("update", "moodle");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of what happened.
|
||||
*/
|
||||
public function get_description() {
|
||||
return new \lang_string("backupfinished", "moodle");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL related to the action.
|
||||
*/
|
||||
public function get_url() {
|
||||
return new \moodle_url("/local/webhooks/index.php");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Registration of the system of events.
|
||||
*
|
||||
* @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\event;
|
||||
|
||||
defined("MOODLE_INTERNAL") || die();
|
||||
|
||||
/**
|
||||
* 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 response_answer extends \core\event\base {
|
||||
/**
|
||||
* Init method.
|
||||
*/
|
||||
protected function init() {
|
||||
$this->data["crud"] = "r";
|
||||
$this->data["edulevel"] = self::LEVEL_OTHER;
|
||||
$this->data["objecttable"] = "local_webhooks_service";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return localised event name.
|
||||
*/
|
||||
public static function get_name() {
|
||||
return new \lang_string("answer", "moodle");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of what happened.
|
||||
*/
|
||||
public function get_description() {
|
||||
return $this->other["status"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL related to the action.
|
||||
*/
|
||||
public function get_url() {
|
||||
return new \moodle_url("/local/webhooks/editservice.php", array("serviceid" => $this->objectid));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Registration of the system of events.
|
||||
*
|
||||
* @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\event;
|
||||
|
||||
defined("MOODLE_INTERNAL") || die();
|
||||
|
||||
/**
|
||||
* 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 service_added extends \core\event\base {
|
||||
/**
|
||||
* Init method.
|
||||
*/
|
||||
protected function init() {
|
||||
$this->data["crud"] = "c";
|
||||
$this->data["edulevel"] = self::LEVEL_OTHER;
|
||||
$this->data["objecttable"] = "local_webhooks_service";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return localised event name.
|
||||
*/
|
||||
public static function get_name() {
|
||||
return new \lang_string("create", "moodle");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of what happened.
|
||||
*/
|
||||
public function get_description() {
|
||||
return new \lang_string("eventwebserviceservicecreated", "webservice");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL related to the action.
|
||||
*/
|
||||
public function get_url() {
|
||||
return new \moodle_url("/local/webhooks/editservice.php", array("serviceid" => $this->objectid));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Registration of the system of events.
|
||||
*
|
||||
* @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\event;
|
||||
|
||||
defined("MOODLE_INTERNAL") || die();
|
||||
|
||||
/**
|
||||
* 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 service_deleted extends \core\event\base {
|
||||
/**
|
||||
* Init method.
|
||||
*/
|
||||
protected function init() {
|
||||
$this->data["crud"] = "d";
|
||||
$this->data["edulevel"] = self::LEVEL_OTHER;
|
||||
$this->data["objecttable"] = "local_webhooks_service";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return localised event name.
|
||||
*/
|
||||
public static function get_name() {
|
||||
return new \lang_string("deleted", "moodle");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of what happened.
|
||||
*/
|
||||
public function get_description() {
|
||||
return new \lang_string("eventwebserviceservicedeleted", "webservice");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL related to the action.
|
||||
*/
|
||||
public function get_url() {
|
||||
return new \moodle_url("/local/webhooks/index.php");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Registration of the system of events.
|
||||
*
|
||||
* @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\event;
|
||||
|
||||
defined("MOODLE_INTERNAL") || die();
|
||||
|
||||
/**
|
||||
* 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 service_deletedall extends \core\event\base {
|
||||
/**
|
||||
* Init method.
|
||||
*/
|
||||
protected function init() {
|
||||
$this->data["crud"] = "d";
|
||||
$this->data["edulevel"] = self::LEVEL_OTHER;
|
||||
$this->data["objecttable"] = "local_webhooks_service";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return localised event name.
|
||||
*/
|
||||
public static function get_name() {
|
||||
return new \lang_string("deleted", "moodle");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of what happened.
|
||||
*/
|
||||
public function get_description() {
|
||||
return new \lang_string("deletecompletely", "moodle");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL related to the action.
|
||||
*/
|
||||
public function get_url() {
|
||||
return new \moodle_url("/local/webhooks/index.php");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Registration of the system of events.
|
||||
*
|
||||
* @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\event;
|
||||
|
||||
defined("MOODLE_INTERNAL") || die();
|
||||
|
||||
/**
|
||||
* 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 service_updated extends \core\event\base {
|
||||
/**
|
||||
* Init method.
|
||||
*/
|
||||
protected function init() {
|
||||
$this->data["crud"] = "u";
|
||||
$this->data["edulevel"] = self::LEVEL_OTHER;
|
||||
$this->data["objecttable"] = "local_webhooks_service";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return localised event name.
|
||||
*/
|
||||
public static function get_name() {
|
||||
return new \lang_string("update", "moodle");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of what happened.
|
||||
*/
|
||||
public function get_description() {
|
||||
return new \lang_string("eventwebserviceserviceupdated", "webservice");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL related to the action.
|
||||
*/
|
||||
public function get_url() {
|
||||
return new \moodle_url("/local/webhooks/editservice.php", array("serviceid" => $this->objectid));
|
||||
}
|
||||
}
|
||||
@@ -1,105 +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();
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
$config = get_config("local_webhooks");
|
||||
|
||||
if (boolval($config->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) {
|
||||
$events = unserialize(gzuncompress(base64_decode($callback->events)));
|
||||
|
||||
if (boolval($events[$data["eventname"]])) {
|
||||
self::send($data, $callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sending data to the node.
|
||||
*
|
||||
* @param array $data
|
||||
* @param object $callback
|
||||
*/
|
||||
private static function send($data, $callback) {
|
||||
$curl = new curl();
|
||||
$package = self::packup($data);
|
||||
$curl::request($callback->url, $package);
|
||||
}
|
||||
|
||||
/**
|
||||
* Packs the data for transmission.
|
||||
*
|
||||
* @param array $data
|
||||
*/
|
||||
private static function packup($data) {
|
||||
return json_encode($data);
|
||||
}
|
||||
}
|
||||
@@ -1,112 +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) {
|
||||
$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->setType("title", PARAM_NOTAGS);
|
||||
$mform->addRule("title", null, "required");
|
||||
|
||||
/* Callback address */
|
||||
$mform->addElement("text", "url",
|
||||
new lang_string("url", "moodle"),
|
||||
array("size" => 60));
|
||||
$mform->setType("url", PARAM_URL);
|
||||
$mform->addRule("url", null, "required");
|
||||
|
||||
/* Enabling the service */
|
||||
$mform->addElement("checkbox", "enable",
|
||||
new lang_string("enable", "moodle"));
|
||||
$mform->setType("enable", PARAM_BOOL);
|
||||
$mform->setDefault("enable", 1);
|
||||
$mform->setAdvanced("enable");
|
||||
|
||||
/* 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("advcheckbox", $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);
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Create a class for sending data.
|
||||
* The event handler.
|
||||
*
|
||||
* @package local_webhooks
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
@@ -26,26 +26,29 @@ namespace local_webhooks;
|
||||
|
||||
defined("MOODLE_INTERNAL") || die();
|
||||
|
||||
class curl {
|
||||
public function __construct() {
|
||||
if (!function_exists("curl_init")) {
|
||||
print_error("nocurl", "mnet");
|
||||
require_once(__DIR__ . "/../lib.php");
|
||||
|
||||
/**
|
||||
* Defines event handlers.
|
||||
*
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class handler {
|
||||
/**
|
||||
* External handler.
|
||||
*
|
||||
* @param object $event
|
||||
*/
|
||||
public static function events($event) {
|
||||
$data = $event->get_data();
|
||||
|
||||
if ($callbacks = local_webhooks_get_list_records()) {
|
||||
foreach ($callbacks as $callback) {
|
||||
if (boolval($callback->enable) && !empty($callback->events[$data["eventname"]])) {
|
||||
local_webhooks_send_request($data, $callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function request($url, $data) {
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER,
|
||||
array(
|
||||
"Content-Type: application/json",
|
||||
"Content-Length: " . strlen($data))
|
||||
);
|
||||
|
||||
$result = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
<?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.
|
||||
*
|
||||
* @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();
|
||||
|
||||
require_once(__DIR__ . "/../lib.php");
|
||||
|
||||
require_once($CFG->libdir . "/formslib.php");
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the standard structure of the form.
|
||||
*/
|
||||
protected function definition() {
|
||||
$mform =& $this->_form;
|
||||
$size = array("size" => 60);
|
||||
|
||||
/* Form heading */
|
||||
$mform->addElement("header", "editserviceheader", new lang_string("service", "webservice"));
|
||||
|
||||
/* Name of the service */
|
||||
$mform->addElement("text", "title", new lang_string("name", "moodle"), $size);
|
||||
$mform->addRule("title", null, "required");
|
||||
$mform->setType("title", PARAM_NOTAGS);
|
||||
|
||||
/* Callback address */
|
||||
$mform->addElement("text", "url", new lang_string("url", "moodle"), $size);
|
||||
$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"), $size);
|
||||
$mform->setType("token", PARAM_NOTAGS);
|
||||
|
||||
/* Additional information */
|
||||
$mform->addElement("text", "other", new lang_string("sourceext", "plugin"), $size);
|
||||
$mform->setType("other", PARAM_NOTAGS);
|
||||
$mform->setAdvanced("other");
|
||||
|
||||
/* Content type */
|
||||
$contenttype = array("json" => "application/json", "x-www-form-urlencoded" => "application/x-www-form-urlencoded");
|
||||
$mform->addElement("select", "type", "Content type", $contenttype);
|
||||
$mform->setAdvanced("type");
|
||||
|
||||
/* Form heading */
|
||||
$mform->addElement("header", "editserviceheaderevent", new lang_string("edulevel", "moodle"));
|
||||
|
||||
/* List of events */
|
||||
$eventlist = local_webhooks_get_list_events();
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the form of restoration.
|
||||
*
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class service_backup_form extends moodleform {
|
||||
/**
|
||||
* @param string $baseurl
|
||||
*/
|
||||
public function __construct($baseurl) {
|
||||
parent::__construct($baseurl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the standard structure of the form.
|
||||
*/
|
||||
protected function definition() {
|
||||
$mform =& $this->_form;
|
||||
|
||||
/* Form heading */
|
||||
$mform->addElement("header", "editserviceheader", new lang_string("restore", "moodle"));
|
||||
|
||||
/* Flag for deleting existing data */
|
||||
$mform->addElement("advcheckbox", "deleterecords", new lang_string("deleteall", "moodle"));
|
||||
$mform->setType("deleterecords", PARAM_BOOL);
|
||||
$mform->setDefault("deleterecords", 1);
|
||||
|
||||
/* Download the file */
|
||||
$mform->addElement("filepicker", "backupfile", new lang_string("file", "moodle"));
|
||||
$mform->addRule("backupfile", null, "required");
|
||||
|
||||
/* Control Panel */
|
||||
$this->add_action_buttons(true, new lang_string("restore", "moodle"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Describes the plugin tables.
|
||||
*
|
||||
* @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();
|
||||
|
||||
require_once($CFG->libdir . "/tablelib.php");
|
||||
|
||||
/**
|
||||
* Describes the main table of the plugin.
|
||||
*
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class local_webhooks_table extends table_sql {
|
||||
/**
|
||||
* Manager address.
|
||||
*
|
||||
* @var string $manager
|
||||
*/
|
||||
protected static $manager = "/local/webhooks/index.php";
|
||||
|
||||
/**
|
||||
* Editor's address.
|
||||
*
|
||||
* @var string $editor
|
||||
*/
|
||||
protected static $editor = "/local/webhooks/editservice.php";
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $uniqueid The unique identifier of the table.
|
||||
*/
|
||||
public function __construct($uniqueid) {
|
||||
parent::__construct($uniqueid);
|
||||
$this->define_table_columns();
|
||||
$this->define_table_configs();
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the basic settings of the table.
|
||||
*/
|
||||
public function define_table_configs() {
|
||||
$this->set_sql("*", "{local_webhooks_service}", "1");
|
||||
$this->collapsible(false);
|
||||
$this->is_downloadable(false);
|
||||
$this->no_sorting("actions");
|
||||
$this->pageable(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the main columns and table headers.
|
||||
*/
|
||||
public function define_table_columns() {
|
||||
$columns = array(
|
||||
"title",
|
||||
"url",
|
||||
"events",
|
||||
"actions"
|
||||
);
|
||||
|
||||
$headers = array(
|
||||
new lang_string("name", "moodle"),
|
||||
new lang_string("url", "moodle"),
|
||||
new lang_string("edulevel", "moodle"),
|
||||
new lang_string("actions", "moodle")
|
||||
);
|
||||
|
||||
$this->define_columns($columns);
|
||||
$this->define_headers($headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies the display of a column with actions.
|
||||
*
|
||||
* @param object $row Data from the database.
|
||||
* @return string Displayed data.
|
||||
*/
|
||||
public function col_actions($row) {
|
||||
global $OUTPUT;
|
||||
|
||||
$hideshowicon = "t/show";
|
||||
$hideshowstring = new lang_string("enable", "moodle");
|
||||
if (boolval($row->enable)) {
|
||||
$hideshowicon = "t/hide";
|
||||
$hideshowstring = new lang_string("disable", "moodle");
|
||||
}
|
||||
|
||||
$hideshowlink = new moodle_url(self::$manager, array("hideshowid" => $row->id, "sesskey" => sesskey()));
|
||||
$hideshowitem = $OUTPUT->action_icon($hideshowlink, new pix_icon($hideshowicon, $hideshowstring));
|
||||
|
||||
$editlink = new moodle_url(self::$editor, array("serviceid" => $row->id));
|
||||
$edititem = $OUTPUT->action_icon($editlink, new pix_icon("t/edit", new lang_string("edit", "moodle")));
|
||||
|
||||
$deletelink = new moodle_url(self::$manager, array("deleteid" => $row->id, "sesskey" => sesskey()));
|
||||
$deleteitem = $OUTPUT->action_icon($deletelink, new pix_icon("t/delete", new lang_string("delete", "moodle")));
|
||||
|
||||
$html = $hideshowitem . $edititem . $deleteitem;
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies the display of a column with events.
|
||||
*
|
||||
* @param object $row Data from the database.
|
||||
* @return string Displayed data.
|
||||
*/
|
||||
public function col_events($row) {
|
||||
$eventlist = local_webhooks_deserialization_data($row->events);
|
||||
return count($eventlist);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies the display of the column with the service name.
|
||||
*
|
||||
* @param object $row Data from the database.
|
||||
* @return string Displayed data.
|
||||
*/
|
||||
public function col_title($row) {
|
||||
$link = new moodle_url(self::$editor, array("serviceid" => $row->id));
|
||||
$html = html_writer::link($link, $row->title);
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -26,10 +26,10 @@ defined("MOODLE_INTERNAL") || die();
|
||||
|
||||
$observers = array(
|
||||
array(
|
||||
"eventname" => "*",
|
||||
"callback" => "\local_webhooks\\events::handler",
|
||||
"callback" => "\local_webhooks\\handler::events",
|
||||
"eventname" => "*",
|
||||
"includefile" => null,
|
||||
"priority" => 200,
|
||||
"internal" => true
|
||||
"internal" => true,
|
||||
"priority" => 200
|
||||
)
|
||||
);
|
||||
+6
-3
@@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<XMLDB PATH="blocks/local_webhooks/db" VERSION="20171025" COMMENT="XMLDB file for Moodle" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd">
|
||||
<XMLDB PATH="blocks/local_webhooks/db" VERSION="20171029" COMMENT="XMLDB file for Moodle" 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.">
|
||||
<FIELDS>
|
||||
@@ -7,11 +7,14 @@
|
||||
<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="events" TYPE="text" 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="other" TYPE="text" NOTNULL="false" SEQUENCE="false" />
|
||||
</FIELDS>
|
||||
<KEYS>
|
||||
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
|
||||
</KEYS>
|
||||
</TABLE>
|
||||
</TABLES>
|
||||
</XMLDB>
|
||||
</XMLDB>
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
<?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.
|
||||
*
|
||||
* @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();
|
||||
|
||||
$functions = array(
|
||||
"local_webhooks_change_status" => array(
|
||||
"classname" => "local_webhooks_external",
|
||||
"methodname" => "change_status",
|
||||
"classpath" => "local/webhooks/externallib.php",
|
||||
"description" => "Change the status of the service.",
|
||||
"type" => "write"
|
||||
),
|
||||
|
||||
"local_webhooks_get_record" => array(
|
||||
"classname" => "local_webhooks_external",
|
||||
"methodname" => "get_record",
|
||||
"classpath" => "local/webhooks/externallib.php",
|
||||
"description" => "Get the record from the database.",
|
||||
"type" => "read"
|
||||
),
|
||||
|
||||
"local_webhooks_get_list_records" => array(
|
||||
"classname" => "local_webhooks_external",
|
||||
"methodname" => "get_list_records",
|
||||
"classpath" => "local/webhooks/externallib.php",
|
||||
"description" => "Get all records from the database.",
|
||||
"type" => "read"
|
||||
),
|
||||
|
||||
"local_webhooks_get_list_events" => array(
|
||||
"classname" => "local_webhooks_external",
|
||||
"methodname" => "get_list_events",
|
||||
"classpath" => "local/webhooks/externallib.php",
|
||||
"description" => "Get a list of all system events.",
|
||||
"type" => "read"
|
||||
),
|
||||
|
||||
"local_webhooks_create_record" => array(
|
||||
"classname" => "local_webhooks_external",
|
||||
"methodname" => "create_record",
|
||||
"classpath" => "local/webhooks/externallib.php",
|
||||
"description" => "Create an entry in the database.",
|
||||
"type" => "write"
|
||||
),
|
||||
|
||||
"local_webhooks_update_record" => array(
|
||||
"classname" => "local_webhooks_external",
|
||||
"methodname" => "update_record",
|
||||
"classpath" => "local/webhooks/externallib.php",
|
||||
"description" => "Update the record in the database.",
|
||||
"type" => "write"
|
||||
),
|
||||
|
||||
"local_webhooks_delete_record" => array(
|
||||
"classname" => "local_webhooks_external",
|
||||
"methodname" => "delete_record",
|
||||
"classpath" => "local/webhooks/externallib.php",
|
||||
"description" => "Delete the record from the database.",
|
||||
"type" => "write"
|
||||
),
|
||||
|
||||
"local_webhooks_delete_all_records" => array(
|
||||
"classname" => "local_webhooks_external",
|
||||
"methodname" => "delete_all_records",
|
||||
"classpath" => "local/webhooks/externallib.php",
|
||||
"description" => "Delete all records from the database.",
|
||||
"type" => "write"
|
||||
),
|
||||
|
||||
"local_webhooks_create_backup" => array(
|
||||
"classname" => "local_webhooks_external",
|
||||
"methodname" => "create_backup",
|
||||
"classpath" => "local/webhooks/externallib.php",
|
||||
"description" => "Create a backup.",
|
||||
"type" => "read"
|
||||
),
|
||||
|
||||
"local_webhooks_restore_backup" => array(
|
||||
"classname" => "local_webhooks_external",
|
||||
"methodname" => "restore_backup",
|
||||
"classpath" => "local/webhooks/externallib.php",
|
||||
"description" => "Restore from a backup.",
|
||||
"type" => "write"
|
||||
)
|
||||
);
|
||||
@@ -15,12 +15,38 @@
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Strings for component "local_webhooks", language "ru".
|
||||
* Keeps track of upgrades to the 'local_webhooks' plugin.
|
||||
*
|
||||
* @package local_webhooks
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
$string["managerservice"] = "Менеджер управления службами";
|
||||
$string["pluginname"] = "WebHooks";
|
||||
defined("MOODLE_INTERNAL") || die();
|
||||
|
||||
require_once(__DIR__ . "/../lib.php");
|
||||
|
||||
/**
|
||||
* Function to upgrade 'local_webhooks'.
|
||||
*
|
||||
* @param number $oldversion
|
||||
* @return boolean
|
||||
*/
|
||||
function xmldb_local_webhooks_upgrade($oldversion) {
|
||||
global $CFG, $DB;
|
||||
|
||||
/* Update from version 3.0.0 */
|
||||
if ($oldversion < 2017112600) {
|
||||
$rs = $DB->get_recordset("local_webhooks_service", null, "id", "*", 0, 0);
|
||||
foreach ($rs as $record) {
|
||||
if (!empty($record->events)) {
|
||||
$record->events = unserialize(gzuncompress(base64_decode($record->events)));
|
||||
local_webhooks_update_record($record);
|
||||
}
|
||||
}
|
||||
$rs->close();
|
||||
upgrade_plugin_savepoint(true, 2017112600, "local", "webhooks");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
# Getting Started
|
||||
|
||||
## Navigation
|
||||
|
||||
* [Installation](#installation)
|
||||
* [Build](#build)
|
||||
* [User guide](#user-guide)
|
||||
* [Guide to the manager](#guide-to-the-manager)
|
||||
* [Guide to the editor](#guide-to-the-editor)
|
||||
* [Request format](#request-format)
|
||||
|
||||
## Installation
|
||||
|
||||
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
|
||||
```
|
||||
|
||||
## User guide
|
||||
|
||||
After you install the plug-in, the Service Manager appears on the `Server` tab in the administration panel.
|
||||
|
||||
To open the page, go:
|
||||
`Site administration` ->
|
||||
`Server` ->
|
||||
`WebHooks`.
|
||||
|
||||
### Guide to the manager
|
||||
|
||||
The main window looks like this:
|
||||
|
||||

|
||||
|
||||
On the main page you can see the list of services and manage each service separately.
|
||||
|
||||
Column description
|
||||
* "**Name**" shows the name of the service.
|
||||
* "**URL**" indicates the address of the service to which notifications are received.
|
||||
* "**Actions**" allows you to enable / disable the service, edit and delete.
|
||||
|
||||
Description of control buttons:
|
||||
* "**Add service**" opens a page for creating a new service.
|
||||
* "**Backup**" loads the backup file of the service list.
|
||||
* "**Restore**" redirects the data recovery page from the backup.
|
||||
|
||||
### Guide to the editor
|
||||
|
||||
During the editing and creation of the service, this page opens:
|
||||
|
||||

|
||||
|
||||
Description of fields:
|
||||
* "**Name**" stores an arbitrary name for the service.
|
||||
* "**URL**" stores the address of the service to which notifications will be sent as a POST request.
|
||||
* "**Token**" allows you to specify an individual key that will allow an external service to identify requests.
|
||||
* "**Advanced**" stores a large string passed to the service.
|
||||
This can be useful for some services or users.
|
||||
* "**Content Type**" allows you to configure the type of outbound requests if there are compatibility issues.
|
||||
|
||||
The "**All events**" list contains a list of all events registered in the system.
|
||||
The selected events will notify the service.
|
||||
|
||||
## Request format
|
||||
|
||||
Events come in
|
||||
[JSON](https://en.wikipedia.org/wiki/JSON)
|
||||
format.
|
||||
|
||||
An example of an observed course event:
|
||||
|
||||
```JSON
|
||||
{
|
||||
"eventname": "\\core\\event\\course_viewed",
|
||||
"component": "core",
|
||||
"action": "viewed",
|
||||
"target": "course",
|
||||
"objecttable": null,
|
||||
"objectid": null,
|
||||
"crud": "r",
|
||||
"edulevel": 2,
|
||||
"contextid": 2,
|
||||
"contextlevel": 50,
|
||||
"contextinstanceid": "1",
|
||||
"userid": "2",
|
||||
"courseid": "1",
|
||||
"relateduserid": null,
|
||||
"anonymous": 0,
|
||||
"other": null,
|
||||
"timecreated": 1512961456,
|
||||
"host": "localhost",
|
||||
"token": "",
|
||||
"extra": ""
|
||||
}
|
||||
```
|
||||
|
||||
[Detailed description of the fields](https://docs.moodle.org/dev/Event_2#Properties).
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 165 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 122 KiB |
+23
-36
@@ -15,7 +15,7 @@
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Service editor.
|
||||
* Page for editing the service.
|
||||
*
|
||||
* @package local_webhooks
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
@@ -23,28 +23,25 @@
|
||||
*/
|
||||
|
||||
require_once(__DIR__ . "/../../config.php");
|
||||
require_once(__DIR__ . "/classes/forms.php");
|
||||
require_once(__DIR__ . "/classes/service_form.php");
|
||||
require_once(__DIR__ . "/lib.php");
|
||||
|
||||
$idservice = optional_param("idservice", 0, PARAM_INT);
|
||||
require_once($CFG->libdir . "/adminlib.php");
|
||||
|
||||
require_login();
|
||||
/* Optional parameters */
|
||||
$serviceid = optional_param("serviceid", 0, PARAM_INT);
|
||||
|
||||
/* Link generation */
|
||||
$urlparameters = array("idservice" => $idservice);
|
||||
$managerservice = new moodle_url("/local/webhooks/managerservice.php", $urlparameters);
|
||||
$baseurl = new moodle_url("/local/webhooks/editservice.php", $urlparameters);
|
||||
$PAGE->set_url($baseurl, $urlparameters);
|
||||
$urlparameters = array("serviceid" => $serviceid);
|
||||
$baseurl = new moodle_url("/local/webhooks/editservice.php", $urlparameters);
|
||||
$managerservice = new moodle_url("/local/webhooks/index.php");
|
||||
|
||||
/* Configure the context of the page */
|
||||
admin_externalpage_setup("local_webhooks", "", null, $baseurl, array());
|
||||
$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);
|
||||
$mform = new service_edit_form($PAGE->url);
|
||||
|
||||
/* Cancel processing */
|
||||
if ($mform->is_cancelled()) {
|
||||
@@ -52,43 +49,33 @@ if ($mform->is_cancelled()) {
|
||||
}
|
||||
|
||||
/* Getting the data */
|
||||
if ($idediting = boolval($idservice)) {
|
||||
$servicerecord = $DB->get_record("local_webhooks_service", array("id" => $idservice), "*", MUST_EXIST);
|
||||
$servicerecord = new stdClass();
|
||||
if ($editing = boolval($serviceid)) {
|
||||
$servicerecord = local_webhooks_get_record($serviceid);
|
||||
$mform->set_data($servicerecord);
|
||||
}
|
||||
|
||||
/* Processing of received data */
|
||||
if ($data = $mform->get_data()) {
|
||||
if (empty($data->enable)) {
|
||||
$data->enable = 0;
|
||||
}
|
||||
|
||||
/* Packing of data */
|
||||
$data->events = base64_encode(gzcompress(serialize($data->events), 9));
|
||||
|
||||
if ($idediting) {
|
||||
$data->id = $idservice;
|
||||
$DB->update_record("local_webhooks_service", $data);
|
||||
if ($editing) {
|
||||
$data->id = $serviceid;
|
||||
local_webhooks_update_record($data);
|
||||
} else {
|
||||
$DB->insert_record("local_webhooks_service", $data);
|
||||
local_webhooks_create_record($data);
|
||||
}
|
||||
|
||||
redirect($managerservice);
|
||||
redirect($managerservice, new lang_string("changessaved", "moodle"));
|
||||
}
|
||||
|
||||
/* Page template */
|
||||
$PAGE->set_pagelayout("admin");
|
||||
/* The page title */
|
||||
$titlepage = new lang_string("externalservice", "webservice");
|
||||
$PAGE->navbar->add($titlepage);
|
||||
$PAGE->set_heading($titlepage);
|
||||
$PAGE->set_title($titlepage);
|
||||
|
||||
/* The page title */
|
||||
$PAGE->navbar->add(new lang_string("localplugins", "moodle"));
|
||||
$PAGE->navbar->add(new lang_string("pluginname", "local_webhooks"));
|
||||
$PAGE->navbar->add(new lang_string("managerservice", "local_webhooks"), $managerservice);
|
||||
$PAGE->navbar->add($titlepage);
|
||||
echo $OUTPUT->header();
|
||||
|
||||
/* Displays the form */
|
||||
$mform->display();
|
||||
|
||||
/* Footer */
|
||||
echo $OUTPUT->footer();
|
||||
+589
@@ -0,0 +1,589 @@
|
||||
<?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 defines the plugin's external functions.
|
||||
*
|
||||
* @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();
|
||||
|
||||
require_once(__DIR__ . "/lib.php");
|
||||
|
||||
require_once($CFG->libdir . "/externallib.php");
|
||||
|
||||
/**
|
||||
* External functions.
|
||||
*
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class local_webhooks_external extends external_api {
|
||||
/**
|
||||
* Returns description of method parameters.
|
||||
*
|
||||
* @return external_function_parameters
|
||||
* @since Moodle 2.9 Options available
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function change_status_parameters() {
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
"serviceid" => new external_value(PARAM_INT, "Service identifier.")
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the status of the service.
|
||||
*
|
||||
* @param number $serviceid
|
||||
* @return boolean
|
||||
* @since Moodle 2.9 Options available
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function change_status($serviceid = 0) {
|
||||
$parameters = self::validate_parameters(self::change_status_parameters(), array("serviceid" => $serviceid));
|
||||
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
$result = local_webhooks_change_status($parameters["serviceid"]);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method result value.
|
||||
*
|
||||
* @return external_description
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function change_status_returns() {
|
||||
return new external_value(PARAM_BOOL, "The result of the operation.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method parameters.
|
||||
*
|
||||
* @return external_function_parameters
|
||||
* @since Moodle 2.9 Options available
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function get_record_parameters() {
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
"serviceid" => new external_value(PARAM_INT, "Service identifier.")
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the record from the database.
|
||||
*
|
||||
* @param number $serviceid
|
||||
* @return array
|
||||
* @since Moodle 2.9 Options available
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function get_record($serviceid = 0) {
|
||||
$parameters = self::validate_parameters(self::get_record_parameters(), array("serviceid" => $serviceid));
|
||||
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
$service = array();
|
||||
if ($record = local_webhooks_get_record($parameters["serviceid"])) {
|
||||
$service["enable"] = $record->enable;
|
||||
$service["id"] = $record->id;
|
||||
$service["other"] = $record->other;
|
||||
$service["title"] = $record->title;
|
||||
$service["token"] = $record->token;
|
||||
$service["type"] = $record->type;
|
||||
$service["url"] = $record->url;
|
||||
|
||||
$service["events"] = array();
|
||||
foreach ($record->events as $key => $value) {
|
||||
$service["events"][] = array("name" => $key, "value" => $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method result value.
|
||||
*
|
||||
* @return external_description
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function get_record_returns() {
|
||||
return new external_single_structure(
|
||||
array(
|
||||
"id" => new external_value(PARAM_INT, "Service identifier."),
|
||||
"enable" => new external_value(PARAM_INT, "Service status."),
|
||||
"title" => new external_value(PARAM_TEXT, "Name of the service."),
|
||||
"url" => new external_value(PARAM_URL, "URL address."),
|
||||
"type" => new external_value(PARAM_TEXT, "Header type."),
|
||||
"token" => new external_value(PARAM_TEXT, "Authorization key.", VALUE_OPTIONAL),
|
||||
"other" => new external_value(PARAM_TEXT, "Additional field.", VALUE_OPTIONAL),
|
||||
"events" => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array(
|
||||
"name" => new external_value(PARAM_TEXT, "Event name."),
|
||||
"value" => new external_value(PARAM_INT, "Observation status.")
|
||||
)
|
||||
), "Observed events.", VALUE_OPTIONAL
|
||||
)
|
||||
), "Record about the service."
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method parameters.
|
||||
*
|
||||
* @return external_function_parameters
|
||||
* @since Moodle 2.9 Options available
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function get_list_records_parameters() {
|
||||
return new external_function_parameters(array());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all records from the database.
|
||||
*
|
||||
* @return array
|
||||
* @since Moodle 2.9 Options available
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function get_list_records() {
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
$result = array();
|
||||
if ($listrecords = local_webhooks_get_list_records()) {
|
||||
foreach ($listrecords as $index => $record) {
|
||||
$result[$index]["enable"] = $record->enable;
|
||||
$result[$index]["id"] = $record->id;
|
||||
$result[$index]["other"] = $record->other;
|
||||
$result[$index]["title"] = $record->title;
|
||||
$result[$index]["token"] = $record->token;
|
||||
$result[$index]["type"] = $record->type;
|
||||
$result[$index]["url"] = $record->url;
|
||||
|
||||
$result[$index]["events"] = array();
|
||||
foreach ($record->events as $key => $value) {
|
||||
$result[$index]["events"][] = array("name" => $key, "value" => $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method result value.
|
||||
*
|
||||
* @return external_description
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function get_list_records_returns() {
|
||||
return new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array(
|
||||
"id" => new external_value(PARAM_INT, "Service identifier."),
|
||||
"enable" => new external_value(PARAM_INT, "Service status."),
|
||||
"title" => new external_value(PARAM_TEXT, "Name of the service."),
|
||||
"url" => new external_value(PARAM_URL, "URL address."),
|
||||
"type" => new external_value(PARAM_TEXT, "Header type."),
|
||||
"token" => new external_value(PARAM_TEXT, "Authorization key.", VALUE_OPTIONAL),
|
||||
"other" => new external_value(PARAM_TEXT, "Additional field.", VALUE_OPTIONAL),
|
||||
"events" => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array(
|
||||
"name" => new external_value(PARAM_TEXT, "Event name."),
|
||||
"value" => new external_value(PARAM_INT, "Observation status.")
|
||||
)
|
||||
), "Observed events.", VALUE_OPTIONAL
|
||||
)
|
||||
), "Record about the service."
|
||||
), "List of services."
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method parameters.
|
||||
*
|
||||
* @return external_function_parameters
|
||||
* @since Moodle 2.9 Options available
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function get_list_events_parameters() {
|
||||
return new external_function_parameters(array());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of all system events.
|
||||
*
|
||||
* @return array
|
||||
* @since Moodle 2.9 Options available
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function get_list_events() {
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
$result = array();
|
||||
if ($eventlist = local_webhooks_get_list_events()) {
|
||||
foreach ($eventlist as $item) {
|
||||
$result[] = $item["eventname"];
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method result value.
|
||||
*
|
||||
* @return external_description
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function get_list_events_returns() {
|
||||
return new external_multiple_structure(
|
||||
new external_value(PARAM_TEXT, "Event name.")
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method parameters.
|
||||
*
|
||||
* @return external_function_parameters
|
||||
* @since Moodle 2.9 Options available
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function create_record_parameters() {
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
"service" => new external_single_structure(
|
||||
array(
|
||||
"enable" => new external_value(PARAM_BOOL, "Service status.", VALUE_OPTIONAL),
|
||||
"title" => new external_value(PARAM_TEXT, "Name of the service."),
|
||||
"url" => new external_value(PARAM_URL, "URL address."),
|
||||
"type" => new external_value(PARAM_TEXT, "Header type.", VALUE_OPTIONAL),
|
||||
"token" => new external_value(PARAM_TEXT, "Authorization key.", VALUE_OPTIONAL),
|
||||
"other" => new external_value(PARAM_TEXT, "Additional field.", VALUE_OPTIONAL),
|
||||
"events" => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array(
|
||||
"name" => new external_value(PARAM_TEXT, "Event name."),
|
||||
"value" => new external_value(PARAM_INT, "Observation status.")
|
||||
)
|
||||
), "Observed events.", VALUE_OPTIONAL
|
||||
)
|
||||
), "Record about the service."
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an entry in the database.
|
||||
*
|
||||
* @param array $service
|
||||
* @return number
|
||||
* @since Moodle 2.9 Options available
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function create_record($service = array()) {
|
||||
$parameters = self::validate_parameters(self::create_record_parameters(), array("service" => $service));
|
||||
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
$record = new stdClass();
|
||||
$record->other = $parameters["service"]["other"];
|
||||
$record->title = $parameters["service"]["title"];
|
||||
$record->token = $parameters["service"]["token"];
|
||||
$record->url = $parameters["service"]["url"];
|
||||
|
||||
$record->enable = !empty($parameters["service"]["enable"]) ? $parameters["service"]["enable"] : false;
|
||||
$record->type = !empty($parameters["service"]["type"]) ? $parameters["service"]["type"] : "json";
|
||||
|
||||
$record->events = array();
|
||||
foreach ($parameters["service"]["events"] as $value) {
|
||||
$record->events[$value["name"]] = $value["value"];
|
||||
}
|
||||
|
||||
$result = local_webhooks_create_record($record);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method result value.
|
||||
*
|
||||
* @return external_description
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function create_record_returns() {
|
||||
return new external_value(PARAM_INT, "Service identifier.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method parameters.
|
||||
*
|
||||
* @return external_function_parameters
|
||||
* @since Moodle 2.9 Options available
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function update_record_parameters() {
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
"id" => new external_value(PARAM_INT, "Service identifier."),
|
||||
"service" => new external_single_structure(
|
||||
array(
|
||||
"enable" => new external_value(PARAM_BOOL, "Service status.", VALUE_OPTIONAL),
|
||||
"title" => new external_value(PARAM_TEXT, "Name of the service.", VALUE_OPTIONAL),
|
||||
"url" => new external_value(PARAM_URL, "URL address.", VALUE_OPTIONAL),
|
||||
"type" => new external_value(PARAM_TEXT, "Header type.", VALUE_OPTIONAL),
|
||||
"token" => new external_value(PARAM_TEXT, "Authorization key.", VALUE_OPTIONAL),
|
||||
"other" => new external_value(PARAM_TEXT, "Additional field.", VALUE_OPTIONAL),
|
||||
"events" => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array(
|
||||
"name" => new external_value(PARAM_TEXT, "Event name."),
|
||||
"value" => new external_value(PARAM_INT, "Observation status.")
|
||||
)
|
||||
), "Observed events.", VALUE_OPTIONAL
|
||||
)
|
||||
), "Record about the service."
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the record in the database.
|
||||
*
|
||||
* @param number $serviceid
|
||||
* @param array $service
|
||||
* @return boolean
|
||||
* @since Moodle 2.9 Options available
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function update_record($serviceid = 0, $service = array()) {
|
||||
$parameters = self::validate_parameters(self::update_record_parameters(), array("id" => $serviceid, "service" => $service));
|
||||
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
$result = false;
|
||||
if ($record = local_webhooks_get_record($parameters["id"])) {
|
||||
$record->enable = !empty($parameters["service"]["enable"]) ? $parameters["service"]["enable"] : $record->enable;
|
||||
$record->other = !empty($parameters["service"]["other"]) ? $parameters["service"]["other"] : $record->other;
|
||||
$record->title = !empty($parameters["service"]["title"]) ? $parameters["service"]["title"] : $record->title;
|
||||
$record->token = !empty($parameters["service"]["token"]) ? $parameters["service"]["token"] : $record->token;
|
||||
$record->type = !empty($parameters["service"]["type"]) ? $parameters["service"]["type"] : $record->type;
|
||||
$record->url = !empty($parameters["service"]["url"]) ? $parameters["service"]["url"] : $record->url;
|
||||
|
||||
if (!empty($parameters["service"]["events"])) {
|
||||
$record->events = array();
|
||||
foreach ($parameters["service"]["events"] as $value) {
|
||||
$record->events[$value["name"]] = $value["value"];
|
||||
}
|
||||
}
|
||||
|
||||
$result = local_webhooks_update_record($record);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method result value.
|
||||
*
|
||||
* @return external_description
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function update_record_returns() {
|
||||
return new external_value(PARAM_BOOL, "The result of the operation.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method parameters.
|
||||
*
|
||||
* @return external_function_parameters
|
||||
* @since Moodle 2.9 Options available
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function delete_record_parameters() {
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
"serviceid" => new external_value(PARAM_INT, "Service identifier.")
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the record from the database.
|
||||
*
|
||||
* @param number $serviceid
|
||||
* @return boolean
|
||||
* @since Moodle 2.9 Options available
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function delete_record($serviceid = 0) {
|
||||
$parameters = self::validate_parameters(self::delete_record_parameters(), array("serviceid" => $serviceid));
|
||||
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
$result = local_webhooks_delete_record($parameters["serviceid"]);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method result value.
|
||||
*
|
||||
* @return external_description
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function delete_record_returns() {
|
||||
return new external_value(PARAM_BOOL, "The result of the operation.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method parameters.
|
||||
*
|
||||
* @return external_function_parameters
|
||||
* @since Moodle 2.9 Options available
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function delete_all_records_parameters() {
|
||||
return new external_function_parameters(array());
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all records from the database.
|
||||
*
|
||||
* @return boolean
|
||||
* @since Moodle 2.9 Options available
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function delete_all_records() {
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
$result = local_webhooks_delete_all_records();
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method result value.
|
||||
*
|
||||
* @return external_description
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function delete_all_records_returns() {
|
||||
return new external_value(PARAM_BOOL, "The result of the operation.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method parameters.
|
||||
*
|
||||
* @return external_function_parameters
|
||||
* @since Moodle 2.9 Options available
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function create_backup_parameters() {
|
||||
return new external_function_parameters(array());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a backup.
|
||||
*
|
||||
* @return string
|
||||
* @since Moodle 2.9 Options available
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function create_backup() {
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
$result = local_webhooks_create_backup();
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method result value.
|
||||
*
|
||||
* @return external_description
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function create_backup_returns() {
|
||||
return new external_value(PARAM_TEXT, "Backup copy.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method parameters.
|
||||
*
|
||||
* @return external_function_parameters
|
||||
* @since Moodle 2.9 Options available
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function restore_backup_parameters() {
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
"options" => new external_single_structure(
|
||||
array(
|
||||
"backup" => new external_value(PARAM_TEXT, "Backup copy."),
|
||||
"deleterecords" => new external_value(PARAM_BOOL, "Delete existing records.", VALUE_OPTIONAL)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore from a backup.
|
||||
*
|
||||
* @param array $options
|
||||
* @since Moodle 2.9 Options available
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function restore_backup($options = array()) {
|
||||
$parameters = self::validate_parameters(self::restore_backup_parameters(), array("options" => $options));
|
||||
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
$deleterecords = !empty($parameters["options"]["deleterecords"]) ? boolval($parameters["options"]["deleterecords"]) : false;
|
||||
local_webhooks_restore_backup($parameters["options"]["backup"], $deleterecords);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method result value.
|
||||
*
|
||||
* @return external_description
|
||||
* @since Moodle 2.2
|
||||
*/
|
||||
public static function restore_backup_returns() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?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(__DIR__ . "/classes/webhooks_table.php");
|
||||
require_once(__DIR__ . "/lib.php");
|
||||
|
||||
require_once($CFG->libdir . "/adminlib.php");
|
||||
|
||||
/* Optional parameters */
|
||||
$backupservices = optional_param("getbackup", 0, PARAM_BOOL);
|
||||
$deleteall = optional_param("deleteall", 0, PARAM_INT);
|
||||
$deleteid = optional_param("deleteid", 0, PARAM_INT);
|
||||
$hideshowid = optional_param("hideshowid", 0, PARAM_INT);
|
||||
|
||||
/* Link generation */
|
||||
$editservice = "/local/webhooks/editservice.php";
|
||||
$managerservice = "/local/webhooks/index.php";
|
||||
$restorebackup = "/local/webhooks/restorebackup.php";
|
||||
$baseurl = new moodle_url($managerservice);
|
||||
|
||||
/* Configure the context of the page */
|
||||
admin_externalpage_setup("local_webhooks", "", null, $baseurl, array());
|
||||
$context = context_system::instance();
|
||||
|
||||
/* Delete the service */
|
||||
if (!empty($deleteid) && confirm_sesskey()) {
|
||||
local_webhooks_delete_record($deleteid);
|
||||
redirect($PAGE->url, new lang_string("deleted", "moodle"));
|
||||
}
|
||||
|
||||
/* Switching the status of the service */
|
||||
if (!empty($hideshowid) && confirm_sesskey()) {
|
||||
local_webhooks_change_status($hideshowid);
|
||||
redirect($PAGE->url, new lang_string("changessaved", "moodle"));
|
||||
}
|
||||
|
||||
/* Deletes all data */
|
||||
if (boolval($deleteall) && confirm_sesskey()) {
|
||||
local_webhooks_delete_all_records();
|
||||
redirect($PAGE->url, new lang_string("deleted", "moodle"));
|
||||
}
|
||||
|
||||
/* Upload settings as a file */
|
||||
if (boolval($backupservices)) {
|
||||
$filecontent = local_webhooks_create_backup();
|
||||
$filename = "webhooks_" . date("U") . ".backup";
|
||||
send_file($filecontent, $filename, 0, 0, true, true);
|
||||
}
|
||||
|
||||
/* The page title */
|
||||
$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($editservice);
|
||||
echo $OUTPUT->single_button($addserviceurl, new lang_string("add", "moodle"));
|
||||
|
||||
/* Adds a delete button */
|
||||
$deleteallurl = new moodle_url($managerservice, array("deleteall" => true, "sesskey" => sesskey()));
|
||||
echo $OUTPUT->single_button($deleteallurl, new lang_string("deleteall", "moodle"), "get");
|
||||
|
||||
/* Adds a backup button */
|
||||
$backupurl = new moodle_url($managerservice, array("getbackup" => true));
|
||||
echo $OUTPUT->single_button($backupurl, new lang_string("backup", "moodle"), "get");
|
||||
|
||||
/* Adds a restore button */
|
||||
$restorebackupurl = new moodle_url($restorebackup);
|
||||
echo $OUTPUT->single_button($restorebackupurl, new lang_string("restore", "moodle"));
|
||||
|
||||
/* Displays the table */
|
||||
$table = new local_webhooks_table("local-webhooks-table");
|
||||
$table->define_baseurl($baseurl);
|
||||
$table->out(25, true);
|
||||
|
||||
/* Footer */
|
||||
echo $OUTPUT->footer();
|
||||
@@ -22,5 +22,4 @@
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
$string["managerservice"] = "Service management manager";
|
||||
$string["pluginname"] = "WebHooks";
|
||||
@@ -0,0 +1,232 @@
|
||||
<?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 contains the functions used by 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();
|
||||
|
||||
require_once(__DIR__ . "/locallib.php");
|
||||
|
||||
/**
|
||||
* Change the status of the service.
|
||||
*
|
||||
* @param number $serviceid
|
||||
* @return boolean
|
||||
*/
|
||||
function local_webhooks_change_status($serviceid) {
|
||||
global $DB;
|
||||
|
||||
$result = false;
|
||||
if ($record = local_webhooks_get_record($serviceid)) {
|
||||
$record->enable = !boolval($record->enable);
|
||||
$result = local_webhooks_update_record($record);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the record from the database.
|
||||
*
|
||||
* @param number $serviceid
|
||||
* @return object
|
||||
*/
|
||||
function local_webhooks_get_record($serviceid) {
|
||||
global $DB;
|
||||
|
||||
$servicerecord = $DB->get_record("local_webhooks_service", array("id" => $serviceid), "*", MUST_EXIST);
|
||||
|
||||
if (!empty($servicerecord->events)) {
|
||||
$servicerecord->events = local_webhooks_deserialization_data($servicerecord->events);
|
||||
}
|
||||
|
||||
return $servicerecord;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all records from the database.
|
||||
*
|
||||
* @param number $limitfrom
|
||||
* @param number $limitnum
|
||||
* @return array
|
||||
*/
|
||||
function local_webhooks_get_list_records($limitfrom = 0, $limitnum = 0) {
|
||||
global $DB;
|
||||
|
||||
$listrecords = $DB->get_records("local_webhooks_service", null, "id", "*", $limitfrom, $limitnum);
|
||||
|
||||
foreach ($listrecords as $servicerecord) {
|
||||
if (!empty($servicerecord->events)) {
|
||||
$servicerecord->events = local_webhooks_deserialization_data($servicerecord->events);
|
||||
}
|
||||
}
|
||||
|
||||
return $listrecords;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of all system events.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function local_webhooks_get_list_events() {
|
||||
$eventlist = report_eventlist_list_generator::get_all_events_list(true);
|
||||
return $eventlist;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an entry in the database.
|
||||
*
|
||||
* @param object $record
|
||||
* @return number
|
||||
*/
|
||||
function local_webhooks_create_record($record) {
|
||||
global $DB;
|
||||
|
||||
if (!empty($record->events)) {
|
||||
$record->events = local_webhooks_serialization_data($record->events);
|
||||
}
|
||||
|
||||
$result = $DB->insert_record("local_webhooks_service", $record, true, false);
|
||||
local_webhooks_events::service_added($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the record in the database.
|
||||
*
|
||||
* @param object $data
|
||||
* @return boolean
|
||||
*/
|
||||
function local_webhooks_update_record($record) {
|
||||
global $DB;
|
||||
|
||||
if (empty($record->id)) {
|
||||
print_error("missingparam", "error", null, "id");
|
||||
}
|
||||
|
||||
$record->events = !empty($record->events) ? local_webhooks_serialization_data($record->events) : null;
|
||||
$result = $DB->update_record("local_webhooks_service", $record, false);
|
||||
local_webhooks_events::service_updated($record->id);
|
||||
return boolval($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the record from the database.
|
||||
*
|
||||
* @param number $serviceid
|
||||
* @return boolean
|
||||
*/
|
||||
function local_webhooks_delete_record($serviceid) {
|
||||
global $DB;
|
||||
$result = $DB->delete_records("local_webhooks_service", array("id" => $serviceid));
|
||||
local_webhooks_events::service_deleted($serviceid);
|
||||
return boolval($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all records from the database.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
function local_webhooks_delete_all_records() {
|
||||
global $DB;
|
||||
$result = $DB->delete_records("local_webhooks_service", null);
|
||||
local_webhooks_events::service_deletedall();
|
||||
return boolval($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a backup.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function local_webhooks_create_backup() {
|
||||
$listrecords = local_webhooks_get_list_records();
|
||||
$result = local_webhooks_serialization_data($listrecords);
|
||||
local_webhooks_events::backup_performed();
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore from a backup.
|
||||
*
|
||||
* @param string $data
|
||||
*/
|
||||
function local_webhooks_restore_backup($data, $deleterecords = false) {
|
||||
$listrecords = local_webhooks_deserialization_data($data);
|
||||
|
||||
if (boolval($deleterecords)) {
|
||||
local_webhooks_delete_all_records();
|
||||
}
|
||||
|
||||
foreach ($listrecords as $servicerecord) {
|
||||
local_webhooks_create_record($servicerecord);
|
||||
}
|
||||
|
||||
local_webhooks_events::backup_restored();
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the event remotely to the service.
|
||||
*
|
||||
* @param array $event
|
||||
* @param object $callback
|
||||
* @return array
|
||||
*/
|
||||
function local_webhooks_send_request($event, $callback) {
|
||||
global $CFG;
|
||||
|
||||
$event["host"] = parse_url($CFG->wwwroot)["host"];
|
||||
$event["token"] = $callback->token;
|
||||
$event["extra"] = $callback->other;
|
||||
|
||||
$curl = new curl();
|
||||
$curl->setHeader(array("Content-Type: application/" . $callback->type));
|
||||
$curl->post($callback->url, json_encode($event));
|
||||
|
||||
$response = $curl->getResponse();
|
||||
local_webhooks_events::response_answer($callback->id, $response);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Data serialization.
|
||||
*
|
||||
* @param array|object $data
|
||||
* @return string
|
||||
*/
|
||||
function local_webhooks_serialization_data($data) {
|
||||
$result = serialize($data);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Data deserialization.
|
||||
*
|
||||
* @param string $data
|
||||
* @return array|object
|
||||
*/
|
||||
function local_webhooks_deserialization_data($data) {
|
||||
$result = unserialize($data);
|
||||
return $result;
|
||||
}
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Classes of modules.
|
||||
*
|
||||
* @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();
|
||||
|
||||
/**
|
||||
* Description of functions of the call of events
|
||||
*
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class local_webhooks_events {
|
||||
/**
|
||||
* Call the event when creating a backup.
|
||||
*/
|
||||
public static function backup_performed() {
|
||||
$context = context_system::instance();
|
||||
$event = local_webhooks\event\backup_performed::create(array("context" => $context, "objectid" => 0));
|
||||
$event->trigger();
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the event when restoring from a backup.
|
||||
*/
|
||||
public static function backup_restored() {
|
||||
$context = context_system::instance();
|
||||
$event = local_webhooks\event\backup_restored::create(array("context" => $context, "objectid" => 0));
|
||||
$event->trigger();
|
||||
}
|
||||
|
||||
/**
|
||||
* Call event when the response is received from the service
|
||||
*
|
||||
* @param number $objectid Service ID
|
||||
* @param array $response Server response
|
||||
*/
|
||||
public static function response_answer($objectid = 0, $response = array()) {
|
||||
$context = context_system::instance();
|
||||
$status = "Error sending request";
|
||||
if (!empty($response["HTTP/1.1"])) {
|
||||
$status = $response["HTTP/1.1"];
|
||||
}
|
||||
|
||||
$event = local_webhooks\event\response_answer::create(array("context" => $context, "objectid" => $objectid, "other" => array("status" => $status)));
|
||||
$event->trigger();
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the event when the service is added.
|
||||
*
|
||||
* @param number $objectid Service ID
|
||||
*/
|
||||
public static function service_added($objectid = 0) {
|
||||
$context = context_system::instance();
|
||||
$event = local_webhooks\event\service_added::create(array("context" => $context, "objectid" => $objectid));
|
||||
$event->trigger();
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the event when the service is deleted.
|
||||
*
|
||||
* @param number $objectid Service ID
|
||||
*/
|
||||
public static function service_deleted($objectid = 0) {
|
||||
$context = context_system::instance();
|
||||
$event = local_webhooks\event\service_deleted::create(array("context" => $context, "objectid" => $objectid));
|
||||
$event->trigger();
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the event when all services are deleted.
|
||||
*/
|
||||
public static function service_deletedall() {
|
||||
$context = context_system::instance();
|
||||
$event = local_webhooks\event\service_deletedall::create(array("context" => $context, "objectid" => 0));
|
||||
$event->trigger();
|
||||
}
|
||||
|
||||
/**
|
||||
* Call event when the service is updated.
|
||||
*
|
||||
* @param number $objectid Service ID
|
||||
*/
|
||||
public static function service_updated($objectid = 0) {
|
||||
$context = context_system::instance();
|
||||
$event = local_webhooks\event\service_updated::create(array("context" => $context, "objectid" => $objectid));
|
||||
$event->trigger();
|
||||
}
|
||||
}
|
||||
@@ -1,105 +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");
|
||||
|
||||
$deleteservice = optional_param("deleteservice", 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 ($deleteservice && confirm_sesskey()) {
|
||||
$DB->delete_records("local_webhooks_service", array("id" => $deleteservice));
|
||||
redirect($PAGE->url, new lang_string("deleted", "moodle"));
|
||||
}
|
||||
|
||||
/* Retrieving a list of services */
|
||||
$select = null;
|
||||
$callbacks = $DB->get_records_select("local_webhooks_service", $select, null, $DB->sql_order_by_text("id"));
|
||||
|
||||
/* Page template */
|
||||
$titlepage = new lang_string("managerservice", "local_webhooks");
|
||||
$PAGE->set_pagelayout("standard");
|
||||
$PAGE->set_title($titlepage);
|
||||
$PAGE->set_heading($titlepage);
|
||||
|
||||
/* The page title */
|
||||
$PAGE->navbar->add(new lang_string("localplugins", "moodle"));
|
||||
$PAGE->navbar->add(new lang_string("pluginname", "local_webhooks"));
|
||||
$PAGE->navbar->add($titlepage, $baseurl);
|
||||
echo $OUTPUT->header();
|
||||
|
||||
/* Table declaration */
|
||||
$table = new flexible_table("callbacks-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");
|
||||
|
||||
/* Link for editing */
|
||||
$editlink = new moodle_url($editservice,
|
||||
array("idservice" => $callback->id));
|
||||
$edititem = $OUTPUT->action_icon($editlink,
|
||||
new pix_icon("t/edit", get_string("edit")));
|
||||
|
||||
/* Link to remove */
|
||||
$deletelink = new moodle_url($managerservice,
|
||||
array("deleteservice" => $callback->id, "sesskey" => sesskey()));
|
||||
$deleteitem = $OUTPUT->action_icon($deletelink,
|
||||
new pix_icon("t/delete", get_string("delete")));
|
||||
|
||||
/* Adding data to the table */
|
||||
$table->add_data(array($titlecallback, $urlcallback, $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();
|
||||
@@ -0,0 +1,65 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Restore the settings page.
|
||||
*
|
||||
* @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(__DIR__ . "/classes/service_form.php");
|
||||
require_once(__DIR__ . "/lib.php");
|
||||
|
||||
require_once($CFG->libdir . "/adminlib.php");
|
||||
|
||||
/* Link generation */
|
||||
$baseurl = new moodle_url("/local/webhooks/restorebackup.php");
|
||||
$managerservice = new moodle_url("/local/webhooks/index.php");
|
||||
|
||||
/* Configure the context of the page */
|
||||
admin_externalpage_setup("local_webhooks", "", null, $baseurl, array());
|
||||
$context = context_system::instance();
|
||||
|
||||
/* Create an editing form */
|
||||
$mform = new service_backup_form($PAGE->url);
|
||||
|
||||
/* Cancel processing */
|
||||
if ($mform->is_cancelled()) {
|
||||
redirect($managerservice);
|
||||
}
|
||||
|
||||
/* Processing the received file */
|
||||
if ($data = $mform->get_data()) {
|
||||
$content = $mform->get_file_content("backupfile");
|
||||
local_webhooks_restore_backup($content, $data->deleterecords);
|
||||
redirect($managerservice, new lang_string("restorefinished", "moodle"));
|
||||
}
|
||||
|
||||
/* The page title */
|
||||
$titlepage = new lang_string("backup", "moodle");
|
||||
$PAGE->navbar->add($titlepage);
|
||||
$PAGE->set_heading($titlepage);
|
||||
$PAGE->set_title($titlepage);
|
||||
echo $OUTPUT->header();
|
||||
|
||||
/* Displays the form */
|
||||
$mform->display();
|
||||
|
||||
/* Footer */
|
||||
echo $OUTPUT->footer();
|
||||
+4
-17
@@ -24,22 +24,9 @@
|
||||
|
||||
defined("MOODLE_INTERNAL") || die();
|
||||
|
||||
$settings = null;
|
||||
if ($hassiteconfig) {
|
||||
$settings = new admin_settingpage("local_webhooks",
|
||||
new lang_string("pluginname", "local_webhooks"));
|
||||
|
||||
$ADMIN->add("localplugins", $settings);
|
||||
|
||||
$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("managerservice", "local_webhooks");
|
||||
$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")
|
||||
));
|
||||
}
|
||||
+7
-5
@@ -23,8 +23,10 @@
|
||||
*/
|
||||
|
||||
defined("MOODLE_INTERNAL") || die();
|
||||
$plugin->release = "0.2.0 (Build: 2017102600)";
|
||||
$plugin->version = 2017102600;
|
||||
$plugin->requires = 2016112900;
|
||||
$plugin->component = "local_webhooks";
|
||||
$plugin->maturity = MATURITY_BETA;
|
||||
|
||||
$plugin->release = "4.0.0-rc.1 (Build: 2017122900)";
|
||||
$plugin->version = 2017122900;
|
||||
$plugin->requires = 2016112900;
|
||||
$plugin->component = "local_webhooks";
|
||||
$plugin->maturity = MATURITY_RC;
|
||||
$plugin->dependencies = array("report_eventlist" => 2016120500);
|
||||
Reference in New Issue
Block a user