SQL query to quickly build vocabulary quizzes

SQL query to quickly build vocabulary quizzes

by Erin Billy -
Number of replies: 5

First, thank you, Tim for all the work you've put into this quiz module. I use the quiz more than I do any other part of Moodle.

I'm getting up to speed with things and trying to learn a bit of PHP and MySQL and the like to facilitate what I want to accomplish. My big question (for now) is this:

How can I query the database very quickly for questions (database field mdl_question) that have a certain name and return the id of that question? I know that this must be pretty easy to search for one question, but I need to do this for a lot of words at once (e.g., 50 word) and return all id's. I would then take the question id's and plug them manually into a quiz.

For example, I would love to do this:

  1. I need a vocab quiz consisting of 50 words. For example:

unshorn
racking
stowing
fray
surging
vaulting
pawnbroker
warily
ravishingly
hideous

I would like to have a query to find all the mdl_questions for those vocab questions. (The name of the mdl_question is the question I seek. For example, the vocab question for unshorn has the name "unshorn".) The query would then return the id of those questions. For example, 61,2217,1421,1214,217 and so on. I would then plug in those ids into the quiz. Of course, if there's an easier way, I'd love to know.

I realize that this may be fairly complicated (well, at least for me), but if you could point me in the right direction, I'd greatly appreciate it. Do I need to make a form? Or can I just use a query?

Thank you very much for everything. smile

Average of ratings: -
In reply to Erin Billy

Re: SQL query to quickly build vocabulary quizzes

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

I think you need the Moodle function get_records_list, so something like:

$quiz = get_record('quiz', $quizid);
$names = array('unshorn', 'racking', ...);
$questions = get_records_list('question', 'name', $names);
foreach ($questions as $questionid => $question) {
quiz_add_quiz_question($questionid, $quiz);
}

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

Re: SQL query to quickly build vocabulary quizzes

by Erin Billy -

Yay! I'm going to try that very, very soon. THANK YOU!

In reply to Erin Billy

Re: SQL query to quickly build vocabulary quizzes

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

I cannot promise that code is actually correct. It is along the right lines, but probably buggy. Should get you started though.

In reply to Tim Hunt

Re: SQL query to quickly build vocabulary quizzes

by Erin Billy -

Hi, Tim. No worries.

So... I have a noobie question. Um... Can I run this in PHPMyAdmin? In PuTTY? (I've been using PuTTY and PHPMyAdmin to run my SQL queries.) Or should I use something in Moodle? I've been poking around, and it seems you've created some pretty nifty tools for queries and whatnot.

*Goes off to look at the functions in lib/dmllib.php...*

In reply to Erin Billy

Re: SQL query to quickly build vocabulary quizzes

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

Sorry, I didn't explain. You would have to wrap those lines of code up into some sort of complete PHP script, then execute that script. So, if you are not used to PHP development, you would have to learn a bit.

Or, work out exactly what SQL those Moodle functions do, so you could execute the same commands manually via PHPMyAdmin or something.