Issue #26 - Added the option to preview how a warning bar will look before and during an outage.

This commit is contained in:
Daniel Thee Roperto
2016-09-13 16:00:44 +10:00
parent 9cc954961a
commit 92681c7ec4
8 changed files with 176 additions and 32 deletions

View File

@@ -97,6 +97,25 @@ class outage {
throw new \InvalidArgumentException('$data must be null (default), an array or an object.');
}
/**
* Checks if the outage is active (in warning period or ongoing).
* @param int|null $time Null to check if the outage is active now or another time to use as reference.
* @return bool True if outage is ongoing or during the warning period.
*/
public function is_active($time = null) {
if ($time === null) {
$time = time();
}
if (!is_int($time) || ($time <= 0)) {
throw new \InvalidArgumentException('$time must be an positive int.');
}
if (is_null($this->warntime) || is_null($this->stoptime)) {
return false;
}
return (($this->warntime <= $time) && ($time < $this->stoptime));
}
/**
* Checks if the outage is happening.
* @param int|null $time Null to check if the outage is happening now or another time to use as reference.

View File

@@ -68,17 +68,17 @@ class outagelib {
$previewid = optional_param('auth_outage_preview', null, PARAM_INT);
$time = time();
if (is_null($previewid)) {
if (($active = outagedb::get_active()) == null) {
if (!$active = outagedb::get_active()) {
return;
}
} else {
if (($active = outagedb::get_by_id($previewid)) == null) {
if (!$active = outagedb::get_by_id($previewid)) {
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);
// Delta is in seconds, setting the time our warning bar will consider relative to the outage start time.
$time = $active->starttime + optional_param('auth_outage_delta', 0, PARAM_INT);
if (!$active->is_active($time)) {
return;
}
}

View File

@@ -39,6 +39,9 @@ $PAGE->set_title($outage->get_title());
$PAGE->set_heading($outage->get_title());
$PAGE->set_url(new \moodle_url('/auth/outage/info.php'));
// No hooks injecting into this page, do it manually.
outagelib::inject();
echo $OUTPUT->header();
echo outagelib::get_renderer()->renderoutagepage($outage);

View File

@@ -39,6 +39,12 @@ $string['defaultwarningdescriptiondescription'] = 'Default warning message for o
$string['defaultwarningdescriptionvalue'] = 'There is an scheduled maintenance from {{start}} to {{stop}} and our system will not be available during that time.';
$string['description'] = 'Public Description';
$string['description_help'] = 'A full description of the outage, publicly visible by all users.';
$string['info1minutebefore'] = '1 minute before';
$string['infoendofoutage'] = 'end of outage';
$string['infofrom'] = 'From:';
$string['infountil'] = 'Until:';
$string['infostart'] = 'start';
$string['infostartofwarning'] = 'start of warning';
$string['menudefaults'] = 'Default Settings';
$string['menumanage'] = 'Manage';
$string['messageoutageongoing'] = 'Our system will be under maintenance until {$a->stop}.';

View File

@@ -138,30 +138,52 @@ class auth_outage_renderer extends plugin_renderer_base {
);
}
public function renderoutagepage(outage $outage) {
$start = userdate($outage->starttime, get_string('strftimedatetimeshort'));
$stop = userdate($outage->stoptime, get_string('strftimedatetimeshort'));
/**
* @param outage $outage
* @param null $time
* @return string
* @SuppressWarnings("unused") because $admineditlink is used inside require(...)
*/
public function renderoutagepage(outage $outage, $time = null) {
global $CFG;
$admin = '';
if (is_siteadmin()) {
$admin = html_writer::tag('div',
'[' . html_writer::link(
new moodle_url('/auth/outage/edit.php', ['id' => $outage->id]),
get_string('outageedit', 'auth_outage')
) . ']'
if (is_null($time)) {
$time = time();
}
if (!is_int($time)) {
throw new \InvalidArgumentException('$time is not an int or null.');
}
$adminlinks = [];
foreach ([
'startofwarning' => -$outage->get_warning_duration(),
'1minutebefore' => -60,
'start' => 0,
'endofoutage' => $outage->get_duration(),
] as $title => $delta) {
$adminlinks[] = html_writer::link(
new moodle_url(
'/auth/outage/info.php',
[
'id' => $outage->id,
'auth_outage_preview' => $outage->id,
'auth_outage_delta' => $delta,
]
),
get_string('info' . $title, 'auth_outage')
);
}
return html_writer::div(
html_writer::tag('p',
html_writer::tag('b', 'From: ')
. $start
. html_writer::tag('b', ' Until: ')
. $stop
)
. html_writer::div($outage->get_description())
. $admin
$admineditlink = html_writer::link(
new moodle_url('/auth/outage/edit.php', ['id' => $outage->id]),
get_string('outageedit', 'auth_outage')
);
ob_start();
require($CFG->dirroot . '/auth/outage/views/infopage.php');
$html = ob_get_contents();
ob_end_clean();
return $html;
}
/**
@@ -169,7 +191,6 @@ class auth_outage_renderer extends plugin_renderer_base {
* @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, $time = null) {
global $CFG;
@@ -181,8 +202,8 @@ class auth_outage_renderer extends plugin_renderer_base {
throw new \InvalidArgumentException('$time is not an int or null.');
}
$start = userdate($outage->starttime, get_string('strftimedatetimeshort'));
$stop = userdate($outage->stoptime, get_string('strftimedatetimeshort'));
$start = userdate($outage->starttime, get_string('datetimeformat', 'auth_outage'));
$stop = userdate($outage->stoptime, get_string('datetimeformat', 'auth_outage'));
$countdown = get_string(
$outage->is_ongoing($time) ? 'messageoutageongoing' : 'messageoutagewarning',
@@ -190,6 +211,7 @@ class auth_outage_renderer extends plugin_renderer_base {
['start' => $start, 'stop' => $stop]
);
ob_start();
require($CFG->dirroot . '/auth/outage/views/warningbar.php');
$html = ob_get_contents();

View File

@@ -44,8 +44,8 @@ class outage_test extends basic_testcase {
// In the past.
$outage = new outage([
'starttime' => $now + (-3 * 60 * 60),
'stoptime' => $now + (-2 * 60 * 60),
'starttime' => $now - (3 * 60 * 60),
'stoptime' => $now - (2 * 60 * 60),
'warntime' => $now - (2 * 60 * 60),
'title' => '',
'description' => ''
@@ -54,7 +54,7 @@ class outage_test extends basic_testcase {
// In the present (ongoing).
$outage = new outage([
'starttime' => $now + (-1 * 60 * 60),
'starttime' => $now - (1 * 60 * 60),
'stoptime' => $now + (1 * 60 * 60),
'warntime' => $now - (2 * 60 * 60),
'title' => '',
@@ -72,4 +72,48 @@ class outage_test extends basic_testcase {
]);
self::assertFalse($outage->is_ongoing($now));
}
public function test_isactive() {
$now = time();
// In the past.
$outage = new outage([
'starttime' => $now - (3 * 60 * 60),
'stoptime' => $now - (2 * 60 * 60),
'warntime' => $now - (2 * 60 * 60),
'title' => '',
'description' => ''
]);
self::assertFalse($outage->is_active($now));
// In the present (ongoing).
$outage = new outage([
'starttime' => $now - (1 * 60 * 60),
'stoptime' => $now + (1 * 60 * 60),
'warntime' => $now - (2 * 60 * 60),
'title' => '',
'description' => ''
]);
self::assertTrue($outage->is_active($now));
// In the future (warning).
$outage = new outage([
'starttime' => $now + (1 * 60 * 60),
'stoptime' => $now + (2 * 60 * 60),
'warntime' => $now - (2 * 60 * 60),
'title' => '',
'description' => ''
]);
self::assertTrue($outage->is_active($now));
// In the future (not warning).
$outage = new outage([
'starttime' => $now + (2 * 60 * 60),
'stoptime' => $now + (3 * 60 * 60),
'warntime' => $now + (1 * 60 * 60),
'title' => '',
'description' => ''
]);
self::assertFalse($outage->is_active($now));
}
}

50
views/infopage.php Normal file
View File

@@ -0,0 +1,50 @@
<?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/>.
/**
* View included by the renderer to output the outage information page.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
}
?>
<div class="auth_outage_info">
<div>
<b><?php echo get_string('infofrom', 'auth_outage'); ?></b>
<?php echo userdate($outage->starttime, get_string('datetimeformat', 'auth_outage')); ?>
</div>
<div>
<b><?php echo get_string('infountil', 'auth_outage'); ?></b>
<?php echo userdate($outage->stoptime, get_string('datetimeformat', 'auth_outage')); ?>
</div>
<div class="auth_outage_info_description"><?php echo $outage->get_description(); ?></div>
<?php if (is_siteadmin()): ?>
<div class="auth_outage_info_adminlinks">
<b><?php echo get_string('preview'); ?>:</b>
<?php echo implode(' | ', $adminlinks); ?><br />
<?php echo $admineditlink; ?>
</div>
<?php endif; ?>
</div>

View File

@@ -67,11 +67,11 @@ echo html_writer::tag('style',
, init: function () {
this.span = document.getElementById('auth_outage_warningbar_countdown');
this.text = this.span.innerHTML;
this.tick();
var $this = this;
this.timer = setInterval(function () {
$this.tick();
}, 1000);
this.tick();
}
, tick: function () {
var elapsed = Math.round((Date.now() - this.clienttime) / 1000);