From 3b1b1ea0e1d0ec687acbd26e9498a605e032d05e Mon Sep 17 00:00:00 2001 From: Brendan Heywood Date: Wed, 25 Mar 2020 22:21:35 +1100 Subject: [PATCH] Allow default start time using cron syntax #163 --- classes/local/outagelib.php | 1 + edit.php | 18 ++++++++++++++++++ lang/en/auth_outage.php | 2 ++ settings.php | 7 +++++++ version.php | 4 ++-- 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/classes/local/outagelib.php b/classes/local/outagelib.php index 8af5aa9..2e28ff8 100644 --- a/classes/local/outagelib.php +++ b/classes/local/outagelib.php @@ -152,6 +152,7 @@ class outagelib { return [ 'allowedips' => '', 'css' => '', + 'default_time' => '', 'default_autostart' => '0', 'default_duration' => (string)(60 * 60), 'default_warning_duration' => (string)(60 * 60), diff --git a/edit.php b/edit.php index 5cd25f8..d707989 100644 --- a/edit.php +++ b/edit.php @@ -61,6 +61,24 @@ if ($clone) { } else { $config = outagelib::get_config(); $time = time(); + + $default = $config->default_time; + if ($default) { + + // Lean on the Task API to convert the cron syntax to + // the next valid outage date and time. + $parts = explode(' ', $default); + if (count($parts) == 5) { + $task = new \core\task\calendar_cron_task(); + $task->set_minute($parts[0]); + $task->set_hour($parts[1]); + $task->set_day($parts[2]); + $task->set_month($parts[3]); + $task->set_day_of_week($parts[4]); + $time = $task->get_next_scheduled_time(); + } + } + $outage = new outage([ 'autostart' => $config->default_autostart, 'starttime' => $time, diff --git a/lang/en/auth_outage.php b/lang/en/auth_outage.php index ac1c164..3ea661e 100644 --- a/lang/en/auth_outage.php +++ b/lang/en/auth_outage.php @@ -80,6 +80,8 @@ $string['defaultoutageduration'] = 'Outage duration'; $string['defaultoutagedurationdescription'] = 'Default duration (in minutes) of an outage.'; $string['defaultwarningduration'] = 'Warning duration'; $string['defaultwarningdurationdescription'] = 'Default warning time (in minutes) for outages.'; +$string['defaulttime'] = 'Default time'; +$string['defaulttimedescription'] = 'The default time for the next outage. This is expressed in cron notation, eg to say that a regular outage window is any thursday at 7pm you would set this to:
0 19 * * 4
'; $string['defaulttitle'] = 'Title'; $string['defaulttitledescription'] = 'Default title for outages. Use {{start}} and {{stop}} placeholders as required.'; $string['defaulttitlevalue'] = 'System down from {{start}} for {{duration}}'; diff --git a/settings.php b/settings.php index 9c3be2c..c86670e 100644 --- a/settings.php +++ b/settings.php @@ -59,6 +59,13 @@ if ($hassiteconfig && is_enabled_auth('outage')) { $defaults['default_duration'], 60 )); + $settings->add(new admin_setting_configtext( + 'auth_outage/default_time', + get_string('defaulttime', 'auth_outage'), + get_string('defaulttimedescription', 'auth_outage'), + '', + PARAM_TEXT + )); $settings->add(new admin_setting_configtext( 'auth_outage/default_title', get_string('defaulttitle', 'auth_outage'), diff --git a/version.php b/version.php index b07af31..cd60ab6 100644 --- a/version.php +++ b/version.php @@ -28,7 +28,7 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = "auth_outage"; -$plugin->version = 2020021300; // The current plugin version (Date: YYYYMMDDXX). -$plugin->release = '1.0.11'; // Human-readable release information. +$plugin->version = 2020032500; // The current plugin version (Date: YYYYMMDDXX). +$plugin->release = 2020032500; // Human-readable release information. $plugin->requires = 2017051500; // Requires 3.3 and higher. $plugin->maturity = MATURITY_STABLE; // Suitable for PRODUCTION environments!