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

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