]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-18535:
authorthepurpleblob <thepurpleblob>
Thu, 12 Mar 2009 14:45:13 +0000 (14:45 +0000)
committerthepurpleblob <thepurpleblob>
Thu, 12 Mar 2009 14:45:13 +0000 (14:45 +0000)
Adding checks for field name mismatches and "proper" error messages.

Merged from STABLE19

mod/data/import.php

index 26a11f959c9154a7ab7ad7df62d6259f9e6e75af..5ebe3408d00f1b70c8eecaa9a3b5aeef495de1c3 100755 (executable)
         $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 = ',';
     }