Ejemplo n.º 1
0
 public static function saveAnswer($request)
 {
     $userId = Yii::app()->user->id;
     if ($userId && isset($request['answerId'])) {
         $currentAnswer = Answers::model()->current()->find('userId=?', [$userId]);
         if ($currentAnswer) {
             $currentAnswer->isCurrent = false;
             if ($request['answerId'] != $currentAnswer->dictonaryId) {
                 $currentAnswer->isError = true;
                 $errAns = new ErrorAnswers();
                 $errAns->userId = $userId;
                 $errAns->dictonaryId = $currentAnswer->dictonaryId;
                 $errAns->answerId = $request['answerId'];
                 if ($errAns->save()) {
                     return ['success' => true, 'reenter' => true, 'error' => $currentAnswer->isError];
                     Yii::app()->end();
                 } else {
                     var_dump($errAns->getErrors());
                 }
             } else {
                 $currentAnswer->isOk = true;
             }
             if ($currentAnswer->save()) {
                 return ['success' => true, 'reenter' => false, 'question' => Dictonary::getQuestion($userId), 'error' => $currentAnswer->isError, 'ok' => $currentAnswer->isOk];
                 Yii::app()->end();
             } else {
                 var_dump($currentAnswer->getErrors());
             }
         }
     }
     return ['success' => false, 'error' => 'ошибка при сохранении ответа'];
 }
Ejemplo n.º 2
0
 /**
  * This is the default 'index' action that is invoked
  * when an action is not explicitly requested by users.
  */
 public function actionIndex()
 {
     //echo CJSON::encode(Dictonary::getQuestion(1));
     //        exit();
     $user = ['login' => false];
     if (!Yii::app()->user->getId()) {
         $model = new LoginForm();
         $model->attributes = ['username' => isset(Yii::app()->request->cookies['name']->value) ? Yii::app()->request->cookies['name']->value : '', 'password' => isset(Yii::app()->request->cookies['uuid']->value) ? Yii::app()->request->cookies['uuid']->value : ''];
         if ($model->validate() && $model->login()) {
             $user = ['uuid' => Yii::app()->user->getState('uuid'), 'name' => Yii::app()->user->name, 'login' => true];
         }
     } else {
         $user = ['uuid' => Yii::app()->user->getState('uuid'), 'name' => Yii::app()->user->name, 'login' => true];
     }
     if (isset($user['uuid'])) {
         $this->render('index', ['data' => ['user' => $user, 'countQuestions' => Yii::app()->db->createCommand('SELECT count(id) FROM Dictonary')->queryScalar(), 'current' => Answers::getCount(Yii::app()->user->id, Answers::FLAG_CURRENT), 'error' => ErrorAnswers::getCount(Yii::app()->user->id), 'ok' => Answers::getCount(Yii::app()->user->id, Answers::FLAG_OK), 'question' => Dictonary::getQuestion(Yii::app()->user->id)]]);
     } else {
         $this->render('index', ['data' => ['user' => $user, 'countQuestions' => Yii::app()->db->createCommand('SELECT count(id) FROM Dictonary')->queryScalar(), 'current' => 0, 'error' => 0, 'ok' => 0, 'question' => false]]);
     }
 }
Ejemplo n.º 3
0
 public static function createUser($request)
 {
     if (isset($request['name'])) {
         $model = Users::model()->find('LOWER(name)=?', [$request['name']]);
         if (!$model) {
             $model = new Users();
             $model->name = $request['name'];
             if (!$model->save()) {
                 return ['success' => false, 'error' => CVarDumper::dumpAsString($model->getErrors())];
                 Yii::app()->end();
             }
         }
         $model->setCookies();
         $login = new LoginForm();
         $login->attributes = ['username' => $model->name, 'password' => $model->uuid];
         if ($login->validate() && $login->login()) {
             return ['success' => true, 'user' => ['uuid' => $model->uuid, 'name' => $model->name, 'login' => true], 'question' => Dictonary::getQuestion($model->id), 'error' => ErrorAnswers::getCount($model->id), 'ok' => Answers::getCount($model->id, Answers::FLAG_OK)];
         }
     }
     return ['success' => false, 'error' => 'Не смогли получить данные'];
 }
Ejemplo n.º 4
0
 public static function getQuestion($userId = null)
 {
     if ($userId) {
         $db = Yii::app()->db;
         $_question = [];
         $all = [];
         $fields = (double) rand() / (double) getrandmax() > 0.5 ? ['answer' => 'nameRu as answer', 'question' => 'nameEn'] : ['answer' => 'nameEn as answer', 'question' => 'nameRu'];
         $currentAnswer = Answers::model()->current()->find('userId=?', [$userId]);
         if ($currentAnswer === null) {
             $_question = $db->createCommand('SELECT id,' . implode(',', $fields) . ' FROM `Dictonary` WHERE id NOT IN (SELECT dictonaryId From Answers WHERE userId = ' . (int) $userId . ') ORDER BY RAND() LIMIT 1;')->queryRow();
             if (isset($_question['id'])) {
                 $currentAnswer = new Answers();
                 $currentAnswer->userId = $userId;
                 $currentAnswer->dictonaryId = $_question['id'];
                 $currentAnswer->isCurrent = true;
                 if ($currentAnswer->save() === false) {
                     var_dump($currentAnswer->getErrors());
                 }
             }
         } else {
             $_question = $db->createCommand('SELECT id,' . implode(',', $fields) . ' FROM `Dictonary` WHERE id = ' . $currentAnswer->dictonaryId)->queryRow();
         }
         if (count($_question) > 0) {
             $question = $_question[$fields['question']];
             unset($_question[$fields['question']]);
         }
         $all = $db->createCommand()->select('id,' . $fields['answer'])->from('Dictonary')->where('id!=:userId', array(':userId' => $_question['id']))->order('RAND()')->limit(3)->queryAll();
         $all[] = $_question;
         shuffle($all);
         shuffle($all);
         if (count($_question) > 0 && count($all) > 0) {
             return ['success' => true, 'question' => $question, 'answers' => $all, 'error' => ErrorAnswers::getCount(Yii::app()->user->id), 'ok' => Answers::getCount($userId, Answers::FLAG_OK)];
         } else {
             return ['success' => true, 'question' => false, 'answers' => false, 'error' => ErrorAnswers::getCount(Yii::app()->user->id), 'ok' => Answers::getCount($userId, Answers::FLAG_OK)];
         }
     } else {
         return ['success' => false];
     }
 }