Пример #1
0
 /**
  * @return string
  *
  * Отображает чистую форму для заполнения со списком вопросов
  */
 public function actionCreate($id)
 {
     $modelAnswerList = $this->findAnswerListModel($id);
     if (!$this->getAccessToOffice($modelAnswerList->do_id)) {
         Yii::$app->getResponse()->redirect(Url::toRoute(['write-test/index']));
     }
     if ($modelAnswerList->status !== 'clear') {
         Yii::$app->getResponse()->redirect(Url::toRoute(['write-test/update', 'id' => $modelAnswerList->id]));
     }
     $modelQuestionList = $modelAnswerList->questionList;
     $modelsQuestion = $modelQuestionList->questions;
     // если форма отправлена.
     if ($postData = Yii::$app->request->post()) {
         $modelAnswerList->status = 'answered';
         $modelsAnswer = Model::createMultiple(Answer::classname(), [], $scenario = ['scenario' => 'create']);
         Model::loadMultiple($modelsAnswer, $postData);
         $valid = $modelQuestionList->validate();
         foreach ($modelsAnswer as $indexModelAnswer => $modelAnswer) {
             $modelAnswer->profile_id = Yii::$app->user->identity->username;
             $valid = $modelAnswer->validate() && $valid;
         }
         if ($valid) {
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 $flag = false;
                 if ($modelAnswerList->save()) {
                     foreach ($modelsAnswer as $indexModelAnswer => $modelAnswer) {
                         if (!($flag = $modelAnswer->save(false))) {
                             $transaction->rollBack();
                             break;
                         }
                     }
                 }
                 if ($flag) {
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $modelAnswerList->id]);
                 } else {
                     $transaction->rollBack();
                 }
             } catch (Exception $e) {
                 $transaction->rollBack();
             }
         }
     }
     return $this->render('create', ['modelQuestionList' => $modelQuestionList, 'modelAnswerList' => $modelAnswerList, 'modelsQuestion' => $modelsQuestion]);
 }
 /**
  * Creates a new QuestionList model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     // TODO : Провести рефакторинг
     $modelQuestionList = new QuestionList();
     // добавляем в объект класса QuestionList данные, пришедние из формы
     if ($modelQuestionList->load(Yii::$app->request->post())) {
         // создаются
         $modelsQuestion = Model::createMultiple(Question::classname());
         $modelsAnswerVariant = [];
         Model::loadMultiple($modelsQuestion, Yii::$app->request->post());
         $valid = $modelQuestionList->validate();
         $valid = Model::validateMultiple($modelsQuestion) && $valid;
         $postAnswerVariants = Yii::$app->request->post()['AnswerVariant'];
         if (isset($postAnswerVariants[0])) {
             foreach ($postAnswerVariants as $indexQuestion => $answers) {
                 if ($modelsQuestion[$indexQuestion]->type === 'multiple') {
                     foreach ($answers as $indexAnswer => $answer) {
                         $data = [];
                         $data['AnswerVariant'] = $answer;
                         $modelAnswerVariant = new AnswerVariant();
                         $modelAnswerVariant->load($data);
                         $modelsAnswerVariant[$indexQuestion][] = $modelAnswerVariant;
                     }
                 }
             }
         }
         if ($valid) {
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 if ($flag = $modelQuestionList->save(false)) {
                     foreach ($modelsQuestion as $indexQuestion => $modelQuestion) {
                         if (!($flag = $modelQuestion->save(false))) {
                             $transaction->rollBack();
                             break;
                         } else {
                             $modelQuestion->link('questionLists', $modelQuestionList);
                         }
                         if (isset($modelsAnswerVariant[$indexQuestion]) && is_array($modelsAnswerVariant[$indexQuestion])) {
                             foreach ($modelsAnswerVariant[$indexQuestion] as $modelAnswerVariant) {
                                 if (!$modelAnswerVariant->answer) {
                                     continue;
                                 }
                                 $modelAnswerVariant->question_id = $modelQuestion->id;
                                 if (!($flag = $modelAnswerVariant->save(false))) {
                                     break;
                                 }
                             }
                         }
                     }
                 }
                 if ($flag) {
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $modelQuestionList->id]);
                 } else {
                     $transaction->rollBack();
                 }
             } catch (Exception $e) {
                 $transaction->rollBack();
             }
         }
     } else {
         return $this->render('create', ['modelQuestionList' => $modelQuestionList, 'modelsQuestion' => empty($modelsQuestions) ? [new Question()] : $modelsQuestions, 'modelsAnswerVariant' => empty($modelsAnswerVariant) ? [[new AnswerVariant()]] : $modelsAnswerVariant]);
     }
 }