public function action(Request $request) { $quizz = $this->quizzRepository->getActif(); $answer = Answer::find($request->get('answer')); if (!$answer) { return redirect('/question', 302, [], true)->with('status', 'Vous devez cochez au moins une réponse'); } $answeredQuestionNbr = $this->scoreRepository->getAnsweredQuestionNbr($quizz); if ($answeredQuestionNbr < $quizz->max_question) { $unansweredQuestion = $this->scoreRepository->getUnansweredQuestion($quizz); $this->scoreRepository->checkAndStore($answer, $unansweredQuestion); $answeredQuestions = $this->scoreRepository->getAnsweredQuestions($quizz); foreach ($quizz->questions as $key => $question) { foreach ($answeredQuestions as $answeredQuestion) { if ($question->id == $answeredQuestion->question->id) { $quizz->questions->forget($key); } } } if ($quizz->questions->isEmpty()) { return redirect('/result', 302, [], true); } else { $question = $quizz->questions->random(1); $this->scoreRepository->storeUnansweredQuestion($quizz, $question); } return redirect('/question', 302, [], true); } return redirect('/result', 302, [], true); }
/** * Show the form for creating a new resource. * * @return Response */ public function answer(Request $request, Test $Test, Question $Question) { foreach ($request->input('answers', []) as $answer) { $Answer = Answer::find($answer); $Test->answers()->attach($Answer); } return redirect(route('client.test', $Test)); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Answer::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, 'is_correct' => $this->is_correct, 'question_id' => $this->question_id]); $query->andFilterWhere(['like', 'title', $this->title]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Answer::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, 'askId' => $this->askId, 'userId' => $this->userId, 'createTime' => $this->createTime, 'status' => $this->status, 'helpNum' => $this->helpNum]); $query->andFilterWhere(['like', 'content', $this->content]); return $dataProvider; }
public function comment(Request $request) { if ($request->has('answer_id')) { $answer = Answer::find($request->input('answer_id')); $comment = $answer->comments()->create(['body' => $request->input('body')]); $comment->user()->associate(\Auth::user())->save(); } else { if ($request->has('question_id')) { $question = Question::find($request->input('question_id')); $comment = $question->comments()->create(['body' => $request->input('body')]); $comment->user()->associate(\Auth::user())->save(); } } return back(); }
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]); } }
/** * Display a listing of the resource. * * @return Response */ public function index(Request $request) { $question = Answer::find($request->answer_id)->question()->full()->first(); return view('questions.show', compact('question')); }
/** * Lists all Answer models. * @return mixed */ public function actionIndex() { $dataProvider = new ActiveDataProvider(['query' => Answer::find()]); return $this->render('index', ['dataProvider' => $dataProvider]); }
<?php use app\models\Question; use app\models\Answer; $question = array_column(Question::find()->select(['id'])->asArray()->all(), 'id'); $answer = array_column(Answer::find()->select(['id'])->asArray()->all(), 'id'); $boolean = [0, 1]; return ['question_id' => $faker->randomElement($question), 'answer_id' => $faker->randomElement($answer), 'is_right' => $faker->randomElement($boolean)];