From d9b852792ecc68bf5c4a3afe8050e7ef8b4b6b50 Mon Sep 17 00:00:00 2001 From: Daniel Thee Roperto Date: Wed, 31 Aug 2016 11:26:27 +1000 Subject: [PATCH] Improved outage class encapsulation. Added outagedb as DB Context. Other minor changes. --- auth.php | 7 +++ classes/outage.php | 141 ++++++++++++++++++++++++++++++++++++++++--- classes/outagedb.php | 108 +++++++++++++++++++++++++++++++++ db/install.xml | 12 ++-- list.php | 17 +----- renderer.php | 34 ++++++----- settings.php | 2 + version.php | 3 +- 8 files changed, 280 insertions(+), 44 deletions(-) create mode 100644 classes/outagedb.php diff --git a/auth.php b/auth.php index d33b90a..b8838f9 100644 --- a/auth.php +++ b/auth.php @@ -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 diff --git a/classes/outage.php b/classes/outage.php index a1c3851..168a400 100644 --- a/classes/outage.php +++ b/classes/outage.php @@ -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; + } } \ No newline at end of file diff --git a/classes/outagedb.php b/classes/outagedb.php new file mode 100644 index 0000000..27d85b7 --- /dev/null +++ b/classes/outagedb.php @@ -0,0 +1,108 @@ +. + +/** + * The DB Context to manipulate Outages. Singleton class. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @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; + } +} \ No newline at end of file diff --git a/db/install.xml b/db/install.xml index c72b242..d5a046d 100644 --- a/db/install.xml +++ b/db/install.xml @@ -7,14 +7,14 @@ - - - + + + - - - + + + diff --git a/list.php b/list.php index 8adb2ef..733693d 100644 --- a/list.php +++ b/list.php @@ -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(); diff --git a/renderer.php b/renderer.php index a7aa943..99c4258 100644 --- a/renderer.php +++ b/renderer.php @@ -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') ) ); diff --git a/settings.php b/settings.php index 9713047..67d766d 100644 --- a/settings.php +++ b/settings.php @@ -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"; diff --git a/version.php b/version.php index 3a9f8f0..a922cb7 100644 --- a/version.php +++ b/version.php @@ -20,6 +20,7 @@ * @package auth_outage * @author Marcus Boon * @author Brendan Heywood + * @author Daniel Thee Roperto * @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";