From 9ed0b741126ed46b7db6afc4ce9a900592ea4405 Mon Sep 17 00:00:00 2001 From: Brendan Heywood Date: Thu, 26 Mar 2020 01:30:03 +1100 Subject: [PATCH] Added next 3 available window button for new outages #163 --- classes/form/outage/edit.php | 3 ++- classes/local/outagelib.php | 34 ++++++++++++++++++++++++++++++++++ edit.php | 23 +++-------------------- views/manage.php | 22 ++++++++++++++++++---- 4 files changed, 57 insertions(+), 25 deletions(-) diff --git a/classes/form/outage/edit.php b/classes/form/outage/edit.php index b37df11..d27d77d 100644 --- a/classes/form/outage/edit.php +++ b/classes/form/outage/edit.php @@ -164,7 +164,8 @@ class edit extends moodleform { if (!empty($outage->id) && $outage->autostart && $outage->starttime < time() && $outage->stoptime > time()) { $warning = $mform->getElement('warningreenablemaintenancemode'); - $warning->setValue($OUTPUT->notification(get_string('warningreenablemaintenancemode', 'auth_outage'), 'notifywarning')); + $warning->setValue($OUTPUT->notification(get_string('warningreenablemaintenancemode', 'auth_outage'), + 'notifywarning')); } } else { throw new coding_exception('$outage must be an outage object.', $outage); diff --git a/classes/local/outagelib.php b/classes/local/outagelib.php index 2e28ff8..a6dceda 100644 --- a/classes/local/outagelib.php +++ b/classes/local/outagelib.php @@ -79,6 +79,40 @@ class outagelib { self::inject(); } + /** + * Given a time, usually now, when is the next outage window? + */ + public static function get_next_window($time = null) { + + $config = self::get_config(); + + if (!$time) { + $time = time(); + } + + $default = $config->default_time; + if ($default) { + + // First try natural language parsing. + $time = strtotime($default, $time); + + // 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(); + } + } + return $time; + } + + /** * Will check for ongoing or warning outages and will attach the message bar as required. */ diff --git a/edit.php b/edit.php index bbc6ad8..e2b0e18 100644 --- a/edit.php +++ b/edit.php @@ -47,6 +47,7 @@ if ($mform->is_cancelled()) { $clone = optional_param('clone', 0, PARAM_INT); $edit = optional_param('edit', 0, PARAM_INT); +$time = optional_param('starttime', 0, PARAM_INT); if ($clone && $edit) { throw new invalid_parameter_exception('Cannot provide both clone and edit ids.'); } @@ -60,26 +61,8 @@ if ($clone) { $action = 'outageedit'; } else { $config = outagelib::get_config(); - $time = time(); - - $default = $config->default_time; - if ($default) { - - // First try natural language parsing. - $time = strtotime($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(); - } + if (empty($time)) { + $time = outagelib::get_next_window(); } $outage = new outage([ diff --git a/views/manage.php b/views/manage.php index 52c856c..f2fee40 100644 --- a/views/manage.php +++ b/views/manage.php @@ -27,6 +27,7 @@ use auth_outage\output\manage\history_table; use auth_outage\output\manage\planned_table; use auth_outage\output\renderer; use auth_outage\dml\outagedb; +use auth_outage\local\outagelib; defined('MOODLE_INTERNAL') || die(); @@ -49,10 +50,23 @@ echo $viewbag['warning']; $table->finish_output(); ?> - - - single_button($urlnew, get_string('outagecreate', 'auth_outage')); ?> - + default_time; + $max = $default ? 3 : 1; + $next = time(); + for ($c = 0; $c < $max; $c++) { + echo '

'; + $next = outagelib::get_next_window($next); + $urlnew->param('starttime', $next); + echo $OUTPUT->single_button($urlnew, get_string('outagecreate', 'auth_outage')); + if ($default) { + echo ' ' . userdate( $next, get_string('datetimeformat', 'auth_outage')); + } + } + endif; ?>