Outage remodelled both in DB and PHP Class. Issue #17.

This commit is contained in:
Daniel Thee Roperto
2016-09-09 11:39:44 +10:00
parent 4e46e934d0
commit 6bb9320ef6
9 changed files with 115 additions and 58 deletions

View File

@@ -16,6 +16,8 @@
namespace auth_outage\forms\outage;
use \auth_outage\models\outage;
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
}
@@ -53,7 +55,7 @@ class edit extends \moodleform {
'text',
'title',
get_string('title', 'auth_outage'),
'maxlength="'.self::TITLE_MAX_CHARS.'"'
'maxlength="' . self::TITLE_MAX_CHARS . '"'
);
$mform->setType('title', PARAM_TEXT);
@@ -74,8 +76,8 @@ class edit extends \moodleform {
public function validation($data, $files) {
$errors = parent::validation($data, $files);
if ($data['stoptime'] <= $data['starttime']) {
$errors['stoptime'] = get_string('stoptimeerrornotafterstart', 'auth_outage');
if ($data['outageduration'] <= 0) {
$errors['outageduration'] = get_string('outagedurationerrorinvalid', 'auth_outage');
}
if ($data['warningduration'] <= 0) {
$errors['warningduration'] = get_string('warningdurationerrorinvalid', 'auth_outage');
@@ -92,4 +94,44 @@ class edit extends \moodleform {
return $errors;
}
/**
* Return submitted data if properly submitted or returns NULL if validation fails.
* @return outage submitted data; NULL if not valid or not submitted or cancelled
*/
public function get_data() {
// Fetch data and check if description is the correct format.
$data = parent::get_data();
if (is_null($data)) {
return null;
}
if ($data->description['format'] != '1') {
debugging('Not implemented for format ' . $data->description['format'], DEBUG_DEVELOPER);
return null;
}
// Return an outage.
return new outage([
'id' => ($data->id === 0) ? null : $data->id,
'starttime' => $data->starttime,
'stoptime' => $data->starttime + $data->outageduration,
'warntime' => $data->starttime - $data->warningduration,
'title' => $data->title,
'description' => $data->description['text']
]);
}
/**
* Load in existing outage as form defaults.
*
* @param outage $outage outage object with default values
*/
public function set_data(outage $outage) {
$this->_form->setDefaults([
'id' => $outage->id,
'starttime' => $outage->starttime,
'outageduration' => $outage->stoptime - $outage->starttime,
'warningduration' => $outage->starttime - $outage->warntime,
'title' => $outage->title,
'description' => ['text' => $outage->description, 'format' => '1']
]);
}
}

View File

@@ -97,6 +97,11 @@ class outage {
throw new \InvalidArgumentException('$data must be null (default), an array or an object.');
}
/**
* Checks if the outage is happening.
* @param int|null $time Null to check if the outage is happening now or another time to use as reference.
* @return bool True if outage has started but not yet stopped. False otherwise including if in warning period.
*/
public function is_ongoing($time = null) {
if ($time === null) {
$time = time();
@@ -110,4 +115,39 @@ class outage {
return (($this->starttime <= $time) && ($time < $this->stoptime));
}
/**
* Get the title with properly replaced placeholders such as {{start}} and {{stop}}.
* @return string Title.
*/
public function get_title() {
return $this->replace_placeholders($this->title);
}
/**
* Get the description with properly replaced placeholders such as {{start}} and {{stop}}.
* @return string Description.
*/
public function get_description() {
return $this->replace_placeholders($this->description);
}
/**
* Returns the input string with all placeholders replaced.
* @param $str string Input string.
* @return string Output string.
*/
private function replace_placeholders($str) {
return str_replace(
[
'{{start}}',
'{{stop}}'
],
[
userdate($this->starttime, get_string('strftimedatetimeshort')),
userdate($this->stoptime, get_string('strftimedatetimeshort')),
],
$str
);
}
}

View File

@@ -98,21 +98,4 @@ class outagelib {
}
}
}
/**
* Parses data from the form ensuring it is valid for an outage object.
*
* @param $data stdClass The input data.
* @return stdClass The parsed data.
*/
public static function parseformdata(\stdClass $data) {
if ($data->description['format'] != '1') {
throw new \InvalidArgumentException('Not implemented for format ' . $data->description['format']);
}
if ($data->id === 0) {
$data->id = null;
}
$data->description = $data->description['text'];
return $data;
}
}