Using ajax to reload parts of a page

Using ajax to reload parts of a page

by Krunoslav Smrekar -
Number of replies: 5

Hello,


I'm working on a new question type, and I have to make parts of a page to reload where the student is answering the question.

My question is has anybody here used ajax and javascript with moodle? I don't even know if I can do it this way, can you give me some pointers.

Thank you smile

Average of ratings: -
In reply to Krunoslav Smrekar

Re: Using ajax to reload parts of a page

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 question engine is not designed to work that way? What does your question type do? Is this really necessary.

(Ajax and JavaScript is used in lots of places in Moodle, just not in questions.)

In reply to Tim Hunt

Re: Using ajax to reload parts of a page

by Krunoslav Smrekar -

My question type works a lot like SQL Zoo (http://sqlzoo.net/wiki/Main_Page). The student enters a query and I have to display the result and while he's doing the test show him that the query is correct or not with a tick. 
So when the student enters the query I thought there would be a button so it would just show the result for the updated query, I can do it with a refresh of the page but I think it's better this way. 

That's why I asked you to tell me if it's even possible to do it that way? I know the basics of AJAX but I'm not sure how to implement it to do just what I need here. What do you think?

In reply to Krunoslav Smrekar

Re: Using ajax to reload parts of a page

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

If you want to fit into the Moodle quiz/question engine, then you need to follow how they are designed to work, not how you think they should work.

So, if you want that sort of interaction, just make you quesion type work well with the 'Interactive with multiple tries' and the 'Adaptive' options for 'How questions behave'.

I now remember one question type that does use Ajax: the STACK question type. However, it only uses ajax for validation. The Check button still reload the page, and the question type works fine if you disable JavaScript.

It would be possible to change the question engine so that, with Adaptive or Interactive behaviour, all questions get updated in an Ajax way, but that would need to be a change to the whole system, not just one question type.

In reply to Tim Hunt

Re: Using ajax to reload parts of a page

by Krunoslav Smrekar -
So you're saying that it is best not to use AJAX for that type of interaction? 

 I will look into the adaptive options.
In reply to Krunoslav Smrekar

Re: Using ajax to reload parts of a page

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

If you are building a Moodle question type, it is best not to use Ajax for that type of interaction.

It is important to understand the different question behaviours, and what they do. The best way to do that is to create an example question, with some hints, and preview it using all the different behaviours. That way you can see which parts of the overall experience are managed by the question type, and which are managed by the behaviour plug-in.

Average of ratings: Useful (1)