diff options
author | Valentin Popov <info@valentineus.link> | 2018-10-26 04:15:10 +0300 |
---|---|---|
committer | Valentin Popov <info@valentineus.link> | 2018-10-26 04:20:18 +0300 |
commit | 09c54f6627a028941fbaba281733f38b28d3ea8c (patch) | |
tree | c967855d5648f05fc7d1f45183cc2b4c7445ffb0 /db | |
parent | a01ad5f97c1ddc508348f6d2533e9b8eac0639b8 (diff) | |
download | local_webhooks-09c54f6627a028941fbaba281733f38b28d3ea8c.tar.xz local_webhooks-09c54f6627a028941fbaba281733f38b28d3ea8c.zip |
Update from version 4.0.0-rc.*
Signed-off-by: Valentin Popov <info@valentineus.link>
Diffstat (limited to 'db')
-rw-r--r-- | db/upgrade.php | 77 |
1 files changed, 61 insertions, 16 deletions
diff --git a/db/upgrade.php b/db/upgrade.php index 5f34314..0ce98b9 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -105,6 +105,26 @@ function create_table_service() { } /** + * Save data to database. + * + * @param $records + * + * @throws \dml_exception + */ +function save_records($records) { + global $DB; + + foreach ($records as $record) { + $recordid = $DB->insert_record('local_webhooks_service', (object) $record, true, false); + if ($recordid && is_array($record['events'])) { + foreach ($record['events'] as $eventname) { + $DB->insert_record('local_webhooks_events', (object) array('name' => $eventname, 'serviceid' => $recordid), true, false); + } + } + } +} + +/** * Function to upgrade 'local_webhooks'. * * @param int $oldversion @@ -143,8 +163,8 @@ function xmldb_local_webhooks_upgrade($oldversion = 0) { $service['header'] = 'application/x-www-form-urlencoded'; } - foreach ($record->events as $eventname => $eventStatus) { - if ((bool) $eventStatus) { + foreach ($record->events as $eventname => $eventstatus) { + if ((bool) $eventstatus) { $service['events'][] = $eventname; } } @@ -158,29 +178,54 @@ function xmldb_local_webhooks_upgrade($oldversion = 0) { create_table_service(); /* Saving records */ - foreach ($services as $service) { - $serviceid = $DB->insert_record('local_webhooks_service', (object) $service, true, false); - if ($serviceid && is_array($service['events'])) { - foreach ($service['events'] as $eventname) { - $DB->insert_record('local_webhooks_events', (object) array('name' => $eventname, 'serviceid' => $serviceid), true, false); - } - } - } + save_records($services); upgrade_plugin_savepoint(true, 2018061900, 'local', 'webhooks'); } - /* Update from version 4.0.0-rc.1 */ + /* Update from version 4.0.0-rc.* */ + if ($oldversion === 2017122900 || $oldversion === 2018022200 || $oldversion === 2018022500) { + $records = $DB->get_records('local_webhooks_service', null, 'id', '*', 0, 0); + + $services = array(); + + foreach ($records as $record) { + if (!empty($record->events)) { + $record->events = unserialize($record->events); + } + + $service = array( + 'name' => $record->title, + 'point' => $record->url, + 'status' => (bool) $record->enable, + 'token' => $record->token, + ); - /* if ($oldversion === 2017122900) {} */ + if ($record->type === 'json') { + $service['header'] = 'application/json'; + } else { + $service['header'] = 'application/x-www-form-urlencoded'; + } - /* Update from version 4.0.0-rc.2 */ + foreach ($record->events as $eventname => $eventstatus) { + if ((bool) $eventstatus) { + $service['events'][] = $eventname; + } + } - /* if ($oldversion === 2018022200) {} */ + $services[] = $service; + } - /* Update from version 4.0.0-rc.3 */ + /* Update structure */ + drop_table_service(); + create_table_events(); + create_table_service(); - /* if ($oldversion === 2018022500) {} */ + /* Saving records */ + save_records($services); + + upgrade_plugin_savepoint(true, 2018022500, 'local', 'webhooks'); + } return true; }
\ No newline at end of file |