diff options
author | Valentin Popov <info@valentineus.link> | 2017-12-27 14:26:59 +0300 |
---|---|---|
committer | Valentin Popov <info@valentineus.link> | 2017-12-27 14:26:59 +0300 |
commit | 6e3bf072af2bf9d31c93f08a7b553fd614ae11b7 (patch) | |
tree | 5c6a44d41a00ac57099a84a2587246b74f86319f /externallib.php | |
parent | 2e57e732bae667c26e58bb8f4fcc52f3e649bf8f (diff) | |
download | local_webhooks-6e3bf072af2bf9d31c93f08a7b553fd614ae11b7.tar.xz local_webhooks-6e3bf072af2bf9d31c93f08a7b553fd614ae11b7.zip |
Added external plugin functions
Diffstat (limited to 'externallib.php')
-rw-r--r-- | externallib.php | 541 |
1 files changed, 541 insertions, 0 deletions
diff --git a/externallib.php b/externallib.php new file mode 100644 index 0000000..cbe89dd --- /dev/null +++ b/externallib.php @@ -0,0 +1,541 @@ +<?php +// This file is part of Moodle - http://moodle.org/ +// +// Moodle is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Moodle is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see <http://www.gnu.org/licenses/>. + +/** + * This file defines the plugin's external functions. + * + * @package local_webhooks + * @copyright 2017 "Valentin Popov" <info@valentineus.link> + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined("MOODLE_INTERNAL") || die(); + +require_once(__DIR__ . "/lib.php"); + +require_once($CFG->libdir . "/externallib.php"); + +/** + * External functions. + * + * @copyright 2017 "Valentin Popov" <info@valentineus.link> + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class local_webhooks_external extends external_api { + /** + * Returns description of method parameters. + * + * @return external_function_parameters + * @since Moodle 2.9 Options available + * @since Moodle 2.2 + */ + public static function change_status_parameters() { + return new external_function_parameters( + array( + "serviceid" => new external_value(PARAM_INT, "Service identifier.") + ) + ); + } + + /** + * Change the status of the service. + * + * @param number $serviceid + * @return boolean + * @since Moodle 2.9 Options available + * @since Moodle 2.2 + */ + public static function change_status($serviceid = 0) { + $parameters = self::validate_parameters(self::change_status_parameters(), array("serviceid" => $serviceid)); + + $context = context_system::instance(); + self::validate_context($context); + + $result = local_webhooks_change_status($parameters["serviceid"]); + return $result; + } + + /** + * Returns description of method result value. + * + * @return external_description + * @since Moodle 2.2 + */ + public static function change_status_returns() { + return new external_value(PARAM_BOOL, "The result of the operation."); + } + + /** + * Returns description of method parameters. + * + * @return external_function_parameters + * @since Moodle 2.9 Options available + * @since Moodle 2.2 + */ + public static function get_record_parameters() { + return new external_function_parameters( + array( + "serviceid" => new external_value(PARAM_INT, "Service identifier.") + ) + ); + } + + /** + * Get the record from the database. + * + * @param number $serviceid + * @return array + * @since Moodle 2.9 Options available + * @since Moodle 2.2 + */ + public static function get_record($serviceid = 0) { + $parameters = self::validate_parameters(self::get_record_parameters(), array("serviceid" => $serviceid)); + + $context = context_system::instance(); + self::validate_context($context); + + $service = array(); + if ($record = local_webhooks_get_record($parameters["serviceid"])) { + $service["enable"] = $record->enable; + $service["id"] = $record->id; + $service["other"] = $record->other; + $service["title"] = $record->title; + $service["token"] = $record->token; + $service["type"] = $record->type; + $service["url"] = $record->url; + + $service["events"] = array(); + foreach ($record->events as $key => $value) { + $service["events"][] = array("name" => $key, "value" => $value); + } + } + + return $service; + } + + /** + * Returns description of method result value. + * + * @return external_description + * @since Moodle 2.2 + */ + public static function get_record_returns() { + return new external_single_structure( + array( + "id" => new external_value(PARAM_INT, "Service identifier."), + "enable" => new external_value(PARAM_INT, "Service status."), + "title" => new external_value(PARAM_TEXT, "Name of the service."), + "url" => new external_value(PARAM_URL, "URL address."), + "type" => new external_value(PARAM_TEXT, "Header type."), + "token" => new external_value(PARAM_TEXT, "Authorization key.", VALUE_OPTIONAL), + "other" => new external_value(PARAM_TEXT, "Additional field.", VALUE_OPTIONAL), + "events" => new external_multiple_structure( + new external_single_structure( + array( + "name" => new external_value(PARAM_TEXT, "Event name."), + "value" => new external_value(PARAM_INT, "Observation status.") + ) + ), "Observed events.", VALUE_OPTIONAL + ) + ), "Record about the service." + ); + } + + /** + * Returns description of method parameters. + * + * @return external_function_parameters + * @since Moodle 2.9 Options available + * @since Moodle 2.2 + */ + public static function get_list_records_parameters() { + return new external_function_parameters(array()); + } + + /** + * Get all records from the database. + * + * @return array + * @since Moodle 2.9 Options available + * @since Moodle 2.2 + */ + public static function get_list_records() { + $context = context_system::instance(); + self::validate_context($context); + + $result = array(); + if ($listrecords = local_webhooks_get_list_records()) { + foreach ($listrecords as $index => $record) { + $result[$index]["enable"] = $record->enable; + $result[$index]["id"] = $record->id; + $result[$index]["other"] = $record->other; + $result[$index]["title"] = $record->title; + $result[$index]["token"] = $record->token; + $result[$index]["type"] = $record->type; + $result[$index]["url"] = $record->url; + + $result[$index]["events"] = array(); + foreach ($record->events as $key => $value) { + $result[$index]["events"][] = array("name" => $key, "value" => $value); + } + } + } + + return $result; + } + + /** + * Returns description of method result value. + * + * @return external_description + * @since Moodle 2.2 + */ + public static function get_list_records_returns() { + return new external_multiple_structure( + new external_single_structure( + array( + "id" => new external_value(PARAM_INT, "Service identifier."), + "enable" => new external_value(PARAM_INT, "Service status."), + "title" => new external_value(PARAM_TEXT, "Name of the service."), + "url" => new external_value(PARAM_URL, "URL address."), + "type" => new external_value(PARAM_TEXT, "Header type."), + "token" => new external_value(PARAM_TEXT, "Authorization key.", VALUE_OPTIONAL), + "other" => new external_value(PARAM_TEXT, "Additional field.", VALUE_OPTIONAL), + "events" => new external_multiple_structure( + new external_single_structure( + array( + "name" => new external_value(PARAM_TEXT, "Event name."), + "value" => new external_value(PARAM_INT, "Observation status.") + ) + ), "Observed events.", VALUE_OPTIONAL + ) + ), "Record about the service." + ), "List of services." + ); + } + + /** + * Returns description of method parameters. + * + * @return external_function_parameters + * @since Moodle 2.9 Options available + * @since Moodle 2.2 + */ + public static function create_record_parameters() { + return new external_function_parameters( + array( + "service" => new external_single_structure( + array( + "enable" => new external_value(PARAM_BOOL, "Service status.", VALUE_OPTIONAL), + "title" => new external_value(PARAM_TEXT, "Name of the service."), + "url" => new external_value(PARAM_URL, "URL address."), + "type" => new external_value(PARAM_TEXT, "Header type.", VALUE_OPTIONAL), + "token" => new external_value(PARAM_TEXT, "Authorization key.", VALUE_OPTIONAL), + "other" => new external_value(PARAM_TEXT, "Additional field.", VALUE_OPTIONAL), + "events" => new external_multiple_structure( + new external_single_structure( + array( + "name" => new external_value(PARAM_TEXT, "Event name."), + "value" => new external_value(PARAM_INT, "Observation status.") + ) + ), "Observed events.", VALUE_OPTIONAL + ) + ), "Record about the service." + ) + ) + ); + } + + /** + * Create an entry in the database. + * + * @param array $service + * @return number + * @since Moodle 2.9 Options available + * @since Moodle 2.2 + */ + public static function create_record($service = array()) { + $parameters = self::validate_parameters(self::create_record_parameters(), array("service" => $service)); + + $context = context_system::instance(); + self::validate_context($context); + + $record = new stdClass(); + $record->other = $parameters["service"]["other"]; + $record->title = $parameters["service"]["title"]; + $record->token = $parameters["service"]["token"]; + $record->url = $parameters["service"]["url"]; + + $record->enable = !empty($parameters["service"]["enable"]) ? $parameters["service"]["enable"] : false; + $record->type = !empty($parameters["service"]["type"]) ? $parameters["service"]["type"] : "json"; + + $record->events = array(); + foreach ($parameters["service"]["events"] as $value) { + $record->events[$value["name"]] = $value["value"]; + } + + $result = local_webhooks_create_record($record); + return $result; + } + + /** + * Returns description of method result value. + * + * @return external_description + * @since Moodle 2.2 + */ + public static function create_record_returns() { + return new external_value(PARAM_INT, "Service identifier."); + } + + /** + * Returns description of method parameters. + * + * @return external_function_parameters + * @since Moodle 2.9 Options available + * @since Moodle 2.2 + */ + public static function update_record_parameters() { + return new external_function_parameters( + array( + "id" => new external_value(PARAM_INT, "Service identifier."), + "service" => new external_single_structure( + array( + "enable" => new external_value(PARAM_BOOL, "Service status.", VALUE_OPTIONAL), + "title" => new external_value(PARAM_TEXT, "Name of the service.", VALUE_OPTIONAL), + "url" => new external_value(PARAM_URL, "URL address.", VALUE_OPTIONAL), + "type" => new external_value(PARAM_TEXT, "Header type.", VALUE_OPTIONAL), + "token" => new external_value(PARAM_TEXT, "Authorization key.", VALUE_OPTIONAL), + "other" => new external_value(PARAM_TEXT, "Additional field.", VALUE_OPTIONAL), + "events" => new external_multiple_structure( + new external_single_structure( + array( + "name" => new external_value(PARAM_TEXT, "Event name."), + "value" => new external_value(PARAM_INT, "Observation status.") + ) + ), "Observed events.", VALUE_OPTIONAL + ) + ), "Record about the service." + ) + ) + ); + } + + /** + * Update the record in the database. + * + * @param number $serviceid + * @param array $service + * @return boolean + * @since Moodle 2.9 Options available + * @since Moodle 2.2 + */ + public static function update_record($serviceid = 0, $service = array()) { + $parameters = self::validate_parameters(self::update_record_parameters(), array("id" => $serviceid, "service" => $service)); + + $context = context_system::instance(); + self::validate_context($context); + + $result = false; + if ($record = local_webhooks_get_record($parameters["id"])) { + $record->enable = !empty($parameters["service"]["enable"]) ? $parameters["service"]["enable"] : $record->enable; + $record->other = !empty($parameters["service"]["other"]) ? $parameters["service"]["other"] : $record->other; + $record->title = !empty($parameters["service"]["title"]) ? $parameters["service"]["title"] : $record->title; + $record->token = !empty($parameters["service"]["token"]) ? $parameters["service"]["token"] : $record->token; + $record->type = !empty($parameters["service"]["type"]) ? $parameters["service"]["type"] : $record->type; + $record->url = !empty($parameters["service"]["url"]) ? $parameters["service"]["url"] : $record->url; + + if (!empty($parameters["service"]["events"])) { + $record->events = array(); + foreach ($parameters["service"]["events"] as $value) { + $record->events[$value["name"]] = $value["value"]; + } + } + + $result = local_webhooks_update_record($record); + } + + return $result; + } + + /** + * Returns description of method result value. + * + * @return external_description + * @since Moodle 2.2 + */ + public static function update_record_returns() { + return new external_value(PARAM_BOOL, "The result of the operation."); + } + + /** + * Returns description of method parameters. + * + * @return external_function_parameters + * @since Moodle 2.9 Options available + * @since Moodle 2.2 + */ + public static function delete_record_parameters() { + return new external_function_parameters( + array( + "serviceid" => new external_value(PARAM_INT, "Service identifier.") + ) + ); + } + + /** + * Delete the record from the database. + * + * @param number $serviceid + * @return boolean + * @since Moodle 2.9 Options available + * @since Moodle 2.2 + */ + public static function delete_record($serviceid = 0) { + $parameters = self::validate_parameters(self::delete_record_parameters(), array("serviceid" => $serviceid)); + + $context = context_system::instance(); + self::validate_context($context); + + $result = local_webhooks_delete_record($parameters["serviceid"]); + return $result; + } + + /** + * Returns description of method result value. + * + * @return external_description + * @since Moodle 2.2 + */ + public static function delete_record_returns() { + return new external_value(PARAM_BOOL, "The result of the operation."); + } + + /** + * Returns description of method parameters. + * + * @return external_function_parameters + * @since Moodle 2.9 Options available + * @since Moodle 2.2 + */ + public static function delete_all_records_parameters() { + return new external_function_parameters(array()); + } + + /** + * Delete all records from the database. + * + * @return boolean + * @since Moodle 2.9 Options available + * @since Moodle 2.2 + */ + public static function delete_all_records() { + $context = context_system::instance(); + self::validate_context($context); + + $result = local_webhooks_delete_all_records(); + return $result; + } + + /** + * Returns description of method result value. + * + * @return external_description + * @since Moodle 2.2 + */ + public static function delete_all_records_returns() { + return new external_value(PARAM_BOOL, "The result of the operation."); + } + + /** + * Returns description of method parameters. + * + * @return external_function_parameters + * @since Moodle 2.9 Options available + * @since Moodle 2.2 + */ + public static function create_backup_parameters() { + return new external_function_parameters(array()); + } + + /** + * Create a backup. + * + * @return string + * @since Moodle 2.9 Options available + * @since Moodle 2.2 + */ + public static function create_backup() { + $context = context_system::instance(); + self::validate_context($context); + + $result = local_webhooks_create_backup(); + return $result; + } + + /** + * Returns description of method result value. + * + * @return external_description + * @since Moodle 2.2 + */ + public static function create_backup_returns() { + return new external_value(PARAM_TEXT, "Backup copy."); + } + + /** + * Returns description of method parameters. + * + * @return external_function_parameters + * @since Moodle 2.9 Options available + * @since Moodle 2.2 + */ + public static function restore_backup_parameters() { + return new external_function_parameters( + array( + "backup" => new external_value(PARAM_TEXT, "Backup copy."), + "deleterecords" => new external_value(PARAM_BOOL, "Delete existing records.") + ) + ); + } + + /** + * Restore from a backup. + * + * @param string $data + * @param boolean $deleterecords + * @since Moodle 2.9 Options available + * @since Moodle 2.2 + */ + public static function restore_backup($backup = "", $deleterecords = false) { + $parameters = self::validate_parameters(self::restore_backup_parameters(), array("backup" => $backup, "deleterecords" => $deleterecords)); + + $context = context_system::instance(); + self::validate_context($context); + + local_webhooks_restore_backup($parameters["backup"], $parameters["deleterecords"]); + } + + /** + * Returns description of method result value. + * + * @return external_description + * @since Moodle 2.2 + */ + public static function restore_backup_returns() { + return null; + } +}
\ No newline at end of file |