Compare commits

...

62 Commits

Author SHA1 Message Date
edb23d6aad
Added mirror 2024-05-29 20:08:39 +04:00
064e2fa422
Update README.md
Signed-off-by: Valentin Popov <valentin@popov.link>
2023-12-28 16:58:03 +04:00
9cf4fc575c
Correction of spaces 2018-02-25 01:32:29 +04:00
f3c2ebbe4d
Correcting a stupid mistake 2018-02-25 01:27:40 +04:00
8f25379f32
Change version 2018-02-25 01:21:50 +04:00
90897b0e7d
General refactoring of the web services code 2018-02-25 01:19:19 +04:00
685d9ee15e Change version 2018-02-22 12:58:37 +04:00
db500cddce Fix 'invalid OPTIONAL value specified' 2018-02-22 12:55:20 +04:00
85bcb64e37 Added general list functions 2018-02-22 12:44:50 +04:00
1736128dbf Adding the function of forming events 2018-02-22 12:37:59 +04:00
5cdd8de9e3 Small code refactoring 2018-02-20 15:40:50 +04:00
b6ddc8bd40 Some variables have been removed 2018-02-20 15:17:27 +04:00
f10786bfb0 Update function 'local_webhooks_create_record' 2018-02-20 14:38:36 +04:00
899b129d23 The extension is added to the event handler 2018-02-19 15:26:27 +04:00
dd19065313 The extension is added to external functions 2018-02-19 15:25:49 +04:00
3a2bddeea4 The search function is expanded 2018-02-19 15:24:34 +04:00
a41dbbb950 Transferring internal functions to the internal library 2018-02-19 13:44:11 +04:00
a68882140b An external service search function is declared 2018-02-19 13:38:27 +04:00
43f0bf98d2 Added external functions to find services 2018-02-19 13:37:27 +04:00
95c4cf061a Rename the function to reset the cache 2018-02-19 13:15:14 +04:00
a1099245b5 Cache description added 2018-02-19 13:06:06 +04:00
66b5928e6e Added clearing the cache when changing data 2018-02-19 12:36:09 +04:00
4f75d9c5c8 Event handler is rewritten to work with the cache 2018-02-18 22:05:58 +04:00
29bdbc7456 Added a service search function that contains the specified event 2018-02-18 21:37:43 +04:00
ebf76795da Change the location of the functions for working with the cache 2018-02-18 21:28:46 +04:00
53c49b69de Added functions for working with the cache 2018-02-18 20:01:55 +04:00
f392fd42d9 Update the PHP version for testing. 2018-02-18 18:05:33 +04:00
67f8d73a15 Update versions of Moodle for testing. 2018-02-18 18:02:23 +04:00
22b7569f71 Correct the check for 'sesskey'. 2018-02-18 17:58:59 +04:00
fa74622268 Links to the editor contain sesskey 2018-01-04 15:38:55 +04:00
9b7426fb20 Check the sesskey on the editor page 2018-01-04 15:35:27 +04:00
283c64f62e Check the sesskey on the recovery page 2018-01-04 15:34:51 +04:00
17ae01ab71 Change version 2017-12-29 14:29:39 +04:00
28e1169be8 Added correct update from old versions 2017-12-29 14:25:49 +04:00
2126708201 An additional column is added 2017-12-27 21:03:09 +04:00
0041667754 Refactoring the handler class 2017-12-27 20:45:31 +04:00
91bf79f030 Added function to send the request 2017-12-27 20:43:36 +04:00
7d1a0b9fec Updated the function to update the record 2017-12-27 20:30:27 +04:00
51007a0fb5 Register a new external function 2017-12-27 19:52:21 +04:00
2eaa622161 Added external function display a list of events 2017-12-27 19:51:34 +04:00
3d4b78bd8b Added the function of receiving all the events 2017-12-27 16:01:48 +04:00
41185c6db4 Reconstructed external restore function from the backup 2017-12-27 15:47:36 +04:00
7e1284bfe2 Edit the result of the function 2017-12-27 15:29:58 +04:00
785a541d1c Registration of external plug-in functions is added 2017-12-27 15:27:55 +04:00
6e3bf072af Added external plugin functions 2017-12-27 15:26:59 +04:00
2e57e732ba Fixed the text of the button 2017-12-22 18:58:27 +04:00
499f36d118 Integration of the event system into functions 2017-12-21 12:06:20 +04:00
49516bea82 Refactoring the system of events 2017-12-21 11:48:43 +04:00
a6c6081139 Edit translation 2017-12-21 11:36:42 +04:00
9560267833 Refactoring the editor code 2017-12-21 11:31:58 +04:00
2057a1efb6 The functional was updated to a new form 2017-12-21 11:14:40 +04:00
0cbf2bbb87 The data recovery form has been extended 2017-12-21 11:12:40 +04:00
3e054dd94e Updated package dependencies 2017-12-21 11:11:24 +04:00
54fb29c5ea Correction on the main page 2017-12-21 10:01:37 +04:00
78e9e435cc Refactoring function code plugins 2017-12-21 09:59:14 +04:00
97711bb186 Added function to change the status of the service 2017-12-19 14:50:30 +04:00
999ad9f472 Refactoring the main page 2017-12-19 14:39:40 +04:00
82c7463e4a A class was added to display the table 2017-12-19 14:15:07 +04:00
e0e9322644 Additions to the description file 2017-12-11 07:47:51 +04:00
7278ee951b Changes to the User's Guide 2017-12-11 07:43:17 +04:00
01e2ba9f2f User guide 2017-12-11 07:37:49 +04:00
99868c158a Screenshots for documentation 2017-12-11 07:37:09 +04:00
20 changed files with 1504 additions and 279 deletions

17
.github/workflows/main.yml vendored Normal file
View File

@ -0,0 +1,17 @@
name: Workflow
on: [push]
jobs:
mirror:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: yesolutions/mirror-action@master
with:
REMOTE: 'https://git.popov.link/moodle/local_webhooks.git'
GIT_USERNAME: ${{ secrets.GIT_USERNAME }}
GIT_PASSWORD: ${{ secrets.GIT_PASSWORD }}

View File

@ -6,25 +6,15 @@ os: "linux"
php:
- 5.6
- 7.1
- 7.2
env:
- DB=pgsql MOODLE_BRANCH=MOODLE_26_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_27_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_28_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_29_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_30_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_31_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_32_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_33_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_26_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_27_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_28_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_29_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_30_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_31_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_34_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_32_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_33_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_34_STABLE
before_install:
- cd ../..
@ -51,9 +41,9 @@ jobs:
stage: deploy
env:
- DB=mysqli MOODLE_BRANCH=MOODLE_33_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_34_STABLE
php: 7.1
php: 7.2
before_deploy:
- cd $TRAVIS_REPO_SLUG

View File

@ -1,8 +1,5 @@
# Moodle WebHook's
[![GitHub Release](https://img.shields.io/github/release/valentineus/moodle-webhooks.svg)](https://github.com/valentineus/moodle-webhooks/releases)
[![Build Status](https://travis-ci.org/valentineus/moodle-webhooks.svg?branch=master)](https://travis-ci.org/valentineus/moodle-webhooks)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/376ab669f2d3436884015554f686a915)](https://www.codacy.com/app/valentineus/moodle-webhooks)
[![Gitter Badge](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/valentineus/moodle-webhooks)
Adding Webhooks to Moodle.
The Webhooks feature allows applications to receive real-time notification of changes to certain data.
@ -20,27 +17,11 @@ Features:
* Use a secret phrase to authenticate requests;
* [JSON](https://en.wikipedia.org/wiki/JSON) - Format of outgoing data;
## Installation
## Documentation
Get the installation package in any of the available methods:
* [GitHub Releases](https://github.com/valentineus/moodle-webhooks/releases).
* [Compilation from the source code](#build).
## Build
Self-assembly package is as follows:
* Clone the repository:
```bash
git clone https://github.com/valentineus/moodle-webhooks.git moodle-webhooks
```
* Run the build script:
```bash
cd ./moodle-webhooks
/bin/sh build.sh
```
* [Install the plugin](docs/getting-started.md#installation).
* [User guide](docs/getting-started.md#user-guide).
* [Request format](docs/getting-started.md#request-format).
## License
@ -48,4 +29,4 @@ cd ./moodle-webhooks
[GNU GPLv3](LICENSE.txt).
Copyright (c)
[Valentin Popov](mailto:info@valentineus.link).
[Valentin Popov](mailto:info@valentineus.link).

View File

@ -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");
}
}

View File

@ -26,13 +26,11 @@ namespace local_webhooks;
defined("MOODLE_INTERNAL") || die();
require_once(__DIR__ . "/../lib.php");
require_once(__DIR__ . "/../locallib.php");
require_once($CFG->libdir . "/filelib.php");
require_once(__DIR__ . "/../lib.php");
/**
* Defines how to work with events.
* Defines event handlers.
*
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
@ -46,48 +44,13 @@ class handler {
public static function events($event) {
$data = $event->get_data();
if ($callbacks = local_webhooks_get_list_records()) {
foreach ($callbacks as $callback) {
self::handler_callback($data, $callback);
}
if (!is_array($recordlist = local_webhooks_cache_get($data["eventname"]))) {
$recordlist = local_webhooks_search_services_by_event($data["eventname"], true);
local_webhooks_cache_set($data["eventname"], $recordlist);
}
}
/**
* Processes each callback.
*
* @param array $data
* @param object $callback
*/
private static function handler_callback($data, $callback) {
global $CFG;
if (boolval($callback->enable)) {
if (!empty($callback->events[$data["eventname"]])) {
$urlparse = parse_url($CFG->wwwroot);
$data["host"] = $urlparse['host'];
$data["token"] = $callback->token;
$data["extra"] = $callback->other;
self::send($data, $callback);
}
foreach ($recordlist as $record) {
local_webhooks_send_request($data, $record);
}
}
/**
* Sending data to the node.
*
* @param array $data
* @param object $callback
*/
private static function send($data, $callback) {
$curl = new \curl();
$curl->setHeader(array("Content-Type: application/" . $callback->type));
$curl->post($callback->url, json_encode($data));
$response = $curl->getResponse();
\local_webhooks_events::response_answer($callback->id, $response);
return $response;
}
}

View File

@ -24,6 +24,8 @@
defined("MOODLE_INTERNAL") || die();
require_once(__DIR__ . "/../lib.php");
require_once($CFG->libdir . "/formslib.php");
/**
@ -84,7 +86,7 @@ class service_edit_form extends moodleform {
$mform->addElement("header", "editserviceheaderevent", new lang_string("edulevel", "moodle"));
/* List of events */
$eventlist = report_eventlist_list_generator::get_all_events_list(true);
$eventlist = local_webhooks_get_list_events();
$events = array();
/* Formation of the list of elements */
@ -125,6 +127,11 @@ class service_backup_form extends moodleform {
/* 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");

145
classes/webhooks_table.php Normal file
View File

@ -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, "sesskey" => sesskey()));
$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, "sesskey" => sesskey()));
$html = html_writer::link($link, $row->title);
return $html;
}
}

31
db/caches.php Normal file
View File

@ -0,0 +1,31 @@
<?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/>.
/**
* Definition of the plug-in cache.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$definitions = array(
"webhooks_services" => array(
"mode" => cache_store::MODE_APPLICATION,
"requiredataguarantee" => true,
"staticacceleration" => true
)
);

115
db/services.php Normal file
View File

@ -0,0 +1,115 @@
<?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_search_services_by_event" => array(
"classname" => "local_webhooks_external",
"methodname" => "search_services_by_event",
"classpath" => "local/webhooks/externallib.php",
"description" => "Search for services that contain the specified event.",
"type" => "read"
),
"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"
)
);

52
db/upgrade.php Normal file
View File

@ -0,0 +1,52 @@
<?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/>.
/**
* 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
*/
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;
}

111
docs/getting-started.md Normal file
View File

@ -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:
![Service Manager](screenshots/managerservice.jpg)
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:
![Service Editor](screenshots/editservice.jpg)
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

View File

@ -56,15 +56,15 @@ if ($editing = boolval($serviceid)) {
}
/* Processing of received data */
if ($data = $mform->get_data()) {
if (($data = $mform->get_data()) && confirm_sesskey()) {
if ($editing) {
$data->id = $serviceid;
local_webhooks_update_record($data, false);
redirect($managerservice, new lang_string("eventwebserviceserviceupdated", "webservice"));
local_webhooks_update_record($data);
} else {
local_webhooks_update_record($data, true);
redirect($managerservice, new lang_string("eventwebserviceservicecreated", "webservice"));
local_webhooks_create_record($data);
}
redirect($managerservice, new lang_string("changessaved", "moodle"));
}
/* The page title */
@ -77,4 +77,5 @@ echo $OUTPUT->header();
/* Displays the form */
$mform->display();
/* Footer */
echo $OUTPUT->footer();

643
externallib.php Normal file
View File

@ -0,0 +1,643 @@
<?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 {
/**
* Single template output parameters.
*
* @return external_description
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
private static function structure_record_parameters() {
return new external_single_structure(
array(
"id" => new external_value(PARAM_INT, "Service ID."),
"enable" => new external_value(PARAM_BOOL, "Enable or disable the service."),
"title" => new external_value(PARAM_TEXT, "Name of the service."),
"url" => new external_value(PARAM_URL, "Web address of the service."),
"type" => new external_value(PARAM_TEXT, "Used header type."),
"token" => new external_value(PARAM_RAW, "Authorization key.", VALUE_OPTIONAL),
"other" => new external_value(PARAM_RAW, "Additional field.", VALUE_OPTIONAL),
"events" => new external_multiple_structure(
new external_single_structure(
array(
"name" => new external_value(PARAM_TEXT, "The name of the event."),
"value" => new external_value(PARAM_BOOL, "Enabled or disabled handler.")
)
), "List of observed events.", VALUE_OPTIONAL
)
), "Information about the service."
);
}
/**
* Formation of the final list.
*
* @param array $listrecords
* @return array
*/
private static function formation_list($listrecords) {
$result = array();
foreach ($listrecords as $index => $record) {
$result[$index] = (array) $record;
$result[$index]["events"] = self::formation_events($record->events);
}
return $result;
}
/**
* Formation of the final list of events.
*
* @param array $listevents
* @return array
*/
private static function formation_events($listevents) {
$result = array();
foreach ($listevents as $key => $value) {
$result[] = array("name" => $key, "value" => $value);
}
return $result;
}
/**
* 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 ID.")
)
);
}
/**
* 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) {
$parameters = self::validate_parameters(self::change_status_parameters(), array("serviceid" => $serviceid));
$context = context_system::instance();
self::validate_context($context);
return local_webhooks_change_status($parameters["serviceid"]);
}
/**
* 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, "Result of execution.");
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function search_services_by_event_parameters() {
return new external_function_parameters(
array(
"eventname" => new external_value(PARAM_TEXT, "The name of the event."),
"active" => new external_value(PARAM_BOOL, "Filter show active or all services.")
)
);
}
/**
* Search for services that contain the specified event.
*
* @param string $eventname
* @param boolean $active
* @return array
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function search_services_by_event($eventname, $active) {
$parameters = self::validate_parameters(self::search_services_by_event_parameters(), array("eventname" => $eventname, "active" => $active));
$context = context_system::instance();
self::validate_context($context);
$result = array();
if ($listrecords = local_webhooks_search_services_by_event($parameters["eventname"], $parameters["active"])) {
$result = self::formation_list($listrecords);
}
return $result;
}
/**
* Returns description of method result value.
*
* @return external_description
* @since Moodle 2.2
*/
public static function search_services_by_event_returns() {
return new external_multiple_structure(
self::structure_record_parameters(), "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_record_parameters() {
return new external_function_parameters(
array(
"serviceid" => new external_value(PARAM_INT, "Service ID.")
)
);
}
/**
* 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) {
$parameters = self::validate_parameters(self::get_record_parameters(), array("serviceid" => $serviceid));
$context = context_system::instance();
self::validate_context($context);
$result = array();
if ($record = local_webhooks_get_record($parameters["serviceid"])) {
$result = (array) $record;
$result["events"] = self::formation_events($record->events);
}
return $result;
}
/**
* Returns description of method result value.
*
* @return external_description
* @since Moodle 2.2
*/
public static function get_record_returns() {
return self::structure_record_parameters();
}
/**
* 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()) {
$result = self::formation_list($listrecords);
}
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(
self::structure_record_parameters(), "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, "The name of the event.")
);
}
/**
* 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, "Enable or disable the service.", VALUE_OPTIONAL),
"title" => new external_value(PARAM_TEXT, "Name of the service."),
"url" => new external_value(PARAM_URL, "Web address of the service."),
"type" => new external_value(PARAM_TEXT, "Used header type.", VALUE_OPTIONAL),
"token" => new external_value(PARAM_RAW, "Authorization key.", VALUE_OPTIONAL),
"other" => new external_value(PARAM_RAW, "Additional field.", VALUE_OPTIONAL),
"events" => new external_multiple_structure(
new external_single_structure(
array(
"name" => new external_value(PARAM_TEXT, "The name of the event."),
"value" => new external_value(PARAM_BOOL, "Enabled or disabled handler.")
)
), "List of observed events.", VALUE_OPTIONAL
)
), "Information about the service."
)
)
);
}
/**
* Create an entry in the database.
*
* @param array $service
* @return boolean
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function create_record($service) {
$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->events = array();
$record->enable = !empty($parameters["service"]["enable"]) ? $parameters["service"]["enable"] : false;
$record->type = !empty($parameters["service"]["type"]) ? $parameters["service"]["type"] : "json";
foreach ($parameters["service"]["events"] as $value) {
$record->events[$value["name"]] = $value["value"];
}
return local_webhooks_create_record($record);
}
/**
* Returns description of method result value.
*
* @return external_description
* @since Moodle 2.2
*/
public static function create_record_returns() {
return new external_value(PARAM_BOOL, "Result of execution.");
}
/**
* 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(
"service" => new external_single_structure(
array(
"id" => new external_value(PARAM_INT, "Service ID."),
"enable" => new external_value(PARAM_BOOL, "Enable or disable the service.", VALUE_OPTIONAL),
"title" => new external_value(PARAM_TEXT, "Name of the service.", VALUE_OPTIONAL),
"url" => new external_value(PARAM_URL, "Web address of the service.", VALUE_OPTIONAL),
"type" => new external_value(PARAM_TEXT, "Used header type.", VALUE_OPTIONAL),
"token" => new external_value(PARAM_RAW, "Authorization key.", VALUE_OPTIONAL),
"other" => new external_value(PARAM_RAW, "Additional field.", VALUE_OPTIONAL),
"events" => new external_multiple_structure(
new external_single_structure(
array(
"name" => new external_value(PARAM_TEXT, "The name of the event."),
"value" => new external_value(PARAM_BOOL, "Enabled or disabled handler.")
)
), "List of observed events.", VALUE_OPTIONAL
)
), "Information about the service."
)
)
);
}
/**
* Update the record in the database.
*
* @param array $service
* @return boolean
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function update_record($service) {
$parameters = self::validate_parameters(self::update_record_parameters(), array("service" => $service));
$context = context_system::instance();
self::validate_context($context);
$result = false;
if ($record = local_webhooks_get_record($parameters["service"]["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, "Result of execution.");
}
/**
* 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 ID.")
)
);
}
/**
* 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) {
$parameters = self::validate_parameters(self::delete_record_parameters(), array("serviceid" => $serviceid));
$context = context_system::instance();
self::validate_context($context);
return local_webhooks_delete_record($parameters["serviceid"]);
}
/**
* 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, "Result of execution.");
}
/**
* 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);
return local_webhooks_delete_all_records();
}
/**
* 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, "Result of execution.");
}
/**
* 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);
return local_webhooks_create_backup();
}
/**
* Returns description of method result value.
*
* @return external_description
* @since Moodle 2.2
*/
public static function create_backup_returns() {
return new external_value(PARAM_RAW, "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_RAW, "Backup copy."),
"deleterecords" => new external_value(PARAM_BOOL, "Delete or leave all 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) {
$parameters = self::validate_parameters(self::restore_backup_parameters(), array("options" => $options));
$context = context_system::instance();
self::validate_context($context);
$deleterecords = !empty($parameters["options"]["deleterecords"]) ? $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;
}
}

View File

@ -23,13 +23,14 @@
*/
require_once(__DIR__ . "/../../config.php");
require_once(__DIR__ . "/classes/webhooks_table.php");
require_once(__DIR__ . "/lib.php");
require_once($CFG->libdir . "/adminlib.php");
require_once($CFG->libdir . "/tablelib.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);
@ -44,13 +45,22 @@ admin_externalpage_setup("local_webhooks", "", null, $baseurl, array());
$context = context_system::instance();
/* Delete the service */
if (boolval($deleteid)) {
local_webhooks_remove_record($deleteid);
redirect($PAGE->url, new lang_string("eventwebserviceservicedeleted", "webservice"));
if (!empty($deleteid) && confirm_sesskey()) {
local_webhooks_delete_record($deleteid);
redirect($PAGE->url, new lang_string("deleted", "moodle"));
}
/* Retrieving a list of services */
$callbacks = local_webhooks_get_list_records();
/* 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)) {
@ -59,74 +69,32 @@ if (boolval($backupservices)) {
send_file($filecontent, $filename, 0, 0, true, true);
}
/* Switching the status of the service */
if (boolval($hideshowid)) {
$callback = $callbacks[$hideshowid];
if (!empty($callback)) {
$callback->enable = !boolval($callback->enable);
local_webhooks_update_record($callback, false);
redirect($PAGE->url, new lang_string("eventwebserviceserviceupdated", "webservice"));
}
}
/* The page title */
$titlepage = new lang_string("pluginname", "local_webhooks");
$PAGE->set_heading($titlepage);
$PAGE->set_title($titlepage);
echo $OUTPUT->header();
/* Table declaration */
$table = new flexible_table("webhooks-service-table");
/* Adds the add button */
$addserviceurl = new moodle_url($editservice, array("sesskey" => sesskey()));
echo $OUTPUT->single_button($addserviceurl, new lang_string("add", "moodle"));
/* 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();
/* 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");
foreach ($callbacks as $callback) {
/* Filling of information columns */
$titlecallback = html_writer::div($callback->title, "title");
$urlcallback = html_writer::div($callback->url, "url");
/* Defining service status */
$hideshowicon = "t/show";
$hideshowstring = new lang_string("enable", "moodle");
if (boolval($callback->enable)) {
$hideshowicon = "t/hide";
$hideshowstring = new lang_string("disable", "moodle");
}
/* Link to enable / disable the service */
$hideshowlink = new moodle_url($managerservice, array("hideshowid" => $callback->id));
$hideshowitem = $OUTPUT->action_icon($hideshowlink, new pix_icon($hideshowicon, $hideshowstring));
/* Link for editing */
$editlink = new moodle_url($editservice, array("serviceid" => $callback->id));
$edititem = $OUTPUT->action_icon($editlink, new pix_icon("t/edit", new lang_string("edit", "moodle")));
/* Link to remove */
$deletelink = new moodle_url($managerservice, array("deleteid" => $callback->id));
$deleteitem = $OUTPUT->action_icon($deletelink, new pix_icon("t/delete", new lang_string("delete", "moodle")));
/* Adding data to the table */
$table->add_data(array($titlecallback, $urlcallback, $hideshowitem . $edititem . $deleteitem));
}
/* Display the table */
$table->print_html();
/* Add service button */
$addserviceurl = new moodle_url($editservice);
echo $OUTPUT->single_button($addserviceurl, new lang_string("addaservice", "webservice"), "get");
/* Button to get a backup */
/* Adds a backup button */
$backupurl = new moodle_url($managerservice, array("getbackup" => true));
echo $OUTPUT->single_button($backupurl, new lang_string("backup", "moodle"), "get");
/* Button for restoring settings */
/* Adds a restore button */
$restorebackupurl = new moodle_url($restorebackup);
echo $OUTPUT->single_button($restorebackupurl, new lang_string("restore", "moodle"), "get");
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();

247
lib.php
View File

@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Library code used by the service control interfaces.
* This file contains the functions used by the plugin.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
@ -27,7 +27,70 @@ defined("MOODLE_INTERNAL") || die();
require_once(__DIR__ . "/locallib.php");
/**
* Getting a list of all services.
* 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;
}
/**
* Search for services that contain the specified event.
*
* @param string $eventname
* @param boolean $active
* @return array
*/
function local_webhooks_search_services_by_event($eventname, $active = false) {
$recordlist = local_webhooks_get_list_records();
$active = boolval($active);
$result = array();
foreach ($recordlist as $record) {
if (!empty($record->events[$eventname])) {
if ($active && boolval($record->enable)) {
$result[] = $record;
}
if (!$active) {
$result[] = $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
@ -36,130 +99,170 @@ require_once(__DIR__ . "/locallib.php");
function local_webhooks_get_list_records($limitfrom = 0, $limitnum = 0) {
global $DB;
$listservices = $DB->get_records("local_webhooks_service", null, "id", "*", $limitfrom, $limitnum);
$listrecords = $DB->get_records("local_webhooks_service", null, "id", "*", $limitfrom, $limitnum);
foreach ($listservices as $servicerecord) {
foreach ($listrecords as $servicerecord) {
if (!empty($servicerecord->events)) {
$servicerecord->events = local_webhooks_unarchive_data($servicerecord->events);
$servicerecord->events = local_webhooks_deserialization_data($servicerecord->events);
}
}
return $listservices;
return $listrecords;
}
/**
* Getting information about the service.
* Get a list of all system events.
*
* @param number $serviceid
* @return object
* @return array
*/
function local_webhooks_get_record($serviceid = 0) {
function local_webhooks_get_list_events() {
return report_eventlist_list_generator::get_all_events_list(true);
}
/**
* Create an entry in the database.
*
* @param object $record
* @return boolean
*/
function local_webhooks_create_record($record) {
global $DB;
$servicerecord = $DB->get_record("local_webhooks_service", array("id" => $serviceid), "*", MUST_EXIST);
if (!empty($servicerecord->events)) {
$servicerecord->events = local_webhooks_unarchive_data($servicerecord->events);
if (!empty($record->events)) {
$record->events = local_webhooks_serialization_data($record->events);
}
return $servicerecord;
}
$result = $DB->insert_record("local_webhooks_service", $record, true, false);
/**
* Clear the database table.
*/
function local_webhooks_remove_list_records() {
global $DB;
/* Clear the plugin cache */
local_webhooks_cache_reset();
$DB->delete_records("local_webhooks_service", null);
}
/* Event notification */
local_webhooks_events::service_added($result);
/**
* Delete the record.
*
* @param number $serviceid
*/
function local_webhooks_remove_record($serviceid = 0) {
global $DB;
$DB->delete_records("local_webhooks_service", array("id" => $serviceid));
local_webhooks_events::service_deleted($serviceid);
return boolval($result);
}
/**
* Update the record in the database.
*
* @param object $data
* @param boolean $insert
* @return boolean
*/
function local_webhooks_update_record($data, $insert = true) {
function local_webhooks_update_record($record) {
global $DB;
if (empty($data->events)) {
$data->events = array();
if (empty($record->id)) {
print_error("missingparam", "error", null, "id");
}
$data->events = local_webhooks_archiving_data($data->events);
$record->events = !empty($record->events) ? local_webhooks_serialization_data($record->events) : null;
$result = $DB->update_record("local_webhooks_service", $record, false);
if (boolval($insert)) {
$result = $DB->insert_record("local_webhooks_service", $data, true, false);
local_webhooks_events::service_added($result);
} else {
$result = $DB->update_record("local_webhooks_service", $data, false);
local_webhooks_events::service_updated($data->id);
}
/* Clear the plugin cache */
local_webhooks_cache_reset();
/* Event notification */
local_webhooks_events::service_updated($record->id);
return boolval($result);
}
/**
* Make a backup copy of all the services.
* 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));
/* Clear the plugin cache */
local_webhooks_cache_reset();
/* Event notification */
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);
/* Clear the plugin cache */
local_webhooks_cache_reset();
/* Event notification */
local_webhooks_events::service_deletedall();
return boolval($result);
}
/**
* Create a backup.
*
* @return string
*/
function local_webhooks_create_backup() {
$listservices = local_webhooks_get_list_records();
$listservices = local_webhooks_archiving_data($listservices);
$listrecords = local_webhooks_get_list_records();
$result = local_webhooks_serialization_data($listrecords);
/* Event notification */
local_webhooks_events::backup_performed();
return $listservices;
return $result;
}
/**
* Restore the data from the backup.
* Restore from a backup.
*
* @param string $data
*/
function local_webhooks_restore_backup($listservices = "") {
$listservices = local_webhooks_unarchive_data($listservices);
function local_webhooks_restore_backup($data, $deleterecords = false) {
$listrecords = local_webhooks_deserialization_data($data);
local_webhooks_remove_list_records();
foreach ($listservices as $servicerecord) {
local_webhooks_update_record($servicerecord, true);
if (boolval($deleterecords)) {
local_webhooks_delete_all_records();
}
foreach ($listrecords as $servicerecord) {
local_webhooks_create_record($servicerecord);
}
/* Event notification */
local_webhooks_events::backup_restored();
}
/**
* Compress an array into a string.
* Send the event remotely to the service.
*
* @param array $data
* @return string
*/
function local_webhooks_archiving_data($data = array()) {
$result = base64_encode(gzcompress(serialize($data), 3));
return $result;
}
/**
* Gets an array from a compressed string.
*
* @param string $data
* @param array $event
* @param object $callback
* @return array
*/
function local_webhooks_unarchive_data($data = "") {
$result = unserialize(gzuncompress(base64_decode($data)));
return $result;
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();
/* Event notification */
local_webhooks_events::response_answer($callback->id, $response);
return $response;
}

View File

@ -24,6 +24,70 @@
defined("MOODLE_INTERNAL") || die();
/**
* Get data from the cache by key.
*
* @param string $eventname
* @return array
*/
function local_webhooks_cache_get($eventname) {
$cache = cache::make("local_webhooks", "webhooks_services");
return $cache->get($eventname);
}
/**
* Update the data in the cache by key.
*
* @param string $eventname
* @param array $recordlist
* @return boolean
*/
function local_webhooks_cache_set($eventname, $recordlist = array()) {
$cache = cache::make("local_webhooks", "webhooks_services");
return $cache->set($eventname, $recordlist);
}
/**
* Delete the data in the cache by key.
*
* @param string $eventname
* @return boolean
*/
function local_webhooks_cache_delete($eventname) {
$cache = cache::make("local_webhooks", "webhooks_services");
return $cache->delete($eventname);
}
/**
* Clear the cache of the plugin.
*
* @return boolean
*/
function local_webhooks_cache_reset() {
$cache = cache::make("local_webhooks", "webhooks_services");
return $cache->purge();
}
/**
* Data serialization.
*
* @param array|object $data
* @return string
*/
function local_webhooks_serialization_data($data) {
return serialize($data);
}
/**
* Data deserialization.
*
* @param string $data
* @return array|object
*/
function local_webhooks_deserialization_data($data) {
return unserialize($data);
}
/**
* Description of functions of the call of events
*
@ -36,14 +100,7 @@ class local_webhooks_events {
*/
public static function backup_performed() {
$context = context_system::instance();
$event = local_webhooks\event\backup_performed::create(
array(
"context" => $context,
"objectid" => 0
)
);
$event = local_webhooks\event\backup_performed::create(array("context" => $context, "objectid" => 0));
$event->trigger();
}
@ -52,14 +109,7 @@ class local_webhooks_events {
*/
public static function backup_restored() {
$context = context_system::instance();
$event = local_webhooks\event\backup_restored::create(
array(
"context" => $context,
"objectid" => 0
)
);
$event = local_webhooks\event\backup_restored::create(array("context" => $context, "objectid" => 0));
$event->trigger();
}
@ -71,20 +121,13 @@ class local_webhooks_events {
*/
public static function response_answer($objectid = 0, $response = array()) {
$context = context_system::instance();
$status = "Error sending request";
$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 = local_webhooks\event\response_answer::create(array("context" => $context, "objectid" => $objectid, "other" => array("status" => $status)));
$event->trigger();
}
@ -95,14 +138,7 @@ class local_webhooks_events {
*/
public static function service_added($objectid = 0) {
$context = context_system::instance();
$event = local_webhooks\event\service_added::create(
array(
"context" => $context,
"objectid" => $objectid
)
);
$event = local_webhooks\event\service_added::create(array("context" => $context, "objectid" => $objectid));
$event->trigger();
}
@ -113,14 +149,16 @@ class local_webhooks_events {
*/
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();
}
$event = local_webhooks\event\service_deleted::create(
array(
"context" => $context,
"objectid" => $objectid
)
);
/**
* 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();
}
@ -131,14 +169,7 @@ class local_webhooks_events {
*/
public static function service_updated($objectid = 0) {
$context = context_system::instance();
$event = local_webhooks\event\service_updated::create(
array(
"context" => $context,
"objectid" => $objectid
)
);
$event = local_webhooks\event\service_updated::create(array("context" => $context, "objectid" => $objectid));
$event->trigger();
}
}

View File

@ -45,9 +45,9 @@ if ($mform->is_cancelled()) {
}
/* Processing the received file */
if ($data = $mform->get_data()) {
if (($data = $mform->get_data()) && confirm_sesskey()) {
$content = $mform->get_file_content("backupfile");
local_webhooks_restore_backup($content);
local_webhooks_restore_backup($content, $data->deleterecords);
redirect($managerservice, new lang_string("restorefinished", "moodle"));
}
@ -61,4 +61,5 @@ echo $OUTPUT->header();
/* Displays the form */
$mform->display();
/* Footer */
echo $OUTPUT->footer();

View File

@ -24,8 +24,9 @@
defined("MOODLE_INTERNAL") || die();
$plugin->release = "3.0.0 (Build: 2017112600)";
$plugin->version = 2017112600;
$plugin->requires = 2016112900;
$plugin->component = "local_webhooks";
$plugin->maturity = MATURITY_STABLE;
$plugin->release = "4.0.0-rc.2 (Build: 2018022500)";
$plugin->version = 2018022500;
$plugin->requires = 2016112900;
$plugin->component = "local_webhooks";
$plugin->maturity = MATURITY_RC;
$plugin->dependencies = array("report_eventlist" => 2016120500);