Compare commits
16
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
482d15db8f | ||
|
|
9d70f553bb | ||
|
|
b77e2a2cdb | ||
|
|
f89cdba127 | ||
|
|
1acd3ef21d | ||
|
|
3f74278add | ||
|
|
4e64c0bcd2 | ||
|
|
835f5516e8 | ||
|
|
833434c5ba | ||
|
|
8eda3bc6bb | ||
|
|
9cb5bd0760 | ||
|
|
2f04aeab6d | ||
|
|
849d7126d8 | ||
|
|
d8f3a9f3ed | ||
|
|
f9772b1fb9 | ||
|
|
3fa97bad31 |
+34
-12
@@ -4,13 +4,10 @@ sudo: true
|
||||
addons:
|
||||
firefox: "latest-esr"
|
||||
postgresql: "9.6"
|
||||
|
||||
apt:
|
||||
packages:
|
||||
- "mysql-client-5.6"
|
||||
- "mysql-client-core-5.6"
|
||||
- "mysql-server-5.6"
|
||||
- "openjdk-8-jre-headless"
|
||||
- "oracle-java8-installer"
|
||||
- "oracle-java8-set-default"
|
||||
|
||||
cache:
|
||||
directories:
|
||||
@@ -18,16 +15,16 @@ cache:
|
||||
- "$HOME/.npm"
|
||||
|
||||
php:
|
||||
- "7.0"
|
||||
- "7.1"
|
||||
- "7.2"
|
||||
- 7.0
|
||||
- 7.1
|
||||
- 7.2
|
||||
|
||||
env:
|
||||
global:
|
||||
- MOODLE_BRANCH=MOODLE_35_STABLE
|
||||
matrix:
|
||||
- MOODLE_BRANCH=MOODLE_35_STABLE DB=mysqli
|
||||
- MOODLE_BRANCH=MOODLE_35_STABLE DB=pgsql
|
||||
- MOODLE_BRANCH=MOODLE_36_STABLE DB=mysqli
|
||||
- MOODLE_BRANCH=MOODLE_36_STABLE DB=pgsql
|
||||
- DB=mysqli
|
||||
- DB=pgsql
|
||||
|
||||
before_install:
|
||||
- phpenv config-rm xdebug.ini
|
||||
@@ -52,3 +49,28 @@ script:
|
||||
- moodle-plugin-ci grunt
|
||||
- moodle-plugin-ci phpunit
|
||||
- moodle-plugin-ci behat
|
||||
|
||||
jobs:
|
||||
include:
|
||||
- stage: test old
|
||||
env: DB=mysqli MOODLE_BRANCH=MOODLE_33_STABLE
|
||||
php: 5.6
|
||||
|
||||
- stage: deploy
|
||||
php: 7.2
|
||||
|
||||
before_deploy:
|
||||
- cd "$TRAVIS_REPO_SLUG"
|
||||
- git stash --all
|
||||
- /bin/sh build.sh
|
||||
|
||||
deploy:
|
||||
api_key: $GITHUB_PROJECT_TOKEN
|
||||
email: "info@valentineus.link"
|
||||
file: "../build/*.zip"
|
||||
file_glob: true
|
||||
provider: releases
|
||||
skip_cleanup: true
|
||||
|
||||
on:
|
||||
tags: true
|
||||
@@ -20,11 +20,27 @@ Features:
|
||||
* Use a secret phrase to authenticate requests;
|
||||
* [JSON](https://en.wikipedia.org/wiki/JSON) - Format of outgoing data;
|
||||
|
||||
## Documentation
|
||||
## Installation
|
||||
|
||||
* [Install the plugin](docs/getting-started.md#installation).
|
||||
* [User guide](docs/getting-started.md#user-guide).
|
||||
* [Request format](docs/getting-started.md#request-format).
|
||||
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
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
|
||||
@@ -1,21 +1,32 @@
|
||||
#!/bin/sh
|
||||
# Author: Valentin Popov
|
||||
# Email: info@valentineus.link
|
||||
# Date: 2017-10-19
|
||||
# Usage: /bin/sh build.sh
|
||||
# Date: 2018-06-19
|
||||
# Usage: /bin/sh ./build.sh
|
||||
# Description: Build the final package for installation in Moodle.
|
||||
|
||||
# Updating the Environment
|
||||
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
||||
export PATH="$PATH:/usr/local/scripts"
|
||||
|
||||
# Build the package
|
||||
cd ..
|
||||
mv "./moodle-webhooks" "./local_webhooks"
|
||||
zip -9 -r "local_webhooks.zip" "local_webhooks" \
|
||||
-x "local_webhooks/.git*" \
|
||||
-x "local_webhooks/.travis.yml" \
|
||||
-x "local_webhooks/build.sh"
|
||||
# Current project
|
||||
PROJECT="local_webhooks"
|
||||
|
||||
# Defining directories
|
||||
DIRECTORY="$(pwd)"
|
||||
NAMEDIR="$(basename $DIRECTORY)"
|
||||
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
|
||||
exit 0
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
use core\event\base;
|
||||
use lang_string;
|
||||
use moodle_url;
|
||||
|
||||
/**
|
||||
* 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 backup_performed extends base {
|
||||
/**
|
||||
* Return localised event name.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public static function get_name() {
|
||||
return new lang_string('create', 'moodle');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of what happened.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public function get_description() {
|
||||
return new lang_string('backup', 'moodle');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL related to the action.
|
||||
*
|
||||
* @throws \moodle_exception
|
||||
*/
|
||||
public function get_url() {
|
||||
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';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
use core\event\base;
|
||||
use lang_string;
|
||||
use moodle_url;
|
||||
|
||||
/**
|
||||
* 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 backup_restored extends base {
|
||||
/**
|
||||
* Return localised event name.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public static function get_name() {
|
||||
return new lang_string('update', 'moodle');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of what happened.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public function get_description() {
|
||||
return new lang_string('backupfinished', 'moodle');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL related to the action.
|
||||
*
|
||||
* @throws \moodle_exception
|
||||
*/
|
||||
public function get_url() {
|
||||
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';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?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();
|
||||
|
||||
use core\event\base;
|
||||
use lang_string;
|
||||
use moodle_url;
|
||||
|
||||
/**
|
||||
* Defines how to work with events.
|
||||
*
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class response_answer extends base {
|
||||
/**
|
||||
* Return localised event name.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public static function get_name() {
|
||||
return new lang_string('answer', 'moodle');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of what happened.
|
||||
*/
|
||||
public function get_description() {
|
||||
return $this->other['status'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL related to the action.
|
||||
*
|
||||
* @throws \moodle_exception
|
||||
*/
|
||||
public function get_url() {
|
||||
return new moodle_url('/local/webhooks/editservice.php', array('serviceid' => $this->objectid));
|
||||
}
|
||||
|
||||
/**
|
||||
* Init method.
|
||||
*/
|
||||
protected function init() {
|
||||
$this->data['crud'] = 'r';
|
||||
$this->data['edulevel'] = self::LEVEL_OTHER;
|
||||
$this->data['objecttable'] = 'local_webhooks_service';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
use core\event\base;
|
||||
use lang_string;
|
||||
use moodle_url;
|
||||
|
||||
/**
|
||||
* 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_added extends base {
|
||||
/**
|
||||
* Return localised event name.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public static function get_name() {
|
||||
return new lang_string('create', 'moodle');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of what happened.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public function get_description() {
|
||||
return new lang_string('eventwebserviceservicecreated', 'webservice');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL related to the action.
|
||||
*
|
||||
* @throws \moodle_exception
|
||||
*/
|
||||
public function get_url() {
|
||||
return new moodle_url('/local/webhooks/editservice.php', array('serviceid' => $this->objectid));
|
||||
}
|
||||
|
||||
/**
|
||||
* Init method.
|
||||
*/
|
||||
protected function init() {
|
||||
$this->data['crud'] = 'c';
|
||||
$this->data['edulevel'] = self::LEVEL_OTHER;
|
||||
$this->data['objecttable'] = 'local_webhooks_service';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
use core\event\base;
|
||||
use lang_string;
|
||||
use moodle_url;
|
||||
|
||||
/**
|
||||
* 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_deleted extends base {
|
||||
/**
|
||||
* Return localised event name.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public static function get_name() {
|
||||
return new lang_string('deleted', 'moodle');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of what happened.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public function get_description() {
|
||||
return new lang_string('eventwebserviceservicedeleted', 'webservice');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL related to the action.
|
||||
*
|
||||
* @throws \moodle_exception
|
||||
*/
|
||||
public function get_url() {
|
||||
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';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
use core\event\base;
|
||||
use lang_string;
|
||||
use moodle_url;
|
||||
|
||||
/**
|
||||
* 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_updated extends base {
|
||||
/**
|
||||
* Return localised event name.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public static function get_name() {
|
||||
return new lang_string('update', 'moodle');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of what happened.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public function get_description() {
|
||||
return new lang_string('eventwebserviceserviceupdated', 'webservice');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL related to the action.
|
||||
*
|
||||
* @throws \moodle_exception
|
||||
*/
|
||||
public function get_url() {
|
||||
return new moodle_url('/local/webhooks/editservice.php', array('serviceid' => $this->objectid));
|
||||
}
|
||||
|
||||
/**
|
||||
* Init method.
|
||||
*/
|
||||
protected function init() {
|
||||
$this->data['crud'] = 'u';
|
||||
$this->data['edulevel'] = self::LEVEL_OTHER;
|
||||
$this->data['objecttable'] = 'local_webhooks_service';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* The event handler.
|
||||
*
|
||||
* @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;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
|
||||
require_once(__DIR__ . '/../lib.php');
|
||||
require_once(__DIR__ . '/../locallib.php');
|
||||
|
||||
require_once($CFG->libdir . '/filelib.php');
|
||||
|
||||
use curl;
|
||||
use local_webhooks_events;
|
||||
|
||||
/**
|
||||
* 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 handler {
|
||||
/**
|
||||
* External handler.
|
||||
*
|
||||
* @param object $event
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public static function events($event) {
|
||||
$data = $event->get_data();
|
||||
|
||||
if (!empty($callbacks = local_webhooks_get_list_records())) {
|
||||
foreach ($callbacks as $callback) {
|
||||
self::handler_callback($data, $callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(array('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;
|
||||
}
|
||||
}
|
||||
@@ -1,361 +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/>.
|
||||
|
||||
namespace local_webhooks\local;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
define('LW_TABLE_EVENTS', 'local_webhooks_events');
|
||||
define('LW_TABLE_SERVICES', 'local_webhooks_service');
|
||||
|
||||
global $CFG;
|
||||
|
||||
require_once($CFG->dirroot . '/local/webhooks/classes/local/record.php');
|
||||
|
||||
use coding_exception;
|
||||
use core_component;
|
||||
use ReflectionClass;
|
||||
use function define;
|
||||
use function defined;
|
||||
use function is_array;
|
||||
use function is_int;
|
||||
use function is_object;
|
||||
use function is_string;
|
||||
use function strlen;
|
||||
|
||||
/**
|
||||
* The main class for the plugin.
|
||||
*
|
||||
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package local_webhooks\local
|
||||
*/
|
||||
final class api {
|
||||
/**
|
||||
* Create a new record in the database.
|
||||
*
|
||||
* @param \local_webhooks\local\record $record
|
||||
*
|
||||
* @return int
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public static function add_service(record $record): int {
|
||||
global $DB;
|
||||
|
||||
$id = $DB->insert_record(LW_TABLE_SERVICES, $record);
|
||||
|
||||
if (is_int($id) && is_array($record->events)) {
|
||||
$DB->insert_records(LW_TABLE_EVENTS, array_map(static function (string $name) use ($id) {
|
||||
return ['name' => $name, 'serviceid' => $id];
|
||||
}, $record->events));
|
||||
}
|
||||
|
||||
if (!is_int($id)) {
|
||||
throw new coding_exception('Variable \'id\' must be type \'Integer\'');
|
||||
}
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an existing record in the database.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public static function del_service(int $id): bool {
|
||||
global $DB;
|
||||
|
||||
$DB->delete_records(LW_TABLE_EVENTS, ['serviceid' => $id]);
|
||||
|
||||
return $DB->delete_records(LW_TABLE_SERVICES, ['id' => $id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an event's list.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public static function get_events(): array {
|
||||
return array_merge(self::get_core_events_list(), self::get_non_core_event_list());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an existing record from the database.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \local_webhooks\local\record
|
||||
*
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public static function get_service(int $id): record {
|
||||
global $DB;
|
||||
|
||||
$record = $DB->get_record(LW_TABLE_SERVICES, ['id' => $id], '*', MUST_EXIST);
|
||||
$events = $DB->get_records(LW_TABLE_EVENTS, ['serviceid' => $id]);
|
||||
|
||||
$service = new record();
|
||||
$service->events = array_column($events, 'name');
|
||||
$service->header = $record->header;
|
||||
$service->id = $record->id;
|
||||
$service->name = $record->name;
|
||||
$service->point = $record->point;
|
||||
$service->status = $record->status;
|
||||
$service->token = $record->token;
|
||||
|
||||
return $service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list records from the database.
|
||||
*
|
||||
* @param array|null $conditions
|
||||
* @param string|null $sort
|
||||
* @param int|null $from
|
||||
* @param int|null $limit
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public static function get_services(array $conditions = null, string $sort = null, int $from = null, int $limit = null): array {
|
||||
global $DB;
|
||||
|
||||
$records = $DB->get_records(LW_TABLE_SERVICES, $conditions ?? [], $sort ?? '', '*', $from ?? 0, $limit ?? 0);
|
||||
|
||||
$services = [];
|
||||
foreach ($records as $record) {
|
||||
if (!is_object($record)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$events = $DB->get_records(LW_TABLE_EVENTS, ['serviceid' => $record->id]);
|
||||
|
||||
$service = new record();
|
||||
$service->events = array_column($events, 'name');
|
||||
$service->header = $record->header;
|
||||
$service->id = $record->id;
|
||||
$service->name = $record->name;
|
||||
$service->point = $record->point;
|
||||
$service->status = $record->status;
|
||||
$service->token = $record->token;
|
||||
|
||||
$services[] = $service;
|
||||
}
|
||||
|
||||
return $services;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list records from the database by the event's name.
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return \local_webhooks\local\record[]
|
||||
*
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public static function get_services_by_event(string $name): array {
|
||||
global $DB;
|
||||
|
||||
$events = $DB->get_records(LW_TABLE_EVENTS, ['name' => $name]);
|
||||
|
||||
$services = [];
|
||||
foreach ($events as $event) {
|
||||
if (!is_object($event)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$services[] = self::get_service($event->serviceid);
|
||||
}
|
||||
|
||||
return $services;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a total count of existing records.
|
||||
*
|
||||
* @return int
|
||||
*
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public static function get_total_count(): int {
|
||||
global $DB;
|
||||
|
||||
return $DB->count_records(LW_TABLE_SERVICES);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing record in the database.
|
||||
*
|
||||
* @param \local_webhooks\local\record $service
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public static function set_service(record $service): bool {
|
||||
global $DB;
|
||||
|
||||
$result = $DB->update_record(LW_TABLE_SERVICES, $service);
|
||||
$DB->delete_records(LW_TABLE_EVENTS, ['serviceid' => $service->id]);
|
||||
|
||||
if ($result && is_array($service->events)) {
|
||||
$DB->insert_records(LW_TABLE_EVENTS, array_map(static function (string $name) use ($service) {
|
||||
return ['name' => $name, 'serviceid' => $service->id];
|
||||
}, $service->events));
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a system's events list.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
private static function get_core_events_list(): array {
|
||||
global $CFG;
|
||||
|
||||
$debugdeveloper = $CFG->debugdeveloper;
|
||||
$debugdisplay = $CFG->debugdisplay;
|
||||
$debuglevel = $CFG->debug;
|
||||
|
||||
$CFG->debug = 0;
|
||||
$CFG->debugdeveloper = false;
|
||||
$CFG->debugdisplay = false;
|
||||
|
||||
$directory = $CFG->libdir . '/classes/event';
|
||||
$events = [];
|
||||
$files = self::get_file_list($directory);
|
||||
|
||||
if (isset($files['unknown_logged'])) {
|
||||
unset($files['unknown_logged']);
|
||||
}
|
||||
|
||||
foreach (array_keys($files) as $file) {
|
||||
$name = '\\core\\event\\' . $file;
|
||||
|
||||
if (method_exists($name, 'get_static_info')) {
|
||||
$class = new ReflectionClass($name);
|
||||
|
||||
if ($file !== 'manager' && !$class->isAbstract()) {
|
||||
$events[$name] = $name::get_static_info();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$CFG->debug = $debuglevel;
|
||||
$CFG->debugdeveloper = $debugdeveloper;
|
||||
$CFG->debugdisplay = $debugdisplay;
|
||||
|
||||
return $events;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a file's list in the directory.
|
||||
*
|
||||
* @param string $directory
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private static function get_file_list(string $directory): array {
|
||||
global $CFG;
|
||||
|
||||
$files = [];
|
||||
$root = $CFG->dirroot;
|
||||
|
||||
if (is_dir($directory) && is_readable($directory)) {
|
||||
$handle = opendir($directory);
|
||||
|
||||
if ($handle) {
|
||||
foreach (scandir($directory, SCANDIR_SORT_NONE) as $file) {
|
||||
if ($file !== '.' && $file !== '..' && strrpos($directory, $root) !== false) {
|
||||
$location = substr($directory, strlen($root));
|
||||
$eventname = substr($file, 0, -4);
|
||||
|
||||
if (is_string($eventname)) {
|
||||
$files[$eventname] = $location . '/' . $file;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a plugins' events list.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
private static function get_non_core_event_list(): array {
|
||||
global $CFG;
|
||||
|
||||
$debugdeveloper = $CFG->debugdeveloper;
|
||||
$debugdisplay = $CFG->debugdisplay;
|
||||
$debuglevel = $CFG->debug;
|
||||
|
||||
$CFG->debug = 0;
|
||||
$CFG->debugdeveloper = false;
|
||||
$CFG->debugdisplay = false;
|
||||
|
||||
$events = [];
|
||||
|
||||
foreach (array_keys(core_component::get_plugin_types()) as $type) {
|
||||
foreach (core_component::get_plugin_list($type) as $plugin => $directory) {
|
||||
$directory .= '/classes/event';
|
||||
$files = self::get_file_list($directory);
|
||||
|
||||
if (isset($files['unknown_logged'])) {
|
||||
unset($files['unknown_logged']);
|
||||
}
|
||||
|
||||
foreach (array_keys($files) as $file) {
|
||||
$name = '\\' . $type . '_' . $plugin . '\\event\\' . $file;
|
||||
|
||||
if (method_exists($name, 'get_static_info')) {
|
||||
$class = new ReflectionClass($name);
|
||||
|
||||
if ($type . '_' . $plugin !== 'logstore_legacy' && !$class->isAbstract()) {
|
||||
$events[$name] = $name::get_static_info();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$CFG->debug = $debuglevel;
|
||||
$CFG->debugdeveloper = $debugdeveloper;
|
||||
$CFG->debugdisplay = $debugdisplay;
|
||||
|
||||
return $events;
|
||||
}
|
||||
}
|
||||
@@ -1,95 +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/>.
|
||||
|
||||
namespace local_webhooks\local;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
use stdClass;
|
||||
use function defined;
|
||||
|
||||
/**
|
||||
* It's a class description record.
|
||||
*
|
||||
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package local_webhooks\local
|
||||
*/
|
||||
final class record extends stdClass {
|
||||
/**
|
||||
* List of some events.
|
||||
*
|
||||
* @var array|null
|
||||
*/
|
||||
public $events;
|
||||
|
||||
/**
|
||||
* Type of the package.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $header;
|
||||
|
||||
/**
|
||||
* A unique identifier of the service.
|
||||
*
|
||||
* @var int|null
|
||||
*/
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* Name of the service.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* Url's an endpoint to send notifications.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $point;
|
||||
|
||||
/**
|
||||
* Status activity of the service.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $status;
|
||||
|
||||
/**
|
||||
* Secret key of the service.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $token;
|
||||
|
||||
/**
|
||||
* Classes constructor.
|
||||
*
|
||||
* @param array|null $conditions
|
||||
*/
|
||||
public function __construct(array $conditions = null) {
|
||||
$this->events = $conditions['events'] ?? null;
|
||||
$this->header = $conditions['header'] ?? null;
|
||||
$this->id = $conditions['id'] ?? null;
|
||||
$this->name = $conditions['name'] ?? null;
|
||||
$this->point = $conditions['point'] ?? null;
|
||||
$this->status = $conditions['status'] ?? null;
|
||||
$this->token = $conditions['token'] ?? null;
|
||||
}
|
||||
}
|
||||
@@ -1,184 +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/>.
|
||||
|
||||
namespace local_webhooks\output;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
|
||||
require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php');
|
||||
require_once($CFG->libdir . '/tablelib.php');
|
||||
|
||||
use html_writer;
|
||||
use lang_string;
|
||||
use local_webhooks\local\api;
|
||||
use moodle_url;
|
||||
use pix_icon;
|
||||
use stdClass;
|
||||
use table_sql;
|
||||
use function defined;
|
||||
use function is_int;
|
||||
|
||||
/**
|
||||
* Class table for list services.
|
||||
*
|
||||
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package local_webhooks\output
|
||||
*/
|
||||
final class services_table extends table_sql {
|
||||
/**
|
||||
* URL editor page.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private static $editorpage = '/local/webhooks/service.php';
|
||||
|
||||
/**
|
||||
* URL main page.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private static $mainpage = '/local/webhooks/index.php';
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param string $uniqueid
|
||||
*
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public function __construct(string $uniqueid) {
|
||||
parent::__construct($uniqueid);
|
||||
|
||||
$this->define_table_columns();
|
||||
$this->define_table_configs();
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the action column.
|
||||
*
|
||||
* @param \stdClass $row
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \moodle_exception
|
||||
*/
|
||||
public function col_actions(stdClass $row): string {
|
||||
global $OUTPUT;
|
||||
|
||||
$deletelink = new moodle_url(self::$mainpage, ['deleteid' => $row->id, 'sesskey' => sesskey()]);
|
||||
$deleteitem = $OUTPUT->action_icon($deletelink, new pix_icon('t/delete', new lang_string('delete', 'moodle')));
|
||||
|
||||
$editlink = new moodle_url(self::$editorpage, ['serviceid' => $row->id, 'sesskey' => sesskey()]);
|
||||
$edititem = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', new lang_string('edit', 'moodle')));
|
||||
|
||||
$hideshowlink = new moodle_url(self::$mainpage, ['hideshowid' => $row->id, 'sesskey' => sesskey()]);
|
||||
$hideshowitem = $OUTPUT->action_icon($hideshowlink, new pix_icon($row->status ? 't/hide' : 't/show', $row->status
|
||||
? new lang_string('disable', 'moodle')
|
||||
: new lang_string('enable', 'moodle')
|
||||
));
|
||||
|
||||
return $hideshowitem . $edititem . $deleteitem;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the events column.
|
||||
*
|
||||
* @param \stdClass $row
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function col_events(stdClass $row): int {
|
||||
$total = count($row->events);
|
||||
|
||||
return is_int($total) ? $total : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the name column.
|
||||
*
|
||||
* @param \stdClass $row
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws \moodle_exception
|
||||
*/
|
||||
public function col_name(stdClass $row): string {
|
||||
$link = new moodle_url(self::$editorpage, [
|
||||
'serviceid' => $row->id,
|
||||
'sesskey' => sesskey(),
|
||||
]);
|
||||
|
||||
return html_writer::link($link, $row->name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Config table's columns.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public function define_table_columns() {
|
||||
$this->define_columns([
|
||||
'name',
|
||||
'point',
|
||||
'events',
|
||||
'actions',
|
||||
]);
|
||||
|
||||
$this->define_headers([
|
||||
new lang_string('name', 'moodle'),
|
||||
new lang_string('url', 'moodle'),
|
||||
new lang_string('edulevel', 'moodle'),
|
||||
new lang_string('actions', 'moodle'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Config table.
|
||||
*/
|
||||
public function define_table_configs() {
|
||||
$this->collapsible(false);
|
||||
$this->is_downloadable(false);
|
||||
$this->no_sorting('actions');
|
||||
$this->no_sorting('events');
|
||||
$this->pageable(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* The query for the database.
|
||||
*
|
||||
* @param int $pagesize
|
||||
* @param bool $useinitialsbar
|
||||
*
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public function query_db($pagesize, $useinitialsbar = true) {
|
||||
$this->pagesize($pagesize, $pagesize + 1);
|
||||
|
||||
$sort = (string) $this->get_sql_sort();
|
||||
$this->rawdata = api::get_services(null, $sort, $this->get_page_start(), $this->get_page_size());
|
||||
|
||||
$total = api::get_total_count();
|
||||
$this->pagesize($pagesize, $total);
|
||||
|
||||
if ($useinitialsbar) {
|
||||
$this->initialbars($total > $pagesize);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,31 +14,29 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace local_webhooks\event;
|
||||
/**
|
||||
* 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;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
use core\event\base as event;
|
||||
use core\task\manager;
|
||||
use local_webhooks\task\notify;
|
||||
use function defined;
|
||||
class provider implements
|
||||
// This plugin does not store any personal user data.
|
||||
\core_privacy\local\metadata\null_provider {
|
||||
|
||||
/**
|
||||
* Class observer.
|
||||
*
|
||||
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package local_webhooks\event
|
||||
*/
|
||||
final class observer {
|
||||
/**
|
||||
* Main handler for events.
|
||||
* Get the language string identifier with the component's language
|
||||
* file to explain why this plugin stores no data.
|
||||
*
|
||||
* @param \core\event\base $event
|
||||
* @return string
|
||||
*/
|
||||
public static function handler(event $event) {
|
||||
$task = new notify();
|
||||
$task->set_custom_data($event->get_data());
|
||||
manager::queue_adhoc_task($task);
|
||||
public static function get_reason() : string {
|
||||
return 'privacy:metadata';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
<?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 forms.
|
||||
*
|
||||
* @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 of the form of restoration.
|
||||
*
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class service_backup_form extends moodleform {
|
||||
/**
|
||||
* Defines the standard structure of the form.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
protected function definition() {
|
||||
$mform =& $this->_form;
|
||||
|
||||
/* Form heading */
|
||||
$mform->addElement('header', 'editserviceheader', new lang_string('restore', 'moodle'));
|
||||
|
||||
/* Download the file */
|
||||
$mform->addElement('filepicker', 'backupfile', new lang_string('file', 'moodle'));
|
||||
$mform->addRule('backupfile', null, 'required');
|
||||
|
||||
/* Control Panel */
|
||||
$this->add_action_buttons(true, new lang_string('restore', 'moodle'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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', '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 = [
|
||||
'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 = report_eventlist_list_generator::get_all_events_list();
|
||||
$events = [];
|
||||
|
||||
/* Formation of the list of elements */
|
||||
foreach ($eventlist as $event) {
|
||||
/* Escaping event names */
|
||||
$eventname = base64_encode($event['eventname']);
|
||||
$events[$event['component']][] =& $mform->createElement('checkbox', $eventname, $event['eventname']);
|
||||
}
|
||||
|
||||
/* Displays groups of items */
|
||||
foreach ($events as $key => $event) {
|
||||
$mform->addGroup($event, 'events', $key, '<br />');
|
||||
}
|
||||
|
||||
/* Control Panel */
|
||||
$this->add_action_buttons();
|
||||
}
|
||||
}
|
||||
@@ -1,90 +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/>.
|
||||
|
||||
namespace local_webhooks\task;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
|
||||
require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php');
|
||||
|
||||
use core\task\adhoc_task;
|
||||
use curl;
|
||||
use local_webhooks\local\api;
|
||||
use local_webhooks\local\record;
|
||||
use function defined;
|
||||
use function is_object;
|
||||
use function is_string;
|
||||
|
||||
/**
|
||||
* Class for processing events.
|
||||
*
|
||||
* @package local_webhooks\task
|
||||
*/
|
||||
final class notify extends adhoc_task {
|
||||
/**
|
||||
* Debug information.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $debug;
|
||||
|
||||
/**
|
||||
* Process an event.
|
||||
*
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public function execute() {
|
||||
$event = $this->get_custom_data();
|
||||
|
||||
if (!is_object($event) || !isset($event->eventname) || !is_string($event->eventname)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (api::get_services_by_event($event->eventname) as $service) {
|
||||
if (!is_object($service)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$service->status) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->post($service, array_merge((array) $event, [
|
||||
'token' => $service->token,
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a request for the service.
|
||||
*
|
||||
* @param \local_webhooks\local\record $service
|
||||
* @param array $data
|
||||
*/
|
||||
public function post(record $service, array $data) {
|
||||
$curl = new curl();
|
||||
$curl->setHeader(['Content-Type: ' . $service->header]);
|
||||
$curl->post($service->point, json_encode($data));
|
||||
|
||||
if (defined('PHPUNIT_TEST') && PHPUNIT_TEST) {
|
||||
$this->debug = array_merge($this->debug ?? [], [
|
||||
compact('data', 'service'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,105 +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/>.
|
||||
|
||||
/**
|
||||
* Defines forms.
|
||||
*
|
||||
* @copyright 2018 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package local_webhooks
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once($CFG->dirroot . '/local/webhooks/lib.php');
|
||||
require_once($CFG->libdir . '/formslib.php');
|
||||
|
||||
/**
|
||||
* Description editing form definition.
|
||||
*
|
||||
* @copyright 2018 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package local_webhooks
|
||||
*/
|
||||
class local_webhooks_service_edit_form extends moodleform {
|
||||
/**
|
||||
* @param string $baseurl
|
||||
*/
|
||||
public function __construct($baseurl) {
|
||||
parent::__construct($baseurl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the standard structure of the form.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
protected function definition() {
|
||||
$mform =& $this->_form;
|
||||
$size = array('size' => 60);
|
||||
|
||||
/* Form heading */
|
||||
$mform->addElement('header', 'edit-service-header-main', new lang_string('service', 'webservice'));
|
||||
|
||||
/* Name of the service */
|
||||
$mform->addElement('text', 'name', new lang_string('name', 'moodle'), $size);
|
||||
$mform->addRule('name', null, 'required');
|
||||
$mform->setType('name', PARAM_RAW);
|
||||
|
||||
/* Callback address */
|
||||
$mform->addElement('text', 'point', new lang_string('url', 'moodle'), $size);
|
||||
$mform->addRule('point', null, 'required');
|
||||
$mform->setType('point', PARAM_URL);
|
||||
|
||||
/* Enabling the service */
|
||||
$mform->addElement('advcheckbox', 'status', new lang_string('enable', 'moodle'));
|
||||
$mform->setType('status', PARAM_BOOL);
|
||||
$mform->setDefault('status', 1);
|
||||
$mform->setAdvanced('status');
|
||||
|
||||
/* Token */
|
||||
$mform->addElement('text', 'token', new lang_string('token', 'webservice'), $size);
|
||||
$mform->addRule('token', null, 'required');
|
||||
$mform->setType('token', PARAM_RAW);
|
||||
|
||||
/* Content type */
|
||||
$contenttype = array(
|
||||
'application/json' => 'application/json',
|
||||
'application/x-www-form-urlencoded' => 'application/x-www-form-urlencoded',
|
||||
);
|
||||
|
||||
$mform->addElement('select', 'header', 'Content-Type', $contenttype);
|
||||
$mform->setAdvanced('header');
|
||||
|
||||
/* Form heading */
|
||||
$mform->addElement('header', 'edit-service-header-event', new lang_string('edulevel', 'moodle'));
|
||||
|
||||
/* List of events */
|
||||
$eventlist = report_eventlist_list_generator::get_all_events_list(true);
|
||||
|
||||
$events = array();
|
||||
foreach ($eventlist as $event) {
|
||||
$events[$event['component']][] =& $mform->createElement('checkbox', $event['eventname'], $event['eventname']);
|
||||
}
|
||||
|
||||
foreach ($events as $key => $event) {
|
||||
$mform->addGroup($event, 'events', $key, '<br />', true);
|
||||
}
|
||||
|
||||
/* Control Panel */
|
||||
$this->add_action_buttons(true);
|
||||
}
|
||||
}
|
||||
+12
-8
@@ -15,17 +15,21 @@
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Registration of observers for events.
|
||||
* Interception of all events in the system.
|
||||
*
|
||||
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
|
||||
* @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();
|
||||
|
||||
$observers = [
|
||||
[
|
||||
'callback' => '\local_webhooks\event\observer::handler',
|
||||
'eventname' => '*',
|
||||
],
|
||||
];
|
||||
$observers = array(
|
||||
array(
|
||||
'callback' => '\local_webhooks\handler::events',
|
||||
'eventname' => '*',
|
||||
'includefile' => null,
|
||||
'internal' => true,
|
||||
'priority' => 200,
|
||||
),
|
||||
);
|
||||
+10
-21
@@ -1,33 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<XMLDB PATH="local/webhooks/db" VERSION="2018090700" COMMENT="XMLDB file for Moodle local/webhooks"
|
||||
<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>
|
||||
<TABLE NAME="local_webhooks_service" COMMENT="Storing created services.">
|
||||
<TABLE NAME="local_webhooks_service" COMMENT="A table for storing callback services.">
|
||||
<FIELDS>
|
||||
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" />
|
||||
<FIELD NAME="header" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" DEFAULT="application/json" COMMENT="Type of outgoing header." />
|
||||
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="Name of the service." />
|
||||
<FIELD NAME="point" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="Point of delivery of notifications." />
|
||||
<FIELD NAME="status" TYPE="int" LENGTH="1" NOTNULL="true" SEQUENCE="false" DEFAULT="0" COMMENT="Current status of the service." />
|
||||
<FIELD NAME="token" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="Token for verification of requests." />
|
||||
<FIELD NAME="enable" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" />
|
||||
<FIELD NAME="title" TYPE="text" NOTNULL="true" SEQUENCE="false" />
|
||||
<FIELD NAME="url" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" />
|
||||
<FIELD NAME="type" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" />
|
||||
<FIELD NAME="token" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" />
|
||||
<FIELD NAME="events" TYPE="text" NOTNULL="false" SEQUENCE="false" />
|
||||
<FIELD NAME="other" TYPE="text" NOTNULL="false" SEQUENCE="false" />
|
||||
</FIELDS>
|
||||
<KEYS>
|
||||
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
|
||||
</KEYS>
|
||||
</TABLE>
|
||||
|
||||
<TABLE NAME="local_webhooks_events" COMMENT="Storing of viewed events.">
|
||||
<FIELDS>
|
||||
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" />
|
||||
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="Name of the service." />
|
||||
<FIELD NAME="serviceid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Service ID." />
|
||||
</FIELDS>
|
||||
<KEYS>
|
||||
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
|
||||
</KEYS>
|
||||
</TABLE>
|
||||
|
||||
</TABLES>
|
||||
</XMLDB>
|
||||
</XMLDB>
|
||||
@@ -1,74 +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.
|
||||
*
|
||||
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$functions = [
|
||||
'local_webhooks_add_service' => [
|
||||
'classname' => 'local_webhooks_external',
|
||||
'classpath' => 'local/webhooks/externallib.php',
|
||||
'description' => 'Add a new service.',
|
||||
'methodname' => 'add_service',
|
||||
'type' => 'write',
|
||||
],
|
||||
|
||||
'local_webhooks_del_service' => [
|
||||
'classname' => 'local_webhooks_external',
|
||||
'classpath' => 'local/webhooks/externallib.php',
|
||||
'description' => 'Delete the existing service.',
|
||||
'methodname' => 'del_service',
|
||||
'type' => 'write',
|
||||
],
|
||||
|
||||
'local_webhooks_get_events' => [
|
||||
'classname' => 'local_webhooks_external',
|
||||
'classpath' => 'local/webhooks/externallib.php',
|
||||
'description' => 'Get the event\'s list.',
|
||||
'methodname' => 'get_events',
|
||||
'type' => 'read',
|
||||
],
|
||||
|
||||
'local_webhooks_get_service' => [
|
||||
'classname' => 'local_webhooks_external',
|
||||
'classpath' => 'local/webhooks/externallib.php',
|
||||
'description' => 'Get data by service.',
|
||||
'methodname' => 'get_service',
|
||||
'type' => 'read',
|
||||
],
|
||||
|
||||
'local_webhooks_get_services' => [
|
||||
'classname' => 'local_webhooks_external',
|
||||
'classpath' => 'local/webhooks/externallib.php',
|
||||
'description' => 'Get the service\'s list.',
|
||||
'methodname' => 'get_services',
|
||||
'type' => 'read',
|
||||
],
|
||||
|
||||
'local_webhooks_set_service' => [
|
||||
'classname' => 'local_webhooks_external',
|
||||
'classpath' => 'local/webhooks/externallib.php',
|
||||
'description' => 'Update the existing service.',
|
||||
'methodname' => 'set_service',
|
||||
'type' => 'write',
|
||||
],
|
||||
];
|
||||
@@ -1,28 +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/>.
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Function to upgrade 'local_webhooks'.
|
||||
*
|
||||
* @param int $oldversion
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function xmldb_local_webhooks_upgrade(int $oldversion) {
|
||||
return true;
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Page for editing the service.
|
||||
*
|
||||
* @package local_webhooks
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
require_once(__DIR__ . '/../../config.php');
|
||||
require_once(__DIR__ . '/classes/service_form.php');
|
||||
require_once(__DIR__ . '/lib.php');
|
||||
|
||||
require_once($CFG->libdir . '/adminlib.php');
|
||||
|
||||
/* Optional parameters */
|
||||
$serviceid = optional_param('serviceid', 0, PARAM_INT);
|
||||
|
||||
/* Link generation */
|
||||
$urlparameters = array('serviceid' => $serviceid);
|
||||
$baseurl = new moodle_url('/local/webhooks/editservice.php', $urlparameters);
|
||||
$managerservice = new moodle_url('/local/webhooks/index.php');
|
||||
|
||||
/* Configure the context of the page */
|
||||
admin_externalpage_setup('local_webhooks', '', null, $baseurl);
|
||||
$context = context_system::instance();
|
||||
|
||||
/* Create an editing form */
|
||||
$mform = new service_edit_form($PAGE->url);
|
||||
|
||||
/* Cancel processing */
|
||||
if ($mform->is_cancelled()) {
|
||||
redirect($managerservice);
|
||||
}
|
||||
|
||||
/* Getting the data */
|
||||
$servicerecord = new stdClass();
|
||||
if (($editing = (bool) $serviceid) === true) {
|
||||
$servicerecord = local_webhooks_get_record($serviceid);
|
||||
|
||||
if (is_array($servicerecord->events)) {
|
||||
$events = array();
|
||||
|
||||
/* Escaping event names */
|
||||
foreach ($servicerecord->events as $eventname => $eventstatus) {
|
||||
$eventname = base64_encode($eventname);
|
||||
$events[$eventname] = $eventstatus;
|
||||
}
|
||||
|
||||
$servicerecord->events = $events;
|
||||
}
|
||||
|
||||
$mform->set_data($servicerecord);
|
||||
}
|
||||
|
||||
/* Processing of received data */
|
||||
if (!empty($data = $mform->get_data())) {
|
||||
if (is_array($data->events)) {
|
||||
$events = array();
|
||||
|
||||
/* Deciphering event names */
|
||||
foreach ($data->events as $eventname => $eventstatus) {
|
||||
$eventname = (string) base64_decode($eventname);
|
||||
$events[$eventname] = $eventstatus;
|
||||
}
|
||||
|
||||
$data->events = $events;
|
||||
}
|
||||
|
||||
if ($editing) {
|
||||
$data->id = $serviceid;
|
||||
local_webhooks_update_record($data, false);
|
||||
redirect($managerservice, new lang_string('eventwebserviceserviceupdated', 'webservice'));
|
||||
} else {
|
||||
local_webhooks_update_record($data);
|
||||
redirect($managerservice, new lang_string('eventwebserviceservicecreated', 'webservice'));
|
||||
}
|
||||
}
|
||||
|
||||
/* The page title */
|
||||
$titlepage = new lang_string('externalservice', 'webservice');
|
||||
$PAGE->navbar->add($titlepage);
|
||||
$PAGE->set_heading($titlepage);
|
||||
$PAGE->set_title($titlepage);
|
||||
echo $OUTPUT->header();
|
||||
|
||||
/* Displays the form */
|
||||
$mform->display();
|
||||
|
||||
echo $OUTPUT->footer();
|
||||
-366
@@ -1,366 +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/>.
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
|
||||
require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php');
|
||||
require_once($CFG->libdir . '/externallib.php');
|
||||
|
||||
use local_webhooks\local\api;
|
||||
use local_webhooks\local\record;
|
||||
|
||||
/**
|
||||
* WebHooks external functions.
|
||||
*
|
||||
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
final class local_webhooks_external extends external_api {
|
||||
/**
|
||||
* Add a new service.
|
||||
*
|
||||
* @param array $conditions
|
||||
*
|
||||
* @return int
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public static function add_service(array $conditions): int {
|
||||
$parameters = self::validate_parameters(self::add_service_parameters(), [
|
||||
'record' => array_filter($conditions),
|
||||
]);
|
||||
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
$parameters = array_filter($parameters['record']);
|
||||
$record = new record($parameters);
|
||||
|
||||
return api::add_service($record);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method parameters.
|
||||
*
|
||||
* @return \external_function_parameters
|
||||
*/
|
||||
public static function add_service_parameters(): external_function_parameters {
|
||||
return new external_function_parameters([
|
||||
'record' => new external_single_structure([
|
||||
'events' => new external_multiple_structure(
|
||||
new external_value(PARAM_RAW, 'The event\'s name.'), 'The service\'s list events.', false
|
||||
),
|
||||
'header' => new external_value(PARAM_RAW, 'The request\'s header or type', false, 'application/json'),
|
||||
'name' => new external_value(PARAM_RAW, 'The service\'s name.'),
|
||||
'point' => new external_value(PARAM_URL, 'The service\'s endpoint.'),
|
||||
'status' => new external_value(PARAM_BOOL, 'The service\'s status.', false, true),
|
||||
'token' => new external_value(PARAM_RAW, 'The service\'s secret key.'),
|
||||
], 'The new service\'s data.'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method result value.
|
||||
*
|
||||
* @return \external_value
|
||||
*/
|
||||
public static function add_service_returns(): external_value {
|
||||
return new external_value(PARAM_INT, 'The service\'s ID.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the existing service.
|
||||
*
|
||||
* @param int $serviceid
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public static function del_service(int $serviceid): bool {
|
||||
$parameters = self::validate_parameters(self::del_service_parameters(), [
|
||||
'serviceid' => $serviceid,
|
||||
]);
|
||||
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
return api::del_service($parameters['serviceid']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method parameters.
|
||||
*
|
||||
* @return \external_function_parameters
|
||||
*/
|
||||
public static function del_service_parameters(): external_function_parameters {
|
||||
return new external_function_parameters([
|
||||
'serviceid' => new external_value(PARAM_INT, 'The service\'s ID.'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method result value.
|
||||
*
|
||||
* @return \external_value
|
||||
*/
|
||||
public static function del_service_returns(): external_value {
|
||||
return new external_value(PARAM_BOOL, 'The result operation.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the event's list.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public static function get_events(): array {
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
return api::get_events();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method parameters.
|
||||
*
|
||||
* @return \external_function_parameters
|
||||
*/
|
||||
public static function get_events_parameters(): external_function_parameters {
|
||||
return new external_function_parameters([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method result value.
|
||||
*
|
||||
* @return \external_multiple_structure
|
||||
*/
|
||||
public static function get_events_returns(): external_multiple_structure {
|
||||
return new external_multiple_structure(
|
||||
new external_single_structure([
|
||||
'action' => new external_value(PARAM_ALPHANUMEXT, 'The action name.'),
|
||||
'component' => new external_value(PARAM_COMPONENT, 'The component name.'),
|
||||
'crud' => new external_value(PARAM_ALPHA, 'The transaction type.'),
|
||||
'edulevel' => new external_value(PARAM_INT, 'The level of educational value.'),
|
||||
'eventname' => new external_value(PARAM_RAW, 'The event name.'),
|
||||
'objecttable' => new external_value(PARAM_RAW, 'The database table name.'),
|
||||
'target' => new external_value(PARAM_RAW, 'The target on which the action.'),
|
||||
], 'The event\'s data.'), 'The event\'s list.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get data by service.
|
||||
*
|
||||
* @param int $serviceid
|
||||
*
|
||||
* @return \local_webhooks\local\record
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public static function get_service(int $serviceid): record {
|
||||
$parameters = self::validate_parameters(self::get_service_parameters(), [
|
||||
'serviceid' => $serviceid,
|
||||
]);
|
||||
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
return api::get_service($parameters['serviceid']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method parameters.
|
||||
*
|
||||
* @return \external_function_parameters
|
||||
*/
|
||||
public static function get_service_parameters(): external_function_parameters {
|
||||
return new external_function_parameters([
|
||||
'serviceid' => new external_value(PARAM_INT, 'The service\'s ID.'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method result value.
|
||||
*
|
||||
* @return \external_single_structure
|
||||
*/
|
||||
public static function get_service_returns(): external_single_structure {
|
||||
return new external_single_structure([
|
||||
'events' => new external_multiple_structure(
|
||||
new external_value(PARAM_RAW, 'The event\'s name.'), 'The service\'s list events.'
|
||||
),
|
||||
'header' => new external_value(PARAM_RAW, 'The request\'s header or type'),
|
||||
'id' => new external_value(PARAM_INT, 'The service\'s ID.'),
|
||||
'name' => new external_value(PARAM_RAW, 'The service\'s name.'),
|
||||
'point' => new external_value(PARAM_URL, 'The service\'s endpoint.'),
|
||||
'status' => new external_value(PARAM_BOOL, 'The service\'s status.'),
|
||||
'token' => new external_value(PARAM_RAW, 'The service\'s secret key.'),
|
||||
], 'The service\'s data.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the service's list.
|
||||
*
|
||||
* @param array|null $conditions
|
||||
* @param string|null $sort
|
||||
* @param int|null $from
|
||||
* @param int|null $limit
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public static function get_services(array $conditions = null, string $sort = null, int $from = null, int $limit = null): array {
|
||||
$parameters = self::validate_parameters(self::get_services_parameters(), [
|
||||
'conditions' => $conditions ?? [],
|
||||
'from' => $from,
|
||||
'limit' => $limit,
|
||||
'sort' => $sort,
|
||||
]);
|
||||
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
return api::get_services(
|
||||
array_filter($parameters['conditions']),
|
||||
$parameters['sort'],
|
||||
$parameters['from'],
|
||||
$parameters['limit']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method parameters.
|
||||
*
|
||||
* @return \external_function_parameters
|
||||
*/
|
||||
public static function get_services_parameters(): external_function_parameters {
|
||||
return new external_function_parameters([
|
||||
'conditions' => new external_single_structure([
|
||||
'header' => new external_value(PARAM_RAW, 'The request\'s header or type', false),
|
||||
'name' => new external_value(PARAM_RAW, 'The service\'s name.', false),
|
||||
'point' => new external_value(PARAM_URL, 'The service\'s endpoint.', false),
|
||||
'status' => new external_value(PARAM_BOOL, 'The service\'s status.', false),
|
||||
'token' => new external_value(PARAM_RAW, 'The service\'s secret key.', false),
|
||||
], 'SQL conditions.', false),
|
||||
'sort' => new external_value(PARAM_RAW, 'SQL sort parameters.', false),
|
||||
'from' => new external_value(PARAM_INT, 'The start index.', false),
|
||||
'limit' => new external_value(PARAM_INT, 'The count elements.', false),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method result value.
|
||||
*
|
||||
* @return \external_multiple_structure
|
||||
*/
|
||||
public static function get_services_returns(): external_multiple_structure {
|
||||
return new external_multiple_structure(
|
||||
new external_single_structure([
|
||||
'events' => new external_multiple_structure(
|
||||
new external_value(PARAM_RAW, 'The event\'s name.'), 'The service\'s list events.'
|
||||
),
|
||||
'header' => new external_value(PARAM_RAW, 'The request\'s header or type'),
|
||||
'id' => new external_value(PARAM_INT, 'The service\'s ID.'),
|
||||
'name' => new external_value(PARAM_RAW, 'The service\'s name.'),
|
||||
'point' => new external_value(PARAM_URL, 'The service\'s endpoint.'),
|
||||
'status' => new external_value(PARAM_BOOL, 'The service\'s status.'),
|
||||
'token' => new external_value(PARAM_RAW, 'The service\'s secret key.'),
|
||||
], 'The service\'s data.'), 'The service\'s list.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the existing service.
|
||||
*
|
||||
* @param array $conditions
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public static function set_service(array $conditions): bool {
|
||||
$conditions = array_filter($conditions);
|
||||
$conditions['events'] = $conditions['events'] ?? [];
|
||||
|
||||
$parameters = self::validate_parameters(self::set_service_parameters(), [
|
||||
'record' => $conditions,
|
||||
]);
|
||||
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
$parameters = array_filter($parameters['record']);
|
||||
$record = api::get_service($parameters['id']);
|
||||
|
||||
foreach ($parameters as $index => $value) {
|
||||
if (property_exists($record, $index)) {
|
||||
$record->$index = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return api::set_service($record);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method parameters.
|
||||
*
|
||||
* @return \external_function_parameters
|
||||
*/
|
||||
public static function set_service_parameters(): external_function_parameters {
|
||||
return new external_function_parameters([
|
||||
'record' => new external_single_structure([
|
||||
'events' => new external_multiple_structure(
|
||||
new external_value(PARAM_RAW, 'The event\'s name.'), 'The service\'s list events.', false
|
||||
),
|
||||
'header' => new external_value(PARAM_RAW, 'The request\'s header or type', false),
|
||||
'id' => new external_value(PARAM_INT, 'The service\'s ID.'),
|
||||
'name' => new external_value(PARAM_RAW, 'The service\'s name.', false),
|
||||
'point' => new external_value(PARAM_URL, 'The service\'s endpoint.', false),
|
||||
'status' => new external_value(PARAM_BOOL, 'The service\'s status.', false),
|
||||
'token' => new external_value(PARAM_RAW, 'The service\'s secret key.', false),
|
||||
], 'The new service\'s data.'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of the method result value.
|
||||
*
|
||||
* @return \external_value
|
||||
*/
|
||||
public static function set_service_returns(): external_value {
|
||||
return new external_value(PARAM_BOOL, 'The result operation.');
|
||||
}
|
||||
}
|
||||
@@ -14,63 +14,133 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
require_once(dirname(__DIR__, 2) . '/config.php');
|
||||
/**
|
||||
* Service Management Manager.
|
||||
*
|
||||
* @package local_webhooks
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
global $CFG;
|
||||
require_once(__DIR__ . '/../../config.php');
|
||||
require_once(__DIR__ . '/lib.php');
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php');
|
||||
require_once($CFG->dirroot . '/local/webhooks/classes/output/services_table.php');
|
||||
require_once($CFG->libdir . '/adminlib.php');
|
||||
require_once($CFG->libdir . '/tablelib.php');
|
||||
|
||||
use local_webhooks\local\api;
|
||||
use local_webhooks\output\services_table;
|
||||
|
||||
/* Optional parameters */
|
||||
$backupservices = optional_param('getbackup', 0, PARAM_BOOL);
|
||||
$deleteid = optional_param('deleteid', 0, PARAM_INT);
|
||||
$hideshowid = optional_param('hideshowid', 0, PARAM_INT);
|
||||
|
||||
$editpage = '/local/webhooks/service.php';
|
||||
$mainpage = '/local/webhooks/index.php';
|
||||
/* Link generation */
|
||||
$editservice = '/local/webhooks/editservice.php';
|
||||
$managerservice = '/local/webhooks/index.php';
|
||||
$restorebackup = '/local/webhooks/restorebackup.php';
|
||||
$baseurl = new moodle_url($managerservice);
|
||||
|
||||
$baseurl = new moodle_url($mainpage);
|
||||
/* Configure the context of the page */
|
||||
admin_externalpage_setup('local_webhooks', '', null, $baseurl);
|
||||
$context = context_system::instance();
|
||||
|
||||
admin_externalpage_setup('local_webhooks', '', ['contextid' => $context->contextlevel], $baseurl, []);
|
||||
|
||||
// Deleting the existing service.
|
||||
if ($deleteid !== 0 && confirm_sesskey()) {
|
||||
api::del_service($deleteid);
|
||||
|
||||
redirect($PAGE->url, new lang_string('deleted', 'moodle'));
|
||||
/* Delete the service */
|
||||
if ((bool) $deleteid) {
|
||||
local_webhooks_remove_record($deleteid);
|
||||
redirect($PAGE->url, new lang_string('eventwebserviceservicedeleted', 'webservice'));
|
||||
}
|
||||
|
||||
// Setting status to existing service.
|
||||
if ($hideshowid !== 0 && confirm_sesskey()) {
|
||||
$service = api::get_service($hideshowid);
|
||||
$service->status = !filter_var($service->status, FILTER_VALIDATE_BOOLEAN);
|
||||
api::set_service($service);
|
||||
/* Retrieving a list of services */
|
||||
$callbacks = local_webhooks_get_list_records();
|
||||
|
||||
redirect($PAGE->url, new lang_string('changessaved', 'moodle'));
|
||||
/* Upload settings as a file */
|
||||
if ((bool) $backupservices) {
|
||||
$filecontent = local_webhooks_create_backup();
|
||||
$filename = 'webhooks_' . date('U') . '.backup';
|
||||
send_file($filecontent, $filename, 0, 0, true, true);
|
||||
}
|
||||
|
||||
// Page header.
|
||||
/* 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', 'webservice'));
|
||||
}
|
||||
}
|
||||
|
||||
/* The page title */
|
||||
$titlepage = new lang_string('pluginname', 'local_webhooks');
|
||||
$PAGE->set_heading($titlepage);
|
||||
$PAGE->set_title($titlepage);
|
||||
echo $OUTPUT->header();
|
||||
|
||||
// Adds the add button.
|
||||
$addserviceurl = new moodle_url($editpage, ['sesskey' => sesskey()]);
|
||||
echo $OUTPUT->single_button($addserviceurl, new lang_string('add', 'moodle'));
|
||||
/* Table declaration */
|
||||
$table = new flexible_table('webhooks-service-table');
|
||||
|
||||
// Separation.
|
||||
echo html_writer::empty_tag('br');
|
||||
/* Customize the table */
|
||||
$table->define_columns(
|
||||
array(
|
||||
'title',
|
||||
'url',
|
||||
'actions',
|
||||
)
|
||||
);
|
||||
|
||||
$table->define_headers(
|
||||
array(
|
||||
new lang_string('name', 'moodle'),
|
||||
new lang_string('url', 'moodle'),
|
||||
new lang_string('actions', 'moodle'),
|
||||
)
|
||||
);
|
||||
|
||||
// Displays the table.
|
||||
$table = new services_table('local-webhooks-services');
|
||||
$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', 'moodle');
|
||||
if ((bool) $callback->enable) {
|
||||
$hideshowicon = 't/hide';
|
||||
$hideshowstring = new lang_string('disable', 'moodle');
|
||||
}
|
||||
|
||||
/* Link to enable / disable the service */
|
||||
$hideshowlink = new moodle_url($managerservice, array('hideshowid' => $callback->id));
|
||||
$hideshowitem = $OUTPUT->action_icon($hideshowlink, new pix_icon($hideshowicon, $hideshowstring));
|
||||
|
||||
/* Link for editing */
|
||||
$editlink = new moodle_url($editservice, array('serviceid' => $callback->id));
|
||||
$edititem = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', new lang_string('edit', 'moodle')));
|
||||
|
||||
/* Link to remove */
|
||||
$deletelink = new moodle_url($managerservice, array('deleteid' => $callback->id));
|
||||
$deleteitem = $OUTPUT->action_icon($deletelink, new pix_icon('t/delete', new lang_string('delete', 'moodle')));
|
||||
|
||||
/* Adding data to the table */
|
||||
$table->add_data(array($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', 'webservice'), 'get');
|
||||
|
||||
/* Button to get a backup */
|
||||
$backupurl = new moodle_url($managerservice, array('getbackup' => true));
|
||||
echo $OUTPUT->single_button($backupurl, new lang_string('backup', 'moodle'), 'get');
|
||||
|
||||
/* Button for restoring settings */
|
||||
$restorebackupurl = new moodle_url($restorebackup);
|
||||
echo $OUTPUT->single_button($restorebackupurl, new lang_string('restore', 'moodle'), 'get');
|
||||
|
||||
// Footer.
|
||||
echo $OUTPUT->footer();
|
||||
@@ -15,11 +15,12 @@
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Strings for component 'local_webhooks', language 'en'.
|
||||
* Strings for component "local_webhooks", language "en".
|
||||
*
|
||||
* @copyright 2018 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package local_webhooks
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
$string['pluginname'] = 'WebHooks';
|
||||
$string['pluginname'] = 'WebHooks';
|
||||
$string['privacy:metadata'] = 'WebHooks does not store any personal data.';
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Library code used by the service control interfaces.
|
||||
*
|
||||
* @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__ . '/locallib.php');
|
||||
|
||||
/**
|
||||
* Getting a list of all services.
|
||||
*
|
||||
* @param int $limitfrom
|
||||
* @param int $limitnum
|
||||
*
|
||||
* @return array
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
function local_webhooks_get_list_records($limitfrom = 0, $limitnum = 0) {
|
||||
global $DB;
|
||||
|
||||
$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', array('id' => $serviceid), '*', MUST_EXIST);
|
||||
|
||||
if (!empty($servicerecord->events)) {
|
||||
$servicerecord->events = local_webhooks_unarchive_data($servicerecord->events);
|
||||
}
|
||||
|
||||
return $servicerecord;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the database table.
|
||||
*
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
function local_webhooks_remove_list_records() {
|
||||
global $DB;
|
||||
|
||||
$DB->delete_records('local_webhooks_service');
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the record.
|
||||
*
|
||||
* @param int $serviceid
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
function local_webhooks_remove_record($serviceid = 0) {
|
||||
global $DB;
|
||||
|
||||
$DB->delete_records('local_webhooks_service', array('id' => $serviceid));
|
||||
local_webhooks_events::service_deleted($serviceid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the record in the database.
|
||||
*
|
||||
* @param object $data
|
||||
* @param boolean $insert
|
||||
*
|
||||
* @return boolean
|
||||
* @throws \dml_exception
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
function local_webhooks_update_record($data, $insert = true) {
|
||||
global $DB;
|
||||
|
||||
if (empty($data->events)) {
|
||||
$data->events = array();
|
||||
}
|
||||
|
||||
$data->events = local_webhooks_archiving_data($data->events);
|
||||
|
||||
if ((bool) $insert) {
|
||||
$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 (bool) $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a backup copy of all the services.
|
||||
*
|
||||
* @return string
|
||||
* @throws \dml_exception
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
function local_webhooks_create_backup() {
|
||||
$listservices = local_webhooks_get_list_records();
|
||||
$listservices = local_webhooks_archiving_data($listservices);
|
||||
local_webhooks_events::backup_performed();
|
||||
|
||||
return $listservices;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore the data from the backup.
|
||||
*
|
||||
* @param string $listservices
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
function local_webhooks_restore_backup($listservices = '') {
|
||||
$listservices = local_webhooks_unarchive_data($listservices);
|
||||
|
||||
local_webhooks_remove_list_records();
|
||||
|
||||
foreach ($listservices as $servicerecord) {
|
||||
local_webhooks_update_record($servicerecord);
|
||||
}
|
||||
|
||||
local_webhooks_events::backup_restored();
|
||||
}
|
||||
|
||||
/**
|
||||
* Compress an array into a string.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function local_webhooks_archiving_data(array $data = array()) {
|
||||
return base64_encode(gzcompress(serialize($data), 3));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an array from a compressed string.
|
||||
*
|
||||
* @param string $data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function local_webhooks_unarchive_data($data = '') {
|
||||
return unserialize(gzuncompress(base64_decode($data)));
|
||||
}
|
||||
+153
@@ -0,0 +1,153 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Classes of modules.
|
||||
*
|
||||
* @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();
|
||||
|
||||
/**
|
||||
* Description of functions of the call of events
|
||||
*
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class local_webhooks_events {
|
||||
/**
|
||||
* Call the event when creating a backup.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public static function backup_performed() {
|
||||
$context = context_system::instance();
|
||||
|
||||
$event = local_webhooks\event\backup_performed::create(
|
||||
array(
|
||||
'context' => $context,
|
||||
'objectid' => 0,
|
||||
)
|
||||
);
|
||||
|
||||
$event->trigger();
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the event when restoring from a backup.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public static function backup_restored() {
|
||||
$context = context_system::instance();
|
||||
|
||||
$event = local_webhooks\event\backup_restored::create(
|
||||
array(
|
||||
'context' => $context,
|
||||
'objectid' => 0,
|
||||
)
|
||||
);
|
||||
|
||||
$event->trigger();
|
||||
}
|
||||
|
||||
/**
|
||||
* Call event when the response is received from the service
|
||||
*
|
||||
* @param int $objectid Service ID
|
||||
* @param array $response Server response
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public static function response_answer($objectid = 0, array $response = array()) {
|
||||
$context = context_system::instance();
|
||||
|
||||
$status = 'Error sending request';
|
||||
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->trigger();
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the event when the service is added.
|
||||
*
|
||||
* @param int $objectid Service ID
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public static function service_added($objectid = 0) {
|
||||
$context = context_system::instance();
|
||||
|
||||
$event = local_webhooks\event\service_added::create(
|
||||
compact('context', 'objectid')
|
||||
);
|
||||
|
||||
$event->trigger();
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the event when the service is deleted.
|
||||
*
|
||||
* @param int $objectid Service ID
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public static function service_deleted($objectid = 0) {
|
||||
$context = context_system::instance();
|
||||
|
||||
$event = local_webhooks\event\service_deleted::create(
|
||||
compact('context', 'objectid')
|
||||
);
|
||||
|
||||
$event->trigger();
|
||||
}
|
||||
|
||||
/**
|
||||
* Call event when the service is updated.
|
||||
*
|
||||
* @param int $objectid Service ID
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public static function service_updated($objectid = 0) {
|
||||
$context = context_system::instance();
|
||||
|
||||
$event = local_webhooks\event\service_updated::create(
|
||||
compact('context', 'objectid')
|
||||
);
|
||||
|
||||
$event->trigger();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Restore the settings page.
|
||||
*
|
||||
* @package local_webhooks
|
||||
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
require_once(__DIR__ . '/../../config.php');
|
||||
require_once(__DIR__ . '/classes/service_form.php');
|
||||
require_once(__DIR__ . '/lib.php');
|
||||
|
||||
require_once($CFG->libdir . '/adminlib.php');
|
||||
|
||||
/* Link generation */
|
||||
$baseurl = new moodle_url('/local/webhooks/restorebackup.php');
|
||||
$managerservice = new moodle_url('/local/webhooks/index.php');
|
||||
|
||||
/* Configure the context of the page */
|
||||
admin_externalpage_setup('local_webhooks', '', null, $baseurl);
|
||||
$context = context_system::instance();
|
||||
|
||||
/* Create an editing form */
|
||||
$mform = new service_backup_form($PAGE->url);
|
||||
|
||||
/* Cancel processing */
|
||||
if ($mform->is_cancelled()) {
|
||||
redirect($managerservice);
|
||||
}
|
||||
|
||||
/* Processing the received file */
|
||||
if (!empty($data = $mform->get_data())) {
|
||||
$content = $mform->get_file_content('backupfile');
|
||||
local_webhooks_restore_backup($content);
|
||||
redirect($managerservice, new lang_string('restorefinished', 'moodle'));
|
||||
}
|
||||
|
||||
/* The page title */
|
||||
$titlepage = new lang_string('backup', 'moodle');
|
||||
$PAGE->navbar->add($titlepage);
|
||||
$PAGE->set_heading($titlepage);
|
||||
$PAGE->set_title($titlepage);
|
||||
echo $OUTPUT->header();
|
||||
|
||||
/* Displays the form */
|
||||
$mform->display();
|
||||
|
||||
echo $OUTPUT->footer();
|
||||
-82
@@ -1,82 +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/>.
|
||||
|
||||
/**
|
||||
* Page of the service editor.
|
||||
*
|
||||
* @copyright 2018 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package local_webhooks
|
||||
*/
|
||||
|
||||
require_once(__DIR__ . '/../../config.php');
|
||||
|
||||
require_once($CFG->dirroot . '/local/webhooks/classes/ui_forms_plugin.php');
|
||||
require_once($CFG->dirroot . '/local/webhooks/lib.php');
|
||||
require_once($CFG->libdir . '/adminlib.php');
|
||||
|
||||
$serviceid = optional_param('serviceid', 0, PARAM_INT);
|
||||
|
||||
$urlparameters = array('serviceid' => $serviceid);
|
||||
$baseurl = new moodle_url('/local/webhooks/service.php', $urlparameters);
|
||||
$mainpage = new moodle_url('/local/webhooks/index.php');
|
||||
|
||||
admin_externalpage_setup('local_webhooks', '', null, $baseurl, array());
|
||||
$context = context_system::instance();
|
||||
|
||||
$mform = new local_webhooks_service_edit_form($PAGE->url);
|
||||
$formdata = (array) $mform->get_data();
|
||||
|
||||
/* Cancel */
|
||||
if ($mform->is_cancelled()) {
|
||||
redirect($mainpage);
|
||||
}
|
||||
|
||||
/* Updating the data */
|
||||
if (!empty($formdata) && confirm_sesskey()) {
|
||||
if (isset($formdata['events'])) {
|
||||
$formdata['events'] = array_keys($formdata['events']);
|
||||
}
|
||||
|
||||
if (!empty($serviceid)) {
|
||||
$formdata['id'] = $serviceid;
|
||||
local_webhooks_api::update_service($formdata);
|
||||
} else {
|
||||
local_webhooks_api::create_service($formdata);
|
||||
}
|
||||
|
||||
redirect($mainpage, new lang_string('changessaved', 'moodle'));
|
||||
}
|
||||
|
||||
/* Loading service data */
|
||||
if (!empty($serviceid)) {
|
||||
$service = local_webhooks_api::get_service($serviceid);
|
||||
$service->events = array_fill_keys($service->events, 1);
|
||||
$mform->set_data($service);
|
||||
}
|
||||
|
||||
/* The page title */
|
||||
$titlepage = new lang_string('externalservice', 'webservice');
|
||||
$PAGE->navbar->add($titlepage);
|
||||
$PAGE->set_heading($titlepage);
|
||||
$PAGE->set_title($titlepage);
|
||||
echo $OUTPUT->header();
|
||||
|
||||
/* Displays the form */
|
||||
$mform->display();
|
||||
|
||||
/* Footer */
|
||||
echo $OUTPUT->footer();
|
||||
+2
-2
@@ -17,9 +17,9 @@
|
||||
/**
|
||||
* Settings of the plugin.
|
||||
*
|
||||
* @copyright 2018 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @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();
|
||||
|
||||
@@ -1,364 +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/>.
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
|
||||
require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php');
|
||||
|
||||
use local_webhooks\local\api;
|
||||
use local_webhooks\local\record;
|
||||
|
||||
/**
|
||||
* Testing the API plugin class.
|
||||
*
|
||||
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
final class local_webhooks_api_testcase extends advanced_testcase {
|
||||
/**
|
||||
* Generate random an event's list.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
private static function get_random_events(): array {
|
||||
$result = array_rand(api::get_events(), random_int(2, 10));
|
||||
|
||||
return is_array($result) ? $result : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a random record.
|
||||
*
|
||||
* @return \local_webhooks\local\record
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
private static function get_random_record(): record {
|
||||
$record = new record();
|
||||
|
||||
$record->events = self::get_random_events();
|
||||
$record->header = 'application/json';
|
||||
$record->name = uniqid('', false);
|
||||
$record->point = 'http://example.org/' . urlencode($record->name);
|
||||
$record->status = true;
|
||||
$record->token = generate_uuid();
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing creation of the service.
|
||||
*
|
||||
* @group local_webhooks
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \moodle_exception
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function test_adding() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
|
||||
$events = $DB->get_records(LW_TABLE_EVENTS);
|
||||
$services = $DB->get_records(LW_TABLE_SERVICES);
|
||||
|
||||
self::assertCount(1, $services);
|
||||
$service = array_shift($services);
|
||||
|
||||
self::assertEquals($record->header, $service->header);
|
||||
self::assertEquals($record->id, $service->id);
|
||||
self::assertEquals($record->name, $service->name);
|
||||
self::assertEquals($record->point, $service->point);
|
||||
self::assertEquals($record->status, (bool) $service->status);
|
||||
self::assertEquals($record->token, $service->token);
|
||||
|
||||
self::assertCount(count($record->events), $events);
|
||||
foreach ($events as $event) {
|
||||
self::assertContains($event->name, $record->events);
|
||||
self::assertEquals($record->id, $event->serviceid);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deletion of the service.
|
||||
*
|
||||
* @group local_webhooks
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \moodle_exception
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function test_deleting() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Testing correct delete record of the database.
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
|
||||
self::assertTrue(api::del_service($record->id));
|
||||
self::assertCount(0, $DB->get_records(LW_TABLE_EVENTS));
|
||||
self::assertCount(0, $DB->get_records(LW_TABLE_SERVICES));
|
||||
|
||||
$ids = [];
|
||||
$total = random_int(5, 20);
|
||||
|
||||
// Testing correct delete record of the record's list.
|
||||
for ($i = 0; $i < $total; $i++) {
|
||||
$record = self::get_random_record();
|
||||
$ids[] = api::add_service($record);
|
||||
}
|
||||
|
||||
self::assertEquals(count($ids), api::get_total_count());
|
||||
self::assertTrue(api::del_service($ids[array_rand($ids, 1)]));
|
||||
self::assertEquals(count($ids) - 1, api::get_total_count());
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing get an event's list.
|
||||
*
|
||||
* @group local_webhooks
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function test_get_events() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$events = api::get_events();
|
||||
|
||||
self::assertNotCount(0, $events);
|
||||
|
||||
foreach ($events as $event) {
|
||||
self::assertInternalType('array', $event);
|
||||
|
||||
self::assertEquals([
|
||||
'eventname', 'component', 'target', 'action', 'crud', 'edulevel', 'objecttable',
|
||||
], array_keys($event));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing get to a service.
|
||||
*
|
||||
* @group local_webhooks
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \moodle_exception
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function test_get_service() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
$service = api::get_service($record->id);
|
||||
|
||||
self::assertEquals($record->header, $service->header);
|
||||
self::assertEquals($record->id, $service->id);
|
||||
self::assertEquals($record->name, $service->name);
|
||||
self::assertEquals($record->point, $service->point);
|
||||
self::assertEquals($record->status, (bool) $service->status);
|
||||
self::assertEquals($record->token, $service->token);
|
||||
|
||||
self::assertInternalType('array', $service->events);
|
||||
self::assertCount(count($record->events), $service->events);
|
||||
foreach ($service->events as $event) {
|
||||
self::assertContains($event, $record->events);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing get to the list services.
|
||||
*
|
||||
* @group local_webhooks
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \moodle_exception
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function test_get_services() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$records = [];
|
||||
$total = random_int(5, 20);
|
||||
|
||||
for ($i = 0; $i < $total; $i++) {
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
$records[$record->id] = $record;
|
||||
}
|
||||
|
||||
$services = api::get_services();
|
||||
self::assertCount(count($records), $services);
|
||||
|
||||
foreach ($services as $service) {
|
||||
$record = $records[$service->id];
|
||||
|
||||
self::assertEquals($record->header, $service->header);
|
||||
self::assertEquals($record->id, $service->id);
|
||||
self::assertEquals($record->name, $service->name);
|
||||
self::assertEquals($record->point, $service->point);
|
||||
self::assertEquals($record->status, $service->status);
|
||||
self::assertEquals($record->token, $service->token);
|
||||
|
||||
self::assertInternalType('array', $service->events);
|
||||
self::assertCount(count($record->events), $service->events);
|
||||
foreach ($service->events as $event) {
|
||||
self::assertContains($event, $record->events);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing get to the list services by event name.
|
||||
*
|
||||
* @group local_webhooks
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \moodle_exception
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function test_get_services_by_event() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$eventname = generate_uuid();
|
||||
$limit = random_int(1, 5);
|
||||
$total = random_int(5, 20);
|
||||
|
||||
$ids = [];
|
||||
|
||||
for ($i = 0; $i < $total; $i++) {
|
||||
$record = self::get_random_record();
|
||||
$record->events[] = $i < $limit ? $eventname : '';
|
||||
$ids[] = api::add_service($record);
|
||||
}
|
||||
|
||||
self::assertEquals(count($ids), api::get_total_count());
|
||||
$services = api::get_services_by_event($eventname);
|
||||
self::assertCount($limit, $services);
|
||||
|
||||
foreach ($services as $service) {
|
||||
self::assertContains($service->id, $ids);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing get to the list services with conditions.
|
||||
*
|
||||
* @group local_webhooks
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \moodle_exception
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function test_get_services_with_conditions() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$records = [];
|
||||
$total = random_int(5, 20);
|
||||
$limit = intdiv($total, 2);
|
||||
|
||||
// Creating some records.
|
||||
for ($i = 0; $i < $total; $i++) {
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
$records[] = $record;
|
||||
}
|
||||
|
||||
// Testing condition fields.
|
||||
$record = $records[array_rand($records, 1)];
|
||||
self::assertCount(1, api::get_services([
|
||||
'point' => $record->point,
|
||||
]));
|
||||
|
||||
// Testing limit fields.
|
||||
self::assertCount($limit, api::get_services([], null, 1, $limit));
|
||||
|
||||
// Testing sort fields.
|
||||
$service1 = api::get_services(null, 'id asc')[0];
|
||||
$service2 = api::get_services(null, 'id desc')[0];
|
||||
self::assertNotEquals($service1->id, $service2->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing get a total count of existing records.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function test_total() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$total = random_int(5, 20);
|
||||
|
||||
for ($i = 0; $i < $total; $i++) {
|
||||
$record = self::get_random_record();
|
||||
api::add_service($record);
|
||||
}
|
||||
|
||||
self::assertEquals($total, api::get_total_count());
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing of the service update.
|
||||
*
|
||||
* @group local_webhooks
|
||||
*
|
||||
* @throws \dml_exception
|
||||
* @throws \moodle_exception
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function test_updating() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
$record1 = self::get_random_record();
|
||||
$record2 = self::get_random_record();
|
||||
|
||||
$record2->id = api::add_service($record1);
|
||||
self::assertTrue(api::set_service($record2));
|
||||
|
||||
$events = $DB->get_records(LW_TABLE_EVENTS);
|
||||
$services = $DB->get_records(LW_TABLE_SERVICES);
|
||||
|
||||
self::assertCount(1, $services);
|
||||
$service = array_shift($services);
|
||||
|
||||
self::assertEquals($record2->header, $service->header);
|
||||
self::assertEquals($record2->id, $service->id);
|
||||
self::assertEquals($record2->name, $service->name);
|
||||
self::assertEquals($record2->point, $service->point);
|
||||
self::assertEquals($record2->status, (bool) $service->status);
|
||||
self::assertEquals($record2->token, $service->token);
|
||||
|
||||
self::assertCount(count($record2->events), $events);
|
||||
foreach ($events as $event) {
|
||||
self::assertContains($event->name, $record2->events);
|
||||
self::assertEquals($record2->id, $event->serviceid);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,386 +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/>.
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
|
||||
require_once($CFG->dirroot . '/local/webhooks/externallib.php');
|
||||
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
|
||||
|
||||
use local_webhooks\local\api;
|
||||
use local_webhooks\local\record;
|
||||
|
||||
/**
|
||||
* Testing external functions.
|
||||
*
|
||||
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
final class local_webhooks_external_testcase extends externallib_advanced_testcase {
|
||||
/**
|
||||
* Generate random an event's list.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
private static function get_random_events(): array {
|
||||
$result = array_rand(api::get_events(), random_int(2, 10));
|
||||
|
||||
return is_array($result) ? $result : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a random record.
|
||||
*
|
||||
* @return \local_webhooks\local\record
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
private static function get_random_record(): record {
|
||||
$record = new record();
|
||||
|
||||
$record->events = self::get_random_events();
|
||||
$record->header = 'application/json';
|
||||
$record->name = uniqid('', false);
|
||||
$record->point = 'http://example.org/' . urlencode($record->name);
|
||||
$record->status = true;
|
||||
$record->token = generate_uuid();
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing external add service.
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \invalid_response_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public function test_adding() {
|
||||
$this->resetAfterTest();
|
||||
self::setAdminUser();
|
||||
|
||||
$record = self::get_random_record();
|
||||
$return = local_webhooks_external::add_service((array) $record);
|
||||
$return = external_api::clean_returnvalue(local_webhooks_external::add_service_returns(), $return);
|
||||
|
||||
self::assertInternalType('integer', $return);
|
||||
self::assertEquals(1, api::get_total_count());
|
||||
|
||||
$service = api::get_service($return);
|
||||
self::assertEquals($record->header, $service->header);
|
||||
self::assertEquals($record->name, $service->name);
|
||||
self::assertEquals($record->point, $service->point);
|
||||
self::assertEquals($record->status, $service->status);
|
||||
self::assertEquals($record->token, $service->token);
|
||||
self::assertEquals($return, $service->id);
|
||||
|
||||
self::assertInternalType('array', $service->events);
|
||||
self::assertCount(count($record->events), $service->events);
|
||||
foreach ($service->events as $event) {
|
||||
self::assertContains($event, $record->events);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing the external delete service.
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \invalid_response_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public function test_deleting() {
|
||||
$this->resetAfterTest();
|
||||
self::setAdminUser();
|
||||
|
||||
// Testing correct delete record of the database.
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
|
||||
$return = local_webhooks_external::del_service($record->id);
|
||||
$return = external_api::clean_returnvalue(local_webhooks_external::del_service_returns(), $return);
|
||||
|
||||
self::assertEquals(0, api::get_total_count());
|
||||
self::assertInternalType('bool', $return);
|
||||
|
||||
$ids = [];
|
||||
$total = random_int(5, 20);
|
||||
|
||||
// Testing correct delete record of the record's list.
|
||||
for ($i = 0; $i < $total; $i++) {
|
||||
$record = self::get_random_record();
|
||||
$ids[] = api::add_service($record);
|
||||
}
|
||||
|
||||
self::assertEquals(count($ids), api::get_total_count());
|
||||
$return = local_webhooks_external::del_service($ids[array_rand($ids, 1)]);
|
||||
$return = external_api::clean_returnvalue(local_webhooks_external::del_service_returns(), $return);
|
||||
|
||||
self::assertEquals(count($ids) - 1, api::get_total_count());
|
||||
self::assertInternalType('bool', $return);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing external function get the event's list.
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \invalid_response_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public function test_get_events() {
|
||||
$this->resetAfterTest();
|
||||
self::setAdminUser();
|
||||
|
||||
$return = local_webhooks_external::get_events();
|
||||
$return = external_api::clean_returnvalue(local_webhooks_external::get_events_returns(), $return);
|
||||
|
||||
self::assertInternalType('array', $return);
|
||||
self::assertCount(count(api::get_events()), $return);
|
||||
|
||||
foreach ($return as $item) {
|
||||
self::assertInternalType('array', $item);
|
||||
|
||||
self::assertEquals([
|
||||
'action', 'component', 'crud', 'edulevel', 'eventname', 'objecttable', 'target',
|
||||
], array_keys($item));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing external get record's data.
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \invalid_response_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public function test_get_service() {
|
||||
$this->resetAfterTest();
|
||||
self::setAdminUser();
|
||||
|
||||
// Creating a new record.
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
|
||||
$return = local_webhooks_external::get_service($record->id);
|
||||
$return = external_api::clean_returnvalue(local_webhooks_external::get_service_returns(), $return);
|
||||
self::assertInternalType('array', $return);
|
||||
|
||||
// Testing the main fields.
|
||||
self::assertEquals($record->header, $return['header']);
|
||||
self::assertEquals($record->id, $return['id']);
|
||||
self::assertEquals($record->name, $return['name']);
|
||||
self::assertEquals($record->point, $return['point']);
|
||||
self::assertEquals($record->status, (int) $return['status']);
|
||||
self::assertEquals($record->token, $return['token']);
|
||||
|
||||
// Testing an event's list.
|
||||
self::assertInternalType('array', $return['events']);
|
||||
self::assertNotCount(0, $return['events']);
|
||||
foreach ($return['events'] as $event) {
|
||||
self::assertContains($event, $record->events);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing external get the record's list.
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \invalid_response_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public function test_get_services() {
|
||||
$this->resetAfterTest();
|
||||
self::setAdminUser();
|
||||
|
||||
$records = [];
|
||||
$total = random_int(5, 10);
|
||||
|
||||
// Creating some records.
|
||||
for ($i = 0; $i < $total; $i++) {
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
$records[$record->id] = $record;
|
||||
}
|
||||
|
||||
self::assertEquals(count($records), api::get_total_count());
|
||||
|
||||
$return = local_webhooks_external::get_services();
|
||||
$return = external_api::clean_returnvalue(local_webhooks_external::get_services_returns(), $return);
|
||||
|
||||
// Testing received item's list.
|
||||
self::assertInternalType('array', $return);
|
||||
self::assertCount(count($records), $return);
|
||||
|
||||
foreach ($return as $item) {
|
||||
self::assertInternalType('array', $item);
|
||||
|
||||
$record = $records[$item['id']];
|
||||
|
||||
self::assertEquals($record->header, $item['header']);
|
||||
self::assertEquals($record->id, $item['id']);
|
||||
self::assertEquals($record->name, $item['name']);
|
||||
self::assertEquals($record->point, $item['point']);
|
||||
self::assertEquals($record->status, (int) $item['status']);
|
||||
self::assertEquals($record->token, $item['token']);
|
||||
|
||||
self::assertInternalType('array', $item['events']);
|
||||
self::assertNotCount(0, $item['events']);
|
||||
foreach ($item['events'] as $event) {
|
||||
self::assertContains($event, $record->events);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing external get to the list services with conditions.
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \invalid_response_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public function test_get_services_with_conditions() {
|
||||
$this->resetAfterTest();
|
||||
self::setAdminUser();
|
||||
|
||||
$records = [];
|
||||
$total = random_int(5, 10);
|
||||
$limit = intdiv($total, 2);
|
||||
|
||||
// Creating some records.
|
||||
for ($i = 0; $i < $total; $i++) {
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
$records[$record->id] = $record;
|
||||
}
|
||||
|
||||
self::assertEquals(count($records), api::get_total_count());
|
||||
|
||||
// Testing condition fields.
|
||||
$record = $records[array_rand($records, 1)];
|
||||
$return = local_webhooks_external::get_services(['point' => $record->point]);
|
||||
$return = external_api::clean_returnvalue(local_webhooks_external::get_services_returns(), $return);
|
||||
|
||||
self::assertCount(1, $return);
|
||||
self::assertEquals($return[0]['id'], $record->id);
|
||||
|
||||
// Testing limit fields.
|
||||
$return = local_webhooks_external::get_services(null, null, 1, $limit);
|
||||
$return = external_api::clean_returnvalue(local_webhooks_external::get_services_returns(), $return);
|
||||
self::assertCount($limit, $return);
|
||||
|
||||
// Testing sort fields.
|
||||
$return = local_webhooks_external::get_services(null, 'id asc');
|
||||
$return = external_api::clean_returnvalue(local_webhooks_external::get_services_returns(), $return);
|
||||
$service1 = array_shift($return);
|
||||
|
||||
$return = local_webhooks_external::get_services(null, 'id desc');
|
||||
$return = external_api::clean_returnvalue(local_webhooks_external::get_services_returns(), $return);
|
||||
$service2 = array_shift($return);
|
||||
|
||||
self::assertNotEquals($service1['id'], $service2['id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing full update parameters of the service.
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public function test_updating_full() {
|
||||
$this->resetAfterTest();
|
||||
self::setAdminUser();
|
||||
|
||||
$record = self::get_random_record();
|
||||
$record2 = self::get_random_record();
|
||||
$record2->id = api::add_service($record);
|
||||
|
||||
$return = local_webhooks_external::set_service((array) $record2);
|
||||
$return = external_api::validate_parameters(local_webhooks_external::set_service_returns(), $return);
|
||||
self::assertInternalType('bool', $return);
|
||||
|
||||
$service = api::get_service($record2->id);
|
||||
self::assertEquals($record2->id, $service->id);
|
||||
self::assertEquals($record2->header, $service->header);
|
||||
self::assertEquals($record2->name, $service->name);
|
||||
self::assertEquals($record2->point, $service->point);
|
||||
self::assertEquals($record2->status, $service->status);
|
||||
self::assertEquals($record2->token, $service->token);
|
||||
|
||||
self::assertInternalType('array', $service->events);
|
||||
self::assertCount(count($record2->events), $service->events);
|
||||
foreach ($service->events as $event) {
|
||||
self::assertContains($event, $record2->events);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing selective update parameters of the service.
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
* @throws \invalid_parameter_exception
|
||||
* @throws \restricted_context_exception
|
||||
*/
|
||||
public function test_updating_short() {
|
||||
$this->resetAfterTest();
|
||||
self::setAdminUser();
|
||||
|
||||
$record = self::get_random_record();
|
||||
$record2 = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
|
||||
$return = local_webhooks_external::set_service(['id' => $record->id, 'name' => $record2->name, 'point' => $record2->point]);
|
||||
$return = external_api::validate_parameters(local_webhooks_external::set_service_returns(), $return);
|
||||
self::assertInternalType('bool', $return);
|
||||
|
||||
$service = api::get_service($record->id);
|
||||
self::assertEquals($record->header, $service->header);
|
||||
self::assertEquals($record->id, $service->id);
|
||||
self::assertEquals($record->status, $service->status);
|
||||
self::assertEquals($record->token, $service->token);
|
||||
self::assertEquals($record2->name, $service->name);
|
||||
self::assertEquals($record2->point, $service->point);
|
||||
|
||||
self::assertInternalType('array', $service->events);
|
||||
self::assertCount(count($record->events), $service->events);
|
||||
foreach ($service->events as $event) {
|
||||
self::assertContains($event, $record->events);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,205 +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/>.
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
|
||||
require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php');
|
||||
|
||||
use core\event\course_created;
|
||||
use core\task\manager;
|
||||
use local_webhooks\local\api;
|
||||
use local_webhooks\local\record;
|
||||
use local_webhooks\task\notify;
|
||||
|
||||
/**
|
||||
* Class local_webhooks_task_testcase.
|
||||
*
|
||||
* @copyright 2019 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
final class local_webhooks_task_testcase extends advanced_testcase {
|
||||
const EVENTNAME = '\\' . course_created::class;
|
||||
|
||||
/**
|
||||
* Generate a random record.
|
||||
*
|
||||
* @return \local_webhooks\local\record
|
||||
*/
|
||||
private static function get_random_record(): record {
|
||||
$record = new record();
|
||||
|
||||
$record->events = [self::EVENTNAME];
|
||||
$record->header = 'application/json';
|
||||
$record->name = uniqid('', false);
|
||||
$record->point = 'http://example.org/' . urlencode($record->name);
|
||||
$record->status = true;
|
||||
$record->token = generate_uuid();
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing add a task to the queue.
|
||||
*/
|
||||
public function test_add_task() {
|
||||
global $DB;
|
||||
|
||||
$DB->delete_records('task_adhoc');
|
||||
$this->resetAfterTest();
|
||||
|
||||
manager::queue_adhoc_task(new notify());
|
||||
|
||||
$tasks = manager::get_adhoc_tasks(notify::class);
|
||||
|
||||
self::assertCount(1, $tasks);
|
||||
self::assertInstanceOf(notify::class, array_shift($tasks));
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing disabled processing service.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public function test_execute_disabled() {
|
||||
global $DB;
|
||||
|
||||
$DB->delete_records('task_adhoc');
|
||||
curl::mock_response('{}');
|
||||
$this->resetAfterTest();
|
||||
|
||||
$record = self::get_random_record();
|
||||
$record->status = false;
|
||||
$record->id = api::add_service($record);
|
||||
|
||||
$task = new notify();
|
||||
$task->set_custom_data(['eventname' => self::EVENTNAME]);
|
||||
$task->execute();
|
||||
|
||||
self::assertNull($task->debug);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing enabled processing service.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public function test_execute_enabled() {
|
||||
global $DB;
|
||||
|
||||
$DB->delete_records('task_adhoc');
|
||||
curl::mock_response('{}');
|
||||
$this->resetAfterTest();
|
||||
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
|
||||
$task = new notify();
|
||||
$task->set_custom_data(['eventname' => self::EVENTNAME]);
|
||||
$task->execute();
|
||||
|
||||
self::assertCount(1, $task->debug);
|
||||
self::assertInternalType('array', $task->debug);
|
||||
|
||||
$element = array_shift($task->debug);
|
||||
self::assertInternalType('array', $element['data']);
|
||||
self::assertInternalType('object', $element['service']);
|
||||
|
||||
self::assertEquals($record->events[0], $element['data']['eventname']);
|
||||
self::assertEquals($record->id, $element['service']->id);
|
||||
self::assertEquals($record->token, $element['data']['token']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing count creating tasks.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public function test_observer_multiple() {
|
||||
global $DB;
|
||||
|
||||
$DB->delete_records('task_adhoc');
|
||||
curl::mock_response('{}');
|
||||
$this->resetAfterTest();
|
||||
|
||||
$generator = self::getDataGenerator();
|
||||
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
|
||||
$total = random_int(5, 20);
|
||||
for ($i = 0; $i < $total; $i++) {
|
||||
$generator->create_course();
|
||||
}
|
||||
|
||||
$debug = [];
|
||||
foreach (manager::get_adhoc_tasks(notify::class) as $event) {
|
||||
self::assertInstanceOf(notify::class, $event);
|
||||
/** @var \local_webhooks\task\notify $event */
|
||||
|
||||
$event->execute();
|
||||
|
||||
if (isset($event->debug) && is_array($event->debug)) {
|
||||
$debug[] = array_merge(...$event->debug);
|
||||
}
|
||||
}
|
||||
|
||||
self::assertCount($total, $debug);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing structure a creating task.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public function test_observer_single() {
|
||||
global $DB;
|
||||
|
||||
$DB->delete_records('task_adhoc');
|
||||
curl::mock_response('{}');
|
||||
$this->resetAfterTest();
|
||||
|
||||
$generator = self::getDataGenerator();
|
||||
|
||||
$record = self::get_random_record();
|
||||
$record->id = api::add_service($record);
|
||||
|
||||
$course = $generator->create_course();
|
||||
|
||||
$debug = [];
|
||||
foreach (manager::get_adhoc_tasks(notify::class) as $event) {
|
||||
self::assertInstanceOf(notify::class, $event);
|
||||
/** @var \local_webhooks\task\notify $event */
|
||||
|
||||
$event->execute();
|
||||
|
||||
if (isset($event->debug) && is_array($event->debug)) {
|
||||
$debug[] = array_merge(...$event->debug);
|
||||
}
|
||||
}
|
||||
|
||||
self::assertCount(1, $debug);
|
||||
$element = array_shift($debug);
|
||||
|
||||
self::assertInternalType('array', $element['data']);
|
||||
self::assertEquals($course->id, $element['data']['courseid']);
|
||||
}
|
||||
}
|
||||
+12
-4
@@ -14,10 +14,18 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Information about the version of the 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();
|
||||
|
||||
$plugin->component = 'local_webhooks';
|
||||
$plugin->maturity = MATURITY_ALPHA;
|
||||
$plugin->release = '5.0.0-rc.1 (Build: 2019040100)';
|
||||
$plugin->requires = 2016052300;
|
||||
$plugin->version = 2019040100;
|
||||
$plugin->maturity = MATURITY_STABLE;
|
||||
$plugin->release = '3.0.5 (Build: 2020051600)';
|
||||
$plugin->requires = 2019052000;
|
||||
$plugin->version = 2020051600;
|
||||
|
||||
Reference in New Issue
Block a user