Compare commits

..
20 Commits
Author SHA1 Message Date
Valentin Popov a81530d0d7 refactor: remove cross-component runtime dependencies
Moodle Plugin CI / test (push) Successful in 3m20s
2026-08-12 17:53:02 +00:00
Valentin Popov 0d4bf6a208 style: apply Moodle coding standards
Moodle Plugin CI / test (push) Successful in 1m27s
2026-08-12 17:13:30 +00:00
Valentin Popov 91111b66d3 fix(ci): copy plugin into test image
Moodle Plugin CI / test (push) Failing after 1m44s
2026-08-12 17:07:32 +00:00
Valentin Popov d35ce9b445 ci: add Moodle 5.2 Docker checks 2026-08-12 16:57:33 +00:00
Valentin Popov 482d15db8f Bump version
Signed-off-by: Valentin Popov <info@valentineus.link>
2020-05-16 01:59:03 +04:00
Valentin PopovandGitHub 9d70f553bb Merge pull request #19 from catalyst/requires-v3.0.0-stable
Set minimum required version to Moodle 3.7.0
2020-05-16 00:57:28 +04:00
Valentin PopovandGitHub b77e2a2cdb Merge pull request #18 from catalyst/issue15-v3.0.0-stable
Add null privacy provider to fix #15
2020-05-16 00:56:45 +04:00
Dmitrii Metelkin f89cdba127 Set minimum required version to Moodle 3.7.0 2020-04-22 09:21:33 +10:00
Dmitrii Metelkin 1acd3ef21d Add null privacy provider to fix #15 2020-04-22 09:16:57 +10:00
Valentin Popov 3f74278add Bump 3.0.4 version
Signed-off-by: Valentin Popov <info@valentineus.link>
2020-04-14 13:32:14 +04:00
Valentin Popov 4e64c0bcd2 Version fix
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-02-17 03:38:45 +04:00
Valentin Popov 835f5516e8 Correction problem tag display
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-02-17 03:36:52 +04:00
Valentin Popov 833434c5ba Formatting code
Signed-off-by: Valentin Popov <info@valentineus.link>
2019-02-17 03:34:52 +04:00
Valentin Popov 8eda3bc6bb Version update
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-11-02 02:51:29 +04:00
Valentin Popov 9cb5bd0760 Bug fixes
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-11-02 02:38:31 +04:00
Valentin Popov 2f04aeab6d Correction of possible problems
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-11-02 02:06:47 +04:00
Valentin Popov 849d7126d8 Correction of work with event names
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-11-02 02:03:17 +04:00
Valentin Popov d8f3a9f3ed Formatting code and eliminating possible errors
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-11-02 01:07:14 +04:00
Valentin Popov f9772b1fb9 Fix check MOODLE_INTERNAL
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-06-19 22:22:45 +04:00
Valentin Popov 3fa97bad31 Fix error install.xml
Signed-off-by: Valentin Popov <info@valentineus.link>
2018-06-19 19:49:00 +04:00
37 changed files with 994 additions and 1791 deletions
+38
View File
@@ -0,0 +1,38 @@
FROM composer:2.10.2@sha256:4d71c3c2109c61d5415544264b59ad4087e4c5b7244481723664138fd36d5040 AS composer
FROM node:22.23.2-bookworm-slim@sha256:d649c27dae7ba0137b3cef5dd75baa422c08dc3d9e3fc0c23dfb172dc3cc6436 AS node
FROM moodlehq/moodle-php-apache:8.4@sha256:07cac70e0ed3f96f334ffeece92075b98adfbe4110312a3a9308b95eab8e2080
ARG MOODLE_PLUGIN_CI_COMMIT=9276f968746960275b12190b4ef8e47b1e6af674
ARG MOODLE_PLUGIN_CI_LOCK_SHA256=e5c8ce77b90f43a057ee717c52e856bf81d43387834dab36083a1c1b17a24be4
COPY --from=composer /usr/bin/composer /usr/local/bin/composer
COPY --from=node /usr/local/bin/node /usr/local/bin/node
COPY --from=node /usr/local/lib/node_modules /usr/local/lib/node_modules
RUN ln -s /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm \
&& ln -s /usr/local/lib/node_modules/npm/bin/npx-cli.js /usr/local/bin/npx \
&& apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends postgresql-client \
&& rm -rf /var/lib/apt/lists/* \
&& locale-gen en_AU.UTF-8 \
&& git init /opt/moodle-plugin-ci \
&& git -C /opt/moodle-plugin-ci remote add origin https://github.com/moodlehq/moodle-plugin-ci.git \
&& git -C /opt/moodle-plugin-ci fetch --depth 1 origin "${MOODLE_PLUGIN_CI_COMMIT}" \
&& git -C /opt/moodle-plugin-ci checkout --detach FETCH_HEAD \
&& test "$(git -C /opt/moodle-plugin-ci rev-parse HEAD)" = "${MOODLE_PLUGIN_CI_COMMIT}" \
&& echo "${MOODLE_PLUGIN_CI_LOCK_SHA256} /opt/moodle-plugin-ci/composer.lock" | sha256sum --check \
&& COMPOSER_ALLOW_SUPERUSER=1 composer install \
--working-dir=/opt/moodle-plugin-ci \
--no-dev \
--no-interaction \
--prefer-dist \
--classmap-authoritative \
&& ln -s /opt/moodle-plugin-ci/bin/moodle-plugin-ci /usr/local/bin/moodle-plugin-ci
COPY . /plugin
ENV LANG=en_AU.UTF-8
WORKDIR /workspace
+11
View File
@@ -0,0 +1,11 @@
.git
.github
.gitea
.docker
.dockerignore
.env
.env.*
compose.yaml
node_modules
vendor
*.log
+31
View File
@@ -0,0 +1,31 @@
name: Moodle Plugin CI
on:
push:
branches:
- develop
- main
pull_request:
workflow_dispatch:
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
env:
COMPOSE_PROJECT_NAME: local_webhooks_${{ gitea.run_id }}_${{ gitea.run_attempt }}
steps:
- name: Check out repository
uses: https://github.com/actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Build test image
run: docker compose build --pull ci
- name: Test plugin
run: docker compose up --abort-on-container-exit --exit-code-from ci
- name: Clean up
if: always()
run: docker compose down --volumes --remove-orphans
-60
View File
@@ -1,60 +0,0 @@
language: php
sudo: false
os: "linux"
php:
- 5.6
- 7.2
env:
- DB=pgsql MOODLE_BRANCH=MOODLE_32_STABLE
- DB=pgsql MOODLE_BRANCH=MOODLE_33_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 ../..
- composer selfupdate
- composer create-project -n --no-dev moodlerooms/moodle-plugin-ci ci ^1
- export PATH="$(cd ci/bin; pwd):$(cd ci/vendor/bin; pwd):$PATH"
install:
- moodle-plugin-ci install
script:
- moodle-plugin-ci phplint
- moodle-plugin-ci phpcpd
- moodle-plugin-ci phpmd
- moodle-plugin-ci codechecker
- moodle-plugin-ci csslint
- moodle-plugin-ci shifter
- moodle-plugin-ci jshint
- moodle-plugin-ci phpunit
- moodle-plugin-ci behat
jobs:
include:
stage: deploy
env:
- DB=mysqli MOODLE_BRANCH=MOODLE_34_STABLE
php: 7.2
before_deploy:
- cd $TRAVIS_REPO_SLUG
- git stash --all
- /bin/sh build.sh
deploy:
provider: releases
skip_cleanup: true
file: "../local_webhooks.zip"
email: "info@valentineus.link"
api_key: $GITHUB_PROJECT_TOKEN
on:
tags: true
+20 -4
View File
@@ -20,11 +20,27 @@ Features:
* Use a secret phrase to authenticate requests; * Use a secret phrase to authenticate requests;
* [JSON](https://en.wikipedia.org/wiki/JSON) - Format of outgoing data; * [JSON](https://en.wikipedia.org/wiki/JSON) - Format of outgoing data;
## Documentation ## Installation
* [Install the plugin](docs/getting-started.md#installation). Get the installation package in any of the available methods:
* [User guide](docs/getting-started.md#user-guide).
* [Request format](docs/getting-started.md#request-format). * [GitHub Releases](https://github.com/valentineus/moodle-webhooks/releases).
* [Compilation from the source code](#build).
## Build
Self-assembly package is as follows:
* Clone the repository:
```bash
git clone https://github.com/valentineus/moodle-webhooks.git moodle-webhooks
```
* Run the build script:
```bash
cd ./moodle-webhooks
/bin/sh build.sh
```
## License ## License
+20 -9
View File
@@ -1,21 +1,32 @@
#!/bin/sh #!/bin/sh
# Author: Valentin Popov # Author: Valentin Popov
# Email: info@valentineus.link # Email: info@valentineus.link
# Date: 2017-10-19 # Date: 2018-06-19
# Usage: /bin/sh build.sh # Usage: /bin/sh ./build.sh
# Description: Build the final package for installation in Moodle. # Description: Build the final package for installation in Moodle.
# Updating the Environment # Updating the Environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
export PATH="$PATH:/usr/local/scripts" export PATH="$PATH:/usr/local/scripts"
# Build the package # Current project
cd .. PROJECT="local_webhooks"
mv "./moodle-webhooks" "./local_webhooks"
zip -9 -r "local_webhooks.zip" "local_webhooks" \ # Defining directories
-x "local_webhooks/.git*" \ DIRECTORY="$(pwd)"
-x "local_webhooks/.travis.yml" \ NAMEDIR="$(basename $DIRECTORY)"
-x "local_webhooks/build.sh" TMPDIR="$(mktemp --directory)"
# Creating a Temporary Directory
cp --recursive --verbose "../$NAMEDIR" "$TMPDIR/$PROJECT"
mkdir --parents --verbose "$DIRECTORY/build"
cd "$TMPDIR"
# Creating an archive
zip -9 -r "$DIRECTORY/build/$PROJECT.zip" "$PROJECT" \
-x "$PROJECT/.git*" \
-x "$PROJECT/.travis.yml" \
-x "$PROJECT/build*"
# End of work # End of work
exit 0 exit 0
+21 -14
View File
@@ -24,7 +24,8 @@
namespace local_webhooks\event; namespace local_webhooks\event;
defined("MOODLE_INTERNAL") || die(); use core\event\base;
use moodle_url;
/** /**
* Defines how to work with events. * Defines how to work with events.
@@ -32,34 +33,40 @@ defined("MOODLE_INTERNAL") || die();
* @copyright 2017 "Valentin Popov" <info@valentineus.link> * @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @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
*/ */
class backup_performed extends \core\event\base { class backup_performed extends base {
/**
* Init method.
*/
protected function init() {
$this->data["crud"] = "c";
$this->data["edulevel"] = self::LEVEL_OTHER;
$this->data["objecttable"] = "local_webhooks_service";
}
/** /**
* Return localised event name. * Return localised event name.
*
* @throws \coding_exception
*/ */
public static function get_name() { public static function get_name() {
return new \lang_string("create", "moodle"); return get_string('create', 'local_webhooks');
} }
/** /**
* Returns description of what happened. * Returns description of what happened.
*
* @throws \coding_exception
*/ */
public function get_description() { public function get_description() {
return new \lang_string("backup", "moodle"); return get_string('backup', 'local_webhooks');
} }
/** /**
* Get URL related to the action. * Get URL related to the action.
*
* @throws \moodle_exception
*/ */
public function get_url() { public function get_url() {
return new \moodle_url("/local/webhooks/index.php"); return new moodle_url('/local/webhooks/index.php');
}
/**
* Init method.
*/
protected function init() {
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'local_webhooks_service';
} }
} }
+21 -14
View File
@@ -24,7 +24,8 @@
namespace local_webhooks\event; namespace local_webhooks\event;
defined("MOODLE_INTERNAL") || die(); use core\event\base;
use moodle_url;
/** /**
* Defines how to work with events. * Defines how to work with events.
@@ -32,34 +33,40 @@ defined("MOODLE_INTERNAL") || die();
* @copyright 2017 "Valentin Popov" <info@valentineus.link> * @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @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
*/ */
class backup_restored extends \core\event\base { class backup_restored extends base {
/**
* Init method.
*/
protected function init() {
$this->data["crud"] = "u";
$this->data["edulevel"] = self::LEVEL_OTHER;
$this->data["objecttable"] = "local_webhooks_service";
}
/** /**
* Return localised event name. * Return localised event name.
*
* @throws \coding_exception
*/ */
public static function get_name() { public static function get_name() {
return new \lang_string("update", "moodle"); return get_string('update', 'local_webhooks');
} }
/** /**
* Returns description of what happened. * Returns description of what happened.
*
* @throws \coding_exception
*/ */
public function get_description() { public function get_description() {
return new \lang_string("backupfinished", "moodle"); return get_string('backupfinished', 'local_webhooks');
} }
/** /**
* Get URL related to the action. * Get URL related to the action.
*
* @throws \moodle_exception
*/ */
public function get_url() { public function get_url() {
return new \moodle_url("/local/webhooks/index.php"); return new moodle_url('/local/webhooks/index.php');
}
/**
* Init method.
*/
protected function init() {
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'local_webhooks_service';
} }
} }
+19 -14
View File
@@ -24,7 +24,8 @@
namespace local_webhooks\event; namespace local_webhooks\event;
defined("MOODLE_INTERNAL") || die(); use core\event\base;
use moodle_url;
/** /**
* Defines how to work with events. * Defines how to work with events.
@@ -32,34 +33,38 @@ defined("MOODLE_INTERNAL") || die();
* @copyright 2017 "Valentin Popov" <info@valentineus.link> * @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @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
*/ */
class response_answer extends \core\event\base { class response_answer extends base {
/**
* Init method.
*/
protected function init() {
$this->data["crud"] = "r";
$this->data["edulevel"] = self::LEVEL_OTHER;
$this->data["objecttable"] = "local_webhooks_service";
}
/** /**
* Return localised event name. * Return localised event name.
*
* @throws \coding_exception
*/ */
public static function get_name() { public static function get_name() {
return new \lang_string("answer", "moodle"); return get_string('answer', 'local_webhooks');
} }
/** /**
* Returns description of what happened. * Returns description of what happened.
*/ */
public function get_description() { public function get_description() {
return $this->other["status"]; return $this->other['status'];
} }
/** /**
* Get URL related to the action. * Get URL related to the action.
*
* @throws \moodle_exception
*/ */
public function get_url() { public function get_url() {
return new \moodle_url("/local/webhooks/editservice.php", array("serviceid" => $this->objectid)); return new moodle_url('/local/webhooks/editservice.php', ['serviceid' => $this->objectid]);
}
/**
* Init method.
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'local_webhooks_service';
} }
} }
+21 -14
View File
@@ -24,7 +24,8 @@
namespace local_webhooks\event; namespace local_webhooks\event;
defined("MOODLE_INTERNAL") || die(); use core\event\base;
use moodle_url;
/** /**
* Defines how to work with events. * Defines how to work with events.
@@ -32,34 +33,40 @@ defined("MOODLE_INTERNAL") || die();
* @copyright 2017 "Valentin Popov" <info@valentineus.link> * @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @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
*/ */
class service_added extends \core\event\base { class service_added extends base {
/**
* Init method.
*/
protected function init() {
$this->data["crud"] = "c";
$this->data["edulevel"] = self::LEVEL_OTHER;
$this->data["objecttable"] = "local_webhooks_service";
}
/** /**
* Return localised event name. * Return localised event name.
*
* @throws \coding_exception
*/ */
public static function get_name() { public static function get_name() {
return new \lang_string("create", "moodle"); return get_string('create', 'local_webhooks');
} }
/** /**
* Returns description of what happened. * Returns description of what happened.
*
* @throws \coding_exception
*/ */
public function get_description() { public function get_description() {
return new \lang_string("eventwebserviceservicecreated", "webservice"); return get_string('eventwebserviceservicecreated', 'local_webhooks');
} }
/** /**
* Get URL related to the action. * Get URL related to the action.
*
* @throws \moodle_exception
*/ */
public function get_url() { public function get_url() {
return new \moodle_url("/local/webhooks/editservice.php", array("serviceid" => $this->objectid)); return new moodle_url('/local/webhooks/editservice.php', ['serviceid' => $this->objectid]);
}
/**
* Init method.
*/
protected function init() {
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'local_webhooks_service';
} }
} }
+21 -14
View File
@@ -24,7 +24,8 @@
namespace local_webhooks\event; namespace local_webhooks\event;
defined("MOODLE_INTERNAL") || die(); use core\event\base;
use moodle_url;
/** /**
* Defines how to work with events. * Defines how to work with events.
@@ -32,34 +33,40 @@ defined("MOODLE_INTERNAL") || die();
* @copyright 2017 "Valentin Popov" <info@valentineus.link> * @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @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
*/ */
class service_deleted extends \core\event\base { class service_deleted extends base {
/**
* Init method.
*/
protected function init() {
$this->data["crud"] = "d";
$this->data["edulevel"] = self::LEVEL_OTHER;
$this->data["objecttable"] = "local_webhooks_service";
}
/** /**
* Return localised event name. * Return localised event name.
*
* @throws \coding_exception
*/ */
public static function get_name() { public static function get_name() {
return new \lang_string("deleted", "moodle"); return get_string('deleted', 'local_webhooks');
} }
/** /**
* Returns description of what happened. * Returns description of what happened.
*
* @throws \coding_exception
*/ */
public function get_description() { public function get_description() {
return new \lang_string("eventwebserviceservicedeleted", "webservice"); return get_string('eventwebserviceservicedeleted', 'local_webhooks');
} }
/** /**
* Get URL related to the action. * Get URL related to the action.
*
* @throws \moodle_exception
*/ */
public function get_url() { public function get_url() {
return new \moodle_url("/local/webhooks/index.php"); return new moodle_url('/local/webhooks/index.php');
}
/**
* Init method.
*/
protected function init() {
$this->data['crud'] = 'd';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'local_webhooks_service';
} }
} }
-65
View File
@@ -1,65 +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/>.
/**
* 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 service_deletedall extends \core\event\base {
/**
* Init method.
*/
protected function init() {
$this->data["crud"] = "d";
$this->data["edulevel"] = self::LEVEL_OTHER;
$this->data["objecttable"] = "local_webhooks_service";
}
/**
* Return localised event name.
*/
public static function get_name() {
return new \lang_string("deleted", "moodle");
}
/**
* Returns description of what happened.
*/
public function get_description() {
return new \lang_string("deletecompletely", "moodle");
}
/**
* Get URL related to the action.
*/
public function get_url() {
return new \moodle_url("/local/webhooks/index.php");
}
}
+21 -14
View File
@@ -24,7 +24,8 @@
namespace local_webhooks\event; namespace local_webhooks\event;
defined("MOODLE_INTERNAL") || die(); use core\event\base;
use moodle_url;
/** /**
* Defines how to work with events. * Defines how to work with events.
@@ -32,34 +33,40 @@ defined("MOODLE_INTERNAL") || die();
* @copyright 2017 "Valentin Popov" <info@valentineus.link> * @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @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
*/ */
class service_updated extends \core\event\base { class service_updated extends base {
/**
* Init method.
*/
protected function init() {
$this->data["crud"] = "u";
$this->data["edulevel"] = self::LEVEL_OTHER;
$this->data["objecttable"] = "local_webhooks_service";
}
/** /**
* Return localised event name. * Return localised event name.
*
* @throws \coding_exception
*/ */
public static function get_name() { public static function get_name() {
return new \lang_string("update", "moodle"); return get_string('update', 'local_webhooks');
} }
/** /**
* Returns description of what happened. * Returns description of what happened.
*
* @throws \coding_exception
*/ */
public function get_description() { public function get_description() {
return new \lang_string("eventwebserviceserviceupdated", "webservice"); return get_string('eventwebserviceserviceupdated', 'local_webhooks');
} }
/** /**
* Get URL related to the action. * Get URL related to the action.
*
* @throws \moodle_exception
*/ */
public function get_url() { public function get_url() {
return new \moodle_url("/local/webhooks/editservice.php", array("serviceid" => $this->objectid)); return new moodle_url('/local/webhooks/editservice.php', ['serviceid' => $this->objectid]);
}
/**
* Init method.
*/
protected function init() {
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'local_webhooks_service';
} }
} }
+60 -9
View File
@@ -24,13 +24,20 @@
namespace local_webhooks; namespace local_webhooks;
defined("MOODLE_INTERNAL") || die(); defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . "/../locallib.php"); global $CFG;
require_once(__DIR__ . "/../lib.php");
require_once(__DIR__ . '/../lib.php');
require_once(__DIR__ . '/../locallib.php');
require_once($CFG->libdir . '/filelib.php');
use curl;
use local_webhooks_events;
/** /**
* Defines event handlers. * Defines how to work with events.
* *
* @copyright 2017 "Valentin Popov" <info@valentineus.link> * @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @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
@@ -40,17 +47,61 @@ class handler {
* External handler. * External handler.
* *
* @param object $event * @param object $event
*
* @throws \dml_exception
* @throws \coding_exception
*/ */
public static function events($event) { public static function events($event) {
$data = $event->get_data(); $data = $event->get_data();
if (!is_array($recordlist = local_webhooks_cache_get($data["eventname"]))) { if (!empty($callbacks = local_webhooks_get_list_records())) {
$recordlist = local_webhooks_search_services_by_event($data["eventname"], true); foreach ($callbacks as $callback) {
local_webhooks_cache_set($data["eventname"], $recordlist); self::handler_callback($data, $callback);
}
}
} }
foreach ($recordlist as $record) { /**
local_webhooks_send_request($data, $record); * Processes each callback.
*
* @param array $data
* @param object $callback
*
* @throws \coding_exception
* @throws \dml_exception
*/
private static function handler_callback($data, $callback) {
global $CFG;
if ((bool) $callback->enable && !empty($callback->events[$data['eventname']])) {
$urlparse = parse_url($CFG->wwwroot);
$data['host'] = $urlparse['host'];
$data['token'] = $callback->token;
$data['extra'] = $callback->other;
self::send($data, $callback);
} }
} }
/**
* Sending data to the node.
*
* @param array $data
* @param object $callback
*
* @return array
* @throws \coding_exception
* @throws \dml_exception
*/
private static function send($data, $callback) {
$curl = new curl();
$curl->setHeader(['Content-Type: application/' . $callback->type]);
$curl->post($callback->url, json_encode($data));
$response = $curl->getResponse();
local_webhooks_events::response_answer($callback->id, $response);
return $response;
}
} }
+42
View File
@@ -0,0 +1,42 @@
<?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/>.
/**
* Privacy Subsystem implementation for local_webhooks.
*
* @package local_webhooks
* @copyright 2020 Dmitrii Metelkin <dmitriim@catalyst-net.au>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_webhooks\privacy;
/**
* Declares that the plugin does not store personal data.
*/
class provider implements
// This plugin does not store any personal user data.
\core_privacy\local\metadata\null_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}
+103
View File
@@ -0,0 +1,103 @@
<?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/>.
/**
* Defines the service editing form.
*
* @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();
require_once($CFG->libdir . '/formslib.php');
/**
* Description editing form definition.
*
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class service_edit_form extends moodleform {
/**
* Defines the standard structure of the form.
*
* @throws \coding_exception
*/
protected function definition() {
$mform =& $this->_form;
$size = ['size' => 60];
/* Form heading */
$mform->addElement('header', 'editserviceheader', new lang_string('service', 'local_webhooks'));
/* Name of the service */
$mform->addElement('text', 'title', new lang_string('name', 'local_webhooks'), $size);
$mform->addRule('title', null, 'required');
$mform->setType('title', PARAM_NOTAGS);
/* Callback address */
$mform->addElement('text', 'url', new lang_string('url', 'local_webhooks'), $size);
$mform->addRule('url', null, 'required');
$mform->setType('url', PARAM_URL);
/* Enabling the service */
$mform->addElement('advcheckbox', 'enable', new lang_string('enable', 'local_webhooks'));
$mform->setType('enable', PARAM_BOOL);
$mform->setDefault('enable', 1);
$mform->setAdvanced('enable');
/* Token */
$mform->addElement('text', 'token', new lang_string('token', 'local_webhooks'), $size);
$mform->setType('token', PARAM_NOTAGS);
/* Additional information */
$mform->addElement('text', 'other', new lang_string('sourceext', 'local_webhooks'), $size);
$mform->setType('other', PARAM_NOTAGS);
$mform->setAdvanced('other');
/* Content type */
$contenttype = [
'json' => 'application/json',
'x-www-form-urlencoded' => 'application/x-www-form-urlencoded',
];
$mform->addElement('select', 'type', new lang_string('contenttype', 'local_webhooks'), $contenttype);
$mform->setAdvanced('type');
/* Form heading */
$mform->addElement('header', 'editserviceheaderevent', new lang_string('edulevel', 'local_webhooks'));
/* List of events */
$events = [];
/* Formation of the list of elements */
foreach (local_webhooks_get_event_choices() as $component => $choices) {
foreach ($choices as $key => $eventname) {
$events[$component][] =& $mform->createElement('checkbox', $key, $eventname);
}
}
/* Displays groups of items */
foreach ($events as $key => $event) {
$mform->addGroup($event, 'events', $key, '<br />');
}
/* Control Panel */
$this->add_action_buttons();
}
}
+8 -96
View File
@@ -22,87 +22,9 @@
* @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
*/ */
defined("MOODLE_INTERNAL") || die(); defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . "/../lib.php"); require_once($CFG->libdir . '/formslib.php');
require_once($CFG->libdir . "/formslib.php");
/**
* Description editing form definition.
*
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class service_edit_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;
$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"), $size);
$mform->addRule("title", null, "required");
$mform->setType("title", PARAM_NOTAGS);
/* Callback address */
$mform->addElement("text", "url", new lang_string("url", "moodle"), $size);
$mform->addRule("url", null, "required");
$mform->setType("url", PARAM_URL);
/* Enabling the service */
$mform->addElement("advcheckbox", "enable", new lang_string("enable", "moodle"));
$mform->setType("enable", PARAM_BOOL);
$mform->setDefault("enable", 1);
$mform->setAdvanced("enable");
/* Token */
$mform->addElement("text", "token", new lang_string("token", "webservice"), $size);
$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 */
$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 */
$mform->addElement("header", "editserviceheaderevent", new lang_string("edulevel", "moodle"));
/* List of events */
$eventlist = local_webhooks_get_list_events();
$events = array();
/* Formation of the list of elements */
foreach ($eventlist as $event) {
$events[$event["component"]][] =& $mform->createElement("checkbox", $event["eventname"], $event["eventname"]);
}
/* Displays groups of items */
foreach ($events as $key => $event) {
$mform->addGroup($event, "events", $key, "<br />", true);
}
/* Control Panel */
$this->add_action_buttons(true);
}
}
/** /**
* Description of the form of restoration. * Description of the form of restoration.
@@ -111,32 +33,22 @@ class service_edit_form extends moodleform {
* @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
*/ */
class service_backup_form extends moodleform { class service_backup_form extends moodleform {
/**
* @param string $baseurl
*/
public function __construct($baseurl) {
parent::__construct($baseurl);
}
/** /**
* Defines the standard structure of the form. * Defines the standard structure of the form.
*
* @throws \coding_exception
*/ */
protected function definition() { protected function definition() {
$mform =& $this->_form; $mform =& $this->_form;
/* Form heading */ /* Form heading */
$mform->addElement("header", "editserviceheader", new lang_string("restore", "moodle")); $mform->addElement('header', 'editserviceheader', new lang_string('restore', 'local_webhooks'));
/* Flag for deleting existing data */
$mform->addElement("advcheckbox", "deleterecords", new lang_string("deleteall", "moodle"));
$mform->setType("deleterecords", PARAM_BOOL);
$mform->setDefault("deleterecords", 1);
/* Download the file */ /* Download the file */
$mform->addElement("filepicker", "backupfile", new lang_string("file", "moodle")); $mform->addElement('filepicker', 'backupfile', new lang_string('file', 'local_webhooks'));
$mform->addRule("backupfile", null, "required"); $mform->addRule('backupfile', null, 'required');
/* Control Panel */ /* Control Panel */
$this->add_action_buttons(true, new lang_string("restore", "moodle")); $this->add_action_buttons(true, new lang_string('restore', 'local_webhooks'));
} }
} }
-145
View File
@@ -1,145 +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/>.
/**
* Describes the plugin tables.
*
* @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();
require_once($CFG->libdir . "/tablelib.php");
/**
* Describes the main table of the plugin.
*
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class local_webhooks_table extends table_sql {
/**
* Manager address.
*
* @var string $manager
*/
protected static $manager = "/local/webhooks/index.php";
/**
* Editor's address.
*
* @var string $editor
*/
protected static $editor = "/local/webhooks/editservice.php";
/**
* Constructor.
*
* @param string $uniqueid The unique identifier of the table.
*/
public function __construct($uniqueid) {
parent::__construct($uniqueid);
$this->define_table_columns();
$this->define_table_configs();
}
/**
* Defines the basic settings of the table.
*/
public function define_table_configs() {
$this->set_sql("*", "{local_webhooks_service}", "1");
$this->collapsible(false);
$this->is_downloadable(false);
$this->no_sorting("actions");
$this->pageable(true);
}
/**
* Defines the main columns and table headers.
*/
public function define_table_columns() {
$columns = array(
"title",
"url",
"events",
"actions"
);
$headers = array(
new lang_string("name", "moodle"),
new lang_string("url", "moodle"),
new lang_string("edulevel", "moodle"),
new lang_string("actions", "moodle")
);
$this->define_columns($columns);
$this->define_headers($headers);
}
/**
* Specifies the display of a column with actions.
*
* @param object $row Data from the database.
* @return string Displayed data.
*/
public function col_actions($row) {
global $OUTPUT;
$hideshowicon = "t/show";
$hideshowstring = new lang_string("enable", "moodle");
if (boolval($row->enable)) {
$hideshowicon = "t/hide";
$hideshowstring = new lang_string("disable", "moodle");
}
$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, "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()));
$deleteitem = $OUTPUT->action_icon($deletelink, new pix_icon("t/delete", new lang_string("delete", "moodle")));
$html = $hideshowitem . $edititem . $deleteitem;
return $html;
}
/**
* Specifies the display of a column with events.
*
* @param object $row Data from the database.
* @return string Displayed data.
*/
public function col_events($row) {
$eventlist = local_webhooks_deserialization_data($row->events);
return count($eventlist);
}
/**
* Specifies the display of the column with the service name.
*
* @param object $row Data from the database.
* @return string Displayed data.
*/
public function col_title($row) {
$link = new moodle_url(self::$editor, array("serviceid" => $row->id, "sesskey" => sesskey()));
$html = html_writer::link($link, $row->title);
return $html;
}
}
+44
View File
@@ -0,0 +1,44 @@
services:
postgres:
image: postgres:16.13-alpine3.22@sha256:caf536ebd910e85473a9f5e2fd1aea360c965c4a911c8555ac1f61b2102006cb
environment:
POSTGRES_HOST_AUTH_METHOD: trust
POSTGRES_USER: postgres
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 2s
timeout: 5s
retries: 15
tmpfs:
- /var/lib/postgresql/data
ci:
build:
context: .
dockerfile: .docker/ci/Dockerfile
depends_on:
postgres:
condition: service_healthy
environment:
DB: pgsql
DB_HOST: postgres
DB_USER: postgres
MOODLE_BRANCH: v5.2.2
NPM_CONFIG_FETCH_RETRIES: 5
command:
- /bin/bash
- -lc
- |
set -euo pipefail
php --version
composer --version
node --version
moodle-plugin-ci --version
moodle-plugin-ci install --plugin /plugin --branch v5.2.2 --db-host postgres
test "$$(git -C /workspace/moodle rev-parse HEAD)" = "acfa383c13dd46c83ca82894a8e95fac273fda33"
moodle-plugin-ci phplint
moodle-plugin-ci phpcs --max-warnings 0
moodle-plugin-ci phpdoc --max-warnings 0
moodle-plugin-ci validate
moodle-plugin-ci savepoints
moodle-plugin-ci phpunit --fail-on-warning
-31
View File
@@ -1,31 +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/>.
/**
* 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
)
);
+10 -10
View File
@@ -22,14 +22,14 @@
* @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
*/ */
defined("MOODLE_INTERNAL") || die(); defined('MOODLE_INTERNAL') || die();
$observers = array( $observers = [
array( [
"callback" => "\local_webhooks\\handler::events", 'callback' => '\local_webhooks\handler::events',
"eventname" => "*", 'eventname' => '*',
"includefile" => null, 'includefile' => null,
"internal" => true, 'internal' => true,
"priority" => 200 'priority' => 200,
) ],
); ];
+3 -1
View File
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<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"> <XMLDB PATH="local/webhooks/db" VERSION="2018061900" 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>
-115
View File
@@ -1,115 +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/>.
/**
* This file registers the plugin's external functions.
*
* @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();
$functions = array(
"local_webhooks_change_status" => array(
"classname" => "local_webhooks_external",
"methodname" => "change_status",
"classpath" => "local/webhooks/externallib.php",
"description" => "Change the status of the service.",
"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",
"classpath" => "local/webhooks/externallib.php",
"description" => "Get the record from the database.",
"type" => "read"
),
"local_webhooks_get_list_records" => array(
"classname" => "local_webhooks_external",
"methodname" => "get_list_records",
"classpath" => "local/webhooks/externallib.php",
"description" => "Get all records from the database.",
"type" => "read"
),
"local_webhooks_get_list_events" => array(
"classname" => "local_webhooks_external",
"methodname" => "get_list_events",
"classpath" => "local/webhooks/externallib.php",
"description" => "Get a list of all system events.",
"type" => "read"
),
"local_webhooks_create_record" => array(
"classname" => "local_webhooks_external",
"methodname" => "create_record",
"classpath" => "local/webhooks/externallib.php",
"description" => "Create an entry in the database.",
"type" => "write"
),
"local_webhooks_update_record" => array(
"classname" => "local_webhooks_external",
"methodname" => "update_record",
"classpath" => "local/webhooks/externallib.php",
"description" => "Update the record in the database.",
"type" => "write"
),
"local_webhooks_delete_record" => array(
"classname" => "local_webhooks_external",
"methodname" => "delete_record",
"classpath" => "local/webhooks/externallib.php",
"description" => "Delete the record from the database.",
"type" => "write"
),
"local_webhooks_delete_all_records" => array(
"classname" => "local_webhooks_external",
"methodname" => "delete_all_records",
"classpath" => "local/webhooks/externallib.php",
"description" => "Delete all records from the database.",
"type" => "write"
),
"local_webhooks_create_backup" => array(
"classname" => "local_webhooks_external",
"methodname" => "create_backup",
"classpath" => "local/webhooks/externallib.php",
"description" => "Create a backup.",
"type" => "read"
),
"local_webhooks_restore_backup" => array(
"classname" => "local_webhooks_external",
"methodname" => "restore_backup",
"classpath" => "local/webhooks/externallib.php",
"description" => "Restore from a backup.",
"type" => "write"
)
);
-52
View File
@@ -1,52 +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/>.
/**
* Keeps track of upgrades to the 'local_webhooks' plugin.
*
* @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();
require_once(__DIR__ . "/../lib.php");
/**
* Function to upgrade 'local_webhooks'.
*
* @param number $oldversion
* @return boolean
*/
function xmldb_local_webhooks_upgrade($oldversion) {
global $CFG, $DB;
/* Update from version 3.0.0 */
if ($oldversion < 2017112600) {
$rs = $DB->get_recordset("local_webhooks_service", null, "id", "*", 0, 0);
foreach ($rs as $record) {
if (!empty($record->events)) {
$record->events = unserialize(gzuncompress(base64_decode($record->events)));
local_webhooks_update_record($record);
}
}
$rs->close();
upgrade_plugin_savepoint(true, 2017112600, "local", "webhooks");
}
return true;
}
-111
View File
@@ -1,111 +0,0 @@
# Getting Started
## Navigation
* [Installation](#installation)
* [Build](#build)
* [User guide](#user-guide)
* [Guide to the manager](#guide-to-the-manager)
* [Guide to the editor](#guide-to-the-editor)
* [Request format](#request-format)
## Installation
Get the installation package in any of the available methods:
* [GitHub Releases](https://github.com/valentineus/moodle-webhooks/releases).
* [Compilation from the source code](#build).
### Build
Self-assembly package is as follows:
* Clone the repository:
```bash
git clone https://github.com/valentineus/moodle-webhooks.git moodle-webhooks
```
* Run the build script:
```bash
cd ./moodle-webhooks
/bin/sh build.sh
```
## User guide
After you install the plug-in, the Service Manager appears on the `Server` tab in the administration panel.
To open the page, go:
`Site administration` ->
`Server` ->
`WebHooks`.
### Guide to the manager
The main window looks like this:
![Service Manager](screenshots/managerservice.jpg)
On the main page you can see the list of services and manage each service separately.
Column description
* "**Name**" shows the name of the service.
* "**URL**" indicates the address of the service to which notifications are received.
* "**Actions**" allows you to enable / disable the service, edit and delete.
Description of control buttons:
* "**Add service**" opens a page for creating a new service.
* "**Backup**" loads the backup file of the service list.
* "**Restore**" redirects the data recovery page from the backup.
### Guide to the editor
During the editing and creation of the service, this page opens:
![Service Editor](screenshots/editservice.jpg)
Description of fields:
* "**Name**" stores an arbitrary name for the service.
* "**URL**" stores the address of the service to which notifications will be sent as a POST request.
* "**Token**" allows you to specify an individual key that will allow an external service to identify requests.
* "**Advanced**" stores a large string passed to the service.
This can be useful for some services or users.
* "**Content Type**" allows you to configure the type of outbound requests if there are compatibility issues.
The "**All events**" list contains a list of all events registered in the system.
The selected events will notify the service.
## Request format
Events come in
[JSON](https://en.wikipedia.org/wiki/JSON)
format.
An example of an observed course event:
```JSON
{
"eventname": "\\core\\event\\course_viewed",
"component": "core",
"action": "viewed",
"target": "course",
"objecttable": null,
"objectid": null,
"crud": "r",
"edulevel": 2,
"contextid": 2,
"contextlevel": 50,
"contextinstanceid": "1",
"userid": "2",
"courseid": "1",
"relateduserid": null,
"anonymous": 0,
"other": null,
"timecreated": 1512961456,
"host": "localhost",
"token": "",
"extra": ""
}
```
[Detailed description of the fields](https://docs.moodle.org/dev/Event_2#Properties).
Binary file not shown.

Before

Width:  |  Height:  |  Size: 165 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 122 KiB

+43 -19
View File
@@ -22,22 +22,22 @@
* @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
*/ */
require_once(__DIR__ . "/../../config.php"); require_once(__DIR__ . '/../../config.php');
require_once(__DIR__ . "/classes/service_form.php"); require_once(__DIR__ . '/classes/service_edit_form.php');
require_once(__DIR__ . "/lib.php"); require_once(__DIR__ . '/lib.php');
require_once($CFG->libdir . "/adminlib.php"); require_once($CFG->libdir . '/adminlib.php');
/* Optional parameters */ /* Optional parameters */
$serviceid = optional_param("serviceid", 0, PARAM_INT); $serviceid = optional_param('serviceid', 0, PARAM_INT);
/* Link generation */ /* Link generation */
$urlparameters = array("serviceid" => $serviceid); $urlparameters = ['serviceid' => $serviceid];
$baseurl = new moodle_url("/local/webhooks/editservice.php", $urlparameters); $baseurl = new moodle_url('/local/webhooks/editservice.php', $urlparameters);
$managerservice = new moodle_url("/local/webhooks/index.php"); $managerservice = new moodle_url('/local/webhooks/index.php');
/* Configure the context of the page */ /* Configure the context of the page */
admin_externalpage_setup("local_webhooks", "", null, $baseurl, array()); admin_externalpage_setup('local_webhooks', '', null, $baseurl);
$context = context_system::instance(); $context = context_system::instance();
/* Create an editing form */ /* Create an editing form */
@@ -50,25 +50,50 @@ if ($mform->is_cancelled()) {
/* Getting the data */ /* Getting the data */
$servicerecord = new stdClass(); $servicerecord = new stdClass();
if ($editing = boolval($serviceid)) { if (($editing = (bool) $serviceid) === true) {
$servicerecord = local_webhooks_get_record($serviceid); $servicerecord = local_webhooks_get_record($serviceid);
if (is_array($servicerecord->events)) {
$events = [];
/* Escaping event names */
foreach ($servicerecord->events as $eventname => $eventstatus) {
$eventname = base64_encode($eventname);
$events[$eventname] = $eventstatus;
}
$servicerecord->events = $events;
}
$mform->set_data($servicerecord); $mform->set_data($servicerecord);
} }
/* Processing of received data */ /* Processing of received data */
if (($data = $mform->get_data()) && confirm_sesskey()) { if (!empty($data = $mform->get_data())) {
if ($editing) { if (is_array($data->events)) {
$data->id = $serviceid; $events = [];
local_webhooks_update_record($data);
} else { /* Deciphering event names */
local_webhooks_create_record($data); foreach ($data->events as $eventname => $eventstatus) {
$eventname = (string) base64_decode($eventname);
$events[$eventname] = $eventstatus;
} }
redirect($managerservice, new lang_string("changessaved", "moodle")); $data->events = $events;
}
if ($editing) {
$data->id = $serviceid;
local_webhooks_update_record($data, false);
redirect($managerservice, new lang_string('eventwebserviceserviceupdated', 'local_webhooks'));
} else {
local_webhooks_update_record($data);
redirect($managerservice, new lang_string('eventwebserviceservicecreated', 'local_webhooks'));
}
} }
/* The page title */ /* The page title */
$titlepage = new lang_string("externalservice", "webservice"); $titlepage = new lang_string('externalservice', 'local_webhooks');
$PAGE->navbar->add($titlepage); $PAGE->navbar->add($titlepage);
$PAGE->set_heading($titlepage); $PAGE->set_heading($titlepage);
$PAGE->set_title($titlepage); $PAGE->set_title($titlepage);
@@ -77,5 +102,4 @@ echo $OUTPUT->header();
/* Displays the form */ /* Displays the form */
$mform->display(); $mform->display();
/* Footer */
echo $OUTPUT->footer(); echo $OUTPUT->footer();
-643
View File
@@ -1,643 +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/>.
/**
* This file defines the plugin's external functions.
*
* @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();
require_once(__DIR__ . "/lib.php");
require_once($CFG->libdir . "/externallib.php");
/**
* External functions.
*
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @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.
*
* @return external_function_parameters
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function change_status_parameters() {
return new external_function_parameters(
array(
"serviceid" => new external_value(PARAM_INT, "Service ID.")
)
);
}
/**
* Change the status of the service.
*
* @param number $serviceid
* @return boolean
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
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);
return local_webhooks_change_status($parameters["serviceid"]);
}
/**
* Returns description of method result value.
*
* @return external_description
* @since Moodle 2.2
*/
public static function change_status_returns() {
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."
);
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function get_record_parameters() {
return new external_function_parameters(
array(
"serviceid" => new external_value(PARAM_INT, "Service ID.")
)
);
}
/**
* Get the record from the database.
*
* @param number $serviceid
* @return array
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
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);
$result = array();
if ($record = local_webhooks_get_record($parameters["serviceid"])) {
$result = (array) $record;
$result["events"] = self::formation_events($record->events);
}
return $result;
}
/**
* Returns description of method result value.
*
* @return external_description
* @since Moodle 2.2
*/
public static function get_record_returns() {
return self::structure_record_parameters();
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function get_list_records_parameters() {
return new external_function_parameters(array());
}
/**
* Get all records from the database.
*
* @return array
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function get_list_records() {
$context = context_system::instance();
self::validate_context($context);
$result = array();
if ($listrecords = local_webhooks_get_list_records()) {
$result = self::formation_list($listrecords);
}
return $result;
}
/**
* Returns description of method result value.
*
* @return external_description
* @since Moodle 2.2
*/
public static function get_list_records_returns() {
return new external_multiple_structure(
self::structure_record_parameters(), "List of services."
);
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function get_list_events_parameters() {
return new external_function_parameters(array());
}
/**
* Get a list of all system events.
*
* @return array
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function get_list_events() {
$context = context_system::instance();
self::validate_context($context);
$result = array();
if ($eventlist = local_webhooks_get_list_events()) {
foreach ($eventlist as $item) {
$result[] = $item["eventname"];
}
}
return $result;
}
/**
* Returns description of method result value.
*
* @return external_description
* @since Moodle 2.2
*/
public static function get_list_events_returns() {
return new external_multiple_structure(
new external_value(PARAM_TEXT, "The name of the event.")
);
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function create_record_parameters() {
return new external_function_parameters(
array(
"service" => new external_single_structure(
array(
"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, "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, "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."
)
)
);
}
/**
* Create an entry in the database.
*
* @param array $service
* @return boolean
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
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->events = array();
$record->enable = !empty($parameters["service"]["enable"]) ? $parameters["service"]["enable"] : false;
$record->type = !empty($parameters["service"]["type"]) ? $parameters["service"]["type"] : "json";
foreach ($parameters["service"]["events"] as $value) {
$record->events[$value["name"]] = $value["value"];
}
return local_webhooks_create_record($record);
}
/**
* Returns description of method result value.
*
* @return external_description
* @since Moodle 2.2
*/
public static function create_record_returns() {
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 update_record_parameters() {
return new external_function_parameters(
array(
"service" => new external_single_structure(
array(
"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, "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, "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."
)
)
);
}
/**
* Update the record in the database.
*
* @param array $service
* @return boolean
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
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["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;
$record->token = !empty($parameters["service"]["token"]) ? $parameters["service"]["token"] : $record->token;
$record->type = !empty($parameters["service"]["type"]) ? $parameters["service"]["type"] : $record->type;
$record->url = !empty($parameters["service"]["url"]) ? $parameters["service"]["url"] : $record->url;
if (!empty($parameters["service"]["events"])) {
$record->events = array();
foreach ($parameters["service"]["events"] as $value) {
$record->events[$value["name"]] = $value["value"];
}
}
$result = local_webhooks_update_record($record);
}
return $result;
}
/**
* Returns description of method result value.
*
* @return external_description
* @since Moodle 2.2
*/
public static function update_record_returns() {
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 delete_record_parameters() {
return new external_function_parameters(
array(
"serviceid" => new external_value(PARAM_INT, "Service ID.")
)
);
}
/**
* Delete the record from the database.
*
* @param number $serviceid
* @return boolean
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
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);
return local_webhooks_delete_record($parameters["serviceid"]);
}
/**
* Returns description of method result value.
*
* @return external_description
* @since Moodle 2.2
*/
public static function delete_record_returns() {
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 delete_all_records_parameters() {
return new external_function_parameters(array());
}
/**
* Delete all records from the database.
*
* @return boolean
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function delete_all_records() {
$context = context_system::instance();
self::validate_context($context);
return local_webhooks_delete_all_records();
}
/**
* Returns description of method result value.
*
* @return external_description
* @since Moodle 2.2
*/
public static function delete_all_records_returns() {
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 create_backup_parameters() {
return new external_function_parameters(array());
}
/**
* Create a backup.
*
* @return string
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function create_backup() {
$context = context_system::instance();
self::validate_context($context);
return local_webhooks_create_backup();
}
/**
* Returns description of method result value.
*
* @return external_description
* @since Moodle 2.2
*/
public static function create_backup_returns() {
return new external_value(PARAM_RAW, "Backup copy.");
}
/**
* Returns description of method parameters.
*
* @return external_function_parameters
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
public static function restore_backup_parameters() {
return new external_function_parameters(
array(
"options" => new external_single_structure(
array(
"backup" => new external_value(PARAM_RAW, "Backup copy."),
"deleterecords" => new external_value(PARAM_BOOL, "Delete or leave all records.", VALUE_OPTIONAL)
)
)
)
);
}
/**
* Restore from a backup.
*
* @param array $options
* @since Moodle 2.9 Options available
* @since Moodle 2.2
*/
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"]) ? $parameters["options"]["deleterecords"] : false;
local_webhooks_restore_backup($parameters["options"]["backup"], $deleterecords);
}
/**
* Returns description of method result value.
*
* @return external_description
* @since Moodle 2.2
*/
public static function restore_backup_returns() {
return null;
}
}
+92 -46
View File
@@ -22,79 +22,125 @@
* @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
*/ */
require_once(__DIR__ . "/../../config.php"); require_once(__DIR__ . '/../../config.php');
require_once(__DIR__ . "/classes/webhooks_table.php"); require_once(__DIR__ . '/lib.php');
require_once(__DIR__ . "/lib.php");
require_once($CFG->libdir . "/adminlib.php"); require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->libdir . '/tablelib.php');
/* Optional parameters */ /* Optional parameters */
$backupservices = optional_param("getbackup", 0, PARAM_BOOL); $backupservices = optional_param('getbackup', 0, PARAM_BOOL);
$deleteall = optional_param("deleteall", 0, PARAM_INT); $deleteid = optional_param('deleteid', 0, PARAM_INT);
$deleteid = optional_param("deleteid", 0, PARAM_INT); $hideshowid = optional_param('hideshowid', 0, PARAM_INT);
$hideshowid = optional_param("hideshowid", 0, PARAM_INT);
/* Link generation */ /* Link generation */
$editservice = "/local/webhooks/editservice.php"; $editservice = '/local/webhooks/editservice.php';
$managerservice = "/local/webhooks/index.php"; $managerservice = '/local/webhooks/index.php';
$restorebackup = "/local/webhooks/restorebackup.php"; $restorebackup = '/local/webhooks/restorebackup.php';
$baseurl = new moodle_url($managerservice); $baseurl = new moodle_url($managerservice);
/* Configure the context of the page */ /* Configure the context of the page */
admin_externalpage_setup("local_webhooks", "", null, $baseurl, array()); admin_externalpage_setup('local_webhooks', '', null, $baseurl);
$context = context_system::instance(); $context = context_system::instance();
/* Delete the service */ /* Delete the service */
if (!empty($deleteid) && confirm_sesskey()) { if ((bool) $deleteid) {
local_webhooks_delete_record($deleteid); local_webhooks_remove_record($deleteid);
redirect($PAGE->url, new lang_string("deleted", "moodle")); redirect($PAGE->url, new lang_string('eventwebserviceservicedeleted', 'local_webhooks'));
} }
/* Switching the status of the service */ /* Retrieving a list of services */
if (!empty($hideshowid) && confirm_sesskey()) { $callbacks = local_webhooks_get_list_records();
local_webhooks_change_status($hideshowid);
redirect($PAGE->url, new lang_string("changessaved", "moodle"));
}
/* Deletes all data */
if (boolval($deleteall) && confirm_sesskey()) {
local_webhooks_delete_all_records();
redirect($PAGE->url, new lang_string("deleted", "moodle"));
}
/* Upload settings as a file */ /* Upload settings as a file */
if (boolval($backupservices)) { if ((bool) $backupservices) {
$filecontent = local_webhooks_create_backup(); $filecontent = local_webhooks_create_backup();
$filename = "webhooks_" . date("U") . ".backup"; $filename = 'webhooks_' . date('U') . '.backup';
send_file($filecontent, $filename, 0, 0, true, true); send_file($filecontent, $filename, 0, 0, true, true);
} }
/* Switching the status of the service */
if ((bool) $hideshowid) {
$callback = $callbacks[$hideshowid];
if (!empty($callback)) {
$callback->enable = !(bool) $callback->enable;
local_webhooks_update_record($callback, false);
redirect($PAGE->url, new lang_string('eventwebserviceserviceupdated', 'local_webhooks'));
}
}
/* The page title */ /* The page title */
$titlepage = new lang_string("pluginname", "local_webhooks"); $titlepage = new lang_string('pluginname', 'local_webhooks');
$PAGE->set_heading($titlepage); $PAGE->set_heading($titlepage);
$PAGE->set_title($titlepage); $PAGE->set_title($titlepage);
echo $OUTPUT->header(); echo $OUTPUT->header();
/* Adds the add button */ /* Table declaration */
$addserviceurl = new moodle_url($editservice, array("sesskey" => sesskey())); $table = new flexible_table('webhooks-service-table');
echo $OUTPUT->single_button($addserviceurl, new lang_string("add", "moodle"));
/* Adds a delete button */ /* Customize the table */
$deleteallurl = new moodle_url($managerservice, array("deleteall" => true, "sesskey" => sesskey())); $table->define_columns(
echo $OUTPUT->single_button($deleteallurl, new lang_string("deleteall", "moodle"), "get"); [
'title',
'url',
'actions',
]
);
/* Adds a backup button */ $table->define_headers(
$backupurl = new moodle_url($managerservice, array("getbackup" => true)); [
echo $OUTPUT->single_button($backupurl, new lang_string("backup", "moodle"), "get"); new lang_string('name', 'local_webhooks'),
new lang_string('url', 'local_webhooks'),
new lang_string('actions', 'local_webhooks'),
]
);
/* Adds a restore button */
$restorebackupurl = new moodle_url($restorebackup);
echo $OUTPUT->single_button($restorebackupurl, new lang_string("restore", "moodle"));
/* Displays the table */
$table = new local_webhooks_table("local-webhooks-table");
$table->define_baseurl($baseurl); $table->define_baseurl($baseurl);
$table->out(25, true); $table->setup();
foreach ($callbacks as $callback) {
/* Filling of information columns */
$titlecallback = html_writer::div($callback->title, 'title');
$urlcallback = html_writer::div($callback->url, 'url');
/* Defining service status */
$hideshowicon = 't/show';
$hideshowstring = new lang_string('enable', 'local_webhooks');
if ((bool) $callback->enable) {
$hideshowicon = 't/hide';
$hideshowstring = new lang_string('disable', 'local_webhooks');
}
/* Link to enable / disable the service */
$hideshowlink = new moodle_url($managerservice, ['hideshowid' => $callback->id]);
$hideshowitem = $OUTPUT->action_icon($hideshowlink, new pix_icon($hideshowicon, $hideshowstring));
/* Link for editing */
$editlink = new moodle_url($editservice, ['serviceid' => $callback->id]);
$edititem = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', new lang_string('edit', 'local_webhooks')));
/* Link to remove */
$deletelink = new moodle_url($managerservice, ['deleteid' => $callback->id]);
$deleteitem = $OUTPUT->action_icon($deletelink, new pix_icon('t/delete', new lang_string('delete', 'local_webhooks')));
/* Adding data to the table */
$table->add_data([$titlecallback, $urlcallback, $hideshowitem . $edititem . $deleteitem]);
}
/* Display the table */
$table->print_html();
/* Add service button */
$addserviceurl = new moodle_url($editservice);
echo $OUTPUT->single_button($addserviceurl, new lang_string('addaservice', 'local_webhooks'), 'get');
/* Button to get a backup */
$backupurl = new moodle_url($managerservice, ['getbackup' => true]);
echo $OUTPUT->single_button($backupurl, new lang_string('backup', 'local_webhooks'), 'get');
/* Button for restoring settings */
$restorebackupurl = new moodle_url($restorebackup);
echo $OUTPUT->single_button($restorebackupurl, new lang_string('restore', 'local_webhooks'), 'get');
/* Footer */
echo $OUTPUT->footer(); echo $OUTPUT->footer();
+29 -1
View File
@@ -22,4 +22,32 @@
* @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["pluginname"] = "WebHooks"; $string['actions'] = 'Actions';
$string['addaservice'] = 'Add service';
$string['answer'] = 'Answer';
$string['backup'] = 'Backup';
$string['backupfinished'] = 'Backup completed successfully';
$string['contenttype'] = 'Content type';
$string['create'] = 'Create';
$string['delete'] = 'Delete';
$string['deleted'] = 'Deleted';
$string['disable'] = 'Disable';
$string['edit'] = 'Edit';
$string['edulevel'] = 'All events';
$string['enable'] = 'Enable';
$string['errorsendingrequest'] = 'Error sending request';
$string['eventwebserviceservicecreated'] = 'Web service created';
$string['eventwebserviceservicedeleted'] = 'Web service deleted';
$string['eventwebserviceserviceupdated'] = 'Web service updated';
$string['externalservice'] = 'External service';
$string['file'] = 'File';
$string['name'] = 'Name';
$string['pluginname'] = 'WebHooks';
$string['privacy:metadata'] = 'WebHooks does not store any personal data.';
$string['restore'] = 'Restore';
$string['restorefinished'] = 'Restore completed successfully';
$string['service'] = 'Service';
$string['sourceext'] = 'Additional';
$string['token'] = 'Token';
$string['update'] = 'Update';
$string['url'] = 'URL';
+125 -168
View File
@@ -15,254 +15,211 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** /**
* This file contains the functions used by the plugin. * Library code used by the service control interfaces.
* *
* @package local_webhooks * @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link> * @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @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
*/ */
defined("MOODLE_INTERNAL") || die(); defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . "/locallib.php"); require_once(__DIR__ . '/locallib.php');
/** /**
* Change the status of the service. * Returns available event classes grouped by component.
* *
* @param number $serviceid
* @return boolean
*/
function local_webhooks_change_status($serviceid) {
global $DB;
$result = false;
if ($record = local_webhooks_get_record($serviceid)) {
$record->enable = !boolval($record->enable);
$result = local_webhooks_update_record($record);
}
return $result;
}
/**
* Search for services that contain the specified event.
*
* @param string $eventname
* @param boolean $active
* @return array * @return array
*/ */
function local_webhooks_search_services_by_event($eventname, $active = false) { function local_webhooks_get_event_choices() {
$recordlist = local_webhooks_get_list_records(); global $CFG;
$active = boolval($active);
$result = array();
foreach ($recordlist as $record) { $choices = [];
if (!empty($record->events[$eventname])) { $debug = $CFG->debug;
if ($active && boolval($record->enable)) {
$result[] = $record; // Deprecated core events emit debugging while loading; mirror the core tool_monitor workaround.
try {
$CFG->debug = DEBUG_NONE;
$classes = core_component::get_component_classes_in_namespace(null, 'event');
} finally {
$CFG->debug = $debug;
} }
if (!$active) { ksort($classes);
$result[] = $record; foreach (array_keys($classes) as $classname) {
} if (
} $classname === \core\event\unknown_logged::class
|| !is_subclass_of($classname, \core\event\base::class)
|| $classname::is_deprecated()
) {
continue;
} }
return $result; $reflection = new ReflectionClass($classname);
if ($reflection->isAbstract()) {
continue;
}
$eventname = '\\' . ltrim($classname, '\\');
$component = core_component::get_component_from_classname($classname);
$choices[$component][base64_encode($eventname)] = $eventname;
}
return $choices;
} }
/** /**
* Get the record from the database. * Getting a list of all services.
* *
* @param number $serviceid * @param int $limitfrom
* @return object * @param int $limitnum
*
* @return array
* @throws \dml_exception
*/ */
function local_webhooks_get_record($serviceid) { function local_webhooks_get_list_records($limitfrom = 0, $limitnum = 0) {
global $DB; global $DB;
$servicerecord = $DB->get_record("local_webhooks_service", array("id" => $serviceid), "*", MUST_EXIST); $listservices = $DB->get_records('local_webhooks_service', null, 'id', '*', $limitfrom, $limitnum);
foreach ($listservices as $servicerecord) {
if (!empty($servicerecord->events)) {
$servicerecord->events = local_webhooks_unarchive_data($servicerecord->events);
}
}
return $listservices;
}
/**
* Getting information about the service.
*
* @param int $serviceid
*
* @return object
* @throws \dml_exception
*/
function local_webhooks_get_record($serviceid = 0) {
global $DB;
$servicerecord = $DB->get_record('local_webhooks_service', ['id' => $serviceid], '*', MUST_EXIST);
if (!empty($servicerecord->events)) { if (!empty($servicerecord->events)) {
$servicerecord->events = local_webhooks_deserialization_data($servicerecord->events); $servicerecord->events = local_webhooks_unarchive_data($servicerecord->events);
} }
return $servicerecord; return $servicerecord;
} }
/** /**
* Get all records from the database. * Clear the database table.
* *
* @param number $limitfrom * @throws \dml_exception
* @param number $limitnum
* @return array
*/ */
function local_webhooks_get_list_records($limitfrom = 0, $limitnum = 0) { function local_webhooks_remove_list_records() {
global $DB; global $DB;
$listrecords = $DB->get_records("local_webhooks_service", null, "id", "*", $limitfrom, $limitnum); $DB->delete_records('local_webhooks_service');
foreach ($listrecords as $servicerecord) {
if (!empty($servicerecord->events)) {
$servicerecord->events = local_webhooks_deserialization_data($servicerecord->events);
}
}
return $listrecords;
} }
/** /**
* Get a list of all system events. * Delete the record.
* *
* @return array * @param int $serviceid
*/
function local_webhooks_get_list_events() {
return report_eventlist_list_generator::get_all_events_list(true);
}
/**
* Create an entry in the database.
* *
* @param object $record * @throws \dml_exception
* @return boolean * @throws \coding_exception
*/ */
function local_webhooks_create_record($record) { function local_webhooks_remove_record($serviceid = 0) {
global $DB; global $DB;
if (!empty($record->events)) { $DB->delete_records('local_webhooks_service', ['id' => $serviceid]);
$record->events = local_webhooks_serialization_data($record->events); local_webhooks_events::service_deleted($serviceid);
}
$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 boolval($result);
} }
/** /**
* Update the record in the database. * Update the record in the database.
* *
* @param object $data * @param object $data
* @return boolean * @param boolean $insert
*/
function local_webhooks_update_record($record) {
global $DB;
if (empty($record->id)) {
print_error("missingparam", "error", null, "id");
}
$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);
}
/**
* Delete the record from the database.
*
* @param number $serviceid
* @return boolean
*/
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);
}
/**
* Delete all records from the database.
* *
* @return boolean * @return boolean
* @throws \dml_exception
* @throws \coding_exception
*/ */
function local_webhooks_delete_all_records() { function local_webhooks_update_record($data, $insert = true) {
global $DB; global $DB;
$result = $DB->delete_records("local_webhooks_service", null); if (empty($data->events)) {
$data->events = [];
}
/* Clear the plugin cache */ $data->events = local_webhooks_archiving_data($data->events);
local_webhooks_cache_reset();
/* Event notification */ if ((bool) $insert) {
local_webhooks_events::service_deletedall(); $result = $DB->insert_record('local_webhooks_service', $data);
local_webhooks_events::service_added($result);
} else {
$result = $DB->update_record('local_webhooks_service', $data);
local_webhooks_events::service_updated($data->id);
}
return boolval($result); return (bool) $result;
} }
/** /**
* Create a backup. * Make a backup copy of all the services.
* *
* @return string * @return string
* @throws \dml_exception
* @throws \coding_exception
*/ */
function local_webhooks_create_backup() { function local_webhooks_create_backup() {
$listrecords = local_webhooks_get_list_records(); $listservices = local_webhooks_get_list_records();
$result = local_webhooks_serialization_data($listrecords); $listservices = local_webhooks_archiving_data($listservices);
/* Event notification */
local_webhooks_events::backup_performed(); local_webhooks_events::backup_performed();
return $result; return $listservices;
} }
/** /**
* Restore from a backup. * Restore the data from the backup.
* *
* @param string $data * @param string $listservices
*
* @throws \dml_exception
* @throws \coding_exception
*/ */
function local_webhooks_restore_backup($data, $deleterecords = false) { function local_webhooks_restore_backup($listservices = '') {
$listrecords = local_webhooks_deserialization_data($data); $listservices = local_webhooks_unarchive_data($listservices);
if (boolval($deleterecords)) { local_webhooks_remove_list_records();
local_webhooks_delete_all_records();
foreach ($listservices as $servicerecord) {
local_webhooks_update_record($servicerecord);
} }
foreach ($listrecords as $servicerecord) {
local_webhooks_create_record($servicerecord);
}
/* Event notification */
local_webhooks_events::backup_restored(); local_webhooks_events::backup_restored();
} }
/** /**
* Send the event remotely to the service. * Compress an array into a string.
*
* @param array $data
*
* @return string
*/
function local_webhooks_archiving_data(array $data = []) {
return base64_encode(gzcompress(serialize($data), 3));
}
/**
* Gets an array from a compressed string.
*
* @param string $data
* *
* @param array $event
* @param object $callback
* @return array * @return array
*/ */
function local_webhooks_send_request($event, $callback) { function local_webhooks_unarchive_data($data = '') {
global $CFG; return unserialize(gzuncompress(base64_decode($data)));
$event["host"] = parse_url($CFG->wwwroot)["host"];
$event["token"] = $callback->token;
$event["extra"] = $callback->other;
$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;
} }
+64 -88
View File
@@ -22,72 +22,6 @@
* @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
*/ */
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 * Description of functions of the call of events
* *
@@ -97,79 +31,121 @@ function local_webhooks_deserialization_data($data) {
class local_webhooks_events { class local_webhooks_events {
/** /**
* Call the event when creating a backup. * Call the event when creating a backup.
*
* @throws \coding_exception
* @throws \dml_exception
*/ */
public static function backup_performed() { public static function backup_performed() {
$context = context_system::instance(); $context = context_system::instance();
$event = local_webhooks\event\backup_performed::create(array("context" => $context, "objectid" => 0));
$event = local_webhooks\event\backup_performed::create(
[
'context' => $context,
'objectid' => 0,
]
);
$event->trigger(); $event->trigger();
} }
/** /**
* Call the event when restoring from a backup. * Call the event when restoring from a backup.
*
* @throws \coding_exception
* @throws \dml_exception
*/ */
public static function backup_restored() { public static function backup_restored() {
$context = context_system::instance(); $context = context_system::instance();
$event = local_webhooks\event\backup_restored::create(array("context" => $context, "objectid" => 0));
$event = local_webhooks\event\backup_restored::create(
[
'context' => $context,
'objectid' => 0,
]
);
$event->trigger(); $event->trigger();
} }
/** /**
* Call event when the response is received from the service * Call event when the response is received from the service
* *
* @param number $objectid Service ID * @param int $objectid Service ID
* @param array $response Server response * @param array $response Server response
*
* @throws \coding_exception
* @throws \dml_exception
*/ */
public static function response_answer($objectid = 0, $response = array()) { public static function response_answer($objectid = 0, array $response = []) {
$context = context_system::instance(); $context = context_system::instance();
$status = "Error sending request";
if (!empty($response["HTTP/1.1"])) { $status = get_string('errorsendingrequest', 'local_webhooks');
$status = $response["HTTP/1.1"]; if (!empty($response['HTTP/1.1'])) {
$status = $response['HTTP/1.1'];
} }
$event = local_webhooks\event\response_answer::create(array("context" => $context, "objectid" => $objectid, "other" => array("status" => $status))); $event = local_webhooks\event\response_answer::create(
[
'context' => $context,
'objectid' => $objectid,
'other' => ['status' => $status],
]
);
$event->trigger(); $event->trigger();
} }
/** /**
* Call the event when the service is added. * Call the event when the service is added.
* *
* @param number $objectid Service ID * @param int $objectid Service ID
*
* @throws \coding_exception
* @throws \dml_exception
*/ */
public static function service_added($objectid = 0) { public static function service_added($objectid = 0) {
$context = context_system::instance(); $context = context_system::instance();
$event = local_webhooks\event\service_added::create(array("context" => $context, "objectid" => $objectid));
$event = local_webhooks\event\service_added::create(
compact('context', 'objectid')
);
$event->trigger(); $event->trigger();
} }
/** /**
* Call the event when the service is deleted. * Call the event when the service is deleted.
* *
* @param number $objectid Service ID * @param int $objectid Service ID
*
* @throws \coding_exception
* @throws \dml_exception
*/ */
public static function service_deleted($objectid = 0) { public static function service_deleted($objectid = 0) {
$context = context_system::instance(); $context = context_system::instance();
$event = local_webhooks\event\service_deleted::create(array("context" => $context, "objectid" => $objectid));
$event->trigger();
}
/** $event = local_webhooks\event\service_deleted::create(
* Call the event when all services are deleted. compact('context', 'objectid')
*/ );
public static function service_deletedall() {
$context = context_system::instance();
$event = local_webhooks\event\service_deletedall::create(array("context" => $context, "objectid" => 0));
$event->trigger(); $event->trigger();
} }
/** /**
* Call event when the service is updated. * Call event when the service is updated.
* *
* @param number $objectid Service ID * @param int $objectid Service ID
*
* @throws \coding_exception
* @throws \dml_exception
*/ */
public static function service_updated($objectid = 0) { public static function service_updated($objectid = 0) {
$context = context_system::instance(); $context = context_system::instance();
$event = local_webhooks\event\service_updated::create(array("context" => $context, "objectid" => $objectid));
$event = local_webhooks\event\service_updated::create(
compact('context', 'objectid')
);
$event->trigger(); $event->trigger();
} }
} }
+12 -13
View File
@@ -22,18 +22,18 @@
* @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
*/ */
require_once(__DIR__ . "/../../config.php"); require_once(__DIR__ . '/../../config.php');
require_once(__DIR__ . "/classes/service_form.php"); require_once(__DIR__ . '/classes/service_form.php');
require_once(__DIR__ . "/lib.php"); require_once(__DIR__ . '/lib.php');
require_once($CFG->libdir . "/adminlib.php"); require_once($CFG->libdir . '/adminlib.php');
/* Link generation */ /* Link generation */
$baseurl = new moodle_url("/local/webhooks/restorebackup.php"); $baseurl = new moodle_url('/local/webhooks/restorebackup.php');
$managerservice = new moodle_url("/local/webhooks/index.php"); $managerservice = new moodle_url('/local/webhooks/index.php');
/* Configure the context of the page */ /* Configure the context of the page */
admin_externalpage_setup("local_webhooks", "", null, $baseurl, array()); admin_externalpage_setup('local_webhooks', '', null, $baseurl);
$context = context_system::instance(); $context = context_system::instance();
/* Create an editing form */ /* Create an editing form */
@@ -45,14 +45,14 @@ if ($mform->is_cancelled()) {
} }
/* Processing the received file */ /* Processing the received file */
if (($data = $mform->get_data()) && confirm_sesskey()) { if (!empty($data = $mform->get_data())) {
$content = $mform->get_file_content("backupfile"); $content = $mform->get_file_content('backupfile');
local_webhooks_restore_backup($content, $data->deleterecords); local_webhooks_restore_backup($content);
redirect($managerservice, new lang_string("restorefinished", "moodle")); redirect($managerservice, new lang_string('restorefinished', 'local_webhooks'));
} }
/* The page title */ /* The page title */
$titlepage = new lang_string("backup", "moodle"); $titlepage = new lang_string('backup', 'local_webhooks');
$PAGE->navbar->add($titlepage); $PAGE->navbar->add($titlepage);
$PAGE->set_heading($titlepage); $PAGE->set_heading($titlepage);
$PAGE->set_title($titlepage); $PAGE->set_title($titlepage);
@@ -61,5 +61,4 @@ echo $OUTPUT->header();
/* Displays the form */ /* Displays the form */
$mform->display(); $mform->display();
/* Footer */
echo $OUTPUT->footer(); echo $OUTPUT->footer();
+9 -5
View File
@@ -22,11 +22,15 @@
* @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
*/ */
defined("MOODLE_INTERNAL") || die(); defined('MOODLE_INTERNAL') || die();
if ($hassiteconfig) { if ($hassiteconfig) {
$ADMIN->add("server", new admin_externalpage("local_webhooks", $ADMIN->add(
new lang_string("pluginname", "local_webhooks"), 'server',
new moodle_url("/local/webhooks/index.php") new admin_externalpage(
)); 'local_webhooks',
new lang_string('pluginname', 'local_webhooks'),
new moodle_url('/local/webhooks/index.php')
)
);
} }
+91
View File
@@ -0,0 +1,91 @@
<?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/>.
/**
* Runtime autonomy tests.
*
* @package local_webhooks
* @copyright 2026 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_webhooks;
/**
* Runtime autonomy tests.
*/
#[\PHPUnit\Framework\Attributes\CoversFunction('local_webhooks_get_event_choices')]
final class autonomy_test extends \advanced_testcase {
/**
* Event choices keep their storage format and custom event names are strings.
*/
public function test_event_contracts(): void {
global $CFG;
require_once($CFG->dirroot . '/local/webhooks/lib.php');
require_once($CFG->dirroot . '/local/webhooks/classes/service_edit_form.php');
$debugsettings = [$CFG->debug, $CFG->debugdisplay, $CFG->debugdeveloper];
$choices = \local_webhooks_get_event_choices();
$this->assertNotEmpty($choices);
$this->assertSame($debugsettings, [$CFG->debug, $CFG->debugdisplay, $CFG->debugdeveloper]);
foreach ($choices as $component => $events) {
foreach ($events as $key => $eventname) {
$classname = ltrim($eventname, '\\');
$this->assertSame(base64_encode($eventname), $key);
$this->assertSame($component, \core_component::get_component_from_classname($classname));
$this->assertTrue(is_subclass_of($classname, \core\event\base::class));
$this->assertFalse((new \ReflectionClass($classname))->isAbstract());
$this->assertFalse($classname::is_deprecated());
}
}
$coreevent = '\\' . \core\event\user_created::class;
$localevent = '\\' . \local_webhooks\event\service_added::class;
$unknown = '\\' . \core\event\unknown_logged::class;
$deprecated = '\\' . \core\event\role_capabilities_updated::class;
$this->assertSame($coreevent, $choices['core'][base64_encode($coreevent)]);
$this->assertSame($localevent, $choices['local_webhooks'][base64_encode($localevent)]);
$this->assertArrayNotHasKey(base64_encode($unknown), $choices['core']);
$this->assertArrayNotHasKey(base64_encode($deprecated), $choices['core']);
$events = [
\local_webhooks\event\backup_performed::class => 'create',
\local_webhooks\event\backup_restored::class => 'update',
\local_webhooks\event\response_answer::class => 'answer',
\local_webhooks\event\service_added::class => 'create',
\local_webhooks\event\service_deleted::class => 'deleted',
\local_webhooks\event\service_updated::class => 'update',
];
foreach ($events as $event => $stringkey) {
$name = $event::get_name();
$this->assertIsString($name);
$this->assertNotSame('', trim($name));
$this->assertSame(get_string($stringkey, 'local_webhooks'), $name);
}
$this->assertFalse(class_exists('report_eventlist_list_generator', false));
$form = new \service_edit_form(new \moodle_url('/local/webhooks/editservice.php'));
$html = $form->render();
$this->assertStringContainsString($coreevent, $html);
$this->assertStringContainsString($localevent, $html);
$this->assertFalse(class_exists('report_eventlist_list_generator', false));
}
}
+6 -7
View File
@@ -22,11 +22,10 @@
* @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
*/ */
defined("MOODLE_INTERNAL") || die(); defined('MOODLE_INTERNAL') || die();
$plugin->release = "4.0.0-rc.2 (Build: 2018022500)"; $plugin->component = 'local_webhooks';
$plugin->version = 2018022500; $plugin->maturity = MATURITY_STABLE;
$plugin->requires = 2016112900; $plugin->release = '3.0.5 (Build: 2020051600)';
$plugin->component = "local_webhooks"; $plugin->requires = 2019052000;
$plugin->maturity = MATURITY_RC; $plugin->version = 2020051600;
$plugin->dependencies = array("report_eventlist" => 2016120500);