Compare commits

..
12 Commits
6 changed files with 37 additions and 17 deletions
+3 -3
View File
@@ -33,14 +33,14 @@ class curl {
} }
} }
public static function request($url, $data) { public static function request($callback, $data) {
$ch = curl_init($url); $ch = curl_init($callback->url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, curl_setopt($ch, CURLOPT_HTTPHEADER,
array( array(
"Content-Type: application/json", "Content-Type: application/" . $callback->type,
"Content-Length: " . strlen($data)) "Content-Length: " . strlen($data))
); );
+10 -3
View File
@@ -39,9 +39,9 @@ class events {
* @param object $event * @param object $event
*/ */
public static function handler($event) { public static function handler($event) {
$config = get_config("local_webhooks"); $enable = get_config("local_webhooks", "enable");
if (boolval($config->enable)) { if (boolval($enable)) {
$data = $event->get_data(); $data = $event->get_data();
self::transmitter($data); self::transmitter($data);
} }
@@ -74,9 +74,16 @@ class events {
*/ */
private static function handler_callback($data, $callback) { private static function handler_callback($data, $callback) {
if ($callback->enable) { if ($callback->enable) {
if (!empty($callback->events)) {
$events = unserialize(gzuncompress(base64_decode($callback->events))); $events = unserialize(gzuncompress(base64_decode($callback->events)));
}
if (boolval($events[$data["eventname"]])) { if (boolval($events[$data["eventname"]])) {
/* Adding to the data token */
if (boolval($callback->token)) {
$data["token"] = $callback->token;
}
self::send($data, $callback); self::send($data, $callback);
} }
} }
@@ -91,7 +98,7 @@ class events {
private static function send($data, $callback) { private static function send($data, $callback) {
$curl = new curl(); $curl = new curl();
$package = self::packup($data); $package = self::packup($data);
$curl::request($callback->url, $package); $curl::request($callback, $package);
} }
/** /**
+15 -2
View File
@@ -81,12 +81,25 @@ class service_edit_form extends moodleform {
$mform->addRule("url", null, "required"); $mform->addRule("url", null, "required");
/* Enabling the service */ /* Enabling the service */
$mform->addElement("checkbox", "enable", $mform->addElement("advcheckbox", "enable",
new lang_string("enable", "moodle")); new lang_string("enable", "moodle"));
$mform->setType("enable", PARAM_BOOL); $mform->setType("enable", PARAM_BOOL);
$mform->setDefault("enable", 1); $mform->setDefault("enable", 1);
$mform->setAdvanced("enable"); $mform->setAdvanced("enable");
/* Token */
$mform->addElement("text", "token",
new lang_string("token", "webservice"),
array("size" => 60));
$mform->setType("token", PARAM_NOTAGS);
/* Content type */
$mform->addElement("select", "type", "Content type",
array(
"json" => "application/json",
"x-www-form-urlencoded" => "application/x-www-form-urlencoded"));
$mform->setAdvanced("type");
/* Form heading */ /* Form heading */
$mform->addElement("header", "editserviceheaderevent", $mform->addElement("header", "editserviceheaderevent",
new lang_string("edulevel", "moodle")); new lang_string("edulevel", "moodle"));
@@ -98,7 +111,7 @@ class service_edit_form extends moodleform {
/* Formation of the list of elements */ /* Formation of the list of elements */
foreach ($eventlist as $event) { foreach ($eventlist as $event) {
$events[$event["component"]][] =& $events[$event["component"]][] =&
$mform->createElement("advcheckbox", $event["eventname"], $event["eventname"]); $mform->createElement("checkbox", $event["eventname"], $event["eventname"]);
} }
/* Displays groups of items */ /* Displays groups of items */
+3 -1
View File
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?> <?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="20171026" COMMENT="XMLDB file for Moodle" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd">
<TABLES> <TABLES>
<TABLE NAME="local_webhooks_service" COMMENT="A table for storing callback services."> <TABLE NAME="local_webhooks_service" COMMENT="A table for storing callback services.">
<FIELDS> <FIELDS>
@@ -7,6 +7,8 @@
<FIELD NAME="enable" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" /> <FIELD NAME="enable" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" />
<FIELD NAME="title" TYPE="text" NOTNULL="true" SEQUENCE="false" /> <FIELD NAME="title" TYPE="text" NOTNULL="true" SEQUENCE="false" />
<FIELD NAME="url" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" /> <FIELD NAME="url" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" />
<FIELD NAME="type" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" />
<FIELD NAME="token" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" />
<FIELD NAME="events" TYPE="text" NOTNULL="true" SEQUENCE="false" /> <FIELD NAME="events" TYPE="text" NOTNULL="true" SEQUENCE="false" />
</FIELDS> </FIELDS>
<KEYS> <KEYS>
+2 -4
View File
@@ -59,12 +59,10 @@ if ($idediting = boolval($idservice)) {
/* Processing of received data */ /* Processing of received data */
if ($data = $mform->get_data()) { if ($data = $mform->get_data()) {
if (empty($data->enable)) {
$data->enable = 0;
}
/* Packing of data */ /* Packing of data */
if (!empty($data->events)) {
$data->events = base64_encode(gzcompress(serialize($data->events), 9)); $data->events = base64_encode(gzcompress(serialize($data->events), 9));
}
if ($idediting) { if ($idediting) {
$data->id = $idservice; $data->id = $idservice;
+2 -2
View File
@@ -23,8 +23,8 @@
*/ */
defined("MOODLE_INTERNAL") || die(); defined("MOODLE_INTERNAL") || die();
$plugin->release = "0.2.0 (Build: 2017102600)"; $plugin->release = "0.4.0 (Build: 2017102620)";
$plugin->version = 2017102600; $plugin->version = 2017102620;
$plugin->requires = 2016112900; $plugin->requires = 2016112900;
$plugin->component = "local_webhooks"; $plugin->component = "local_webhooks";
$plugin->maturity = MATURITY_BETA; $plugin->maturity = MATURITY_BETA;