Does anyone know how I can execute logic after a user completes a quiz attempt? I have tried using the following, but does not work in the attempt.php :
if (attemptobj->is_finished()){
execute this code...
}
Why not use the Moodle events system, and subscribe to the quiz attempt_submitted event?
Thank you Tim, that's exactly the guidance I needed.
For any others wanting to know what I did, look for the event listener name '\mod_quiz\event\attempt_submitted' located in (mod/quiz/db/events.php). Then refer to the locallib.php and search for the callback function and simply insert your logic there; your logic gets executed after the submission of the quiz because after all that's when the event is triggered. Enjoy!
Tim, would this work for a completion based on a scormed course or would you go a different route? I have an external table managing histories that I want to update every time a scormed module is completed.
What you did is not quite right.
You have hacked the quiz's own event observer. (It is slightly confusing that the quiz observes itself.)
You should make your own observer, more like the way that the quiz listens to the group events.
As far as I can see, the SCORM module does not have an attempt_complete event, which seems like an odd omission, but you would need to ask about that in the SCORM forum.
Hi Tim,
I have been trying to make another observer doing exactly that (in the form of a local plugin):
In my local plugin ("percentiles") db/events.php file, I have:
$observers = array(
array(
'eventname' => '\mod_quiz\event\attempt_submitted',
'includefile' => '/local/percentiles/locallib.php',
'callback' => 'percentiles_quiz_attempt_submitted_handler',
)
);
In the locallib.php file, I have function percentiles_quiz_attempt_submitted_handler($event)
And in Moodle, it says "Quiz Attempt Submitted" is being observer by local_percentiles.
However, none of the code in the includefile or callback in my local plugin is executed.
No php error messages or logs. Cache has been purged and version updated.
Am I forgetting or doing something wrong here?
Thanks.
What you are doing looks right. At least I cannot see anything obviously wrong.
The problem is not quiz specific. I suggest you post in the general developer forums.
I know this is old but...
I had the same 'error' and got some understanding...
We all tend to test our code with all the capabilities of an administrator, but in this case it backfires on us...
If one solves the quiz not as the student of the course it will be a 'preview attempt' and will not fire none of the events... One must made the attempt as student and then events fill flow in,,,