Quiz access rule - prevent new attempt

Quiz access rule - prevent new attempt

by Jose Velazquez-Torres -
Number of replies: 1

Im trying to do add a rule that limit the studet to only have one quiz open at the time. I currently have this code:


It's surely bloquing the student to open the second quiz but when I close the other quiz and go back to try open the quiz that was bloqued it remains bloqued eventhough the information on the mdl_quiz_attempts table has already been updated. Anyone know why this could be happening? I seems to my that in the prevent new attempt function is not running each time I try to access the quiz. 

Average of ratings: -
In reply to Jose Velazquez-Torres

Re: Quiz access rule - prevent new 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
It is very inefficient to load all the student's quiz attempts from the database, and search though them in a for loop in PHP. Databases are good at doing searches. Use that.

You probably want something like:
if ($DB->record_exists_select('quiz_attempts', 'userid = ? AND (state = ? OR state = ?)',
[$USER->id, quiz_attempt::IN_PROGRESS, quiz_attempt::OVERDUE]) {
// User has an active attempt somewhere.
}

Alos, core\notification is not great for debugging. print_object is better for that.