I am only a user and I would not know how to code this. Maybe a workaround would be enough, depending on your needs:
- Click on the quiz
- Click on 'Attempts: xx'
- 'Download table data as': Microsoft Excel (.xlsx)
- In Excel, add two columns, say M and N, with typical cell formulas:
- Cell M2: =IF(A2=A1;J2+M1;J2)
- Cell N2: =IF(A2=A3;"";A2)
where column A contains the name of the student, or you could use column E with the email address, and column J contains the grade for each attempt. You could then import these results back into Moodle.