I'm working in a quiz report to allow teachers to speed up the grading of essay questions manually.
Many students just tinker with the template answer, add/delete some chars, and then simply save and go for other other questions. The report identifies question attempts with student responses similar to the question's stored template response, and then tries to set the mark, the grade to 0 (zero).
I say tries because all is working but the grade saving. I have ways to set how similar the responses must be, and to get the relevant question usages and questions attempts from quiz. But set grading in the database seems a mistery,
There is a method in question_attemp class
public function manual_grade($comment, $mark, $commentformat = null, $timestamp = null, $userid = null)
public function manual_grade($slot, $comment, $mark, $commentformat = null)
After doing anything with a question_usage_by_activity class, you need to call the save method to write the change to the database. https://docs.moodle.org/dev/Using_the_question_engine_from_module#Saving_the_usage (This is so that you can make lots of different changes, then have them all written to the database efficiently.)