Develop a question type plugin for Moodle 2.x

Develop a question type plugin for Moodle 2.x

by Silvia Lopera -
Number of replies: 7

Hello, my name is Silvia. I hope to be in the proper forum. I am trying to develop 3 specific type of questions as the final proyect of my degree in Computer Science.

I downloaded the TEMPLATE folder from the CVS repository and followed the instructions on:

http://docs.moodle.org/dev/Question_type_plugin_how_to#Model_Question_Types 

They were enough for Moodle 1.9.14 so that Moodle detected my plugin and the new question type appeared on the list with the rest of question types, but I had some troubles with release 2.1.1. After this, I realized that the structure of files is different in both. For example, there is a display.html file in every question type on Moodle 1.9.14 and not in Moodle 2.1.1.

My questions are the following ones:

Are this instructions and the TEMPLATE folder the good ones to develop a question type plugin for Moodle 2.1.1?
or  should I develop my plugin for Moodle 1.9.14?

I am pretty confused. I would appreciate any help.

Thanks. 

 

 

 

 

 

 

Average of ratings: -
In reply to Silvia Lopera

Re: Develop a question type plugin for Moodle 2.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

The up-to-date docs are not written. The incomplete version is here: http://docs.moodle.org/dev/Developing_a_Question_Type

Also, the base classes have good PHPdocs to explain what each method is for, and the existing question types are more or less good examples. There are also some third-party question types that you can look at, including the ones at https://github.com/jamiepratt/.

In reply to Silvia Lopera

Re: Develop a question type plugin for Moodle 2.x

by Andreas Hackl -

Hello everyone!

I want to use this thread to ask some further questions about question type developing. For my thesis I want to create an eLearning platform for German-speakers to learn basic Japanese. For this I would like to develop a question type plugin.

After checking this thread and the links mentioned I took a look on the code of the default plugins and I'm rather confused at the moment. I have found the PHPDocs so far but most times I don't really get what's doing what.

For the start it would be a help what is the question type class for and also the question definition class. I have read what Tim Hunt wrote in the Docs but it doesn't help me much. sad What are the core methods which must be written in these files and what are they doing (a short description put in other words than in the PHPDocs are fine)?

I can imagine for an Moodle developer veteran my questions are quite dumb but I would be really thankful for some help. However, the renderer class was quite easy to understand for me.

Thank you!

Andreas

In reply to Andreas Hackl

Re: Develop a question type plugin for Moodle 2.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

Yes, I really need to find time to finish http://docs.moodle.org/dev/Developing_a_Question_Type.

The distinction between the question type class and the question definition class is quite simple.

An object of type qtype_shortanswer_question is a short-answer question. If yo load three short-answer questions from the question bank, then you will get three instances of that class. This class is not just the question definition, it can also track the current state of a question as a student attempts it. For example, for a multiple-choice question, the class will store what order the choices have been randomly shuffled into for that student. To put it another way, the question_definition often works with a particular question_attempt.

In contrast, the qtype_shortanswer class represents the type of question, and there will normally only be a single instance of this class. It has several responsiblities, including providing meta-data about this question type (e.g. public function name()). It knows how to load, save and delete questions of this type to and from the database (get_question_options and save_question_options, delete_question) and hence how to construct instances of the qtype_shortanswer_question class. It provides methods to help with editing questions of this type. It can also provide the implmenentation for import and export in various formats.

The other key classes are the editing form, which is obviously used to edit the question, and the renderer, which is responsible for displaying all the parts of the question as HTML.

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

Re: Develop a question type plugin for Moodle 2.x

by Andreas Hackl -

It has been a while but nevertheless I thank you, Mr. Hunt. It got me some help to understand the system.

Unfortunately I have a new question which seems quite easy to answer but I don't get the solution by myself. At the moment I write the logic for the question type I work on. For this I orientate on the code of the truefalse question type.

I'm stuck on the method save_question_options() in the questiontype class. As a parameter it has the $question variable and I would like to know where the variable is going to be initialised so I can check which data I can use (unfortunately Aptana 3 don't show me any proposal after the write of "$question->") which is somehow logical as the IDE don't know of which type $question is).

I would be really glad to get some help again. Thank you in advance.

In reply to Andreas Hackl

Re: Develop a question type plugin for Moodle 2.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

It is the data returned by the qtype_..._form's get_data method, so the fields of that object closely match the fields in your editing form. It is just a stdClass, which is why the field names won't auto-complete in your IDE. Instead you just need to temporarily add something like

print_object($question);

inside save_question_options so you can see what you have got. (print_object is just a wrapper around PHP's print_r that formats the output nicely. It is useful for debugging.)

In reply to Tim Hunt

Re: Develop a question type plugin for Moodle 2.x

by Andreas Hackl -

Do you mean the edit_truefalse_form (let's say truefalse because it's the qtype I'm orientating on) file? Well, there's no get_data method but a method called data_preprocessing which returns $question but also has the variable as a parameter. Did you mean this one?

http://xref.moodle.org/_functions/index.html#get_data told me the existence of two get_data methods whose one is used for Excel worksheets and the other in lib/formslib.php which seems too basic to me.

However, thanks for the debugging hint. I'll keep that in mind!