*/
class AnswerChecker {
private $questionString;
private $correct;
private $userName;
private $sql;
public function __construct() {
$this->userName = e107::getUser()->getLoginName();
$this->sql = e107::getDb();
}
public function checkAnswers() {
//print_a('deleting session val.');
setcookie('tkQuestion', '', time()-7200);
$decodedQuestion = json_decode(base64_decode(filter_input(INPUT_POST, 'checkerData')),true);
//print_a([$_POST, $decodedQuestion]);
$int = 1;
while (filter_input(INPUT_POST, 'a'.$int)) {
$decodedQuestion['userAnswer'.$int] = filter_input(INPUT_POST, 'a'.$int);
$int++;
}
if(isset($decodedQuestion['answer1'])) {
$this->questionFormater($decodedQuestion);
if ($this->cheating($decodedQuestion['question'])) {
return 't'; //silently fail
}
$anserString = $this->checkAnswer($decodedQuestion);
//print_a(['anserString'=>$anserString]);
if($this->correct) {
$text .= "Congratulations '" . trim(trim($anserString['user']),',') . "' was correct for " . $this->questionString . "
";
}else{
$text .= "Sorry but '" . trim(trim($anserString['user']),',') . "' was incorrect it should have been '" . trim(trim($anserString['correct']),',') . "' for " . $this->questionString . "
";
}
$this->addToReport($anserString,$decodedQuestion);
$this->addRewardPoint();
//print_a(['rawPost'=>$_POST, 'decodedPost'=>$decodedQuestion,'questionString'=>$this->questionString]);
return $text;
}
}
private function cheating() {
$lastQuestion = $this->sql->retrieve('tkReport', 'question', 'username="' . $this->userName . '" ORDER BY id DESC LIMIT 1');
if ($lastQuestion == $this->questionString) {
return true;
}
return false;
}
private function checkAnswer($decodedQuestion) {
$this->correct = true;
$answers = ['correct'=>'','user'=>''];
$int = 1;
while (isset($decodedQuestion['userAnswer'.$int])) {
if($decodedQuestion['userAnswer'.$int] == $decodedQuestion['answer'.$int]) {
// true
}else{
$this->correct = false;
}
$answers['correct'] .= $decodedQuestion['answer'.$int] . ', ';
$answers['user'] .= $decodedQuestion['userAnswer'.$int] . ', ';
$int++;
}
return $answers;
}
private function questionFormater($data) {
switch ($data['type']) {
case 'numbers':
$this->numbers($data['question']);
break;
case 'spelling':
$this->spelling($data['answer1']);
break;
case 'numeracy':
$this->numeracy($data['question']);
break;
case 'math_word_problems':
$this->math_word_problems($data['question']);
break;
case 'elements':
$this->elements($data['question']);
break;
default:
$this->questionString = 'format error.';
print_a([$data['type'] => $data]);
break;
}
}
private function elements($data) {
//print_a($data);
if($data[1]=='name'){
$this->questionString = 'Find the missing information for element '.$data['element']['symbol'].'?';
}else{
$this->questionString = 'Find the missing information for element '.$data['element']['name'].'?';
}
}
private function spelling($data) {
$this->questionString = 'Spell the word \'' . $data . '\'.';
}
private function numeracy($data) {
if($data[1]=='r') {
$this->questionString = 'Round the following off to the nearest 5 cents. ' . $data[0];
}else{
$this->questionString = $data[0] . ' ' . $data[1] . ' ' . $data[2] . ' =';
}
}
private function math_word_problems($data) {
$this->questionString = $data;
}
private function numbers($data) {
$retString = '';
foreach($data as $seq) {
$retString .= $seq;
}
$this->questionString = 'Find the missing number? '.trim($retString,',');
}
private function addToReport($answers,$data) {
if($this->correct) {
$correct = 1;
}else{
$correct = 0;
}
$insert = [
'username' => $this->userName,
'question' => $this->questionString,
'answereddatestamp' => time(),
'userAnswer' => trim(trim($answers['user']),','),
'answer' => trim(trim($answers['correct']),','),
'subject' => $data['type'],
'correct' => $correct
];
//print_a(['SQLinsert'=>$insert]);
$this->sql->insert('tkReport', $insert);
}
private function addRewardPoint() {
// TODO link to minecraft/minetest engines
}
}