Issue #23 - Generate a static template page every time an outage is created, updated or deleted.

This commit is contained in:
Daniel Thee Roperto
2016-09-20 14:54:24 +10:00
parent edbbc2dd83
commit 6824127624
10 changed files with 375 additions and 28 deletions

View File

@@ -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.

View File

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