Compare commits

..
17 Commits
10 changed files with 181 additions and 58 deletions
+39 -2
View File
@@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** /**
* Defines the form of editing the service. * Defines forms.
* *
* @package local_webhooks * @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link> * @copyright 2017 "Valentin Popov" <info@valentineus.link>
@@ -28,9 +28,9 @@ defined("MOODLE_INTERNAL") || die();
require_once($CFG->libdir . "/formslib.php"); require_once($CFG->libdir . "/formslib.php");
use report_eventlist_list_generator;
use lang_string; use lang_string;
use moodleform; use moodleform;
use report_eventlist_list_generator;
/** /**
* Description editing form definition. * Description editing form definition.
@@ -89,6 +89,11 @@ class service_edit_form extends moodleform {
$mform->addElement("text", "token", new lang_string("token", "webservice"), $size); $mform->addElement("text", "token", new lang_string("token", "webservice"), $size);
$mform->setType("token", PARAM_NOTAGS); $mform->setType("token", PARAM_NOTAGS);
/* Additional information */
$mform->addElement("text", "other", new lang_string("sourceext", "plugin"), $size);
$mform->setType("other", PARAM_NOTAGS);
$mform->setAdvanced("other");
/* Content type */ /* Content type */
$contenttype = array("json" => "application/json", "x-www-form-urlencoded" => "application/x-www-form-urlencoded"); $contenttype = array("json" => "application/json", "x-www-form-urlencoded" => "application/x-www-form-urlencoded");
$mform->addElement("select", "type", "Content type", $contenttype); $mform->addElement("select", "type", "Content type", $contenttype);
@@ -115,3 +120,35 @@ class service_edit_form extends moodleform {
$this->add_action_buttons(true); $this->add_action_buttons(true);
} }
} }
/**
* Description of the form of restoration.
*
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class service_backup_form extends moodleform {
/**
* @param string $baseurl
*/
public function __construct($baseurl) {
parent::__construct($baseurl);
}
/**
* Defines the standard structure of the form.
*/
protected function definition() {
$mform =& $this->_form;
/* Form heading */
$mform->addElement("header", "editserviceheader", new lang_string("restore", "moodle"));
/* Download the file */
$mform->addElement("filepicker", "backupfile", new lang_string("file", "moodle"));
$mform->addRule("backupfile", null, "required");
/* Control Panel */
$this->add_action_buttons(true, new lang_string("restore", "moodle"));
}
}
+2 -2
View File
@@ -37,9 +37,9 @@ class response_get extends \core\event\base {
* Init method. * Init method.
*/ */
protected function init() { protected function init() {
$this->data["crud"] = "u"; $this->data["crud"] = "r";
$this->data["objecttable"] = "local_webhooks_service";
$this->data["edulevel"] = self::LEVEL_OTHER; $this->data["edulevel"] = self::LEVEL_OTHER;
$this->data["objecttable"] = "local_webhooks_service";
} }
/** /**
+6 -5
View File
@@ -41,13 +41,9 @@ class handler {
* @param object $event * @param object $event
*/ */
public static function events($event) { public static function events($event) {
$enable = get_config("local_webhooks", "enable");
if (boolval($enable)) {
$data = $event->get_data(); $data = $event->get_data();
self::transmitter($data); self::transmitter($data);
} }
}
/** /**
* Transmitter, processing event and services. * Transmitter, processing event and services.
@@ -86,6 +82,10 @@ class handler {
$data["token"] = $callback->token; $data["token"] = $callback->token;
} }
if (!empty($callback->other)) {
$data["extra"] = $callback->other;
}
self::send($data, $callback); self::send($data, $callback);
} }
} }
@@ -125,7 +125,8 @@ class handler {
"other" => array( "other" => array(
"status" => $status "status" => $status
) )
)); )
);
$event->trigger(); $event->trigger();
} }
+3 -3
View File
@@ -26,10 +26,10 @@ defined("MOODLE_INTERNAL") || die();
$observers = array( $observers = array(
array( array(
"eventname" => "*",
"callback" => "\local_webhooks\\handler::events", "callback" => "\local_webhooks\\handler::events",
"eventname" => "*",
"includefile" => null, "includefile" => null,
"priority" => 200, "internal" => true,
"internal" => true "priority" => 200
) )
); );
+2 -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="20171027" COMMENT="XMLDB file for Moodle" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"> <XMLDB PATH="blocks/local_webhooks/db" VERSION="20171029" COMMENT="XMLDB file for Moodle" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd">
<TABLES> <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>
@@ -10,6 +10,7 @@
<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="false" SEQUENCE="false" /> <FIELD NAME="events" TYPE="text" NOTNULL="false" SEQUENCE="false" />
<FIELD NAME="other" TYPE="text" NOTNULL="false" SEQUENCE="false" />
</FIELDS> </FIELDS>
<KEYS> <KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" /> <KEY NAME="primary" TYPE="primary" FIELDS="id" />
+7 -7
View File
@@ -26,12 +26,11 @@ require_once(__DIR__ . "/../../config.php");
require_once(__DIR__ . "/classes/editform.php"); require_once(__DIR__ . "/classes/editform.php");
require_once($CFG->libdir . "/adminlib.php"); require_once($CFG->libdir . "/adminlib.php");
admin_externalpage_setup("managelocalplugins"); admin_externalpage_setup("local_webhooks");
/* Optional parameters */
$serviceid = optional_param("serviceid", 0, PARAM_INT); $serviceid = optional_param("serviceid", 0, PARAM_INT);
require_login();
/* Link generation */ /* Link generation */
$urlparameters = array("serviceid" => $serviceid); $urlparameters = array("serviceid" => $serviceid);
$managerservice = new moodle_url("/local/webhooks/managerservice.php", $urlparameters); $managerservice = new moodle_url("/local/webhooks/managerservice.php", $urlparameters);
@@ -62,10 +61,13 @@ if ($editing = boolval($serviceid)) {
/* Processing of received data */ /* Processing of received data */
if ($data = $mform->get_data()) { if ($data = $mform->get_data()) {
if (!empty($data->events)) { if (empty($data->events)) {
$data->events = base64_encode(gzcompress(serialize($data->events), 9)); $data->events = array();
} }
/* Pack the list of events */
$data->events = base64_encode(gzcompress(serialize($data->events), 9));
if ($editing) { if ($editing) {
$data->id = $serviceid; $data->id = $serviceid;
$DB->update_record("local_webhooks_service", $data); $DB->update_record("local_webhooks_service", $data);
@@ -82,8 +84,6 @@ $PAGE->set_heading($titlepage);
$PAGE->set_title($titlepage); $PAGE->set_title($titlepage);
/* The page title */ /* The page title */
$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("externalservices", "webservice"), $managerservice);
$PAGE->navbar->add($titlepage); $PAGE->navbar->add($titlepage);
echo $OUTPUT->header(); echo $OUTPUT->header();
+26 -10
View File
@@ -26,16 +26,19 @@ require_once(__DIR__ . "/../../config.php");
require_once($CFG->libdir . "/tablelib.php"); require_once($CFG->libdir . "/tablelib.php");
require_once($CFG->libdir . "/adminlib.php"); require_once($CFG->libdir . "/adminlib.php");
admin_externalpage_setup("managelocalplugins"); admin_externalpage_setup("local_webhooks");
$hideshowid = optional_param("hideshowid", 0, PARAM_INT); /* Optional parameters */
$backupservices = optional_param("getbackup", 0, PARAM_BOOL);
$deleteid = optional_param("deleteid", 0, PARAM_INT); $deleteid = optional_param("deleteid", 0, PARAM_INT);
$hideshowid = optional_param("hideshowid", 0, PARAM_INT);
require_login(); /* Used references */
$editservice = "/local/webhooks/editservice.php";
$managerservice = "/local/webhooks/managerservice.php";
$restorebackup = "/local/webhooks/restorebackup.php";
/* Link generation */ /* Link generation */
$managerservice = "/local/webhooks/managerservice.php";
$editservice = "/local/webhooks/editservice.php";
$baseurl = new moodle_url($managerservice); $baseurl = new moodle_url($managerservice);
$PAGE->set_url($baseurl); $PAGE->set_url($baseurl);
@@ -52,6 +55,13 @@ if (boolval($deleteid) && confirm_sesskey()) {
/* Retrieving a list of services */ /* Retrieving a list of services */
$callbacks = $DB->get_records_select("local_webhooks_service", null, null, $DB->sql_order_by_text("id")); $callbacks = $DB->get_records_select("local_webhooks_service", null, null, $DB->sql_order_by_text("id"));
/* Upload settings as a file */
if (boolval($backupservices)) {
$filecontent = base64_encode(gzcompress(serialize($callbacks), 9));
$filename = "webhooks_" . date("U") . ".backup";
send_file($filecontent, $filename, 0, 0, true, true);
}
/* Switching the status of the service */ /* Switching the status of the service */
if (boolval($hideshowid) && confirm_sesskey()) { if (boolval($hideshowid) && confirm_sesskey()) {
$callback = $callbacks[$hideshowid]; $callback = $callbacks[$hideshowid];
@@ -64,14 +74,12 @@ if (boolval($hideshowid) && confirm_sesskey()) {
} }
/* Page template */ /* Page template */
$titlepage = new lang_string("externalservices", "webservice"); $titlepage = new lang_string("pluginname", "local_webhooks");
$PAGE->set_pagelayout("admin"); $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("pluginname", "local_webhooks"), new moodle_url("/admin/settings.php", array("section" => "local_webhooks")));
$PAGE->navbar->add($titlepage, $baseurl);
echo $OUTPUT->header(); echo $OUTPUT->header();
/* Table declaration */ /* Table declaration */
@@ -116,7 +124,15 @@ foreach ($callbacks as $callback) {
$table->print_html(); $table->print_html();
/* Add service button */ /* Add service button */
$addurl = new moodle_url("/local/webhooks/editservice.php"); $addserviceurl = new moodle_url($editservice);
echo $OUTPUT->single_button($addurl, new lang_string("addaservice", "webservice"), "get"); echo $OUTPUT->single_button($addserviceurl, new lang_string("addaservice", "webservice"), "get");
/* Button to get a backup */
$backupurl = new moodle_url($managerservice, array("getbackup" => true));
echo $OUTPUT->single_button($backupurl, new lang_string("backup", "moodle"), "get");
/* Button for restoring settings */
$restorebackupurl = new moodle_url($restorebackup, array("sesskey" => sesskey()));
echo $OUTPUT->single_button($restorebackupurl, new lang_string("restore", "moodle"), "get");
echo $OUTPUT->footer(); echo $OUTPUT->footer();
+75
View File
@@ -0,0 +1,75 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Restore settings page.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . "/../../config.php");
require_once(__DIR__ . "/classes/editform.php");
require_once($CFG->libdir . "/adminlib.php");
admin_externalpage_setup("local_webhooks");
/* Link generation */
$managerservice = new moodle_url("/local/webhooks/managerservice.php");
$baseurl = new moodle_url("/local/webhooks/restorebackup.php");
$PAGE->set_url($baseurl);
/* Configure the context of the page */
$context = context_system::instance();
$PAGE->set_context($context);
/* Create an editing form */
$mform = new \local_webhooks\service_backup_form($PAGE->url);
/* Cancel processing */
if ($mform->is_cancelled()) {
redirect($managerservice);
}
/* Processing the received file */
$data = $mform->get_data();
if (boolval($data) && confirm_sesskey()) {
$content = $mform->get_file_content("backupfile");
$callbacks = unserialize(gzuncompress(base64_decode($content)));
$DB->delete_records("local_webhooks_service");
foreach ($callbacks as $callback) {
$DB->insert_record("local_webhooks_service", $callback);
}
redirect($managerservice, new lang_string("restorefinished", "moodle"));
}
/* Page template */
$titlepage = new lang_string("backup", "moodle");
$PAGE->set_pagelayout("admin");
$PAGE->set_heading($titlepage);
$PAGE->set_title($titlepage);
/* The page title */
$PAGE->navbar->add($titlepage);
echo $OUTPUT->header();
/* Displays the form */
$mform->display();
echo $OUTPUT->footer();
+4 -11
View File
@@ -24,16 +24,9 @@
defined("MOODLE_INTERNAL") || die(); defined("MOODLE_INTERNAL") || die();
$settings = null;
if ($hassiteconfig) { if ($hassiteconfig) {
$settings = new admin_settingpage("local_webhooks", new lang_string("pluginname", "local_webhooks")); $ADMIN->add("server", new admin_externalpage("local_webhooks",
$ADMIN->add("localplugins", $settings); new lang_string("pluginname", "local_webhooks"),
new moodle_url("/local/webhooks/managerservice.php")
/* The switch of the interceptor of events */ ));
$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("externalservices", "webservice");
$linkurl = new moodle_url("/local/webhooks/managerservice.php");
$settings->add(new admin_setting_heading("local_webhooks_managerservice", null, html_writer::link($linkurl, $linktext)));
} }
+2 -2
View File
@@ -24,8 +24,8 @@
defined("MOODLE_INTERNAL") || die(); defined("MOODLE_INTERNAL") || die();
$plugin->release = "1.0.0 (Build: 2017102700)"; $plugin->release = "2.0.0 (Build: 2017111800)";
$plugin->version = 2017102700; $plugin->version = 2017111800;
$plugin->requires = 2016112900; $plugin->requires = 2016112900;
$plugin->component = "local_webhooks"; $plugin->component = "local_webhooks";
$plugin->maturity = MATURITY_STABLE; $plugin->maturity = MATURITY_STABLE;