Show correct answer

More
7 years 5 months ago #41389 by idolyz
idolyz created the topic: Show correct answer
Hi,

I've been customising a LimeSurvey installation over the past few days, where I'm turning the surveys into assessments (so I've disabled the previous button, show question by question, and flag a correct answer).

I have the requirement to display the answer the user chose and the correct answer on the page as soon as they hit the next button and before it goes to the next page (like the mandatory error that gets displayed), but I am having troubles getting the function to work and where to put the code.

This is as far as I have got:

I added this code in red on the question.php script:
if (isset($notanswered) && $notanswered!=false) {
list($mandatorypopup, $popup)=mandatory_popup($ia, $notanswered);
} elseif (isset($move) && $move == "movenext") {
showcorrect_popup($ia[0]);
}


and this code in red on the qanda.php script:

function showcorrect_popup($ia) {
// Show Correct Answer
$aquery = "SELECT answer FROM {$dbprefix}answers WHERE qid=".$ia[0]." ORDER BY code ASC LIMIT 1"; // Correct answer will be the first option (but it gets displayed randomly, so users don't know this)
$aresult = db_execute_assoc($aquery); //Checked
$arow = $aresult->FetchRow();
$qtitle = "<strong><br /><span class='errormandatory'>Correct answer is: ".$arow."</span></strong><br />";
}


Has anyone attempted this before? and if so, could you please provide me with an example or point me in the right direction?

Appreciate any help,
Ben

Please Log in to join the conversation.

More
7 years 5 months ago - 7 years 5 months ago #41676 by idolyz
idolyz replied the topic: Show correct answer
I have coded a solution. I edited the question.php page with the following...

I created this function:

// *****************************************************
function showcorrect_popup($prevquestion, $prevanswer) {
global $dbprefix;
global $correctaanswerpopup, $capopup, $clang;
// Set correct answers session variable
if (!isset($_SESSION)) {
$_SESSION = "0";
}
// A1 is the code of the answer, and A1 will always be the correct answer
//$prevanswer = {INSERTANS:SIDXGIDXQID};
if ($prevanswer == "A1") {
$_SESSION++;
$capopup = "<script type=\"text/javascript\">\n
alert(\"".$clang->gT("Correct", "js")."\");\n
</script>";
} else {
// Show Correct Answer
$aquery = "SELECT answer FROM {$dbprefix}answers WHERE qid=".$prevquestion[0]." ORDER BY code ASC LIMIT 1";
$aresult = db_execute_assoc($aquery); //Checked
while ($arow = $aresult->FetchRow()) {
$capopup = "<script type=\"text/javascript\">\n
alert(\"".$clang->gT("The correct answer is: ".$arow.". Continue", "js")."\");\n
</script>";
}
}
$correctaanswerpopup="Y";
return array($correctaanswerpopup, $capopup);
}
// *****************************************************


And i call it just after the mandatory check:

// Display the "mandatory" popup if necessary
if (isset($notanswered) && $notanswered!=false) {
list($mandatorypopup, $popup) = mandatory_popup($ia, $notanswered);
// Show answer to previous question
} elseif ((isset($move)) && (($move == "movenext") || ($move == "movesubmit"))) {
// These variables are to show the variables for current question, step, etc.
/*print_r($_SESSION[$currentquestion-1]);
echo '<br />';
echo '$currentquestion: '.$currentquestion.'<br />';
echo '$_SESSION[step]: '.$_SESSION.'<br />';
echo '$thisstep: '.$thisstep.'<br />';
echo '$_POST[grpdesc]: '.$_POST.'<br />';
echo '$newgroup: '.$newgroup.'<br />';*/
// If not a new group and $_POST[grpdesc] doesn't exist, call the function
$prevquestion = $_SESSION[$currentquestion-1];
$prevanswer = $_SESSION["$prevquestion[1]"];
if (($newgroup == "N") && (!isset($_POST))) {
list($correctanswerpopup, $capopup) = showcorrect_popup($prevquestion,$prevanswer); // Display alert
}
}


I hope this helps someone out, as it took me a few days to get my head around the system.
Last Edit: 7 years 5 months ago by idolyz.

Please Log in to join the conversation.

More
7 years 5 months ago #41763 by Mazi
Mazi replied the topic: Show correct answer
This seems to be a VERY useful feature!

Can you please open a ticket at our Bugtracker and attach your files (.diff patch would be great) there?! One of our developers will then take a look at it and give some feedback in order to integrate this nice add-on into the core.
This has the advantage that you are future save when updating and you will help lots of users looking for such a feature!

Thanks!


Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in to join the conversation.

More
6 years 3 months ago #62506 by sgaucho
sgaucho replied the topic: Show correct answer
Hi,

Has this Show Results feature been integrated into version 1.91 yet?

Thanks!
sg

Please Log in to join the conversation.

More
8 months 3 weeks ago #147550 by ggerber
ggerber replied the topic: Show correct answer
Hello,

I am wondering if this functionality has yet been added to Limesurvey's functionality? I am running version 2.59 but cannot find the functionality anywhere... perhaps it is available now and I can't find it?

Any information, or if you could point me in the appropriate direction - very much appreciated!
Thanks
GG

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now