Issue #9 - Hooks installed, getting current outage implemented, missing adding information to the UI.

This commit is contained in:
Daniel Thee Roperto
2016-09-06 14:00:14 +10:00
parent aca6a0122e
commit 13418a6a28
5 changed files with 110 additions and 18 deletions

View File

@@ -27,8 +27,7 @@ namespace auth_outage\models;
use auth_outage\outagelib;
class outage
{
class outage {
/**
* @var int Outage ID (auto generated by the DB).
*/
@@ -85,6 +84,13 @@ class outage
if (is_object($data) || is_array($data)) {
outagelib::data2object($data, $this);
// FIXME types are wrong. Is this behaving as expected?
foreach (['createdby', 'id', 'lastmodified', 'modifiedby', 'starttime', 'stoptime', 'warningduration']
as $f) {
$this->$f = ($this->$f === null) ? null : (int)$this->$f;
}
return;
}

View File

@@ -135,4 +135,32 @@ final class outagedb {
$DB->delete_records('auth_outage', ['id' => $id]);
}
}
public static function getactive($time = null) {
global $DB;
if ($time === null) {
$time = time();
}
if (!is_int($time)) {
throw new \InvalidArgumentException('$time must be null or an int.');
}
// TODO Is there a way to use Moodle API instead of writing SQL (conditions not equals)?
// TODO Query not fully using indexes (starttime + 90)
// Gets any active outage (already started or during warning period).
// Gets only one record if available, the one that starts(ed) first and that stops last.
$now = time();
$data = $DB->get_record_sql('
SELECT *
FROM {auth_outage}
WHERE (starttime - warningduration <= :now1 AND stoptime >= :now2)
ORDER BY starttime ASC, stoptime DESC, title ASC
LIMIT 1
',
['now1' => $now, 'now2' => $now]
);
return ($data === false) ? null : new \auth_outage\models\outage($data);
}
}

View File

@@ -52,6 +52,9 @@ class outagelib {
return $PAGE->get_renderer('auth_outage');
}
/**
* Will check for ongoing or warning outages and will attach the message bar as required.
*/
public static function initialize() {
global $CFG;
@@ -61,17 +64,9 @@ class outagelib {
}
self::$initialized = true;
// Stop if no current outage is found.
$outage = new \auth_outage\models\outage([
'starttime' => time() - 60, // 1 minute ago.
'stoptime' => time() + 60 * 60, // In 1 hour.
'warningduration' => 1, // Does not matter.
'title' => 'Fixed Outage',
'description' => '<p>This is an <b>OUTAGE</b>.</p>'
]);
// FIXME Get from DB instead.
if (!$outage) return;
$CFG->additionalhtmltopofbody .= self::get_renderer()->renderbar($outage);
if (($active = outagedb::getactive()) == null) return;
$CFG->additionalhtmltopofbody .= self::get_renderer()->renderbar($active);
}
/**