> The new method "when you enenrol someone, you delete all the associated data" is highly dangerous and can easily lead to dataloss.
Agreed, deleting data, without a really good reason to do so and being really sure, is capital B Bad. It follows that where we can achieve some goal either by either a) deleting data or b) keeping the data, while actively using only relevant data, we prefer b), as the gradebook does. Do we agree on this? If so, I'm happily surprised and I must have misread your earlier comment.
> it is only possible because the greadbook happens to implement keeping a complete record of grade histories.
Good for gradebook.
> It is no good trying to delete the quiz attempts when a user is re-enroled
> with "do not recover old grades" selected. At least I assume that is the
> case. There are many different ways that users can be re-enroled, in
> addition to doing it through the Moodle UI (e.g. DB enrolment plugins, web services).
Assume for the moment the just as gradebook does the right thing, choosing not to delete data that doesn't have to be deleted, Quiz could EASILY do the same thing. Let's just assume for the moment that your code was well written enough that having te option to preserve that information would be little more than adjusting two WHERE clauses. No need to delete attempts on unenrol, perhaps, but simply ignore outdated attempts when grading and counting the number of attempts. That's entirely within the control Quiz, which quiz attempts it counts as relevant. Putting aside just for the moment whether or not it actually IS that simple, would Gradebook like behavior of preserving data rather than deleting it be desirable in Quiz?
Based on looking through the code and testing, I believe your code actually IS written so well that to have it do "the right thing" like gradebook iactually is a matter of tweaking the two WHERE clauses. Testing shows it does work with the small patch I submitted. Granted, the patch we're actually using is slightly different due to local customizations, but it appears to show that if we wanted to preserve data like gradebook does, while counting only relevant data (from this enrollment), it's simple to do so.