Refactoring the code of the service editor

This commit is contained in:
Valentin Popov 2017-11-23 17:37:41 +04:00
parent 1ad4527c1d
commit 5639dc7b0f

View File

@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Service editor.
* Page for editing the service.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
@ -24,6 +24,8 @@
require_once(__DIR__ . "/../../config.php");
require_once(__DIR__ . "/classes/editform.php");
require_once(__DIR__ . "/lib.php");
require_once($CFG->libdir . "/adminlib.php");
/* Optional parameters */
@ -39,7 +41,7 @@ admin_externalpage_setup("local_webhooks", "", null, $baseurl, array());
$context = context_system::instance();
/* Create an editing form */
$mform = new \local_webhooks\service_edit_form($PAGE->url);
$mform = new local_webhooks\service_edit_form($PAGE->url);
/* Cancel processing */
if ($mform->is_cancelled()) {
@ -49,35 +51,18 @@ if ($mform->is_cancelled()) {
/* Getting the data */
$servicerecord = new stdClass();
if ($editing = boolval($serviceid)) {
$servicerecord = $DB->get_record("local_webhooks_service", array("id" => $serviceid), "*", MUST_EXIST);
$servicerecord = local_webhooks_get_record($serviceid);
$mform->set_data($servicerecord);
}
/* Processing of received data */
if ($data = $mform->get_data()) {
if (empty($data->events)) {
$data->events = array();
}
/* Pack the list of events */
$data->events = base64_encode(gzcompress(serialize($data->events), 9));
if ($editing) {
$data->id = $serviceid;
$DB->update_record("local_webhooks_service", $data);
/* Run the event */
$event = \local_webhooks\event\service_updated::create(array("context" => $context, "objectid" => $data->id));
$event->trigger();
local_webhooks_update_record($data, false);
redirect($managerservice, new lang_string("eventwebserviceserviceupdated", "webservice"));
} else {
$servicenewid = $DB->insert_record("local_webhooks_service", $data);
/* Run the event */
$event = \local_webhooks\event\service_added::create(array("context" => $context, "objectid" => $servicenewid));
$event->trigger();
local_webhooks_update_record($data, true);
redirect($managerservice, new lang_string("eventwebserviceservicecreated", "webservice"));
}
}