spelling2.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 spelling2 {
  13. private $user;
  14. /**
  15. * Setup new spelling engine
  16. *
  17. * @param bool $user user to supply question for.
  18. */
  19. public function __construct($user) {
  20. $this->user = $user;
  21. }
  22. public function getSpellingWord() {
  23. $difficulty = $this->difficulty();
  24. $question = e107::getDb()->retrieve("SELECT `word`,`usage`, IFNULL(`cnt`,'0') AS Ncnt FROM
  25. (SELECT `word`,`usage` FROM e107.e107_tk_wordList WHERE `year` <= '".ceil($difficulty)."' AND `usage` != '') AS wList LEFT JOIN
  26. (SELECT `answer`,COUNT(`answer`)AS `cnt` FROM e107.e107_tkReport
  27. WHERE `answereddatestamp` > unix_timestamp((NOW()) - INTERVAL 1 MONTH) AND `username` = '".$this->user."'
  28. AND `subject` = 'spelling' GROUP BY `answer` ORDER BY COUNT(`answer`)) AS Ans
  29. ON wList.`word` = Ans.`answer` ORDER BY IFNULL(`cnt`,'0'), RAND() LIMIT 1;");
  30. $questionHtml = $this->spellingHtml($question);
  31. e107::getDb()->insert('tkLastQuestion', ['user'=>$this->user,'LastQuestionHtml'=>base64_encode($questionHtml)]);
  32. e107::getRender()->tablerender('Spelling', $questionHtml);
  33. }
  34. /**
  35. * Create a spelling form using the supplied question.
  36. * And write it into "$this->returnQuestion".
  37. *
  38. * @param array() $data Question data to be used.
  39. */
  40. private function spellingHtml($data) {
  41. $tp = e107::getParser();
  42. $frm = e107::getForm();
  43. $fString = $frm->open('questionForm', 'post', '', ['autocomplete' => 'off', 'class' => 'formclass']);
  44. $fString .= $frm->label('<h4>Spelling</h4>') . "</br>\n";
  45. $fString .= $frm->label('Spell the word in the sentence.') . "\n";
  46. $fString .= '</br>';
  47. //if (!file_exists('./audio/' . md5($data['word']) . '3v.wav')) {
  48. //exec('./audio/flite-2.0.0-release/bin/flite -voice ./audio/voices/cmu_us_aew.flitevox -o "./audio/' . md5($data['word']) . '3v.wav" "Spell the word, ' . $tp->toHTML($data['word']) . '. ' . $tp->toHTML($data['usage']) . '"');
  49. //}
  50. if (!file_exists('./audio/' . md5($data['word']) . '.mp3')) {
  51. $voices = ['bf_emma','bf_isabella','bf_alice','bf_lily','bm_george','bm_lewis','bm_daniel','bm_fable'];
  52. $voice = $voices[array_rand($voices,1)];
  53. exec('curl http://192.168.1.252:8880/v1/audio/speech -H "Content-Type: application/json" -d \'{"model":"tts-1","input":"Spell the word, ' . $tp->toHTML($data['word']) . '. ' . $tp->toHTML($data['usage']) . '.","voice":"'.$voice.'"}\' --output ./audio/' . md5($data['word']) . '.mp3');
  54. }
  55. $fString .= "<audio controls><source src='audio/" . md5($data['word']) . ".mp3' type='audio/wav'>Your browser does not support the audio element.</audio>\n";
  56. $fString .= '</br></br>';
  57. $fString .= $frm->text('a1', '', 80, ['placeholder' => 'Write answer here!', 'autocomplete' => "off", 'spellcheck' => "false"]) . "\n";
  58. $fString .= $frm->hidden('checkerData', TkAnswerStore::issue($this->user, $data), ['id' => 'checkerData']);
  59. $fString .= $frm->hidden('subject', 'spelling', ['id' => 'subject']);
  60. $fString .= '</br>' . "\n";
  61. $fString .= e107::getForm()->button('Answer', 'Check answer') . "\n";
  62. $fString .= e107::getForm()->close();
  63. $fString .= '</br></br>';
  64. return $fString;
  65. }
  66. /**
  67. * Return the difficulty based on previously answered questions.
  68. *
  69. * @return array['percent','count'] Difficulty 1-13, Number of questions answered.
  70. */
  71. private function difficulty() {
  72. $stats = e107::getDb()->retrieve("SELECT (SUM(correct)/COUNT(correct)*13) as percent, COUNT(correct) as count
  73. FROM e107.`e107_tkReport` WHERE
  74. answereddatestamp >= unix_timestamp((NOW()) - INTERVAL 1 MONTH)
  75. AND `username` = '$this->user' AND `subject` = 'spelling';"
  76. );
  77. if($stats["count"]>=300) {
  78. return $stats["percent"];
  79. } elseif($stats["count"]>=250) {
  80. return min($stats["percent"],7);
  81. } elseif($stats["count"]>=200) {
  82. return min($stats["percent"],5);
  83. } elseif($stats["count"]>=100) {
  84. return min($stats["percent"],3);
  85. } else {
  86. return 1;
  87. }
  88. }
  89. }