From d25043270cf5e7654cd74d564e22d6fda495f5e4 Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Mon, 14 Sep 2015 10:03:11 +0800 Subject: [PATCH 1/2] MDL-51108 mod_data: Add validation for Lat. and Long. Added a check to see that both the Lat. and Long. values are entered when filling in database module entries. --- mod/data/field/latlong/field.class.php | 22 ++++++++++++++++++++++ mod/data/lang/en/data.php | 1 + mod/data/lib.php | 11 ++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/mod/data/field/latlong/field.class.php b/mod/data/field/latlong/field.class.php index 0bb0221c4c7..a72ebe172ce 100644 --- a/mod/data/field/latlong/field.class.php +++ b/mod/data/field/latlong/field.class.php @@ -253,4 +253,26 @@ class data_field_latlong extends data_field_base { return isset($value) && !($value == ''); } + /** + * Validate values for this field. + * Both the Latitude and the Longitude fields need to be filled in. + * + * @param array $values The entered values for the lat. and long. + * @return string|bool Error message or false. + */ + public function field_validation($values) { + $valuecount = 0; + // The lat long class has two values that need to be checked. + foreach ($values as $value) { + if (isset($value->value) && !($value->value == '')) { + $valuecount++; + } + } + // If we have nothing filled in or both filled in then everything is okay. + if ($valuecount == 0 || $valuecount == 2) { + return false; + } + // If we get here then only one field has been filled in. + return get_string('latlongboth', 'data'); + } } diff --git a/mod/data/lang/en/data.php b/mod/data/lang/en/data.php index cf3f7dccd6f..ef81fa256ec 100644 --- a/mod/data/lang/en/data.php +++ b/mod/data/lang/en/data.php @@ -208,6 +208,7 @@ $string['invalidurl'] = 'The URL you just entered is not valid'; $string['jstemplate'] = 'Javascript template'; $string['latitude'] = 'Latitude'; $string['latlong'] = 'Latitude/longitude'; +$string['latlongboth'] = 'Both the Latitude and the Longitude must be filled in.'; $string['latlongdownloadallhint'] = 'Download link for all entries as KML'; $string['latlongkmllabelling'] = 'How to label items in KML files (Google Earth)'; $string['latlonglinkservicesdisplayed'] = 'Link-out services to display'; diff --git a/mod/data/lib.php b/mod/data/lib.php index d6641073566..6fd220ea0c3 100644 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -3838,6 +3838,7 @@ function data_process_submission(stdClass $mod, $fields, stdClass $datarecord) { // Empty form checking - you can't submit an empty form. $emptyform = true; $requiredfieldsfilled = true; + $fieldsvalidated = true; // Store the notifications. $result->generalnotifications = array(); @@ -3875,6 +3876,14 @@ function data_process_submission(stdClass $mod, $fields, stdClass $datarecord) { $field = data_get_field($fieldrecord, $mod); if (isset($submitteddata[$fieldrecord->id])) { + // Field validation check. + if (method_exists($field, 'field_validation')) { + $errormessage = $field->field_validation($submitteddata[$fieldrecord->id]); + if ($errormessage) { + $result->fieldnotifications[$field->field->name][] = $errormessage; + $fieldsvalidated = false; + } + } foreach ($submitteddata[$fieldrecord->id] as $fieldname => $value) { if ($field->notemptyfield($value->value, $value->fieldname)) { // The field has content and the form is not empty. @@ -3906,7 +3915,7 @@ function data_process_submission(stdClass $mod, $fields, stdClass $datarecord) { $result->generalnotifications[] = get_string('emptyaddform', 'data'); } - $result->validated = $requiredfieldsfilled && !$emptyform; + $result->validated = $requiredfieldsfilled && !$emptyform && $fieldsvalidated; return $result; } From 453adb1adfa21fba96d7272bb853702f4aea3af1 Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Mon, 14 Sep 2015 14:13:35 +0800 Subject: [PATCH 2/2] MDL-51108 mod_data: Addition to behat test for Lat. and Long. --- mod/data/tests/behat/required_entries.feature | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/mod/data/tests/behat/required_entries.feature b/mod/data/tests/behat/required_entries.feature index 0ddad876b05..d5ef8e548a5 100644 --- a/mod/data/tests/behat/required_entries.feature +++ b/mod/data/tests/behat/required_entries.feature @@ -204,3 +204,24 @@ Feature: Users can be required to specify certain fields when adding entries to | Required URL | http://example.com/ | | Required Multimenu | Option 1 | | Required Two-Option Multimenu | Option 1 | + + Scenario: A student fills in Latitude but not Longitude will see an error + Given I log in as "student1" + And I follow "Course 1" + When I add an entry to "Test database name" database with: + | Base Text input | Some input to allow us to submit the otherwise empty form | + | Required Checkbox Option 1 | 1 | + | RTOC Option 1 | 1 | + | Latitude | 24 | + | Required Menu | 1 | + | Required Number | 1 | + | Required Radio Option 1 | 1 | + | Required Text input | New entry text | + | Required Text area | More text | + | Required URL | http://example.com/ | + | Required Multimenu | 1 | + | Required Two-Option Multimenu | 1 | + And I set the field with xpath "//div[@title='Not required Latlong']//tr[td/label[normalize-space(.)='Latitude']]/td/input" to "20" + And I press "Save and view" + Then ".alert.alert-error" "css_element" should exist in the "Required Latlong" "table_row" + And ".alert.alert-error" "css_element" should exist in the "Not required Latlong" "table_row"