Compare commits

...
16 Commits
11 changed files with 102 additions and 172 deletions
+15 -4
View File
@@ -2,12 +2,23 @@
[![GitHub Release](https://img.shields.io/github/release/valentineus/moodle-webhooks.svg)](https://github.com/valentineus/moodle-webhooks/releases) [![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) [![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) [![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/auth-link) [![Gitter Badge](https://badges.gitter.im/Join%20Chat.svg)](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!**: Using Webhooks, your application will be able to receive notifications of changes to selected topics and their fields.
This plugin is in active development. 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.
Features:
* 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;
## Installation ## Installation
-51
View File
@@ -1,51 +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/>.
/**
* Create a class for sending data.
*
* @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();
class curl {
public function __construct() {
if (!function_exists("curl_init")) {
print_error("nocurl", "mnet");
}
}
public static function request($callback, $data) {
$ch = curl_init($callback->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/" . $callback->type,
"Content-Length: " . strlen($data))
);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
+8 -13
View File
@@ -26,6 +26,10 @@ namespace local_webhooks;
defined("MOODLE_INTERNAL") || die(); defined("MOODLE_INTERNAL") || die();
require_once($CFG->libdir . "/filelib.php");
use curl;
/** /**
* Defines how to work with events. * Defines how to work with events.
* *
@@ -78,8 +82,7 @@ class events {
$events = unserialize(gzuncompress(base64_decode($callback->events))); $events = unserialize(gzuncompress(base64_decode($callback->events)));
} }
if (boolval($events[$data["eventname"]])) { if (!empty($events[$data["eventname"]])) {
/* Adding to the data token */
if (boolval($callback->token)) { if (boolval($callback->token)) {
$data["token"] = $callback->token; $data["token"] = $callback->token;
} }
@@ -97,16 +100,8 @@ class events {
*/ */
private static function send($data, $callback) { private static function send($data, $callback) {
$curl = new curl(); $curl = new curl();
$package = self::packup($data); $curl->setHeader(array("Content-Type: application/$callback->type"));
$curl::request($callback, $package); $curl->post($callback->url, json_encode($data));
} return $curl->getResponse();
/**
* Packs the data for transmission.
*
* @param array $data
*/
private static function packup($data) {
return json_encode($data);
} }
} }
+14 -24
View File
@@ -52,7 +52,10 @@ class service_edit_form extends moodleform {
* @param object $record * @param object $record
*/ */
public function set_data($record) { public function set_data($record) {
$record->events = unserialize(gzuncompress(base64_decode($record->events))); if (!empty($record->events)) {
$record->events = unserialize(gzuncompress(base64_decode($record->events)));
}
return parent::set_data($record); return parent::set_data($record);
} }
@@ -63,46 +66,34 @@ class service_edit_form extends moodleform {
$mform =& $this->_form; $mform =& $this->_form;
/* Form heading */ /* Form heading */
$mform->addElement("header", "editserviceheader", $mform->addElement("header", "editserviceheader", new lang_string("service", "webservice"));
new lang_string("service", "webservice"));
/* Name of the service */ /* Name of the service */
$mform->addElement("text", "title", $mform->addElement("text", "title", new lang_string("name", "moodle"), array("size" => 60));
new lang_string("name", "moodle"),
array("size" => 60));
$mform->setType("title", PARAM_NOTAGS);
$mform->addRule("title", null, "required"); $mform->addRule("title", null, "required");
$mform->setType("title", PARAM_NOTAGS);
/* Callback address */ /* Callback address */
$mform->addElement("text", "url", $mform->addElement("text", "url", new lang_string("url", "moodle"), array("size" => 60));
new lang_string("url", "moodle"),
array("size" => 60));
$mform->setType("url", PARAM_URL);
$mform->addRule("url", null, "required"); $mform->addRule("url", null, "required");
$mform->setType("url", PARAM_URL);
/* Enabling the service */ /* Enabling the service */
$mform->addElement("advcheckbox", "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 */ /* Token */
$mform->addElement("text", "token", $mform->addElement("text", "token", new lang_string("token", "webservice"), array("size" => 60));
new lang_string("token", "webservice"),
array("size" => 60));
$mform->setType("token", PARAM_NOTAGS); $mform->setType("token", PARAM_NOTAGS);
/* Content type */ /* Content type */
$mform->addElement("select", "type", "Content type", $mform->addElement("select", "type", "Content type", array("json" => "application/json", "x-www-form-urlencoded" => "application/x-www-form-urlencoded"));
array(
"json" => "application/json",
"x-www-form-urlencoded" => "application/x-www-form-urlencoded"));
$mform->setAdvanced("type"); $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"));
/* List of events */ /* List of events */
$eventlist = report_eventlist_list_generator::get_all_events_list(true); $eventlist = report_eventlist_list_generator::get_all_events_list(true);
@@ -110,8 +101,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("checkbox", $event["eventname"], $event["eventname"]);
$mform->createElement("checkbox", $event["eventname"], $event["eventname"]);
} }
/* Displays groups of items */ /* Displays groups of items */
+1 -1
View File
@@ -9,7 +9,7 @@
<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="type" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" />
<FIELD NAME="token" 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="false" SEQUENCE="false" />
</FIELDS> </FIELDS>
<KEYS> <KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" /> <KEY NAME="primary" TYPE="primary" FIELDS="id" />
+13 -12
View File
@@ -23,14 +23,17 @@
*/ */
require_once(__DIR__ . "/../../config.php"); require_once(__DIR__ . "/../../config.php");
require_once($CFG->libdir . "/adminlib.php");
require_once(__DIR__ . "/classes/forms.php"); require_once(__DIR__ . "/classes/forms.php");
$idservice = optional_param("idservice", 0, PARAM_INT); admin_externalpage_setup("managelocalplugins");
$serviceid = optional_param("serviceid", 0, PARAM_INT);
require_login(); require_login();
/* Link generation */ /* Link generation */
$urlparameters = array("idservice" => $idservice); $urlparameters = array("serviceid" => $serviceid);
$managerservice = new moodle_url("/local/webhooks/managerservice.php", $urlparameters); $managerservice = new moodle_url("/local/webhooks/managerservice.php", $urlparameters);
$baseurl = new moodle_url("/local/webhooks/editservice.php", $urlparameters); $baseurl = new moodle_url("/local/webhooks/editservice.php", $urlparameters);
$PAGE->set_url($baseurl, $urlparameters); $PAGE->set_url($baseurl, $urlparameters);
@@ -52,26 +55,25 @@ if ($mform->is_cancelled()) {
} }
/* Getting the data */ /* Getting the data */
if ($idediting = boolval($idservice)) { if ($idediting = boolval($serviceid)) {
$servicerecord = $DB->get_record("local_webhooks_service", array("id" => $idservice), "*", MUST_EXIST); $servicerecord = $DB->get_record("local_webhooks_service", array("id" => $serviceid), "*", MUST_EXIST);
$mform->set_data($servicerecord); $mform->set_data($servicerecord);
} }
/* Processing of received data */ /* Processing of received data */
if ($data = $mform->get_data()) { if ($data = $mform->get_data()) {
/* Packing of data */
if (!empty($data->events)) { 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 (boolval($idediting)) {
$data->id = $idservice; $data->id = $serviceid;
$DB->update_record("local_webhooks_service", $data); $DB->update_record("local_webhooks_service", $data);
redirect($managerservice, new lang_string("eventwebserviceserviceupdated", "webservice"));
} else { } else {
$DB->insert_record("local_webhooks_service", $data); $DB->insert_record("local_webhooks_service", $data);
redirect($managerservice, new lang_string("eventwebserviceservicecreated", "webservice"));
} }
redirect($managerservice);
} }
/* Page template */ /* Page template */
@@ -80,9 +82,8 @@ $PAGE->set_heading($titlepage);
$PAGE->set_title($titlepage); $PAGE->set_title($titlepage);
/* The page title */ /* The page title */
$PAGE->navbar->add(new lang_string("localplugins", "moodle")); $PAGE->navbar->add(new lang_string("pluginname", "local_webhooks"), new moodle_url("/admin/settings.php", array("section" => "local_webhooks")));
$PAGE->navbar->add(new lang_string("pluginname", "local_webhooks")); $PAGE->navbar->add(new lang_string("externalservices", "webservice"), $managerservice);
$PAGE->navbar->add(new lang_string("managerservice", "local_webhooks"), $managerservice);
$PAGE->navbar->add($titlepage); $PAGE->navbar->add($titlepage);
echo $OUTPUT->header(); echo $OUTPUT->header();
-1
View File
@@ -22,5 +22,4 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
$string["managerservice"] = "Service management manager";
$string["pluginname"] = "WebHooks"; $string["pluginname"] = "WebHooks";
-26
View File
@@ -1,26 +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/>.
/**
* 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["managerservice"] = "Менеджер управления службами";
$string["pluginname"] = "WebHooks";
+43 -26
View File
@@ -24,8 +24,12 @@
require_once(__DIR__ . "/../../config.php"); require_once(__DIR__ . "/../../config.php");
require_once($CFG->libdir . "/tablelib.php"); require_once($CFG->libdir . "/tablelib.php");
require_once($CFG->libdir . "/adminlib.php");
$deleteservice = optional_param("deleteservice", 0, PARAM_INT); admin_externalpage_setup("managelocalplugins");
$hideshowid = optional_param("hideshowid", 0, PARAM_INT);
$deleteid = optional_param("deleteid", 0, PARAM_INT);
require_login(); require_login();
@@ -40,36 +44,42 @@ $context = context_system::instance();
$PAGE->set_context($context); $PAGE->set_context($context);
/* Delete the service */ /* Delete the service */
if ($deleteservice && confirm_sesskey()) { if (boolval($deleteid) && confirm_sesskey()) {
$DB->delete_records("local_webhooks_service", array("id" => $deleteservice)); $DB->delete_records("local_webhooks_service", array("id" => $deleteid));
redirect($PAGE->url, new lang_string("deleted", "moodle")); redirect($PAGE->url, new lang_string("eventwebserviceservicedeleted", "webservice"));
} }
/* Retrieving a list of services */ /* Retrieving a list of services */
$select = null; $callbacks = $DB->get_records_select("local_webhooks_service", null, null, $DB->sql_order_by_text("id"));
$callbacks = $DB->get_records_select("local_webhooks_service", $select, null, $DB->sql_order_by_text("id"));
/* Switching the status of the service */
if (boolval($hideshowid) && confirm_sesskey()) {
$callback = $callbacks[$hideshowid];
if (!empty($callback)) {
$callback->enable = !boolval($callback->enable);
$DB->update_record("local_webhooks_service", $callback);
redirect($PAGE->url, new lang_string("eventwebserviceserviceupdated", "webservice"));
}
}
/* Page template */ /* Page template */
$titlepage = new lang_string("managerservice", "local_webhooks"); $titlepage = new lang_string("externalservices", "webservice");
$PAGE->set_pagelayout("standard"); $PAGE->set_pagelayout("admin");
$PAGE->set_title($titlepage); $PAGE->set_title($titlepage);
$PAGE->set_heading($titlepage); $PAGE->set_heading($titlepage);
/* The page title */ /* The page title */
$PAGE->navbar->add(new lang_string("localplugins", "moodle")); $PAGE->navbar->add(new lang_string("pluginname", "local_webhooks"), new moodle_url("/admin/settings.php", array("section" => "local_webhooks")));
$PAGE->navbar->add(new lang_string("pluginname", "local_webhooks"));
$PAGE->navbar->add($titlepage, $baseurl); $PAGE->navbar->add($titlepage, $baseurl);
echo $OUTPUT->header(); echo $OUTPUT->header();
/* Table declaration */ /* Table declaration */
$table = new flexible_table("callbacks-table"); $table = new flexible_table("webhooks-service-table");
/* Customize the table */ /* Customize the table */
$table->define_columns(array("title", "url", "actions")); $table->define_columns(array("title", "url", "actions"));
$table->define_headers(array( $table->define_headers(array(new lang_string("name", "moodle"), new lang_string("url", "moodle"), new lang_string("actions", "moodle")));
new lang_string("name", "moodle"),
new lang_string("url", "moodle"),
new lang_string("actions", "moodle")));
$table->define_baseurl($baseurl); $table->define_baseurl($baseurl);
$table->setup(); $table->setup();
@@ -78,20 +88,28 @@ foreach ($callbacks as $callback) {
$titlecallback = html_writer::div($callback->title, "title"); $titlecallback = html_writer::div($callback->title, "title");
$urlcallback = html_writer::div($callback->url, "url"); $urlcallback = html_writer::div($callback->url, "url");
/* Defining service status */
$hideshowicon = "t/show";
$hideshowstring = new lang_string("enable", "moodle");
if (boolval($callback->enable)) {
$hideshowicon = "t/hide";
$hideshowstring = new lang_string("disable", "moodle");
}
/* Link to enable / disable the service */
$hideshowlink = new moodle_url($managerservice, array("hideshowid" => $callback->id, "sesskey" => sesskey()));
$hideshowitem = $OUTPUT->action_icon($hideshowlink, new pix_icon($hideshowicon, $hideshowstring));
/* Link for editing */ /* Link for editing */
$editlink = new moodle_url($editservice, $editlink = new moodle_url($editservice, array("serviceid" => $callback->id));
array("idservice" => $callback->id)); $edititem = $OUTPUT->action_icon($editlink, new pix_icon("t/edit", new lang_string("edit", "moodle")));
$edititem = $OUTPUT->action_icon($editlink,
new pix_icon("t/edit", get_string("edit")));
/* Link to remove */ /* Link to remove */
$deletelink = new moodle_url($managerservice, $deletelink = new moodle_url($managerservice, array("deleteid" => $callback->id, "sesskey" => sesskey()));
array("deleteservice" => $callback->id, "sesskey" => sesskey())); $deleteitem = $OUTPUT->action_icon($deletelink, new pix_icon("t/delete", new lang_string("delete", "moodle")));
$deleteitem = $OUTPUT->action_icon($deletelink,
new pix_icon("t/delete", get_string("delete")));
/* Adding data to the table */ /* Adding data to the table */
$table->add_data(array($titlecallback, $urlcallback, $edititem . $deleteitem)); $table->add_data(array($titlecallback, $urlcallback, $hideshowitem . $edititem . $deleteitem));
} }
/* Display the table */ /* Display the table */
@@ -99,7 +117,6 @@ $table->print_html();
/* Add service button */ /* Add service button */
$addurl = new moodle_url("/local/webhooks/editservice.php"); $addurl = new moodle_url("/local/webhooks/editservice.php");
echo $OUTPUT->single_button($addurl, echo $OUTPUT->single_button($addurl, new lang_string("addaservice", "webservice"), "get");
new lang_string("addaservice", "webservice"), "get");
echo $OUTPUT->footer(); echo $OUTPUT->footer();
+5 -11
View File
@@ -26,20 +26,14 @@ defined("MOODLE_INTERNAL") || die();
$settings = null; $settings = null;
if ($hassiteconfig) { if ($hassiteconfig) {
$settings = new admin_settingpage("local_webhooks", $settings = new admin_settingpage("local_webhooks", new lang_string("pluginname", "local_webhooks"));
new lang_string("pluginname", "local_webhooks"));
$ADMIN->add("localplugins", $settings); $ADMIN->add("localplugins", $settings);
$settings->add(new admin_setting_configcheckbox( /* The switch of the interceptor of events */
"local_webhooks/enable", $settings->add(new admin_setting_configcheckbox("local_webhooks/enable", new lang_string("enable", "moodle"), new lang_string("enablews", "webservice"), false));
new lang_string("enable", "moodle"),
new lang_string("enablews", "webservice"),
false));
/* Link to the service manager */ /* Link to the service manager */
$linktext = new lang_string("managerservice", "local_webhooks"); $linktext = new lang_string("externalservices", "webservice");
$linkurl = new moodle_url("/local/webhooks/managerservice.php"); $linkurl = new moodle_url("/local/webhooks/managerservice.php");
$settings->add(new admin_setting_heading("local_webhooks_managerservice", null, $settings->add(new admin_setting_heading("local_webhooks_managerservice", null, html_writer::link($linkurl, $linktext)));
html_writer::link($linkurl, $linktext)));
} }
+3 -3
View File
@@ -23,8 +23,8 @@
*/ */
defined("MOODLE_INTERNAL") || die(); defined("MOODLE_INTERNAL") || die();
$plugin->release = "0.4.0 (Build: 2017102620)"; $plugin->release = "0.5.0 (Build: 2017102630)";
$plugin->version = 2017102620; $plugin->version = 2017102630;
$plugin->requires = 2016112900; $plugin->requires = 2016112900;
$plugin->component = "local_webhooks"; $plugin->component = "local_webhooks";
$plugin->maturity = MATURITY_BETA; $plugin->maturity = MATURITY_RC;