Basic form validation for create/edit outage. Checks if starttime is in past, stoptime is after starttime, warningduration is not zero and title size. Issue #7.

This commit is contained in:
Daniel Thee Roperto
2016-09-05 15:01:22 +10:00
parent 6055f52d64
commit 98de8a6c69
6 changed files with 51 additions and 24 deletions

View File

@@ -31,6 +31,8 @@ require_once($CFG->libdir . '/formslib.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class edit extends \moodleform {
const TITLE_MAX_CHARS = 100;
/**
* {@inheritDoc}
* @see moodleform::definition()
@@ -47,7 +49,12 @@ class edit extends \moodleform {
$mform->addElement('duration', 'warningduration', get_string('warningduration', 'auth_outage'));
$mform->addElement('text', 'title', get_string('title', 'auth_outage'));
$mform->addElement(
'text',
'title',
get_string('title', 'auth_outage'),
'maxlength="'.self::TITLE_MAX_CHARS.'"'
);
$mform->setType('title', PARAM_TEXT);
$mform->addElement('editor', 'description', get_string('description', 'auth_outage'));
@@ -65,6 +72,24 @@ class edit extends \moodleform {
public function validation($data, $files) {
$errors = parent::validation($data, $files);
if ($data['starttime'] <= time()) {
$errors['starttime'] = get_string('starttimeerrornotinfuture', 'auth_outage');
}
if ($data['stoptime'] <= $data['starttime']) {
$errors['stoptime'] = get_string('stoptimeerrornotafterstart', 'auth_outage');
}
if ($data['warningduration'] <= 0) {
$errors['warningduration'] = get_string('warningdurationerrorinvalid', 'auth_outage');
}
$titlelen = strlen(trim($data['title']));
if ($titlelen == 0) {
$errors['title'] = get_string('titleerrorinvalid', 'auth_outage');
}
if ($titlelen > self::TITLE_MAX_CHARS) {
$errors['title'] = get_string('titleerrortoolong', 'auth_outage', self::TITLE_MAX_CHARS);
}
return $errors;
}

View File

@@ -98,7 +98,7 @@ final class outagedb {
['objectid' => $outage->id, 'other' => (array)$outage]
)->trigger();
} else {
// Not new, clean up the class (remove creator field) then update.
// Remove the createdby field so it does not get updated.
unset($outage->createdby);
$DB->update_record('auth_outage', $outage);
// Log it.

View File

@@ -10,7 +10,7 @@
<FIELD NAME="starttime" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="When outage starts."/>
<FIELD NAME="stoptime" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="When outage ends."/>
<FIELD NAME="warningduration" TYPE="int" LENGTH="7" NOTNULL="true" SEQUENCE="false" COMMENT="How many seconds before the outage to display a warning to all users."/>
<FIELD NAME="title" TYPE="char" LENGTH="20" NOTNULL="true" SEQUENCE="false" COMMENT="Title for the outage (short description)."/>
<FIELD NAME="title" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false" COMMENT="Title for the outage (short description)."/>
<FIELD NAME="description" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="More information about the outage."/>
<FIELD NAME="createdby" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Who created this entry."/>
<FIELD NAME="modifiedby" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Who last modified this entry."/>

View File

@@ -24,25 +24,27 @@
*/
$string['auth_outagedescription'] = 'Auxiliary plugin that warns users about a future outage and prevents them from logging in once the outage starts.';
$string['pluginname'] = 'Outage';
$string['menudefaults'] = 'Default Settings';
$string['menumanage'] = 'Manage';
$string['defaultwarningtime'] = 'Default Warning Time';
$string['defaultwarningtimedescription'] = 'Default warning time (in minutes) for outages.';
$string['defaultwarningmessage'] = 'Default Warning Message';
$string['defaultwarningmessagedescription'] = 'Default warning message for outages. Use [from] and [until] placeholders as required.';
$string['defaultwarningmessagevalue'] = 'There is an scheduled maintenance from [from] to [until] and our system will not be available during that time.';
$string['starttime'] = 'Start date and time';
$string['stoptime'] = 'Stop date and time';
$string['warningduration'] = 'Warning duration';
$string['title'] = 'Title';
$string['defaultwarningtime'] = 'Default Warning Time';
$string['defaultwarningtimedescription'] = 'Default warning time (in minutes) for outages.';
$string['description'] = 'Public description';
$string['modifyoutage'] = 'Modify Outage';
$string['removeoutage'] = 'Remove Outage';
$string['removeoutagewarning'] = 'You are about to permanently remove the outage below. This cannot be undone.';
$string['outageslist'] = 'Outages List';
$string['createoutage'] = 'Create Outage';
$string['menudefaults'] = 'Default Settings';
$string['menumanage'] = 'Manage';
$string['modify'] = 'Modify';
$string['modifyoutage'] = 'Modify Outage';
$string['outagecreate'] = 'Create Outage';
$string['outageremove'] = 'Remove Outage';
$string['outageremovewarning'] = 'You are about to permanently remove the outage below. This cannot be undone.';
$string['outageslist'] = 'Outages List';
$string['pluginname'] = 'Outage';
$string['starttimeerrornotinfuture'] = 'Start time must be in the future.';
$string['starttime'] = 'Start date and time';
$string['stoptimeerrornotafterstart'] = 'Stop time must be after start time.';
$string['stoptime'] = 'Stop date and time';
$string['titleerrorinvalid'] = 'Title cannot be left blank.';
$string['titleerrortoolong'] = 'Title cannot have more than {$a} characters.';
$string['title'] = 'Title';
$string['warningdurationerrorinvalid'] = 'Warning duration cannot be zero.';
$string['warningduration'] = 'Warning duration';

View File

@@ -39,8 +39,8 @@ class auth_outage_renderer extends plugin_renderer_base
}
public function renderdeleteconfirmation(outage $outage) {
return $this->rendersubtitle('removeoutage')
. html_writer::tag('p', get_string('removeoutagewarning', 'auth_outage'))
return $this->rendersubtitle('outageremove')
. html_writer::tag('p', get_string('outageremovewarning', 'auth_outage'))
. $this->renderoutage($outage, false);
}
@@ -61,7 +61,7 @@ class auth_outage_renderer extends plugin_renderer_base
$html .= html_writer::empty_tag('br')
. html_writer::link(
$url,
$img . ' ' . get_string('createoutage', 'auth_outage'),
$img . ' ' . get_string('outagecreate', 'auth_outage'),
['title' => get_string('remove')])
. html_writer::empty_tag('br');

View File

@@ -29,7 +29,7 @@ if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
}
$plugin->version = 2016090100; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2016090500; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = $plugin->version; // Same as version
$plugin->requires = 2014051200; // Requires Moodle 2.7 or later.
$plugin->component = "auth_outage";