mirror of
https://github.com/catalyst/moodle-auth_outage.git
synced 2026-05-17 05:48:43 +02:00
Editing outage entry implemented.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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');
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user