public function actionCheck()
 {
     $result = [];
     $request = Yii::$app->request;
     $test_id = $request->post('test_id');
     $word_id = $request->post('word_id');
     $answer_id = $request->post('answer_id');
     $language = $request->post('language');
     $username = $request->post('username');
     $user = User::findByUsername($username);
     $test = Test::findOne($test_id);
     $dictionaries = Dictionary::findAll([$language . '_id' => $word_id]);
     if (!$user || !$test) {
         return ['error' => true, 'message' => 'Either the user or test not found'];
     }
     foreach ($dictionaries as $dictionary) {
         $error = $language == 'ru' ? $dictionary->en_id != $answer_id : $dictionary->ru_id != $answer_id;
         if ($error) {
             continue;
         } else {
             break;
         }
     }
     if ($error) {
         $result['error'] = $error;
     }
     if (Records::hasMaxErrors($test_id)) {
         $result['maxErrors'] = true;
         return $result;
     }
     $lang_record = Languages::findOne(['lang' => $language]);
     $testRecord = new Records();
     $testRecord->attributes = ['test_id' => $test_id, 'dictionary_id' => $dictionary->id, 'language' => $lang_record->id, 'is_error' => (int) $error, 'wrong_answer_id' => $error ? $answer_id : null];
     $testRecord->save();
     // Check once again for max errors after the save
     if (Records::hasMaxErrors($test_id)) {
         $result['maxErrors'] = true;
         return $result;
     }
     if (!$error) {
         // Increase the score by 1
         $test->scoreUp();
     }
     return $result;
 }
 public function actionCheck()
 {
     $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) {
         $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;
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getTest()
 {
     return $this->hasOne(Test::className(), ['id' => 'test_id']);
 }
示例#4
0
 public function actionIndex()
 {
     $name_test = Test::getTestTitle();
     return $this->render('index', ['name_test' => $name_test]);
 }