/**
  * Updates an existing QuestionList model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $modelQuestionList = $this->findModel($id);
     $modelsQuestion = $modelQuestionList->questions;
     $modelsAnswerVariant = [];
     $oldAnswerVariants = [];
     if (!empty($modelsQuestion)) {
         foreach ($modelsQuestion as $indexQuestion => $modelQuestion) {
             $answerVariants = $modelQuestion->answerVariants;
             $modelsAnswerVariant[$indexQuestion] = $answerVariants;
             $oldAnswerVariants = ArrayHelper::merge(ArrayHelper::index($answerVariants, 'id'), $oldAnswerVariants);
         }
     }
     if ($modelQuestionList->load(Yii::$app->request->post())) {
         $oldQuestionIDs = ArrayHelper::map($modelsQuestion, 'id', 'id');
         $modelsQuestion = [];
         $postQuestionData = Yii::$app->request->post()['Question'];
         if ($postQuestionData) {
             foreach ($postQuestionData as $q) {
                 $question = new Question();
                 if ($q['id']) {
                     $actualIds[$q['id']] = $q['id'];
                     $question->id = $q['id'];
                 }
                 $modelsQuestion[] = $question;
             }
         }
         Model::loadMultiple($modelsQuestion, Yii::$app->request->post());
         $deletedQuestionIDs = array_diff($oldQuestionIDs, array_filter(ArrayHelper::map($modelsQuestion, 'id', 'id')));
         $valid = $modelQuestionList->validate();
         $valid = Model::validateMultiple($modelsQuestion) && $valid;
         $answerVariantsIDs = [];
         $postAnswersData = Yii::$app->request->post()['AnswerVariant'];
         if (isset($postAnswersData[0])) {
             foreach ($postAnswersData as $indexQuestion => $answerVariants) {
                 $answerVariantsIDs = ArrayHelper::merge($answerVariantsIDs, array_filter(ArrayHelper::getColumn($answerVariants, 'id')));
                 foreach ($answerVariants as $indexAnswerVariant => $answerVariant) {
                     if (!$answerVariant['answer']) {
                         continue;
                     }
                     $data['AnswerVariant'] = $answerVariant;
                     $modelAnswerVariant = isset($answerVariant['id']) && isset($oldAnswerVariants[$answerVariant['id']]) ? $oldAnswerVariants[$answerVariant['id']] : new AnswerVariant();
                     $modelAnswerVariant->load($data);
                     $modelsAnswerVariant[$indexQuestion][$indexAnswerVariant] = $modelAnswerVariant;
                     $valid = $modelAnswerVariant->validate();
                 }
             }
         }
         $oldAnswerVariantsIDs = ArrayHelper::getColumn($oldAnswerVariants, 'id');
         $deletedAnswerVariantsIDs = array_diff($oldAnswerVariantsIDs, $answerVariantsIDs);
         if ($valid) {
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 if ($flag = $modelQuestionList->save(false)) {
                     if (!empty($deletedAnswerVariantsIDs)) {
                         AnswerVariant::deleteAll(['id' => $deletedAnswerVariantsIDs]);
                     }
                     if (!empty($deletedQuestionIDs)) {
                         foreach ($deletedQuestionIDs as $q_id) {
                             Question::findOne($q_id)->unlink('questionLists', $modelQuestionList);
                             Question::findOne($q_id)->delete();
                         }
                     }
                     foreach ($modelsQuestion as $indexQuestion => $modelQuestion) {
                         if ($modelQuestion->id) {
                             $model = Question::findOne($modelQuestion->id);
                             $data = $modelQuestion->toArray();
                             if ($model) {
                                 $model->attributes = $data;
                                 $modelQuestion = $model;
                             }
                         }
                         if (!($flag = $modelQuestion->save())) {
                             $transaction->rollBack();
                             break;
                         }
                         if (isset($modelsAnswerVariant[$indexQuestion]) && is_array($modelsAnswerVariant[$indexQuestion])) {
                             foreach ($modelsAnswerVariant[$indexQuestion] as $indexAnswerVariant => $modelAnswerVariant) {
                                 if ($modelsQuestion[$indexQuestion]->type !== 'multiple') {
                                     $modelAnswerVariant->delete();
                                 } else {
                                     $modelAnswerVariant->question_id = $modelQuestion->id;
                                     if (!($flag = $modelAnswerVariant->save(false))) {
                                         break;
                                     }
                                 }
                             }
                         }
                         if (!$actualIds[$modelQuestion->id]) {
                             $modelQuestion->link('questionLists', $modelQuestionList);
                         }
                     }
                 }
                 if ($flag) {
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $modelQuestionList->id]);
                 }
             } catch (Exception $e) {
                 $transaction->rollBack();
             }
         }
     }
     return $this->render('update', ['modelQuestionList' => $modelQuestionList, 'modelsQuestion' => empty($modelsQuestion) ? [new Question()] : $modelsQuestion, 'modelsAnswerVariant' => empty($modelsAnswerVariant) ? [[new AnswerVariant()]] : $modelsAnswerVariant]);
 }
예제 #2
0
 public function getAnswerVariants()
 {
     return $this->hasMany(AnswerVariant::className(), ['question_id' => 'id']);
 }