How to get quiz id in the URL from database?

How to get quiz id in the URL from database?

by wz z -
Number of replies: 5

For example, there is a quiz activity in a course, the URL of the quiz is https://example.com/mod/quiz/view.php?id=3. When I run SELECT id, name FROM mdl_quiz in the database, the id is 1, not 3 in the URL query parameter. So I don't know how to get the quiz id showing in the URL from database.  Thank you guys!

Average of ratings: -
In reply to wz z

Re: How to get quiz id in the URL from database?

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Yes, it is confusing. What you are seeing in the URL bar is the course_module id, (sometimes referred to as cm or cmid. This is a record in the course_modules table which will contain a instance that points to the actual quiz id. You may get an idea of how the relationship works from this diagram.
https://examulator.com/er/3.0/components/course.html.
If you cannot get your head around that I will create a sql query to illustrate.
In reply to wz z

Re: How to get quiz id in the URL from database?

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

In the database schema, there are two key tables you need to consider, not just one.

  • The table specific to the activity, like quiz, forum, assignment, etc.
  • The table that holds the settings that all activities share, whic is called course_modules.

So, the id in the URL matches course_modules.id.

Then, in course_modules, the module column points to the 'modules' table which lets you look up what type of activity it is, and then the 'instance' column holds the quiz id (if this is a quiz).

Average of ratings: Useful (1)
In reply to Tim Hunt

Re: How to get quiz id in the URL from database?

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
I think Tim is confirming what I wrote, but if there is a difference, assume Tim is correct.
In reply to Marcus Green

Re: How to get quiz id in the URL from database?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
No, just we were both writing our replies at the same time. Had not seen yours until now.
In reply to Tim Hunt

Re: How to get quiz id in the URL from database?

by wz z -
@Tim, thank you verrry much!