Welcome, Guest
Username: Password: Remember me

TOPIC: Show correct answer

Show correct answer 4 years 6 months ago #41389

  • idolyz
  • idolyz's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 2
  • Karma: 0
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
The administrator has disabled public write access.

Re:Show correct answer 4 years 5 months ago #41676

  • idolyz
  • idolyz's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 2
  • Karma: 0
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: 4 years 5 months ago by idolyz.
The administrator has disabled public write access.

Re:Show correct answer 4 years 5 months ago #41763

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5333
  • Thank you received: 296
  • Karma: 249
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)limesurvey.org'"
The administrator has disabled public write access.

Re:Show correct answer 3 years 4 months ago #62506

  • sgaucho
  • sgaucho's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 7
  • Karma: 0
Hi,

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

Thanks!
sg
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.113 seconds
Donation Image