diff --git a/classes/outagedb.php b/classes/outagedb.php index a97dcda..754f0d6 100644 --- a/classes/outagedb.php +++ b/classes/outagedb.php @@ -88,8 +88,12 @@ final class outagedb public function getbyid($id) { global $DB; - if (!is_int($id)) throw new InvalidArgumentException('$id must be an int.'); - if ($id <= 0) throw new InvalidArgumentException('$id must be positive.'); + if (!is_int($id)) { + throw new InvalidArgumentException('$id must be an int.'); + } + if ($id <= 0) { + throw new InvalidArgumentException('$id must be positive.'); + } $outage = $DB->get_record('auth_outage', ['id' => $id]); if ($outage === false) { @@ -130,4 +134,22 @@ final class outagedb $DB->update_record('auth_outage', $outage); return $outage->id; } + + /** + * Deletes an outage from the database. + * + * @param $id Outage ID to delete + */ + public function delete($id) { + global $DB; + + if (!is_int($id)) { + throw new InvalidArgumentException('$id must be an int.'); + } + if ($id <= 0) { + throw new InvalidArgumentException('$id must be positive.'); + } + + $DB->delete_records('auth_outage', ['id' => $id]); + } } \ No newline at end of file diff --git a/classes/outagedeleteform.php b/classes/outagedeleteform.php new file mode 100644 index 0000000..aeebf12 --- /dev/null +++ b/classes/outagedeleteform.php @@ -0,0 +1,61 @@ +. + +namespace auth_outage; + +if (!defined('MOODLE_INTERNAL')) { + die('Direct access to this script is forbidden.'); // It must be included from a Moodle page. +} + +require_once($CFG->libdir . '/formslib.php'); + +/** + * Outage delete confirmation form. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class outagedeleteform extends \moodleform { + /** + * {@inheritDoc} + * @see moodleform::definition() + */ + public function definition() { + $mform = $this->_form; + + $mform->addElement('hidden', 'id'); + $mform->setType('id', PARAM_INT); + + $this->add_action_buttons(true, 'Remove'); + } + + /** + * Validate the parts of the request form for this module + * + * @param array $data An array of form data + * @param array $files An array of form files + * @return array of error messages + */ + public function validation($data, $files) { + $errors = parent::validation($data, $files); + $mform = $this->_form; + + return $errors; + } + +} \ No newline at end of file diff --git a/remove.php b/remove.php new file mode 100644 index 0000000..e8e418a --- /dev/null +++ b/remove.php @@ -0,0 +1,61 @@ +. + +/** + * Delete an outage. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +use \auth_outage\outage; +use \auth_outage\outageutils; +use \auth_outage\outagedb; +use \auth_outage\outagedeleteform; + +require_once('../../config.php'); +require_once($CFG->libdir . '/adminlib.php'); +require_once($CFG->libdir . '/formslib.php'); + +$renderer = outageutils::pagesetup(); + +$mform = new outagedeleteform(); +if ($mform->is_cancelled()) { + redirect('/auth/outage/list.php'); +} else if ($fromform = $mform->get_data()) { + outagedb::get()->delete($fromform->id); + redirect('/auth/outage/list.php'); +} + +$id = required_param('id', PARAM_INT); +$outage = outagedb::get()->getbyid($id); +if ($outage == null) { + throw new invalid_parameter_exception('Outage #' . $id . ' not found.'); +} + +$dataid = new stdClass(); +$dataid->id = $outage->id; +$mform->set_data($dataid); + +echo $OUTPUT->header(); + +echo $renderer->renderdeleteconfirmation($outage); + +$mform->display(); + +echo $OUTPUT->footer(); diff --git a/renderer.php b/renderer.php index 984774b..d9df979 100644 --- a/renderer.php +++ b/renderer.php @@ -31,6 +31,11 @@ if (!defined('MOODLE_INTERNAL')) { */ class auth_outage_renderer extends plugin_renderer_base { + public function renderdeleteconfirmation(outage $outage) { + return html_writer::tag('h3', 'You are about to remove the following outage:') + . $this->renderoutage($outage, false); + } + public function renderoutagelist(array $outages) { global $OUTPUT; @@ -38,7 +43,7 @@ class auth_outage_renderer extends plugin_renderer_base // Generate list of outages. foreach ($outages as $outage) { - $html .= $this->renderoutagelistentry($outage); + $html .= $this->renderoutage($outage, true); } // Add 'add' button. @@ -52,7 +57,7 @@ class auth_outage_renderer extends plugin_renderer_base return $html; } - private function renderoutagelistentry(outage $outage) { + private function renderoutage(outage $outage, $buttons) { global $OUTPUT; $created = core_user::get_user($outage->createdby, 'firstname,lastname', MUST_EXIST); @@ -98,8 +103,7 @@ class auth_outage_renderer extends plugin_renderer_base . userdate($outage->lastmodified, '%d %h %Y %l:%M%P') ) . html_writer::empty_tag('br') - . $linkedit . $linkdelete - . html_writer::empty_tag('br') + . ($buttons ? $linkedit . $linkdelete . html_writer::empty_tag('br') : '') . html_writer::empty_tag('br') ) );