diff --git a/classes/forms/outage/edit.php b/classes/forms/outage/edit.php index e43aa57..a4e8152 100644 --- a/classes/forms/outage/edit.php +++ b/classes/forms/outage/edit.php @@ -129,8 +129,8 @@ class edit extends \moodleform { $this->_form->setDefaults([ 'id' => $outage->id, 'starttime' => $outage->starttime, - 'outageduration' => $outage->stoptime - $outage->starttime, - 'warningduration' => $outage->starttime - $outage->warntime, + 'outageduration' => $outage->get_duration(), + '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 a799078..ab24dd3 100644 --- a/classes/models/outage.php +++ b/classes/models/outage.php @@ -28,6 +28,29 @@ namespace auth_outage\models; use auth_outage\outagelib; class outage { + private static function get_seconds_duration_string($duration) { + if (!is_int($duration)) { + throw new \InvalidArgumentException('$seconds must be an int.'); + } + + if (($duration < 60) || ($duration % 60 != 0)) { + return $duration . ' ' . get_string('durationseconds', 'auth_outage'); + } + + $duration /= 60; + if (($duration < 60) || ($duration % 60 != 0)) { + return $duration . ' ' . get_string('durationminutes', 'auth_outage'); + } + + $duration /= 60; + if (($duration < 60) || ($duration % 24 != 0)) { + return $duration . ' ' . get_string('durationhours', 'auth_outage'); + } + + $duration /= 24; + return $duration . ' ' . get_string('durationdays', 'auth_outage'); + } + /** * @var int Outage ID (auto generated by the DB). */ @@ -150,4 +173,36 @@ class outage { $str ); } + + /** + * Gets the duration of the outage (start to stop, warning not included). + * @return int Duration in seconds. + */ + public function get_duration() { + return $this->stoptime - $this->starttime; + } + + /** + * Gets the duration of the outage (start to stop, warning not included). + * @return string The duration as text, for example '6 hour(s)'. + */ + public function get_duration_string() { + return self::get_seconds_duration_string($this->get_duration()); + } + + /** + * Gets the warning duration from the outage (from warning time to start time). + * @return int Warning duration in seconds. + */ + public function get_warning_duration() { + return $this->starttime - $this->warntime; + } + + /** + * Gets the warning duration from the outage (from warning time to start time). + * @return string The warning duration as text, for example '6 hour(s)'. + */ + public function get_warning_duration_string() { + return self::get_seconds_duration_string($this->get_warning_duration()); + } } diff --git a/classes/tables/manage.php b/classes/tables/manage.php new file mode 100644 index 0000000..a34e32b --- /dev/null +++ b/classes/tables/manage.php @@ -0,0 +1,79 @@ +. + +namespace auth_outage\tables; + +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 manage extends \flexible_table { + private static $autoid = 0; + + public function __construct($id = null) { + global $PAGE; + + $id = (is_null($id) ? self::$autoid++ : $id); + parent::__construct('auth_outage_manage_' . $id); + + $this->define_columns(['starttime', 'stopsafter', 'warnbefore', 'title', '']); + + $this->define_headers([ + get_string('tableheaderstarttime', 'auth_outage'), + get_string('tableheaderstopsafter', 'auth_outage'), + get_string('tableheaderwarnbefore', 'auth_outage'), + get_string('tableheadertitle', 'auth_outage'), + '', + ] + ); + + $this->define_baseurl($PAGE->url); + $this->set_attribute('class', 'generaltable admintable'); + $this->setup(); + } + + public function set_data(array $outages) { + global $OUTPUT; + foreach ($outages as $outage) { + $buttons = ''; + + $url = new \moodle_url('/auth/outage/edit.php', ['id' => $outage->id]); + $html = \html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/edit'), 'alt' => get_string('edit'), 'class' => 'iconsmall')); + $buttons .= \html_writer::link($url, $html, array('title' => get_string('edit'))); + + $url = new \moodle_url('/auth/outage/delete.php', ['id' => $outage->id]); + $html = \html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/delete'), 'alt' => get_string('delete'), 'class' => 'iconsmall')); + $buttons .= \html_writer::link($url, $html, array('title' => get_string('delete'))); + + // Table columns 'name', 'action', 'role', 'parent', 'continue', 'priority', 'data'. + $values = [ + userdate($outage->starttime, get_string('tablerowstarts', 'auth_outage')), + $outage->get_duration_string(), + $outage->get_warning_duration_string(), + $outage->get_title(), + $buttons, + ]; + + $this->add_data($values); + } + } +} \ No newline at end of file diff --git a/lang/en/auth_outage.php b/lang/en/auth_outage.php index 08c8555..14cc147 100644 --- a/lang/en/auth_outage.php +++ b/lang/en/auth_outage.php @@ -37,6 +37,10 @@ $string['defaultwarningdescription'] = 'Description'; $string['defaultwarningdescriptiondescription'] = 'Default warning message for outages. Use {{start}} and {{stop}} placeholders as required.'; $string['defaultwarningdescriptionvalue'] = 'There is an scheduled maintenance from {{start}} to {{stop}} and our system will not be available during that time.'; $string['description'] = 'Public Description'; +$string['durationseconds'] = 'second(s)'; +$string['durationminutes'] = 'minutes(s)'; +$string['durationhours'] = 'hour(s)'; +$string['durationdays'] = 'day(s)'; $string['menudefaults'] = 'Default Settings'; $string['menumanage'] = 'Manage'; $string['messageoutageongoing'] = 'Our system will be under maintenance until {$a->stop}.'; @@ -51,6 +55,11 @@ $string['outageslist'] = 'Outages List'; $string['pluginname'] = 'Outage manager'; $string['readmore'] = 'Read More'; $string['starttime'] = 'Start date and time'; +$string['tableheaderstarttime'] = 'Starts on'; +$string['tableheaderstopsafter'] = 'Stops after'; +$string['tableheaderwarnbefore'] = 'Warns before'; +$string['tableheadertitle'] = 'Title'; +$string['tablerowstarts'] = '%d/%m/%Y %H:%M'; $string['textplaceholdershint'] = 'You can use {{start}} and {{stop}} as placeholders on the title/description for the actual start/stop time.'; $string['titleerrorinvalid'] = 'Title cannot be left blank.'; $string['titleerrortoolong'] = 'Title cannot have more than {$a} characters.'; diff --git a/manage.php b/manage.php index d5af095..f695786 100644 --- a/manage.php +++ b/manage.php @@ -33,6 +33,8 @@ $renderer = outagelib::pagesetup(); echo $OUTPUT->header(); -echo $renderer->renderoutagelist(outagedb::get_all()); +$table = new \auth_outage\tables\manage(); +$table->set_data(outagedb::get_all()); +echo $table->finish_output(); echo $OUTPUT->footer(); diff --git a/renderer.php b/renderer.php index 4ca1f9e..dfb8b9c 100644 --- a/renderer.php +++ b/renderer.php @@ -43,31 +43,6 @@ class auth_outage_renderer extends plugin_renderer_base { . $this->renderoutage($outage, false); } - public function renderoutagelist(array $outages) { - global $OUTPUT; - - $html = $this->rendersubtitle('outageslist'); - - // Generate list of outages. - foreach ($outages as $outage) { - $html .= $this->renderoutage($outage, true); - } - - // Add 'add' button. - $url = new moodle_url('/auth/outage/new.php'); - $img = html_writer::empty_tag('img', - ['src' => $OUTPUT->pix_url('t/add'), 'alt' => get_string('create'), 'class' => 'iconsmall']); - $html .= html_writer::tag('p', - html_writer::link( - $url, - $img . ' ' . get_string('outagecreate', 'auth_outage'), - ['title' => get_string('delete')] - ) - ); - - return $html; - } - private function renderoutage(outage $outage, $buttons) { global $OUTPUT;