From 54b72de472bd9d1dfd86b1c3106e28e5e8b6be02 Mon Sep 17 00:00:00 2001 From: Daniel Thee Roperto Date: Mon, 19 Sep 2016 17:06:16 +1000 Subject: [PATCH] Issue #39 - Minor UI changes. --- classes/cli/create.php | 2 +- classes/forms/outage/edit.php | 2 +- classes/models/outage.php | 17 +++- classes/outagedb.php | 2 +- classes/tables/manage/history.php | 80 +++++++++++++++++++ .../{manage.php => manage/managebase.php} | 57 ++----------- classes/tables/manage/planned.php | 67 ++++++++++++++++ lang/en/auth_outage.php | 5 +- renderer.php | 23 +++--- tests/cli/create_test.php | 4 +- 10 files changed, 188 insertions(+), 71 deletions(-) create mode 100644 classes/tables/manage/history.php rename classes/tables/{manage.php => manage/managebase.php} (67%) create mode 100644 classes/tables/manage/planned.php diff --git a/classes/cli/create.php b/classes/cli/create.php index 58d88b9..3c245aa 100644 --- a/classes/cli/create.php +++ b/classes/cli/create.php @@ -168,7 +168,7 @@ class create extends clibase { $outage = outagedb::get_by_id((int)$id); $this->set_defaults([ 'warn' => $outage->get_warning_duration(), - 'duration' => $outage->get_duration(), + 'duration' => $outage->get_duration_planned(), 'title' => $outage->title, 'description' => $outage->description, ]); diff --git a/classes/forms/outage/edit.php b/classes/forms/outage/edit.php index f4986a8..e724a73 100644 --- a/classes/forms/outage/edit.php +++ b/classes/forms/outage/edit.php @@ -134,7 +134,7 @@ class edit extends \moodleform { $this->_form->setDefaults([ 'id' => $outage->id, 'starttime' => $outage->starttime, - 'outageduration' => $outage->get_duration(), + 'outageduration' => $outage->get_duration_planned(), 'warningduration' => $outage->get_warning_duration(), 'title' => $outage->title, 'description' => ['text' => $outage->description, 'format' => '1'] diff --git a/classes/models/outage.php b/classes/models/outage.php index 6f51efa..21f5311 100644 --- a/classes/models/outage.php +++ b/classes/models/outage.php @@ -207,6 +207,17 @@ class outage { return $this->replace_placeholders($this->title); } + /** + * Gets the duration of the outage (start to actual finish, warning not included). + * @return int|null Duration in seconds or null if not finished. + */ + public function get_duration_actual() { + if (is_null($this->finished)) { + return null; + } + return $this->finished - $this->starttime; + } + /** * Returns the input string with all placeholders replaced. * @param $str string Input string. @@ -222,17 +233,17 @@ class outage { [ userdate($this->starttime, get_string('datetimeformat', 'auth_outage')), userdate($this->stoptime, get_string('datetimeformat', 'auth_outage')), - format_time($this->get_duration()), + format_time($this->get_duration_planned()), ], $str ); } /** - * Gets the duration of the outage (start to stop, warning not included). + * Gets the planned duration of the outage (start to planned stop, warning not included). * @return int Duration in seconds. */ - public function get_duration() { + public function get_duration_planned() { return $this->stoptime - $this->starttime; } diff --git a/classes/outagedb.php b/classes/outagedb.php index f15e505..4fa6d85 100644 --- a/classes/outagedb.php +++ b/classes/outagedb.php @@ -333,7 +333,7 @@ class outagedb { 'eventtype' => 'auth_outage', 'timestart' => $outage->starttime, 'visible' => true, - 'timeduration' => $outage->get_duration(), + 'timeduration' => $outage->get_duration_planned(), ]; } diff --git a/classes/tables/manage/history.php b/classes/tables/manage/history.php new file mode 100644 index 0000000..a13a0cc --- /dev/null +++ b/classes/tables/manage/history.php @@ -0,0 +1,80 @@ +. + +namespace auth_outage\tables\manage; + +use auth_outage\models\outage; +use html_writer; +use moodle_url; + +require_once($CFG->libdir . '/tablelib.php'); + +/** + * Manage outages table. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class history extends managebase { + /** + * Constructor + */ + public function __construct() { + parent::__construct(); + + $this->define_columns(['warning', 'starts', 'durationplanned', 'durationactual', 'title', 'actions']); + + $this->define_headers([ + get_string('tableheaderwarnbefore', 'auth_outage'), + get_string('tableheaderstarttime', 'auth_outage'), + get_string('tableheaderdurationplanned', 'auth_outage'), + get_string('tableheaderdurationactual', 'auth_outage'), + get_string('tableheadertitle', 'auth_outage'), + get_string('actions'), + ] + ); + + $this->setup(); + } + + /** + * Sets the data of the table. + * @param outage[] $outages An array with outage objects. + */ + public function set_data(array $outages) { + foreach ($outages as $outage) { + $title = html_writer::link( + new moodle_url('/auth/outage/edit.php', ['id' => $outage->id]), + $outage->get_title(), + ['title' => get_string('edit')] + ); + + $finished = $outage->get_duration_actual(); + $finished = is_null($finished) ? '-' : format_time($finished); + + $this->add_data([ + format_time($outage->get_warning_duration()), + userdate($outage->starttime, get_string('datetimeformat', 'auth_outage')), + format_time($outage->get_duration_planned()), + $finished, + $title, + $this->set_data_buttons($outage, false), + ]); + } + } +} diff --git a/classes/tables/manage.php b/classes/tables/manage/managebase.php similarity index 67% rename from classes/tables/manage.php rename to classes/tables/manage/managebase.php index 6126cb0..5101d93 100644 --- a/classes/tables/manage.php +++ b/classes/tables/manage/managebase.php @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -namespace auth_outage\tables; +namespace auth_outage\tables\manage; use auth_outage\models\outage; use flexible_table; @@ -24,14 +24,14 @@ use moodle_url; require_once($CFG->libdir . '/tablelib.php'); /** - * Manage outages table. + * Manage outages table base. * * @package auth_outage * @author Daniel Thee Roperto * @copyright Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class manage extends flexible_table { +class managebase extends flexible_table { private static $autoid = 0; /** @@ -44,55 +44,8 @@ class manage extends flexible_table { $id = (is_null($id) ? self::$autoid++ : $id); parent::__construct('auth_outage_manage_' . $id); - $this->define_columns(['starttime', 'stopsafter', 'warnbefore', 'finished', 'title', '']); - - $this->define_headers([ - get_string('tableheaderwarnbefore', 'auth_outage'), - get_string('tableheaderstarttime', 'auth_outage'), - get_string('tableheaderstopsafter', 'auth_outage'), - get_string('tableheaderfinishedat', 'auth_outage'), - get_string('tableheadertitle', 'auth_outage'), - get_string('actions'), - ] - ); - $this->define_baseurl($PAGE->url); $this->set_attribute('class', 'generaltable admintable'); - $this->setup(); - } - - /** - * Sets the data of the table. - * @param outage[] $outages An array with outage objects. - * @param bool $editdelete If it should display the edit and delete button. - */ - public function set_data(array $outages, $editdelete) { - if (!is_bool($editdelete)) { - throw new \InvalidArgumentException('$editdelete must be a bool.'); - } - - foreach ($outages as $outage) { - $title = $outage->get_title(); - if ($editdelete) { - $title = html_writer::link( - new moodle_url('/auth/outage/edit.php', ['id' => $outage->id]), - $title, - ['title' => get_string('edit')] - ); - } - - $finished = $outage->finished; - $finished = is_null($finished) ? '-' : userdate($finished, get_string('datetimeformat', 'auth_outage')); - - $this->add_data([ - format_time($outage->get_warning_duration()), - userdate($outage->starttime, get_string('datetimeformat', 'auth_outage')), - format_time($outage->get_duration()), - $finished, - $title, - $this->set_data_buttons($outage, $editdelete), - ]); - } } /** @@ -101,7 +54,7 @@ class manage extends flexible_table { * @param bool $editdelete If it should display the edit and delete button. * @return string The HTML code of the action buttons. */ - private function set_data_buttons(outage $outage, $editdelete) { + protected function set_data_buttons(outage $outage, $editdelete) { global $OUTPUT; $buttons = ''; @@ -171,6 +124,6 @@ class manage extends flexible_table { ); } - return $buttons; + return '' . $buttons . ''; } } diff --git a/classes/tables/manage/planned.php b/classes/tables/manage/planned.php new file mode 100644 index 0000000..9128c5e --- /dev/null +++ b/classes/tables/manage/planned.php @@ -0,0 +1,67 @@ +. + +namespace auth_outage\tables\manage; + +use auth_outage\models\outage; + +require_once($CFG->libdir . '/tablelib.php'); + +/** + * Manage outages table. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class planned extends managebase { + /** + * Constructor + */ + public function __construct() { + parent::__construct(); + + $this->define_columns(['warning', 'starts', 'duration', 'title', 'actions']); + + $this->define_headers([ + get_string('tableheaderwarnbefore', 'auth_outage'), + get_string('tableheaderstarttime', 'auth_outage'), + get_string('tableheaderduration', 'auth_outage'), + get_string('tableheadertitle', 'auth_outage'), + get_string('actions'), + ] + ); + + $this->setup(); + } + + /** + * Sets the data of the table. + * @param outage[] $outages An array with outage objects. + */ + public function set_data(array $outages) { + foreach ($outages as $outage) { + $this->add_data([ + format_time($outage->get_warning_duration()), + userdate($outage->starttime, get_string('datetimeformat', 'auth_outage')), + format_time($outage->get_duration_planned()), + $outage->get_title(), + $this->set_data_buttons($outage, true), + ]); + } + } +} diff --git a/lang/en/auth_outage.php b/lang/en/auth_outage.php index c339171..e937d52 100644 --- a/lang/en/auth_outage.php +++ b/lang/en/auth_outage.php @@ -98,9 +98,10 @@ $string['outageslistpast'] = 'Outage history'; $string['pluginname'] = 'Outage manager'; $string['starttime'] = 'Start date and time'; $string['starttime_help'] = 'At which date and time the outage starts, preventing general access to the system.'; -$string['tableheaderfinishedat'] = 'Finished at'; +$string['tableheaderduration'] = 'Duration'; +$string['tableheaderdurationplanned'] = 'Planned Duration'; +$string['tableheaderdurationactual'] = 'Actual Duration'; $string['tableheaderstarttime'] = 'Starts on'; -$string['tableheaderstopsafter'] = 'Stops after'; $string['tableheaderwarnbefore'] = 'Warns before'; $string['tableheadertitle'] = 'Title'; $string['textplaceholdershint'] = 'You can use {{start}}, {{stop}} and {{duration}} as placeholders on the title and description.'; diff --git a/renderer.php b/renderer.php index f09eb2b..1978587 100644 --- a/renderer.php +++ b/renderer.php @@ -15,6 +15,7 @@ // along with Moodle. If not, see . use auth_outage\models\outage; +use auth_outage\tables\manage\planned; if (!defined('MOODLE_INTERNAL')) { die('Direct access to this script is forbidden.'); // It must be included from a Moodle page. @@ -87,8 +88,8 @@ class auth_outage_renderer extends plugin_renderer_base { if (empty($future)) { echo html_writer::tag('p', html_writer::tag('small', get_string('notfound', 'auth_outage'))); } else { - $table = new \auth_outage\tables\manage(); - $table->set_data($future, true); + $table = new planned(); + $table->set_data($future); $table->finish_output(); } @@ -96,8 +97,8 @@ class auth_outage_renderer extends plugin_renderer_base { if (empty($past)) { echo html_writer::tag('p', html_writer::tag('small', get_string('notfound', 'auth_outage'))); } else { - $table = new \auth_outage\tables\manage(); - $table->set_data($past, false); + $table = new \auth_outage\tables\manage\history(); + $table->set_data($past); $table->finish_output(); } } @@ -146,7 +147,11 @@ class auth_outage_renderer extends plugin_renderer_base { $linkdelete = html_writer::link($url, $img, ['title' => get_string('delete')]); $finished = $outage->finished; - $finished = is_null($finished) ? '-' : userdate($finished, get_string('datetimeformat', 'auth_outage')); + if (is_null($finished)) { + $finished = get_string('na', 'auth_outage'); + } else { + $finished = userdate($finished, get_string('datetimeformat', 'auth_outage')); + } return html_writer::div( html_writer::tag('blockquote', @@ -161,11 +166,11 @@ class auth_outage_renderer extends plugin_renderer_base { . userdate($outage->starttime, get_string('datetimeformat', 'auth_outage')) ) . html_writer::div( - html_writer::tag('b', get_string('tableheaderstopsafter', 'auth_outage') . ': ') - . format_time($outage->get_duration()) + html_writer::tag('b', get_string('tableheaderdurationplanned', 'auth_outage') . ': ') + . format_time($outage->get_duration_planned()) ) . html_writer::div( - html_writer::tag('b', get_string('tableheaderfinishedat', 'auth_outage') . ': ') + html_writer::tag('b', get_string('tableheaderdurationactual', 'auth_outage') . ': ') . $finished ) . html_writer::div( @@ -201,7 +206,7 @@ class auth_outage_renderer extends plugin_renderer_base { 'startofwarning' => -$outage->get_warning_duration(), '15secondsbefore' => -15, 'start' => 0, - 'endofoutage' => $outage->get_duration(), + 'endofoutage' => $outage->get_duration_planned(), ] as $title => $delta) { $adminlinks[] = html_writer::link( new moodle_url( diff --git a/tests/cli/create_test.php b/tests/cli/create_test.php index 0701e2e..4fb0abc 100644 --- a/tests/cli/create_test.php +++ b/tests/cli/create_test.php @@ -148,7 +148,7 @@ class create_test extends cli_testcase { $outage = outagedb::get_by_id($id); self::assertSame($now, $outage->starttime); self::assertSame(10, $outage->get_warning_duration()); - self::assertSame(30, $outage->get_duration()); + self::assertSame(30, $outage->get_duration_planned()); self::assertNull($outage->finished); self::assertSame('A Title', $outage->title); self::assertSame('A Description', $outage->description); @@ -235,7 +235,7 @@ class create_test extends cli_testcase { $cloned = outagedb::get_by_id((int)$id); self::assertSame($now + 60, $cloned->starttime); self::assertSame($original->get_warning_duration(), $cloned->get_warning_duration()); - self::assertSame($original->get_duration(), $cloned->get_duration()); + self::assertSame($original->get_duration_planned(), $cloned->get_duration_planned()); self::assertSame($original->title, $cloned->title); self::assertSame($original->description, $cloned->description); }