/** * 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]]); } }
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' => 'Не смогли получить данные']; }
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]; } }