/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Exam::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(['like', '_id', $this->_id])->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'num', $this->num])->andFilterWhere(['like', 'people', $this->people])->andFilterWhere(['like', 'difficulty', $this->difficulty])->andFilterWhere(['like', 'addtime', $this->addtime])->andFilterWhere(['like', 'is_hot', $this->is_hot])->andFilterWhere(['like', 'is_news', $this->is_news])->andFilterWhere(['like', 'job', $this->job])->andFilterWhere(['like', 'zhishi', $this->zhishi])->andFilterWhere(['like', 'do_time', $this->do_time])->andFilterWhere(['like', 'sum_grade', $this->sum_grade])->andFilterWhere(['like', 'e_desc', $this->e_desc])->andFilterWhere(['like', 'company_id', $this->company_id])->andFilterWhere(['like', 'head_logo', $this->head_logo]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Exam::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, 'duration' => $this->duration, 'created_time' => $this->created_time, 'completed' => $this->completed, 'exam_date' => $this->exam_date, 'user_id' => $this->user_id]); $query->andFilterWhere(['like', 'subject', $this->subject]); return $dataProvider; }
public function actionIndex() { if (\Yii::$app->user->isGuest) { $this->redirect(array("login")); } else { $exam = Exam::find()->where("1=1")->one(); // modify later if (Yii::$app->request->post()) { $post = Yii::$app->request->post(); $answers = $post["Answers"]; $transaction = Yii::$app->db->beginTransaction(); try { $userscore = new UserScore(); $userscore->finished_time = date("Y-m-d H:i:s"); $userscore->user_id = Yii::$app->user->identity->id; $userscore->exam_id = $exam->id; $userscore->score = 0; $userAnswers = []; $totalScore = 0; foreach ($exam->getQuestions()->all() as $question) { $userAnswer = new UserHasAnswer(); $userAnswer->user_id = Yii::$app->user->identity->id; $userAnswer->answer_id = $answers[$question->id]; $userAnswer->exam_id = $exam->id; $userAnswer->save(); $correctAnswer = Answer::find()->where("is_correct=:is_correct AND question_id=:question_id", [":is_correct" => 1, ":question_id" => $question->id])->one(); if ($userAnswer->answer_id === $correctAnswer->id) { $userscore->score += $question->score; } $totalScore += $question->score; $userAnswers[] = $userAnswer; } $userAnswer->save(); $transaction->commit(); // $this->redirect(["showScore"], ["userAnswers" => $userAnswers, "userScore" => $userscore]); return $this->render("score_summary", ["userAnswers" => $userAnswers, "userScore" => $userscore, "totalScore" => $totalScore]); } catch (Exception $exc) { $transaction->rollBack(); Yii::$app->session->setFlash("error", $exc->getTraceAsString()); } } return $this->render('index', ["exam" => $exam]); } }