Question creation of via api

Question creation of via api

by Luis Sola Ruiz -
Number of replies: 7

Hello everyone. 

Currently I am creating a block of moodle which creates a quiz automatically. After this I would like to add a new question (for example essay), not from the bank. 

is there an API to solve this? I have been reading https://docs.moodle.org/dev/Question_types#Question_type_plug_in_development but I don't fully understand it. 

In the case of the quiz it was to fill the information of the quiz and then call the function add_module(). Simple smile

Thank!

Average of ratings: -
In reply to Luis Sola Ruiz

Re: Question creation of via api

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 API is a bit weird, but it is

question_bank::get_qtype($qtype)->save_question($question, $fromform);

$question needs to look like

        $question = new stdClass();
        $question->category  = $fromform->category;
        $question->qtype     = $qtype;
        $question->createdby = $USER->id;

 and $fromform needs to be the kind of data object you get when edit_$qtype_form is submitted - and you can normally see that by looking in the test code for each question type, e.g. https://github.com/moodle/moodle/blob/master/question/type/essay/tests/helper.php#L79

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

Re: Question creation of via api

by Luis Sola Ruiz -
Thank Tim for your helpful answer. I tried the example you gave me and it worked perfectly I could add a default essay question.

I also tried it with a gapselect question but a error appeared referring at the class "test_question_maker:" https://github.com/moodle/moodle/blob/master/question/type/gapselect/tests/helper.php#L76 with the error: exception - Class 'test_question_maker' not found

I tried to add the file but no success
In reply to Luis Sola Ruiz

Re: Question creation of via api

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 almost certainly should not be using test_question_maker in real code. It is only for tests. I only pointed you there so that you could see the type of data you need to call save_question for a particular question type.

The error looks like a missing require-once.
In reply to Tim Hunt

Re: Question creation of via api

by Luis Sola Ruiz -
Thanks Tim, so, should I recreate the callings to the different functions inside test_question_maker or which way would be the best?
In reply to Tim Hunt

Re: Question creation of via api

by Luis Sola Ruiz -
Hi Tim, I had another question related to this in some way.

Since I have my questions created properly, now I want to give the option to the user of select one category and add some questions automatically. Trying to replicate the Moodle process, I saw this function https://github.com/moodle/moodle/blob/master/question/classes/bank/search/category_condition.php#L132 which lists the categories but in some points of the use of this code it appears that this function is deprecated.
Do you know some way to do this?

Thank you.
In reply to Tim Hunt

Re: Question creation of via api

by Amir karim -

hello my friend ,

i making quiz by using api , but when i want to add question to the quiz ,  it seems everything works fine but there is no button to add some question , whereas when i make quiz manually this is differenet,

please look at the pictures :

in normal case :
normal case

in when i make quiz via api , i got this:

quiz api
so i can not add questions. do you know how can solve it...

thank you for your helping
In reply to Amir karim

Re: Question creation of via api

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 idea. Which API are you using to create the quiz?

Anyway, what I would do to debug this is: Create a quiz each way, and then compare data in the database, to see what is different.