/** * Creates a new Answer model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Answer(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Store a newly created resource in storage. * * @return Response */ public function store(AnswerRequest $request) { $comment = new Answer(); $comment->text = $request->text; $comment->question_id = $request->question_id; $comment->user_id = Auth::user()->id; if ($comment->save()) { Event::fire(new CreateComment($request->type, $request->question_id, $request->user_id, $comment->id)); } return \Redirect::to('/questions/' . $request->question_id); }
/** * Creates a new Answer model. * If creation is successful, the browser will be redirected to the 'view' page. * * @return mixed */ public function actionCreate() { $model = new Answer(); $post = Yii::$app->request->post(); if (isset($post['Answer']['content']) && isset($post['Answer']['askId'])) { $model->load($post); $model->userId = Yii::$app->user->identity->getId(); $model->createTime = date('Y-m-d H:i:s', time()); $model->status = 1; $model->helpNum = 0; if ($model->save()) { $main = Main::findOne($post['Answer']['askId']); $main->status = 2; $main->save(); } } return $this->redirect(['/main/view', 'id' => $post['Answer']['askId']]); }
public function actionIndex() { $file = file_get_contents(__DIR__ . '/QuestionImport.txt'); $line = explode("\n", $file); $array = []; foreach ($line as $k => $v) { $el = explode("\t", $v); $count_el = count($el); $array[$k]['title'] = $el[0]; $array[$k]['occured_number'] = $el[1]; $array[$k]['correct_answer_key'] = $el[2]; for ($i = 3; $i < $count_el; $i++) { $array[$k]['answer'][$i] = $el[$i]; } $array[$k]['number_of_elements'] = $count_el; } $user = User::findByUsername('admin'); foreach ($array as $v) { $model_question = new Question(); $model_question->title = $v['title']; $model_question->occured_number = $v['occured_number']; $model_question->correct_answer_id = '0'; $model_question->author_id = $user->id; $model_question->save(); $correct_answer_id = 0; foreach ($v['answer'] as $k2 => $v2) { $model_answer = new Answer(); $model_answer->question_id = $model_question->id; $model_answer->title = $v2; $model_answer->author_id = $user->id; $model_answer->save(); if ($k2 - 3 == $v['correct_answer_key']) { $correct_answer_id = $model_answer->id; } } $model_question->correct_answer_id = $correct_answer_id; $model_question->save(); } }