add filters, refactoring

This commit is contained in:
iIronside 2021-11-08 12:41:39 +03:00
parent 5be69526ac
commit c1c24fc5e4
42 changed files with 12364 additions and 443 deletions

View File

@ -5,7 +5,7 @@ namespace backend\modules\notes\models;
use Yii;
use common\models\FieldsValueNew;
class Note extends \common\models\Note
class kNote extends \common\models\Note
{
public $fields;

View File

@ -62,11 +62,16 @@ class AnswerController extends Controller
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
public function actionCreate($question_id = null)
{
$model = new Answer();
$model->question_id = $question_id;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
if ($question_id !== null)
{
return $this->redirect(['question/view', 'id' => $question_id]);
}
return $this->redirect(['view', 'id' => $model->id]);
}
@ -82,11 +87,15 @@ class AnswerController extends Controller
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
public function actionUpdate($id, $question_id = null)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
if ($question_id !== null)
{
return $this->redirect(['question/view', 'id' => $question_id]);
}
return $this->redirect(['view', 'id' => $model->id]);
}
@ -102,9 +111,13 @@ class AnswerController extends Controller
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
public function actionDelete($id, $question_id = null)
{
$this->findModel($id)->delete();
if ($question_id !== null)
{
return $this->redirect(['question/view', 'id' => $question_id]);
}
return $this->redirect(['index']);
}

View File

@ -2,9 +2,11 @@
namespace backend\modules\questionnaire\controllers;
use backend\modules\questionnaire\models\AnswerSearch;
use Yii;
use backend\modules\questionnaire\models\Question;
use backend\modules\questionnaire\models\QuestionSearch;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
@ -52,8 +54,19 @@ class QuestionController extends Controller
*/
public function actionView($id)
{
$model = $this->findModel($id);
$answerSearchModel = new AnswerSearch();
$answerDataProvider = new ActiveDataProvider([
'query' => $model->getAnswers(),
'pagination' => [
'pageSize' => 20,
],
]);
return $this->render('view', [
'model' => $this->findModel($id),
'model' => $model,
'answerSearchModel' => $answerSearchModel,
'answerDataProvider' => $answerDataProvider,
]);
}
@ -62,11 +75,23 @@ class QuestionController extends Controller
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
public function actionCreate($questionnaire_id = null, $question_type_id = null)
{
$model = new Question();
$model->questionnaire_id = $questionnaire_id;
$model->question_type_id = $question_type_id;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
if ($questionnaire_id !== null )
{
return $this->redirect(['questionnaire/view', 'id' => $questionnaire_id]);
}
elseif ($question_type_id !== null)
{
return $this->redirect(['question-type/view', 'id' => $question_type_id]);
}
return $this->redirect(['view', 'id' => $model->id]);
}
@ -82,11 +107,21 @@ class QuestionController extends Controller
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
public function actionUpdate(int $id, $questionnaire_id = null, $question_type_id = null)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
if ($questionnaire_id !== null)
{
return $this->redirect(['questionnaire/view', 'id' => $questionnaire_id]);
}
elseif ($question_type_id !== null)
{
return $this->redirect(['question-type/view', 'id' => $question_type_id]);
}
return $this->redirect(['view', 'id' => $model->id]);
}
@ -102,10 +137,20 @@ class QuestionController extends Controller
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
public function actionDelete(int $id, $questionnaire_id = null, $question_type_id = null)
{
$this->findModel($id)->delete();
if ($questionnaire_id !== null)
{
return $this->redirect(['questionnaire/view', 'id' => $questionnaire_id]);
}
elseif ($question_type_id !== null)
{
return $this->redirect(['question-type/view', 'id' => $question_type_id]);
}
return $this->redirect(['index']);
}

View File

@ -2,9 +2,11 @@
namespace backend\modules\questionnaire\controllers;
use backend\modules\questionnaire\models\QuestionSearch;
use Yii;
use backend\modules\questionnaire\models\QuestionType;
use backend\modules\questionnaire\models\QuestionTypeSearch;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
@ -52,8 +54,19 @@ class QuestionTypeController extends Controller
*/
public function actionView($id)
{
$model = $this->findModel($id);
$questionSearchModel = new QuestionSearch();
$questionDataProvider = new ActiveDataProvider([
'query' => $model->getQuestions(),
'pagination' => [
'pageSize' => 20,
],
]);
return $this->render('view', [
'model' => $this->findModel($id),
'questionDataProvider' => $questionDataProvider,
'questionSearchModel' => $questionSearchModel,
]);
}

View File

@ -2,9 +2,11 @@
namespace backend\modules\questionnaire\controllers;
use backend\modules\questionnaire\models\QuestionnaireSearch;
use Yii;
use backend\modules\questionnaire\models\QuestionnaireCategory;
use backend\modules\questionnaire\models\QuestionnaireCategorySearch;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
@ -52,8 +54,20 @@ class QuestionnaireCategoryController extends Controller
*/
public function actionView($id)
{
$model = $this->findModel($id);
$questionnaireSearchModel = new QuestionnaireSearch();
$questionnaireDataProvider = new ActiveDataProvider([
'query' => $model->getQuestionnaires(),
'pagination' => [
'pageSize' => 20,
],
]);
return $this->render('view', [
'model' => $this->findModel($id),
'model' => $model,
'questionnaireDataProvider' => $questionnaireDataProvider,
'questionnaireSearchModel' => $questionnaireSearchModel,
]);
}

View File

@ -76,11 +76,16 @@ class QuestionnaireController extends Controller
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
public function actionCreate($category_id = null)
{
$model = new Questionnaire();
$model->category_id = $category_id;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
if ($category_id !== null)
{
return $this->redirect(['questionnaire-category/view', 'id' => $model->category_id]);
}
return $this->redirect(['view', 'id' => $model->id]);
}
@ -96,11 +101,15 @@ class QuestionnaireController extends Controller
* @return string|Response
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate(int $id)
public function actionUpdate(int $id, $category_id = null)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
if ($category_id !== null)
{
return $this->redirect(['questionnaire-category/view', 'id' => $category_id]);
}
return $this->redirect(['view', 'id' => $model->id]);
}
@ -116,10 +125,15 @@ class QuestionnaireController extends Controller
* @return Response
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete(int $id)
public function actionDelete(int $id, $category_id = null)
{
$this->findModel($id)->delete();
if ($category_id !== null)
{
return $this->redirect(['questionnaire-category/view', 'id' => $category_id]);
}
return $this->redirect(['index']);
}

View File

@ -4,6 +4,7 @@ namespace backend\modules\questionnaire\controllers;
use backend\modules\questionnaire\models\Questionnaire;
use backend\modules\questionnaire\models\QuestionnaireCategory;
use common\helpers\ScoreCalculatorHelper;
use Yii;
use backend\modules\questionnaire\models\UserQuestionnaire;
use backend\modules\questionnaire\models\UserQuestionnaireSearch;
@ -11,7 +12,7 @@ use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\Response;
/**
@ -144,16 +145,22 @@ class UserQuestionnaireController extends Controller
throw new NotFoundHttpException('The requested page does not exist.');
}
public function actionQuestionnaire() {
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
public function actionQuestionnaire(): array
{
Yii::$app->response->format = Response::FORMAT_JSON;
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$cat_id = $parents[0];
$categories = Questionnaire::getQuestionnaireByCategory($cat_id);
$categories = Questionnaire::questionnairesOfCategoryArr($cat_id);
return ['output'=>$categories, 'selected'=>''];
$formattedCatArr = array();
foreach ($categories as $key => $value){
$formattedCatArr[] = array('id' => $key, 'name' => $value);
}
return ['output'=>$formattedCatArr, 'selected'=>''];
}
}
return ['output'=>'', 'selected'=>''];
@ -161,15 +168,16 @@ class UserQuestionnaireController extends Controller
public function actionRateResponses($id)
{
$model = $this->findModel($id);
$model->rateResponses();
$user_questionnaire = $this->findModel($id);
ScoreCalculatorHelper::rateResponses($user_questionnaire);
return $this->actionView($id);
}
public function actionCalculateScore($id)
{
$model = $this->findModel($id);
$model->getScore();
$user_questionnaire = $this->findModel($id);
ScoreCalculatorHelper::calculateScore($user_questionnaire);
return $this->actionView($id);
}

View File

@ -3,7 +3,6 @@
namespace backend\modules\questionnaire\controllers;
use backend\modules\questionnaire\models\Questionnaire;
use backend\modules\questionnaire\models\User;
use backend\modules\questionnaire\models\UserQuestionnaire;
use Yii;
use backend\modules\questionnaire\models\UserResponse;
@ -91,11 +90,16 @@ class UserResponseController extends Controller
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
public function actionUpdate(int $id, $user_questionnaire_id = null)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
if ($user_questionnaire_id !== null)
{
return $this->redirect(['user-questionnaire/view', 'id' => $user_questionnaire_id]);
}
return $this->redirect(['view', 'id' => $model->id]);
}

View File

@ -40,7 +40,7 @@ class AnswerSearch extends Answer
*/
public function search($params)
{
$query = Answer::find();
$query = Answer::find()->with('question');
// add conditions that should always apply here

View File

@ -40,7 +40,7 @@ class QuestionSearch extends Question
*/
public function search($params)
{
$query = Question::find();
$query = Question::find()->with(['questionnaire', 'questionType']);
// add conditions that should always apply here

View File

@ -40,7 +40,7 @@ class QuestionnaireSearch extends Questionnaire
*/
public function search($params)
{
$query = Questionnaire::find();
$query = Questionnaire::find()->with('category');
// add conditions that should always apply here

View File

@ -41,7 +41,7 @@ class UserQuestionnaireSearch extends UserQuestionnaire
*/
public function search($params)
{
$query = UserQuestionnaire::find();
$query = UserQuestionnaire::find()->with('questionnaire')->joinWith('user');
// add conditions that should always apply here

View File

@ -41,7 +41,7 @@ class UserResponseSearch extends UserResponse
*/
public function search($params)
{
$query = UserResponse::find();
$query = UserResponse::find()->with('user');
// add conditions that should always apply here

View File

@ -1,5 +1,8 @@
<?php
use backend\modules\questionnaire\models\Question;
use common\helpers\AnswerHelper;
use common\helpers\StatusHelper;
use kartik\select2\Select2;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
@ -14,23 +17,26 @@ use yii\widgets\ActiveForm;
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'question_id')->widget(Select2::class, [
'data' => \yii\helpers\ArrayHelper::map(\common\models\Question::find()->where(['!=', 'question_type_id', '1'])->all(), 'id', 'question_body'),
'data' => Question::find()->select(['question_body', 'id'])
->where(['!=', 'question_type_id', '1'])
->indexBy('id')
->column(),
'pluginOptions' => [
'allowClear' => true
'allowClear' => false
],
]) ?>
<?= $form->field($model, 'answer_body')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'answer_flag')->dropDownList(
\common\helpers\AnswerHelper::answerFlagsList(),
AnswerHelper::answerFlagsList(),
[
'prompt' => 'Выберите'
]
) ?>
<?= $form->field($model, 'status')->dropDownList(
\common\helpers\StatusHelper::statusList(),
StatusHelper::statusList(),
[
'prompt' => 'Выберите'
]

View File

@ -1,5 +1,8 @@
<?php
use backend\modules\questionnaire\models\Question;
use common\helpers\AnswerHelper;
use common\helpers\StatusHelper;
use yii\helpers\Html;
use yii\grid\GridView;
@ -24,27 +27,25 @@ $this->params['breadcrumbs'][] = $this->title;
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'filter' => \yii\helpers\ArrayHelper::map(\common\models\Question::find()->where(['!=', 'question_type_id', '1'])->all(), 'id', 'question_body'),
'filter' => Question::find()->select(['question_body', 'id'])->where(['!=', 'question_type_id', '1'])->indexBy('id')->column(),
'attribute' => 'question_id',
'value' => function($model){
return $model->getQuestionBody();
}
'value' => 'question.question_body'
],
'answer_body',
[
'attribute' => 'answer_flag',
'format' => 'raw',
'filter' => \common\helpers\AnswerHelper::answerFlagsList(),
'filter' => AnswerHelper::answerFlagsList(),
'value' => function ($model) {
return \common\helpers\AnswerHelper::answerStatusLabel($model->answer_flag);
return AnswerHelper::answerStatusLabel($model->answer_flag);
},
],
[
'attribute' => 'status',
'format' => 'raw',
'filter' => \common\helpers\StatusHelper::statusList(),
'filter' => StatusHelper::statusList(),
'value' => function($model){
return \common\helpers\StatusHelper::statusLabel($model->status);
return StatusHelper::statusLabel($model->status);
}
],
['class' => 'yii\grid\ActionColumn'],

View File

@ -1,5 +1,8 @@
<?php
use common\helpers\AnswerHelper;
use common\helpers\StatusHelper;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\widgets\DetailView;
@ -39,25 +42,19 @@ function cut_title($str)
'id',
[
'attribute' => 'question_id',
'value' => function($model){
return $model->getQuestionBody();
}
'value' => ArrayHelper::getValue($model, 'question.question_body'),
],
[
'attribute' => 'answer_flag',
'format' => 'raw',
'filter' => \common\helpers\AnswerHelper::answerFlagsList(),
'value' => function ($model) {
return \common\helpers\AnswerHelper::answerStatusLabel($model->status);
},
'filter' => AnswerHelper::answerFlagsList(),
'value' => AnswerHelper::answerStatusLabel($model->answer_flag),
],
[
'attribute' => 'status',
'format' => 'raw',
'filter' => \common\helpers\StatusHelper::statusList(),
'value' => function($model){
return \common\helpers\StatusHelper::statusLabel($model->status);
}
'filter' => StatusHelper::statusList(),
'value' => StatusHelper::statusLabel($model->status),
],
'created_at',
'updated_at',

View File

@ -1,10 +1,16 @@
<?php
use backend\modules\questionnaire\models\QuestionType;
use common\helpers\StatusHelper;
use common\helpers\TimeHelper;
use kartik\grid\GridView;
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model backend\modules\questionnaire\models\QuestionType */
/* @var $questionDataProvider yii\data\ActiveDataProvider */
/* @var $questionSearchModel backend\modules\questionnaire\models\QuestionSearch */
$this->title = $model->question_type;
$this->params['breadcrumbs'][] = ['label' => 'Question Types', 'url' => ['index']];
@ -34,4 +40,68 @@ $this->params['breadcrumbs'][] = $this->title;
],
]) ?>
<?= GridView::widget([
'dataProvider' => $questionDataProvider,
'filterModel' => $questionSearchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'question_body',
[
'attribute' => 'question_type_id',
'filter' => QuestionType::find()->select(['question_type', 'id'])->indexBy('id')->column(),
'value' => 'questionType.question_type'
],
'question_priority',
'next_question',
[
'attribute' => 'status',
'format' => 'raw',
'filter' => StatusHelper::statusList(),
'value' => function ($model) {
return StatusHelper::statusLabel($model->status);
},
],
[
'attribute' => 'time_limit',
'format' => 'raw',
'value' => function($model){
return TimeHelper::limitTime($model->time_limit);
}
],
'score',
[
'class' => 'yii\grid\ActionColumn',
'template' => '{view} {update} {delete}',
'controller' => 'question',
'buttons' => [
'update' => function ($url,$model) {
return Html::a(
'<span class="glyphicon glyphicon-pencil"></span>',
['question/update', 'id' => $model['id'], 'question_type_id' => $model['question_type_id']]);
},
'delete' => function ($url,$model) {
return Html::a(
'<span class="glyphicon glyphicon-trash"></span>',
[
'question/delete', 'id' => $model['id'], 'question_type_id' => $model['question_type_id']
],
[
'data' => ['confirm' => 'Вы уверены, что хотите удалить этот вопрос?', 'method' => 'post']
]
);
},
],
],
],
]); ?>
<p>
<?= Html::a(
'Добавить новый вопрос',
['question/create', 'question_type_id' => $model->id],
['class' => 'btn btn-primary']
) ?>
</p>
</div>

View File

@ -1,7 +1,10 @@
<?php
use backend\components\timepicker\src\TimePicker;
use backend\modules\questionnaire\models\Questionnaire;
use backend\modules\questionnaire\models\QuestionType;
use common\helpers\StatusHelper;
use kartik\select2\Select2;
use kartik\time\TimePicker;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
@ -16,7 +19,7 @@ use yii\widgets\ActiveForm;
<?= $form->field($model, 'question_type_id')->widget(Select2::class,
[
'data' => \yii\helpers\ArrayHelper::map(\common\models\QuestionType::find()->all(),'id', 'question_type'),
'data' => QuestionType::find()->select(['question_type', 'id'])->indexBy('id')->column(),
'options' => ['placeholder' => '...','class' => 'form-control'],
'pluginOptions' => [
'allowClear' => true
@ -26,7 +29,7 @@ use yii\widgets\ActiveForm;
<?= $form->field($model, 'questionnaire_id')->widget(Select2::class,
[
'data' => \yii\helpers\ArrayHelper::map(\common\models\Questionnaire::find()->all(),'id', 'title'),
'data' => Questionnaire::find()->select(['title', 'id'])->indexBy('id')->column(),
'options' => ['placeholder' => '...','class' => 'form-control'],
'pluginOptions' => [
'allowClear' => true
@ -41,13 +44,13 @@ use yii\widgets\ActiveForm;
<?= $form->field($model, 'next_question')->textInput() ?>
<?= $form->field($model, 'status')->dropDownList(
\common\helpers\StatusHelper::statusList(),
StatusHelper::statusList(),
[
'prompt' => 'Выберите'
]
) ?>
<?= $form->field($model, 'time_limit')->widget(\backend\components\timepicker\src\TimePicker::class,
<?= $form->field($model, 'time_limit')->widget(TimePicker::class,
[
'name' => 'time_limit_picker',
'pluginOptions' => [
@ -60,8 +63,6 @@ use yii\widgets\ActiveForm;
<?= $form->field($model, 'score')->textInput(['maxlength' => true]) ?>
<div class="form-group">
<?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>
</div>

View File

@ -1,5 +1,9 @@
<?php
use backend\modules\questionnaire\models\Questionnaire;
use backend\modules\questionnaire\models\QuestionType;
use common\helpers\StatusHelper;
use common\helpers\TimeHelper;
use yii\helpers\Html;
use yii\grid\GridView;
@ -24,33 +28,29 @@ $this->params['breadcrumbs'][] = $this->title;
'question_body',
[
'attribute' => 'question_type_id',
'filter' => \yii\helpers\ArrayHelper::map(\backend\modules\questionnaire\models\QuestionType::find()->all(), 'id', 'question_type'),
'value' => function($model){
return $model->getQuestionTitle();
}
'filter' => QuestionType::find()->select(['question_type', 'id'])->indexBy('id')->column(),
'value' => 'questionType.question_type'
],
[
'attribute' => 'questionnaire_id',
'filter' => \yii\helpers\ArrayHelper::map(\backend\modules\questionnaire\models\Questionnaire::find()->all(), 'id', 'title'),
'value' => function($model){
return $model->getQuestionnaireTitle();
}
'filter' => Questionnaire::find()->select(['title', 'id'])->indexBy('id')->column(),
'value' => 'questionnaire.title',
],
'question_priority',
'next_question',
[
'attribute' => 'status',
'format' => 'raw',
'filter' => \common\helpers\StatusHelper::statusList(),
'filter' => StatusHelper::statusList(),
'value' => function ($model) {
return \common\helpers\StatusHelper::statusLabel($model->status);
return StatusHelper::statusLabel($model->status);
},
],
[
'attribute' => 'time_limit',
'format' => 'raw',
'value' => function($model){
return \common\helpers\TimeHelper::limitTime($model->time_limit);
return TimeHelper::limitTime($model->time_limit);
}
],
'score',

View File

@ -1,10 +1,17 @@
<?php
use common\helpers\AnswerHelper;
use common\helpers\StatusHelper;
use common\helpers\TimeHelper;
use yii\grid\GridView;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model backend\modules\questionnaire\models\Question */
/* @var $answerSearchModel backend\modules\questionnaire\models\Question */
/* @var $answerDataProvider backend\modules\questionnaire\models\Question */
$this->title = $model->question_body;
$this->params['breadcrumbs'][] = ['label' => 'Questions', 'url' => ['index']];
@ -31,15 +38,11 @@ $this->params['breadcrumbs'][] = $this->title;
'id',
[
'attribute' => 'question_type_id',
'value' => function($model){
return $model->getQuestionTitle();
}
'value' => ArrayHelper::getValue($model, 'questionType.question_type'),
],
[
'attribute' => 'questionnaire_id',
'value' => function($model){
return $model->getQuestionnaireTitle();
}
'value' => ArrayHelper::getValue($model,'questionnaire.title'),
],
'question_body',
'question_priority',
@ -47,22 +50,81 @@ $this->params['breadcrumbs'][] = $this->title;
[
'attribute' => 'status',
'format' => 'raw',
'filter' => \common\helpers\StatusHelper::statusList(),
'value' => function ($model) {
return \common\helpers\StatusHelper::statusLabel($model->status);
},
'filter' => StatusHelper::statusList(),
'value' => StatusHelper::statusLabel($model->status),
],
'created_at',
'updated_at',
[
'attribute' => 'time_limit',
'format' => 'raw',
'value' => function($model){
return \common\helpers\TimeHelper::limitTime($model->time_limit);
}
'value' => TimeHelper::limitTime($model->time_limit),
],
'score'
],
]) ?>
<div>
<h2>
<?= 'Ответы: '?>
</h2>
</div>
<?= GridView::widget([
'dataProvider' => $answerDataProvider,
'filterModel' => $answerSearchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'answer_body',
[
'attribute' => 'answer_flag',
'format' => 'raw',
'filter' => AnswerHelper::answerFlagsList(),
'value' => function ($model) {
return AnswerHelper::answerStatusLabel($model->answer_flag);
},
],
[
'attribute' => 'status',
'format' => 'raw',
'filter' => StatusHelper::statusList(),
'value' => function($model){
return StatusHelper::statusLabel($model->status);
}
],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{view} {update} {delete}',
'controller' => 'answer',
'buttons' => [
'update' => function ($url,$model) {
return Html::a(
'<span class="glyphicon glyphicon-pencil"></span>',
['answer/update', 'id' => $model['id'], 'question_id' => $model['question_id']]);
},
'delete' => function ($url,$model) {
return Html::a(
'<span class="glyphicon glyphicon-trash"></span>',
[
'answer/delete', 'id' => $model['id'], 'question_id' => $model['question_id']
],
[
'data' => ['confirm' => 'Вы уверены, что хотите удалить этот вопрос?', 'method' => 'post']
]
);
},
],
],
],
]); ?>
<p>
<?= Html::a(
'Добавить новый ответ',
['answer/create', 'question_id' => $model->id],
['class' => 'btn btn-primary']
) ?>
</p>
</div>

View File

@ -1,11 +1,17 @@
<?php
use backend\modules\questionnaire\models\QuestionnaireCategory;
use common\helpers\StatusHelper;
use common\helpers\TimeHelper;
use kartik\grid\GridView;
use yii\helpers\Html;
use yii\web\YiiAsset;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model backend\modules\questionnaire\models\QuestionnaireCategory */
/* @var $questionnaireDataProvider yii\data\ActiveDataProvider */
/* @var $questionnaireSearchModel backend\modules\questionnaire\models\QuestionnaireCategory */
$this->title = $model->title;
$this->params['breadcrumbs'][] = ['label' => 'Questionnaire Categories', 'url' => ['index']];
@ -34,9 +40,9 @@ YiiAsset::register($this);
[
'attribute' => 'status',
'format' => 'raw',
'filter' => \common\helpers\StatusHelper::statusList(),
'filter' => StatusHelper::statusList(),
'value' => function($model) {
return \common\helpers\StatusHelper::statusLabel($model->status);
return StatusHelper::statusLabel($model->status);
},
],
'created_at',
@ -44,4 +50,65 @@ YiiAsset::register($this);
],
]) ?>
<?= GridView::widget([
'dataProvider' => $questionnaireDataProvider,
'filterModel' => $questionnaireSearchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'title',
[
'attribute' => 'status',
'format' => 'raw',
'filter' => StatusHelper::statusList(),
'value' => function($model){
return StatusHelper::statusLabel($model->status);
}
],
[
'attribute' => 'category_id',
'filter' => QuestionnaireCategory::find()->select(['title', 'id'])->indexBy('id')->column(),
'value' => 'category.title'
],
[
'attribute' => 'time_limit',
'format' => 'raw',
'value' => function($model){
return TimeHelper::limitTime($model->time_limit);
}
],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{view} {update} {delete}',
'controller' => 'questionnaire',
'buttons' => [
'update' => function ($url,$model) {
return Html::a(
'<span class="glyphicon glyphicon-pencil"></span>',
['questionnaire/update', 'id' => $model['id'], 'category_id' => $model['category_id']]);
},
'delete' => function ($url,$model) {
return Html::a(
'<span class="glyphicon glyphicon-trash"></span>',
[
'questionnaire/delete', 'id' => $model['id'], 'category_id' => $model['category_id']
],
[
'data' => ['confirm' => 'Вы уверены, что хотите удалить эту анкету?', 'method' => 'post']
]
);
},
],
],
],
]); ?>
<p>
<?= Html::a(
'Создать новую анкету',
['questionnaire/create', 'category_id' => $model->id],
['class' => 'btn btn-primary']
) ?>
</p>
</div>

View File

@ -1,7 +1,9 @@
<?php
use backend\components\timepicker\src\TimePicker;
use common\helpers\StatusHelper;
use backend\modules\questionnaire\models\QuestionnaireCategory;
use kartik\select2\Select2;
use kartik\time\TimePicker;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
@ -16,7 +18,7 @@ use yii\widgets\ActiveForm;
<?= $form->field($model, 'category_id')->widget(Select2::class,
[
'data' => \yii\helpers\ArrayHelper::map(\common\models\QuestionnaireCategory::find()->all(),'id', 'title'),
'data' => QuestionnaireCategory::find()->select(['title', 'id'])->indexBy('id')->column(),
'options' => ['placeholder' => '...','class' => 'form-control'],
'pluginOptions' => [
'allowClear' => true
@ -27,13 +29,13 @@ use yii\widgets\ActiveForm;
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'status')->dropDownList(
\common\helpers\StatusHelper::statusList(),
StatusHelper::statusList(),
[
'prompt' => 'Выберите'
]
) ?>
<?= $form->field($model, 'time_limit')->widget(\backend\components\timepicker\src\TimePicker::class,
<?= $form->field($model, 'time_limit')->widget(TimePicker::class,
[
'name' => 'time_limit_picker',
'pluginOptions' => [

View File

@ -1,5 +1,9 @@
<?php
use backend\modules\questionnaire\models\QuestionnaireCategory;
use common\helpers\StatusHelper;
use common\helpers\TimeHelper;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\grid\GridView;
@ -27,26 +31,25 @@ $this->params['breadcrumbs'][] = $this->title;
[
'attribute' => 'status',
'format' => 'raw',
'filter' => \common\helpers\StatusHelper::statusList(),
'filter' => StatusHelper::statusList(),
'value' => function($model){
return \common\helpers\StatusHelper::statusLabel($model->status);
return StatusHelper::statusLabel($model->status);
}
],
[
'attribute' => 'category_id',
'filter' => \yii\helpers\ArrayHelper::map(common\models\QuestionnaireCategory::find()->all(), 'id', 'title'),
'value' => function($model){
return $model->getCategoryTitle();
}
'filter' => QuestionnaireCategory::find()->select(['title', 'id'])->indexBy('id')->column(),
'value' => 'category.title'
],
[
'attribute' => 'time_limit',
'format' => 'raw',
'value' => function($model){
return \common\helpers\TimeHelper::limitTime($model->time_limit);
return TimeHelper::limitTime($model->time_limit);
}
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>

View File

@ -1,7 +1,12 @@
<?php
use backend\modules\questionnaire\models\QuestionType;
use common\helpers\StatusHelper;
use common\helpers\TimeHelper;
use yii\grid\GridView;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\web\YiiAsset;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
@ -12,7 +17,7 @@ use yii\widgets\DetailView;
$this->title = $model->title;
$this->params['breadcrumbs'][] = ['label' => 'Questionnaires', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
YiiAsset::register($this);
?>
<div class="questionnaire-view">
@ -22,7 +27,7 @@ $this->params['breadcrumbs'][] = $this->title;
<?= Html::a('Удалить', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'confirm' => 'Вы уверены, что хотите удалить эту анкету?',
'method' => 'post',
],
]) ?>
@ -34,26 +39,20 @@ $this->params['breadcrumbs'][] = $this->title;
'id',
[
'attribute' => 'category_id',
'value' => function($model){
return $model->getCategoryTitle();
}
'value' => ArrayHelper::getValue($model,'category.title')
],
'title',
[
'attribute' => 'status',
'format' => 'raw',
'value' => function ($model) {
return \common\helpers\StatusHelper::statusLabel($model->status);
},
'value' => StatusHelper::statusLabel($model->status),
],
'created_at',
'updated_at',
[
'attribute' => 'time_limit',
'format' => 'raw',
'value' => function($model){
return \common\helpers\TimeHelper::limitTime($model->time_limit);
}
'value' => TimeHelper::limitTime($model->time_limit),
],
],
]) ?>
@ -64,56 +63,68 @@ $this->params['breadcrumbs'][] = $this->title;
</h2>
</div>
<?php
echo GridView::widget([
<?= GridView::widget([
'dataProvider' => $questionDataProvider,
'filterModel' => $questionSearchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'question_body',
[
'attribute' => 'question_type_id',
'filter' => \yii\helpers\ArrayHelper::map(\backend\modules\questionnaire\models\QuestionType::find()->all(), 'id', 'question_type'),
'value' => function($model){
return $model->getQuestionTitle();
}
'filter' => QuestionType::find()->select(['question_type', 'id'])->indexBy('id')->column(),
'value' => 'questionType.question_type'
],
'question_body',
'question_priority',
'next_question',
[
'attribute' => 'status',
'format' => 'raw',
'filter' => \common\helpers\StatusHelper::statusList(),
'value' => function($model) {
return \common\helpers\StatusHelper::statusLabel($model->status);
'filter' => StatusHelper::statusList(),
'value' => function ($model) {
return StatusHelper::statusLabel($model->status);
},
],
[
'attribute' => 'time_limit',
'format' => 'raw',
'value' => function($model){
return \common\helpers\TimeHelper::limitTime($model->time_limit);
return TimeHelper::limitTime($model->time_limit);
}
],
'score',
[
'class' => 'yii\grid\ActionColumn',
'template' => '{view} {update}', // {delete}
'template' => '{view} {update} {delete}',
'controller' => 'question',
'buttons' => [
'view' => function ($url,$model) {
return Html::a(
'<span class="glyphicon glyphicon-eye-open"></span>',
['question/view', 'id' => $model['id']]);
},
'update' => function ($url,$model) {
return Html::a(
'<span class="glyphicon glyphicon-pencil"></span>',
['question/update', 'id' => $model['id']]);
['question/update', 'id' => $model['id'], 'questionnaire_id' => $model['questionnaire_id']]);
},
'delete' => function ($url,$model) {
return Html::a(
'<span class="glyphicon glyphicon-trash"></span>',
[
'question/delete', 'id' => $model['id'], 'questionnaire_id' => $model['questionnaire_id']
],
[
'data' => ['confirm' => 'Вы уверены, что хотите удалить этот вопрос?', 'method' => 'post']
]
);
},
],
],
],
]); ?>
]);
?>
<p>
<?= Html::a(
'Добавить новый вопрос',
['question/create', 'questionnaire_id' => $model->id],
['class' => 'btn btn-primary']
) ?>
</p>
</div>

View File

@ -1,5 +1,8 @@
<?php
use backend\modules\questionnaire\models\QuestionnaireCategory;
use common\helpers\StatusHelper;
use common\models\User;
use kartik\select2\Select2;
use yii\helpers\Html;
use yii\helpers\Url;
@ -17,8 +20,11 @@ use kartik\depdrop\DepDrop;
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($modelCategory, 'title')
->dropDownList($modelCategory->getIdTitlesArr(),
<?= $form->field($modelCategory, 'title')->dropDownList(QuestionnaireCategory::find()
->select(['title', 'id'])
->where(['status' => '1'])
->indexBy('id')
->column(),
[
'id' => 'cat-id',
'prompt' => 'Выберите'
@ -39,7 +45,7 @@ use kartik\depdrop\DepDrop;
<?= $form->field($model, 'user_id')->widget(Select2::class,
[
'data' => \yii\helpers\ArrayHelper::map(\common\models\User::find()->all(),'id', 'username'),
'data' => User::find()->select(['username', 'id'])->indexBy('id')->column(),
'options' => ['placeholder' => 'Выберите пользователя','class' => 'form-control'],
'pluginOptions' => [
'placeholder' => 'Выберите',
@ -49,7 +55,7 @@ use kartik\depdrop\DepDrop;
); ?>
<?= $form->field($model, 'status')->dropDownList(
\common\helpers\StatusHelper::statusList(),
StatusHelper::statusList(),
[
'prompt' => 'Выберите'
]

View File

@ -1,7 +1,11 @@
<?php
use common\helpers\StatusHelper;
use common\models\User;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\grid\GridView;
use backend\modules\questionnaire\models\Questionnaire;
/* @var $this yii\web\View */
/* @var $searchModel backend\modules\questionnaire\models\UserQuestionnaireSearch */
@ -23,17 +27,13 @@ $this->params['breadcrumbs'][] = $this->title;
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'questionnaire_id',
'filter' => \yii\helpers\ArrayHelper::map(backend\modules\questionnaire\models\Questionnaire::find()->all(), 'id', 'title'),
'value' => function($model){
return $model->getQuestionnaireTitle();
}
'filter' => Questionnaire::find()->select(['title', 'id'])->indexBy('id')->column(),
'value' => 'questionnaire.title'
],
[
'attribute' => 'user_id',
'filter' => \yii\helpers\ArrayHelper::map(\common\models\User::find()->all(), 'id', 'username'),
'value' => function($model){
return $model->getUserName();
}
'filter' => ArrayHelper::map(User::find()->all(), 'id', 'username'),
'value' => 'user.username'
],
'score',
[
@ -46,9 +46,9 @@ $this->params['breadcrumbs'][] = $this->title;
[
'attribute' => 'status',
'format' => 'raw',
'filter' => \common\helpers\StatusHelper::statusList(),
'filter' => StatusHelper::statusList(),
'value' => function ($model) {
return \common\helpers\StatusHelper::statusLabel($model->status);
return StatusHelper::statusLabel($model->status);
},
],
'created_at',

View File

@ -1,7 +1,11 @@
<?php
use common\helpers\ScoreCalculatorHelper;
use common\helpers\AnswerHelper;
use common\helpers\StatusHelper;
use yii\bootstrap\Modal;
use yii\grid\GridView;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\web\YiiAsset;
use yii\widgets\DetailView;
@ -51,11 +55,11 @@ YiiAsset::register($this);
'id',
[
'attribute' => 'questionnaires_id',
'value' => $questionnaire,
'value' => ArrayHelper::getValue($model, 'questionnaire.title'),
],
[
'attribute' => 'user_id',
'value' => $user,
'value' => ArrayHelper::getValue($model, 'user.username'),
],
'uuid',
'score',
@ -69,9 +73,7 @@ YiiAsset::register($this);
[
'attribute' => 'status',
'format' => 'raw',
'value' => function($model) {
return common\helpers\StatusHelper::statusLabel($model->status);
},
'value' => StatusHelper::statusLabel($model->status),
],
'created_at',
'updated_at',
@ -96,7 +98,7 @@ YiiAsset::register($this);
'class' => 'btn btn-success',
],
]);
if($model->checkAnswerFlagsForNull())
if(ScoreCalculatorHelper::checkAnswerFlagsForNull($model))
{
echo 'Ответы проверены. Посчитать баллы?';
echo Html::a('Посчитать баллы', ['calculate-score', 'id' => $model->id], [
@ -129,13 +131,11 @@ YiiAsset::register($this);
'response_body',
[
'attribute' => 'question_id',
'value' => function($model){
return $model->getQuestionBody();
}
'value' => 'question.question_body'
],
[
'attribute' => 'Тип вопроса',
'value' => function($model){
'value' => function($model) {
return $model->getQuestionType();
}
],
@ -143,18 +143,20 @@ YiiAsset::register($this);
'attribute' => 'answer_flag',
'format' => 'raw',
'value' => function ($model) {
return \common\helpers\AnswerHelper::answerFlagLable($model->answer_flag);
return AnswerHelper::answerFlagLable($model->answer_flag);
},
],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update}', // {delete}
'template' => '{view} {update} {delete}',
'controller' => 'user-response',
'buttons' => [
'update' => function ($url,$model) {
return Html::a(
'<span class="glyphicon glyphicon-pencil"></span>',
['user-response/update', 'id' => $model['id']]);
['user-response/update', 'id' => $model['id'], 'user_questionnaire_id' => $model['user_questionnaire_id']]);
},
],
],

View File

@ -1,5 +1,7 @@
<?php
use backend\modules\questionnaire\models\Question;
use backend\modules\questionnaire\models\UserQuestionnaire;
use kartik\select2\Select2;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
@ -29,7 +31,7 @@ use yii\widgets\ActiveForm;
) ?>
<?= $form->field($model, 'user_id')->widget(Select2::class, [
'data' => \yii\helpers\ArrayHelper::map(\common\models\User::find()->all(),'id', 'username'),
'data' => \common\models\User::find()->select(['username','id'] )->indexBy('id')->column(),
'options' => ['placeholder' => '...','class' => 'form-control'],
'pluginOptions' => [
'allowClear' => true
@ -38,16 +40,15 @@ use yii\widgets\ActiveForm;
]); ?>
<?= $form->field($model, 'question_id')->widget(Select2::class,[
'data' => \yii\helpers\ArrayHelper::map(\common\models\Question::find()
// ->where('question_type_id != :question_type_id', ['question_type_id' => 1])
->all(), 'id', 'question_body'),
'data' => Question::find()->select(['question_body', 'id'])->indexBy('id')->column(),
'pluginOptions' => [
'allowClear' => true // 'id != :id', ['id'=>1]
],
])?>
<?= $form->field($model, 'user_questionnaire_id')->widget(Select2::class,[
'data' => \yii\helpers\ArrayHelper::map(\common\models\UserQuestionnaire::find()->all(), 'id', 'id'),
'data' => UserQuestionnaire::find()->select(['id', 'id'])->indexBy('id')->column(),
'options' => ['placeholder' => '...','class' => 'form-control'],
'pluginOptions' => [
'allowClear' => true

View File

@ -1,7 +1,11 @@
<?php
use backend\modules\questionnaire\models\Questionnaire;
use backend\modules\questionnaire\models\UserQuestionnaire;
use common\helpers\AnswerHelper;
use common\models\User;
use kartik\depdrop\DepDrop;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\grid\GridView;
use yii\helpers\Url;
@ -18,63 +22,31 @@ $this->params['breadcrumbs'][] = $this->title;
?>
<div class="user-responses-index">
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a('Создать новый ответ пользователя', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<!-- --><?php //$form = ActiveForm::begin(); ?>
<!-- <?//= $form->field($model, 'user_id')->dropDownList(\yii\helpers\ArrayHelper::map(
// \common\models\User::find()->all(), 'id', 'username'),
// [
// 'id'=>'user-id',
// 'prompt' => 'Выберите пользователя'
// ]
// ) ?>
-->
<!-- <?//= $form->field($questionnaire, 'title')->widget(DepDrop::classname(), [
// 'options'=>['id'=>'questionnaire-id'],
// 'pluginOptions'=>[
// 'depends'=>['user-id'],
// 'placeholder'=>'Выберите...',
// 'url' => Url::to(['/questionnaire/user-response/questionnaire'])
// ]
// ]);?>
-->
<!-- --><?php //ActiveForm::end(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
// 'id',
// 'user_id',
[
'attribute' => 'user_questionnaire_id',
'filter' => Questionnaire::find()->select(['title', 'id'])->indexBy('id')->column(),
'value' => function($model){
return $model->getQuestionnaireTitle();
}
],
// 'user_questionnaire_id',
[
'attribute' => 'user_id',
'value' => function($model){
return $model->getUserName();
}
'filter' => User::find()->select(['username', 'id'])->indexBy('id')->column(),
'value' => 'user.username',
],
[
'attribute' => 'question_id',
'value' => function($model){
return $model->getQuestionBody();
}
'value' => 'question.question_body',
],
'response_body',
@ -86,15 +58,12 @@ $this->params['breadcrumbs'][] = $this->title;
],
[
'attribute' => 'answer_flag',
'filter' => AnswerHelper::answerFlagsList(),
'format' => 'raw',
'value' => function ($model) {
return \common\helpers\AnswerHelper::answerFlagLable($model->answer_flag);
return AnswerHelper::answerFlagLable($model->answer_flag);
},
],
// 'created_at',
// 'updated_at',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>

View File

@ -1,8 +1,10 @@
<?php
use common\helpers\AnswerHelper;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\web\YiiAsset;
use yii\widgets\DetailView;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $model backend\modules\questionnaire\models\UserResponse */
@ -11,7 +13,7 @@ use yii\grid\GridView;
$this->title =cut_title($model->response_body);
$this->params['breadcrumbs'][] = ['label' => 'User Responses', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
YiiAsset::register($this);
function cut_title($str)
{
@ -41,15 +43,11 @@ function cut_title($str)
'id',
[
'attribute' => 'Пользователь',
'value' => function($model){
return $model->getUserName();
}
'value' => ArrayHelper::getValue($model,'user.username'),
],
[
'attribute' => 'Вопрос',
'value' => function($model){
return $model->getQuestionBody();
}
'value' => ArrayHelper::getValue($model,'question.question_body'),
],
'response_body',
'created_at',
@ -57,9 +55,7 @@ function cut_title($str)
[
'attribute' => 'answer_flag',
'format' => 'raw',
'value' => function ($model) {
return \common\helpers\AnswerHelper::answerFlagLable($model->answer_flag);
},
'value' => AnswerHelper::answerFlagLable($model->answer_flag),
],
'user_questionnaires_uuid',
],

View File

@ -63,15 +63,15 @@
'badge' => '<span class="badge badge-info right">4</span>'
],
[
'label' => 'Анкеты', 'icon' => 'gears', 'url' => '#',
'label' => 'Анкеты', 'icon' => 'book', 'url' => '#',
'items' => [
['label' => 'Категории анкет', 'icon' => 'file-text-o', 'url' => ['/questionnaire/questionnaire-category'], 'active' => \Yii::$app->controller->id == 'questionnaire-category'],
['label' => 'Список анкет', 'icon' => 'file-text-o', 'url' => ['/questionnaire/questionnaire'], 'active' => \Yii::$app->controller->id == 'questionnaire'],
['label' => 'Типы вопросов', 'icon' => 'file-text-o', 'url' => ['/questionnaire/question-type'], 'active' => \Yii::$app->controller->id == 'question-type'],
['label' => 'Вопросы', 'icon' => 'file-text-o', 'url' => ['/questionnaire/question'], 'active' => \Yii::$app->controller->id == 'question'],
['label' => 'Ответы', 'icon' => 'file-text-o', 'url' => ['/questionnaire/answer'], 'active' => \Yii::$app->controller->id == 'answer'],
['label' => 'Анкеты пользователей', 'icon' => 'file-text-o', 'url' => ['/questionnaire/user-questionnaire'], 'active' => \Yii::$app->controller->id == 'user-questionnaire'],
['label' => 'Ответы пользователей', 'icon' => 'file-text-o', 'url' => ['/questionnaire/user-response'], 'active' => \Yii::$app->controller->id == 'user-response'],
['label' => 'Типы вопросов', 'icon' => 'pencil-square-o', 'url' => ['/questionnaire/question-type'], 'active' => \Yii::$app->controller->id == 'question-type'],
['label' => 'Категории анкет', 'icon' => 'pencil-square', 'url' => ['/questionnaire/questionnaire-category'], 'active' => \Yii::$app->controller->id == 'questionnaire-category'],
['label' => 'Список анкет', 'icon' => 'clipboard', 'url' => ['/questionnaire/questionnaire'], 'active' => \Yii::$app->controller->id == 'questionnaire'],
['label' => 'Вопросы', 'icon' => 'question', 'url' => ['/questionnaire/question'], 'active' => \Yii::$app->controller->id == 'question'],
['label' => 'Ответы', 'icon' => 'comment', 'url' => ['/questionnaire/answer'], 'active' => \Yii::$app->controller->id == 'answer'],
['label' => 'Анкеты пользователей', 'icon' => 'drivers-license', 'url' => ['/questionnaire/user-questionnaire'], 'active' => \Yii::$app->controller->id == 'user-questionnaire'],
['label' => 'Ответы пользователей', 'icon' => 'comments', 'url' => ['/questionnaire/user-response'], 'active' => \Yii::$app->controller->id == 'user-response'],
],
// 'visible' => Yii::$app->user->can('confidential_information')

View File

@ -0,0 +1,109 @@
<?php
namespace common\helpers;
use backend\modules\questionnaire\models\Answer;
use backend\modules\questionnaire\models\UserQuestionnaire;
use backend\modules\questionnaire\models\UserResponse;
use yii\helpers\ArrayHelper;
class ScoreCalculatorHelper
{
public static function rateResponses(UserQuestionnaire $user_questionnaire)
{
$responses = $user_questionnaire->getUserResponses()->all();
foreach ($responses as $response)
{
self::rateOneResponse($response);
}
}
public static function rateOneResponse(UserResponse $response)
{
if ($response->answer_flag === null && $response->getQuestionTypeValue() != 1) // not open question
{
$correct_answers = $response->getCorrectAnswers();
foreach ($correct_answers as $correct_answer)
{
if ($response->response_body === $correct_answer['answer_body'])
{
$response->answer_flag = 1;
$response->save();
return;
}
}
$response->answer_flag = 0;
$response->save();
}
}
public static function checkAnswerFlagsForNull(UserQuestionnaire $userQuestionnaire): bool
{
$responses = $userQuestionnaire->getUserResponses()->AsArray()->all();
foreach ($responses as $response)
{
if (ArrayHelper::isIn(null, $response))
return false;
}
return true;
}
public static function calculateScore(UserQuestionnaire $userQuestionnaire)
{
$responses_questions = $userQuestionnaire->hasMany(UserResponse::className(), ['user_questionnaire_id' => 'id'])
->joinWith('question')->asArray()->all();
$score = null;
$user_correct_answers_num = null;
foreach ($responses_questions as $response_question)
{
if(self::isCorrect($response_question['answer_flag']))
{
$user_correct_answers_num += 1;
switch ($response_question['question']['question_type_id'])
{
case '1': // open question
$score += $response_question['answer_flag'] * $response_question['question']['score'];
break;
case '2': // one answer
$score += $response_question['question']['score'];
break;
case '3': // multi answer
$score += $response_question['question']['score'] / self::correctAnswersNum($response_question['question']['id']);
break;
}
}
}
if($score !== null) {
self::setPercentCorrectAnswers($user_correct_answers_num, $userQuestionnaire);
$userQuestionnaire->score = round($score);
$userQuestionnaire->save();
}
}
protected static function isCorrect($answer_flag): bool
{
if ($answer_flag > 0) {
return true;
}
return false;
}
protected static function correctAnswersNum($question_id)
{
return Answer::numCorrectAnswers($question_id);
}
protected static function setPercentCorrectAnswers($user_correct_answers_num, UserQuestionnaire $userQuestionnaire)
{
$all_correct_answers_num = $userQuestionnaire->numCorrectAnswersWithoutOpenQuestions();
$all_correct_answers_num += $userQuestionnaire->numOpenQuestionsAnswers();
$percent = $user_correct_answers_num / $all_correct_answers_num;
$userQuestionnaire->percent_correct_answers = round($percent, 2);
$userQuestionnaire->save();
}
}

View File

@ -85,13 +85,7 @@ class Answer extends \yii\db\ActiveRecord
return $this->hasOne(Question::className(), ['id' => 'question_id']);
}
public function getQuestionBody()
{
return $this->getQuestion()->one()->question_body;
}
static function getCorrectAnswersNum($question_id)
static function numCorrectAnswers($question_id)
{
return Answer::find()
->where('question_id=:question_id', [':question_id' => $question_id])
@ -100,7 +94,7 @@ class Answer extends \yii\db\ActiveRecord
->count();
}
public static function getActiveAnswers($question_id): array
public static function activeAnswers($question_id): array
{
return self::find()->where(['question_id' => $question_id])
->andWhere(['status' => '1'])

View File

@ -2,7 +2,6 @@
namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;
@ -133,32 +132,7 @@ class Question extends \yii\db\ActiveRecord
return $this->hasMany(UserResponse::className(), ['question_id' => 'id']);
}
public function getStatusText()
{
return $this->statuses[$this->status];
}
public function getQuestionnaireTitle()
{
return $this->getQuestionnaire()->one()->title;
}
public function getQuestionTitle()
{
return $this->getQuestionType()->one()->question_type;
}
public function getLimitTime()
{
if ($this->time_limit === null)
{
return 'Не ограничено';
}
return date("i:s", mktime(null, null, $this->time_limit));
}
public static function getActiveQuestions($questionnaire_id)
public static function activeQuestions($questionnaire_id)
{
return self::find()->where(['questionnaire_id' => $questionnaire_id])
->andWhere(['status' => '1'])

View File

@ -2,8 +2,8 @@
namespace common\models;
use Yii;
use yii\behaviors\SluggableBehavior;
use yii\db\ActiveRecord;
/**
* This is the model class for table "question_type".
@ -14,7 +14,7 @@ use yii\behaviors\SluggableBehavior;
*
* @property Question[] $questions
*/
class QuestionType extends \yii\db\ActiveRecord
class QuestionType extends ActiveRecord
{
/**
* {@inheritdoc}

View File

@ -3,6 +3,8 @@
namespace common\models;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
use yii\db\Expression;
use yii\helpers\ArrayHelper;
@ -21,7 +23,7 @@ use yii\helpers\ArrayHelper;
* @property QuestionnaireCategory $category
* @property UserQuestionnaire[] $userQuestionnaires
*/
class Questionnaire extends \yii\db\ActiveRecord
class Questionnaire extends ActiveRecord
{
const STATUS_PASSIVE = 0;
const STATUS_ACTIVE = 1;
@ -92,7 +94,7 @@ class Questionnaire extends \yii\db\ActiveRecord
}
/**
* @return \yii\db\ActiveQuery
* @return ActiveQuery
*/
public function getQuestions()
{
@ -100,7 +102,7 @@ class Questionnaire extends \yii\db\ActiveRecord
}
/**
* @return \yii\db\ActiveQuery
* @return ActiveQuery
*/
public function getAnswers()
{
@ -109,36 +111,25 @@ class Questionnaire extends \yii\db\ActiveRecord
}
/**
* @return \yii\db\ActiveQuery
* @return ActiveQuery
*/
public function getCategory()
public function getCategory(): ActiveQuery
{
return $this->hasOne(QuestionnaireCategory::className(), ['id' => 'category_id']);
}
/**
* @return \yii\db\ActiveQuery
* @return ActiveQuery
*/
public function getUserQuestionnaires()
{
return $this->hasMany(UserQuestionnaire::className(), ['questionnaire_id' => 'id']);
}
public function getCategoryTitle()
{
return $this->getCategory()->one()->title;
}
public static function getQuestionnaireByCategory($category_id)
public static function questionnairesOfCategoryArr($category_id): array
{
$categories = self::find()->where(['category_id' => $category_id, 'status' => '1'])->all();
$catArr = ArrayHelper::map($categories, 'id', 'title');
$formattedCatArr = array();
foreach ($catArr as $key => $value){
$formattedCatArr[] = array('id' => $key, 'name' => $value);
}
return $formattedCatArr;
return ArrayHelper::map($categories, 'id', 'title');
}
}

View File

@ -3,6 +3,7 @@
namespace common\models;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;
use yii\db\Expression;
use yii\helpers\ArrayHelper;
@ -71,16 +72,10 @@ class QuestionnaireCategory extends \yii\db\ActiveRecord
}
/**
* @return \yii\db\ActiveQuery
* @return ActiveQuery
*/
public function getQuestionnaires()
{
return $this->hasMany(Questionnaire::className(), ['category_id' => 'id']);
}
public function getIdTitlesArr()
{
$categories = self::find()->select(['id', 'title'])->where(['status' => '1'])->all();
return ArrayHelper::map($categories,'id','title');
}
}

View File

@ -3,7 +3,6 @@
namespace common\models;
use Ramsey\Uuid\Uuid;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;
use yii\db\Expression;
@ -134,11 +133,6 @@ class UserQuestionnaire extends \yii\db\ActiveRecord
return $this->getUser()->one()->username;
}
public function getCategoryId(): string
{
return $this->created_at;
}
public static function getQuestionnaireByUser($id): array
{
$questionnaire = ArrayHelper::map(self::find()->where(['user_id' => $id])
@ -153,105 +147,13 @@ class UserQuestionnaire extends \yii\db\ActiveRecord
return $formatQuestionnaireArr;
}
public function getStatuses(): array
{
return [
self::STATUS_ACTIVE => 'Активен',
self::STATUS_PASSIVE => 'Не используется'
];
}
public function getStatusText()
{
return $this->statuses[$this->status];
}
public function checkAnswerFlagsForNull(): bool
{
$responses = $this->getUserResponses()->AsArray()->all();
foreach ($responses as $response)
{
if (ArrayHelper::isIn(null, $response))
return false;
}
return true;
}
public function getQuestions(): ActiveQuery
{
return $this->hasMany(Question::className(), ['id' => 'question_id'])
->viaTable('user_response', ['user_questionnaire_id' => 'id']);
}
public function getScore()
{
$responses_questions = $this->hasMany(UserResponse::className(), ['user_questionnaire_id' => 'id'])
->joinWith('question')->asArray()->all();
$calc_score = $this->calculateScore($responses_questions);
$this->score = $calc_score;
$this->save();
}
protected function calculateScore($responses_questions)
{
$score = null;
$user_correct_answers_num = null;
foreach ($responses_questions as $response_question)
{
if($this->isCorrect($response_question['answer_flag']))
{
$user_correct_answers_num += 1;
switch ($response_question['question']['question_type_id'])
{
case '1': // open question
$score += $response_question['answer_flag'] * $response_question['question']['score'];
break;
case '2': // one answer
$score += $response_question['question']['score'];
break;
case '3': // multi answer
$score += $response_question['question']['score'] / $this->correctAnswersNum($response_question['question']['id']);
break;
}
}
}
$this->setPercentCorrectAnswers($user_correct_answers_num);
if($score === null) {
return $score;
}
else {
return round($score);
}
}
protected function correctAnswersNum($question_id)
{
return Answer::getCorrectAnswersNum($question_id);
}
protected function isCorrect($answer_flag): bool
{
if ($answer_flag > 0) {
return true;
}
return false;
}
protected function setPercentCorrectAnswers($user_correct_answers_num)
{
$all_correct_answers_num = $this->numCorrectAnswersWithoutOpenQuestions();
$all_correct_answers_num += $this->numOpenQuestionsAnswers();
$percent = $user_correct_answers_num / $all_correct_answers_num;
$this->percent_correct_answers = round($percent, 2);
$this->save();
}
protected function numCorrectAnswersWithoutOpenQuestions()
public function numCorrectAnswersWithoutOpenQuestions()
{
return $this->hasMany(Answer::className(), ['question_id' => 'question_id'])
->viaTable('user_response', ['user_questionnaire_id' => 'id'])
@ -260,7 +162,7 @@ class UserQuestionnaire extends \yii\db\ActiveRecord
->count();
}
protected function numOpenQuestionsAnswers()
public function numOpenQuestionsAnswers()
{
return $this->hasMany(Question::className(), ['id' => 'question_id'])
->viaTable('user_response', ['user_questionnaire_id' => 'id'])
@ -268,16 +170,6 @@ class UserQuestionnaire extends \yii\db\ActiveRecord
->count();
}
public function rateResponses()
{
$responses = $this->getUserResponses()->all();
foreach ($responses as $response)
{
$response->rateResponse();
}
}
public static function findActiveUserQuestionnaires($user_id)
{
return self::find()->where(['user_id' => $user_id])

View File

@ -2,9 +2,9 @@
namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
use yii\db\Expression;
use yii\helpers\ArrayHelper;
use \backend\modules\questionnaire\models\UserQuestionnaire;
@ -25,7 +25,7 @@ use \backend\modules\questionnaire\models\UserQuestionnaire;
* @property Question $question
* @property User $user
*/
class UserResponse extends \yii\db\ActiveRecord
class UserResponse extends ActiveRecord
{
/**
* {@inheritdoc}
@ -104,17 +104,7 @@ class UserResponse extends \yii\db\ActiveRecord
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
public function getUserName()
{
return $this->getUser()->one()->username;
}
public function getQuestionBody()
{
return $this->getQuestion()->one()->question_body;
}
private function getCorrectAnswers()
public function getCorrectAnswers()
{
return $this->hasMany(Answer::class, ['question_id' => 'question_id'])
->where(['answer_flag' => '1'])->all();
@ -124,49 +114,18 @@ class UserResponse extends \yii\db\ActiveRecord
{
$tmp = $this->hasOne(Questionnaire::className(), ['id' => 'questionnaire_id'])
->viaTable('user_questionnaire', ['id' => 'user_questionnaire_id'])->one();
$value = ArrayHelper::getValue($tmp, 'title');
return $value;
return ArrayHelper::getValue($tmp, 'title');
}
public function getQuestionType()
{
$qType = $this->hasOne(QuestionType::class, ['id' => 'question_type_id'])
->viaTable('question', ['id' => 'question_id'])->one();
$value = ArrayHelper::getValue($qType, 'question_type');
return $value;
return ArrayHelper::getValue($this->hasOne(QuestionType::class, ['id' => 'question_type_id'])
->viaTable('question', ['id' => 'question_id'])->one(), 'question_type');
}
public function getQuestionTypeValue()
{
$qType = $this->getQuestion()->one();
$value = ArrayHelper::getValue($qType, 'question_type_id');
return $value;
}
public function rateResponse()
{
if ($this->answer_flag === null && $this->getQuestionTypeValue() != 1) // not open question
{
$correct_answers = $this->getCorrectAnswers();
foreach ($correct_answers as $correct_answer)
{
if ($this->response_body === $correct_answer['answer_body'])
{
$this->answer_flag = 1;
$this->save();
return;
}
}
$this->answer_flag = 0;
$this->save();
}
return ArrayHelper::getValue($qType, 'question_type_id');
}
}

File diff suppressed because it is too large Load Diff

View File

@ -40,7 +40,7 @@ class AnswerController extends Controller
throw new NotFoundHttpException('Incorrect questionnaire ID');
}
$answers = Answer::getActiveAnswers($question_id);
$answers = Answer::activeAnswers($question_id);
if(empty($answers)) {
throw new NotFoundHttpException('Active questionnaire not found');
}

View File

@ -40,7 +40,7 @@ class QuestionController extends \yii\rest\Controller
throw new NotFoundHttpException('Incorrect questionnaire ID');
}
$questions = Question::getActiveQuestions($questionnaire_id);
$questions = Question::activeQuestions($questionnaire_id);
if(empty($questions)) {
throw new NotFoundHttpException('Active questionnaire not found');
}