numbers.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 numbers
  9. *
  10. * @author gregory
  11. */
  12. class numbers {
  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 getNumbers() {
  33. $this->difficultyValue = $this->difficulty();
  34. if($this->debug) {
  35. $this->returnData['difficulty'] = $this->difficultyValue;
  36. }
  37. $this->returnData['question'] = $this->getSequence();
  38. return $this->returnData;
  39. }
  40. /**
  41. * Return the difficulty based on previously answered questions.
  42. *
  43. * @return int Difficulty as percent.
  44. */
  45. private function difficulty() {
  46. $query = "SELECT (SUM(correct)/COUNT(correct)*100) as percent
  47. FROM e107.`e107_tkReport` WHERE
  48. answereddatestamp >= unix_timestamp((NOW()) - INTERVAL 1 MONTH)
  49. AND username = '$this->user' AND subject = '".__CLASS__."';";
  50. if($this->debug) {
  51. $this->returnData['difficultySQL'] = $query;
  52. }
  53. $this->sql->gen($query);
  54. while ($row = $this->sql->fetch()) {
  55. if (is_numeric($row["percent"])) {
  56. return (int)$row["percent"];
  57. } else {
  58. return 1;
  59. }
  60. }
  61. }
  62. private function getSequence() {
  63. $startingNumber = rand(0, (int) ($this->difficultyValue * ($this->difficultyValue/5)));
  64. $incrementalNumber = rand(1, (int) ($this->difficultyValue * ($this->difficultyValue/10)));
  65. $missingNumber = rand(1, 10);
  66. $TempQuestion = [];
  67. for ($i = 1; $i <= 10; $i++) {
  68. if ($i == $missingNumber) {
  69. $TempQuestion[] = "___,";
  70. $solution = $startingNumber;
  71. } elseif ($i == 10) {
  72. $TempQuestion[] = $startingNumber;
  73. } else {
  74. $TempQuestion[] = $startingNumber . ",";
  75. }
  76. $startingNumber += $incrementalNumber;
  77. }
  78. return [ 'answer1'=>$solution, 'question'=>$TempQuestion, 'type'=>__CLASS__];
  79. }
  80. }