AnswerChecker.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. /**
  8. * This class contains the code for checking the user supplied answers
  9. * @author Gregory Huxley <pigs.fly.in.packs@gmail.com>
  10. */
  11. class AnswerChecker {
  12. private $questionString;
  13. private $correct;
  14. private $userName;
  15. private $sql;
  16. public function __construct() {
  17. $this->userName = e107::getUser()->getLoginName();
  18. $this->sql = e107::getDb();
  19. }
  20. public function checkAnswers() {
  21. //print_a('deleting session val.');
  22. setcookie('tkQuestion', '', time()-7200);
  23. $decodedQuestion = json_decode(base64_decode(filter_input(INPUT_POST, 'checkerData')),true);
  24. //print_a([$_POST, $decodedQuestion]);
  25. $int = 1;
  26. while (filter_input(INPUT_POST, 'a'.$int)) {
  27. $decodedQuestion['userAnswer'.$int] = filter_input(INPUT_POST, 'a'.$int);
  28. $int++;
  29. }
  30. if(isset($decodedQuestion['answer1'])) {
  31. $this->questionFormater($decodedQuestion);
  32. if ($this->cheating($decodedQuestion['question'])) {
  33. return 't'; //silently fail
  34. }
  35. $anserString = $this->checkAnswer($decodedQuestion);
  36. //print_a(['anserString'=>$anserString]);
  37. if($this->correct) {
  38. $text .= "Congratulations '" . trim(trim($anserString['user']),',') . "' was correct for " . $this->questionString . "<br/>";
  39. }else{
  40. $text .= "Sorry but '" . trim(trim($anserString['user']),',') . "' was incorrect it should have been '" . trim(trim($anserString['correct']),',') . "' for " . $this->questionString . "<br/>";
  41. }
  42. $this->addToReport($anserString,$decodedQuestion);
  43. $this->addRewardPoint();
  44. //print_a(['rawPost'=>$_POST, 'decodedPost'=>$decodedQuestion,'questionString'=>$this->questionString]);
  45. return $text;
  46. }
  47. }
  48. private function cheating() {
  49. $lastQuestion = $this->sql->retrieve('tkReport', 'question', 'username="' . $this->userName . '" ORDER BY id DESC LIMIT 1');
  50. if ($lastQuestion == $this->questionString) {
  51. return true;
  52. }
  53. return false;
  54. }
  55. private function checkAnswer($decodedQuestion) {
  56. $this->correct = true;
  57. $answers = ['correct'=>'','user'=>''];
  58. $int = 1;
  59. while (isset($decodedQuestion['userAnswer'.$int])) {
  60. if($decodedQuestion['userAnswer'.$int] == $decodedQuestion['answer'.$int]) {
  61. // true
  62. }else{
  63. $this->correct = false;
  64. }
  65. $answers['correct'] .= $decodedQuestion['answer'.$int] . ', ';
  66. $answers['user'] .= $decodedQuestion['userAnswer'.$int] . ', ';
  67. $int++;
  68. }
  69. return $answers;
  70. }
  71. private function questionFormater($data) {
  72. switch ($data['type']) {
  73. case 'numbers':
  74. $this->numbers($data['question']);
  75. break;
  76. case 'spelling':
  77. $this->spelling($data['answer1']);
  78. break;
  79. case 'numeracy':
  80. $this->numeracy($data['question']);
  81. break;
  82. case 'math_word_problems':
  83. $this->math_word_problems($data['question']);
  84. break;
  85. case 'elements':
  86. $this->elements($data['question']);
  87. break;
  88. default:
  89. $this->questionString = 'format error.';
  90. print_a([$data['type'] => $data]);
  91. break;
  92. }
  93. }
  94. private function elements($data) {
  95. //print_a($data);
  96. if($data[1]=='name'){
  97. $this->questionString = 'Find the missing information for element '.$data['element']['symbol'].'?';
  98. }else{
  99. $this->questionString = 'Find the missing information for element '.$data['element']['name'].'?';
  100. }
  101. }
  102. private function spelling($data) {
  103. $this->questionString = 'Spell the word \'' . $data . '\'.';
  104. }
  105. private function numeracy($data) {
  106. if($data[1]=='r') {
  107. $this->questionString = 'Round the following off to the nearest 5 cents. ' . $data[0];
  108. }else{
  109. $this->questionString = $data[0] . ' ' . $data[1] . ' ' . $data[2] . ' =';
  110. }
  111. }
  112. private function math_word_problems($data) {
  113. $this->questionString = $data;
  114. }
  115. private function numbers($data) {
  116. $retString = '';
  117. foreach($data as $seq) {
  118. $retString .= $seq;
  119. }
  120. $this->questionString = 'Find the missing number? '.trim($retString,',');
  121. }
  122. private function addToReport($answers,$data) {
  123. if($this->correct) {
  124. $correct = 1;
  125. }else{
  126. $correct = 0;
  127. }
  128. $insert = [
  129. 'username' => $this->userName,
  130. 'question' => $this->questionString,
  131. 'answereddatestamp' => time(),
  132. 'userAnswer' => trim(trim($answers['user']),','),
  133. 'answer' => trim(trim($answers['correct']),','),
  134. 'subject' => $data['type'],
  135. 'correct' => $correct
  136. ];
  137. //print_a(['SQLinsert'=>$insert]);
  138. $this->sql->insert('tkReport', $insert);
  139. }
  140. private function addRewardPoint() {
  141. // TODO link to minecraft/minetest engines
  142. }
  143. }