From 583516a3c7c7e329db7487ab8f7a0170b5803e00 Mon Sep 17 00:00:00 2001 From: Daniel Thee Roperto Date: Tue, 6 Sep 2016 17:17:26 +1000 Subject: [PATCH] Issue #9 - Checking if outage is ongoing or just a warning. Improved hooks, tests and HTML output. --- auth.php | 2 +- classes/models/outage.php | 14 ++++++++++++++ classes/outagedb.php | 5 ++--- classes/outagelib.php | 4 +++- lang/en/auth_outage.php | 2 ++ lib.php | 18 ++++-------------- renderer.php | 12 +++++++++++- res/outage.css | 4 ++++ tests/outage_test.php | 37 +++++++++++++++++++++++++++++++++++-- 9 files changed, 76 insertions(+), 22 deletions(-) diff --git a/auth.php b/auth.php index c1fbf7c..e03e823 100644 --- a/auth.php +++ b/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(); } } diff --git a/classes/models/outage.php b/classes/models/outage.php index c023c6f..7827e38 100644 --- a/classes/models/outage.php +++ b/classes/models/outage.php @@ -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)); + } } \ No newline at end of file diff --git a/classes/outagedb.php b/classes/outagedb.php index 896a8f3..f7ef64e 100644 --- a/classes/outagedb.php +++ b/classes/outagedb.php @@ -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); diff --git a/classes/outagelib.php b/classes/outagelib.php index 83b34dc..62cb1be 100644 --- a/classes/outagelib.php +++ b/classes/outagelib.php @@ -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); } diff --git a/lang/en/auth_outage.php b/lang/en/auth_outage.php index 0febcae..efe9311 100644 --- a/lang/en/auth_outage.php +++ b/lang/en/auth_outage.php @@ -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'; diff --git a/lib.php b/lib.php index 7eb1e0b..31903ec 100644 --- a/lib.php +++ b/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(); } diff --git a/renderer.php b/renderer.php index 46e5a7b..2a4bbb1 100644 --- a/renderer.php +++ b/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' ); } diff --git a/res/outage.css b/res/outage.css index 54ee5f9..c605e0a 100644 --- a/res/outage.css +++ b/res/outage.css @@ -1,3 +1,7 @@ .auth_outage_warningbar { background-color: red; } + +.auth_outage_warningbar_title { + font-size: 120%; +} \ No newline at end of file diff --git a/tests/outage_test.php b/tests/outage_test.php index b62097d..0c0bd87 100644 --- a/tests/outage_test.php +++ b/tests/outage_test.php @@ -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)); + } }