--- grade/import/csv/index.php.orig	2011-09-23 09:30:29.000000000 -0400
+++ grade/import/csv/index.php	2011-09-23 16:46:25.000000000 -0400
@@ -257,40 +259,38 @@
                     $t1 = '';
                 }
 
+                $user_fields = array(
+                    'userid' => array(
+                        'field' => 'id',
+                        'label' => 'id',
+                    ),
+                    'useridnumber' => array(
+                        'field' => 'idnumber',
+                        'label' => 'idnumber',
+                    ),
+                    'useremail' => array(
+                        'field' => 'email',
+                        'label' => 'email address',
+                    ),
+                    'username' => array(
+                        'field' => 'username',
+                        'label' => 'username',
+                    ),
+                );
+
                 switch ($t0) {
-                    case 'userid': //
-                        if (!$user = get_record('user','id', addslashes($value))) {
-                            // user not found, abort whold import
-                            import_cleanup($importcode);
-                            notify("user mapping error, could not find user with id \"$value\"");
-                            $status = false;
-                            break 3;
-                        }
-                        $studentid = $value;
-                    break;
+                    case 'userid':
                     case 'useridnumber':
-                        if (!$user = get_record('user', 'idnumber', addslashes($value))) {
-                             // user not found, abort whold import
-                            import_cleanup($importcode);
-                            notify("user mapping error, could not find user with idnumber \"$value\"");
-                            $status = false;
-                            break 3;
-                        }
-                        $studentid = $user->id;
-                    break;
                     case 'useremail':
-                        if (!$user = get_record('user', 'email', addslashes($value))) {
-                            import_cleanup($importcode);
-                            notify("user mapping error, could not find user with email address \"$value\"");
-                            $status = false;
-                            break 3;
-                        }
-                        $studentid = $user->id;
-                    break;
                     case 'username':
-                        if (!$user = get_record('user', 'username', addslashes($value))) {
+                        // Skip invalid row with blank user field
+                        if ($value === '') {
+                            continue 3;
+                        }
+                        if (!$user = get_record('user', $user_fields[$t0]['field'], addslashes($value))) {
+                            // user not found, abort whole import
                             import_cleanup($importcode);
-                            notify("user mapping error, could not find user with username \"$value\"");
+                            notify("user mapping error, could not find user with {$user_fields[$t0]['label']} \"$value\"");
                             $status = false;
                             break 3;
                         }
@@ -479,7 +483,12 @@
 
         /// at this stage if things are all ok, we commit the changes from temp table
         if ($status) {
-            grade_import_commit($course->id, $importcode);
+            ob_start();
+            $commit_success = grade_import_commit($course->id, $importcode);
+            if (!$commit_success) {
+                error('Grade Import failed during commit. Details:<br />' . ob_get_clean());
+            }
+            ob_end_flush();
         }
         // temporary file can go now
         fclose($fp);
--- grade/import/lib.php.orig	2011-09-23 09:30:19.000000000 -0400
+++ grade/import/lib.php	2011-09-23 13:57:42.000000000 -0400
@@ -46,6 +46,7 @@
 function grade_import_commit($courseid, $importcode, $importfeedback=true, $verbose=true) {
     global $CFG, $USER;
 
+    $failed = false;
     $commitstart = time(); // start time in case we need to roll back
     $newitemids = array(); // array to hold new grade_item ids from grade_import_newitem table, mapping array
 
@@ -58,7 +59,6 @@
         // instances of the new grade_items created, cached
         // in case grade_update fails, so that we can remove them
         $instances = array();
-        $failed = false;
         foreach ($newitems as $newitem) {
             // get all grades with this item
 
@@ -112,19 +112,19 @@
                         $grade->feedback = false; // ignore it
                     }
                     if (!$gradeitem->update_final_grade($grade->userid, $grade->finalgrade, 'import', $grade->feedback)) {
-                        $failed = 1;
-                        break 2;
+                        echo "Error setting grade for \"{$gradeitem->itemname}\" for userid {$grade->userid}<br />" . PHP_EOL;
+                        $failed = true;
                     }
                 }
                 //$itemdetails -> idnumber = $gradeitem->idnumber;
                 $modifieditems[] = $itemid;
 
             }
+        }
 
-            if (!empty($failed)) {
-                import_cleanup($importcode);
-                return false;
-            }
+        if ($failed) {
+            import_cleanup($importcode);
+            return false;
         }
     }
 
