Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
07970f36e3 | ||
|
|
1e81988bf5 | ||
|
|
217c79d867 | ||
|
|
4c63223050 | ||
|
|
41a1acc36b | ||
|
|
9e1c2cca4e | ||
|
|
74c106aa87 | ||
|
|
700833b532 | ||
|
|
31ecf15e95 | ||
|
|
8afc94b51c | ||
|
|
2885dddccc | ||
|
|
0e4b7bc4d9 | ||
|
|
d6d13338e3 | ||
|
|
43ee70c01e | ||
|
|
6dfe28dab5 | ||
|
|
04e60b112b | ||
|
|
9e4c2dcbae | ||
|
|
1160a52753 | ||
|
|
af6ac9fb22 |
@@ -47,7 +47,7 @@ class service_edit_form extends moodleform {
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpacks data for display.
|
||||
* Preparing data before displaying.
|
||||
*
|
||||
* @param object $record
|
||||
*/
|
||||
@@ -64,17 +64,18 @@ class service_edit_form extends moodleform {
|
||||
*/
|
||||
protected function definition() {
|
||||
$mform =& $this->_form;
|
||||
$size = array("size" => 60);
|
||||
|
||||
/* 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->addElement("text", "title", new lang_string("name", "moodle"), $size);
|
||||
$mform->addRule("title", null, "required");
|
||||
$mform->setType("title", PARAM_NOTAGS);
|
||||
|
||||
/* Callback address */
|
||||
$mform->addElement("text", "url", new lang_string("url", "moodle"), array("size" => 60));
|
||||
$mform->addElement("text", "url", new lang_string("url", "moodle"), $size);
|
||||
$mform->addRule("url", null, "required");
|
||||
$mform->setType("url", PARAM_URL);
|
||||
|
||||
@@ -85,11 +86,17 @@ class service_edit_form extends moodleform {
|
||||
$mform->setAdvanced("enable");
|
||||
|
||||
/* Token */
|
||||
$mform->addElement("text", "token", new lang_string("token", "webservice"), array("size" => 60));
|
||||
$mform->addElement("text", "token", new lang_string("token", "webservice"), $size);
|
||||
$mform->setType("token", PARAM_NOTAGS);
|
||||
|
||||
/* Additional information */
|
||||
$mform->addElement("text", "other", new lang_string("courserequestsupport", "moodle"), $size);
|
||||
$mform->setType("other", PARAM_NOTAGS);
|
||||
$mform->setAdvanced("other");
|
||||
|
||||
/* Content type */
|
||||
$mform->addElement("select", "type", "Content type", 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->setAdvanced("type");
|
||||
|
||||
/* Form heading */
|
||||
@@ -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 response_get extends \core\event\base {
|
||||
/**
|
||||
* Init method.
|
||||
*/
|
||||
protected function init() {
|
||||
$this->data["crud"] = "u";
|
||||
$this->data["objecttable"] = "local_webhooks_service";
|
||||
$this->data["edulevel"] = self::LEVEL_OTHER;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return localised event name.
|
||||
*/
|
||||
public static function get_name() {
|
||||
return new \lang_string("answer", "moodle");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of what happened.
|
||||
*/
|
||||
public function get_description() {
|
||||
return $this->other["status"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL related to the action.
|
||||
*/
|
||||
public function get_url() {
|
||||
return new \moodle_url("/local/webhooks/editservice.php", array("serviceid" => $this->objectid));
|
||||
}
|
||||
}
|
||||
@@ -28,21 +28,19 @@ defined("MOODLE_INTERNAL") || die();
|
||||
|
||||
require_once($CFG->libdir . "/filelib.php");
|
||||
|
||||
use curl;
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
class handler {
|
||||
/**
|
||||
* External handler.
|
||||
*
|
||||
* @param object $event
|
||||
*/
|
||||
public static function handler($event) {
|
||||
public static function events($event) {
|
||||
$enable = get_config("local_webhooks", "enable");
|
||||
|
||||
if (boolval($enable)) {
|
||||
@@ -77,16 +75,21 @@ class events {
|
||||
* @param object $callback
|
||||
*/
|
||||
private static function handler_callback($data, $callback) {
|
||||
if ($callback->enable) {
|
||||
if (boolval($callback->enable)) {
|
||||
$events = array();
|
||||
if (!empty($callback->events)) {
|
||||
$events = unserialize(gzuncompress(base64_decode($callback->events)));
|
||||
}
|
||||
|
||||
if (!empty($events[$data["eventname"]])) {
|
||||
if (boolval($callback->token)) {
|
||||
if (!empty($callback->token)) {
|
||||
$data["token"] = $callback->token;
|
||||
}
|
||||
|
||||
if (!empty($callback->other)) {
|
||||
$data["extra"] = $callback->other;
|
||||
}
|
||||
|
||||
self::send($data, $callback);
|
||||
}
|
||||
}
|
||||
@@ -99,9 +102,35 @@ class events {
|
||||
* @param object $callback
|
||||
*/
|
||||
private static function send($data, $callback) {
|
||||
$curl = new curl();
|
||||
$curl = new \curl();
|
||||
$curl->setHeader(array("Content-Type: application/$callback->type"));
|
||||
$curl->post($callback->url, json_encode($data));
|
||||
return $curl->getResponse();
|
||||
$response = $curl->getResponse();
|
||||
self::logger($callback, $response);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event logging.
|
||||
*
|
||||
* @param array $response
|
||||
* @param object $callback
|
||||
*/
|
||||
private static function logger($callback, $response) {
|
||||
$status = "Error sending request";
|
||||
if (!empty($response["HTTP/1.1"])) {
|
||||
$status = $response["HTTP/1.1"];
|
||||
}
|
||||
|
||||
$event = \local_webhooks\event\response_get::create(
|
||||
array(
|
||||
"context" => \context_system::instance(0),
|
||||
"objectid" => $callback->id,
|
||||
"other" => array(
|
||||
"status" => $status
|
||||
)
|
||||
));
|
||||
|
||||
$event->trigger();
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -27,7 +27,7 @@ defined("MOODLE_INTERNAL") || die();
|
||||
$observers = array(
|
||||
array(
|
||||
"eventname" => "*",
|
||||
"callback" => "\local_webhooks\\events::handler",
|
||||
"callback" => "\local_webhooks\\handler::events",
|
||||
"includefile" => null,
|
||||
"priority" => 200,
|
||||
"internal" => true
|
||||
|
||||
+3
-2
@@ -1,5 +1,5 @@
|
||||
<?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">
|
||||
<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>
|
||||
<TABLE NAME="local_webhooks_service" COMMENT="A table for storing callback services.">
|
||||
<FIELDS>
|
||||
@@ -10,10 +10,11 @@
|
||||
<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="false" SEQUENCE="false" />
|
||||
<FIELD NAME="other" TYPE="text" NOTNULL="false" SEQUENCE="false" />
|
||||
</FIELDS>
|
||||
<KEYS>
|
||||
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
|
||||
</KEYS>
|
||||
</TABLE>
|
||||
</TABLES>
|
||||
</XMLDB>
|
||||
</XMLDB>
|
||||
+3
-3
@@ -23,8 +23,8 @@
|
||||
*/
|
||||
|
||||
require_once(__DIR__ . "/../../config.php");
|
||||
require_once(__DIR__ . "/classes/editform.php");
|
||||
require_once($CFG->libdir . "/adminlib.php");
|
||||
require_once(__DIR__ . "/classes/forms.php");
|
||||
|
||||
admin_externalpage_setup("managelocalplugins");
|
||||
|
||||
@@ -55,7 +55,7 @@ if ($mform->is_cancelled()) {
|
||||
}
|
||||
|
||||
/* Getting the data */
|
||||
if ($idediting = boolval($serviceid)) {
|
||||
if ($editing = boolval($serviceid)) {
|
||||
$servicerecord = $DB->get_record("local_webhooks_service", array("id" => $serviceid), "*", MUST_EXIST);
|
||||
$mform->set_data($servicerecord);
|
||||
}
|
||||
@@ -66,7 +66,7 @@ if ($data = $mform->get_data()) {
|
||||
$data->events = base64_encode(gzcompress(serialize($data->events), 9));
|
||||
}
|
||||
|
||||
if (boolval($idediting)) {
|
||||
if ($editing) {
|
||||
$data->id = $serviceid;
|
||||
$DB->update_record("local_webhooks_service", $data);
|
||||
redirect($managerservice, new lang_string("eventwebserviceserviceupdated", "webservice"));
|
||||
|
||||
+4
-3
@@ -23,8 +23,9 @@
|
||||
*/
|
||||
|
||||
defined("MOODLE_INTERNAL") || die();
|
||||
$plugin->release = "0.5.0 (Build: 2017102630)";
|
||||
$plugin->version = 2017102630;
|
||||
|
||||
$plugin->release = "1.1.0 (Build: 2017102900)";
|
||||
$plugin->version = 2017102900;
|
||||
$plugin->requires = 2016112900;
|
||||
$plugin->component = "local_webhooks";
|
||||
$plugin->maturity = MATURITY_RC;
|
||||
$plugin->maturity = MATURITY_STABLE;
|
||||
Reference in New Issue
Block a user