diff --git a/change.php b/change.php new file mode 100644 index 0000000..8865611 --- /dev/null +++ b/change.php @@ -0,0 +1,61 @@ +. + +/** + * Create new 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\outageform; + +require_once('../../config.php'); +require_once($CFG->libdir . '/adminlib.php'); +require_once($CFG->libdir . '/formslib.php'); + +outageutils::pagesetup(); + +$mform = new outageform(); + +if ($mform->is_cancelled()) { + redirect('/auth/outage/list.php'); +} else if ($fromform = $mform->get_data()) { + $fromform = outageutils::parseformdata($fromform); + $outage = new outage($fromform); + $id = outagedb::get()->save($outage); + redirect('/auth/outage/list.php#auth_outage_id=' . $id); +} + +$id = required_param('id', PARAM_INT); +$outage = outagedb::get()->getbyid($id); +if ($outage == null) { + throw new invalid_parameter_exception('Outage #' . $id . ' not found.'); +} +$data = get_object_vars($outage); +$data['description'] = ['text' => $data['description'], 'format' => '1']; +$mform->set_data($data); + +echo $OUTPUT->header(); + +$mform->display(); + +echo $OUTPUT->footer(); diff --git a/classes/outagedb.php b/classes/outagedb.php index 2efe26d..a97dcda 100644 --- a/classes/outagedb.php +++ b/classes/outagedb.php @@ -25,6 +25,8 @@ namespace auth_outage; +use Box\Spout\Common\Exception\InvalidArgumentException; + final class outagedb { /** @@ -83,9 +85,32 @@ final class outagedb return $outages; } + 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.'); + + $outage = $DB->get_record('auth_outage', ['id' => $id]); + if ($outage === false) { + return null; + } + + return new outage($outage); + } + + /** + * Saves an outage to the database. + * + * @param outage $outage Outage to save. + * @return int Outage ID. + */ public function save(outage $outage) { global $DB, $USER; + // Do not change the original object. + $outage = clone $outage; + // If new outage, set its creator. if ($outage->id === null) { $outage->createdby = $USER->id; @@ -95,7 +120,14 @@ final class outagedb $outage->modifiedby = $USER->id; $outage->lastmodified = time(); - // Save it and return the id. - return $DB->insert_record('auth_outage', $outage, true); + // If new, create it and return the id. + if ($outage->id === null) { + return $DB->insert_record('auth_outage', $outage, true); + } + + // Clean up the class (remove creator field), then update it and return its id. + unset($outage->createdby); + $DB->update_record('auth_outage', $outage); + return $outage->id; } } \ No newline at end of file diff --git a/classes/outageform.php b/classes/outageform.php index ac87c44..93041c3 100644 --- a/classes/outageform.php +++ b/classes/outageform.php @@ -37,7 +37,9 @@ class outageform extends \moodleform { */ public function definition() { $mform = $this->_form; - $data = $this->_customdata; + + $mform->addElement('hidden', 'id'); + $mform->setType('id', PARAM_INT); $mform->addElement('date_time_selector', 'starttime', 'Start Time'); diff --git a/classes/outageutils.php b/classes/outageutils.php index f5c6847..e1abb87 100644 --- a/classes/outageutils.php +++ b/classes/outageutils.php @@ -83,6 +83,9 @@ class outageutils 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; } diff --git a/create.php b/create.php index 94c81f3..bd997b6 100644 --- a/create.php +++ b/create.php @@ -36,7 +36,7 @@ outageutils::pagesetup(); $mform = new outageform(); if ($mform->is_cancelled()) { - redirect($listurl); + redirect('/auth/outage/list.php'); } else if ($fromform = $mform->get_data()) { $fromform = outageutils::parseformdata($fromform); $outage = new outage($fromform); diff --git a/renderer.php b/renderer.php index 08c0324..984774b 100644 --- a/renderer.php +++ b/renderer.php @@ -67,14 +67,12 @@ class auth_outage_renderer extends plugin_renderer_base trim($modified->firstname . ' ' . $modified->lastname) ); - $url = new moodle_url('/auth/outage/update.php', ['id' => $outage->id, 'sesskey' => sesskey()]); - - $url->param('action', 'edit'); + $url = new moodle_url('/auth/outage/change.php', ['id' => $outage->id]); $img = html_writer::empty_tag('img', ['src' => $OUTPUT->pix_url('t/edit'), 'alt' => get_string('edit'), 'class' => 'iconsmall']); $linkedit = html_writer::link($url, $img, ['title' => get_string('edit')]); - $url->param('action', 'delete'); + $url = new moodle_url('/auth/outage/remove.php', ['id' => $outage->id]); $img = html_writer::empty_tag('img', ['src' => $OUTPUT->pix_url('t/delete'), 'alt' => get_string('delete'), 'class' => 'iconsmall']); $linkdelete = html_writer::link($url, $img, ['title' => get_string('delete')]);