Created a worked upgrade mechanism
Signed-off-by: Valentin Popov <info@valentineus.link>
This commit is contained in:
parent
0fe3b14f29
commit
7ae31a6eee
147
db/upgrade.php
147
db/upgrade.php
@ -1,4 +1,19 @@
|
||||
<?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 is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
@ -14,16 +29,19 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Keeps track of upgrades to the 'local_webhooks' plugin.
|
||||
*
|
||||
* @copyright 2018 'Valentin Popov' <info@valentineus.link>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @package local_webhooks
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* It's a base64 encoded?
|
||||
*
|
||||
* @param string $string
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function is_base64(string $string) {
|
||||
return base64_encode(base64_decode($string)) === $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a table local_webhooks_events.
|
||||
*
|
||||
@ -39,12 +57,12 @@ function create_table_events() {
|
||||
$table = new xmldb_table('local_webhooks_events');
|
||||
|
||||
// Adding fields to table local_webhooks_events.
|
||||
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
|
||||
$table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
|
||||
$table->add_field('serviceid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
|
||||
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE);
|
||||
$table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL);
|
||||
$table->add_field('serviceid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL);
|
||||
|
||||
// Adding keys to table local_webhooks_events.
|
||||
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
|
||||
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
|
||||
|
||||
// Conditionally launch create table for local_webhooks_events.
|
||||
if (!$dbman->table_exists($table)) {
|
||||
@ -88,15 +106,15 @@ function create_table_service() {
|
||||
$table = new xmldb_table('local_webhooks_service');
|
||||
|
||||
// Adding fields to table local_webhooks_service.
|
||||
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
|
||||
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE);
|
||||
$table->add_field('header', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, 'application/json');
|
||||
$table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
|
||||
$table->add_field('point', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
|
||||
$table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL);
|
||||
$table->add_field('point', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL);
|
||||
$table->add_field('status', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
|
||||
$table->add_field('token', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
|
||||
$table->add_field('token', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL);
|
||||
|
||||
// Adding keys to table local_webhooks_service.
|
||||
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
|
||||
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
|
||||
|
||||
// Conditionally launch create table for local_webhooks_service.
|
||||
if (!$dbman->table_exists($table)) {
|
||||
@ -107,19 +125,26 @@ function create_table_service() {
|
||||
/**
|
||||
* Save data to database.
|
||||
*
|
||||
* @param $records
|
||||
* @param array $records
|
||||
*
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
function save_records($records) {
|
||||
function save_records(array $records) {
|
||||
global $DB;
|
||||
|
||||
foreach ($records as $record) {
|
||||
$recordid = $DB->insert_record('local_webhooks_service', (object) $record, true, false);
|
||||
if (!is_array($record)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$recordid = $DB->insert_record('local_webhooks_service', (object) $record);
|
||||
|
||||
if ($recordid && is_array($record['events'])) {
|
||||
foreach ($record['events'] as $eventname) {
|
||||
$event = array('name' => $eventname, 'serviceid' => $recordid);
|
||||
$DB->insert_record('local_webhooks_events', (object) $event, true, false);
|
||||
$DB->insert_record('local_webhooks_events', (object) [
|
||||
'name' => $eventname,
|
||||
'serviceid' => $recordid,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -137,26 +162,26 @@ function save_records($records) {
|
||||
* @throws \downgrade_exception
|
||||
* @throws \upgrade_exception
|
||||
*/
|
||||
function xmldb_local_webhooks_upgrade($oldversion = 0) {
|
||||
function xmldb_local_webhooks_upgrade(int $oldversion) {
|
||||
global $DB;
|
||||
|
||||
/* Update from versions 3.* */
|
||||
if ($oldversion < 2017112600 || $oldversion === 2018061900) {
|
||||
$records = $DB->get_records('local_webhooks_service', null, 'id', '*', 0, 0);
|
||||
if (in_array($oldversion, [2017112600, 2018061900, 2018061910, 2018061920], true)) {
|
||||
$records = $DB->get_records('local_webhooks_service', null, 'id');
|
||||
|
||||
$services = array();
|
||||
$services = [];
|
||||
|
||||
foreach ($records as $record) {
|
||||
if (!empty($record->events)) {
|
||||
$record->events = unserialize(gzuncompress(base64_decode($record->events)));
|
||||
if (!is_object($record)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$service = array(
|
||||
$service = [
|
||||
'name' => $record->title,
|
||||
'point' => $record->url,
|
||||
'status' => (bool) $record->enable,
|
||||
'token' => $record->token,
|
||||
);
|
||||
];
|
||||
|
||||
if ($record->type === 'json') {
|
||||
$service['header'] = 'application/json';
|
||||
@ -164,9 +189,19 @@ function xmldb_local_webhooks_upgrade($oldversion = 0) {
|
||||
$service['header'] = 'application/x-www-form-urlencoded';
|
||||
}
|
||||
|
||||
foreach ($record->events as $eventname => $eventstatus) {
|
||||
if ((bool) $eventstatus) {
|
||||
$service['events'][] = $eventname;
|
||||
if (!empty($record->events)) {
|
||||
$record->events = unserialize(gzuncompress(base64_decode($record->events)), [
|
||||
'allowed_classes' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
if (is_array($record->events) && !empty($record->events)) {
|
||||
foreach ($record->events as $eventname => $eventstatus) {
|
||||
if ((bool) $eventstatus) {
|
||||
$service['events'][] = is_base64($eventname)
|
||||
? base64_decode($eventname, true)
|
||||
: $eventname;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -181,51 +216,7 @@ function xmldb_local_webhooks_upgrade($oldversion = 0) {
|
||||
/* Saving records */
|
||||
save_records($services);
|
||||
|
||||
upgrade_plugin_savepoint(true, 2018061900, 'local', 'webhooks');
|
||||
}
|
||||
|
||||
/* 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 ($record->type === 'json') {
|
||||
$service['header'] = 'application/json';
|
||||
} else {
|
||||
$service['header'] = 'application/x-www-form-urlencoded';
|
||||
}
|
||||
|
||||
foreach ($record->events as $eventname => $eventstatus) {
|
||||
if ((bool) $eventstatus) {
|
||||
$service['events'][] = $eventname;
|
||||
}
|
||||
}
|
||||
|
||||
$services[] = $service;
|
||||
}
|
||||
|
||||
/* Update structure */
|
||||
drop_table_service();
|
||||
create_table_events();
|
||||
create_table_service();
|
||||
|
||||
/* Saving records */
|
||||
save_records($services);
|
||||
|
||||
upgrade_plugin_savepoint(true, 2018022500, 'local', 'webhooks');
|
||||
upgrade_plugin_savepoint(true, 2019040100, 'local', 'webhooks');
|
||||
}
|
||||
|
||||
return true;
|
||||
|
31
version.php
31
version.php
@ -1,4 +1,19 @@
|
||||
<?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 is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
@ -14,19 +29,11 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Information about the version of the plugin.
|
||||
*
|
||||
* @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();
|
||||
|
||||
$plugin->release = '4.0.0-rc.2 (Build: 2018022500)';
|
||||
$plugin->version = 2018022500;
|
||||
$plugin->requires = 2016112900;
|
||||
$plugin->component = 'local_webhooks';
|
||||
$plugin->dependencies = ['report_eventlist' => 2016120500];
|
||||
$plugin->maturity = MATURITY_RC;
|
||||
$plugin->dependencies = array('report_eventlist' => 2016120500);
|
||||
$plugin->release = '5.0.0-rc.1 (Build: 2019040100)';
|
||||
$plugin->requires = 2016112900;
|
||||
$plugin->version = 2019040100;
|
Loading…
x
Reference in New Issue
Block a user