mirror of
https://github.com/catalyst/moodle-auth_outage.git
synced 2026-05-17 05:48:43 +02:00
Issue #9 - Checking if outage is ongoing or just a warning. Improved hooks, tests and HTML output.
This commit is contained in:
2
auth.php
2
auth.php
@@ -54,6 +54,6 @@ class auth_plugin_outage extends auth_plugin_base
|
||||
* Login page hook.
|
||||
*/
|
||||
public function loginpage_hook() {
|
||||
\auth_outage\outagelib::initialize();
|
||||
\auth_outage\outagelib::inject();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,4 +96,18 @@ class outage {
|
||||
|
||||
throw new \InvalidArgumentException('$data must be null (default), an array or an object.');
|
||||
}
|
||||
|
||||
public function is_ongoing($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->starttime) || is_null($this->stoptime)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (($this->starttime <= $time) && ($time < $this->stoptime));
|
||||
}
|
||||
}
|
||||
@@ -158,15 +158,14 @@ final class outagedb {
|
||||
// TODO Query not fully using indexes (starttime + 90)
|
||||
// Gets any active outage (already started or during warning period).
|
||||
// Gets only one record if available, the one that starts(ed) first and that stops last.
|
||||
$now = time();
|
||||
$data = $DB->get_record_sql('
|
||||
SELECT *
|
||||
FROM {auth_outage}
|
||||
WHERE (starttime - warningduration <= :now1 AND stoptime >= :now2)
|
||||
WHERE (starttime - warningduration <= :datetime1 AND stoptime >= :datetime2)
|
||||
ORDER BY starttime ASC, stoptime DESC, title ASC
|
||||
LIMIT 1
|
||||
',
|
||||
['now1' => $now, 'now2' => $now]
|
||||
['datetime1' => $time, 'datetime2' => $time]
|
||||
);
|
||||
|
||||
return ($data === false) ? null : new \auth_outage\models\outage($data);
|
||||
|
||||
@@ -55,8 +55,9 @@ class outagelib {
|
||||
/**
|
||||
* Will check for ongoing or warning outages and will attach the message bar as required.
|
||||
*/
|
||||
public static function initialize() {
|
||||
public static function inject() {
|
||||
global $CFG;
|
||||
global $PAGE;
|
||||
|
||||
// Many hooks can call it, execute only once.
|
||||
if (self::$initialized) {
|
||||
@@ -68,6 +69,7 @@ class outagelib {
|
||||
return;
|
||||
}
|
||||
|
||||
$PAGE->add_body_class('auth_outage_active');
|
||||
$CFG->additionalhtmltopofbody .= self::get_renderer()->renderbar($active);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,8 @@ $string['defaultwarningtimedescription'] = 'Default warning time (in minutes) fo
|
||||
$string['description'] = 'Public description';
|
||||
$string['menudefaults'] = 'Default Settings';
|
||||
$string['menumanage'] = 'Manage';
|
||||
$string['messageoutageongoing'] = 'Our system will be under maintenance until {$a->stop}.';
|
||||
$string['messageoutagewarning'] = 'There is an scheduled downtime from {$a->start} until {$a->stop}.';
|
||||
$string['modify'] = 'Modify';
|
||||
$string['modifyoutage'] = 'Modify Outage';
|
||||
$string['outagecreate'] = 'Create Outage';
|
||||
|
||||
18
lib.php
18
lib.php
@@ -24,22 +24,12 @@
|
||||
*/
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
// FIXME hook not installing in courses/index.php page.
|
||||
|
||||
function auth_outage_extend_navigation_user() {
|
||||
\auth_outage\outagelib::initialize();
|
||||
}
|
||||
|
||||
function auth_outage_extend_navigation($data) {
|
||||
global $CFG;
|
||||
|
||||
// FIXME if function is not used, remove it completely.
|
||||
if ($CFG->debugdisplay) {
|
||||
var_dump($data);
|
||||
throw new \Exception("Check outage/lib.php");
|
||||
}
|
||||
|
||||
\auth_outage\outagelib::initialize();
|
||||
\auth_outage\outagelib::inject();
|
||||
}
|
||||
|
||||
function auth_outage_extend_navigation_frontpage() {
|
||||
\auth_outage\outagelib::initialize();
|
||||
\auth_outage\outagelib::inject();
|
||||
}
|
||||
|
||||
12
renderer.php
12
renderer.php
@@ -129,8 +129,18 @@ class auth_outage_renderer extends plugin_renderer_base {
|
||||
|
||||
$PAGE->requires->css(new moodle_url('/auth/outage/res/outage.css'));
|
||||
|
||||
$start = userdate($outage->starttime, get_string('strftimedatetimeshort'));
|
||||
$stop = userdate($outage->stoptime, get_string('strftimedatetimeshort'));
|
||||
|
||||
$message = get_string(
|
||||
$outage->is_ongoing() ? 'messageoutageongoing' : 'messageoutagewarning',
|
||||
'auth_outage',
|
||||
['start' => $start, 'stop' => $stop]
|
||||
);
|
||||
|
||||
return html_writer::div(
|
||||
html_writer::tag('b', $outage->title),
|
||||
html_writer::div($outage->title, 'auth_outage_warningbar_title')
|
||||
. html_writer::div($message, 'auth_outage_warningbar_message'),
|
||||
'auth_outage_warningbar'
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
.auth_outage_warningbar {
|
||||
background-color: red;
|
||||
}
|
||||
|
||||
.auth_outage_warningbar_title {
|
||||
font-size: 120%;
|
||||
}
|
||||
@@ -28,8 +28,7 @@ use auth_outage\models\outage;
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
|
||||
class outage_test extends basic_testcase
|
||||
{
|
||||
class outage_test extends basic_testcase {
|
||||
public function test_constructor() {
|
||||
$outage = new outage();
|
||||
// Very important, this should never change.
|
||||
@@ -39,4 +38,38 @@ class outage_test extends basic_testcase
|
||||
self::assertNull($v);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_isongoing() {
|
||||
$now = time();
|
||||
|
||||
// In the past.
|
||||
$outage = new outage([
|
||||
'starttime' => $now + (-3 * 60 * 60),
|
||||
'stoptime' => $now + (-2 * 60 * 60),
|
||||
'warningduration' => 2 * 60 * 60,
|
||||
'title' => '',
|
||||
'description' => ''
|
||||
]);
|
||||
self::assertFalse($outage->is_ongoing($now));
|
||||
|
||||
// In the present (ongoing).
|
||||
$outage = new outage([
|
||||
'starttime' => $now + (-1 * 60 * 60),
|
||||
'stoptime' => $now + (1 * 60 * 60),
|
||||
'warningduration' => 2 * 60 * 60,
|
||||
'title' => '',
|
||||
'description' => ''
|
||||
]);
|
||||
self::assertTrue($outage->is_ongoing($now));
|
||||
|
||||
// In the future.
|
||||
$outage = new outage([
|
||||
'starttime' => $now + (1 * 60 * 60),
|
||||
'stoptime' => $now + (2 * 60 * 60),
|
||||
'warningduration' => 2 * 60 * 60,
|
||||
'title' => '',
|
||||
'description' => ''
|
||||
]);
|
||||
self::assertFalse($outage->is_ongoing($now));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user