Execute Logic After Quiz Attempt

Execute Logic After Quiz Attempt

by Sal Hernandez -
Number of replies: 8

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...

}

Average of ratings: -
In reply to Sal Hernandez

Re: Execute Logic After Quiz Attempt

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Why not use the Moodle events system, and subscribe to the quiz attempt_submitted event?

https://docs.moodle.org/dev/Event_2#Event_observers

In reply to Tim Hunt

Re: Execute Logic After Quiz Attempt

by Sal Hernandez -

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! 

In reply to Tim Hunt

Re: Execute Logic After Quiz Attempt

by Sal Hernandez -

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.

In reply to Sal Hernandez

Re: Execute Logic After Quiz Attempt

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

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.

In reply to Tim Hunt

Re: Execute Logic After Quiz Attempt

by Michael Ko -

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.

Attachment AttemptSubmitted.png
In reply to Michael Ko

Re: Execute Logic After Quiz Attempt

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

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.

In reply to Tim Hunt

Re: Execute Logic After Quiz Attempt

by Peter Eliyahu Kornfeld -

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,,,

In reply to Sal Hernandez

Re: Execute Logic After Quiz Attempt

by Angie Mariño -

Hi Sal!
I'm trying to develop something similar to yours . I want that when a user completes a quiz attemp, moodle display a message "hello world" . I copy my code because I could not do it. Thank you very much for your help.


function quiz_attempt_submitted_handler($event) {
global $DB;
$course = $DB->get_record('course', array('id' => $event->courseid));
$attempt = $event->get_record_snapshot('quiz_attempts', $event->objectid);
$quiz = $event->get_record_snapshot('quiz', $attempt->quiz);
$cm = get_coursemodule_from_id('quiz', $event->get_context()->instanceid, $event->courseid);

if (!($course && $quiz && $cm && $attempt)) {
// Something has been deleted since the event was raised. Therefore, the
// event is no longer relevant.
return true;
}
return quiz_send_notification_messages($course, $quiz, $attempt,
context_module::instance($cm->id), $cm);


if (attemptobj->is_finished()){
echo "Hello world";
}
}