if (value('AB01') == 3) {
text('FB01'); // Answer correct
} else {
text('FB02'); // Answer wrong
}
==== Multiple Choice ====
A multiple choice has one variable for each choice option. This can have the value 1 (not ticked) or 2 (ticked). In order to answer a multiple choice correctly, it must be checked for each choice option whether it has been answered correctly -- i.e. whether it has been correctly selected or correctly left blank.
Take as an example a multiple choice AB02 with 4 answer options, of which the second and fourth must be ticked in order to answer correctly:
* (AB02_01) apple
* (AB02_02) aubergine
* (AB02_03) strawberry
* (AB02_04) potatoe
There now are two ways for checking. The first case connects 4 single answer checks via logical "and" ([[:en:create:filter-boolean]]). This is realtively simple but bulky
if ((value('AB02_01') == 1) && (value('AB02_02') == 2) && (value('AB02_03') == 1) && (value('AB02_04') == 2)) {
text('FB01'); // Answer correct
} else {
text('FB02'); // Answer wrong
}
The same code can also be written a little more clearly, but that makes it only slightly more manageable:
if (
(value('AB02_01') == 1) &&
(value('AB02_02') == 2) &&
(value('AB02_03') == 1) &&
(value('AB02_04') == 2)
) {
text('FB01'); // Answer correct
} else {
text('FB02'); // Answer wrong
}
It becomes more elegant if one defines the correct answer scheme as an array (''[1,2,1,2]'') and compares this list with the list of given answers as provided by ''[[:en:create:functions:valuelist]]''. However, the function ''array_values()'' must still be used here, because ''valueList()'' also returns the variable identifiers that would interfere with the comparison.
$answer = array_values(valueList('AB02'));
$correct = [1,2,1,2];
if ($answer == $correkt) {
text('FB01'); // answer correct
} else {
text('FB02'); // answer wrong
}
If you need a little more flexibility, you can also compare several variables with a FOR loop (see below).
==== Comparison of Several Variables ====
If a page includes several questions, then this can also be solved with ''valueList()'' as shown above with the multiple selection. However, this is more time-consuming because the variable names may have to be specified individually.
As an example, let's take the two questions "AB01" and "AB02", both of which must be answered correctly. So a 3 for the variable "AB01" and the values 1, 2, 1, 2 for the variables "AB02_01" to "AB02_04".
$answer = array_values(valueList(['AB01', 'AB02_01', 'AB02_02', 'AB02_03', 'AB02_04']));
$correct = [3,1,2,1,2];
if ($answer == $correct) {
text('FB01'); // answer correct
} else {
text('FB02'); // answer wrong
}
An alternative solution is to use a FOR loop. The variable identifier and the correct answers are first defined as an array and then checked individually in the loop. WIf a comparison is wrong, the complete answer is wrong.
$solution = [
'AB01' => 3,
'AB02_01' => 1,
'AB02_02' => 2,
'AB02_03' => 1,
'AB02_04' => 2
];
$allRight = true;
foreach ($solution as $varID => $correct) {
if (value($varID) != $correct) {
$allRight = false;
}
}
if ($allRight) {
text('FB01'); // answer correct
} else {
text('FB02'); // answer wrong
}
===== Respond to Wrong Answers=====
The reaction to a correct or incorrect answer can be designed quite differently. Essential building blocks are:
* The PHP Function ''[[:en:create:functions:text]]'' to display a predefined text.
* The PHP Function ''[[:en:create:functions:option|option('nextbutton', false)]]'' to hide the Next button so that respondents have to go back to the previous page and correct their answer.
* The PHP Function ''[[:en:create:functions:repeatpage]]'', to show a message and return directly to the previous page.
* The PHP Functions ''[[:en:create:functions:getitems]]'', ''[[:en:create:functions:getitemtext]]'' and ''[[:en:create:functions:html]]'', to redisplay the previous question and mark wrong/correct answer options.
==== Show a Text ====
The simplest variant, namely to show a text "Your answer was correct", has already been used several times above.
If you add a //text// to a rubric of your choice in the question catalogue and select the option "Formatted text" for the //presentation//, you can also design the text a little.
If you want to make the text a little more sophisticated, select the option "HTML code" at //presentation// and use HTML and CSS ([[:en:create:texts#format texts|format texts]]):
Your answer is correct.
For further design you can also use images, see [[:en:create:images]].
The integration of the text is done, as shown above, by means of ''text()''. The following example assumes that the text for the positive feedback has the identifier "FB01" and the text for the negative feedback has the identifier "FB02".
The [[:en:create:variables#php-variablen|PHP-Variable]] ''$allRight'' would have been defined further up in the PHP code -- something like in the example above. Of course, the IF filter can also contain a different condition (see above).
if ($allRight) {
text('FB01');
} else {
text('FB02');
}
==== Block Next Button ====
With the function ''[[:en:create:functions:option|option('nextbutton', false)]]'' you can also hide the back button if the answer is wrong.
if ($allRight) {
text('FB01');
} else {
text('FB02');
option('nextbutton', false);
}
==== Go Directly to the Previous Page ====
With the function ''[[:en:create:functions:repeatpage]]'' you can also directly repeat the page with the question in case of a wrong answer.
if (!$allRight) {
repeatPage('FB02');
}
The positive message would be obsolete in this case.
===== Detailed Feedback =====
It is slightly more elaborate if you want to show the question and the wrong and right answers directly as feedback. The following PHP code first checks the answers to different questions "AB01" to "AB05" and then shows for each sub-question whether it was answered correctly or not.
// Enter the questions and the correct answers here
$answers = [
'AB01' => 1,
'AB02' => 2,
'AB03' => 2,
'AB04' => 1,
'AB05' => 1
];
// Here follows the PHP code for the display
foreach ($answers as $question => $correct) {
html('
'.getItemtext($question, 'question').'
');
$answer = value($question);
if ($antwort == $correct) {
html('✔️ '.getItemtext($question, $answer).'');
} else {
html('❌ '.getItemtext($question, $answer).'');
html('⚠️ '.getItemtext($question, $correct).'');
}
html('');
}
// Here the layout
pageCSS('
div.question { margin-bottom: 3em; }
div.question div.text { font-weight: bold; margin-bottom: 0.5em; }
div.question div.correct { margin-bottom: 0.5em; color: green; }
div.question div.wrong { margin-bottom: 0.5em; text-decoration: line-through; }
div.question div.better { margin-bottom: 0.5em; color: green; font-weight: bold; }
');
===== Many Questions =====
If a relatively large number of questions are to be presented on individual pages, then the function ''loopToPage()'' is used. The list of questions including answers is defined in an array.
In a questionnaire are two pages needed. In the following example, the first page is given the [[:en:glossary#pageidentifier|pageidentifier]] "question", the second page is given the page identifier "feedback".
On the "question" page, the list of questions and correct answers is defined. In the following example for multiple choice questions "AB02" to "AB06". By means of ''registerVariable()'' this array is also made available for page 2.
$questions = [
'AB02' => [1,2,1,2],
'AB03' => [2,2,1,1],
'AB04' => [2,1,1,2],
'AB05' => [1,2,2,1],
'AB06' => [1,1,1,2]
];
registerVariable($questions);
$i = loopToPage('feedback', count($questions));
// show question
$questionIDs = array_keys($question);
$questionID = $questionIDs[$i];
question($question);
The second page "feedback" then checks the answer and displays the feedback.
$i = loopIndex();
$questionIDs = array_keys($questions);
$questionID = $questionIDs[$i];
$correct = $questions[$questionID];
$answer = array_values(valueList($questionID));
if ($answer == $correct) {
text('FB01');
} else {
text('FB02');
}