spelling.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 spelling
  9. *
  10. * @author gregory
  11. */
  12. class spelling {
  13. public $state = "error";
  14. private $debug = false;
  15. private $sql;
  16. private $user;
  17. private $returnData;
  18. /**
  19. * Setup new spelling engine
  20. *
  21. * @param bool $test Enable debug mode.
  22. */
  23. public function __construct($test = false) {
  24. if ($test) {
  25. $this->debug = true;
  26. }
  27. $this->sql = e107::getDB();
  28. $this->user = e107::getUser()->getLoginName();
  29. $this->state = "ready";
  30. }
  31. public function getSpellingWord() {
  32. $difficulty = $this->difficulty();
  33. $query = "SELECT `word`,`usage`, IFNULL(`cnt`,'0') AS Ncnt FROM
  34. (SELECT `word`,`usage` FROM e107.e107_tk_wordList WHERE `year` <= '$difficulty' AND `usage` != '') AS wList LEFT JOIN
  35. (SELECT `answer`,COUNT(`answer`)AS `cnt` FROM e107.e107_tkReport
  36. WHERE `answereddatestamp` > unix_timestamp((NOW()) - INTERVAL 1 MONTH) AND `username` = '$this->user'
  37. AND `subject` = '".__CLASS__."' GROUP BY `answer` ORDER BY COUNT(`answer`)) AS Ans
  38. ON wList.`word` = Ans.`answer`
  39. ORDER BY IFNULL(`cnt`,'0'), RAND() LIMIT 1;";
  40. if($this->debug) {
  41. $this->returnData['difficulty'] = $difficulty;
  42. $this->returnData['getWordSQL'] = $query;
  43. }
  44. $this->sql->gen($query);
  45. while ($row = $this->sql->fetch()) {
  46. $this->returnData['question'] = ['answer1' => $row['word'], 'question' => $row['usage'], 'type'=>__CLASS__];
  47. }
  48. return $this->returnData;
  49. }
  50. /**
  51. * Return the difficulty based on previously answered questions.
  52. *
  53. * @return int Difficulty 1-13.
  54. */
  55. private function difficulty() {
  56. $query = "SELECT (SUM(correct)/COUNT(correct)*13) as percent
  57. FROM e107.`e107_tkReport` WHERE
  58. answereddatestamp >= unix_timestamp((NOW()) - INTERVAL 1 MONTH)
  59. AND `username` = '$this->user' AND `subject` = '".__CLASS__."';";
  60. if($this->debug) {
  61. $this->returnData['difficultySQL'] = $query;
  62. }
  63. $this->sql->gen($query);
  64. while ($row = $this->sql->fetch()) {
  65. //zprint_a(['PercentIs'=>$row["percent"]]);
  66. if (is_numeric($row["percent"])) {
  67. return $row["percent"];
  68. } else {
  69. return 1;
  70. }
  71. }
  72. }
  73. }