2017-11-23 17:17:08 +04:00
|
|
|
<?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/>.
|
|
|
|
|
|
|
|
/**
|
2017-12-21 09:59:14 +04:00
|
|
|
* This file contains the functions used by the plugin.
|
2017-11-23 17:17:08 +04:00
|
|
|
*
|
|
|
|
* @package local_webhooks
|
|
|
|
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined("MOODLE_INTERNAL") || die();
|
|
|
|
|
2018-03-12 22:50:40 +04:00
|
|
|
define("LOCAL_WEBHOOKS_TABLE_SERVICES", "local_webhooks_service");
|
|
|
|
define("LOCAL_WEBHOOKS_TABLE_EVENTS", "local_webhooks_events");
|
2018-03-12 02:20:47 +04:00
|
|
|
|
2017-11-23 21:55:40 +04:00
|
|
|
require_once(__DIR__ . "/locallib.php");
|
|
|
|
|
2017-12-19 14:50:30 +04:00
|
|
|
/**
|
|
|
|
* Change the status of the service.
|
|
|
|
*
|
|
|
|
* @param number $serviceid
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function local_webhooks_change_status($serviceid) {
|
|
|
|
global $DB;
|
|
|
|
|
2018-03-13 01:01:42 +04:00
|
|
|
$status = $DB->get_field(LOCAL_WEBHOOKS_TABLE_SERVICES, "status", array("id" => $serviceid), IGNORE_MISSING);
|
|
|
|
$result = $DB->set_field(LOCAL_WEBHOOKS_TABLE_SERVICES, "status", !boolval($status), array("id" => $serviceid));
|
2017-12-19 14:50:30 +04:00
|
|
|
|
2017-12-21 12:06:20 +04:00
|
|
|
return $result;
|
2017-12-19 14:50:30 +04:00
|
|
|
}
|
|
|
|
|
2018-02-18 21:37:43 +04:00
|
|
|
/**
|
|
|
|
* Search for services that contain the specified event.
|
|
|
|
*
|
2018-02-19 15:05:58 +04:00
|
|
|
* @param string $eventname
|
|
|
|
* @param boolean $active
|
2018-02-18 21:37:43 +04:00
|
|
|
* @return array
|
|
|
|
*/
|
2018-02-19 15:05:58 +04:00
|
|
|
function local_webhooks_search_services_by_event($eventname, $active = false) {
|
2018-02-18 21:37:43 +04:00
|
|
|
$recordlist = local_webhooks_get_list_records();
|
2018-02-19 15:05:58 +04:00
|
|
|
$active = boolval($active);
|
2018-02-19 12:36:09 +04:00
|
|
|
$result = array();
|
2018-02-18 21:37:43 +04:00
|
|
|
|
|
|
|
foreach ($recordlist as $record) {
|
2018-02-19 15:05:58 +04:00
|
|
|
if (!empty($record->events[$eventname])) {
|
|
|
|
if ($active && boolval($record->enable)) {
|
|
|
|
$result[] = $record;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$active) {
|
|
|
|
$result[] = $record;
|
|
|
|
}
|
2018-02-18 21:37:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:11:33 +04:00
|
|
|
/**
|
2017-12-21 09:59:14 +04:00
|
|
|
* Get the record from the database.
|
|
|
|
*
|
|
|
|
* @param number $serviceid
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
function local_webhooks_get_record($serviceid) {
|
|
|
|
global $DB;
|
|
|
|
|
2018-03-13 00:16:08 +04:00
|
|
|
$record = $DB->get_record(LOCAL_WEBHOOKS_TABLE_SERVICES, array("id" => $serviceid), "*", IGNORE_MISSING);
|
|
|
|
$record->events = local_webhooks_get_list_events_for_service($serviceid);
|
2017-12-21 09:59:14 +04:00
|
|
|
|
2018-03-13 00:16:08 +04:00
|
|
|
return $record;
|
2017-12-21 09:59:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all records from the database.
|
2017-11-23 18:11:33 +04:00
|
|
|
*
|
|
|
|
* @param number $limitfrom
|
|
|
|
* @param number $limitnum
|
2018-03-12 14:28:51 +04:00
|
|
|
* @param array $conditions
|
2017-11-23 18:11:33 +04:00
|
|
|
* @return array
|
|
|
|
*/
|
2018-03-13 00:25:55 +04:00
|
|
|
function local_webhooks_get_list_records($limitfrom = 0, $limitnum = 0, $conditions = array()) {
|
2017-11-23 18:11:33 +04:00
|
|
|
global $DB;
|
|
|
|
|
2018-03-13 00:25:55 +04:00
|
|
|
$records = $DB->get_records(LOCAL_WEBHOOKS_TABLE_SERVICES, $conditions, "id", "*", $limitfrom, $limitnum);
|
2017-11-23 18:11:33 +04:00
|
|
|
|
2018-03-13 00:25:55 +04:00
|
|
|
foreach ($records as $record) {
|
|
|
|
$record->events = local_webhooks_get_list_events_for_service($record->id);
|
2017-11-23 18:11:33 +04:00
|
|
|
}
|
|
|
|
|
2018-03-13 00:25:55 +04:00
|
|
|
return $records;
|
2017-11-23 18:11:33 +04:00
|
|
|
}
|
|
|
|
|
2017-12-27 16:01:48 +04:00
|
|
|
/**
|
|
|
|
* Get a list of all system events.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function local_webhooks_get_list_events() {
|
2018-02-20 15:17:27 +04:00
|
|
|
return report_eventlist_list_generator::get_all_events_list(true);
|
2017-12-27 16:01:48 +04:00
|
|
|
}
|
|
|
|
|
2018-03-12 02:03:18 +04:00
|
|
|
/**
|
|
|
|
* Get the total number of records.
|
|
|
|
*
|
|
|
|
* @return number
|
|
|
|
*/
|
|
|
|
function local_webhooks_get_total_count() {
|
|
|
|
global $DB;
|
2018-03-12 03:30:57 +04:00
|
|
|
|
2018-03-12 22:50:40 +04:00
|
|
|
return $DB->count_records(LOCAL_WEBHOOKS_TABLE_SERVICES, array());
|
2018-03-12 02:03:18 +04:00
|
|
|
}
|
|
|
|
|
2017-11-23 17:17:08 +04:00
|
|
|
/**
|
2017-12-21 09:59:14 +04:00
|
|
|
* Create an entry in the database.
|
2017-11-23 17:17:08 +04:00
|
|
|
*
|
2018-03-12 22:50:40 +04:00
|
|
|
* @param object $record
|
|
|
|
* @return number
|
2017-11-23 17:17:08 +04:00
|
|
|
*/
|
2017-12-21 09:59:14 +04:00
|
|
|
function local_webhooks_create_record($record) {
|
2017-11-23 17:17:08 +04:00
|
|
|
global $DB;
|
|
|
|
|
2018-03-12 23:48:00 +04:00
|
|
|
if (empty($record->events)) {
|
|
|
|
$record->events = array();
|
2017-11-23 17:17:08 +04:00
|
|
|
}
|
|
|
|
|
2018-03-12 23:48:00 +04:00
|
|
|
/* Adding entries */
|
|
|
|
$transaction = $DB->start_delegated_transaction();
|
|
|
|
$serviceid = $DB->insert_record(LOCAL_WEBHOOKS_TABLE_SERVICES, $record, true, false);
|
2018-03-13 00:10:56 +04:00
|
|
|
local_webhooks_insert_events_for_service($serviceid, $record->events);
|
2018-03-12 22:50:40 +04:00
|
|
|
$transaction->allow_commit();
|
2018-02-19 12:36:09 +04:00
|
|
|
|
|
|
|
/* Clear the plugin cache */
|
2018-02-19 13:15:14 +04:00
|
|
|
local_webhooks_cache_reset();
|
2018-02-19 12:36:09 +04:00
|
|
|
|
|
|
|
/* Event notification */
|
2017-12-21 12:06:20 +04:00
|
|
|
local_webhooks_events::service_added($result);
|
2018-02-19 12:36:09 +04:00
|
|
|
|
2018-03-12 22:50:40 +04:00
|
|
|
return $serviceid;
|
2017-11-23 17:17:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-21 09:59:14 +04:00
|
|
|
* Update the record in the database.
|
|
|
|
*
|
2018-03-12 23:45:35 +04:00
|
|
|
* @param object $record
|
2017-12-21 09:59:14 +04:00
|
|
|
* @return boolean
|
2017-11-23 17:17:08 +04:00
|
|
|
*/
|
2017-12-21 09:59:14 +04:00
|
|
|
function local_webhooks_update_record($record) {
|
2017-11-23 17:17:08 +04:00
|
|
|
global $DB;
|
|
|
|
|
2017-12-27 20:30:27 +04:00
|
|
|
if (empty($record->id)) {
|
|
|
|
print_error("missingparam", "error", null, "id");
|
2017-12-21 09:59:14 +04:00
|
|
|
}
|
|
|
|
|
2018-03-12 23:45:35 +04:00
|
|
|
if (empty($record->events)) {
|
|
|
|
$record->events = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update records */
|
|
|
|
$transaction = $DB->start_delegated_transaction();
|
2018-03-12 22:50:40 +04:00
|
|
|
$result = $DB->update_record(LOCAL_WEBHOOKS_TABLE_SERVICES, $record, false);
|
2018-03-13 00:10:56 +04:00
|
|
|
local_webhooks_insert_events_for_service($record->id, $record->events);
|
2018-03-12 23:45:35 +04:00
|
|
|
$transaction->allow_commit();
|
2018-02-19 12:36:09 +04:00
|
|
|
|
|
|
|
/* Clear the plugin cache */
|
2018-02-19 13:15:14 +04:00
|
|
|
local_webhooks_cache_reset();
|
2018-02-19 12:36:09 +04:00
|
|
|
|
|
|
|
/* Event notification */
|
2017-12-21 12:06:20 +04:00
|
|
|
local_webhooks_events::service_updated($record->id);
|
2018-02-19 12:36:09 +04:00
|
|
|
|
2017-12-21 09:59:14 +04:00
|
|
|
return boolval($result);
|
2017-11-23 17:17:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-21 09:59:14 +04:00
|
|
|
* Delete the record from the database.
|
2017-11-23 17:17:08 +04:00
|
|
|
*
|
2017-12-21 09:59:14 +04:00
|
|
|
* @param number $serviceid
|
|
|
|
* @return boolean
|
2017-11-23 17:17:08 +04:00
|
|
|
*/
|
2017-12-21 09:59:14 +04:00
|
|
|
function local_webhooks_delete_record($serviceid) {
|
2017-11-23 17:17:08 +04:00
|
|
|
global $DB;
|
2018-02-19 12:36:09 +04:00
|
|
|
|
2018-03-12 22:50:40 +04:00
|
|
|
$result = $DB->delete_records(LOCAL_WEBHOOKS_TABLE_SERVICES, array("id" => $serviceid));
|
2018-03-13 00:48:12 +04:00
|
|
|
local_webhooks_delete_events_for_service($serviceid);
|
2018-02-19 12:36:09 +04:00
|
|
|
|
|
|
|
/* Clear the plugin cache */
|
2018-02-19 13:15:14 +04:00
|
|
|
local_webhooks_cache_reset();
|
2018-02-19 12:36:09 +04:00
|
|
|
|
|
|
|
/* Event notification */
|
2017-12-21 12:06:20 +04:00
|
|
|
local_webhooks_events::service_deleted($serviceid);
|
2018-02-19 12:36:09 +04:00
|
|
|
|
2017-12-21 09:59:14 +04:00
|
|
|
return boolval($result);
|
2017-11-23 17:17:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-21 09:59:14 +04:00
|
|
|
* Delete all records from the database.
|
2017-11-23 17:17:08 +04:00
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2017-12-21 09:59:14 +04:00
|
|
|
function local_webhooks_delete_all_records() {
|
2017-11-23 17:17:08 +04:00
|
|
|
global $DB;
|
2018-02-19 12:36:09 +04:00
|
|
|
|
2018-03-12 22:50:40 +04:00
|
|
|
$result = $DB->delete_records(LOCAL_WEBHOOKS_TABLE_SERVICES, null);
|
2018-03-13 00:52:51 +04:00
|
|
|
$DB->delete_records(LOCAL_WEBHOOKS_TABLE_EVENTS, null);
|
2018-02-19 12:36:09 +04:00
|
|
|
|
|
|
|
/* Clear the plugin cache */
|
2018-02-19 13:15:14 +04:00
|
|
|
local_webhooks_cache_reset();
|
2018-02-19 12:36:09 +04:00
|
|
|
|
|
|
|
/* Event notification */
|
2017-12-21 12:06:20 +04:00
|
|
|
local_webhooks_events::service_deletedall();
|
2018-02-19 12:36:09 +04:00
|
|
|
|
2017-11-23 17:17:08 +04:00
|
|
|
return boolval($result);
|
|
|
|
}
|
|
|
|
|
2017-11-23 18:42:47 +04:00
|
|
|
/**
|
2017-12-21 09:59:14 +04:00
|
|
|
* Create a backup.
|
2017-11-23 18:42:47 +04:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function local_webhooks_create_backup() {
|
2017-12-21 09:59:14 +04:00
|
|
|
$listrecords = local_webhooks_get_list_records();
|
|
|
|
$result = local_webhooks_serialization_data($listrecords);
|
2018-02-19 12:36:09 +04:00
|
|
|
|
|
|
|
/* Event notification */
|
2017-12-21 12:06:20 +04:00
|
|
|
local_webhooks_events::backup_performed();
|
2018-02-19 12:36:09 +04:00
|
|
|
|
2017-12-21 09:59:14 +04:00
|
|
|
return $result;
|
2017-11-23 18:42:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-21 09:59:14 +04:00
|
|
|
* Restore from a backup.
|
2017-11-23 18:42:47 +04:00
|
|
|
*
|
2018-03-12 01:45:08 +04:00
|
|
|
* @param string $data
|
|
|
|
* @param boolean $deleterecords
|
2017-11-23 18:42:47 +04:00
|
|
|
*/
|
2017-12-21 09:59:14 +04:00
|
|
|
function local_webhooks_restore_backup($data, $deleterecords = false) {
|
|
|
|
$listrecords = local_webhooks_deserialization_data($data);
|
2017-11-23 18:42:47 +04:00
|
|
|
|
2017-12-21 09:59:14 +04:00
|
|
|
if (boolval($deleterecords)) {
|
|
|
|
local_webhooks_delete_all_records();
|
2017-11-23 18:42:47 +04:00
|
|
|
}
|
2017-11-23 21:55:40 +04:00
|
|
|
|
2017-12-21 09:59:14 +04:00
|
|
|
foreach ($listrecords as $servicerecord) {
|
|
|
|
local_webhooks_create_record($servicerecord);
|
|
|
|
}
|
2017-12-21 12:06:20 +04:00
|
|
|
|
2018-02-19 12:36:09 +04:00
|
|
|
/* Event notification */
|
2017-12-21 12:06:20 +04:00
|
|
|
local_webhooks_events::backup_restored();
|
2017-11-23 18:42:47 +04:00
|
|
|
}
|
|
|
|
|
2017-12-27 20:43:36 +04:00
|
|
|
/**
|
|
|
|
* Send the event remotely to the service.
|
|
|
|
*
|
|
|
|
* @param array $event
|
2018-03-12 01:44:01 +04:00
|
|
|
* @param object $record
|
2017-12-27 20:43:36 +04:00
|
|
|
* @return array
|
|
|
|
*/
|
2018-03-12 01:44:01 +04:00
|
|
|
function local_webhooks_send_request($event, $record) {
|
2017-12-27 20:43:36 +04:00
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$event["host"] = parse_url($CFG->wwwroot)["host"];
|
2018-03-12 01:44:01 +04:00
|
|
|
$event["token"] = $record->token;
|
|
|
|
$event["extra"] = $record->other;
|
2017-12-27 20:43:36 +04:00
|
|
|
|
|
|
|
$curl = new curl();
|
2018-03-12 01:44:01 +04:00
|
|
|
$curl->setHeader(array("Content-Type: application/" . $record->type));
|
|
|
|
$curl->post($record->url, json_encode($event));
|
2017-12-27 20:43:36 +04:00
|
|
|
$response = $curl->getResponse();
|
2018-02-19 12:36:09 +04:00
|
|
|
|
|
|
|
/* Event notification */
|
2018-03-12 01:44:01 +04:00
|
|
|
local_webhooks_events::response_answer($record->id, $response);
|
2018-02-19 12:36:09 +04:00
|
|
|
|
2017-12-27 20:43:36 +04:00
|
|
|
return $response;
|
2018-03-12 22:50:40 +04:00
|
|
|
}
|