| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- * Description of spelling
- *
- * @author gregory
- */
- class spelling2 {
- private $user;
- /**
- * Setup new spelling engine
- *
- * @param bool $user user to supply question for.
- */
- public function __construct($user) {
- $this->user = $user;
- }
- public function getSpellingWord() {
- $difficulty = $this->difficulty();
-
- $question = e107::getDb()->retrieve("SELECT `word`,`usage`, IFNULL(`cnt`,'0') AS Ncnt FROM
- (SELECT `word`,`usage` FROM e107.e107_tk_wordList WHERE `year` <= '".ceil($difficulty)."' AND `usage` != '') AS wList LEFT JOIN
- (SELECT `answer`,COUNT(`answer`)AS `cnt` FROM e107.e107_tkReport
- WHERE `answereddatestamp` > unix_timestamp((NOW()) - INTERVAL 1 MONTH) AND `username` = '".$this->user."'
- AND `subject` = 'spelling' GROUP BY `answer` ORDER BY COUNT(`answer`)) AS Ans
- ON wList.`word` = Ans.`answer` ORDER BY IFNULL(`cnt`,'0'), RAND() LIMIT 1;");
-
- $questionHtml = $this->spellingHtml($question);
-
- e107::getDb()->insert('tkLastQuestion', ['user'=>$this->user,'LastQuestionHtml'=>base64_encode($questionHtml)]);
-
- e107::getRender()->tablerender('Spelling', $questionHtml);
- }
- /**
- * Create a spelling form using the supplied question.
- * And write it into "$this->returnQuestion".
- *
- * @param array() $data Question data to be used.
- */
- private function spellingHtml($data) {
-
- $tp = e107::getParser();
- $frm = e107::getForm();
- $fString = $frm->open('questionForm', 'post', '', ['autocomplete' => 'off', 'class' => 'formclass']);
- $fString .= $frm->label('<h4>Spelling</h4>') . "</br>\n";
- $fString .= $frm->label('Spell the word in the sentence.') . "\n";
- $fString .= '</br>';
- //if (!file_exists('./audio/' . md5($data['word']) . '3v.wav')) {
- //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']) . '"');
- //}
-
- if (!file_exists('./audio/' . md5($data['word']) . '.mp3')) {
- $voices = ['bf_emma','bf_isabella','bf_alice','bf_lily','bm_george','bm_lewis','bm_daniel','bm_fable'];
- $voice = $voices[array_rand($voices,1)];
- 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');
- }
- $fString .= "<audio controls><source src='audio/" . md5($data['word']) . ".mp3' type='audio/wav'>Your browser does not support the audio element.</audio>\n";
- $fString .= '</br></br>';
- $fString .= $frm->text('a1', '', 80, ['placeholder' => 'Write answer here!', 'autocomplete' => "off", 'spellcheck' => "false"]) . "\n";
- $fString .= $frm->hidden('checkerData', TkAnswerStore::issue($this->user, $data), ['id' => 'checkerData']);
- $fString .= $frm->hidden('subject', 'spelling', ['id' => 'subject']);
- $fString .= '</br>' . "\n";
- $fString .= e107::getForm()->button('Answer', 'Check answer') . "\n";
- $fString .= e107::getForm()->close();
- $fString .= '</br></br>';
-
- return $fString;
- }
-
- /**
- * Return the difficulty based on previously answered questions.
- *
- * @return array['percent','count'] Difficulty 1-13, Number of questions answered.
- */
- private function difficulty() {
- $stats = e107::getDb()->retrieve("SELECT (SUM(correct)/COUNT(correct)*13) as percent, COUNT(correct) as count
- FROM e107.`e107_tkReport` WHERE
- answereddatestamp >= unix_timestamp((NOW()) - INTERVAL 1 MONTH)
- AND `username` = '$this->user' AND `subject` = 'spelling';"
- );
-
- if($stats["count"]>=300) {
- return $stats["percent"];
- } elseif($stats["count"]>=250) {
- return min($stats["percent"],7);
- } elseif($stats["count"]>=200) {
- return min($stats["percent"],5);
- } elseif($stats["count"]>=100) {
- return min($stats["percent"],3);
- } else {
- return 1;
- }
- }
- }
|