Editing outage entry implemented.

This commit is contained in:
Daniel Thee Roperto
2016-08-31 16:19:50 +10:00
parent 1d7a042e65
commit 9e1b5e8315
6 changed files with 104 additions and 8 deletions

61
change.php Normal file
View File

@@ -0,0 +1,61 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Create new outage.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @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();

View File

@@ -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;
}
}

View File

@@ -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');

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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')]);