Esempio n. 1
0
 /**
  * @return string
  *
  * Отображает чистую форму для заполнения со списком вопросов
  */
 public function actionUpdate($id)
 {
     $modelAnswerList = $this->findAnswerListModel($id);
     $modelQuestionList = $modelAnswerList->questionList;
     $modelsQuestion = $modelQuestionList->questions;
     $modelsAnswer = $modelAnswerList->answers;
     if (!$this->getAccessToOffice($modelAnswerList->do_id)) {
         Yii::$app->getResponse()->redirect(Url::toRoute(['write-test/index']));
     }
     // если форма отправлена.
     if ($postData = Yii::$app->request->post()) {
         $valid = $modelQuestionList->validate();
         if (isset($postData['Answer'])) {
             $answerIds = ArrayHelper::getColumn($postData['Answer'], 'id');
             $modelsAnswer = Answer::findAll($answerIds);
             Model::loadMultiple($modelsAnswer, $postData);
         }
         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('update', ['modelQuestionList' => $modelQuestionList, 'modelAnswerList' => $modelAnswerList, 'modelsQuestion' => $modelsQuestion, 'modelsAnswer' => $modelsAnswer]);
 }
 /**
  * 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]);
 }