Compare commits

...

32 Commits

Author SHA1 Message Date
edb23d6aad
Added mirror 2024-05-29 20:08:39 +04:00
064e2fa422
Update README.md
Signed-off-by: Valentin Popov <valentin@popov.link>
2023-12-28 16:58:03 +04:00
9cf4fc575c
Correction of spaces 2018-02-25 01:32:29 +04:00
f3c2ebbe4d
Correcting a stupid mistake 2018-02-25 01:27:40 +04:00
8f25379f32
Change version 2018-02-25 01:21:50 +04:00
90897b0e7d
General refactoring of the web services code 2018-02-25 01:19:19 +04:00
685d9ee15e Change version 2018-02-22 12:58:37 +04:00
db500cddce Fix 'invalid OPTIONAL value specified' 2018-02-22 12:55:20 +04:00
85bcb64e37 Added general list functions 2018-02-22 12:44:50 +04:00
1736128dbf Adding the function of forming events 2018-02-22 12:37:59 +04:00
5cdd8de9e3 Small code refactoring 2018-02-20 15:40:50 +04:00
b6ddc8bd40 Some variables have been removed 2018-02-20 15:17:27 +04:00
f10786bfb0 Update function 'local_webhooks_create_record' 2018-02-20 14:38:36 +04:00
899b129d23 The extension is added to the event handler 2018-02-19 15:26:27 +04:00
dd19065313 The extension is added to external functions 2018-02-19 15:25:49 +04:00
3a2bddeea4 The search function is expanded 2018-02-19 15:24:34 +04:00
a41dbbb950 Transferring internal functions to the internal library 2018-02-19 13:44:11 +04:00
a68882140b An external service search function is declared 2018-02-19 13:38:27 +04:00
43f0bf98d2 Added external functions to find services 2018-02-19 13:37:27 +04:00
95c4cf061a Rename the function to reset the cache 2018-02-19 13:15:14 +04:00
a1099245b5 Cache description added 2018-02-19 13:06:06 +04:00
66b5928e6e Added clearing the cache when changing data 2018-02-19 12:36:09 +04:00
4f75d9c5c8 Event handler is rewritten to work with the cache 2018-02-18 22:05:58 +04:00
29bdbc7456 Added a service search function that contains the specified event 2018-02-18 21:37:43 +04:00
ebf76795da Change the location of the functions for working with the cache 2018-02-18 21:28:46 +04:00
53c49b69de Added functions for working with the cache 2018-02-18 20:01:55 +04:00
f392fd42d9 Update the PHP version for testing. 2018-02-18 18:05:33 +04:00
67f8d73a15 Update versions of Moodle for testing. 2018-02-18 18:02:23 +04:00
22b7569f71 Correct the check for 'sesskey'. 2018-02-18 17:58:59 +04:00
fa74622268 Links to the editor contain sesskey 2018-01-04 15:38:55 +04:00
9b7426fb20 Check the sesskey on the editor page 2018-01-04 15:35:27 +04:00
283c64f62e Check the sesskey on the recovery page 2018-01-04 15:34:51 +04:00
14 changed files with 394 additions and 194 deletions

17
.github/workflows/main.yml vendored Normal file
View File

@ -0,0 +1,17 @@
name: Workflow
on: [push]
jobs:
mirror:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: yesolutions/mirror-action@master
with:
REMOTE: 'https://git.popov.link/moodle/local_webhooks.git'
GIT_USERNAME: ${{ secrets.GIT_USERNAME }}
GIT_PASSWORD: ${{ secrets.GIT_PASSWORD }}

View File

@ -6,25 +6,15 @@ os: "linux"
php:
- 5.6
- 7.1
- 7.2
env:
- DB=pgsql MOODLE_BRANCH=MOODLE_26_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_27_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_28_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_29_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_30_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_31_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_32_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_33_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_26_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_27_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_28_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_29_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_30_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_31_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_34_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_32_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_33_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_34_STABLE
before_install:
- cd ../..
@ -51,9 +41,9 @@ jobs:
stage: deploy
env:
- DB=mysqli MOODLE_BRANCH=MOODLE_33_STABLE
- DB=mysqli MOODLE_BRANCH=MOODLE_34_STABLE
php: 7.1
php: 7.2
before_deploy:
- cd $TRAVIS_REPO_SLUG

View File

@ -1,8 +1,5 @@
# Moodle WebHook's
[![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)
[![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/moodle-webhooks)
Adding Webhooks to Moodle.
The Webhooks feature allows applications to receive real-time notification of changes to certain data.
@ -32,4 +29,4 @@ Features:
[GNU GPLv3](LICENSE.txt).
Copyright (c)
[Valentin Popov](mailto:info@valentineus.link).
[Valentin Popov](mailto:info@valentineus.link).

View File

@ -26,6 +26,7 @@ namespace local_webhooks;
defined("MOODLE_INTERNAL") || die();
require_once(__DIR__ . "/../locallib.php");
require_once(__DIR__ . "/../lib.php");
/**
@ -43,12 +44,13 @@ class handler {
public static function events($event) {
$data = $event->get_data();
if ($callbacks = local_webhooks_get_list_records()) {
foreach ($callbacks as $callback) {
if (boolval($callback->enable) && !empty($callback->events[$data["eventname"]])) {
local_webhooks_send_request($data, $callback);
}
}
if (!is_array($recordlist = local_webhooks_cache_get($data["eventname"]))) {
$recordlist = local_webhooks_search_services_by_event($data["eventname"], true);
local_webhooks_cache_set($data["eventname"], $recordlist);
}
foreach ($recordlist as $record) {
local_webhooks_send_request($data, $record);
}
}
}

View File

@ -110,7 +110,7 @@ class local_webhooks_table extends table_sql {
$hideshowlink = new moodle_url(self::$manager, array("hideshowid" => $row->id, "sesskey" => sesskey()));
$hideshowitem = $OUTPUT->action_icon($hideshowlink, new pix_icon($hideshowicon, $hideshowstring));
$editlink = new moodle_url(self::$editor, array("serviceid" => $row->id));
$editlink = new moodle_url(self::$editor, array("serviceid" => $row->id, "sesskey" => sesskey()));
$edititem = $OUTPUT->action_icon($editlink, new pix_icon("t/edit", new lang_string("edit", "moodle")));
$deletelink = new moodle_url(self::$manager, array("deleteid" => $row->id, "sesskey" => sesskey()));
@ -138,7 +138,7 @@ class local_webhooks_table extends table_sql {
* @return string Displayed data.
*/
public function col_title($row) {
$link = new moodle_url(self::$editor, array("serviceid" => $row->id));
$link = new moodle_url(self::$editor, array("serviceid" => $row->id, "sesskey" => sesskey()));
$html = html_writer::link($link, $row->title);
return $html;
}

31
db/caches.php Normal file
View File

@ -0,0 +1,31 @@
<?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/>.
/**
* Definition of the plug-in cache.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$definitions = array(
"webhooks_services" => array(
"mode" => cache_store::MODE_APPLICATION,
"requiredataguarantee" => true,
"staticacceleration" => true
)
);

View File

@ -33,6 +33,14 @@ $functions = array(
"type" => "write"
),
"local_webhooks_search_services_by_event" => array(
"classname" => "local_webhooks_external",
"methodname" => "search_services_by_event",
"classpath" => "local/webhooks/externallib.php",
"description" => "Search for services that contain the specified event.",
"type" => "read"
),
"local_webhooks_get_record" => array(
"classname" => "local_webhooks_external",
"methodname" => "get_record",

View File

@ -56,7 +56,7 @@ if ($editing = boolval($serviceid)) {
}
/* Processing of received data */
if ($data = $mform->get_data()) {
if (($data = $mform->get_data()) && confirm_sesskey()) {
if ($editing) {
$data->id = $serviceid;
local_webhooks_update_record($data);

View File

@ -35,6 +35,68 @@ require_once($CFG->libdir . "/externallib.php");
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class local_webhooks_external extends external_api {
/**
* Single template output parameters.
*
* @return external_description
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
private static function structure_record_parameters() {
return new external_single_structure(
array(
"id" => new external_value(PARAM_INT, "Service ID."),
"enable" => new external_value(PARAM_BOOL, "Enable or disable the service."),
"title" => new external_value(PARAM_TEXT, "Name of the service."),
"url" => new external_value(PARAM_URL, "Web address of the service."),
"type" => new external_value(PARAM_TEXT, "Used header type."),
"token" => new external_value(PARAM_RAW, "Authorization key.", VALUE_OPTIONAL),
"other" => new external_value(PARAM_RAW, "Additional field.", VALUE_OPTIONAL),
"events" => new external_multiple_structure(
new external_single_structure(
array(
"name" => new external_value(PARAM_TEXT, "The name of the event."),
"value" => new external_value(PARAM_BOOL, "Enabled or disabled handler.")
)
), "List of observed events.", VALUE_OPTIONAL
)
), "Information about the service."
);
}
/**
* Formation of the final list.
*
* @param array $listrecords
* @return array
*/
private static function formation_list($listrecords) {
$result = array();
foreach ($listrecords as $index => $record) {
$result[$index] = (array) $record;
$result[$index]["events"] = self::formation_events($record->events);
}
return $result;
}
/**
* Formation of the final list of events.
*
* @param array $listevents
* @return array
*/
private static function formation_events($listevents) {
$result = array();
foreach ($listevents as $key => $value) {
$result[] = array("name" => $key, "value" => $value);
}
return $result;
}
/**
* Returns description of method parameters.
*
@ -45,7 +107,7 @@ class local_webhooks_external extends external_api {
public static function change_status_parameters() {
return new external_function_parameters(
array(
"serviceid" => new external_value(PARAM_INT, "Service identifier.")
"serviceid" => new external_value(PARAM_INT, "Service ID.")
)
);
}
@ -58,14 +120,13 @@ class local_webhooks_external extends external_api {
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function change_status($serviceid = 0) {
public static function change_status($serviceid) {
$parameters = self::validate_parameters(self::change_status_parameters(), array("serviceid" => $serviceid));
$context = context_system::instance();
self::validate_context($context);
$result = local_webhooks_change_status($parameters["serviceid"]);
return $result;
return local_webhooks_change_status($parameters["serviceid"]);
}
/**
@ -75,7 +136,59 @@ class local_webhooks_external extends external_api {
* @since Moodle 2.2
*/
public static function change_status_returns() {
return new external_value(PARAM_BOOL, "The result of the operation.");
return new external_value(PARAM_BOOL, "Result of execution.");
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function search_services_by_event_parameters() {
return new external_function_parameters(
array(
"eventname" => new external_value(PARAM_TEXT, "The name of the event."),
"active" => new external_value(PARAM_BOOL, "Filter show active or all services.")
)
);
}
/**
* Search for services that contain the specified event.
*
* @param string $eventname
* @param boolean $active
* @return array
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function search_services_by_event($eventname, $active) {
$parameters = self::validate_parameters(self::search_services_by_event_parameters(), array("eventname" => $eventname, "active" => $active));
$context = context_system::instance();
self::validate_context($context);
$result = array();
if ($listrecords = local_webhooks_search_services_by_event($parameters["eventname"], $parameters["active"])) {
$result = self::formation_list($listrecords);
}
return $result;
}
/**
* Returns description of method result value.
*
* @return external_description
* @since Moodle 2.2
*/
public static function search_services_by_event_returns() {
return new external_multiple_structure(
self::structure_record_parameters(), "List of services."
);
}
/**
@ -88,7 +201,7 @@ class local_webhooks_external extends external_api {
public static function get_record_parameters() {
return new external_function_parameters(
array(
"serviceid" => new external_value(PARAM_INT, "Service identifier.")
"serviceid" => new external_value(PARAM_INT, "Service ID.")
)
);
}
@ -101,29 +214,20 @@ class local_webhooks_external extends external_api {
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function get_record($serviceid = 0) {
public static function get_record($serviceid) {
$parameters = self::validate_parameters(self::get_record_parameters(), array("serviceid" => $serviceid));
$context = context_system::instance();
self::validate_context($context);
$service = array();
if ($record = local_webhooks_get_record($parameters["serviceid"])) {
$service["enable"] = $record->enable;
$service["id"] = $record->id;
$service["other"] = $record->other;
$service["title"] = $record->title;
$service["token"] = $record->token;
$service["type"] = $record->type;
$service["url"] = $record->url;
$result = array();
$service["events"] = array();
foreach ($record->events as $key => $value) {
$service["events"][] = array("name" => $key, "value" => $value);
}
if ($record = local_webhooks_get_record($parameters["serviceid"])) {
$result = (array) $record;
$result["events"] = self::formation_events($record->events);
}
return $service;
return $result;
}
/**
@ -133,25 +237,7 @@ class local_webhooks_external extends external_api {
* @since Moodle 2.2
*/
public static function get_record_returns() {
return new external_single_structure(
array(
"id" => new external_value(PARAM_INT, "Service identifier."),
"enable" => new external_value(PARAM_INT, "Service status."),
"title" => new external_value(PARAM_TEXT, "Name of the service."),
"url" => new external_value(PARAM_URL, "URL address."),
"type" => new external_value(PARAM_TEXT, "Header type."),
"token" => new external_value(PARAM_TEXT, "Authorization key.", VALUE_OPTIONAL),
"other" => new external_value(PARAM_TEXT, "Additional field.", VALUE_OPTIONAL),
"events" => new external_multiple_structure(
new external_single_structure(
array(
"name" => new external_value(PARAM_TEXT, "Event name."),
"value" => new external_value(PARAM_INT, "Observation status.")
)
), "Observed events.", VALUE_OPTIONAL
)
), "Record about the service."
);
return self::structure_record_parameters();
}
/**
@ -177,21 +263,9 @@ class local_webhooks_external extends external_api {
self::validate_context($context);
$result = array();
if ($listrecords = local_webhooks_get_list_records()) {
foreach ($listrecords as $index => $record) {
$result[$index]["enable"] = $record->enable;
$result[$index]["id"] = $record->id;
$result[$index]["other"] = $record->other;
$result[$index]["title"] = $record->title;
$result[$index]["token"] = $record->token;
$result[$index]["type"] = $record->type;
$result[$index]["url"] = $record->url;
$result[$index]["events"] = array();
foreach ($record->events as $key => $value) {
$result[$index]["events"][] = array("name" => $key, "value" => $value);
}
}
if ($listrecords = local_webhooks_get_list_records()) {
$result = self::formation_list($listrecords);
}
return $result;
@ -205,25 +279,7 @@ class local_webhooks_external extends external_api {
*/
public static function get_list_records_returns() {
return new external_multiple_structure(
new external_single_structure(
array(
"id" => new external_value(PARAM_INT, "Service identifier."),
"enable" => new external_value(PARAM_INT, "Service status."),
"title" => new external_value(PARAM_TEXT, "Name of the service."),
"url" => new external_value(PARAM_URL, "URL address."),
"type" => new external_value(PARAM_TEXT, "Header type."),
"token" => new external_value(PARAM_TEXT, "Authorization key.", VALUE_OPTIONAL),
"other" => new external_value(PARAM_TEXT, "Additional field.", VALUE_OPTIONAL),
"events" => new external_multiple_structure(
new external_single_structure(
array(
"name" => new external_value(PARAM_TEXT, "Event name."),
"value" => new external_value(PARAM_INT, "Observation status.")
)
), "Observed events.", VALUE_OPTIONAL
)
), "Record about the service."
), "List of services."
self::structure_record_parameters(), "List of services."
);
}
@ -250,6 +306,7 @@ class local_webhooks_external extends external_api {
self::validate_context($context);
$result = array();
if ($eventlist = local_webhooks_get_list_events()) {
foreach ($eventlist as $item) {
$result[] = $item["eventname"];
@ -267,7 +324,7 @@ class local_webhooks_external extends external_api {
*/
public static function get_list_events_returns() {
return new external_multiple_structure(
new external_value(PARAM_TEXT, "Event name.")
new external_value(PARAM_TEXT, "The name of the event.")
);
}
@ -283,21 +340,21 @@ class local_webhooks_external extends external_api {
array(
"service" => new external_single_structure(
array(
"enable" => new external_value(PARAM_BOOL, "Service status.", VALUE_OPTIONAL),
"enable" => new external_value(PARAM_BOOL, "Enable or disable the service.", VALUE_OPTIONAL),
"title" => new external_value(PARAM_TEXT, "Name of the service."),
"url" => new external_value(PARAM_URL, "URL address."),
"type" => new external_value(PARAM_TEXT, "Header type.", VALUE_OPTIONAL),
"token" => new external_value(PARAM_TEXT, "Authorization key.", VALUE_OPTIONAL),
"other" => new external_value(PARAM_TEXT, "Additional field.", VALUE_OPTIONAL),
"url" => new external_value(PARAM_URL, "Web address of the service."),
"type" => new external_value(PARAM_TEXT, "Used header type.", VALUE_OPTIONAL),
"token" => new external_value(PARAM_RAW, "Authorization key.", VALUE_OPTIONAL),
"other" => new external_value(PARAM_RAW, "Additional field.", VALUE_OPTIONAL),
"events" => new external_multiple_structure(
new external_single_structure(
array(
"name" => new external_value(PARAM_TEXT, "Event name."),
"value" => new external_value(PARAM_INT, "Observation status.")
"name" => new external_value(PARAM_TEXT, "The name of the event."),
"value" => new external_value(PARAM_BOOL, "Enabled or disabled handler.")
)
), "Observed events.", VALUE_OPTIONAL
), "List of observed events.", VALUE_OPTIONAL
)
), "Record about the service."
), "Information about the service."
)
)
);
@ -306,33 +363,32 @@ class local_webhooks_external extends external_api {
/**
* Create an entry in the database.
*
* @param array $service
* @return number
* @param array $service
* @return boolean
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function create_record($service = array()) {
public static function create_record($service) {
$parameters = self::validate_parameters(self::create_record_parameters(), array("service" => $service));
$context = context_system::instance();
self::validate_context($context);
$record = new stdClass();
$record->other = $parameters["service"]["other"];
$record->title = $parameters["service"]["title"];
$record->token = $parameters["service"]["token"];
$record->url = $parameters["service"]["url"];
$record->other = $parameters["service"]["other"];
$record->title = $parameters["service"]["title"];
$record->token = $parameters["service"]["token"];
$record->url = $parameters["service"]["url"];
$record->events = array();
$record->enable = !empty($parameters["service"]["enable"]) ? $parameters["service"]["enable"] : false;
$record->type = !empty($parameters["service"]["type"]) ? $parameters["service"]["type"] : "json";
$record->events = array();
foreach ($parameters["service"]["events"] as $value) {
$record->events[$value["name"]] = $value["value"];
}
$result = local_webhooks_create_record($record);
return $result;
return local_webhooks_create_record($record);
}
/**
@ -342,7 +398,7 @@ class local_webhooks_external extends external_api {
* @since Moodle 2.2
*/
public static function create_record_returns() {
return new external_value(PARAM_INT, "Service identifier.");
return new external_value(PARAM_BOOL, "Result of execution.");
}
/**
@ -355,24 +411,24 @@ class local_webhooks_external extends external_api {
public static function update_record_parameters() {
return new external_function_parameters(
array(
"id" => new external_value(PARAM_INT, "Service identifier."),
"service" => new external_single_structure(
array(
"enable" => new external_value(PARAM_BOOL, "Service status.", VALUE_OPTIONAL),
"id" => new external_value(PARAM_INT, "Service ID."),
"enable" => new external_value(PARAM_BOOL, "Enable or disable the service.", VALUE_OPTIONAL),
"title" => new external_value(PARAM_TEXT, "Name of the service.", VALUE_OPTIONAL),
"url" => new external_value(PARAM_URL, "URL address.", VALUE_OPTIONAL),
"type" => new external_value(PARAM_TEXT, "Header type.", VALUE_OPTIONAL),
"token" => new external_value(PARAM_TEXT, "Authorization key.", VALUE_OPTIONAL),
"other" => new external_value(PARAM_TEXT, "Additional field.", VALUE_OPTIONAL),
"url" => new external_value(PARAM_URL, "Web address of the service.", VALUE_OPTIONAL),
"type" => new external_value(PARAM_TEXT, "Used header type.", VALUE_OPTIONAL),
"token" => new external_value(PARAM_RAW, "Authorization key.", VALUE_OPTIONAL),
"other" => new external_value(PARAM_RAW, "Additional field.", VALUE_OPTIONAL),
"events" => new external_multiple_structure(
new external_single_structure(
array(
"name" => new external_value(PARAM_TEXT, "Event name."),
"value" => new external_value(PARAM_INT, "Observation status.")
"name" => new external_value(PARAM_TEXT, "The name of the event."),
"value" => new external_value(PARAM_BOOL, "Enabled or disabled handler.")
)
), "Observed events.", VALUE_OPTIONAL
), "List of observed events.", VALUE_OPTIONAL
)
), "Record about the service."
), "Information about the service."
)
)
);
@ -381,20 +437,20 @@ class local_webhooks_external extends external_api {
/**
* Update the record in the database.
*
* @param number $serviceid
* @param array $service
* @return boolean
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function update_record($serviceid = 0, $service = array()) {
$parameters = self::validate_parameters(self::update_record_parameters(), array("id" => $serviceid, "service" => $service));
public static function update_record($service) {
$parameters = self::validate_parameters(self::update_record_parameters(), array("service" => $service));
$context = context_system::instance();
self::validate_context($context);
$result = false;
if ($record = local_webhooks_get_record($parameters["id"])) {
if ($record = local_webhooks_get_record($parameters["service"]["id"])) {
$record->enable = !empty($parameters["service"]["enable"]) ? $parameters["service"]["enable"] : $record->enable;
$record->other = !empty($parameters["service"]["other"]) ? $parameters["service"]["other"] : $record->other;
$record->title = !empty($parameters["service"]["title"]) ? $parameters["service"]["title"] : $record->title;
@ -404,6 +460,7 @@ class local_webhooks_external extends external_api {
if (!empty($parameters["service"]["events"])) {
$record->events = array();
foreach ($parameters["service"]["events"] as $value) {
$record->events[$value["name"]] = $value["value"];
}
@ -419,10 +476,10 @@ class local_webhooks_external extends external_api {
* Returns description of method result value.
*
* @return external_description
* @since Moodle 2.2
* @since Moodle 2.2
*/
public static function update_record_returns() {
return new external_value(PARAM_BOOL, "The result of the operation.");
return new external_value(PARAM_BOOL, "Result of execution.");
}
/**
@ -435,7 +492,7 @@ class local_webhooks_external extends external_api {
public static function delete_record_parameters() {
return new external_function_parameters(
array(
"serviceid" => new external_value(PARAM_INT, "Service identifier.")
"serviceid" => new external_value(PARAM_INT, "Service ID.")
)
);
}
@ -448,14 +505,13 @@ class local_webhooks_external extends external_api {
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function delete_record($serviceid = 0) {
public static function delete_record($serviceid) {
$parameters = self::validate_parameters(self::delete_record_parameters(), array("serviceid" => $serviceid));
$context = context_system::instance();
self::validate_context($context);
$result = local_webhooks_delete_record($parameters["serviceid"]);
return $result;
return local_webhooks_delete_record($parameters["serviceid"]);
}
/**
@ -465,7 +521,7 @@ class local_webhooks_external extends external_api {
* @since Moodle 2.2
*/
public static function delete_record_returns() {
return new external_value(PARAM_BOOL, "The result of the operation.");
return new external_value(PARAM_BOOL, "Result of execution.");
}
/**
@ -490,8 +546,7 @@ class local_webhooks_external extends external_api {
$context = context_system::instance();
self::validate_context($context);
$result = local_webhooks_delete_all_records();
return $result;
return local_webhooks_delete_all_records();
}
/**
@ -501,7 +556,7 @@ class local_webhooks_external extends external_api {
* @since Moodle 2.2
*/
public static function delete_all_records_returns() {
return new external_value(PARAM_BOOL, "The result of the operation.");
return new external_value(PARAM_BOOL, "Result of execution.");
}
/**
@ -526,8 +581,7 @@ class local_webhooks_external extends external_api {
$context = context_system::instance();
self::validate_context($context);
$result = local_webhooks_create_backup();
return $result;
return local_webhooks_create_backup();
}
/**
@ -537,7 +591,7 @@ class local_webhooks_external extends external_api {
* @since Moodle 2.2
*/
public static function create_backup_returns() {
return new external_value(PARAM_TEXT, "Backup copy.");
return new external_value(PARAM_RAW, "Backup copy.");
}
/**
@ -552,8 +606,8 @@ class local_webhooks_external extends external_api {
array(
"options" => new external_single_structure(
array(
"backup" => new external_value(PARAM_TEXT, "Backup copy."),
"deleterecords" => new external_value(PARAM_BOOL, "Delete existing records.", VALUE_OPTIONAL)
"backup" => new external_value(PARAM_RAW, "Backup copy."),
"deleterecords" => new external_value(PARAM_BOOL, "Delete or leave all records.", VALUE_OPTIONAL)
)
)
)
@ -563,17 +617,17 @@ class local_webhooks_external extends external_api {
/**
* Restore from a backup.
*
* @param array $options
* @param array $options
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function restore_backup($options = array()) {
public static function restore_backup($options) {
$parameters = self::validate_parameters(self::restore_backup_parameters(), array("options" => $options));
$context = context_system::instance();
self::validate_context($context);
$deleterecords = !empty($parameters["options"]["deleterecords"]) ? boolval($parameters["options"]["deleterecords"]) : false;
$deleterecords = !empty($parameters["options"]["deleterecords"]) ? $parameters["options"]["deleterecords"] : false;
local_webhooks_restore_backup($parameters["options"]["backup"], $deleterecords);
}
@ -586,4 +640,4 @@ class local_webhooks_external extends external_api {
public static function restore_backup_returns() {
return null;
}
}
}

View File

@ -76,7 +76,7 @@ $PAGE->set_title($titlepage);
echo $OUTPUT->header();
/* Adds the add button */
$addserviceurl = new moodle_url($editservice);
$addserviceurl = new moodle_url($editservice, array("sesskey" => sesskey()));
echo $OUTPUT->single_button($addserviceurl, new lang_string("add", "moodle"));
/* Adds a delete button */

92
lib.php
View File

@ -44,6 +44,33 @@ function local_webhooks_change_status($serviceid) {
return $result;
}
/**
* Search for services that contain the specified event.
*
* @param string $eventname
* @param boolean $active
* @return array
*/
function local_webhooks_search_services_by_event($eventname, $active = false) {
$recordlist = local_webhooks_get_list_records();
$active = boolval($active);
$result = array();
foreach ($recordlist as $record) {
if (!empty($record->events[$eventname])) {
if ($active && boolval($record->enable)) {
$result[] = $record;
}
if (!$active) {
$result[] = $record;
}
}
}
return $result;
}
/**
* Get the record from the database.
*
@ -89,15 +116,14 @@ function local_webhooks_get_list_records($limitfrom = 0, $limitnum = 0) {
* @return array
*/
function local_webhooks_get_list_events() {
$eventlist = report_eventlist_list_generator::get_all_events_list(true);
return $eventlist;
return report_eventlist_list_generator::get_all_events_list(true);
}
/**
* Create an entry in the database.
*
* @param object $record
* @return number
* @param object $record
* @return boolean
*/
function local_webhooks_create_record($record) {
global $DB;
@ -107,8 +133,14 @@ function local_webhooks_create_record($record) {
}
$result = $DB->insert_record("local_webhooks_service", $record, true, false);
/* Clear the plugin cache */
local_webhooks_cache_reset();
/* Event notification */
local_webhooks_events::service_added($result);
return $result;
return boolval($result);
}
/**
@ -126,7 +158,13 @@ function local_webhooks_update_record($record) {
$record->events = !empty($record->events) ? local_webhooks_serialization_data($record->events) : null;
$result = $DB->update_record("local_webhooks_service", $record, false);
/* Clear the plugin cache */
local_webhooks_cache_reset();
/* Event notification */
local_webhooks_events::service_updated($record->id);
return boolval($result);
}
@ -138,8 +176,15 @@ function local_webhooks_update_record($record) {
*/
function local_webhooks_delete_record($serviceid) {
global $DB;
$result = $DB->delete_records("local_webhooks_service", array("id" => $serviceid));
/* Clear the plugin cache */
local_webhooks_cache_reset();
/* Event notification */
local_webhooks_events::service_deleted($serviceid);
return boolval($result);
}
@ -150,8 +195,15 @@ function local_webhooks_delete_record($serviceid) {
*/
function local_webhooks_delete_all_records() {
global $DB;
$result = $DB->delete_records("local_webhooks_service", null);
/* Clear the plugin cache */
local_webhooks_cache_reset();
/* Event notification */
local_webhooks_events::service_deletedall();
return boolval($result);
}
@ -163,7 +215,10 @@ function local_webhooks_delete_all_records() {
function local_webhooks_create_backup() {
$listrecords = local_webhooks_get_list_records();
$result = local_webhooks_serialization_data($listrecords);
/* Event notification */
local_webhooks_events::backup_performed();
return $result;
}
@ -183,6 +238,7 @@ function local_webhooks_restore_backup($data, $deleterecords = false) {
local_webhooks_create_record($servicerecord);
}
/* Event notification */
local_webhooks_events::backup_restored();
}
@ -203,30 +259,10 @@ function local_webhooks_send_request($event, $callback) {
$curl = new curl();
$curl->setHeader(array("Content-Type: application/" . $callback->type));
$curl->post($callback->url, json_encode($event));
$response = $curl->getResponse();
/* Event notification */
local_webhooks_events::response_answer($callback->id, $response);
return $response;
}
/**
* Data serialization.
*
* @param array|object $data
* @return string
*/
function local_webhooks_serialization_data($data) {
$result = serialize($data);
return $result;
}
/**
* Data deserialization.
*
* @param string $data
* @return array|object
*/
function local_webhooks_deserialization_data($data) {
$result = unserialize($data);
return $result;
}

View File

@ -24,6 +24,70 @@
defined("MOODLE_INTERNAL") || die();
/**
* Get data from the cache by key.
*
* @param string $eventname
* @return array
*/
function local_webhooks_cache_get($eventname) {
$cache = cache::make("local_webhooks", "webhooks_services");
return $cache->get($eventname);
}
/**
* Update the data in the cache by key.
*
* @param string $eventname
* @param array $recordlist
* @return boolean
*/
function local_webhooks_cache_set($eventname, $recordlist = array()) {
$cache = cache::make("local_webhooks", "webhooks_services");
return $cache->set($eventname, $recordlist);
}
/**
* Delete the data in the cache by key.
*
* @param string $eventname
* @return boolean
*/
function local_webhooks_cache_delete($eventname) {
$cache = cache::make("local_webhooks", "webhooks_services");
return $cache->delete($eventname);
}
/**
* Clear the cache of the plugin.
*
* @return boolean
*/
function local_webhooks_cache_reset() {
$cache = cache::make("local_webhooks", "webhooks_services");
return $cache->purge();
}
/**
* Data serialization.
*
* @param array|object $data
* @return string
*/
function local_webhooks_serialization_data($data) {
return serialize($data);
}
/**
* Data deserialization.
*
* @param string $data
* @return array|object
*/
function local_webhooks_deserialization_data($data) {
return unserialize($data);
}
/**
* Description of functions of the call of events
*
@ -36,7 +100,7 @@ class local_webhooks_events {
*/
public static function backup_performed() {
$context = context_system::instance();
$event = local_webhooks\event\backup_performed::create(array("context" => $context, "objectid" => 0));
$event = local_webhooks\event\backup_performed::create(array("context" => $context, "objectid" => 0));
$event->trigger();
}
@ -45,7 +109,7 @@ class local_webhooks_events {
*/
public static function backup_restored() {
$context = context_system::instance();
$event = local_webhooks\event\backup_restored::create(array("context" => $context, "objectid" => 0));
$event = local_webhooks\event\backup_restored::create(array("context" => $context, "objectid" => 0));
$event->trigger();
}
@ -57,7 +121,8 @@ class local_webhooks_events {
*/
public static function response_answer($objectid = 0, $response = array()) {
$context = context_system::instance();
$status = "Error sending request";
$status = "Error sending request";
if (!empty($response["HTTP/1.1"])) {
$status = $response["HTTP/1.1"];
}
@ -73,7 +138,7 @@ class local_webhooks_events {
*/
public static function service_added($objectid = 0) {
$context = context_system::instance();
$event = local_webhooks\event\service_added::create(array("context" => $context, "objectid" => $objectid));
$event = local_webhooks\event\service_added::create(array("context" => $context, "objectid" => $objectid));
$event->trigger();
}
@ -84,7 +149,7 @@ class local_webhooks_events {
*/
public static function service_deleted($objectid = 0) {
$context = context_system::instance();
$event = local_webhooks\event\service_deleted::create(array("context" => $context, "objectid" => $objectid));
$event = local_webhooks\event\service_deleted::create(array("context" => $context, "objectid" => $objectid));
$event->trigger();
}
@ -93,7 +158,7 @@ class local_webhooks_events {
*/
public static function service_deletedall() {
$context = context_system::instance();
$event = local_webhooks\event\service_deletedall::create(array("context" => $context, "objectid" => 0));
$event = local_webhooks\event\service_deletedall::create(array("context" => $context, "objectid" => 0));
$event->trigger();
}
@ -104,7 +169,7 @@ class local_webhooks_events {
*/
public static function service_updated($objectid = 0) {
$context = context_system::instance();
$event = local_webhooks\event\service_updated::create(array("context" => $context, "objectid" => $objectid));
$event = local_webhooks\event\service_updated::create(array("context" => $context, "objectid" => $objectid));
$event->trigger();
}
}

View File

@ -45,7 +45,7 @@ if ($mform->is_cancelled()) {
}
/* Processing the received file */
if ($data = $mform->get_data()) {
if (($data = $mform->get_data()) && confirm_sesskey()) {
$content = $mform->get_file_content("backupfile");
local_webhooks_restore_backup($content, $data->deleterecords);
redirect($managerservice, new lang_string("restorefinished", "moodle"));

View File

@ -24,8 +24,8 @@
defined("MOODLE_INTERNAL") || die();
$plugin->release = "4.0.0-rc.1 (Build: 2017122900)";
$plugin->version = 2017122900;
$plugin->release = "4.0.0-rc.2 (Build: 2018022500)";
$plugin->version = 2018022500;
$plugin->requires = 2016112900;
$plugin->component = "local_webhooks";
$plugin->maturity = MATURITY_RC;