From 92ad2f5db53647ea5a69bba3b501fb8bf6b0bd01 Mon Sep 17 00:00:00 2001 From: Kristian Ringer Date: Fri, 24 May 2019 16:56:03 +1000 Subject: [PATCH] Clean up outages from additionalhtmltopofbody --- classes/local/outagelib.php | 29 +++++++++++++++++++ classes/output/renderer.php | 58 ++++++++++++++++++++----------------- 2 files changed, 60 insertions(+), 27 deletions(-) diff --git a/classes/local/outagelib.php b/classes/local/outagelib.php index ba400ae..fa9a395 100644 --- a/classes/local/outagelib.php +++ b/classes/local/outagelib.php @@ -47,6 +47,11 @@ require_once(__DIR__.'/../../lib.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class outagelib { + + const OUTAGE_START = ''; + + const OUTAGE_END = ''; + /** * @var bool Flags in the injection function was already called. */ @@ -87,6 +92,8 @@ 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(); @@ -356,4 +363,26 @@ 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/classes/output/renderer.php b/classes/output/renderer.php index d7daca5..d8f59bd 100644 --- a/classes/output/renderer.php +++ b/classes/output/renderer.php @@ -26,6 +26,7 @@ namespace auth_outage\output; use auth_outage\local\outage; +use auth_outage\local\outagelib; use coding_exception; use core_user; use html_writer; @@ -196,35 +197,38 @@ class renderer extends plugin_renderer_base { $finished = userdate($finished, get_string('datetimeformat', 'auth_outage')); } - return html_writer::div( + $start = outagelib::OUTAGE_START; + $end = outagelib::OUTAGE_END; + $outagehtml = html_writer::div( html_writer::tag('blockquote', - html_writer::div(html_writer::tag('b', $outage->get_title(), ['data-id' => $outage->id])). - html_writer::div(html_writer::tag('i', $outage->get_description())). - html_writer::div( - html_writer::tag('b', get_string('tableheaderwarnbefore', 'auth_outage').': '). - format_time($outage->get_warning_duration()) - ). - html_writer::div( - html_writer::tag('b', get_string('tableheaderstarttime', 'auth_outage').': '). - userdate($outage->starttime, get_string('datetimeformat', 'auth_outage')) - ). - html_writer::div( - html_writer::tag('b', get_string('tableheaderdurationplanned', 'auth_outage').': '). - format_time($outage->get_duration_planned()) - ). - html_writer::div( - html_writer::tag('b', get_string('tableheaderdurationactual', 'auth_outage').': '). - $finished - ). - html_writer::div( - html_writer::tag('small', - 'Created by '.$created. - ', modified by '.$modified.' on '. - userdate($outage->lastmodified, get_string('datetimeformat', 'auth_outage')) - ) - ). - ($buttons ? html_writer::div($linkedit.$linkdelete) : '') + html_writer::div(html_writer::tag('b', $outage->get_title(), ['data-id' => $outage->id])). + html_writer::div(html_writer::tag('i', $outage->get_description())). + html_writer::div( + html_writer::tag('b', get_string('tableheaderwarnbefore', 'auth_outage').': '). + format_time($outage->get_warning_duration()) + ). + html_writer::div( + html_writer::tag('b', get_string('tableheaderstarttime', 'auth_outage').': '). + userdate($outage->starttime, get_string('datetimeformat', 'auth_outage')) + ). + html_writer::div( + html_writer::tag('b', get_string('tableheaderdurationplanned', 'auth_outage').': '). + format_time($outage->get_duration_planned()) + ). + html_writer::div( + html_writer::tag('b', get_string('tableheaderdurationactual', 'auth_outage').': '). + $finished + ). + html_writer::div( + html_writer::tag('small', + 'Created by '.$created. + ', modified by '.$modified.' on '. + userdate($outage->lastmodified, get_string('datetimeformat', 'auth_outage')) + ) + ). + ($buttons ? html_writer::div($linkedit.$linkdelete) : '') ) ); + return $start . $outagehtml . $end; } }