General developer forum

Persisting data about question answers and reusing it when next

Picture of Камен Кънчев
Persisting data about question answers and reusing it when next


I am developing a new question type plugin for Moodle. It is multiple choice question. I need to add some specific data to the answers of the question, based on the original question answer defined by the teacher and display the updated answer text to the student taking the test.

Let's say it is a random number in interval defined in the answer text. The main idea is that my question type should generate different answers in different quizes for the same question, but the answers should be the same withing given quiz instance so when going back and next between the quiz pages and when reviewing the answers after finish the question text should not different.

So as I suppose I should persist the updated answer text for the current quiz instance, without modifying the DB. 

I have tried to do this in may ways, I have tried to store this info in question attempt step instance, to persist it in the question instance and even in the question type instance. I also tried to do this in custom renderer.  But it seems that all of them are reinitialized in each refresh or next/previous question move.

As I suppose it should be similar to generating the order of the questions or keeping the selected answer but I can not achieve it.

Can someone explain me how to achieve that or point me to a resource where I can read more about question attempt steps or other mechanism how to persist  the needed data.



P.S. I am new to the forum, please excuse me if the topic is not in the correct forum.

Average of ratings: -
Tim at Lone Pine Koala Sanctuary
Re: Persisting data about question answers and reusing it when next
Core developersDocumentation writersParticularly helpful MoodlersPlugin developers

It sounds like you are trying to make something like qtype_calculatedmulti, so you could look at that code.

Or even, the standard mutliple-choice question type has to do this, since it randomly suffles the choices, and that order eneds to be remembered.

The key methods are start_attempt and apply_attempt_state, for example

The best docs about this are the comments in the base class:

Average of ratings: -