padding : 5px;
    margin : 5px;
}
');
?>
<div class="answer-list-view">

    <h1><?php 
echo Html::encode($this->title);
?>
</h1>
    <?php 
if ($model->comment) {
    ?>
    <div class="comment">
        <p><i>Комментарий : <?php 
    echo $model->comment;
    ?>
</i></p>
    </div>
    <?php 
}
?>

    <?php 
echo GridView::widget(['dataProvider' => new ActiveDataProvider(['query' => Answer::find()->where(['answer_list_id' => $model->id])]), 'columns' => ['question_text', 'question_type', 'answer', 'answer_comment']]);
?>

</div>

Beispiel #2
0
 public function getAnswers()
 {
     return $this->hasMany(Answer::className(), ['answer_list_id' => 'id']);
 }
 /**
  * @return string
  *
  * Отображает чистую форму для заполнения со списком вопросов
  */
 public function actionUpdate($id)
 {
     $modelAnswerList = $this->findAnswerListModel($id);
     $modelAnswerList->scenario = 'write-test';
     $modelQuestionList = $modelAnswerList->questionList;
     $modelsQuestion = $modelQuestionList->questions;
     $modelsAnswer = $modelAnswerList->answers;
     // если форма отправлена.
     if ($postData = Yii::$app->request->post()) {
         $valid = $modelQuestionList->validate();
         $answerIds = ArrayHelper::getColumn($postData['Answer'], 'id');
         $modelsAnswer = Answer::findAll($answerIds);
         Model::loadMultiple($modelsAnswer, $postData);
         //если был удален один вопрос, на который ранее отвечали
         $oldAnswerIDs = ArrayHelper::map($modelAnswerList->answers, 'id', 'id');
         $deletedAnswerIDs = array_diff($oldAnswerIDs, array_filter($answerIds));
         // если был добавлен еще один вопрос, то создадим пустой и добавим
         foreach ($postData['Answer'] as $answer) {
             if (!$answer['id']) {
                 $modelAnswerVariant = new Answer();
                 $modelAnswerVariant->load(['Answer' => $answer]);
                 $modelsAnswer[] = $modelAnswerVariant;
             }
         }
         // сумма очков
         $sumScores = 0;
         $groupsOfModelsAnswerVariants = $this->getAllAnswerVariantsGroupByQuestionId($modelAnswerList->question_list_id);
         foreach ($modelsAnswer as $modelAnswer) {
             $modelAnswer->question_list_id = $modelAnswerList->question_list_id;
             $modelAnswer->answer_list_id = $modelAnswerList->id;
             $valid = $modelAnswer->validate();
             if (!$valid) {
                 break;
             }
             if (isset($groupsOfModelsAnswerVariants[$modelAnswer->question_id]) && isset($groupsOfModelsAnswerVariants[$modelAnswer->question_id][$modelAnswer->answer])) {
                 $score = $groupsOfModelsAnswerVariants[$modelAnswer->question_id][$modelAnswer->answer]->scores;
                 $sumScores += $score;
             }
         }
         $transaction = \Yii::$app->db->beginTransaction();
         if ($valid) {
             try {
                 $flag = false;
                 $modelAnswerList->scores = $sumScores;
                 if ($modelAnswerList->save()) {
                     foreach ($modelsAnswer as $indexModelAnswer => $modelAnswer) {
                         if (!($flag = $modelAnswer->save(false))) {
                             $transaction->rollBack();
                             break;
                         }
                     }
                 }
                 // удаляем вопросы, которые могли быть удалены в конструкторе
                 if ($deletedAnswerIDs) {
                     $del = Answer::findAll(array_values($deletedAnswerIDs));
                     foreach ($del as $model) {
                         $model->delete();
                     }
                 }
                 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]);
 }