numeracy.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. * Description of numeracy
  9. *
  10. * @author gregory
  11. */
  12. class numeracy {
  13. public $state = "error";
  14. private $debug = false;
  15. private $sql;
  16. private $user;
  17. private $difficultyValue;
  18. private $returnData;
  19. /**
  20. * Setup new numbers engine
  21. *
  22. * @param bool $test Enable debug mode.
  23. */
  24. public function __construct($test = false) {
  25. if ($test) {
  26. $this->debug = true;
  27. }
  28. $this->sql = e107::getDB();
  29. $this->user = e107::getUser()->getLoginName();
  30. $this->state = "ready";
  31. }
  32. public function getNumeracy() {
  33. $this->difficultyValue = $this->difficulty();
  34. $switchValue = rand(0,(int)$this->difficultyValue/16.6);
  35. if($this->debug) {
  36. $this->returnData['difficulty'] = $this->difficultyValue;
  37. $this->returnData['switchValue'] = $switchValue;
  38. }
  39. $this->numeracySwitch($switchValue);
  40. return $this->returnData;
  41. }
  42. private function numeracySwitch($switchValue) {
  43. switch ($switchValue) {
  44. case 0:
  45. $this->returnData['question'] = $this->basic('+');
  46. break;
  47. case 1:
  48. $this->returnData['question'] = $this->basic('-');
  49. break;
  50. case 2:
  51. $this->returnData['question'] = $this->basic('x');
  52. break;
  53. case 3:
  54. $this->returnData['question'] = $this->Rounding();
  55. break;
  56. case 4:
  57. case 5:
  58. $this->returnData['question'] = $this->Division();
  59. break;
  60. default:
  61. break;
  62. }
  63. }
  64. /**
  65. * Return the difficulty based on previously answered questions.
  66. *
  67. * @return int Difficulty as percent.
  68. */
  69. private function difficulty() {
  70. $query = "SELECT (SUM(correct)/COUNT(correct)*100) as percent
  71. FROM e107.`e107_tkReport` WHERE
  72. answereddatestamp >= unix_timestamp((NOW()) - INTERVAL 1 MONTH)
  73. AND username = '$this->user' AND subject = '".__CLASS__."';";
  74. if($this->debug) {
  75. $this->returnData['difficultySQL'] = $query;
  76. }
  77. $this->sql->gen($query);
  78. while ($row = $this->sql->fetch()) {
  79. if (is_numeric($row["percent"])) {
  80. return (int)$row["percent"];
  81. } else {
  82. return 1;
  83. }
  84. }
  85. }
  86. private function basic($sign) {
  87. $firstNumber = rand(1, (int) ($this->difficultyValue * ($this->difficultyValue/5)));
  88. $secondNumber = rand(1, (int) ($this->difficultyValue * ($this->difficultyValue/5)));
  89. $TempQuestion = [];
  90. $TempQuestion[] = $firstNumber;
  91. $TempQuestion[] = $sign;
  92. $TempQuestion[] = $secondNumber;
  93. $solution['+'] = ($firstNumber + $secondNumber);
  94. $solution['-'] = ($firstNumber - $secondNumber);
  95. $solution['x'] = ($firstNumber * $secondNumber);
  96. return [ 'answer1'=>$solution[$sign], 'question'=>$TempQuestion, 'type'=>__CLASS__];
  97. }
  98. private function Division() {
  99. $firstNumber = rand(1, (int) ($this->difficultyValue * ($this->difficultyValue/13)));
  100. $secondNumber = rand(1, (int) ($this->difficultyValue * ($this->difficultyValue/13)));
  101. $divNumber = $firstNumber * $secondNumber;
  102. $TempQuestion = [];
  103. $TempQuestion[] = $divNumber;
  104. $TempQuestion[] = '/';
  105. $TempQuestion[] = $secondNumber;
  106. $solution['/'] = ($divNumber / $secondNumber);
  107. return [ 'answer1'=>$solution['/'], 'question'=>$TempQuestion, 'type'=>__CLASS__];
  108. }
  109. private function Rounding() {
  110. $firstNumber = sprintf("%01.2f", (float) rand(100, (int) ($this->difficultyValue) * 100) / 100);
  111. $TempQuestion = [];
  112. $TempQuestion[] = $firstNumber;
  113. $TempQuestion[] = 'r';
  114. $solution['r'] = (sprintf("%01.2f", round($firstNumber / 5, 2) * 5));
  115. return [ 'answer1'=>$solution['r'], 'question'=>$TempQuestion, 'type'=>__CLASS__];
  116. }
  117. }