From 6a78557487b52f84d79a86f6ed4d229d694c6e6b Mon Sep 17 00:00:00 2001 From: Daniel Thee Roperto Date: Mon, 26 Sep 2016 22:14:03 +1000 Subject: [PATCH] Issue #46 - Merged new, edit and clone into a single page. Moodle code checker was complaining about duplicated code and it was right! --- classes/output/manage/base_table.php | 4 +- classes/output/renderer.php | 2 +- clone.php | 60 --------------------------- edit.php | 42 +++++++++++++++---- lang/en/auth_outage.php | 3 ++ new.php | 62 ---------------------------- tests/behat/behat_auth_outage.php | 2 +- views/info/content.php | 2 +- views/manage.php | 2 +- 9 files changed, 44 insertions(+), 135 deletions(-) delete mode 100644 clone.php delete mode 100644 new.php diff --git a/classes/output/manage/base_table.php b/classes/output/manage/base_table.php index 02e527f..84524c4 100644 --- a/classes/output/manage/base_table.php +++ b/classes/output/manage/base_table.php @@ -78,7 +78,7 @@ class base_table extends flexible_table { // Edit button if required. if ($editdelete) { $buttons .= html_writer::link( - new moodle_url('/auth/outage/edit.php', ['id' => $outage->id]), + new moodle_url('/auth/outage/edit.php', ['edit' => $outage->id]), html_writer::empty_tag('img', [ 'src' => $OUTPUT->pix_url('t/edit'), 'alt' => get_string('edit'), @@ -90,7 +90,7 @@ class base_table extends flexible_table { // Clone button. $buttons .= html_writer::link( - new moodle_url('/auth/outage/clone.php', ['id' => $outage->id]), + new moodle_url('/auth/outage/edit.php', ['clone' => $outage->id]), html_writer::empty_tag('img', [ 'src' => $OUTPUT->pix_url('t/copy'), 'alt' => get_string('clone', 'auth_outage'), diff --git a/classes/output/renderer.php b/classes/output/renderer.php index de4166e..fb71a96 100644 --- a/classes/output/renderer.php +++ b/classes/output/renderer.php @@ -170,7 +170,7 @@ class renderer extends plugin_renderer_base { ); } - $url = new moodle_url('/auth/outage/edit.php', ['id' => $outage->id]); + $url = new moodle_url('/auth/outage/edit.php', ['edit' => $outage->id]); $img = html_writer::empty_tag( 'img', ['src' => $OUTPUT->pix_url('t/edit'), 'alt' => get_string('edit'), 'class' => 'iconsmall'] diff --git a/clone.php b/clone.php deleted file mode 100644 index 6dd00e8..0000000 --- a/clone.php +++ /dev/null @@ -1,60 +0,0 @@ -. - -/** - * Clone outage. - * - * @package auth_outage - * @author Daniel Thee Roperto - * @copyright 2016 Catalyst IT - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -use auth_outage\dml\outagedb; -use auth_outage\form\outage\edit; -use auth_outage\output\renderer; - -require_once(__DIR__.'/../../config.php'); -require_once($CFG->libdir.'/adminlib.php'); -require_once($CFG->libdir.'/formslib.php'); - -admin_externalpage_setup('auth_outage_manage'); -$PAGE->set_url(new moodle_url('/auth/outage/manage.php')); - -$mform = new edit(); - -if ($mform->is_cancelled()) { - redirect('/auth/outage/manage.php'); -} else if ($outage = $mform->get_data()) { - $id = outagedb::save($outage); - redirect('/auth/outage/manage.php#auth_outage_id_'.$id); -} - -$id = required_param('id', PARAM_INT); -$outage = outagedb::get_by_id($id); -if ($outage == null) { - throw new invalid_parameter_exception('Outage #'.$id.' not found.'); -} - -// Remove outage id to force creating a new one. -$outage->id = null; -$mform->set_data($outage); - -$PAGE->navbar->add($outage->get_title()); -echo $OUTPUT->header(); -echo renderer::get()->rendersubtitle('outageclone'); -$mform->display(); -echo $OUTPUT->footer(); diff --git a/edit.php b/edit.php index 074d103..a2008df 100644 --- a/edit.php +++ b/edit.php @@ -15,7 +15,7 @@ // along with Moodle. If not, see . /** - * Create new outage. + * Edit an outage. * * @package auth_outage * @author Daniel Thee Roperto @@ -25,6 +25,8 @@ use auth_outage\dml\outagedb; use auth_outage\form\outage\edit; +use auth_outage\local\outage; +use auth_outage\local\outagelib; use auth_outage\output\renderer; require_once(__DIR__.'/../../config.php'); @@ -43,15 +45,41 @@ if ($mform->is_cancelled()) { redirect('/auth/outage/manage.php#auth_outage_id_'.$id); } -$id = required_param('id', PARAM_INT); -$outage = outagedb::get_by_id($id); -if ($outage == null) { - throw new invalid_parameter_exception('Outage #'.$id.' not found.'); +$clone = optional_param('clone', 0, PARAM_INT); +$edit = optional_param('edit', 0, PARAM_INT); +if ($clone && $edit) { + throw new invalid_parameter_exception('Cannot provide both clone and edit ids.'); } +if ($clone) { + // Remove outage id to force creating a new one. + $outage = outagedb::get_by_id($clone); + $outage->id = null; + $action = 'outageclone'; +} else if ($edit) { + $outage = outagedb::get_by_id($edit); + $action = 'outageedit'; +} else { + $config = outagelib::get_config(); + $time = time(); + $outage = new outage([ + 'autostart' => $config->default_autostart, + 'starttime' => $time, + 'stoptime' => $time + $config->default_duration, + 'warntime' => $time - $config->default_warning_duration, + 'title' => $config->default_title, + 'description' => $config->default_description, + ]); + $action = 'outagecreate'; +} + +if ($outage == null) { + throw new invalid_parameter_exception('Outage not found.'); +} + $mform->set_data($outage); -$PAGE->navbar->add($outage->get_title()); +$PAGE->navbar->add(get_string($action.'crumb', 'auth_outage')); echo $OUTPUT->header(); -echo renderer::get()->rendersubtitle('outageedit'); +echo renderer::get()->rendersubtitle($action); $mform->display(); echo $OUTPUT->footer(); diff --git a/lang/en/auth_outage.php b/lang/en/auth_outage.php index d57a927..b77d248 100644 --- a/lang/en/auth_outage.php +++ b/lang/en/auth_outage.php @@ -93,8 +93,11 @@ $string['messageoutagewarning'] = 'Shutting down in {{countdown}}'; $string['na'] = 'n/a'; $string['notfound'] = 'No outages found.'; $string['outageedit'] = 'Edit Outage'; +$string['outageeditcrumb'] = 'Edit'; $string['outageclone'] = 'Clone Outage'; +$string['outageclonecrumb'] = 'Clone'; $string['outagecreate'] = 'Create Outage'; +$string['outagecreatecrumb'] = 'Create'; $string['outagedelete'] = 'Delete Outage'; $string['outagedeletewarning'] = 'You are about to permanently delete the outage below. This cannot be undone.'; $string['outageduration'] = 'Outage Duration'; diff --git a/new.php b/new.php deleted file mode 100644 index 9d85a92..0000000 --- a/new.php +++ /dev/null @@ -1,62 +0,0 @@ -. - -/** - * Create new outage. - * - * @package auth_outage - * @author Daniel Thee Roperto - * @copyright 2016 Catalyst IT - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -use auth_outage\dml\outagedb; -use auth_outage\form\outage\edit; -use auth_outage\local\outage; -use auth_outage\local\outagelib; - -require_once(__DIR__.'/../../config.php'); -require_once($CFG->libdir.'/adminlib.php'); -require_once($CFG->libdir.'/formslib.php'); - -admin_externalpage_setup('auth_outage_manage'); -$PAGE->set_url(new moodle_url('/auth/outage/manage.php')); - -$mform = new edit(); -if ($mform->is_cancelled()) { - redirect('/auth/outage/manage.php'); -} else if ($outage = $mform->get_data()) { - $id = outagedb::save($outage); - redirect('/auth/outage/manage.php#auth_outage_id_'.$id); -} - -$config = outagelib::get_config(); -$defaults = new outage([ - 'autostart' => $config->default_autostart, - 'starttime' => time(), - 'stoptime' => time() + $config->default_duration, - 'warntime' => time() - $config->default_warning_duration, - 'title' => $config->default_title, - 'description' => $config->default_description, -]); -$mform->set_data($defaults); - -$PAGE->navbar->add(get_string('outagecreate', 'auth_outage')); -echo $OUTPUT->header(); - -$mform->display(); - -echo $OUTPUT->footer(); diff --git a/tests/behat/behat_auth_outage.php b/tests/behat/behat_auth_outage.php index b9c23ca..b0f30c8 100644 --- a/tests/behat/behat_auth_outage.php +++ b/tests/behat/behat_auth_outage.php @@ -67,7 +67,7 @@ class behat_auth_outage extends behat_base { * @Given /^I visit the Create Outage Page$/ */ public function i_visit_the_create_outage_page() { - $this->getSession()->visit($this->locate_path('/auth/outage/new.php')); + $this->getSession()->visit($this->locate_path('/auth/outage/edit.php')); } /** diff --git a/views/info/content.php b/views/info/content.php index f99f2bd..b02a4d3 100644 --- a/views/info/content.php +++ b/views/info/content.php @@ -47,7 +47,7 @@ if ($viewbag['admin']) { } $admineditlink = html_writer::link( - new moodle_url('/auth/outage/edit.php', ['id' => $viewbag['outage']->id]), + new moodle_url('/auth/outage/edit.php', ['edit' => $viewbag['outage']->id]), get_string('outageedit', 'auth_outage') ); } diff --git a/views/manage.php b/views/manage.php index 015aa50..0a54f79 100644 --- a/views/manage.php +++ b/views/manage.php @@ -29,7 +29,7 @@ use auth_outage\output\renderer; defined('MOODLE_INTERNAL') || die(); -$urlnew = new moodle_url('/auth/outage/new.php'); +$urlnew = new moodle_url('/auth/outage/edit.php'); ?>