Issue #26 - Preview warning bar with parameters auth_outage_preview and auth_outage_delta

This commit is contained in:
Daniel Thee Roperto
2016-09-13 11:30:35 +10:00
parent 46a503e383
commit feaca1d78e
4 changed files with 35 additions and 11 deletions

View File

@@ -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;
}

View File

@@ -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]
);

View File

@@ -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' => ''
]);

View File

@@ -36,12 +36,13 @@ echo html_writer::tag('style',
<div class="auth_outage_warningbar">
<div class="auth_outage_warningbar_box">
<div class="auth_outage_warningbar_box_countdown" id="auth_outage_warningbar_countdown"><?php echo $countdown; ?></div>
<div class="auth_outage_warningbar_box_countdown"
id="auth_outage_warningbar_countdown"><?php echo $countdown; ?></div>
<div class="auth_outage_warningbar_box_message">
<?php echo $outage->get_title(); ?>
<small>
[<?php echo html_writer::link(
new moodle_url('/auth/outage/info.php'),
new moodle_url('/auth/outage/info.php', ['id' => $outage->id]),
get_string('readmore', 'auth_outage'),
['target' => 'outage']
); ?>]
@@ -61,7 +62,7 @@ echo html_writer::tag('style',
// Define outage object.
var auth_outage_countdown = {
timer: null
, countdown: <?php echo($outage->starttime - time()); ?>
, countdown: <?php echo($outage->starttime - $time); ?>
, clienttime: Date.now()
, init: function () {
this.span = document.getElementById('auth_outage_warningbar_countdown');