Skip to content

Instantly share code, notes, and snippets.

@boxmein
Last active August 29, 2015 14:09
Show Gist options
  • Save boxmein/f3cd92b5366eeb3823cb to your computer and use it in GitHub Desktop.
Save boxmein/f3cd92b5366eeb3823cb to your computer and use it in GitHub Desktop.
Tiny one-off quiz engine for Fast-Driver
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>quiz</title>
</head>
<body>
<!-- Something like this -->
<select class="question" data-id="1">
<option value="1">Wrong</option>
<option value="2">Wrong</option>
<option value="3">Right</option>
</select>
<select class="question" data-id="2">
<option value="1">Right</option>
<option value="2">Wrong</option>
<option value="3">Wrong</option>
</select>
<button id="a">submit</button>
<script src="zclientside.js"></script>
<script>
document.getElementById('a').onclick = function() {
// Step 1. Collect answers
var questions = [];
var answers = [];
for (var i = 0,
qs = document.getElementsByClassName("question");
i<qs.length;
i++) {
// note: untested
if (qs[i].type && qs[i].type == 'radio') {
// find the checked radio box, and its value
var ds = document.querySelectorAll('input[name="'+i.name+'"]');
for (var d = 0; d < ds.length; d++)
if (ds[d].checked)
qs[i].value = ds[d].value;
}
questions.push(qs[i].getAttribute('data-id'));
answers.push(qs[i].value);
}
console.log(questions, answers);
answerForOne(questions[0], answers[0], function(q, right) {
console.log("results for answerForOne", q, right);
alert("Question #" + q + " was " + (!right?"in":'')+"correct");
});
answerForAll(questions, answers, function(qs, rights) {
console.log("results for answerForAll", qs, rights);
for (var i = 0; i < qs.length; i++) {
alert("Question #"+qs[i]+" was " + (!rights[i]?'in':'')+"correct");
}
});
};
</script>
</body>
</html>
// Submit a question with number N and answer ID a
// And a callback cb that receives if the answer was indeed correct.
function answerForOne(n, a, cb) {
var x = new XMLHttpRequest();
x.open('GET', 'zengine.php?single&question='+n+'&answer='+a);
x.onreadystatechange = function() {
if (x.readyState == 4) {
if (x.status == 200) {
if (/^true/.test(x.responseText))
return cb(n, true);
console.log(x.responseText);
cb(n, false);
}
}
};
x.send();
}
// Submit multiple questions with numbers in ns and respective answers in as
// And as always, a callback in cb
function answerForAll(ns, as, cb) {
ns = ns.join(',');
as = as.join(',');
var x = new XMLHttpRequest();
x.open('GET', 'zengine.php?multiple&questions='+ns+'&answers='+as);
x.onreadystatechange = function() {
if (x.readyState == 4) {
if (x.status == 200) {
var rights = x.responseText.split(',');
for (var i = 0; i<rights.length;i++) {
if (rights[i] == "true")
rights[i] = true;
else
rights[i] = false;
}
return cb(ns.split(','), rights);
}
}
};
x.send();
}
<?php
// Quiz? engine in PHP
// by boxmein 2014
// Set this array to configure questions and answers.
$answers = array(
// Question 1: Answer 3 is correct!
1 => 3,
// Question 2: Answer 1 is correct!
2 => 1
);
function isValidAnswer($q, $a) {
global $answers;
return $answers[$q] == $a;
}
function strify($a) {
return $a ? "true" : "false";
}
// You could do the same with a MySQL database or something similar, but this
// is way simpler
if (isset($_GET['single'])) {
if (!isset($_GET['question']))
die();
if (!isset($_GET['answer']))
die();
if (!array_key_exists( intval($_GET['question'], 10), $answers ))
die();
if ($answers[intval($_GET['question'], 10)] == intval($_GET['answer'], 10))
echo 'true';
}
elseif (isset($_GET['multiple'])) {
if (!isset($_GET['questions']))
die();
if (!isset($_GET['answers']))
die();
$qs = explode(',', $_GET['questions']);
$as = explode(',', $_GET['answers']);
$qs = array_map('intval', $qs);
$as = array_map('intval', $as);
$ans = array_map('isValidAnswer', $qs, $as);
echo implode(',', array_map('strify', $ans));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment