diff --git a/classes/dml/outagedb.php b/classes/dml/outagedb.php index 963ac3f..112787b 100644 --- a/classes/dml/outagedb.php +++ b/classes/dml/outagedb.php @@ -211,21 +211,20 @@ class outagedb { throw new coding_exception('$time must be null or a positive int.', $time); } - $select = ':datetime2 <= stoptime AND (finished IS NULL OR :datetime3 <= finished)'; // End condition. - $select = "(warntime <= :datetime1 AND (${select}))"; // Full select part. - $data = $DB->get_records_select( - 'auth_outage', - $select, - ['datetime1' => $time, 'datetime2' => $time, 'datetime3' => $time], - 'starttime ASC, stoptime DESC, title ASC', - '*', - 0, - 1 - ); + // Get cached outage, or null. + $outageinfo = get_config('moodle', 'auth_outage_active_outage'); - // Not using $DB->get_record_select instead because there is no 'limit' parameter. - // Allowing multiple records still raises an internal error. - return (count($data) == 0) ? null : new outage(array_shift($data)); + if (!$outageinfo) { + return null; + } else { + $outagecache = new outage(json_decode($outageinfo)); + } + + if ($outagecache && $outagecache->warntime <= $time && $outagecache->stoptime >= $time + && (!$outagecache->finished || $outagecache->finished >= $time)) { + return $outagecache; + } + return null; } /** diff --git a/classes/local/outage.php b/classes/local/outage.php index 5fc6174..f9907e4 100644 --- a/classes/local/outage.php +++ b/classes/local/outage.php @@ -297,4 +297,12 @@ class outage { // Adjust bool fields. $this->autostart = ($this->autostart === null) ? null : (bool)$this->autostart; } + + /** + * Return json encoded outage. + * @return string Json string. + */ + public function __toString() { + return json_encode(get_object_vars($this)); + } } diff --git a/classes/local/outagelib.php b/classes/local/outagelib.php index 6d76759..7d5b340 100644 --- a/classes/local/outagelib.php +++ b/classes/local/outagelib.php @@ -210,6 +210,10 @@ class outagelib { } else { $ongoingoutage = true; } + + // Set json formatted outage string to cache. + set_config('auth_outage_active_outage', (string)$outage); + maintenance_static_page::create_from_outage($outage)->generate(); self::update_climaintenance_code($outage); if (!$ongoingoutage || $reenablemaint || is_null($outage)) { diff --git a/tests/phpunit/dml/outagedb_test.php b/tests/phpunit/dml/outagedb_test.php index fe5b237..def70b1 100644 --- a/tests/phpunit/dml/outagedb_test.php +++ b/tests/phpunit/dml/outagedb_test.php @@ -244,6 +244,15 @@ class auth_outage_outagedb_test extends auth_outage_base_testcase { $activeid = self::saveoutage(false, $now, -2, 1, 2, 'An outage in warning period.'); self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.'); + $activeid = self::saveoutage(false, $now, -2, 0, 2, 'An outage starts now.'); + self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.'); + + self::saveoutage(false, $now, -2, 0, -1, 'Invalid outage.'); + self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.'); + + self::saveoutage(false, $now, -2, 0, 0, 'Invalid outage.'); + self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.'); + self::saveoutage(false, $now, -1, 2, 3, 'Another outage in warning period, but ignored as it starts after the previous one.'); self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.'); diff --git a/version.php b/version.php index 304b90b..3482760 100644 --- a/version.php +++ b/version.php @@ -28,8 +28,8 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = "auth_outage"; -$plugin->version = 2022042800; // The current plugin version (Date: YYYYMMDDXX). -$plugin->release = 2022042800; // Human-readable release information. +$plugin->version = 2022051200; // The current plugin version (Date: YYYYMMDDXX). +$plugin->release = 2022051200; // Human-readable release information. $plugin->requires = 2017111309; // 2017111309 = T13, but this really requires 3.9 and higher. $plugin->maturity = MATURITY_STABLE; // Suitable for PRODUCTION environments! $plugin->supported = [39, 311]; // A range of branch numbers of supported moodle versions.