Merge pull request #156 from catalyst/clean_outages

Clean up outages from additionalhtmltopofbody
This commit is contained in:
Adam Riddell
2019-05-28 16:45:27 +10:00
committed by GitHub
2 changed files with 60 additions and 27 deletions

View File

@@ -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 = '<!-- OUTAGESTART -->';
const OUTAGE_END = '<!-- OUTAGEEND -->';
/**
* @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 '';
}
}

View File

@@ -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;
}
}