<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://docs.moodle.org/dev/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Janaranjan</id>
	<title>MoodleDocs - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://docs.moodle.org/dev/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Janaranjan"/>
	<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/Special:Contributions/Janaranjan"/>
	<updated>2026-11-25T17:06:26Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Question_types&amp;diff=29959</id>
		<title>Question types</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Question_types&amp;diff=29959"/>
		<updated>2011-10-10T11:11:18Z</updated>

		<summary type="html">&lt;p&gt;Janaranjan: /* formulation_and_controls */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:Question_engine_2}}&lt;br /&gt;
This page explains how to write a question type that works with the new Moodle [[Question Engine 2|question engine]].&lt;br /&gt;
&lt;br /&gt;
For instructions on making a question type for current versions of Moodle, see [[Question_type_plugin_how_to]].&lt;br /&gt;
&lt;br /&gt;
Previous section: [[Developing a Question Behaviour|Developing a Question Behaviour]]&lt;br /&gt;
&lt;br /&gt;
{{Work in progress}}&lt;br /&gt;
&lt;br /&gt;
To learn to write question types, you are highly encouraged to read the code of some of the existing question types in the Moodle code base.&lt;br /&gt;
&lt;br /&gt;
Also note that all the question engine code has extensive PHP documenter comments that should explain the purpose of every class and method. This document is supposed to provide an overview of the key points to get you started. It does not attempt to duplicate all the details in the PHPdocs.&lt;br /&gt;
&lt;br /&gt;
This document assumes that you understand the data model, where a question attempt comprises a number of steps, and each step has some properties like a state and a mark, and an array of submitted data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==File layout==&lt;br /&gt;
&lt;br /&gt;
Question types live in a folder in &amp;lt;tt&amp;gt;question/type&amp;lt;/tt&amp;gt;. The layout inside this folder follows the typical layout for a Moodle plugin. For example, inside the &amp;lt;tt&amp;gt;question/type/mytype/&amp;lt;/tt&amp;gt; folder we would have:&lt;br /&gt;
&lt;br /&gt;
; questiontype.php : Defines the &amp;lt;tt&amp;gt;qtype_mytype&amp;lt;/tt&amp;gt; class. This must extend &amp;lt;tt&amp;gt;question_type&amp;lt;/tt&amp;gt;.&lt;br /&gt;
; question.php : This contains the definition of the &amp;lt;tt&amp;gt;qtype_mytype_question&amp;lt;/tt&amp;gt; class, which should extend the &amp;lt;tt&amp;gt;question_definition&amp;lt;/tt&amp;gt; base class.&lt;br /&gt;
; renderer.php : This contains the definition of the &amp;lt;tt&amp;gt;qtype_mytype_renderer&amp;lt;/tt&amp;gt; class, which should extend the &amp;lt;tt&amp;gt;qtype_renderer&amp;lt;/tt&amp;gt; base class.&lt;br /&gt;
; simpletest/... : Contains the unit tests for this question type. You are strongly encouraged to write thorough unit tests to ensure the correctness of your question type.&lt;br /&gt;
; lang/en_utf8/qtype_myqtype.php : English language strings for this question type. You can, of course, include other languages too. The language file must define at least the string giving the model a name, for example &amp;lt;tt&amp;gt;$string[&#039;addingmyqtype&#039;] = &#039;Adding a My question type&#039;;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;$string[&#039;editingmyqtype&#039;] = &#039;Editing a My question type&#039;;&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;$string[&#039;myqtype&#039;] = &#039;My question type&#039;;&amp;lt;/tt&amp;gt;&lt;br /&gt;
; db/install.xml, db/upgrade.php : For creating any database tables required, [[Installing_and_upgrading_plugin_database_tables|as normal]]. See [[#Database_tables]] below.&lt;br /&gt;
; db/access.php : Defines any capabilities required by this question type. This is very rarely needed.&lt;br /&gt;
&lt;br /&gt;
==Database tables==&lt;br /&gt;
&lt;br /&gt;
Note that question types should only have database tables for the question definition. All runtime data is managed by the question engine.&lt;br /&gt;
&lt;br /&gt;
Question definitions should use the core table &amp;lt;tt&amp;gt;question&amp;lt;/tt&amp;gt;, and can use the core table &amp;lt;tt&amp;gt;question_answers&amp;lt;/tt&amp;gt;. You only need to define database tables if your question definition requires extra information that cannot be stored in either of these.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Question type class==&lt;br /&gt;
&lt;br /&gt;
This class represents the question type. It is responsible for loading and saving instances of questions of this class. It is also responsible for backing them up and restoring them. It also provides meta-data about the question type.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===TODO===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Question definition class==&lt;br /&gt;
&lt;br /&gt;
This holds the definition of a particular question of this type. There may also be some methods to perform processing. For example &amp;lt;tt&amp;gt;grade_responses&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===TODO===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Renderer class==&lt;br /&gt;
&lt;br /&gt;
Renderers are all about generating HTML output. The overall output of questions is controlled by the &amp;lt;tt&amp;gt;core_question_renderer::question(...)&amp;lt;/tt&amp;gt;, which in turn calls other methods of itself, the question type renderer and the behaviour renderer, to generate the various bits of output. See [[Question_Engine_2:Overview#What_are_the_parts_of_a_question.3F|this overview of the parts of a question]].&lt;br /&gt;
&lt;br /&gt;
Once again, in what follows, I only list the methods your are most likely to want to override.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===formulation_and_controls===&lt;br /&gt;
&lt;br /&gt;
This displays the question text, and the controls the student will use to input their response. Some question types choose to display some feedback embedded in this area, for example ticks and crosses next to parts of the student&#039;s response.&lt;br /&gt;
&lt;br /&gt;
The output code must respect the &amp;lt;tt&amp;gt;question_display_options&amp;lt;/tt&amp;gt; and only reveal as much information as the calling code wants revealed. For example, the teacher creating a quiz may not want the correct answer revealed until after the quiz close date. Also, when outputting the controls, you need to respect the &amp;lt;tt&amp;gt;-&amp;gt;readonly&amp;lt;/tt&amp;gt; option.&lt;br /&gt;
&lt;br /&gt;
===specific_feedback===&lt;br /&gt;
&lt;br /&gt;
Anything returned by this method is included in the &#039;outcome&#039; area of the question, it is some feedback that particularly relates to the response the student gave. This method is only called if the display options allow this to be shows, so you don&#039;t have to worry about testing that.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===correct_response===&lt;br /&gt;
&lt;br /&gt;
This would also be included in the &#039;outcome&#039; area. This should be an automatically generated (from the question definition) display of what the correct answer to the question is.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Unit tests==&lt;br /&gt;
&lt;br /&gt;
For a general introduction, see the [[Unit_tests|Moodle unit testing documentation]].&lt;br /&gt;
&lt;br /&gt;
The most important parts of your question type to test are the parts of the &amp;lt;tt&amp;gt;question_definition&amp;lt;/tt&amp;gt; class that process the students responses, including the compare and grade methods.&lt;br /&gt;
&lt;br /&gt;
The best way to start is probably to look at the tests for some of the standard question types.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advanced ideas==&lt;br /&gt;
&lt;br /&gt;
Note that there is actually no reason why you should only have a single &amp;lt;tt&amp;gt;question_definition&amp;lt;/tt&amp;gt; class for your question type. You may decide, depending on the question options, to use different definition classes where question options cause questions to behave in different ways. The multiple-choice question type does this, using different definition and renderer classes for single-response and multiple-response questions (but with common subclasses). This is possible because your question type can override the &amp;lt;tt&amp;gt;question_type::make_question_instance&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;question_definition::get_renderer&amp;lt;/tt&amp;gt; methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Converting a Moodle 2.0 question type==&lt;br /&gt;
&lt;br /&gt;
1. Change the class definition in questiontype.php from&lt;br /&gt;
 class question_myqtype_qtype extends default_questiontype {&lt;br /&gt;
to&lt;br /&gt;
 class qtype_myqtype extends question_type {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2. Create new renderer.php and question.php files, with a suitable blank class definition in them. (Perhaps copy the files from another question type, then delete everything in the class.)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3. Fill in the the question definition class, including:&lt;br /&gt;
&lt;br /&gt;
3a. appropriate fields to store the question definition.&lt;br /&gt;
&lt;br /&gt;
3b. create &amp;lt;tt&amp;gt;start_attempt&amp;lt;/tt&amp;gt;, based on the old &amp;lt;tt&amp;gt;create_session_and_responses&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
3c. create &amp;lt;tt&amp;gt;apply_attempt_state&amp;lt;/tt&amp;gt;, based on the old &amp;lt;tt&amp;gt;restore_session_and_responses&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
3d. Implement the grading logic &amp;lt;tt&amp;gt;get_expected_data&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;is_complete_response&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;is_gradable_response&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;is_same_response&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;grade_response&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;get_num_parts_right&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;clear_wrong_from_response&amp;lt;/tt&amp;gt;. Some of this will be based on the old &amp;lt;tt&amp;gt;grade_responses method&amp;lt;/tt&amp;gt;. You may also find some of the necessary code mixed into places like &amp;lt;tt&amp;gt;print_question_formulation_and_controls&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
3e. Implement the reporting functionality &amp;lt;tt&amp;gt;get_correct_response&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;get_question_summary&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;summarise_response&amp;lt;/tt&amp;gt;. Some of this may be related to the old grading methods.&lt;br /&gt;
&lt;br /&gt;
3f. Write a test helper class to create one or more typical test instances of your question definition class.&lt;br /&gt;
&lt;br /&gt;
3g. Write unit tests for the grading and reporting methods. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
4. Copy all the code from &amp;lt;tt&amp;gt;print_question_formulation_and_controls&amp;lt;/tt&amp;gt; and display.html, and related methods, and split it into the appropriate methods in the renderer class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
5. Write one or more walk-through tests that take instances of your question with different behaviours.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
6. Implement &amp;lt;tt&amp;gt;initialise_question_instance&amp;lt;/tt&amp;gt; to populate instances of your question definition class from the data returned by &amp;lt;tt&amp;gt;get_question_options&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
7. Copy &amp;lt;tt&amp;gt;check_file_access&amp;lt;/tt&amp;gt; from the question type class to the question definition class, and update it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
8. Delete all the old &amp;lt;tt&amp;gt;qtype_myqtype_question&amp;lt;/tt&amp;gt; methods that are no longer used.&lt;br /&gt;
&lt;br /&gt;
9. Add things like hints to the &amp;lt;tt&amp;gt;question_edit_myqtype_form&amp;lt;/tt&amp;gt; class. Look for opportunities to improve the existing code by using the helper methods like &amp;lt;tt&amp;gt;add_combined_feedback_fields&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;data_preprocessing_answers&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
Next section: [[Using_the_question_engine_from_module|Using the question engine from a module]].&lt;br /&gt;
&lt;br /&gt;
* The PHP documenter comments that explain the purposes of every method in the question engine code.&lt;br /&gt;
* Back to [[Question_Engine_2|Question Engine 2]]&lt;/div&gt;</summary>
		<author><name>Janaranjan</name></author>
	</entry>
	<entry>
		<id>https://docs.moodle.org/dev/index.php?title=Question_Engine_2:Overview&amp;diff=29957</id>
		<title>Question Engine 2:Overview</title>
		<link rel="alternate" type="text/html" href="https://docs.moodle.org/dev/index.php?title=Question_Engine_2:Overview&amp;diff=29957"/>
		<updated>2011-10-10T07:55:50Z</updated>

		<summary type="html">&lt;p&gt;Janaranjan: /* What are the parts of a question? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:Question_engine_2}}&lt;br /&gt;
This page outlines how I think the question engine should work in Moodle 2.0 or 2.1.&lt;br /&gt;
&lt;br /&gt;
Previous section: [[Question Engine 2:Rationale|Rationale]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Normalise the database structure==&lt;br /&gt;
&lt;br /&gt;
At the moment, the some parts of the question_sessions and question_states tables and their relations are not normalised. This is a technical term for describing database designs. Basically, if the tables are normalised, then each &#039;fact&#039; is stored in exactly one place. A normalised database is much less likely to get into an inconsistent state. Changing to a normalised structure should therefore increase robustness.&lt;br /&gt;
&lt;br /&gt;
In addition, I wish to change the tables, so the the responses received from the student are stored in a much more &#039;raw&#039; form. That will mean that the responses can be saved much earlier in the sequence of processing, which will again increase robustness. It will also allow the sequence of responses from the student to be replayed more easily, making it easier to continue after a server crash, to regrade, and to write automatic test scripts.&lt;br /&gt;
&lt;br /&gt;
The detailed design describes the new database structure.&lt;br /&gt;
&lt;br /&gt;
(+++Robustness, +Correctness)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==New concept: Question behaviours==&lt;br /&gt;
&lt;br /&gt;
At the moment, the various sequences of states that a question can move through in response to student input is hard-coded. It is controlled by a combination of the compare_responses and grade_responses methods of the question types, and tangled logic in the question_process_responses function. This makes it difficult to add new ways on interacting with questions, for example certainty based marking. Also, the current code is tricky to keep working.&lt;br /&gt;
&lt;br /&gt;
I would like to separate out the control of how a question moves through different states, into what I will call question behaviours. Currently, Moodle has three or four of these:&lt;br /&gt;
&lt;br /&gt;
===Deferred feedback===&lt;br /&gt;
&lt;br /&gt;
This is how the quiz currently work when adaptive mode is off. The student enters a response to each question, then does Submit all and finish at the end of their attempt. Only then do they get feedback and/or grades on each question, depending on the review settings.&lt;br /&gt;
&lt;br /&gt;
===Adaptive===&lt;br /&gt;
&lt;br /&gt;
In this mode there is a separate submit button beside each question, so the student can submit each question individually during the attempt, and if they are wrong, try to improve their answer, although for a reduced grade.&lt;br /&gt;
&lt;br /&gt;
Currently, the way adaptive mode works from the student point of view is not very good. I propose to replace it with a new Interactive mode. See below.&lt;br /&gt;
&lt;br /&gt;
===Manually graded===&lt;br /&gt;
&lt;br /&gt;
Essay questions need to be manually graded by the teacher, so you cannot really use them in adaptive mode (although currently there is nothing in the Quiz to stop you, which leads to confusing results). And it is not quite the same as Deferred feedback mode, because the student must wait for the teacher to grade their response after clicking submit all and finish.&lt;br /&gt;
&lt;br /&gt;
===Each attempt builds on last===&lt;br /&gt;
&lt;br /&gt;
This is very similary to the deferred feedback model, except that in subsequent attempts, each question does not start blank, but instead with the student&#039;s last response from the previous attempt.&lt;br /&gt;
&lt;br /&gt;
Currently in Moodle there is nothing to stop you trying to combine Each attempt builds on last with Adaptive mode, although that combination does not make any sense to me. I think it simplifies things to treat this as a separate mode, although, of course, it will share code with Deferred feedback mode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are also some new modes that I propose to add, either immediately, or shortly after the main part of the work:&lt;br /&gt;
&lt;br /&gt;
===Interactive===&lt;br /&gt;
&lt;br /&gt;
This will replace adaptive mode. This is the model that has been used successfully for several years in the OU&#039;s [http://www.open.ac.uk/openmarkexamples/ OpenMark] system. The OU has also  modified Moodle to work like this, but because of the way the quiz core currently works, I was not happy just merging the OU changes into Moodle core. In a sense, this whole document grew out of my thoughts about how to implement the OU changes in Moodle core properly.&lt;br /&gt;
&lt;br /&gt;
In the existing adaptive mode, after the student has clicked submit, the question both shows the feedback for the last answer the student submitted, while also letting the student change their answer. This can lead to weird results. For example create a numerical question 2 + 2 = 4. Attempt it. Enter 4. Click Submit. Enter 5. Click Save. You will see a page that seems to say that your answer is 5 and it is correct!&lt;br /&gt;
&lt;br /&gt;
In Interactive mode, the question is either in a state for the student to enter their answer, with a Submit button; or it is showing feedback for the student&#039;s previous attempt, with a Try again button to get back to the first state.&lt;br /&gt;
&lt;br /&gt;
The other difference is that students are only allowed a limited number of tries at each question (typically three). When they submit their third try, or when they submit a correct answer, that question is finished and they must go onto the next one.&lt;br /&gt;
&lt;br /&gt;
(This is quite difficult to explain. Try [https://students.open.ac.uk/openmark/s205.ayrf/ this example] and you should see how it works.)&lt;br /&gt;
&lt;br /&gt;
===Immediate feedback===&lt;br /&gt;
&lt;br /&gt;
This would be like cross between Deferred feedback an Interactive. Each question has a submit button beside it, like in interactive mode, so the student can get the feedback immediately while they still remember their thought processes while answering the question. However, unlike the interactive model, there is no try again button. You only get one chance at each question.&lt;br /&gt;
&lt;br /&gt;
This is a necessary prerequisite for implementing MDL-11047, which is fairly frequently asked for in the quiz forum.&lt;br /&gt;
&lt;br /&gt;
===Certainty based marking with deferred feedback===&lt;br /&gt;
&lt;br /&gt;
This takes any question that can use the deferred feedback model, and adds three radio buttons to the UI (higher, medium, lower) for the student to use to indicate how certain they are that their answer is correct. If they are more certain, the get more marks if they are right, but lose marks if they are wrong. This encourages students to reflect about their level of knowledge.&lt;br /&gt;
&lt;br /&gt;
===Certainty based marking with immediate feedback===&lt;br /&gt;
&lt;br /&gt;
This is like immediate feedback mode with the certainty based marking feature.&lt;br /&gt;
&lt;br /&gt;
===Delegate to a remote system===&lt;br /&gt;
&lt;br /&gt;
The Opaque question type from contrib, which is used to run both OpenMark and [http://stack.bham.ac.uk/ STACK] questions inside a Moodle quiz was very difficult to implement within the current code. Since the remote system controls the flow of the question, it makes sense to use a custom behaviour for this question type, and that will be possible in the new code.&lt;br /&gt;
&lt;br /&gt;
===Information item===&lt;br /&gt;
&lt;br /&gt;
Used for descriptions. There is no grade. Item gets marked as complete after the student has seen it once. Displays general feedback after the attempt is over.&lt;br /&gt;
&lt;br /&gt;
===Missing===&lt;br /&gt;
&lt;br /&gt;
This is like the Missing question type. This behaviour is used in the situation:&lt;br /&gt;
# Admin installs some behaviour.&lt;br /&gt;
# Student attempts quiz using that model.&lt;br /&gt;
# Admin uninstalls that model.&lt;br /&gt;
# Teacher tries to review that student&#039;s attempt.&lt;br /&gt;
The missing question behaviour does the best job it can to allow the review of the attempt with the missing model, while displaying an on-screen warning.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
So, what exactly does the behaviour control? Given the current state of the question, it limits the range of actions that are possible. For example, in adaptive mode, there is a submit button next to each question. In non-adaptive mode, the only option is to enter an answer and have it saved, until the submit all and finish button is pressed. That is, there will be a class in the PHP code for each model, and it will have methods that replace the old question_extract_responses, question_process_responses, question_process_comment and save_question_state functions in the current code.&lt;br /&gt;
&lt;br /&gt;
(+++Richness, ++Correctness, +Robustness)&lt;br /&gt;
&lt;br /&gt;
===Brief digression: who controls the model, the quiz or the question?===&lt;br /&gt;
&lt;br /&gt;
There is one design decisions that it took a long time for me to resolve, and I want to mention it here.&lt;br /&gt;
&lt;br /&gt;
The question is: Is the behaviour a property of the question, or the quiz?&lt;br /&gt;
&lt;br /&gt;
At the moment, there is a setting in the quiz that lets you change all the questions in the quiz from adaptive mode to non-adptive mode at the flick of a switch. Or, at least, it allows you to re-use the same questions in both adaptive and non-adaptive mode. This suggest that the behaviour is a property of the quiz.&lt;br /&gt;
&lt;br /&gt;
On the other hand, suppose that you wanted to introduce the feature that, in adaptive mode, the student gets different amounts of feedback after each try at getting the question right. For example, the first time they get it wrong the only get a brief hint. If they are wrong the second time they get a more detailed comment, and so on. In order to do this, you need more data (the various hints) in order to define an adaptive question that is irrelevant to a non-adaptive question. Also, certain question types (e.g. Opaque, from contrib) have to, of necessity, ignore the adaptive, non-adaptive setting. And I suggested above that, manually graded question types like Essay should really be considered to have a separate behaviour. This suggests that the behaviour is a properly of the individual question. (Although usability considerations suggest that a single quiz should probably be constructed from questions with similar behaviour.)&lt;br /&gt;
&lt;br /&gt;
I eventually concluded that both answers are right. That is, it is a good idea for the quiz to have a setting like adaptive/non-adaptive that sets out the teacher&#039;s intention of how each question in this quiz should behave. However, the exact choice of behaviour to use is up to the different question types. When a quiz attempts is started, each question type is asked something like &amp;quot;A quiz attempt is starting using &#039;adaptive mode&#039;. Exactly which behaviour should be used for questions of this type in this attempt?&amp;quot; This gives questions that can only work in a certain way (for example essay questions) a chance to override the quiz setting.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Clarifying question states==&lt;br /&gt;
&lt;br /&gt;
Although there is a database table called question_states, the column there that stores the type of state is called event, and the values stored there are active verbs like open, save, grade. The question_process_responses function is written in terms of these actions too. One nasty part is that during the processing, the event type can be changed several times, before it is finally stored in the database as a state.&lt;br /&gt;
&lt;br /&gt;
I would like to clearly separate the concept of the current state that a question is in, and the various actions that lead to a change of state. The actions will be handled by the behaviours, and the state will be stored in the database as a state.&lt;br /&gt;
&lt;br /&gt;
The list of available states will also be changed slightly to match the following diagram. This came from thinking about what information it was important to display in the quiz navigation block.&lt;br /&gt;
&lt;br /&gt;
[[Image:Question_state_diagram.png]]&lt;br /&gt;
&lt;br /&gt;
(+Correctness, +Richness).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==What are the parts of a question?==&lt;br /&gt;
&lt;br /&gt;
When we come to think of outputting a question on-screen, we can now see that there are several things that affect what it will look like:&lt;br /&gt;
# The question type (multichoice, shortanswer, essay, ...)&lt;br /&gt;
# The behaviour (deferred feedback, interactive, CBM, ...)&lt;br /&gt;
# What state the question is in (has the student answered yet, has it been graded, ...)&lt;br /&gt;
# The options from the particular activity (whether any marks are available for this question, are students allowed to see their grades, how many decimal places to use to display grades, does this user have permission to make a manual comment ...)&lt;br /&gt;
&lt;br /&gt;
Also, we want different question type and behaviours to have the freedom to display whatever they like to the students; but we also want all the questions in a quiz to look and behave consistently, so that the experience is not confusing.&lt;br /&gt;
&lt;br /&gt;
In the sketch below, I try to summarise the bits that may be present in a typical question. If all questions can have these bits in this order, then that is probably enough consistency. The flexibility comes from allowing the question type or behaviour to put whatever it likes inside those bits. &lt;br /&gt;
&lt;br /&gt;
We can divide what you see when looking at a question is into three main parts:&lt;br /&gt;
# Information (meta-data): Which question number this is, a summary of what state it is currently in, what grade you have got for it (or how many marks are available, if any), whether you have flagged it. This has a grey background in the sketch below.&lt;br /&gt;
# The question and the response to it. This has a blue background in the sketch.&lt;br /&gt;
# The outcome from submitting that response. This has a yellow background.&lt;br /&gt;
&lt;br /&gt;
[[Image:Parts of a question2.png]]&lt;br /&gt;
&lt;br /&gt;
The full list of options that control which of those bits are visible at any time are:&lt;br /&gt;
&lt;br /&gt;
; correctness : (hidden / visible) whether the student gets told whether their answer was (in/partially)correct in the status summary under the question number, or instead are told something vague like &#039;Finished&#039;.&lt;br /&gt;
; marks : (hidden / max only / actual mark and max / actual mark and max with explanation ) whether the student can see the number of marks available, and how many marks they got, and how much detail of the marking to display.&lt;br /&gt;
; marks d.p. : (0 .. 7) how many decimal places marks are displayed to.&lt;br /&gt;
; flags: (hidden / visible / visible and editable) this is the feature that lets students bookmark or flag a question in an attempt for later reference.&lt;br /&gt;
; read-only: Whether the question just shows the response was already entered, or whether it gives controls to the user enter/change their response.&lt;br /&gt;
; specific feedback: (hidden / visible) feedback that relates to the particular response the student entered.&lt;br /&gt;
; general feedback : (hidden / visible) whether the general feedback (same for all students) is visible.&lt;br /&gt;
; correct response : (hidden / visible) whether the automatically generated message or other indication of the correct answer is visible.&lt;br /&gt;
; manual comment: (hidden / visible / visible and editable) whether the comment manually added by the teacher is visible and editable by the current user.&lt;br /&gt;
; response history : (hidden / visible) whether the list of steps the student went through to answer the question is displayed.&lt;br /&gt;
&lt;br /&gt;
These options are initially set by whatever it is that is using the question (for example the quiz will initialise them from the quiz settings). Then these options are modified by the behaviour. For example it should ensure that no feedback is displayed until the student has actually submitted an answer, or that after they have submitted their final answer, the question only appears in read-only mode.&lt;br /&gt;
&lt;br /&gt;
==Reorganise the code==&lt;br /&gt;
&lt;br /&gt;
There will, of course, be a new class for each behaviour, that all inherit from the same base class.&lt;br /&gt;
&lt;br /&gt;
At the moment question type classes have multiple responsibilities.&lt;br /&gt;
# They is information about the question types, and loading and saving, importing and exporting, backing up and restoring questions of that type.&lt;br /&gt;
# Then there is the processing of student responses for a particular instances of that question type.&lt;br /&gt;
# And then there is displaying the question in its various states.&lt;br /&gt;
(Fortunately, 4. Displaying an editing form, is already in a separate class.)&lt;br /&gt;
&lt;br /&gt;
Moodle 2.0 introduces the [[Theme_engines_for_Moodle%3F|renderer concept]]. Introducing qtype renderers will move the output code (3) into separate classes. This change to the question engine is the appropriate time to introduce qtype renderes.&lt;br /&gt;
&lt;br /&gt;
I will also introduce a new set of classes to store the state of a question within a quiz attempt. That is basically the combination of the $question and $state objects that are passed around at the moment, but with a specific subclass for each question type. This moves all the processing logic (2) into a separate class. That leaves the question type class itself just responsible for (1).&lt;br /&gt;
&lt;br /&gt;
Of course the behaviour classes and the question_state classes need to work together, and this is clearly a situation for the [http://en.wikipedia.org/wiki/Strategy_pattern strategy pattern]. However, I have not yet decided whether the question type is the context, and the iteration model is the strategy, or vice versa. I will have to see which comes out better when I do the implementation.&lt;br /&gt;
&lt;br /&gt;
Finally, there will be a new class in questionlib.php for managing the set of questions in an attempt. Already, in the Moodle 2.0 quiz developments, I have changed the quiz code to create a quiz_attempt class. This keeps track of all the questions in the quiz attempt. However, this really needs to be split into two bits. The job of tracking a set of questions and what state they are in is exactly the job of the question engine. Therefore we should have a question_set_attempt class that does that job. That class will take over the job of a number of functions in questionlib.php, for example question_load_states. Then the quiz_attempt class can just focus on the quiz-specific things, and use the question_set_attempt class. This new class should make it easier to use questions in other modules.&lt;br /&gt;
&lt;br /&gt;
It should be possible to organise the code so that only the question_set_attempt class had to load or save data to or from the database. It will then pass that data on to wherever it is needed. That should be good for efficiency.&lt;br /&gt;
&lt;br /&gt;
(+Robustness, +Efficiency)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Simplified API for question types==&lt;br /&gt;
&lt;br /&gt;
In the summary of how the quiz currently works, I said, &amp;quot;The student enters an answer to each question which is saved. Then when they submit the quiz attempt, all the questions are graded.&amp;quot; In fact, that was a lie. Whenever a response is saved, the grade_responses method of the question type is called, even in the state is only being saved. This is confusing, to say the least, and very bad for performance in the case of a question type like JUnit (in contrib) which takes code submitted by the student, and compiles it in order to grade it.&lt;br /&gt;
&lt;br /&gt;
So some of the API will only change to the extent that certain functions will in future only be called when one would expect them to be. I think this can be done in a backwards-compatible way.&lt;br /&gt;
&lt;br /&gt;
Another change will be that, at the moment, question types have to implement tricky load/save_question_state methods that, basically, have to unserialise/serialise some of the state data in a custom way, so it can be stored in the answer column. This is silly design, and leads to extra, unnecessary database queries. The changes to the database structure will to eliminate the need for these methods.&lt;br /&gt;
&lt;br /&gt;
Hopefully the above changes to how the code is organised will make it mauch easier to write new question types.&lt;br /&gt;
&lt;br /&gt;
(+Richness, +Correctness, +Efficiency)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduce more automated testing==&lt;br /&gt;
&lt;br /&gt;
Since the question_set_attempt class keeps track of all the data that is needed when processing questions, it should be very easy to write automatic tests (unit tests) for all the other parts of the question engine. That should greatly help in eliminating bugs.&lt;br /&gt;
&lt;br /&gt;
I am intending to take a http://en.wikipedia.org/wiki/Test-driven_development test-driven] approach to implementing this proposal.&lt;br /&gt;
&lt;br /&gt;
(+++Correctness, ++Robustness)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
In the next section, [[Question Engine 2:Design|Design]], gives the detailed design of the above solution.&lt;br /&gt;
&lt;br /&gt;
* Back to [[Question_Engine_2|Question Engine 2]]&lt;/div&gt;</summary>
		<author><name>Janaranjan</name></author>
	</entry>
</feed>