Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Cloze question type: Difference between revisions

From MoodleDocs
(no more usefull)
(obsolete)
Line 68: Line 68:


In the following days, I will try to explore and set here various proposals that could be integrated in the code cloze code to improve editing cloze questions. [[User:Pierre Pichet|Pierre Pichet]] 20:43, 7 August 2011 (WST)   
In the following days, I will try to explore and set here various proposals that could be integrated in the code cloze code to improve editing cloze questions. [[User:Pierre Pichet|Pierre Pichet]] 20:43, 7 August 2011 (WST)   
== Old or new editing form ==
The code to give the options of editing in the old or new way is not completed actually but I plan to use the
'''question_multianswer database sequence''' field to discriminate between the new and old style.
In the old style it contains the subquestions id in a string like 23,25,45 etc.
If the question is saved from the new editing form (which allows more parameters without any limitation in the characters used and is not backward compatible) the '''question_multianswer database sequence''' will contain 'new:23,25,45 etc'.
A simple test will allow to discriminate the versions
Using the already existing database structure will allow to merge to "old" versions like 1.7 where the new edit_form has been merged.
to be continued [[User:Pierre Pichet|Pierre Pichet]] 09:08, 15 November 2007 (CST)


== Older version ==
== Older version ==

Revision as of 13:04, 7 August 2011


The code for the multianswer cloze question type is in the directory question/type/multianswer.

The name of the questiontype class is class embedded_cloze_qtype extends default_questiontype.

Cloze question creation

A cloze question is a container for several subquestions. So when you create a simple question like

America was discovered in {1:NUMERICAL:=1492:0.1#Feedback}
by {1:shortanswer;=Columbus#OK}
who was {1:MULTICHOICE:=italian#OK~spanish#Wrong~portugese#Wrong}

Cloze question saving

On saving the question text is analyzed by function qtype_multianswer_extract_question($text) which extract the three subquestions:

for ($positionkey=1

   ; preg_match('/'.ANSWER_REGEX.'/', $question->questiontext, $answerregs)
   ; ++$positionkey ) {
   $wrapped = new stdClass;
       ...
   // stored them in 
   $question->options->questions[$positionkey] = clone($wrapped);

}

and replace the sub question i.e. {1:NUMERICAL:=1492:1#Feedback} by {#$positionkey} i.e.{#1} {#2} {#3} successively. the original definition i.e. {1:NUMERICAL:=1492:1#Feedback} being stored in

$wrapped->questiontext= = $answerregs[0];

In the multianswer->questiontext the question stored is

America was discovered in {#1}
by {#2}
who was {#3}

On saving the question the $question->options->questions are stored using the functions specific of their qtypes (short,numerical,multichoice).

$wrapped = $QTYPES[$wrapped->qtype]->save_question($wrapped, $wrapped, $question->course);

Notice that the subquestions are identified in the database by setting their parent field with the main cloze question id.

Cloze question editing

When you edited a question the process is the reversed.

The get_question_options(&$question) gets the subanswers after retrieving them:

// Get relevant data indexed by positionkey from the multianswers table if (!$sequence = get_field('question_multianswer', 'sequence', 'question', $question->id)) {

    notify('Error: Cloze question '.$question->id.' is missing question options!');
    return false;

}

$wrappedquestions = get_records_list('question', 'id', $sequence, 'id ASC');

As the original text defining a given subquestion was stored in the subquestion->questiontext, it is put back in the main multianswer cloze question->questiontext using the {#1},{#2}, etc. that were left as place marker.

Cloze question editing improvments

There are many internal plug-ins or external tools that are proposed as a substitute to previous or actual (2,1) editing interface.

In the following days, I will try to explore and set here various proposals that could be integrated in the code cloze code to improve editing cloze questions. Pierre Pichet 20:43, 7 August 2011 (WST)

Older version

Most of the methods of this questiontype simply call the corresponding methods of the wrapped questions. So mostly this questiontype class only has to do clever bookkeeping.

The way the questiontype it is implemented at the moment all wrapped questions must be of a type that use a single response field $state->responses[''].

Data structure

quiz_multianswers database table

The quiz_multianswers table belongs to the multianswer questiontype and is an extension of the quiz_questions table. It merely stores a comma separated list of question ids in the sequence field, which is important, because that's the only way to know which sub question belongs to which position in the questiontext.

id
int(10) unsigned NOT NULL auto_increment,
Primary key
question
int(10) unsigned NOT NULL default '0',
Foreign key refering to the id field in the quiz_questions table
sequence
varchar(255) NOT NULL default ,
A comma separated list of question ids in the order in which they appear in the questiontext.

Response storage model

The multianswer question uses a serialized format consisting of a comma separated list of pairs. Each pair saves the response for one of the subquestions and is itself separated with a hyphen ('-'). In front of the hyphen there is the sequence number of the subquestion (starting at 1). After the hyphen is the raw response, that was submitted by the student. The serialized format could look like the following example:

1-34,2-Napoleon,3-4,4-correct response,5-1

All commas and hyphens that are part of the raw answer are HTML entity encoded to avoid problems.

The fact that the sequence number of the subquestion, rather than the id of the subquestion is used in the response storage is a pity. It could leads to problems when the teacher changes the layout of the cloze question.