How to retrieve a Quiz and included questions?

How to retrieve a Quiz and included questions?

by Jackie Smile -
Number of replies: 13

I searched the forum and also went through the API documentation. So far I'm a bit clueless which path I should take in order to retrieve a quiz and its containing questions. And if it is correct in any case as well. I'm using Moodle 2.2

What I'm trying to achieve is, to use an external software which can present a list of available quizzes. Those quizzes and their questions need to be retrieved by the software and processed afterwards to be used in a proprietary system.

As far as I researched I could either create a webservice/plugin that uses some specific function to retrieve the quiz data. But so far just the most vital core functions are implemented. I could retrieve the name of the quiz with "core_course_get_contents" but how to proceed from there?

Another option might be to use ODBC and query the data directly from the correct tables. I recognised that the table mdl_quiz has the column questions that contains "1,0,2,0" in my case. I guess 1 and 2 are the ids of the two questions that are in my quiz, but the zeros?
Could this option be a possibility?

As I said I'm a bit stuck here and would really appreciate if someone knows an easy solution for this matter. May be I overlooked something?

 

Cheers

jackie

Average of ratings: -
In reply to Jackie Smile

Re: How to retrieve a Quiz and included questions?

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

You are right that it is not well documented at the moment, and there are no web services for this yet.

You don't really give the scope of what you are trying to do. Which question types does your system use? Any of them, or just multiple-choice? Are your quizzes fixed, or do you use random questions? What about internal randomisation withing questions, like the shuffling of the choices in a multiple choice question?

In reply to Tim Hunt

Re: How to retrieve a Quiz and included questions?

by Jackie Smile -

OK, let me elaborate a bit.

I currently work in a university and they have this software that is used on mobile devices. At the moment it supports multiple choice and multiple response questions. Further development aims to support more question types but right now it is fine with the existing. Right now no shuffle whatsoever is planned. The mobile software communicates with a server where the progress can be monitored by the teacher. That server software also serves the quizzes for the mobile clients. Within that server software I'd also like to be able to select existing quizzes from Moodle and import them so they can be used in this scenario as well. The next question would be about importing the answers along with grades, but I think that will be too much for now.
I think if I could get the quizzes in one way or another would already be a big step for me.

This is a prototype and its use case lies not in the software itself but in what the students are able to learn. Therefore I can't just use Moodle Mobile or other existing software.

 

Thanks
Jackie

In reply to Jackie Smile

Re: How to retrieve a Quiz and included questions?

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

The limited scope makes it quite do-able, but you will have to write some code on the Moode end.

The definition for a single MC question is stored in a combination of the mdl_question, mdl_question_multichoice and mdl_question_answers tables. (One row in the first two tables there, and then one row per choice in the question_answers.)

Then, as you spotted, mdl_quiz.questions is the list of questions in the quiz. The 0s in there mark the page-breaks. This data is also in mdl_quiz_question_instances.

http://docs.moodle.org/dev/Quiz_database_structure may help.

http://docs.moodle.org/dev/Question_database_structure is out-of-date, but the question_bank half of the diagram is still relevant. 

In reply to Tim Hunt

Re: How to retrieve a Quiz and included questions?

by Jackie Smile -

OK, that sounds not bad so far. What exactly to you mean with coding on the Moddle side? Would that be some sort of plugin that communicates with both the database and my software? If so I wont need ODBC, right?

Jackie

In reply to Jackie Smile

Re: How to retrieve a Quiz and included questions?

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 could do it all with ODBC, if you want.

The alternative is to write a PHP script in Moodle (basically some sort of web service) which can use all the library functions that exist to help load the right data. If you know your way round the Moodle code, that would be easier. If you are not familiar with Moodle code, ODBC will probably be easier.

In reply to Tim Hunt

Re: How to retrieve a Quiz and included questions?

by Jackie Smile -

Are those the library functions you meant?
http://xref.moodle.org/nav.html?_functions/index.html

I think I rather do it with the php script than using ODBC. That way I don't have to rely on a specific database layout that might change. In terms of skill I guess I'm better with PHP than with SQL, but Moodle... smile 

Thank you very much for pointing me into the right direction.

In reply to Jackie Smile

Re: How to retrieve a Quiz and included questions?

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, to point you more specifically in the right direction, I suggest you look at mod/quiz/startattempt.php.

Moodle has a nice utility function print_object (which is basically just a wrapper around print_r to format the output prettily). So, I suggest you add some lines like print_object($quizobj); print_object($queston); die; at the applicable points, then go in through the Moodle UI and try to start a quiz attempt, and see if you can work out what is going on.

(You need the die statement there, because this script normally redirects to mod/quiz/addempt.php when it is finished, so you won't see your output without the die.)

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

Re: How to retrieve a Quiz and included questions?

by Jackie Smile -

Thank you Tim, your explanaitiongs got me going quite far already. However, I'm struggeling with the correct way of defining the return parameters in the externallib.php file of my plugin.

The returned data looks like this:

Array
(
[2] => stdClass Object
(
[fullname] => ADL SCORM Test 1.2
[id] => 2
)
     [1] => stdClass Object
(
[fullname] => Test Full site name
[id] => 1
)
)

 

Return definition looks like this:

return 
new external_multiple_structure(
new external_single_structure(
array(
'fullname' => new external_value(PARAM_TEXT, 'course full name'),
'id' => new external_value(PARAM_INT, 'course id'),
)
)
);

 

Right now I'm casting the Objects in that Array into Arrays as well. It was just a try and is probably not very fault tolerant. Is there a way to return the data as is?

Cheers
Matthias

In reply to Jackie Smile

Re: How to retrieve a Quiz and included questions?

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 to ask about this in the web services forum.

In reply to Tim Hunt

Re: How to retrieve a Quiz and included questions?

by Jackie Smile -

Alright, sorry smile

I actually refined my code a bit so it shold work that way. Gonna ask anyway.

Thank you for helping me! 

In reply to Tim Hunt

quiz web services functions available

by Harry Bleckert -

I created web services functions to retrieve full quiz, quiz student grade and full response data.
Available as a local web services plugin. custom development, extends Moodle external API, with full API documentation.

In reply to Harry Bleckert

Re: quiz related web services functions

by Harry Bleckert -

This plugin has not been contributed yet.
I may contribute around mid 2013, but not now.

Meanwhile, I am available for development jobs with focus on extending Web Services.

In reply to Harry Bleckert

Re: quiz related web services functions

by Flo Jungwirth -

Hello,

Is the mentioned plugin available now?

Thanks in advance, Florian