Where are "random" type questions stored in the database in Moodle 4.x?

Where are "random" type questions stored in the database in Moodle 4.x?

by Nicholas Stefanski -
Number of replies: 6

We recently upgraded from Moodle 3.11 to 4.1, so I'm still getting used to the differences. In 3.x, when we created a random question, it created a new record on the `questions` table where the column `qtype` set to "random." I can still see all the random questions that we created prior to the update on this table. However, I noticed that now, when I create a random question, there's not a new record on the `questions` table. So, where is this being stored now?

Average of ratings: -
In reply to Nicholas Stefanski

Re: Where are "random" type questions stored in the database in Moodle 4.x?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Actually, in Moodle 3.11, the real information about random questions was stored in the quiz_slots table. The row with qtype set to random in the questions table was a vestigial legacy.

In Moodle 4.x, the key information has moved into to question_set_references table. You are looking for rows there with questionarea = 'quiz_slot' and itemid as the quiz_slot.id. The key information is then in the filtercondition column. (Why is this a blob of JSON? because in the future, the flexibility to make random selections is going to increase a lot - we hope.)
Average of ratings:Useful (3)
In reply to Tim Hunt

Re: Where are "random" type questions stored in the database in Moodle 4.x?

by Nicholas Stefanski -
Tim, thank you! They actually have questionarea = 'slot' and that gets me the info I need. The changes to the quiz engine in 4.x are great, I'm looking forward to see what comes next!
Average of ratings:Useful (2)
In reply to Tim Hunt

Re: Where are "random" type questions stored in the database in Moodle 4.x?

by Claudio Bartoloni -

I have several questions in my moodle installation and I would like to know which ones are actually used in the various quizzes defined in the various courses

how can I write a query that allows me to find the questions that are used in the quizzes also taking into account the quizzes that use random questions?

Thank you for your support.

In reply to Claudio Bartoloni

Re: Where are "random" type questions stored in the database in Moodle 4.x?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
That is a non-trivial calculation.

The information you need is in the question_set_references table. The data there is in two halves:

(usingcontextid, component, questionarea, itemid) says where this set of questions is used. You need onces where component is 'mod_quiz' and questionarea is 'quiz_slot'. Then itemid is the quiz_slots.id.

Then, (questionscontextid, filtercondition) tell you which questions are being randomly selected from here. filtercondition is a JSON representation of a possibly complex search. At the moment that can only involves question_categories and tags, but in future, more conditions will be possible.
In reply to Tim Hunt

Re: Where are "random" type questions stored in the database in Moodle 4.x?

by Claudio Bartoloni -
To be more explicit I would like to know which questions are not used in any quiz and can be removed from Moodle.
If I understand your suggestion, I need to look into the question_set_reference table to find the questions used in a quiz (randomly selected or not). Questions not referenced in this table are not used in any quiz.
It's correct?
Thank you for taking care of my post so quickly and on a Sunday too
In reply to Claudio Bartoloni

Re: Where are "random" type questions stored in the database in Moodle 4.x?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Well, you still need to break apart filtercondition, but if you don't need 100% accuracy, then yes, computing an approximate answer is easier.

Also, there is another approach. When a student attempts a quiz, you get a record in the question_attempts table, pointing to the question that is acutally used. So, that is worth checking too. (Useful query here to get you started https://docs.moodle.org/dev/Overview_of_the_Moodle_question_engine#Detailed_data_about_an_attempt)
Average of ratings:Useful (1)