Improved outage class encapsulation. Added outagedb as DB Context. Other minor changes.

This commit is contained in:
Daniel Thee Roperto
2016-08-31 11:26:27 +10:00
parent 72d77aea89
commit d9b852792e
8 changed files with 280 additions and 44 deletions

View File

@@ -35,6 +35,13 @@ require_once($CFG->libdir . '/authlib.php');
*/
class auth_plugin_outage extends auth_plugin_base
{
/**
* Constructor.
*/
public function __construct() {
$this->authtype = 'outage';
}
/**
* Do not authenticate users.
* @return bool False

View File

@@ -27,13 +27,136 @@ namespace auth_outage;
class outage
{
public $id;
public $starttime;
public $stoptime;
public $warningminutes;
public $title;
public $description;
public $createdby;
public $modifiedby;
public $lastmodified;
/**
* @var int Outage ID (auto generated by the DB).
*/
private $id;
/**
* @var int Start Time timestamp.
*/
private $starttime;
/**
* @var int Stop Time timestamp.
*/
private $stoptime;
/**
* @var int Amount of minutes before outage starts to show the warning message.
*/
private $warningminutes;
/**
* @var string Short description of the outage (no HTML).
*/
private $title;
/**
* @var string Description of the outage (some HTML allowed).
*/
private $description;
/**
* @var int Moodle User Id that created this outage.
*/
private $createdby;
/**
* @var int Moodle User Id that last modified this outage.
*/
private $modifiedby;
/**
* @var int Timestamp of when this outage was last modified.
*/
private $lastmodified;
/**
* outage constructor.
* @param $id
* @param $starttime
* @param $stoptime
* @param $warningminutes
* @param $title
* @param $description
* @param $createdby
* @param $modifiedby
* @param $lastmodified
*/
public function __construct($id, $starttime, $stoptime, $warningminutes,
$title, $description, $createdby, $modifiedby, $lastmodified) {
$this->id = $id;
$this->starttime = $starttime;
$this->stoptime = $stoptime;
$this->warningminutes = $warningminutes;
$this->title = $title;
$this->description = $description;
$this->createdby = $createdby;
$this->modifiedby = $modifiedby;
$this->lastmodified = $lastmodified;
}
/**
* @return int
*/
public function get_id() {
return $this->id;
}
/**
* @return int
*/
public function get_starttime() {
return $this->starttime;
}
/**
* @return int
*/
public function get_stoptime() {
return $this->stoptime;
}
/**
* @return int
*/
public function get_warningminutes() {
return $this->warningminutes;
}
/**
* @return string
*/
public function get_title() {
return $this->title;
}
/**
* @return string
*/
public function get_description() {
return $this->description;
}
/**
* @return int
*/
public function get_createdby() {
return $this->createdby;
}
/**
* @return int
*/
public function get_modifiedby() {
return $this->modifiedby;
}
/**
* @return int
*/
public function get_lastmodified() {
return $this->lastmodified;
}
}

108
classes/outagedb.php Normal file
View File

@@ -0,0 +1,108 @@
<?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/>.
/**
* The DB Context to manipulate Outages. Singleton 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
*/
namespace auth_outage;
final class outagedb
{
/**
* @var Singleton reference created on first use.
*/
private static $singleton = null;
/**
* Returns the singleton instance.
*
* @return The singleton object.
*/
public static function get() {
if (is_null(self::$singleton)) {
self::$singleton = new outagedb();
}
return self::$singleton;
}
/**
* @var Referente to Moodle global $DB.
*/
private $db;
/**
* Private clone method to prevent cloning singleton.
*
* @return void
*/
private function __clone() {
}
/**
* Private unserialize method to prevent unserializing singleton.
*
* @return void
*/
private function __wakeup() {
}
/**
* Private constructor (singleton), use outagedb::get() instead.
*/
private function __construct() {
global $DB;
$this->db = $DB;
}
/**
* Converts an stdClass object to an outage.
*
* @param \stdClass $obj Object from DB.
* @return outage
*/
private function object2outage(\stdClass $obj) {
return new outage(
$obj->id,
$obj->starttime,
$obj->stoptime,
$obj->warningminutes,
$obj->title,
$obj->description,
$obj->createdby,
$obj->modifiedby,
$obj->lastmod
);
}
/**
* Gets all outage entries.
*/
public function getall() {
$outages = [];
$rs = $this->db->get_recordset('auth_outage', null, 'starttime,stoptime,title');
foreach ($rs as $r) {
$outages[] = $this->object2outage($r);
}
$rs->close();
return $outages;
}
}

View File

@@ -7,14 +7,14 @@
<TABLE NAME="auth_outage" COMMENT="Table used for auth/outage plugin. Holds information about all past, current and future outages.">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="start_time" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="When outage starts."/>
<FIELD NAME="stop_time" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="When outage ends."/>
<FIELD NAME="warning_minutes" TYPE="int" LENGTH="7" NOTNULL="true" SEQUENCE="false" COMMENT="How many minutes before the outage to display a warning to all users."/>
<FIELD NAME="starttime" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="When outage starts."/>
<FIELD NAME="stoptime" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="When outage ends."/>
<FIELD NAME="warningminutes" TYPE="int" LENGTH="7" NOTNULL="true" SEQUENCE="false" COMMENT="How many minutes before the outage to display a warning to all users."/>
<FIELD NAME="title" TYPE="char" LENGTH="20" NOTNULL="true" SEQUENCE="false" COMMENT="Title for the outage (short description)."/>
<FIELD NAME="description" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="More information about the outage."/>
<FIELD NAME="created_by" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Who created this entry."/>
<FIELD NAME="modified_by" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Who last modified this entry."/>
<FIELD NAME="last_modified" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="When was this entry last modified."/>
<FIELD NAME="createdby" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Who created this entry."/>
<FIELD NAME="modifiedby" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Who last modified this entry."/>
<FIELD NAME="lastmodified" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="When was this entry last modified."/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>

View File

@@ -24,6 +24,7 @@
*/
use \auth_outage\outage;
use \auth_outage\outagedb;
require_once('../../config.php');
require_once($CFG->libdir . '/adminlib.php');
@@ -38,22 +39,10 @@ $PAGE->set_heading('List of registered outages.');
$renderer = $PAGE->get_renderer('auth_outage');
$outagelist = [];
for ($i = 1; $i <= 10; $i++) {
$outagelist[$i] = new outage();
$outagelist[$i]->id = $i;
$outagelist[$i]->starttime = time();
$outagelist[$i]->stoptime = time() + 60 * 60 * 4; // 4 hours.
$outagelist[$i]->warningminutes = 10 * $i;
$outagelist[$i]->title = 'Outage #' . $i;
$outagelist[$i]->description = 'This is the Outage #' . $i . ', backup creation.';
$outagelist[$i]->createdby = 1;
$outagelist[$i]->modifiedby = 1;
$outagelist[$i]->lastmodified = time() - 60 * 60 * 10; // 10 hours ago.
};
// TODO Add paging or limiting past entries displayed.
echo $OUTPUT->header();
echo $renderer->renderoutagelist($outagelist);
echo $renderer->renderoutagelist(outagedb::get()->getall());
echo $OUTPUT->footer();

View File

@@ -54,19 +54,19 @@ class auth_outage_renderer extends plugin_renderer_base
private function renderoutagelistentry(outage $outage) {
global $OUTPUT;
$created = core_user::get_user($outage->createdby, 'firstname,lastname', MUST_EXIST);
$created = core_user::get_user($outage->get_createdby(), 'firstname,lastname', MUST_EXIST);
$created = html_writer::link(
new moodle_url('/user/profile.php', ['id' => $outage->createdby]),
new moodle_url('/user/profile.php', ['id' => $outage->get_createdby()]),
trim($created->firstname . ' ' . $created->lastname)
);
$modified = core_user::get_user($outage->modifiedby, 'firstname,lastname', MUST_EXIST);
$modified = core_user::get_user($outage->get_modifiedby(), 'firstname,lastname', MUST_EXIST);
$modified = html_writer::link(
new moodle_url('/user/profile.php', ['id' => $outage->modifiedby]),
new moodle_url('/user/profile.php', ['id' => $outage->get_modifiedby()]),
trim($modified->firstname . ' ' . $modified->lastname)
);
$url = new moodle_url('/auth/outage/update.php', ['id' => $outage->id, 'sesskey' => sesskey()]);
$url = new moodle_url('/auth/outage/update.php', ['id' => $outage->get_id(), 'sesskey' => sesskey()]);
$url->param('action', 'edit');
$img = html_writer::empty_tag('img',
@@ -80,21 +80,27 @@ class auth_outage_renderer extends plugin_renderer_base
return html_writer::div(
html_writer::span(
html_writer::tag('b', $outage->title, ['data-id' => $outage->id])
html_writer::tag('b', $outage->get_title(), ['data-id' => $outage->get_id()])
. html_writer::empty_tag('br')
. html_writer::tag('i', $outage->description)
. html_writer::tag('i', $outage->get_description())
. html_writer::empty_tag('br')
. html_writer::tag('b', 'Warning: ') . userdate($outage->starttime - ($outage->warningminutes * 60))
. html_writer::tag('b', 'Warning: ')
. userdate($outage->get_starttime() - ($outage->get_warningminutes() * 60))
. html_writer::empty_tag('br')
. html_writer::tag('b', 'Starts: ') . userdate($outage->starttime, '%d %h %Y %l:%M%P')
. html_writer::tag('b', 'Starts: ')
. userdate($outage->get_starttime(), '%d %h %Y %l:%M%P')
. html_writer::empty_tag('br')
. html_writer::tag('b', 'Stops: ') . userdate($outage->stoptime, '%d %h %Y %l:%M%P')
. html_writer::tag('b', 'Stops: ')
. userdate($outage->get_stoptime(), '%d %h %Y %l:%M%P')
. html_writer::empty_tag('br')
. html_writer::tag('small',
'Created by ' . $created . ', modified by ' . $modified . ' on '
. userdate($outage->lastmodified, '%d %h %Y %l:%M%P')
) . html_writer::empty_tag('br')
. $linkedit . $linkdelete . html_writer::empty_tag('br')
'Created by ' . $created
. ', modified by ' . $modified . ' on '
. userdate($outage->get_lastmodified(), '%d %h %Y %l:%M%P')
)
. html_writer::empty_tag('br')
. $linkedit . $linkdelete
. html_writer::empty_tag('br')
. html_writer::empty_tag('br')
)
);

View File

@@ -24,6 +24,8 @@
*/
defined('MOODLE_INTERNAL') || die;
// FIXME If plugin not installed, it is still generating the category Outage under Auth Plugins.
if ($hassiteconfig) {
// Configure default settings page.
$settings->visiblename = "Defaults";

View File

@@ -20,6 +20,7 @@
* @package auth_outage
* @author Marcus Boon <marcus@catalyst-au.net>
* @author Brendan Heywood <brendan@catalyst-au.net>
* @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
*/
@@ -28,7 +29,7 @@ if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
}
$plugin->version = 2016083000; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2016083101; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = $plugin->version; // Same as version
$plugin->requires = 2014051200; // Requires Moodle 2.7 or later.
$plugin->component = "auth_outage";