From be91a4c9b4e5d35f74208c45950ce70de659371b Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sun, 22 Oct 2017 13:45:49 +0400 Subject: Forming the settings page --- lang/en/local_webhooks.php | 13 ++++++------- lang/ru/local_webhooks.php | 13 ++++++------- settings.php | 25 ++++++++++++++++++------- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/lang/en/local_webhooks.php b/lang/en/local_webhooks.php index 05def67..b230a69 100644 --- a/lang/en/local_webhooks.php +++ b/lang/en/local_webhooks.php @@ -15,16 +15,15 @@ // along with Moodle. If not, see . /** - * Strings for component 'local_webhooks', language 'en'. + * Strings for component "local_webhooks", language "en". * * @package local_webhooks * @copyright 2017 "Valentin Popov" * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -$string['local_webhooksdescription'] = ''; -$string['pluginname'] = 'WebHook\'s'; -$string['enabled'] = 'Enabled'; -$string['enabled_help'] = 'Enable event tracking.'; -$string['url'] = 'URL'; -$string['url_help'] = 'Web address of the remote service.'; \ No newline at end of file +$string["enableservice"] = "Enabled"; +$string["enableservice_help"] = "Enable the Event Tracking service."; +$string["linkmanagerservice"] = "Service management manager"; +$string["local_webhooksdescription"] = ""; +$string["pluginname"] = "Moodle WebHooks"; \ No newline at end of file diff --git a/lang/ru/local_webhooks.php b/lang/ru/local_webhooks.php index 4883214..8a2807a 100644 --- a/lang/ru/local_webhooks.php +++ b/lang/ru/local_webhooks.php @@ -15,16 +15,15 @@ // along with Moodle. If not, see . /** - * Strings for component 'local_webhooks', language 'ru'. + * Strings for component "local_webhooks", language "ru". * * @package local_webhooks * @copyright 2017 "Valentin Popov" * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -$string['local_webhooksdescription'] = ''; -$string['pluginname'] = 'WebHook\'s'; -$string['enabled'] = 'Включить'; -$string['enabled_help'] = 'Включить отслеживание событий.'; -$string['url'] = 'URL'; -$string['url_help'] = 'Веб-адрес удалённой службы.'; \ No newline at end of file +$string["enableservice"] = "Включить"; +$string["enableservice_help"] = "Включение службы отслеживания событий."; +$string["linkmanagerservice"] = "Менеджер управления службами"; +$string["local_webhooksdescription"] = ""; +$string["pluginname"] = "Moodle WebHooks"; \ No newline at end of file diff --git a/settings.php b/settings.php index 0e9727f..ceb603a 100644 --- a/settings.php +++ b/settings.php @@ -22,15 +22,26 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -defined('MOODLE_INTERNAL') || die(); +defined("MOODLE_INTERNAL") || die(); +$settings = null; if ($hassiteconfig) { - $settings = new admin_settingpage('local_webhooks', get_string('pluginname', 'local_webhooks')); - $ADMIN->add('localplugins', $settings); + $settings = new admin_settingpage( + "local_webhooks", + new lang_string("pluginname", "local_webhooks") + ); - $settings->add(new admin_setting_configcheckbox('local_webhooks/enabled', get_string('enabled', 'local_webhooks'), - get_string('enabled_help', 'local_webhooks'), false)); + $ADMIN->add("localplugins", $settings); - $settings->add(new admin_setting_configtext('local_webhooks/url', get_string('url', 'local_webhooks'), - get_string('url_help', 'local_webhooks'), 'http://example.com/endpoint', PARAM_URL, 40)); + $settings->add(new admin_setting_configcheckbox( + "local_webhooks/enabled", + new lang_string("enableservice", "local_webhooks"), + new lang_string("enableservice_help", "local_webhooks"), + false + )); + + /* Link to the service manager */ + $linktext = new lang_string("linkmanagerservice", "local_webhooks"); + $link = "wwwroot . "/local/webhooks/managerservice.php\">" . $linktext . ""; + $settings->add(new admin_setting_heading("local_webhooks_addheading", "", $link)); } \ No newline at end of file -- cgit v1.2.3 From 706b948be01d11e30675aabe7b2b6d62fb3434b6 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sun, 22 Oct 2017 14:10:07 +0400 Subject: Manager page template --- lang/en/local_webhooks.php | 5 +++- lang/ru/local_webhooks.php | 5 +++- managerservice.php | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 managerservice.php diff --git a/lang/en/local_webhooks.php b/lang/en/local_webhooks.php index b230a69..d2f33bd 100644 --- a/lang/en/local_webhooks.php +++ b/lang/en/local_webhooks.php @@ -26,4 +26,7 @@ $string["enableservice"] = "Enabled"; $string["enableservice_help"] = "Enable the Event Tracking service."; $string["linkmanagerservice"] = "Service management manager"; $string["local_webhooksdescription"] = ""; -$string["pluginname"] = "Moodle WebHooks"; \ No newline at end of file +$string["managementmanager"] = "Service Management Manager"; +$string["managementmanageradd"] = "Add a service"; +$string["pluginname"] = "Moodle WebHooks"; +$string["servicedeleted"] = "Service has been removed"; \ No newline at end of file diff --git a/lang/ru/local_webhooks.php b/lang/ru/local_webhooks.php index 8a2807a..2c8f0b7 100644 --- a/lang/ru/local_webhooks.php +++ b/lang/ru/local_webhooks.php @@ -26,4 +26,7 @@ $string["enableservice"] = "Включить"; $string["enableservice_help"] = "Включение службы отслеживания событий."; $string["linkmanagerservice"] = "Менеджер управления службами"; $string["local_webhooksdescription"] = ""; -$string["pluginname"] = "Moodle WebHooks"; \ No newline at end of file +$string["managementmanager"] = "Менеджер управления службами"; +$string["managementmanageradd"] = "Добавить службу"; +$string["pluginname"] = "Moodle WebHooks"; +$string["servicedeleted"] = "Служба была удалена"; \ No newline at end of file diff --git a/managerservice.php b/managerservice.php new file mode 100644 index 0000000..7f71a3a --- /dev/null +++ b/managerservice.php @@ -0,0 +1,64 @@ +. + +/** + * Service Management Manager. + * + * @package local_webhooks + * @copyright 2017 "Valentin Popov" + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once(__DIR__ . "/../../config.php"); +$deleteservice = optional_param("deleteservice", 0, PARAM_INT); + +require_login(); +$baseurl = new moodle_url("/local/webhooks/managerservice.php"); +$PAGE->set_url($baseurl); + +$context = context_system::instance(); +$PAGE->set_context($context); + +/* Delete the service */ +if ($deleteservice && confirm_sesskey()) { + $DB->delete_records("local_webhooks_service", array("id" => $deleterssid)); + redirect($PAGE->url, new lang_string("servicedeleted", "local_webhooks")); +} + +/* Retrieving a list of services */ +$select = null; +$callbacks = $DB->get_records_select("local_webhooks_service", $select, null, $DB->sql_order_by_text("title")); + +/* Page template */ +$titlepage = new lang_string("managementmanager", "local_webhooks"); +$PAGE->set_pagelayout("standard"); +$PAGE->set_title($titlepage); +$PAGE->set_heading($titlepage); + +/* The page title */ +$PAGE->navbar->add(new lang_string("local")); +$PAGE->navbar->add(new lang_string("pluginname", "local_webhooks")); +$PAGE->navbar->add($titlepage, $baseurl); +echo $OUTPUT->header(); + +/* @todo: Place the formation table */ + +/* Add service button */ +$addurl = new moodle_url("/local/webhooks/editservice.php"); +$addurltext = new lang_string("managementmanageradd", "local_webhooks"); +echo "
" . $OUTPUT->single_button($addurl, $addurltext, "get") . "
"; + +echo $OUTPUT->footer(); \ No newline at end of file -- cgit v1.2.3 From 804fc061ed55abe0f8629841fbac84191bec4374 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sun, 22 Oct 2017 14:12:49 +0400 Subject: Instructions for creating a table in the database --- db/install.xml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 db/install.xml diff --git a/db/install.xml b/db/install.xml new file mode 100644 index 0000000..b152933 --- /dev/null +++ b/db/install.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + +
+
+
-- cgit v1.2.3 From aaacb23d419faa4513eb3c9380e47bc7befa9e6c Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sun, 22 Oct 2017 14:15:16 +0400 Subject: Correcting an error with a variable --- managerservice.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/managerservice.php b/managerservice.php index 7f71a3a..62131d2 100644 --- a/managerservice.php +++ b/managerservice.php @@ -34,7 +34,7 @@ $PAGE->set_context($context); /* Delete the service */ if ($deleteservice && confirm_sesskey()) { - $DB->delete_records("local_webhooks_service", array("id" => $deleterssid)); + $DB->delete_records("local_webhooks_service", array("id" => $deleteservice)); redirect($PAGE->url, new lang_string("servicedeleted", "local_webhooks")); } -- cgit v1.2.3 From 54b20f13354b74dd7266645069961a5d8e215ee9 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sun, 22 Oct 2017 16:45:08 +0400 Subject: Adding a title --- lang/en/local_webhooks.php | 1 + lang/ru/local_webhooks.php | 1 + settings.php | 7 +++++++ 3 files changed, 9 insertions(+) diff --git a/lang/en/local_webhooks.php b/lang/en/local_webhooks.php index d2f33bd..9a3993a 100644 --- a/lang/en/local_webhooks.php +++ b/lang/en/local_webhooks.php @@ -28,5 +28,6 @@ $string["linkmanagerservice"] = "Service management manager"; $string["local_webhooksdescription"] = ""; $string["managementmanager"] = "Service Management Manager"; $string["managementmanageradd"] = "Add a service"; +$string["managementmanagerdescription"] = ""; $string["pluginname"] = "Moodle WebHooks"; $string["servicedeleted"] = "Service has been removed"; \ No newline at end of file diff --git a/lang/ru/local_webhooks.php b/lang/ru/local_webhooks.php index 2c8f0b7..30a5843 100644 --- a/lang/ru/local_webhooks.php +++ b/lang/ru/local_webhooks.php @@ -28,5 +28,6 @@ $string["linkmanagerservice"] = "Менеджер управления служ $string["local_webhooksdescription"] = ""; $string["managementmanager"] = "Менеджер управления службами"; $string["managementmanageradd"] = "Добавить службу"; +$string["managementmanagerdescription"] = ""; $string["pluginname"] = "Moodle WebHooks"; $string["servicedeleted"] = "Служба была удалена"; \ No newline at end of file diff --git a/settings.php b/settings.php index ceb603a..d893b98 100644 --- a/settings.php +++ b/settings.php @@ -40,6 +40,13 @@ if ($hassiteconfig) { false )); + /* Title Manager */ + $settings->add(new admin_setting_heading( + "defaultsettings", + new lang_string("linkmanagerservice", "local_webhooks"), + new lang_string("managementmanagerdescription", "local_webhooks") + )); + /* Link to the service manager */ $linktext = new lang_string("linkmanagerservice", "local_webhooks"); $link = "wwwroot . "/local/webhooks/managerservice.php\">" . $linktext . ""; -- cgit v1.2.3 From b6659da190d3885f9bfba4b4620da87611a28480 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sun, 22 Oct 2017 18:50:30 +0400 Subject: Added page template editor --- editservice.php | 69 ++++++++++++++++++++++++++++++++++++++++++++++ lang/en/local_webhooks.php | 2 ++ lang/ru/local_webhooks.php | 2 ++ 3 files changed, 73 insertions(+) create mode 100644 editservice.php diff --git a/editservice.php b/editservice.php new file mode 100644 index 0000000..a8bda17 --- /dev/null +++ b/editservice.php @@ -0,0 +1,69 @@ +. + +/** + * Service editor. + * + * @package local_webhooks + * @copyright 2017 "Valentin Popov" + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once(__DIR__ . "/../../config.php"); +require_once(__DIR__ . "/classes/form.php"); + +$idservice = optional_param("idservice", 0, PARAM_INT); + +require_login(); + +$managerservice = new moodle_url("/local/webhooks/managerservice.php"); +$baseurl = new moodle_url("/local/webhooks/editservice.php"); +$PAGE->set_url($baseurl); + +$context = context_system::instance(); +$PAGE->set_context($context); + +/* Preparing a template for data */ +$titlepage = new lang_string("editserviceadds", "local_webhooks"); +$servicerecord = new stdClass; + +if (boolval($idservice)) { + $titlepage = new lang_string("editserviceedits", "local_webhooks"); + $servicerecord = $DB->get_record( + "local_webhooks_service", + array("id" => $idservice), + "*", MUST_EXIST); +} + +/* Page template */ +$PAGE->set_pagelayout("admin"); +$PAGE->set_title($titlepage); +$PAGE->set_heading($titlepage); + +/* Create an editing form */ +$mform = new \local_webhooks\service_edit_form(); + +/* The page title */ +$PAGE->navbar->add(new lang_string("local")); +$PAGE->navbar->add(new lang_string("pluginname", "local_webhooks")); +$PAGE->navbar->add(new lang_string("managementmanager", "local_webhooks"), $managerservice); +$PAGE->navbar->add($titlepage); +echo $OUTPUT->header(); + +/* Displays the form */ +$mform->display(); + +echo $OUTPUT->footer(); \ No newline at end of file diff --git a/lang/en/local_webhooks.php b/lang/en/local_webhooks.php index 9a3993a..81b7a10 100644 --- a/lang/en/local_webhooks.php +++ b/lang/en/local_webhooks.php @@ -22,6 +22,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +$string["editserviceadds"] = "Create a service"; +$string["editserviceedits"] = "Edit the service"; $string["enableservice"] = "Enabled"; $string["enableservice_help"] = "Enable the Event Tracking service."; $string["linkmanagerservice"] = "Service management manager"; diff --git a/lang/ru/local_webhooks.php b/lang/ru/local_webhooks.php index 30a5843..390d14a 100644 --- a/lang/ru/local_webhooks.php +++ b/lang/ru/local_webhooks.php @@ -22,6 +22,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +$string["editserviceadds"] = "Создание службы"; +$string["editserviceedits"] = "Редактирование службы"; $string["enableservice"] = "Включить"; $string["enableservice_help"] = "Включение службы отслеживания событий."; $string["linkmanagerservice"] = "Менеджер управления службами"; -- cgit v1.2.3 From 2d2a683549393c54d12d2de1918dd1f49f5bd0c8 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Mon, 23 Oct 2017 07:13:42 +0400 Subject: Added action handlers --- editservice.php | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/editservice.php b/editservice.php index a8bda17..7b2fcb7 100644 --- a/editservice.php +++ b/editservice.php @@ -23,16 +23,19 @@ */ require_once(__DIR__ . "/../../config.php"); -require_once(__DIR__ . "/classes/form.php"); +require_once(__DIR__ . "/classes/forms.php"); $idservice = optional_param("idservice", 0, PARAM_INT); require_login(); -$managerservice = new moodle_url("/local/webhooks/managerservice.php"); -$baseurl = new moodle_url("/local/webhooks/editservice.php"); -$PAGE->set_url($baseurl); +/* Link generation */ +$urlparameters = array("idservice" => $idservice); +$managerservice = new moodle_url("/local/webhooks/managerservice.php", $urlparameters); +$baseurl = new moodle_url("/local/webhooks/editservice.php", $urlparameters); +$PAGE->set_url($baseurl, $urlparameters); +/* Configure the context of the page */ $context = context_system::instance(); $PAGE->set_context($context); @@ -40,12 +43,35 @@ $PAGE->set_context($context); $titlepage = new lang_string("editserviceadds", "local_webhooks"); $servicerecord = new stdClass; -if (boolval($idservice)) { +/* Create an editing form */ +$mform = new \local_webhooks\service_edit_form($PAGE->url); + +/* Cancel processing */ +if ($mform->is_cancelled()) { + redirect($managerservice); +} + +/* Getting the data */ +if ($idediting = boolval($idservice)) { + $servicerecord = $DB->get_record("local_webhooks_service", array("id" => $idservice), "*", MUST_EXIST); $titlepage = new lang_string("editserviceedits", "local_webhooks"); - $servicerecord = $DB->get_record( - "local_webhooks_service", - array("id" => $idservice), - "*", MUST_EXIST); + $mform->set_data($servicerecord); +} + +/* Processing of received data */ +if ($data = $mform->get_data()) { + if (empty($data->enable)) { + $data->enable = 0; + } + + if ($idediting) { + $data->id = $idservice; + $DB->update_record("local_webhooks_service", $data); + } else { + $DB->insert_record("local_webhooks_service", $data); + } + + redirect($managerservice); } /* Page template */ @@ -53,9 +79,6 @@ $PAGE->set_pagelayout("admin"); $PAGE->set_title($titlepage); $PAGE->set_heading($titlepage); -/* Create an editing form */ -$mform = new \local_webhooks\service_edit_form(); - /* The page title */ $PAGE->navbar->add(new lang_string("local")); $PAGE->navbar->add(new lang_string("pluginname", "local_webhooks")); -- cgit v1.2.3 From ea1c9f7322202a10ab13526cb9734917ac323a00 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Mon, 23 Oct 2017 08:31:09 +0400 Subject: Added a class to work with the form --- classes/forms.php | 81 ++++++++++++++++++++++++++++++++++++++++++++++ lang/en/local_webhooks.php | 4 +++ lang/ru/local_webhooks.php | 4 +++ 3 files changed, 89 insertions(+) create mode 100644 classes/forms.php diff --git a/classes/forms.php b/classes/forms.php new file mode 100644 index 0000000..e3c962f --- /dev/null +++ b/classes/forms.php @@ -0,0 +1,81 @@ +. + +/** + * Defines the form of editing the service. + * + * @package local_webhooks + * @copyright 2017 "Valentin Popov" + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace local_webhooks; + +defined("MOODLE_INTERNAL") || die(); + +require_once($CFG->libdir . "/formslib.php"); +use lang_string; +use moodleform; + +/** + * Description editing form definition. + * + * @copyright 2017 "Valentin Popov" + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class service_edit_form extends moodleform { + /** + * @param string $baseurl + */ + public function __construct($baseurl) { + parent::__construct($baseurl); + } + + /** + * Defines the standard structure of the form. + */ + protected function definition() { + $mform =& $this->_form; + + /* Form heading */ + $mform->addElement("header", "editserviceheader", + new lang_string("editserviceheader", "local_webhooks")); + + /* Name of the service */ + $mform->addElement("text", "title", + new lang_string("editservicetitle", "local_webhooks"), + array("size" => 60)); + $mform->setType("title", PARAM_NOTAGS); + $mform->addRule("title", null, "required"); + + /* Callback address */ + $mform->addElement("text", "url", + new lang_string("editserviceurl", "local_webhooks"), + array("size" => 60)); + $mform->setType("url", PARAM_URL); + $mform->addRule("url", null, "required"); + + /* Enabling the service */ + $mform->addElement("checkbox", "enable", + new lang_string("editserviceenable", "local_webhooks")); + $mform->setType("enable", PARAM_BOOL); + $mform->setDefault("enable", 1); + $mform->setAdvanced("enable"); + + /* Control Panel */ + $this->add_action_buttons(true); + } +} \ No newline at end of file diff --git a/lang/en/local_webhooks.php b/lang/en/local_webhooks.php index 81b7a10..a5dd0d2 100644 --- a/lang/en/local_webhooks.php +++ b/lang/en/local_webhooks.php @@ -24,6 +24,10 @@ $string["editserviceadds"] = "Create a service"; $string["editserviceedits"] = "Edit the service"; +$string["editserviceenable"] = "Activate the service"; +$string["editserviceheader"] = "Service"; +$string["editservicetitle"] = "Name of the service"; +$string["editserviceurl"] = "URL of the service"; $string["enableservice"] = "Enabled"; $string["enableservice_help"] = "Enable the Event Tracking service."; $string["linkmanagerservice"] = "Service management manager"; diff --git a/lang/ru/local_webhooks.php b/lang/ru/local_webhooks.php index 390d14a..0cd9c5f 100644 --- a/lang/ru/local_webhooks.php +++ b/lang/ru/local_webhooks.php @@ -24,6 +24,10 @@ $string["editserviceadds"] = "Создание службы"; $string["editserviceedits"] = "Редактирование службы"; +$string["editserviceenable"] = "Активировать службу"; +$string["editserviceheader"] = "Служба"; +$string["editservicetitle"] = "Название службы"; +$string["editserviceurl"] = "URL-адрес службы"; $string["enableservice"] = "Включить"; $string["enableservice_help"] = "Включение службы отслеживания событий."; $string["linkmanagerservice"] = "Менеджер управления службами"; -- cgit v1.2.3 From 876803b844b66fb766ee0e7350a49c2bf323ce19 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Tue, 24 Oct 2017 04:02:30 +0400 Subject: Rethinking the translation --- lang/en/local_webhooks.php | 17 ++--------------- lang/ru/local_webhooks.php | 17 ++--------------- 2 files changed, 4 insertions(+), 30 deletions(-) diff --git a/lang/en/local_webhooks.php b/lang/en/local_webhooks.php index a5dd0d2..041233f 100644 --- a/lang/en/local_webhooks.php +++ b/lang/en/local_webhooks.php @@ -22,18 +22,5 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -$string["editserviceadds"] = "Create a service"; -$string["editserviceedits"] = "Edit the service"; -$string["editserviceenable"] = "Activate the service"; -$string["editserviceheader"] = "Service"; -$string["editservicetitle"] = "Name of the service"; -$string["editserviceurl"] = "URL of the service"; -$string["enableservice"] = "Enabled"; -$string["enableservice_help"] = "Enable the Event Tracking service."; -$string["linkmanagerservice"] = "Service management manager"; -$string["local_webhooksdescription"] = ""; -$string["managementmanager"] = "Service Management Manager"; -$string["managementmanageradd"] = "Add a service"; -$string["managementmanagerdescription"] = ""; -$string["pluginname"] = "Moodle WebHooks"; -$string["servicedeleted"] = "Service has been removed"; \ No newline at end of file +$string["managerservice"] = "Service management manager"; +$string["pluginname"] = "WebHooks"; \ No newline at end of file diff --git a/lang/ru/local_webhooks.php b/lang/ru/local_webhooks.php index 0cd9c5f..73db595 100644 --- a/lang/ru/local_webhooks.php +++ b/lang/ru/local_webhooks.php @@ -22,18 +22,5 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -$string["editserviceadds"] = "Создание службы"; -$string["editserviceedits"] = "Редактирование службы"; -$string["editserviceenable"] = "Активировать службу"; -$string["editserviceheader"] = "Служба"; -$string["editservicetitle"] = "Название службы"; -$string["editserviceurl"] = "URL-адрес службы"; -$string["enableservice"] = "Включить"; -$string["enableservice_help"] = "Включение службы отслеживания событий."; -$string["linkmanagerservice"] = "Менеджер управления службами"; -$string["local_webhooksdescription"] = ""; -$string["managementmanager"] = "Менеджер управления службами"; -$string["managementmanageradd"] = "Добавить службу"; -$string["managementmanagerdescription"] = ""; -$string["pluginname"] = "Moodle WebHooks"; -$string["servicedeleted"] = "Служба была удалена"; \ No newline at end of file +$string["managerservice"] = "Менеджер управления службами"; +$string["pluginname"] = "WebHooks"; \ No newline at end of file -- cgit v1.2.3 From 2bee24931a95d0b5e0b2a95a15d2b30088ba562e Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Tue, 24 Oct 2017 04:07:29 +0400 Subject: Refactoring the settings page --- settings.php | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/settings.php b/settings.php index d893b98..c2235c2 100644 --- a/settings.php +++ b/settings.php @@ -26,29 +26,20 @@ defined("MOODLE_INTERNAL") || die(); $settings = null; if ($hassiteconfig) { - $settings = new admin_settingpage( - "local_webhooks", - new lang_string("pluginname", "local_webhooks") - ); + $settings = new admin_settingpage("local_webhooks", + new lang_string("pluginname", "local_webhooks")); $ADMIN->add("localplugins", $settings); $settings->add(new admin_setting_configcheckbox( "local_webhooks/enabled", - new lang_string("enableservice", "local_webhooks"), - new lang_string("enableservice_help", "local_webhooks"), - false - )); - - /* Title Manager */ - $settings->add(new admin_setting_heading( - "defaultsettings", - new lang_string("linkmanagerservice", "local_webhooks"), - new lang_string("managementmanagerdescription", "local_webhooks") - )); + new lang_string("enable", "moodle"), + new lang_string("enablews", "webservice"), + false)); /* Link to the service manager */ - $linktext = new lang_string("linkmanagerservice", "local_webhooks"); - $link = "wwwroot . "/local/webhooks/managerservice.php\">" . $linktext . ""; - $settings->add(new admin_setting_heading("local_webhooks_addheading", "", $link)); + $linktext = new lang_string("managerservice", "local_webhooks"); + $linkurl = new moodle_url("/local/webhooks/managerservice.php"); + $settings->add(new admin_setting_heading("local_webhooks_managerservice", null, + html_writer::link($linkurl, $linktext))); } \ No newline at end of file -- cgit v1.2.3 From a8ad95f44e1f9f9410b38f69187d843b2b806928 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Tue, 24 Oct 2017 04:13:48 +0400 Subject: Refactoring the service editor --- editservice.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/editservice.php b/editservice.php index 7b2fcb7..5c6d1f6 100644 --- a/editservice.php +++ b/editservice.php @@ -40,7 +40,7 @@ $context = context_system::instance(); $PAGE->set_context($context); /* Preparing a template for data */ -$titlepage = new lang_string("editserviceadds", "local_webhooks"); +$titlepage = new lang_string("externalservice", "webservice"); $servicerecord = new stdClass; /* Create an editing form */ @@ -54,7 +54,6 @@ if ($mform->is_cancelled()) { /* Getting the data */ if ($idediting = boolval($idservice)) { $servicerecord = $DB->get_record("local_webhooks_service", array("id" => $idservice), "*", MUST_EXIST); - $titlepage = new lang_string("editserviceedits", "local_webhooks"); $mform->set_data($servicerecord); } @@ -76,13 +75,13 @@ if ($data = $mform->get_data()) { /* Page template */ $PAGE->set_pagelayout("admin"); -$PAGE->set_title($titlepage); $PAGE->set_heading($titlepage); +$PAGE->set_title($titlepage); /* The page title */ -$PAGE->navbar->add(new lang_string("local")); +$PAGE->navbar->add(new lang_string("local", "moodle")); $PAGE->navbar->add(new lang_string("pluginname", "local_webhooks")); -$PAGE->navbar->add(new lang_string("managementmanager", "local_webhooks"), $managerservice); +$PAGE->navbar->add(new lang_string("managerservice", "local_webhooks"), $managerservice); $PAGE->navbar->add($titlepage); echo $OUTPUT->header(); -- cgit v1.2.3 From 4273d97f5f900cd23f58a8089bfa0bdb0dfccc82 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Tue, 24 Oct 2017 05:01:07 +0400 Subject: The finished version of the service manager --- managerservice.php | 57 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/managerservice.php b/managerservice.php index 62131d2..c7c6749 100644 --- a/managerservice.php +++ b/managerservice.php @@ -23,42 +23,83 @@ */ require_once(__DIR__ . "/../../config.php"); +require_once($CFG->libdir . "/tablelib.php"); + $deleteservice = optional_param("deleteservice", 0, PARAM_INT); require_login(); -$baseurl = new moodle_url("/local/webhooks/managerservice.php"); + +/* Link generation */ +$managerservice = "/local/webhooks/managerservice.php"; +$editservice = "/local/webhooks/editservice.php"; +$baseurl = new moodle_url($managerservice); $PAGE->set_url($baseurl); +/* Configure the context of the page */ $context = context_system::instance(); $PAGE->set_context($context); /* Delete the service */ if ($deleteservice && confirm_sesskey()) { $DB->delete_records("local_webhooks_service", array("id" => $deleteservice)); - redirect($PAGE->url, new lang_string("servicedeleted", "local_webhooks")); + redirect($PAGE->url, new lang_string("deleted", "moodle")); } /* Retrieving a list of services */ $select = null; -$callbacks = $DB->get_records_select("local_webhooks_service", $select, null, $DB->sql_order_by_text("title")); +$callbacks = $DB->get_records_select("local_webhooks_service", $select, null, $DB->sql_order_by_text("id")); /* Page template */ -$titlepage = new lang_string("managementmanager", "local_webhooks"); +$titlepage = new lang_string("managerservice", "local_webhooks"); $PAGE->set_pagelayout("standard"); $PAGE->set_title($titlepage); $PAGE->set_heading($titlepage); /* The page title */ -$PAGE->navbar->add(new lang_string("local")); +$PAGE->navbar->add(new lang_string("local", "moodle")); $PAGE->navbar->add(new lang_string("pluginname", "local_webhooks")); $PAGE->navbar->add($titlepage, $baseurl); echo $OUTPUT->header(); -/* @todo: Place the formation table */ +/* Table declaration */ +$table = new flexible_table("callbacks-table"); + +/* 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"))); +$table->define_baseurl($baseurl); +$table->setup(); + +foreach ($callbacks as $callback) { + /* Filling of information columns */ + $titlecallback = html_writer::div($callback->title, "title"); + $urlcallback = html_writer::div($callback->url, "url"); + + /* Link for editing */ + $editlink = new moodle_url($editservice, + array("idservice" => $callback->id)); + $edititem = $OUTPUT->action_icon($editlink, + new pix_icon("t/edit", get_string("edit"))); + + /* Link to remove */ + $deletelink = new moodle_url($managerservice, + array("deleteservice" => $callback->id, "sesskey" => sesskey())); + $deleteitem = $OUTPUT->action_icon($deletelink, + new pix_icon("t/delete", get_string("delete"))); + + /* Adding data to the table */ + $table->add_data(array($titlecallback, $urlcallback, $edititem . $deleteitem)); +} + +/* Display the table */ +$table->print_html(); /* Add service button */ $addurl = new moodle_url("/local/webhooks/editservice.php"); -$addurltext = new lang_string("managementmanageradd", "local_webhooks"); -echo "
" . $OUTPUT->single_button($addurl, $addurltext, "get") . "
"; +echo $OUTPUT->single_button($addurl, + new lang_string("addaservice", "webservice"), "get"); echo $OUTPUT->footer(); \ No newline at end of file -- cgit v1.2.3 From ed34626ef69805cb543e8765e0bbbc62f163d8cb Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Tue, 24 Oct 2017 05:03:44 +0400 Subject: Refactoring the editor class --- classes/forms.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/classes/forms.php b/classes/forms.php index e3c962f..c232a7b 100644 --- a/classes/forms.php +++ b/classes/forms.php @@ -52,25 +52,25 @@ class service_edit_form extends moodleform { /* Form heading */ $mform->addElement("header", "editserviceheader", - new lang_string("editserviceheader", "local_webhooks")); + new lang_string("service", "webservice")); /* Name of the service */ $mform->addElement("text", "title", - new lang_string("editservicetitle", "local_webhooks"), + new lang_string("name", "moodle"), array("size" => 60)); $mform->setType("title", PARAM_NOTAGS); $mform->addRule("title", null, "required"); /* Callback address */ $mform->addElement("text", "url", - new lang_string("editserviceurl", "local_webhooks"), + new lang_string("url", "moodle"), array("size" => 60)); $mform->setType("url", PARAM_URL); $mform->addRule("url", null, "required"); /* Enabling the service */ $mform->addElement("checkbox", "enable", - new lang_string("editserviceenable", "local_webhooks")); + new lang_string("enable", "moodle")); $mform->setType("enable", PARAM_BOOL); $mform->setDefault("enable", 1); $mform->setAdvanced("enable"); -- cgit v1.2.3 From 8ebb1381c993c7174eaa2aff9587f98239c0a954 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Wed, 25 Oct 2017 08:46:07 +0400 Subject: Adaptation of the handler under new conditions --- classes/events.php | 61 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/classes/events.php b/classes/events.php index bf77d4d..5dfb0cf 100644 --- a/classes/events.php +++ b/classes/events.php @@ -24,19 +24,68 @@ namespace local_webhooks; -defined('MOODLE_INTERNAL') || die(); +defined("MOODLE_INTERNAL") || die(); +/** + * Defines how to work with events. + * + * @copyright 2017 "Valentin Popov" + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ class events { + /** + * External handler. + * + * @param object $event + */ public static function handler($event) { - $enabled = boolval(get_config('local_webhooks', 'enabled')); - $url = get_config('local_webhooks', 'url'); + $config = get_config("local_webhooks"); - if ($enabled && !empty($url)) { + if (boolval($config->enable)) { $data = $event->get_data(); - $json = json_encode($data); + self::transmitter($data); + } + } + + /** + * Transmitter, processing event and services. + * + * @param array $data + */ + private static function transmitter($data) { + global $DB; + $callbacks = $DB->get_recordset("local_webhooks_service"); + + if ($callbacks->valid()) { + foreach ($callbacks as $callback) { + self::send($data, $callback); + } + } + + $callbacks->close(); + } + + /** + * Sending data to the node. + * + * @param array $data + * @param object $callback + */ + private static function send($data, $callback) { + if ($callback->enable) { $curl = new curl(); - $curl::request($url, $json); + $package = self::packup($data); + $curl::request($callback->url, $package); } } + + /** + * Packs the data for transmission. + * + * @param array $data + */ + private static function packup($data) { + return json_encode($data); + } } \ No newline at end of file -- cgit v1.2.3