Developing a plugin "new question type" 3.x

Developing a plugin "new question type" 3.x

by Andrea Custode Quevedo -
Number of replies: 14

Hi to everyone!

I'm new as developer using Moodle, I would like to develop a plugin for my final project university. So I will use Moodle 3.6 at least

I have been researching about https://docs.moodle.org/dev/Question_types and https://docs.moodle.org/dev/Activity_modules

But I'm not sure what is better to use. 

My goal is to develop a plugin which can be use by students by giving them random questions. I want to generate these questions automatically using my own code, my code will need some inputs and it will generate related outputs. The whole complexity of the code is transparent to the student, they will only see a test with questions.

The objective is to show questions in a random test, the student inserts the response (similar to a short-answer) and my plugin executes my code and determines if the response has been right or wrong.

To summarize I need a plugin to:

Generate questions automatically by giving inputs and using programming structures like switch or for to have related outputs.

Randomly the students are shown these questions who insert the response of each question and at the end of the test the students will know all rights and wrong answers.

So to reach my goal, anybody could recommend me if it's better to start by developing a new question-type or an activity module?

And also, anybody could recommend me if it's better to use php language or any other language like javaScript to my code part (I think I will need to use structures like switch, for, while...)

Thanks in advanced.

Andrea.

Average of ratings: -
In reply to Andrea Custode Quevedo

Re: Developing a plugin "new question type" 3.x

by Dominique Palumbo -
Picture of Particularly helpful Moodlers Picture of Plugin developers
Hi,

The best seems to use a question type plugin.
And you'll need both languages (php, javascript).

Read also Moodle dev guides. https://docs.moodle.org/dev/Coding.
And you'll learn a lot by reading and duplicating the code of existing question type.

You can also generate a full "question bank" and use the existing question type random or fork it to make it like you want with your qtype. With a special admin option like generate question bank with x questions from different source file.

Hope it's help.

Dominique.
In reply to Dominique Palumbo

Re: Developing a plugin "new question type" 3.x

by Andrea Custode Quevedo -

Hi Dominique!

Thanks for your answer. I will lead my plugin in the way you recommed me.

One question about "generate a full question bank". Do you mean once I have my code in javaScript I will be able to generate a question bank or you mean in other way?

I thought I need a "code part" in javaScript to generate all questions by using (I think a .js file) or there is another way to create a question bank using javaScript. ¿Do you know it?

Thanks again!

Andrea.

In reply to Andrea Custode Quevedo

Re: Developing a plugin "new question type" 3.x

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

This sounds like it can be achieved without any new code, simply by creating a quiz and adding 3 random questions to the quiz (from a category that has the full list of short answer questions you want to use in it).

The quiz would then select a different set of random questions from the category each time it was attempted.

See: https://docs.moodle.org/en/Building_Quiz#Adding_a_random_question

In reply to Davo Smith

Re: Developing a plugin "new question type" 3.x

by Andrea Custode Quevedo -
Hi Davo,

Thanks for your answer.

What I need is a bit more complex I think. Because I have to give different inputs which follow a logic and obtain related outputs.

For example a NAND gate table, only in case all inputs are true the output will be false (2 inputs and 1 output)

That's why I want to create a .js file (in javaScript) which will execute that logic. At the moment I will research more deeply about create a new question type.

And also https://docs.moodle.org/dev/Coding

I will also check your link https://docs.moodle.org/en/Building_Quiz#Adding_a_random_question
Because I think I will need to learn how to add in a question_bank my generated questions (.js file)

Thanks for your help!

Andrea.
In reply to Andrea Custode Quevedo

Re: Developing a plugin "new question type" 3.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

I think we could advise you better if you explain a bit more about what you are trying to create. What are you doing that makes this new question type interesting? Is it that you are automatically randomly generating questions? Or is it that you have some interesting automatic-grading algorithm? Or is it something else? I see that in a recent message you said something about NAND gates, that helps us understand the kind of thing that you are doing, but please give more information if you can.

Like the other people who replied, I am pretty sure that you want to create a question type. (Then the standard Moodle quiz will handle lots of the details when a student attempts a set of questions.)

Some general advice, when you are thinking about how a new question type should work, the things you need to decide to specify it are:

  1. What will students see when they attempt a question? It is a good idea to make a UI mock-up of this. (Then you get to doing development, this will tell you what code to write in question/type/.../renderer.php.)
  2. When the student has input their answer, how will that get graded, and what will the feedback be? (This will determine the code in question/type/.../question.php).
  3. When a teacher creates a particular instance of your question type to add to the quiz, what settings do they need to input? It is a good idea to make a UI mock-up of the editing form (This determines question/type/.../edit_..._form.php, and then also the database structure.)

Once you have decided these things, most of the rest of the design should be clear.

Since you talk about randomisation, you need to know that there are two different ways that Moodle can have random questions in the quiz.

  • There is the way that Dominique mentioned, where you create several questions in the question bank, and then when a student starts a quiz attempt, one is selected at random by the system.
  • The other method (which is probably what you want here) is to use the 'Question variant' system. To do this, you implement the method get_num_variants() in your question. For example, if your question has 5 different random versions, then this method needs to return 5 for this question. Then, when the system starts your question by calling start_attempt, the $variantno passed in will be between 1 and 5, and you should show that variant of that question. In standard moodle, qtype_calculated is an example that uses this. https://github.com/moodleou/moodle-qtype_varnumericset/blob/master/questionbase.php#L471 is a non-core example, and there are others.

To answer your other question. I think that most of the code you write to implement this question type should be in PHP.

I hope this helps. Please ask more questions in future.

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

Re: Developing a plugin "new question type" 3.x

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
If you do decide to create a new question type you can see a template I maintain here
https://github.com/marcusgreen/moodle-qtype_TEMPLATE
But I suggest plenty of planning and thinking and asking questions before coding
In reply to Marcus Green

Re: Developing a plugin "new question type" 3.x

by Andrea Custode Quevedo -
Hi Marcus.

Thank you for your answer, I will do it!

I'm pretty sure I will have many questions. I would like to start with a good base (clear ideas and planning) before coding.

Andrea.
In reply to Tim Hunt

Re: Developing a plugin "new question type" 3.x

by Andrea Custode Quevedo -
Hi Tim,

Thank you very much for your answer. I hope with all of these answers I could lead my new question-type plugin.

First of all I need to make a UI mock-up as you recommend me. It will be helpful for clarify my ideas and objectives.

I have given an example with NAND gate. However it was only an example. Because there are more gates I would like to develop (some logical circuits: NAND, Multiplexer, Total SUM...).
The truth table of some of these is what I want to develop.

What I have to do is to create a question-type which generates questions randomly (inside a question-bank there will be questions about NAND, Multiplexer, Total SUM...)

The student will see a question similar to:
"Giving the inputs related to the truth table of a NAND gate, A=0 B =1. Which is the output related to, S = ?"
It might be given the inputs using an image, but It is not relevant at the moment.
The output will be a text box in which the student could complete with the correct anwser (text or only numerical I don't know yet). Example S=1

I've given the example with the NAND gate because is shorter than Multiplexer, which contain more inputs and more outputs. But I consider if It is possible to develop for a NAND gate It will be similar for Multiplexer (there are more cases of course)

So the logic I want to create for the question-type is:
1. Generate the questions of each gate randomly (truth table of some combinational circuits) -> I think I can do it by using javaScript and I suppose it is neccessary to create one .js file for each gate I need to develop.
2. Wait for the answer of the student -> Using a box for numerical or text, I'm not sure
3. Then check and mark all answers of the student -> My code will be able to correct each question according the inputs of the students
4. Finally show the rights and wrongs answer/questions -> Using cross and checks

The teacher will have to select the kind of questions to be generated: NAND, Multiplexer, Total SUM. I believe It won't be neccessary to give more control to the teacher in this case.
Due to the whole logic will be implemented into the code.

I hope I've explained it more precisely now.

Thanks in advanced for your help and advice!

Andrea.
In reply to Andrea Custode Quevedo

Re: Developing a plugin "new question type" 3.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
I think you should not try to auto-generate questions in the question bank. It should alwasy be the job of the teacher to create the question.

For your question, however, instead of the usual options on the form where the teacher creates the question (e.g. for mutiple choice, the teacher has to type in all the choices), there will just be a drop-down menu where the teacher can choose from the pre-defined types like NAND, Multiplexer, ...

It is a completely different subject area, but acutally, https://moodle.org/plugins/qtype_musictheory works a bit like that.
In reply to Tim Hunt

Re: Developing a plugin "new question type" 3.5

by Andrea Custode Quevedo -

Hello again Tim.

I've been researching about question types in Moodle (as you recommended to me, qtype_musictheory)

Finally, I decided to develop a new question type. I've read it is better to take an existing question type and take the code neccessary for our new plugin. In my case, I've taken shortanswer and replace in all files my plugin name.

I have some questions:

1 ) I would like to modified my edit_myplugin_form.php file without to extend question_edit_form. Is it possible?

I would like to modify the General part of my form as well as to add new parts (I have attached an example of what I want to achieve)

Could you please give me any advice?

2) I have created into /lang 2 files:

/en_utf8/qtype_mypluginname.php -> For strings in English

  example: $string['answer'] = 'Answer: {$a}';

/es_utf8/qtype_mypluginname.php -> For strings in Spanish (my plugin will be use in this language). 

  example: $string['answer'] = 'Respuesta: {$a}';

Is it neccessary to translate the name of the strings or only the text? (I have already my files defined as the examples)

Thaks in advance.

Regards,

Andrea.

Attachment imagenMOOC3.png
In reply to Andrea Custode Quevedo

Re: Developing a plugin "new question type" 3.5

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
As Marcus said above, https://github.com/marcusgreen/moodle-qtype_TEMPLATE might be a better place to start than another qtype.

Pretty sure lang folder names should not have the _utf8 part. You only translate the array values, not the keys. (And once your plugin is finished, the recommended way to do that is using lang.moodle.org. https://docs.moodle.org/dev/Translating_plugins)

I strongly recommend that you always extend question_edit_form. If not, you will have to reproduce all the things the base class does. If you don't want some of the standard question type fields, then a better way is to just remove them. Here is an example: https://github.com/moodleou/moodle-qtype_opaque/blob/master/edit_opaque_form.php#L41
In reply to Tim Hunt

Re: Developing a plugin "new question type" 3.5

by Andrea Custode Quevedo -
Hi Tim.

Yes, I did it as Marcus said. This qtype_TEMPLATE is a copy of a shortanswer question type with some functionalities deleted.

Thanks for your answer about translations array values and keys. So, is utf8 applied as default? When I tested my plugin (initial version) there were some characters showed wrong. After that I put _utf8 part and apparently it works (I will re-check it just in case)

I will check the links. Thanks again!

Regards,
Andrea.
Average of ratings: Useful (1)
In reply to Tim Hunt

Re: Developing a plugin "new question type" 3.5

by Andrea Custode Quevedo -
Hi!

I've been working on my plugin and I have something similiar what I wanted to achieve.

But I have a question: By using addElement() function. Is there any type of argument can I use for adding an integer?

I have been looking for something in https://docs.moodle.org/dev/lib/formslib.php_Form_Definition. However, I didn't find anything. Any advice?

In my case the three last fields I have defined as text type but I would like a numeric type (maybe a dropdown which I can select a number with)

Thanks in advance!

Regards,
Andrea.
Attachment Opciones de pregunta Moodle form.png
In reply to Andrea Custode Quevedo

Re: Developing a plugin "new question type" 3.5

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 add a 'text' field, and they set the type to PARAM_INT:

$mform->addElement('text', 'myintfield', ...);
$mform->setType('myintfield', PARAM_INT);