예제 #1
0
 public function actionIndex()
 {
     if (Yii::$app->getRequest()->getMethod() === 'OPTIONS') {
         return true;
     }
     return new ActiveDataProvider(['query' => Dictionary::find(), 'pagination' => ['pageSize' => 1]]);
 }
예제 #2
0
 public function actionSubmit()
 {
     $result = [];
     $request = Yii::$app->request;
     $username = $request->post('username');
     $testId = $request->post('testId');
     $word = $request->post('word');
     $translation = $request->post('translation');
     $order = $request->post('order');
     $user = Users::findByUsername($username);
     $test = Test::findOne($testId);
     if ($order) {
         $dictionary = Dictionary::findOne(['translation' => $word]);
     } else {
         $dictionary = Dictionary::findOne(['word' => $word]);
     }
     if (!$user || !$test) {
         return ['error' => true, 'message' => 'Either the user or test not found'];
     }
     $error = $order ? $dictionary->word !== $translation : $dictionary->translation !== $translation;
     if ($error) {
         // TODO: change this var name, it's ambiguous
         $result['error'] = $error;
     }
     if (TestRecords::hasMaxErrors($testId)) {
         $result['maxErrors'] = true;
         return $result;
     }
     // TODO: transaction here
     $testRecord = new TestRecords();
     $testRecord->attributes = ['user_id' => $user->id, 'test_id' => $testId, 'dictionary_id' => $dictionary->id, 'order' => $order, 'is_error' => (int) $error, 'wrong_translation' => $error ? $translation : null];
     $testRecord->save();
     // Check once again for max errors after the save
     if (TestRecords::hasMaxErrors($testId)) {
         $result['maxErrors'] = true;
         return $result;
     }
     if (!$error) {
         // Increase the score by 1
         $test->scoreUp();
     }
     return $result;
 }
예제 #3
0
 public function actionDictionary($seqLength = 4, $appearance = 1)
 {
     $this->seqLength = $seqLength;
     $this->appearance = $appearance;
     $startTime = microtime(true);
     $x = 0;
     $dictionary = Dictionary::find()->where(['>', 'LENGTH(word)', $seqLength])->all();
     Sequence::deleteAll();
     foreach ($dictionary as $entry) {
         if ($x % 1000 == 0) {
             echo "{$x}\n";
         }
         $x++;
         // Some simple checks on our word
         if ($this->checkWordLength($entry->word) && $this->checkSeqPossible($entry->word)) {
             // everything OK so lets continue
             for ($i = 0; $i + $this->seqLength - 1 < strlen($entry->word); $i++) {
                 $str = strtolower(substr($entry->word, $i, $this->seqLength));
                 if (preg_match('/\'|[0-9]/', $str) === 0) {
                     $exists = Sequence::find()->where(['like', 'letters', $str])->exists();
                     if (!$exists) {
                         $count = Dictionary::find()->where(['like', 'word', $str])->count();
                         $seq = new Sequence();
                         $seq->letters = $str;
                         $seq->count = $count;
                         $seq->word = $entry->word;
                         $seq->save();
                     }
                     unset($exists);
                 }
             }
         }
     }
     echo "Elapsed time is: " . (microtime(true) - $startTime) . " seconds \n";
     // var_dump($dictionary);
 }
예제 #4
0
 public function compareWord($word)
 {
     $correct = 0;
     $word = strtoupper($word);
     if (\App\Models\Dictionary::CompareWord($word) != 0) {
         $correct = 1;
     }
     return $correct;
 }
예제 #5
0
 public function rules()
 {
     return [[['word_id', 'user_id', 'answer', 'type'], 'required'], [['answer'], 'string'], [['type'], 'in', 'range' => Dictionary::typesTranslate()], [['word_id'], 'exist', 'targetClass' => '\\app\\models\\Dictionary', 'targetAttribute' => 'id']];
 }
예제 #6
0
 public function actionFoo()
 {
     return new ActiveDataProvider([
         'query' => Dictionary::find()->where(['like','word','abb']),
     ]);
 }
예제 #7
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getDictionary()
 {
     return $this->hasOne(Dictionary::className(), ['id' => 'dictionary_id']);
 }
 public static function check_word($word)
 {
     $count = \App\Models\Dictionary::where('word', '=', strtolower($word))->count();
     if ($count == 1) {
         return true;
     } else {
         return false;
     }
 }