public function actionGetOptionsList() { $id = Yii::$app->request->post('id', null); $optionsHtml = ''; if (!empty($id) && $id > 0) { $options = MappingQuestionsToOptions::find()->where(['question_id' => $id]); if ($options->count() > 0) { $optionsList = $options->all(); $optionsHtml .= '<option>' . Yii::t('backend', 'Select Options') . '</option>'; foreach ($optionsList as $option) { $optionsHtml .= "<option value='" . $option->id . "'>" . $option->title . "</option>"; } } else { $optionsHtml .= "<option> - </option>"; } } echo $optionsHtml; }
if (Yii::$app->request->post()) { $postData = Yii::$app->request->post(); } foreach ($questionsModel as $model) { $value = $option_id = ''; if ($answersModel = MembersQuestionsAnswers::findOne(['question_id' => $model->id, 'member_id' => $member_id])) { $value = $answersModel->value; $option_id = $answersModel->option_id; } else { if (!empty($postData['MembersQuestionsAnswers']["value_{$model->id}"])) { $value = $option_id = $postData['MembersQuestionsAnswers']["value_{$model->id}"]; } } if ($model->fieldsTypes[0]->has_options) { //if question has options display a dropdown $optionsArray = ArrayHelper::map(MappingQuestionsToOptions::findAll(['question_id' => $model->id]), 'id', 'title'); if ($model->fieldsTypes[0]->has_other_field && $model->has_other) { $optionsArray[-1] = Yii::t('frontend', 'Other'); } echo $form->field($questionsAnswers, "value_{$model->id}")->dropDownList($optionsArray, ['prompt' => Yii::t('frontend', '-- Select --'), 'options' => [$option_id => ['Selected' => 'selected']]])->label($model->title); } else { // else if question does not have options display a text field echo $form->field($questionsAnswers, "value_{$model->id}")->textInput(['value' => $value]); } if ($model->fieldsTypes[0]->has_other_field && $model->has_other) { if ($answersModel) { $options = ['value' => $answersModel->other]; } if ($option_id > 0) { $options = ['style' => 'display: none;']; } else {
/** * Finds the MappingQuestionsToOptions model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return MappingQuestionsToOptions the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = MappingQuestionsToOptions::find()->where(['id' => $id])->multilingual()->one()) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @return \yii\db\ActiveQuery */ public function getQuestionOption() { return $this->hasOne(MappingQuestionsToOptions::className(), ['id' => 'option_id']); }
public function actionMapping_old_171115() { if (!Yii::$app->user->isGuest) { $user = Yii::$app->getUser()->getIdentity(); //@TODO new logic to find out if there are answers /*$answers = MembersQuestionsAnswers::find(['member_id' => $user->id])->count(); if ($answers > 0) { $user->status = User::STATUS_ACTIVE; if ($user->save()) { return Yii::$app->response->redirect(array('members/index')); } else { Yii::$app->getSession()->setFlash('warning', Yii::t('errors', 'Can not update user status. Please contact system administrator')); } }*/ $settingsModel = GeneralSettings::findOne(['name' => 'mapping_page_id']); $pageModel = CmsPages::findOne(['id' => $settingsModel->value]); $categoryModel = MappingCategories::findOne(['id' => $pageModel->mapping_id]); $attributesModel = MembersAttributes::findAll(['id' => $categoryModel->attributes_id]); $questionsModel = MappingQuestions::find(['category_id' => $categoryModel->id])->with('fieldsTypes')->all(); $questionsAnswers = new MembersQuestionsAnswers($pageModel->category_id); $attributesAnswers = new MembersAttributesAnswers($categoryModel->attributes_id); if (Yii::$app->request->post()) { $mappingQuestionsSaved = false; $mappingAttributesSaved = false; if ($questionsAnswers->load(Yii::$app->request->post()) && $questionsAnswers->validate()) { foreach ($questionsAnswers->attributes as $attribute_id => $attribute) { $attributesArray = explode('_', $attribute_id); if (count($attributesArray) > 1) { list($name, $id) = $attributesArray; if (!($model = MembersQuestionsAnswers::findOne(['question_id' => $id, 'member_id' => Yii::$app->getUser()->id]))) { $model = new MembersQuestionsAnswers(); } $has_option = false; if ($questionsModel = MappingQuestions::findOne($id)) { $type_id = $questionsModel->type_id; $has_option = FieldsTypes::findOne($type_id)->has_options; } if ($has_option) { $model->value = ''; $model->option_id = ''; if ($optionsModel = MappingQuestionsToOptions::findOne($questionsAnswers->{$attribute_id})) { $model->value = $optionsModel->title; $model->option_id = $optionsModel->id; } } else { $model->value = $questionsAnswers->{$attribute_id}; } $model->question_id = $id; $model->member_id = Yii::$app->getUser()->id; $model->save(); } } $mappingQuestionsSaved = true; } if ($attributesAnswers->load(Yii::$app->request->post()) && $attributesAnswers->validate()) { foreach ($attributesAnswers->attributes as $attribute_id => $attribute) { $attributesArray = explode('_', $attribute_id); if (count($attributesArray) > 1) { list($name, $id) = $attributesArray; if (!($model = MembersAttributesAnswers::findOne(['attribute_id' => $id, 'member_id' => Yii::$app->getUser()->id]))) { $model = new MembersAttributesAnswers(); } $has_option = false; if ($questionsModel = MembersAttributes::findOne($id)) { $type_id = $questionsModel->type_id; $has_option = FieldsTypes::findOne($type_id)->has_options; $filedTypeTitle = FieldsTypes::findOne($type_id)->title; } if ($has_option) { $model->value = ''; $model->option_id = ''; if ($optionsModel = MembersAttributesToOptions::findOne($attributesAnswers->{$attribute_id})) { $model->value = $optionsModel->title; $model->option_id = $optionsModel->id; } } else { $model->value = $attributesAnswers->{$attribute_id}; if (isset($filedTypeTitle) && $filedTypeTitle == 'Date Picker') { //@TODO replace datepicker with some database constatnt value; $model->value = strtotime($attributesAnswers->{$attribute_id}); } } $model->attribute_id = $id; $model->member_id = Yii::$app->getUser()->id; $model->save(); } } $mappingAttributesSaved = true; } if ($mappingQuestionsSaved && $mappingAttributesSaved) { $user = Yii::$app->getUser()->getIdentity(); $user->status = User::STATUS_ACTIVE; if ($user->save()) { return Yii::$app->response->redirect(array('members')); } else { Yii::$app->getSession()->setFlash('warning', Yii::t('errors', 'Can not save mapping data. Please contact system administrator')); } } else { $categoryModel = MappingCategories::findOne(['id' => $pageModel->category_id]); $attributesModel = MembersAttributes::find(['id in ' => $categoryModel->attributes_id])->with('fieldsTypes')->all(); $questionsModel = MappingQuestions::find(['category_id' => $categoryModel->id])->with('fieldsTypes')->all(); return $this->render('mapping', ['questionsAnswers' => $questionsAnswers, 'attributesAnswers' => $attributesAnswers, 'pageModel' => $pageModel, 'categoryModel' => $categoryModel, 'questionsModel' => $questionsModel, 'attributesModel' => $attributesModel]); } } else { return $this->render('mapping', ['questionsAnswers' => $questionsAnswers, 'attributesAnswers' => $attributesAnswers, 'pageModel' => $pageModel, 'categoryModel' => $categoryModel, 'questionsModel' => $questionsModel, 'attributesModel' => $attributesModel]); } } else { return Yii::$app->response->redirect(array('site/login')); } }
<div class="members-view"> <h1><?php echo Html::encode($this->title); ?> </h1> <?php $tab_items[] = ['label' => Yii::t('backend', 'General Information'), 'content' => '<p>' . DetailView::widget(['model' => $model, 'attributes' => ['firstname', 'lastname', 'username', 'email:email', 'braintree_customer_id', 'weight', 'height', ['attribute' => 'status', 'format' => 'html', 'value' => \backend\models\Members::getStatusLabel($model->status)], 'created_at:date']]) . '</p>']; $questionItems = []; foreach ($categoriesModel as $categoryModel) { // get questions answers $questionsAnswers = new ActiveDataProvider(['query' => $categoryModel->getQuestions()->with('questionsAnswers')]); $questionItems[] = ['label' => $categoryModel->name, 'content' => '<p>' . GridView::widget(['dataProvider' => $questionsAnswers, 'summary' => '', 'showFooter' => false, 'showHeader' => false, 'columns' => ['title', ['header' => Yii::t('backend', 'Answer'), 'attribute' => 'questionsAnswers.option_id', 'value' => function ($model) { if ($model->questionsAnswers->option_id > 0) { return \backend\models\MappingQuestionsToOptions::findOne($model->questionsAnswers->option_id)->title; } else { if (!empty($model->questionsAnswers->value)) { return $model->questionsAnswers->value; } else { if (!empty($model->questionsAnswers->other)) { return $model->questionsAnswers->other; } } } }]]]) . '</p>']; } if (count($questionItems) > 0) { $tab_items[] = ['label' => Yii::t('backend', 'Mapping'), 'content' => '<p>' . Tabs::widget(['items' => $questionItems]) . '</p>']; } $tab_items[] = ['label' => Yii::t('backend', 'Weight Tracker'), 'content' => '<p>' . GridView::widget(['dataProvider' => $weightTracker, 'summary' => '', 'showFooter' => false, 'showHeader' => true, 'columns' => ['value', 'created_at:date']]) . '</p>'];
$("select#pdfsrules-options_id").html(data); })']; $answers_options = ['prompt' => Yii::t('backend', 'Select Answers'), 'multiple' => 'true', 'size' => 7]; $questions_condition['fields_type.has_options'] = 1; if (!empty($model->category_id)) { $category_options['options'] = [$model->category_id => ['Selected' => 'selected']]; $questions_condition['category_id'] = $model->category_id; $question_options['options'] = [$model->question_id => ['Selected' => 'selected']]; $answers_condition['question_id'] = $model->question_id; $answers_options['options'] = [$model->options_id => ['Selected' => 'selected']]; } $question_items = array(); $options_items = array(); if (!$model->isNewRecord) { $question_items = \yii\helpers\ArrayHelper::map(\backend\models\MappingQuestions::find()->joinWith('fieldsTypes')->where($questions_condition)->all(), 'id', 'title'); $options_items = \yii\helpers\ArrayHelper::map(\backend\models\MappingQuestionsToOptions::find()->where($answers_condition)->all(), 'id', 'title'); } ?> <?php echo $form->field($model, 'category_id')->dropDownList(\yii\helpers\ArrayHelper::map(\backend\models\MappingCategories::find()->all(), 'id', 'name'), $category_options)->label(Yii::t('backend', 'Select Categories')); ?> <?php echo $form->field($model, 'question_id')->dropDownList($question_items, $question_options)->label(Yii::t('backend', 'Select Question')); ?> <?php echo $form->field($model, 'options_id')->dropDownList($options_items, $answers_options)->label(Yii::t('backend', 'Select Answers')); ?>
public function processQuestions($questionsAnswers) { if ($questionsAnswers->load(Yii::$app->request->post()) && $questionsAnswers->validate()) { $questionsModel = new MappingQuestions(); foreach ($questionsAnswers->attributes as $attribute_id => $attribute) { $attributesArray = explode('_', $attribute_id); //ignore attributes without underscores if (count($attributesArray) < 2) { continue; } list($name, $id) = $attributesArray; if ($id > 0) { list($name, $id) = $attributesArray; if (!($model = MembersQuestionsAnswers::findOne(['question_id' => $id, 'member_id' => Yii::$app->getUser()->id]))) { $model = new MembersQuestionsAnswers(); } $has_option = false; if ($questionsModel = MappingQuestions::findOne($id)) { $type_id = $questionsModel->type_id; $fieldsModel = FieldsTypes::findOne($type_id); $has_option = $fieldsModel->has_options; } $model->{$name} = $questionsAnswers->{$attribute_id}; if ($has_option) { $model->value = ''; $model->option_id = ''; if ($optionsModel = MappingQuestionsToOptions::findOne($questionsAnswers->{$attribute_id})) { $model->value = $optionsModel->title; $model->option_id = $optionsModel->id; } if ($fieldsModel->has_other_field && $questionsModel->has_other && $name == 'value') { $model->option_id = $questionsAnswers->{$attribute_id}; } } $model->question_id = $id; $model->member_id = Yii::$app->getUser()->id; $model->save(); } } if (!empty($questionsModel->category_id)) { $this->saveProgress($questionsModel->category_id); } return true; } $this->stepsInformation['questionsAnswers'] = $questionsAnswers; return false; }
/** * @return \yii\db\ActiveQuery */ public function getOptions() { return $this->hasMany(MappingQuestionsToOptions::className(), ['question_id' => 'id']); }
/** * Displays homepage. * * @return mixed */ public function actionIndex() { if (!Yii::$app->user->isGuest) { Yii::$app->MappingComponent->process(); $steps = Yii::$app->MappingComponent->getSteps(); if (empty($steps['view'])) { $user = Yii::$app->getUser()->getIdentity(); $user->status = User::STATUS_ACTIVE; if ($user->save()) { return Yii::$app->response->redirect(array('members/index')); } else { Yii::$app->getSession()->setFlash('warning', Yii::t('errors', 'Can not update user status. Please contact system administrator')); } } $settingsModel = GeneralSettings::findOne(['name' => 'mapping_page_id']); //$pageModel = CmsPages::findOne(['id' => $settingsModel->value]); return $this->render('mapping', ['steps' => $steps]); $user = Yii::$app->getUser()->getIdentity(); //@TODO new logic to find out if there are answers /*$answers = MembersQuestionsAnswers::find(['member_id' => $user->id])->count(); if ($answers > 0) { $user->status = User::STATUS_ACTIVE; if ($user->save()) { return Yii::$app->response->redirect(array('members/index')); } else { Yii::$app->getSession()->setFlash('warning', Yii::t('errors', 'Can not update user status. Please contact system administrator')); } }*/ if (Yii::$app->request->post()) { $mappingQuestionsSaved = false; if ($questionsAnswers->load(Yii::$app->request->post()) && $questionsAnswers->validate()) { foreach ($questionsAnswers->attributes as $attribute_id => $attribute) { $attributesArray = explode('_', $attribute_id); if (count($attributesArray) > 1) { list($name, $id) = $attributesArray; if (!($model = MembersQuestionsAnswers::findOne(['question_id' => $id, 'member_id' => Yii::$app->getUser()->id]))) { $model = new MembersQuestionsAnswers(); } $has_option = false; if ($questionsModel = MappingQuestions::findOne($id)) { $type_id = $questionsModel->type_id; $has_option = FieldsTypes::findOne($type_id)->has_options; } if ($has_option) { $model->value = ''; $model->option_id = ''; if ($optionsModel = MappingQuestionsToOptions::findOne($questionsAnswers->{$attribute_id})) { $model->value = $optionsModel->title; $model->option_id = $optionsModel->id; } } else { $model->value = $questionsAnswers->{$attribute_id}; } $model->question_id = $id; $model->member_id = Yii::$app->getUser()->id; $model->save(); } } $mappingQuestionsSaved = true; } if ($mappingQuestionsSaved) { $user = Yii::$app->getUser()->getIdentity(); //$user->status = User::STATUS_ACTIVE; if ($user->save()) { return Yii::$app->response->redirect(array('members')); } else { Yii::$app->getSession()->setFlash('warning', Yii::t('errors', 'Can not save mapping data. Please contact system administrator')); } } } //$categoryModel = MappingCategories::findOne(['id' => $pageModel->mapping_id]); $categoryModel = MappingCategories::find()->where(['is_active' => 1])->orderBy('sort_order')->all(); $questionsModel = \backend\models\MappingQuestions::find()->where(['category_id' => $category->id])->with('fieldsTypes')->all(); //$questionsModel = MappingQuestions::find()->where(['category_id' => $categoryModel->id])->with('fieldsTypes')->all(); return $this->render('mapping', ['questionsAnswers' => $questionsAnswers, 'pageModel' => $pageModel, 'categoryModel' => $categoryModel]); } else { return Yii::$app->response->redirect(array('site/login')); } }
<?php /* output all attributes related tot the mapping category */ $member_id = Yii::$app->getUser()->id; ?> <?php /* output all questions related tot the mapping category */ foreach ($questionsModel as $model) { $value = $option_id = ''; if ($answersModel = MembersQuestionsAnswers::findOne(['question_id' => $model->id, 'member_id' => $member_id])) { $value = $answersModel->value; $option_id = $answersModel->option_id; } if ($model->fieldsTypes[0]->has_options) { //if question has options display a dropdown echo $form->field($questionsAnswers, "value_{$model->id}")->dropDownList(ArrayHelper::map(MappingQuestionsToOptions::findAll(['question_id' => $model->id]), 'id', 'title'), ['prompt' => Yii::t('frontend', '-- Select --'), 'options' => [$option_id => ['Selected' => 'selected']]])->label($model->title); //$this->registerJs("$('form').yiiActiveForm('updateMessages', { //'MembersQuestionsAnswers[32][value]': ['I don\'t like it!'] //}, true);"); } else { // else if question does not have options display a text field echo $form->field($questionsAnswers, "value_{$model->id}")->textInput(['value' => $value]); } } ?> <div class="form-group"> <?php echo Html::submitButton(Yii::t('frontend', 'Save'), ['class' => 'btn btn-success']); ?> </div>
/** * @return \yii\db\ActiveQuery */ public function getPdfOptions() { return MappingQuestionsToOptions::find()->where(['id' => $this->options_id])->multilingual()->one(); }
/** * Deletes an existing MappingQuestions model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id, $cat_id) { $this->findModel($id)->delete(); MappingQuestionsToOptions::deleteAll(['question_id' => $id]); return $this->redirect(['mapping-categories/view', 'id' => $cat_id]); }
/** * Deletes an existing MappingCategories model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { foreach (MappingQuestions::find()->where(['category_id' => $id])->all() as $question) { MappingQuestionsToOptions::deleteAll(['question_id' => $question->id]); $question->delete(); } $this->findModel($id)->delete(); return $this->redirect(['index']); }