Learning how to develop a Moodle 2.3 Question Type

Learning how to develop a Moodle 2.3 Question Type

by Andrew Solomon -
Number of replies: 12

I've just written my first Moodle Block and a WebService and now I'd like to (learn how to) implement a new Question Type. 

My problem is that I don't know where to start. I've had a look at Opaque and decided it's not appropriate for my type of question so I'd just like to learn the basics of how to implement a very simple question type.

I've been looking at http://docs.moodle.org/dev/Question_type_plugin_how_to  [1] and it's seems like quite a clear explanation of how to proceed, but http://docs.moodle.org/dev/Developing_a_Question_Type [2] makes me think that [1] will not be a valid approach for Moodle 2.3. 

Reference [2] is rather complicated and has lots of gaps so I'm not at all clear on how to proceed from there.

Advice on where to start would be greatly appreciated!

Average of ratings: -
In reply to Andrew Solomon

Re: Learning how to develop a Moodle 2.3 Question Type

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

@Andrew,

If you gave us a description of that new question type you want to develop (with concrete examples), it might be easier to help you.mixed

The usual approach is to start with an existing question type which is the closest to your new type and adapt it.

Joseph

In reply to Andrew Solomon

Re: Learning how to develop a Moodle 2.3 Question Type

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Andrew, you may find this interesting

http://docs.moodle.org/dev/Question_engine

In reply to Marcus Green

Re: Learning how to develop a Moodle 2.3 Question Type

by Andrew Solomon -

Thanks Marcus - that was a helpful link and I've made progress. My next question is regarding hints.

I've written a question class extending question_graded_automatically and I've given it a method:

public function get_hint($hintnumber, question_attempt $qa) {
return "This is a hint";
}

However this message doesn't appear anywhere on the preview question screen regardless of the question behaviour I choose. Is there anything else I need to do in order to get hints to be displayed?

In reply to Andrew Solomon

Re: Learning how to develop a Moodle 2.3 Question Type

by Jean-Michel Védrine -

Hello Andrew,

Unfortunately you have not answered Joseph's question so it's very difficult to help you.

Most questions types don't need a get_hint function : in the core questions types if I remember well only multichoice question type has a get_hint function to stop displaying hints if the student has checked too many choices.

So unless you are creating a question type that does something very special with hints this is not a good start.

But there are several places in your code where you need to write a few lines in order to support hints.

As other have said the best method is

- to find an existing question type doing things similar to what you want your type to do

- to look at the code

For instance here you will search "hint" in all the code of your model question type to see where there is code related to hints.

But again it is very difficult to help you if you don't give us more informations !

In reply to Jean-Michel Védrine

Re: Learning how to develop a Moodle 2.3 Question Type

by Jean-Michel Védrine -

I forgot to say that hints are only used with interactive with many tries behaviour.

In reply to Jean-Michel Védrine

Re: Learning how to develop a Moodle 2.3 Question Type

by Andrew Solomon -

Here's more information on my question type which I hope will make my issues clearer:

  • A question will be something along the lines of 'Create the file /tmp/your.name and make it readable to everyone'
  • Marking the question makes a GET call to the 'mark web-service' of the machine the student's working on
  • The response is JSON of the form { 'correct' : 'true' } or { 'correct': 'false', 'hint': "it's good you've put the file there, but the permissions are incorrect"}
  • I'd like to display this hint before the student's next attempt.

I'm implementing this as an extension of question_graded_automatically and I've tried:

  • returning a string from get_hint 
  • setting $this->generalfeedback in grade_response

but neither of these strings are displayed.

In reply to Andrew Solomon

Re: Learning how to develop a Moodle 2.3 Question Type

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Andrew "A question will be something along the lines of 'Create the file /tmp/your.name and make it readable to everyone'..."

I must admit this "question type" looks quite mysterious to me. Are you sure the Moodle quiz activity is the best suited for your purpose?

Joseph

Average of ratings: Useful (1)
In reply to Andrew Solomon

Re: Learning how to develop a Moodle 2.3 Question Type

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

What you are attempting sounds very ambitious and very cool (a great example of authentic assessment) but I am not sure it is feasible within the Moodle quiz.

The quiz - or more accurately the question engine - is designed on the assumption that the student's response comes in as a HTTP POST request, and that requires all the information required for grading. Clearly, what you are doing violates that assumption.

Why do we need this assumption? Well, think about what happens when the teacher later tries to re-grade a quiz attempt. (Perhaps you don't yet know about Moodle's re-grade feature, and what it does. You should try it. You access it under quiz -> results -> grades. The code is in mod/quiz/report/overview/report.php, which of cousre call in to the question system.)

If you really want to do something like this, then you would need to take a different approach. For example, you could build this question type by using a signed Java applet displayed as part of the question, which would examing the student's hard disc, and set some hidden form fields which would then be submitted back to Moodle when the student submits the quiz.

In reply to Tim Hunt

Re: Learning how to develop a Moodle 2.3 Question Type

by Andrew Solomon -

Ouch! This is turning out to be harder than I thought. I'd really prefer not to use a Java applet as I want to minimise things the student has to install. 

I've found a partial solution by over-riding question_graded_automatically->get_expected_data with the REST call. The problem is that this is called in cases other than when clicking the 'check/submit' button.

Is there any other function which is called when (and only when) the  'check/submit' button is clicked? Is it question/engine/questionattempt->get_all_submitted_qt_vars ? and if that's the case, how can I over-ride it?

(Alternatively, I'm wondering if I should attach a javascript REST call to the 'check/submit' buttons...)

In reply to Andrew Solomon

Re: Learning how to develop a Moodle 2.3 Question Type

by Jamie Pratt -

Hi Andrew,

As others have said, this sounds like interesting work.

From what I have understood you are wanting either the browser or the Moodle software on the web server to call a web service on the computer of the person acessing Moodle to check if they have put a file in the right place and/ or with the right permissions?? Or is the web service on the server and the person has to place a file in the correct place on the server?


The reason I ask is for me / us to get more of a grasp of what you are trying to do and because if the web service is on a web server then there wouldn't be any network access issues but outside an internal LAN then I think you are going to have access problems trying to call a web service on the client computer.

IF you are trying to check for a file on the server why not use php's file functions to do this rather than calling a web service?


I think in an answer to your question about when to call the web service you probably need to put this in the code to evaluate a student response, browse through the code of outher question types to see how this works. But you should be aware that this code to evaluate student responses will also be used when a question is being regraded, so you need to work out how to stop the web service being called in this case.

If you grade a question wrong in ínteractive response' mode then the student will have the chance to do the question again. This might give you the chance to give the kind of feedback you want to to students. You can adjust the feedback given to the student by changing the 'feedback' property of the answer object you return from the response evaluation.

Jamie

In reply to Jamie Pratt

Re: Learning how to develop a Moodle 2.3 Question Type

by Jamie Pratt -

Ah, I see that the web service will be installed on the client computer. How are you intending to determine the ip address for the web service call?

I would think it would make sense to call the web service using js from the client machine, if that is possible, store the data in a hidden form field and submit via POST request data that will then be evaluated on the server by Moodle in the normal way. You would probably want to attach events to mulitple buttons (check, submit, save, submit all in the quiz) that might be used to submit the form on the Quiz page to make sure you get a chance to call the web service and store the data in the form before the form data is sent to Moodle.

In reply to Jamie Pratt

Re: Learning how to develop a Moodle 2.3 Question Type

by Andrew Solomon -

It's 2 years since I started working on this problem of a question type which involves running the student's code on their own box. With a lot of help from Jamie it's now in production. As Tim said, it's not perfect since regrading a quiz won't work but it has the functionality I need. If you want to have a play with it, go here: https://geekuni.com/ (you can enrol and pay nothing if you close your account within 3 days). If you'd find this functionality useful, let me know.