elements.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 elements
  9. *
  10. * @author gregory
  11. */
  12. class elements {
  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 getElements() {
  33. $this->difficultyValue = $this->difficulty();
  34. $switchValue = rand(0,(int)$this->difficultyValue/22.4);
  35. if($this->debug) {
  36. $this->returnData['difficulty'] = $this->difficultyValue;
  37. $this->returnData['switchValue'] = $switchValue;
  38. }
  39. $this->elementSwitch($switchValue);
  40. return $this->returnData;
  41. }
  42. private function elementSwitch($switchValue) {
  43. switch ($switchValue) {
  44. case 0:
  45. $this->returnData['question'] = $this->elementValues('symbol');
  46. break;
  47. case 1:
  48. $this->returnData['question'] = $this->elementValues('phase');
  49. break;
  50. case 2:
  51. $this->returnData['question'] = $this->elementValues('name');
  52. break;
  53. case 3:
  54. $this->returnData['question'] = $this->elementValues('number');
  55. break;
  56. case 4:
  57. default:
  58. $this->returnData['question'] = $this->elementValues('atomic_mass');
  59. break;
  60. }
  61. }
  62. /**
  63. * Return the difficulty based on previously answered questions.
  64. *
  65. * @return int Difficulty as percent.
  66. */
  67. private function difficulty() {
  68. $query = "SELECT (SUM(correct)/COUNT(correct)*100) as percent
  69. FROM e107.`e107_tkReport` WHERE
  70. answereddatestamp >= unix_timestamp((NOW()) - INTERVAL 1 MONTH)
  71. AND username = '$this->user' AND subject = '".__CLASS__."';";
  72. if($this->debug) {
  73. $this->returnData['difficultySQL'] = $query;
  74. }
  75. $this->sql->gen($query);
  76. while ($row = $this->sql->fetch()) {
  77. if (is_numeric($row["percent"])) {
  78. return (int)$row["percent"];
  79. } else {
  80. return 1;
  81. }
  82. }
  83. }
  84. private function elementValues($sign) {
  85. $elements = json_decode(file_get_contents('resources/periodicTable.json'), true)['elements'];
  86. $selectedElement = rand(0, (int) ($this->difficultyValue * 1.18));
  87. $TempQuestion = [];
  88. $TempQuestion['element'] = $elements[$selectedElement];
  89. $TempQuestion[] = $selectedElement;
  90. $TempQuestion[] = $sign;
  91. return [ 'answer1'=>$TempQuestion['element'][$sign], 'question'=>$TempQuestion, 'type'=>__CLASS__];
  92. }
  93. }