if (empty($g['userid']) or ($k != $g['userid'])) { debugging('Incorrect grade array index, must be user id! Grade ignored.');
Add this code before the code above:
if (!empty($g['userid']) and ($k != $g['userid'])) { debugging('Reindexing!'); if (array_key_exists($g['userid'], $grades)) { debugging('Entry in $grades exists for uid ' . $g['userid']); } else { $grades[$g['userid']] = $g; } }Once you've done that, then go to the gradebook and select Categories and items from the drop-down menu:
Then, click the Synchronize legacy grades button: