/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Question::find()->where(['list_id' => $params['list_id']]);
     // add conditions that should always apply here
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     // grid filtering conditions
     $query->andFilterWhere(['id' => $this->id]);
     $query->andFilterWhere(['like', 'type', $this->type])->andFilterWhere(['like', 'quest_text', $this->quest_text]);
     return $dataProvider;
 }
Beispiel #2
0
 /**
  * @inheritdoc
  */
 public function rules()
 {
     return [[['type', 'quest_text', 'list_id'], 'required'], [['type'], 'string', 'max' => 50], [['ordering'], 'number'], [['quest_text'], 'string', 'max' => 1000], [['visible_condition'], 'string', 'max' => 1000], [['visible_condition_value'], 'string', 'max' => 100], [['visible_condition_value'], 'required', 'when' => function ($model) {
         if (!$model->visible_condition) {
             return false;
         }
         $linkedQuestion = Question::findOne($model->visible_condition);
         if (!$linkedQuestion) {
             throw new \NotFoundHttpException();
         }
         switch ($linkedQuestion->type) {
             case 'select_one':
             case 'select_multiple':
             case 'radio':
             case 'checkbox':
                 return true;
             case 'text':
             default:
                 return false;
         }
     }, 'whenClient' => 'function(){return false;}']];
 }
Beispiel #3
0
 public function getQuestion()
 {
     return $this->hasOne(Question::className(), ['id' => 'question_id']);
 }
Beispiel #4
0
?>

    <?php 
echo $form->field($model, 'ordering')->textInput(['type' => 'number']);
?>

    <?php 
echo $form->field($model, 'quest_text')->textArea(['maxlength' => true]);
?>

    <div id="visible_condition_block" class="panel panel-default">
        <div class="panel-heading"><h4><i class="glyphicon glyphicon-eye-open"></i> Условие видимости</h4></div>
        <div class="panel-body">
            <div class="col-md-6 col-sm-10">
                <?php 
$query = Question::find()->where(['list_id' => $list_id]);
$query->andWhere(['<>', 'type', 'text']);
// Если вопрос изменяется, то исключим его из выбора вопроса, связаного условием
if ($model->id) {
    $query->andWhere(['<>', 'id', $model->id]);
}
$questionsInList = ArrayHelper::map($query->all(), 'id', 'quest_text');
?>
                <?php 
echo $form->field($model, "visible_condition")->dropDownList($questionsInList, ['prompt' => 'Нет']);
?>
            </div>
            <div class="col-md-6 col-sm-10">
                <?php 
echo $form->field($model, 'visible_condition_value')->hiddenInput(['maxlength' => true]);
?>
 /**
  * Finds the Question model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Question the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Question::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 /**
  * 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 !== 'select_one') {
                                     $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]);
 }
Beispiel #7
0
 public function getQuestions()
 {
     return $this->hasMany(Question::className(), ['list_id' => 'id'])->orderBy(['questionlist_question.ordering' => SORT_ASC]);
 }
 protected function getAllAnswerVariantsGroupByQuestionId($question_list_id)
 {
     $result = [];
     $modelQuestionList = Question::findAll(['list_id' => $question_list_id]);
     $ids = array_values(ArrayHelper::map($modelQuestionList, 'id', 'id'));
     $answerVariants = AnswerVariant::findAll(['question_id' => $ids]);
     foreach ($answerVariants as $answerVariant) {
         $result[$answerVariant->question_id][$answerVariant->id] = $answerVariant;
     }
     return $result;
 }