/**
  * Creates a new QuestionList model.
  * For ajax request will return json object
  * and for non-ajax request if creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $request = Yii::$app->request;
     $model = new QuestionList();
     if ($request->isAjax) {
         /*
          *   Process for ajax request
          */
         Yii::$app->response->format = Response::FORMAT_JSON;
         if ($request->isGet) {
             return ['title' => "Создать опросный лист", 'content' => $this->renderAjax('create', ['model' => $model]), 'footer' => Html::button('Закрыть', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . Html::button('Сохранить', ['class' => 'btn btn-primary', 'type' => "submit"])];
         } else {
             if ($model->load($request->post()) && $model->save()) {
                 return ['forceReload' => '#crud-datatable-pjax', 'title' => "Создать опросный лист", 'content' => '<span class="text-success">Create QuestionList success</span>', 'footer' => Html::button('Закрыть', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . Html::a('Create More', ['create'], ['class' => 'btn btn-primary', 'role' => 'modal-remote'])];
             } else {
                 return ['title' => "Создать опросный лист", 'content' => $this->renderAjax('create', ['model' => $model]), 'footer' => Html::button('Закрыть', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . Html::button('Сохранить', ['class' => 'btn btn-primary', 'type' => "submit"])];
             }
         }
     } else {
         /*
          *   Process for non-ajax request
          */
         if ($model->load($request->post()) && $model->save()) {
             return $this->redirect(['view', 'id' => $model->id]);
         } else {
             return $this->render('create', ['model' => $model]);
         }
     }
 }
 /**
  * Creates a new QuestionList model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $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 === 'select_one') {
                     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]);
     }
 }