Creating a new question type

Creating a new question type

napisao/la Krunoslav Smrekar -
Broj odgovora: 14

Hello,

I'm new with developing for Moodle. I have a project to create a new question type for SQL queries.

I've used the template provided in this link: https://github.com/jamiepratt/moodle-qtype_TEMPLATE/ and started working from there. I haven't made a lot, just in the edit_myqtype_form to make it look how I want. Now I know that in the questiontype.php file I have to define how to save the forms in save_question_options($question) but I don't know how to even start.

I've looked in other question types to get me started but haven't had luck because I don't understand most of the functions. Can you provide me with a link where it is explained how all this works and how to proceed from here. I have a couple of textboxes and an image to be saved. I'm using Moodle 2.6 version


I hope that I've put this question in the right place.


Thank you in advance. osmijeh

Odgovor na Krunoslav Smrekar

Re: Creating a new question type

napisao/la Marcus Green -
Slika Core developers Slika Particularly helpful Moodlers Slika Plugin developers Slika Testers

You have come to the right place but your question is rather general. Why not put your code so far (however minimal), on Github and then ask a question about how to do a single specific thing. 


When I built a question type I got most of my knowledge through reading source and running other types of question to see what they did in terms of writing to the database. That may not of course be the most optimal way of learning to code a thing......

Odgovor na Marcus Green

Re: Creating a new question type

napisao/la Krunoslav Smrekar -

Thanks for replying. osmijeh


That is pretty much how I'm trying to make something. But it's going very slowly. Here's my repository. https://github.com/KruxZhlajfa/sqlupiti


I'm stuck now at questiontype.php in the save_question_options function. I'm trying for now to save the "sqlanswer" field from edit_sqlupiti_form but it's giving me an error.


I know that I ask general stuff (I really only just started developing for it) but if you could help me I would be really appreciate it. Thanks :D

Odgovor na Krunoslav Smrekar

Re: Creating a new question type

napisao/la Marcus Green -
Slika Core developers Slika Particularly helpful Moodlers Slika Plugin developers Slika Testers

What is the error message? Have you set messages to debug?

Odgovor na Marcus Green

Re: Creating a new question type

napisao/la Krunoslav Smrekar -

I think I actually fixed that error but it's not quite working.

Let me ask you this. From this line of code: 

$DB->insert_record('question_answers', $answer); 

,correct me if I'm wrong but the "question_answers" holds fields. If so how can I create my own so I can store my own fields. 

That is what's bothering me...

Odgovor na Krunoslav Smrekar

Re: Creating a new question type

napisao/la Marcus Green -
Slika Core developers Slika Particularly helpful Moodlers Slika Plugin developers Slika Testers

I recomend you put your code into an IDE such as NetBeans, that way you can get the nice popups of what the base classes offer. Take a look at the following function in questiontypebase, and then see if you can find a question that uses it. Also install as many other question types as you can so you can see what other people have been written.

   

/* If your question type has a table that extends the question_answers table,     make this method return an array wherer the first element is the table name,  and the subsequent entries are the column names (apart from id and answerid).

 @return mixed array as above, or null to tell the base class to do nothing. */

    public function extra_answer_fields() {

        return null;

    }

Odgovor na Marcus Green

Re: Creating a new question type

napisao/la Krunoslav Smrekar -
Ok I've installed NetBeans, I forgot about the CTRL+Space function, thanks for recommending it. 

I have a few more questions. First what is this questiontypebase? I've seen these files in other question types.
What does the initialise_question_instance function do?
And last question, I have a problem with displaying strings properly for the qtype_mytype file. I use the get_string() function to get the string from that file but when I want to use strings that I've created (other than the plugin... strings) it just says this "mystring". I really don't know why it does that, I've checked and there is not type error. Is this a common error?


Odgovor na Krunoslav Smrekar

Re: Creating a new question type

napisao/la Marcus Green -
Slika Core developers Slika Particularly helpful Moodlers Slika Plugin developers Slika Testers

questiontypebase will be a base class supplied by Moodle that gives you plenty of default functionality, and where you need more than that functionality you override the methods in your class.  I suspect your post of mystring was supposed to be mystring with the braces but this forum turned them into a link. Strings will get cached by moodle so you should open another instance of moodle just at the purge caches form /moodle/admin/purgecaches.php

to ensure you are looking at the most recent version of strings/css. You could switch to CSS designer mode to kill caching, but it tends to make everything rather slow.

Odgovor na Marcus Green

Re: Creating a new question type

napisao/la Krunoslav Smrekar -

Can you tell me how and where to create my own question table?
I see that in the true/false question type there is an "question_truefalse" table that holds some fields, not all but some.


I want to make that for my question type. So that I can get question options from it. 

Odgovor na Krunoslav Smrekar

Re: Creating a new question type

napisao/la Marcus Green -
Slika Core developers Slika Particularly helpful Moodlers Slika Plugin developers Slika Testers

I assume you mean to ask how to get the data from your own table. I have the following method in my question type. As to how it gets created when the question is installed, look in the folder called db for any contrib question type.

   public function extra_question_fields() {

        return array('question_gapfill', 'answerdisplay', 'delimitchars', 'casesensitive',

            'noduplicates', 'disableregex');

    }

Odgovor na Krunoslav Smrekar

Re: Creating a new question type

napisao/la Tim Hunt -
Slika Core developers Slika Documentation writers Slika Particularly helpful Moodlers Slika Peer reviewers Slika Plugin developers
Odgovor na Tim Hunt

Re: Creating a new question type

napisao/la Krunoslav Smrekar -

Ok I think I understand everything that you guys want me to do.

I've made the install.xml file with the XMLDB editor and I'm still getting the error that my table doesn't exist. 
This is probably really simple but I can't find it, how can I trigger the installation so I will be there. I know it is installed when the user installes my question type but how can I install it now?

Odgovor na Krunoslav Smrekar

Re: Creating a new question type

napisao/la Marcus Green -
Slika Core developers Slika Particularly helpful Moodlers Slika Plugin developers Slika Testers

I take it that the table has not been created in the database so far? I reccommend installing phpMyAdmin for local manipulation of the mysql database. Try incrementing the version number in version.php so the system thinks it is a new version and triggers the table creation.

Odgovor na Marcus Green

Re: Creating a new question type

napisao/la Krunoslav Smrekar -

Hello,

Can I ask you another question?


Now I'm having problems with displaying the question to the student. I figured out how the $result works and how to output the data, and it works great when I'm previewing the question with the preview button in the question bank but when I try to attempt the quiz with my question type it says this error: "This question is missing. Unable to display anything.".
I just outputed the question text and a textbox, I haven't yet worked out the specific feedback and correct feedback. I'm wondering if the feedbacks are interfering with displaying the question? What could be the problem? I don't think it's the rendered but where else could I go and look? 

Update: I have noticed that the "Question aswer summary" in technical details is missing in my questions so I think that's the problem, so how can I add it there, is it through the feedbacks?