diff --git a/classes/outagelib.php b/classes/outagelib.php index bded0da..f84f560 100644 --- a/classes/outagelib.php +++ b/classes/outagelib.php @@ -64,11 +64,26 @@ class outagelib { } self::$initialized = true; - if (($active = outagedb::get_active()) == null) { - return; + // Check for a previewing outage, then for an active outage. + $previewid = optional_param('auth_outage_preview', null, PARAM_INT); + $time = time(); + if (is_null($previewid)) { + if (($active = outagedb::get_active()) == null) { + return; + } + } else { + if (($active = outagedb::get_by_id($previewid)) == null) { + return; + } + $delta = optional_param('auth_outage_delta', null, PARAM_FLOAT); + if ($delta) { + // Delta is float in minutes, allowing to check the redirect in a few seconds. + $time = $active->starttime + (int)($delta * 60); + } } - $CFG->additionalhtmltopofbody = self::get_renderer()->renderoutagebar($active) + // There is a previewing or active outage. + $CFG->additionalhtmltopofbody = self::get_renderer()->renderoutagebar($active, $time) . $CFG->additionalhtmltopofbody; } diff --git a/renderer.php b/renderer.php index ebb84d7..5cccc52 100644 --- a/renderer.php +++ b/renderer.php @@ -167,17 +167,25 @@ class auth_outage_renderer extends plugin_renderer_base { /** * Renders the warning bar. * @param outage $outage The outage to show in the warning bar. + * @param int|null $time Timestamp to send to the outage bar in order to render the outage. Null for current time. * @return string HTML of the warning bar. * @SuppressWarnings("unused") because $countdown is used inside require(...) */ - public function renderoutagebar(outage $outage) { + public function renderoutagebar(outage $outage, $time = null) { global $CFG; + if (is_null($time)) { + $time = time(); + } + if (!is_int($time)) { + throw new \InvalidArgumentException('$time is not an int or null.'); + } + $start = userdate($outage->starttime, get_string('strftimedatetimeshort')); $stop = userdate($outage->stoptime, get_string('strftimedatetimeshort')); $countdown = get_string( - $outage->is_ongoing() ? 'messageoutageongoing' : 'messageoutagewarning', + $outage->is_ongoing($time) ? 'messageoutageongoing' : 'messageoutagewarning', 'auth_outage', ['start' => $start, 'stop' => $stop] ); diff --git a/tests/outage_test.php b/tests/outage_test.php index 0c0bd87..79e4535 100644 --- a/tests/outage_test.php +++ b/tests/outage_test.php @@ -46,7 +46,7 @@ class outage_test extends basic_testcase { $outage = new outage([ 'starttime' => $now + (-3 * 60 * 60), 'stoptime' => $now + (-2 * 60 * 60), - 'warningduration' => 2 * 60 * 60, + 'warntime' => $now - (2 * 60 * 60), 'title' => '', 'description' => '' ]); @@ -56,7 +56,7 @@ class outage_test extends basic_testcase { $outage = new outage([ 'starttime' => $now + (-1 * 60 * 60), 'stoptime' => $now + (1 * 60 * 60), - 'warningduration' => 2 * 60 * 60, + 'warntime' => $now - (2 * 60 * 60), 'title' => '', 'description' => '' ]); @@ -66,7 +66,7 @@ class outage_test extends basic_testcase { $outage = new outage([ 'starttime' => $now + (1 * 60 * 60), 'stoptime' => $now + (2 * 60 * 60), - 'warningduration' => 2 * 60 * 60, + 'warntime' => $now - (2 * 60 * 60), 'title' => '', 'description' => '' ]); diff --git a/views/warningbar.php b/views/warningbar.php index e41f1d8..23e0e21 100644 --- a/views/warningbar.php +++ b/views/warningbar.php @@ -36,12 +36,13 @@ echo html_writer::tag('style',