| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- * This class contains the code for checking the user supplied answers
- * @author Gregory Huxley <pigs.fly.in.packs@gmail.com>
- */
- 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 . "<br/>";
- }else{
- $text .= "Sorry but '" . trim(trim($anserString['user']),',') . "' was incorrect it should have been '" . trim(trim($anserString['correct']),',') . "' for " . $this->questionString . "<br/>";
- }
-
- $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
- }
-
- }
|