Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-18535:

Adding checks for field name mismatches and "proper" error messages.

Merged from STABLE19
  • Loading branch information...
commit f24222e2a21dd8a0904d13d3ab6f8da96c609bec 1 parent aa907a9
thepurpleblob authored
Showing with 15 additions and 1 deletion.
  1. +15 −1 mod/data/import.php
View
16 mod/data/import.php
@@ -102,10 +102,23 @@
$recordsadded = 0;
if (!$records = data_get_records_csv($filename, $fielddelimiter, $fieldenclosure)) {
- print_error('get_records_csv failed to read data from the uploaded file. Please check file for field name typos and formatting errors.');
+ print_error('csvfailed','data',"{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}");
} else {
$fieldnames = array_shift($records);
+ // check the fieldnames are valid
+ $fields = $DB->get_records('data_fields', array('dataid'=>$data->id), '', 'name, id, type');
+ $errorfield = '';
+ foreach ($fieldnames as $name) {
+ if (!isset($fields[$name])) {
+ $errorfield .= "'$name' ";
+ }
+ }
+
+ if (!empty($errorfield)) {
+ print_error('fieldnotmatched','data',"{$CFG->wwwroot}/mod/data/edit.php?d={$data->id}",$errorfield);
+ }
+
foreach ($records as $record) {
if ($recordid = data_add_record($data, 0)) { // add instance to data_record
$fields = $DB->get_records('data_fields', array('dataid'=>$data->id), '', 'name, id, type');
@@ -195,6 +208,7 @@ function my_file_get_contents($filename, $use_include_path = 0) {
function data_get_records_csv($filename, $fielddelimiter=',', $fieldenclosure="\n") {
global $DB;
+
if (empty($fielddelimiter)) {
$fielddelimiter = ',';
}
Please sign in to comment.
Something went wrong with that request. Please try again.