public function actionGetquestion() { $user = User::find()->where(['id' => (int) Yii::$app->request->get('user')])->one(); $test = json_decode($user->test, true); //После 5 вопросов заканчиваем тест if (count($test['rights']) >= 5) { return 'finish'; } //Если уже есть ответы, то берем из азы любые кроме отвеченных, иначе все if (count($test['rights']) > 0) { for ($i = 0; $i < count($test['rights']); $i++) { $exclude[] = $test['rights'][$i]['id']; } $rows = Word::find()->select(['id', 'rus', 'eng'])->from('word')->where(['not in', 'id', $exclude])->orderBy('RAND()')->limit(4)->all(); } else { $rows = Word::find()->select(['id', 'rus', 'eng'])->from('word')->orderBy('RAND()')->limit(4)->all(); } //получаем случайны вопрос $right_key = array_rand($rows); //записываем его в пользователя $test['rights'][] = ['id' => $rows[$right_key]['id'], 'a' => null, 't' => 0]; $user->test = json_encode($test, true); $user->save(); //Формируем вопрос и варианты ответа на фронтенд for ($i = 0; $i < count($rows); $i++) { $variation[] = ['id' => $rows[$i]['id'], 'word' => $rows[$i]['eng']]; } $words = ['question' => ['word' => $rows[$right_key]['rus']], 'words' => $variation]; return $words; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Word::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'projekt_id' => $this->projekt_id, 'group_id' => $this->group_id]); $query->andFilterWhere(['like', 'word_name', $this->word_name])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'status', $this->status]); return $dataProvider; }
/** * Finds the Word model based on its primary key value. * @param string $id * @return Word the loaded model * @throws NotFoundHttpException if the model cannot be found */ private function findModel($id) { $model = Word::find()->userId(Yii::$app->user->id)->andWhere(['id' => $id])->limit(1)->one(); if ($model === null) { throw new NotFoundHttpException('データがありません。'); } return $model; }
/** * @dataProvider searchQueryProvider */ public function testSearchQuery($search, $count, $id) { $word = Word::find()->userId(1)->search($search); $this->assertSame($count, $word->count()); $this->assertSame($id, $word->all()[0]->id); }