Refactoring function code plugins

This commit is contained in:
Valentin Popov 2017-12-21 09:59:14 +04:00
parent 97711bb186
commit 78e9e435cc

157
lib.php
View File

@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** /**
* Library code used by the service control interfaces. * This file contains the functions used by the plugin.
* *
* @package local_webhooks * @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link> * @copyright 2017 "Valentin Popov" <info@valentineus.link>
@ -35,18 +35,37 @@ require_once(__DIR__ . "/locallib.php");
function local_webhooks_change_status($serviceid) { function local_webhooks_change_status($serviceid) {
global $DB; global $DB;
$result = false; $result = false;
$conditions = array("id" => $serviceid); $conditions = array("id" => $serviceid);
if ($DB->record_exists("local_webhooks_service", $conditions)) { if ($DB->record_exists("local_webhooks_service", $conditions)) {
$enabled = $DB->get_field("local_webhooks_service", "enable", $conditions, IGNORE_MISSING); $enabled = $DB->get_field("local_webhooks_service", "enable", $conditions, IGNORE_MISSING);
$result = $DB->set_field("local_webhooks_service", "enable", !boolval($enabled), $conditions); $result = $DB->set_field("local_webhooks_service", "enable", !boolval($enabled), $conditions);
} }
return boolval($result); return boolval($result);
} }
/** /**
* Getting a list of all services. * Get the record from the database.
*
* @param number $serviceid
* @return object
*/
function local_webhooks_get_record($serviceid) {
global $DB;
$servicerecord = $DB->get_record("local_webhooks_service", array("id" => $serviceid), "*", MUST_EXIST);
if (!empty($servicerecord->events)) {
$servicerecord->events = local_webhooks_deserialization_data($servicerecord->events);
}
return $servicerecord;
}
/**
* Get all records from the database.
* *
* @param number $limitfrom * @param number $limitfrom
* @param number $limitnum * @param number $limitnum
@ -55,130 +74,120 @@ function local_webhooks_change_status($serviceid) {
function local_webhooks_get_list_records($limitfrom = 0, $limitnum = 0) { function local_webhooks_get_list_records($limitfrom = 0, $limitnum = 0) {
global $DB; global $DB;
$listservices = $DB->get_records("local_webhooks_service", null, "id", "*", $limitfrom, $limitnum); $listrecords = $DB->get_records("local_webhooks_service", null, "id", "*", $limitfrom, $limitnum);
foreach ($listservices as $servicerecord) { foreach ($listrecords as $servicerecord) {
if (!empty($servicerecord->events)) { if (!empty($servicerecord->events)) {
$servicerecord->events = local_webhooks_unarchive_data($servicerecord->events); $servicerecord->events = local_webhooks_deserialization_data($servicerecord->events);
} }
} }
return $listservices; return $listrecords;
} }
/** /**
* Getting information about the service. * Create an entry in the database.
* *
* @param number $serviceid * @param object $record
* @return object * @return boolean
*/ */
function local_webhooks_get_record($serviceid = 0) { function local_webhooks_create_record($record) {
global $DB; global $DB;
$servicerecord = $DB->get_record("local_webhooks_service", array("id" => $serviceid), "*", MUST_EXIST); if (!empty($record->events)) {
$record->events = local_webhooks_serialization_data($record->events);
if (!empty($servicerecord->events)) {
$servicerecord->events = local_webhooks_unarchive_data($servicerecord->events);
} }
return $servicerecord; $result = $DB->insert_record("local_webhooks_service", $record, true, false);
} return boolval($result);
/**
* Clear the database table.
*/
function local_webhooks_remove_list_records() {
global $DB;
$DB->delete_records("local_webhooks_service", null);
}
/**
* Delete the record.
*
* @param number $serviceid
*/
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. * Update the record in the database.
* *
* @param object $data * @param object $data
* @param boolean $insert
* @return boolean * @return boolean
*/ */
function local_webhooks_update_record($data, $insert = true) { function local_webhooks_update_record($record) {
global $DB; global $DB;
if (empty($data->events)) { if (!empty($record->events)) {
$data->events = array(); $record->events = local_webhooks_serialization_data($record->events);
}
$data->events = local_webhooks_archiving_data($data->events);
if (boolval($insert)) {
$result = $DB->insert_record("local_webhooks_service", $data, true, false);
local_webhooks_events::service_added($result);
} else {
$result = $DB->update_record("local_webhooks_service", $data, false);
local_webhooks_events::service_updated($data->id);
} }
$result = $DB->update_record("local_webhooks_service", $record, false);
return boolval($result); return boolval($result);
} }
/** /**
* Make a backup copy of all the services. * Delete the record from the database.
*
* @param number $serviceid
* @return boolean
*/
function local_webhooks_delete_record($serviceid) {
global $DB;
$result = $DB->delete_records("local_webhooks_service", array("id" => $serviceid));
return boolval($result);
}
/**
* Delete all records from the database.
*
* @return boolean
*/
function local_webhooks_delete_all_records() {
global $DB;
$result = $DB->delete_records("local_webhooks_service", null);
return boolval($result);
}
/**
* Create a backup.
* *
* @return string * @return string
*/ */
function local_webhooks_create_backup() { function local_webhooks_create_backup() {
$listservices = local_webhooks_get_list_records(); $listrecords = local_webhooks_get_list_records();
$listservices = local_webhooks_archiving_data($listservices); $result = local_webhooks_serialization_data($listrecords);
local_webhooks_events::backup_performed(); return $result;
return $listservices;
} }
/** /**
* Restore the data from the backup. * Restore from a backup.
* *
* @param string $data * @param string $data
*/ */
function local_webhooks_restore_backup($listservices = "") { function local_webhooks_restore_backup($data, $deleterecords = false) {
$listservices = local_webhooks_unarchive_data($listservices); $listrecords = local_webhooks_deserialization_data($data);
local_webhooks_remove_list_records(); if (boolval($deleterecords)) {
local_webhooks_delete_all_records();
foreach ($listservices as $servicerecord) {
local_webhooks_update_record($servicerecord, true);
} }
local_webhooks_events::backup_restored(); foreach ($listrecords as $servicerecord) {
local_webhooks_create_record($servicerecord);
}
} }
/** /**
* Compress an array into a string. * Data serialization.
* *
* @param array $data * @param array|object $data
* @return string * @return string
*/ */
function local_webhooks_archiving_data($data = array()) { function local_webhooks_serialization_data($data) {
$result = base64_encode(gzcompress(serialize($data), 3)); $result = serialize($data);
return $result; return $result;
} }
/** /**
* Gets an array from a compressed string. * Data deserialization.
* *
* @param string $data * @param string $data
* @return array * @return array|object
*/ */
function local_webhooks_unarchive_data($data = "") { function local_webhooks_deserialization_data($data) {
$result = unserialize(gzuncompress(base64_decode($data))); $result = unserialize($data);
return $result; return $result;
} }