diff --git a/auth.php b/auth.php index 67b59df..b9c0e4c 100644 --- a/auth.php +++ b/auth.php @@ -66,11 +66,4 @@ class auth_plugin_outage extends auth_plugin_base { public function user_login($username, $password) { return false; } - - /** - * Login page hook. - */ - public function loginpage_hook() { - outagelib::inject(); - } } diff --git a/classes/local/outagelib.php b/classes/local/outagelib.php index d5e9e9a..0296d24 100644 --- a/classes/local/outagelib.php +++ b/classes/local/outagelib.php @@ -72,11 +72,10 @@ class outagelib { } /** - * Calls inject even if it was already called before. + * Resets inject called to allow the code to be regenerated. */ - public static function reinject() { + public static function reset_injectcalled() { self::$injectcalled = false; - self::inject(); } /** @@ -100,11 +99,11 @@ class outagelib { /** - * Will check for ongoing or warning outages and will attach the message bar as required. + * Will check for ongoing or warning outages and will return the message bar as required. + * + * @return string|void CSS and HTML for the warning bar if it should be displayed */ - public static function inject() { - global $CFG; - + public static function get_inject_code() { // Ensure we do not kill the whole website in case of an error. try { // Check if we should inject the code. @@ -112,8 +111,6 @@ class outagelib { return; } - self::clean_outages(); - // Check for a previewing outage, then for an active outage. $previewid = optional_param('auth_outage_preview', null, PARAM_INT); $time = time(); @@ -136,8 +133,7 @@ class outagelib { } // There is a previewing or active outage. - $CFG->additionalhtmltopofbody = renderer::get()->render_warningbar($active, $time, false, $preview). - $CFG->additionalhtmltopofbody; + return renderer::get()->render_warningbar($active, $time, false, $preview); } catch (Exception $e) { debugging('Exception occured while injecting our code: '.$e->getMessage()); debugging($e->getTraceAsString(), DEBUG_DEVELOPER); @@ -397,26 +393,4 @@ EOT; return $message; } - - /** - * Checks $CFG->additionalhtmltopofbody for saved outages and removes them. - * We should only be temporarily injecting into that variable and not saving them to the database. - * - * @return string the cleaned content - */ - public static function clean_outages() { - global $CFG; - - // Replace the content to clean up pages that do not have the injection. - $re = '/' . self::OUTAGE_START . '[\s\S]*' . self::OUTAGE_END . '/m'; - $replaced = preg_replace($re, '', $CFG->additionalhtmltopofbody); - - // We have removed the outages and any duplicates as it should be injected and not saved to $CFG. - if ($CFG->additionalhtmltopofbody != $replaced) { - set_config('additionalhtmltopofbody', $replaced); - return $replaced; - } - - return ''; - } } diff --git a/lib.php b/lib.php index a630825..d403d41 100644 --- a/lib.php +++ b/lib.php @@ -27,27 +27,6 @@ use auth_outage\local\outagelib; defined('MOODLE_INTERNAL') || die; -/** - * Used in Moodle 30+ when a user is logged on. - */ -function auth_outage_extend_navigation_user_settings() { - outagelib::inject(); -} - -/** - * Used in Moodle 30+ on the frontpage. - */ -function auth_outage_extend_navigation_frontpage() { - outagelib::inject(); -} - -/** - * Used in Moodle 31+ when a user is logged on. - */ -function auth_outage_extend_navigation_user() { - outagelib::inject(); -} - /** * Used for adminlib::set_updatedcallback which requires a string that resolves to a function. * @@ -98,3 +77,13 @@ function auth_outage_get_fontawesome_icon_map() { 'core:i/auth_outageevent' => 'fa-power-off', ]; } + +/** + * Inject the warning bar into the page if there is currently an outage. + * + * @return string|void + */ +function auth_outage_before_standard_top_of_body_html() { + // Get code to inject. + return outagelib::get_inject_code(); +} \ No newline at end of file diff --git a/tests/phpunit/local/outagelib_test.php b/tests/phpunit/local/outagelib_test.php index 6fd2875..c37715e 100644 --- a/tests/phpunit/local/outagelib_test.php +++ b/tests/phpunit/local/outagelib_test.php @@ -82,7 +82,7 @@ class outagelib_test extends auth_outage_base_testcase { * Check outagelib::inject() works as expected. */ public function test_inject() { - global $CFG; + global $OUTPUT; $this->resetAfterTest(true); self::setAdminUser(); @@ -96,16 +96,15 @@ class outagelib_test extends auth_outage_base_testcase { 'description' => 'Description', ]); $outage->id = outagedb::save($outage); - self::assertEmpty($CFG->additionalhtmltopofbody); - outagelib::reinject(); - self::assertContains('