Example #1
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getTestRecords()
 {
     return $this->hasMany(TestRecords::className(), ['dictionary_id' => 'id']);
 }
Example #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;
 }