mirror of
https://github.com/catalyst/moodle-auth_outage.git
synced 2026-05-17 05:48:43 +02:00
Issue #23 - Generate a static template page every time an outage is created, updated or deleted.
This commit is contained in:
@@ -120,6 +120,9 @@ class outagedb {
|
||||
self::calendar_update($outage);
|
||||
}
|
||||
|
||||
// Trigger static page update.
|
||||
outagelib::updatestaticinfopagefile();
|
||||
|
||||
// All done, return the id.
|
||||
return $outage->id;
|
||||
}
|
||||
@@ -149,6 +152,9 @@ class outagedb {
|
||||
// Delete it and remove from calendar.
|
||||
$DB->delete_records('auth_outage', ['id' => $id]);
|
||||
self::calendar_delete($id);
|
||||
|
||||
// Trigger static page update.
|
||||
outagelib::updatestaticinfopagefile();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -276,6 +282,37 @@ class outagedb {
|
||||
self::save($outage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the next outage which has not started yet.
|
||||
* @param null $time Timestamp reference for current time.
|
||||
* @return outage|null The outage or null if not found.
|
||||
*/
|
||||
public static function get_next_starting($time = null) {
|
||||
global $DB;
|
||||
|
||||
if ($time === null) {
|
||||
$time = time();
|
||||
}
|
||||
if (!is_int($time) || ($time <= 0)) {
|
||||
throw new InvalidArgumentException('$time must be null or an positive int.');
|
||||
}
|
||||
|
||||
$select = ':datetime <= starttime'; // End condition.
|
||||
$data = $DB->get_records_select(
|
||||
'auth_outage',
|
||||
$select,
|
||||
['datetime' => $time],
|
||||
'starttime ASC',
|
||||
'*',
|
||||
0,
|
||||
1
|
||||
);
|
||||
|
||||
// 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));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an event on the calendar for this outage.
|
||||
* @param outage $outage Outage to be added to the calendar.
|
||||
|
||||
@@ -16,8 +16,10 @@
|
||||
|
||||
namespace auth_outage;
|
||||
|
||||
use auth_outage\models\outage;
|
||||
use auth_outage_renderer;
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use moodle_url;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
@@ -118,4 +120,81 @@ class outagelib {
|
||||
'css' => file_get_contents($CFG->dirroot . '/auth/outage/views/warningbar.css'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a static info page for the given outage.
|
||||
* @param outage $outage Outage to generate the info page.
|
||||
* @param string $file File to save the static info page.
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function savestaticinfopage(outage $outage, $file) {
|
||||
if (!is_string($file)) {
|
||||
throw new InvalidArgumentException('$file is not a string.');
|
||||
}
|
||||
|
||||
$html = self::get_renderer()->renderoutagepagestatic($outage);
|
||||
|
||||
// Sanity check before writing/overwriting old file.
|
||||
if (!is_string($html) || ($html == '')) {
|
||||
throw new Exception('Sanity check failed. Invalid contents on $html.');
|
||||
}
|
||||
|
||||
$dir = dirname($file);
|
||||
if (!file_exists($dir) || !is_dir($dir)) {
|
||||
throw new Exception('Directory must exists: ' . $dir);
|
||||
}
|
||||
file_put_contents($file, $html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the static info page by (re)creating or deleting it as needed.
|
||||
* @param null $file
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function updatestaticinfopagefile($file = null) {
|
||||
if (is_null($file)) {
|
||||
$file = self::get_defaulttemplatefile();
|
||||
}
|
||||
if (!is_string($file)) {
|
||||
throw new InvalidArgumentException('$file is not a string.');
|
||||
}
|
||||
|
||||
$outage = outagedb::get_next_starting();
|
||||
if (is_null($outage)) {
|
||||
if (file_exists($file)) {
|
||||
if (is_file($file)) {
|
||||
unlink($file);
|
||||
} else {
|
||||
throw new Exception('Cannot remove non-file: ' . $file);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self::savestaticinfopage($outage, $file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the HTML code for the static page, find the outage id for that page.
|
||||
* @param $html Static info page HTML.
|
||||
* @return int|null Outage id or null if not found.
|
||||
*/
|
||||
public static function get_outageidfrominfopage($html) {
|
||||
if (!is_string($html)) {
|
||||
throw new InvalidArgumentException('$html must be a string.');
|
||||
}
|
||||
|
||||
$output = [];
|
||||
if (preg_match('/data-outage-id="(?P<id>\d+)"/', $html, $output)) {
|
||||
return (int)$output['id'];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string The default template file to use for static info page.
|
||||
*/
|
||||
public static function get_defaulttemplatefile() {
|
||||
global $CFG;
|
||||
return $CFG->dataroot . '/climaintenance.template.html';
|
||||
}
|
||||
}
|
||||
|
||||
21
info.php
21
info.php
@@ -34,16 +34,19 @@ if (is_null($outage)) {
|
||||
redirect(new moodle_url('/'));
|
||||
}
|
||||
|
||||
$PAGE->set_context(context_system::instance());
|
||||
$PAGE->set_title($outage->get_title());
|
||||
$PAGE->set_heading($outage->get_title());
|
||||
$PAGE->set_url(new \moodle_url('/auth/outage/info.php'));
|
||||
if (optional_param('static', false, PARAM_BOOL)) {
|
||||
echo outagelib::get_renderer()->renderoutagepagestatic($outage);
|
||||
} else {
|
||||
$PAGE->set_title($outage->get_title());
|
||||
$PAGE->set_heading($outage->get_title());
|
||||
$PAGE->set_url(new \moodle_url('/auth/outage/info.php'));
|
||||
|
||||
// No hooks injecting into this page, do it manually.
|
||||
outagelib::inject();
|
||||
// No hooks injecting into this page, do it manually.
|
||||
outagelib::inject();
|
||||
|
||||
echo $OUTPUT->header();
|
||||
echo $OUTPUT->header();
|
||||
|
||||
echo outagelib::get_renderer()->renderoutagepage($outage);
|
||||
echo outagelib::get_renderer()->renderoutagepage($outage);
|
||||
|
||||
echo $OUTPUT->footer();
|
||||
echo $OUTPUT->footer();
|
||||
}
|
||||
|
||||
@@ -77,6 +77,7 @@ $string['infofrom'] = 'From:';
|
||||
$string['infountil'] = 'Until:';
|
||||
$string['infostart'] = 'start';
|
||||
$string['infostartofwarning'] = 'start of warning';
|
||||
$string['infopagestaticgenerated'] = 'This warning was generated on {$a->time}.';
|
||||
$string['menudefaults'] = 'Default Settings';
|
||||
$string['menumanage'] = 'Manage';
|
||||
$string['messageoutageongoing'] = 'Back online at {$a->stop}.';
|
||||
|
||||
37
renderer.php
37
renderer.php
@@ -37,7 +37,7 @@ class auth_outage_renderer extends plugin_renderer_base {
|
||||
*/
|
||||
public function rendersubtitle($subtitlekey) {
|
||||
if (!is_string($subtitlekey)) {
|
||||
throw new \InvalidArgumentException('$subtitle is not a string.');
|
||||
throw new InvalidArgumentException('$subtitle is not a string.');
|
||||
}
|
||||
return html_writer::tag('h2', get_string($subtitlekey, 'auth_outage'));
|
||||
}
|
||||
@@ -186,8 +186,9 @@ class auth_outage_renderer extends plugin_renderer_base {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param outage $outage
|
||||
* @param null $time
|
||||
* Renders the outage page.
|
||||
* @param outage $outage Outage to be rendered.
|
||||
* @param null $time Time to use as refence. Null for current time.
|
||||
* @return string
|
||||
* @SuppressWarnings("unused") because $admineditlink is used inside require(...)
|
||||
*/
|
||||
@@ -197,8 +198,8 @@ class auth_outage_renderer extends plugin_renderer_base {
|
||||
if (is_null($time)) {
|
||||
$time = time();
|
||||
}
|
||||
if (!is_int($time)) {
|
||||
throw new \InvalidArgumentException('$time is not an int or null.');
|
||||
if (!is_int($time) || ($time <= 0)) {
|
||||
throw new InvalidArgumentException('$time is not an positive int or null.');
|
||||
}
|
||||
|
||||
$adminlinks = [];
|
||||
@@ -226,6 +227,8 @@ class auth_outage_renderer extends plugin_renderer_base {
|
||||
get_string('outageedit', 'auth_outage')
|
||||
);
|
||||
|
||||
$static = false;
|
||||
|
||||
ob_start();
|
||||
require($CFG->dirroot . '/auth/outage/views/infopage.php');
|
||||
$html = ob_get_contents();
|
||||
@@ -233,6 +236,26 @@ class auth_outage_renderer extends plugin_renderer_base {
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the HTML for a static info page.
|
||||
* @param outage $outage Outage to generate the page.
|
||||
* @return string The HTML code.
|
||||
* @SuppressWarnings("unused") because variables are used in require(...)
|
||||
*/
|
||||
public function renderoutagepagestatic(outage $outage) {
|
||||
global $PAGE, $CFG;
|
||||
$PAGE->set_context(context_system::instance());
|
||||
|
||||
$static = true;
|
||||
$time = $outage->starttime;
|
||||
|
||||
ob_start();
|
||||
require($CFG->dirroot . '/auth/outage/views/infopagestatic.php');
|
||||
$html = ob_get_contents();
|
||||
ob_end_clean();
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the warning bar.
|
||||
* @param outage $outage The outage to show in the warning bar.
|
||||
@@ -246,8 +269,8 @@ class auth_outage_renderer extends plugin_renderer_base {
|
||||
if (is_null($time)) {
|
||||
$time = time();
|
||||
}
|
||||
if (!is_int($time)) {
|
||||
throw new \InvalidArgumentException('$time is not an int or null.');
|
||||
if (!is_int($time) || ($time <= 0)) {
|
||||
throw new InvalidArgumentException('$time is not an positive int or null.');
|
||||
}
|
||||
|
||||
$start = userdate($outage->starttime, get_string('datetimeformat', 'auth_outage'));
|
||||
|
||||
74
tests/cli/outagelib_test.php
Normal file
74
tests/cli/outagelib_test.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use auth_outage\models\outage;
|
||||
use auth_outage\outagelib;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Tests performed on outage class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @covers \auth_outage\outagelib
|
||||
*/
|
||||
class outagelib_test extends advanced_testcase {
|
||||
/**
|
||||
* Gets a temp file to use in the test. Deleted every time a test starts.
|
||||
* @return string A temporary file name.
|
||||
*/
|
||||
public function get_file() {
|
||||
return sys_get_temp_dir() . '/phpunit_authoutage.tmp';
|
||||
}
|
||||
|
||||
public function setUp() {
|
||||
if (file_exists($this->get_file())) {
|
||||
if (is_file($this->get_file())) {
|
||||
unlink($this->get_file());
|
||||
} else {
|
||||
self::fail('Invalid temp file: ' . $this->get_file());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function test_staticpage() {
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'id' => 1,
|
||||
'warntime' => $now - 100,
|
||||
'starttime' => $now + 100,
|
||||
'stoptime' => $now + 200,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
outagelib::savestaticinfopage($outage, $this->get_file());
|
||||
self::assertFileExists($this->get_file());
|
||||
|
||||
$id = outagelib::get_outageidfrominfopage(file_get_contents($this->get_file()));
|
||||
self::assertSame($outage->id, $id);
|
||||
|
||||
unlink($this->get_file());
|
||||
}
|
||||
|
||||
public function test_getdefaulttemplatefile() {
|
||||
$file = outagelib::get_defaulttemplatefile();
|
||||
self::assertTrue(is_string($file));
|
||||
self::assertContains('template', $file);
|
||||
}
|
||||
}
|
||||
54
tests/renderer_test.php
Normal file
54
tests/renderer_test.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use auth_outage\models\outage;
|
||||
use auth_outage\outagelib;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Tests performed on outage class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @covers \auth_outage_renderer
|
||||
*/
|
||||
class renderer_test extends advanced_testcase {
|
||||
public function test_staticpage() {
|
||||
global $PAGE;
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$PAGE->set_context(context_system::instance());
|
||||
$renderer = outagelib::get_renderer();
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'id' => 1,
|
||||
'starttime' => $now + (60 * 60),
|
||||
'warntime' => $now - (60 * 60),
|
||||
'stoptime' => $now + (2 * 60 * 60),
|
||||
'title' => 'Outage Title at {{start}}',
|
||||
'description' => 'This is an <b>important</b> outage, starting at {{start}}.',
|
||||
]);
|
||||
$html = $renderer->renderoutagepagestatic($outage);
|
||||
self::assertContains('<!DOCTYPE html>', $html);
|
||||
self::assertContains('</html>', $html);
|
||||
self::assertContains($outage->get_title(), $html);
|
||||
self::assertContains($outage->get_description(), $html);
|
||||
self::assertSame($outage->id, outagelib::get_outageidfrominfopage($html));
|
||||
}
|
||||
}
|
||||
@@ -39,7 +39,7 @@ if (!defined('MOODLE_INTERNAL')) {
|
||||
</div>
|
||||
<div class="auth_outage_info_description"><?php echo $outage->get_description(); ?></div>
|
||||
|
||||
<?php if (is_siteadmin()): ?>
|
||||
<?php if (!$static && is_siteadmin()): ?>
|
||||
<div class="auth_outage_info_adminlinks">
|
||||
<b><?php echo get_string('preview'); ?>:</b>
|
||||
<?php echo implode(' | ', $adminlinks); ?><br />
|
||||
|
||||
64
views/infopagestatic.php
Normal file
64
views/infopagestatic.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* View included by the renderer to output the static outage information page.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $SITE;
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html data-outage-id="<?php echo $outage->id; ?>">
|
||||
<head>
|
||||
<title><?php echo strip_tags($SITE->fullname); ?></title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<style>
|
||||
body {
|
||||
font-family: sans-serif;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<?php require('warningbar.php'); ?>
|
||||
|
||||
<header>
|
||||
<h1><?php echo strip_tags($SITE->fullname); ?></h1>
|
||||
</header>
|
||||
|
||||
<section>
|
||||
<h2><?php echo $outage->get_title(); ?></h2>
|
||||
<?php require('infopage.php'); ?>
|
||||
</section>
|
||||
|
||||
<!-- <?php echo
|
||||
get_string(
|
||||
'infopagestaticgenerated',
|
||||
'auth_outage',
|
||||
['time' => userdate(time(), get_string('datetimeformat', 'auth_outage'))]
|
||||
);
|
||||
?> -->
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -25,13 +25,21 @@
|
||||
|
||||
use auth_outage\outagelib;
|
||||
|
||||
if (!defined('MOODLE_INTERNAL')) {
|
||||
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
|
||||
}
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $OUTPUT;
|
||||
|
||||
$infolink = new moodle_url('/auth/outage/info.php', ['id' => $outage->id]);
|
||||
if (!isset($static)) {
|
||||
$static = true;
|
||||
}
|
||||
|
||||
if ($static) {
|
||||
$start = userdate($outage->starttime, get_string('datetimeformat', 'auth_outage'));
|
||||
$stop = userdate($outage->stoptime, get_string('datetimeformat', 'auth_outage'));
|
||||
$countdown = get_string('messageoutageongoing', 'auth_outage', ['start' => $start, 'stop' => $stop]);
|
||||
} else {
|
||||
$infolink = new moodle_url('/auth/outage/info.php', ['id' => $outage->id]);
|
||||
}
|
||||
|
||||
echo html_writer::tag('style', outagelib::get_config()->css);
|
||||
?>
|
||||
@@ -41,13 +49,17 @@ echo html_writer::tag('style', outagelib::get_config()->css);
|
||||
<div id="auth_outage_warningbar_countdown"><?php echo $countdown; ?></div>
|
||||
<div>
|
||||
<?php
|
||||
echo html_writer::link(
|
||||
$infolink,
|
||||
$outage->get_title(),
|
||||
['target' => '_blank', 'class' => 'auth_outage_warningbar_box_title']
|
||||
);
|
||||
if ($static) {
|
||||
echo $outage->get_title();
|
||||
} else {
|
||||
echo html_writer::link(
|
||||
$infolink,
|
||||
$outage->get_title(),
|
||||
['target' => '_blank', 'class' => 'auth_outage_warningbar_box_title']
|
||||
);
|
||||
}
|
||||
|
||||
if (is_siteadmin() && $outage->is_ongoing()) {
|
||||
if (!$static && is_siteadmin() && $outage->is_ongoing()) {
|
||||
$url = new moodle_url('/auth/outage/finish.php', ['id' => $outage->id]);
|
||||
$text = html_writer::empty_tag('img', [
|
||||
'src' => $OUTPUT->pix_url('t/check'),
|
||||
@@ -65,7 +77,7 @@ echo html_writer::tag('style', outagelib::get_config()->css);
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php if (!$outage->is_ongoing($time)): ?>
|
||||
<?php if (!$static && !$outage->is_ongoing($time)): ?>
|
||||
<script>
|
||||
<?php require($CFG->dirroot . '/auth/outage/views/warningbar.js'); ?>
|
||||
auth_outage_countdown.init(
|
||||
|
||||
Reference in New Issue
Block a user