Esempio n. 1
0
 /**
  * Displays a single Parts model.
  * @param integer $id
  *
  * @return mixed
  */
 public function actionPart($id)
 {
     $part = $this->findPartModel($id);
     $questions = $part->getQuestions()->all();
     $answers = [];
     $answerIds = [];
     foreach ($questions as $question) {
         $answers[] = ArrayHelper::map($question->getAnswers()->all(), 'id', 'answer');
         foreach ($question->getAnswers()->all() as $answer) {
             $answerIds[] = $answer->id;
         }
     }
     $answersUsers = ArrayHelper::map(AnswersUsers::find()->where(['created_by' => Yii::$app->getUser()->id, 'answer' => $answerIds])->select('answer')->all(), 'answer', 'answer');
     $pass = true;
     if (Yii::$app->request->getIsPost()) {
         $answersUsers = Yii::$app->request->post()['AnswersUsers'];
         foreach ($answersUsers as $answer) {
             if ($answer === '') {
                 $pass = false;
             }
         }
         if ($pass) {
             AnswersUsers::deleteAll(['created_by' => Yii::$app->getUser()->id, 'answer' => $answerIds]);
             foreach ($answersUsers as $answer) {
                 $model = new AnswersUsers();
                 $model->answer = $answer;
                 $model->save();
             }
             $lastPart = Parts::find()->orderBy('id DESC')->limit(1)->one();
             if ($id < $lastPart->id) {
                 return $this->redirect(['part/' . ($id + 1)]);
             }
             return $this->redirect(['results/1']);
         }
     }
     return $this->render('part', ['questions' => $questions, 'answers' => $answers, 'part' => $part, 'pass' => $pass, 'answersUsers' => $answersUsers]);
 }