Compare commits

...
33 Commits
Author SHA1 Message Date
Valentin Popov eaee6b7c81 Change version 2017-10-26 08:02:33 +04:00
Valentin Popov 282e3bcaf1 Add a token to the request 2017-10-26 08:01:16 +04:00
Valentin Popov c037edbc58 Added a field to the database for the security key 2017-10-26 07:51:42 +04:00
Valentin Popov 2c4c05acd6 The form for the security key has been added 2017-10-26 07:50:30 +04:00
Valentin Popov 39fa71faba Change version 2017-10-26 07:39:48 +04:00
Valentin PopovandGitHub 9ecf83b1d6 Merge pull request #2 from valentineus/list-events
List events
2017-10-26 07:35:52 +04:00
Valentin Popov 54d1d030ec Correcting the style of the code 2017-10-26 06:49:33 +04:00
Valentin Popov 8651e6a157 The event handler for sending 2017-10-26 06:45:53 +04:00
Valentin Popov 28632834af Additional field in the database 2017-10-26 06:27:50 +04:00
Valentin Popov a19031d444 Packer of the received data 2017-10-26 06:27:04 +04:00
Valentin Popov bae23caa87 The generator of the list of registered events 2017-10-26 06:25:48 +04:00
Valentin Popov 0d8cb8688d Displays the list of events 2017-10-25 12:09:23 +04:00
Valentin Popov 430c9fa09a Edit the title 2017-10-25 10:33:23 +04:00
Valentin Popov d0f58c52a2 Fix version 2017-10-25 09:34:50 +04:00
Valentin Popov 3540a50d86 Change version 2017-10-25 09:27:10 +04:00
Valentin Popov 2422592c74 Correcting the style of the code 2017-10-25 09:25:54 +04:00
Valentin Popov d235d49dc6 Refactor the observer for events 2017-10-25 09:25:02 +04:00
Valentin Popov d3c79af8cf Change the name of the switch 2017-10-25 09:24:07 +04:00
Valentin PopovandGitHub 7862e885ed Merge pull request #1 from valentineus/multiple-choice-url
Multiple choice url
2017-10-25 09:08:30 +04:00
Valentin Popov 8ebb1381c9 Adaptation of the handler under new conditions 2017-10-25 08:46:07 +04:00
Valentin Popov ed34626ef6 Refactoring the editor class 2017-10-24 05:03:44 +04:00
Valentin Popov 4273d97f5f The finished version of the service manager 2017-10-24 05:01:07 +04:00
Valentin Popov a8ad95f44e Refactoring the service editor 2017-10-24 04:13:48 +04:00
Valentin Popov 2bee24931a Refactoring the settings page 2017-10-24 04:07:29 +04:00
Valentin Popov 876803b844 Rethinking the translation 2017-10-24 04:02:30 +04:00
Valentin Popov ea1c9f7322 Added a class to work with the form 2017-10-23 08:31:09 +04:00
Valentin Popov 2d2a683549 Added action handlers 2017-10-23 08:03:02 +04:00
Valentin Popov b6659da190 Added page template editor 2017-10-22 18:50:30 +04:00
Valentin Popov 54b20f1335 Adding a title 2017-10-22 16:45:08 +04:00
Valentin Popov aaacb23d41 Correcting an error with a variable 2017-10-22 14:15:16 +04:00
Valentin Popov 804fc061ed Instructions for creating a table in the database 2017-10-22 14:12:49 +04:00
Valentin Popov 706b948be0 Manager page template 2017-10-22 14:10:07 +04:00
Valentin Popov be91a4c9b4 Forming the settings page 2017-10-22 13:45:49 +04:00
11 changed files with 453 additions and 49 deletions
+9 -8
View File
@@ -24,24 +24,25 @@
namespace local_webhooks;
defined('MOODLE_INTERNAL') || die();
defined("MOODLE_INTERNAL") || die();
class curl {
public function __construct() {
if (!function_exists('curl_init')) {
print_error('nocurl', 'mnet');
if (!function_exists("curl_init")) {
print_error("nocurl", "mnet");
}
}
public static function request($url, $data) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
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))
);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
"Content-Type: application/json",
"Content-Length: " . strlen($data))
);
$result = curl_exec($ch);
curl_close($ch);
+76 -8
View File
@@ -24,19 +24,87 @@
namespace local_webhooks;
defined('MOODLE_INTERNAL') || die();
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) {
$enabled = boolval(get_config('local_webhooks', 'enabled'));
$url = get_config('local_webhooks', 'url');
$config = get_config("local_webhooks");
if ($enabled && !empty($url)) {
if (boolval($config->enable)) {
$data = $event->get_data();
$json = json_encode($data);
$curl = new curl();
$curl::request($url, $json);
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"]])) {
/* Adding to the data token */
if (boolval($callback->token)) {
$data["token"] = $callback->token;
}
self::send($data, $callback);
}
}
}
/**
* Sending data to the node.
*
* @param array $data
* @param object $callback
*/
private static function send($data, $callback) {
$curl = new curl();
$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);
}
}
+118
View File
@@ -0,0 +1,118 @@
<?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");
/* Token */
$mform->addElement("text", "token",
new lang_string("token", "webservice"),
array("size" => 60));
$mform->setType("token", PARAM_NOTAGS);
/* 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);
}
}
+6 -7
View File
@@ -22,15 +22,14 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
defined("MOODLE_INTERNAL") || die();
$config = get_config('local_webhooks');
$observers = array(
array(
'eventname' => '*',
'callback' => '\local_webhooks\events::handler',
'includefile' => null,
'priority' => 200,
'internal' => true
"eventname" => "*",
"callback" => "\local_webhooks\\events::handler",
"includefile" => null,
"priority" => 200,
"internal" => true
)
);
+18
View File
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="blocks/local_webhooks/db" VERSION="20171026" COMMENT="XMLDB file for Moodle" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd">
<TABLES>
<TABLE NAME="local_webhooks_service" COMMENT="A table for storing callback services.">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" />
<FIELD NAME="enable" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" />
<FIELD NAME="title" TYPE="text" NOTNULL="true" SEQUENCE="false" />
<FIELD NAME="url" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" />
<FIELD NAME="token" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" />
<FIELD NAME="events" TYPE="text" NOTNULL="true" SEQUENCE="false" />
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
+94
View File
@@ -0,0 +1,94 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Service editor.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . "/../../config.php");
require_once(__DIR__ . "/classes/forms.php");
$idservice = optional_param("idservice", 0, PARAM_INT);
require_login();
/* 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);
/* Configure the context of the page */
$context = context_system::instance();
$PAGE->set_context($context);
/* Preparing a template for data */
$titlepage = new lang_string("externalservice", "webservice");
$servicerecord = new stdClass;
/* Create an editing form */
$mform = new \local_webhooks\service_edit_form($PAGE->url);
/* Cancel processing */
if ($mform->is_cancelled()) {
redirect($managerservice);
}
/* Getting the data */
if ($idediting = boolval($idservice)) {
$servicerecord = $DB->get_record("local_webhooks_service", array("id" => $idservice), "*", MUST_EXIST);
$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);
} else {
$DB->insert_record("local_webhooks_service", $data);
}
redirect($managerservice);
}
/* Page template */
$PAGE->set_pagelayout("admin");
$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();
echo $OUTPUT->footer();
+3 -7
View File
@@ -15,16 +15,12 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Strings for component 'local_webhooks', language 'en'.
* Strings for component "local_webhooks", language "en".
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['local_webhooksdescription'] = '';
$string['pluginname'] = 'WebHook\'s';
$string['enabled'] = 'Enabled';
$string['enabled_help'] = 'Enable event tracking.';
$string['url'] = 'URL';
$string['url_help'] = 'Web address of the remote service.';
$string["managerservice"] = "Service management manager";
$string["pluginname"] = "WebHooks";
+3 -7
View File
@@ -15,16 +15,12 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Strings for component 'local_webhooks', language 'ru'.
* Strings for component "local_webhooks", language "ru".
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['local_webhooksdescription'] = '';
$string['pluginname'] = 'WebHook\'s';
$string['enabled'] = 'Включить';
$string['enabled_help'] = 'Включить отслеживание событий.';
$string['url'] = 'URL';
$string['url_help'] = 'Веб-адрес удалённой службы.';
$string["managerservice"] = "Менеджер управления службами";
$string["pluginname"] = "WebHooks";
+105
View File
@@ -0,0 +1,105 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* 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();
+16 -7
View File
@@ -22,15 +22,24 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
defined("MOODLE_INTERNAL") || die();
$settings = null;
if ($hassiteconfig) {
$settings = new admin_settingpage('local_webhooks', get_string('pluginname', 'local_webhooks'));
$ADMIN->add('localplugins', $settings);
$settings = new admin_settingpage("local_webhooks",
new lang_string("pluginname", "local_webhooks"));
$settings->add(new admin_setting_configcheckbox('local_webhooks/enabled', get_string('enabled', 'local_webhooks'),
get_string('enabled_help', 'local_webhooks'), false));
$ADMIN->add("localplugins", $settings);
$settings->add(new admin_setting_configtext('local_webhooks/url', get_string('url', 'local_webhooks'),
get_string('url_help', 'local_webhooks'), 'http://example.com/endpoint', PARAM_URL, 40));
$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)));
}
+5 -5
View File
@@ -22,9 +22,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->release = '0.0.0 (Build: 2017101900)';
$plugin->version = 2017101900;
defined("MOODLE_INTERNAL") || die();
$plugin->release = "0.3.0 (Build: 2017102610)";
$plugin->version = 2017102610;
$plugin->requires = 2016112900;
$plugin->component = 'local_webhooks';
$plugin->maturity = MATURITY_ALPHA;
$plugin->component = "local_webhooks";
$plugin->maturity = MATURITY_BETA;