From 675d78b958ea5d02bd123f7d05b5ccd499f9f193 Mon Sep 17 00:00:00 2001 From: iironside Date: Tue, 15 Mar 2022 14:54:35 +0300 Subject: [PATCH 1/6] move ScoreCalculator fom helpers to services --- .../UserQuestionnaireController.php | 6 ++--- .../views/user-questionnaire/view.php | 17 ++----------- .../ScoreCalculatorService.php} | 11 ++++----- .../controllers/UserResponseController.php | 24 ++----------------- 4 files changed, 12 insertions(+), 46 deletions(-) rename common/{helpers/ScoreCalculatorHelper.php => services/ScoreCalculatorService.php} (92%) diff --git a/backend/modules/questionnaire/controllers/UserQuestionnaireController.php b/backend/modules/questionnaire/controllers/UserQuestionnaireController.php index 6bd9c28..ee451c3 100644 --- a/backend/modules/questionnaire/controllers/UserQuestionnaireController.php +++ b/backend/modules/questionnaire/controllers/UserQuestionnaireController.php @@ -4,7 +4,7 @@ namespace backend\modules\questionnaire\controllers; use backend\modules\questionnaire\models\Questionnaire; use backend\modules\questionnaire\models\QuestionnaireCategory; -use common\helpers\ScoreCalculatorHelper; +use common\services\ScoreCalculatorService; use Yii; use backend\modules\questionnaire\models\UserQuestionnaire; use backend\modules\questionnaire\models\UserQuestionnaireSearch; @@ -169,7 +169,7 @@ class UserQuestionnaireController extends Controller public function actionRateResponses($id) { $user_questionnaire = $this->findModel($id); - ScoreCalculatorHelper::rateResponses($user_questionnaire); + ScoreCalculatorService::rateResponses($user_questionnaire); return $this->actionView($id); } @@ -177,7 +177,7 @@ class UserQuestionnaireController extends Controller public function actionCalculateScore($id) { $user_questionnaire = $this->findModel($id); - ScoreCalculatorHelper::calculateScore($user_questionnaire); + ScoreCalculatorService::calculateScore($user_questionnaire); return $this->actionView($id); } diff --git a/backend/modules/questionnaire/views/user-questionnaire/view.php b/backend/modules/questionnaire/views/user-questionnaire/view.php index 9ae3e09..4342359 100644 --- a/backend/modules/questionnaire/views/user-questionnaire/view.php +++ b/backend/modules/questionnaire/views/user-questionnaire/view.php @@ -1,6 +1,6 @@ params['breadcrumbs'][] = ['label' => 'User Questionnaires', 'url' => ['i $this->params['breadcrumbs'][] = $this->title; YiiAsset::register($this); ?> - -registerJs( -// '$("document").ready(function(){ -// $("#new_note").on("pjax:end", function() { -// $.pjax.reload({container:"#user_responses"}); //Reload GridView -// }); -// });' -//); -?>
-setPercentCorrectAnswers(4)); die();?> -

'btn btn-primary']) ?> $model->id], ['class' => 'btn btn-primary']) ?> @@ -98,7 +86,7 @@ YiiAsset::register($this); 'class' => 'btn btn-success', ], ]); - if(ScoreCalculatorHelper::checkAnswerFlagsForNull($model)) + if(ScoreCalculatorService::checkAnswerFlagsForNull($model)) { echo 'Ответы проверены. Посчитать баллы?'; echo Html::a('Посчитать баллы', ['calculate-score', 'id' => $model->id], [ @@ -169,5 +157,4 @@ YiiAsset::register($this); ]); ?> -

diff --git a/common/helpers/ScoreCalculatorHelper.php b/common/services/ScoreCalculatorService.php similarity index 92% rename from common/helpers/ScoreCalculatorHelper.php rename to common/services/ScoreCalculatorService.php index 6d6aa9a..4168319 100644 --- a/common/helpers/ScoreCalculatorHelper.php +++ b/common/services/ScoreCalculatorService.php @@ -1,14 +1,13 @@ score = round($score); - $userQuestionnaire->save(); + self::setPercentCorrectAnswers($user_correct_answers_num, $userQuestionnaire); + $userQuestionnaire->score = round($score); + $userQuestionnaire->save(); // } } diff --git a/frontend/modules/api/controllers/UserResponseController.php b/frontend/modules/api/controllers/UserResponseController.php index 3662cc1..15533fc 100644 --- a/frontend/modules/api/controllers/UserResponseController.php +++ b/frontend/modules/api/controllers/UserResponseController.php @@ -2,13 +2,10 @@ namespace frontend\modules\api\controllers; -use common\helpers\ScoreCalculatorHelper; +use common\services\ScoreCalculatorService; use common\models\UserResponse; -use Exception; use Yii; use yii\base\InvalidConfigException; -use yii\filters\auth\HttpBearerAuth; -use yii\rest\ActiveController; use yii\web\BadRequestHttpException; use yii\web\ServerErrorHttpException; @@ -16,17 +13,6 @@ class UserResponseController extends ApiController { public $modelClass = 'common\models\UserResponse'; -// public function behaviors(): array -// { -// $behaviors = parent::behaviors(); -// -// $behaviors['authenticator']['authMethods'] = [ -// HttpBearerAuth::className(), -// ]; -// -// return $behaviors; -// } - public function verbs(): array { return [ @@ -35,12 +21,6 @@ class UserResponseController extends ApiController ]; } -// public function actions() -// { -// $actions = parent::actions(); -// unset($actions['create']); -// return $actions; -// } /** * @throws InvalidConfigException @@ -106,7 +86,7 @@ class UserResponseController extends ApiController protected function saveModel($model) { if ($model->save()) { - ScoreCalculatorHelper::rateOneResponse($model); + ScoreCalculatorService::rateOneResponse($model); $response = Yii::$app->getResponse(); $response->setStatusCode(201); } elseif (!$model->hasErrors()) { From a5ef1d5008d1d07cfa1c15f10ba129293a4dac5c Mon Sep 17 00:00:00 2001 From: iironside Date: Tue, 15 Mar 2022 16:54:34 +0300 Subject: [PATCH 2/6] refactor api/UserResponse --- common/services/TemplateService.php | 4 - common/services/UserQuestionnaireService.php | 8 ++ common/services/UserResponseService.php | 74 ++++++++++++++++++ .../UserQuestionnaireController.php | 18 +---- .../controllers/UserResponseController.php | 77 ++++--------------- 5 files changed, 100 insertions(+), 81 deletions(-) create mode 100644 common/services/UserQuestionnaireService.php create mode 100644 common/services/UserResponseService.php diff --git a/common/services/TemplateService.php b/common/services/TemplateService.php index d9b928a..c3d2d11 100644 --- a/common/services/TemplateService.php +++ b/common/services/TemplateService.php @@ -19,11 +19,7 @@ class TemplateService public static function getTemplateWithFields($template_id): array { return Template::find() -// ->select('title') ->joinWith('templateDocumentFields.field') -// ->with([ -// 'fields' => function ($query) { $query->select(['id', 'title', 'field_template']); } -// ]) ->where(['template.id' => $template_id]) ->asArray() ->one(); diff --git a/common/services/UserQuestionnaireService.php b/common/services/UserQuestionnaireService.php new file mode 100644 index 0000000..9efaf69 --- /dev/null +++ b/common/services/UserQuestionnaireService.php @@ -0,0 +1,8 @@ +load($userResponseParams, ''); + (new UserResponseService)->validateResponseModel($userResponse); + (new UserResponseService)->saveModel($userResponse); + return $userResponse; + } + + /** + * @throws BadRequestHttpException + * @throws ServerErrorHttpException + */ + public static function createUserResponses($userResponsesParams): array + { + $userResponseModels = array(); + foreach ($userResponsesParams['userResponses'] as $userResponseParams) { + $model = new UserResponse(); + $model->load($userResponseParams, ''); + (new UserResponseService)->validateResponseModel($model); + + array_push($userResponseModels, $model); + } + + foreach ($userResponseModels as $responseModel) { + (new UserResponseService)->saveModel($responseModel); + } + + return $userResponseModels; + } + + /** + * @throws BadRequestHttpException + */ + protected function validateResponseModel($model) + { + if (!$model->validate()) { + throw new BadRequestHttpException(json_encode($model->errors)); + } + + if (empty($model->user_id) or empty($model->question_id) or empty($model->user_questionnaire_uuid)) { + throw new BadRequestHttpException(json_encode('One of the parameters is empty!')); + } + } + + /** + * @throws ServerErrorHttpException + */ + protected function saveModel($model) + { + if ($model->save()) { + ScoreCalculatorService::rateOneResponse($model); + $response = Yii::$app->getResponse(); + $response->setStatusCode(201); + } elseif (!$model->hasErrors()) { + throw new ServerErrorHttpException('Failed to create the object for unknown reason.'); + } + } +} \ No newline at end of file diff --git a/frontend/modules/api/controllers/UserQuestionnaireController.php b/frontend/modules/api/controllers/UserQuestionnaireController.php index 1d77ccf..79e450a 100644 --- a/frontend/modules/api/controllers/UserQuestionnaireController.php +++ b/frontend/modules/api/controllers/UserQuestionnaireController.php @@ -2,26 +2,14 @@ namespace frontend\modules\api\controllers; -use common\helpers\ScoreCalculatorHelper; +use common\services\ScoreCalculatorService; use common\models\UserQuestionnaire; use Yii; use yii\filters\auth\HttpBearerAuth; -use yii\rest\Controller; use yii\web\NotFoundHttpException; class UserQuestionnaireController extends ApiController { - public function behaviors() - { - $behaviors = parent::behaviors(); - - $behaviors['authenticator']['authMethods'] = [ - HttpBearerAuth::className(), - ]; - - return $behaviors; - } - public function verbs() { return [ @@ -75,8 +63,8 @@ class UserQuestionnaireController extends ApiController throw new NotFoundHttpException('Active questionnaire not found'); } - ScoreCalculatorHelper::rateResponses($userQuestionnaireModel); - ScoreCalculatorHelper::calculateScore($userQuestionnaireModel); + ScoreCalculatorService::rateResponses($userQuestionnaireModel); + ScoreCalculatorService::calculateScore($userQuestionnaireModel); return $userQuestionnaireModel; } diff --git a/frontend/modules/api/controllers/UserResponseController.php b/frontend/modules/api/controllers/UserResponseController.php index 15533fc..694cd31 100644 --- a/frontend/modules/api/controllers/UserResponseController.php +++ b/frontend/modules/api/controllers/UserResponseController.php @@ -2,8 +2,8 @@ namespace frontend\modules\api\controllers; -use common\services\ScoreCalculatorService; use common\models\UserResponse; +use common\services\UserResponseService; use Yii; use yii\base\InvalidConfigException; use yii\web\BadRequestHttpException; @@ -11,8 +11,6 @@ use yii\web\ServerErrorHttpException; class UserResponseController extends ApiController { - public $modelClass = 'common\models\UserResponse'; - public function verbs(): array { return [ @@ -21,76 +19,31 @@ class UserResponseController extends ApiController ]; } - /** * @throws InvalidConfigException - * @throws BadRequestHttpException - * @throws ServerErrorHttpException + * @throws ServerErrorHttpException|BadRequestHttpException */ - public function actionSetResponse() + public function actionSetResponse(): UserResponse { - $request = Yii::$app->getRequest()->getBodyParams(); - - $model = new UserResponse(); - $model->load($request, ''); - - $this->validateResponseModel($model); - $this->saveModel($model); - - return $model; + $userResponseModel = UserResponseService::createUserResponse(Yii::$app->getRequest()->getBodyParams()); + if ($userResponseModel->errors) { + throw new ServerErrorHttpException(json_encode($userResponseModel->errors)); + } + return $userResponseModel; } /** * @throws InvalidConfigException - * @throws ServerErrorHttpException - * @throws BadRequestHttpException + * @throws ServerErrorHttpException|BadRequestHttpException */ public function actionSetResponses(): array { - $requests = Yii::$app->getRequest()->getBodyParams(); - - $responseModels = array(); - - foreach ($requests['userResponses'] as $request) { - $model = new UserResponse(); - $model->load($request, ''); - $this->validateResponseModel($model); - - array_push($responseModels, $model); - } - - foreach ($responseModels as $responseModel) { - $this->saveModel($responseModel); - } - - return $responseModels; - } - - /** - * @throws BadRequestHttpException - */ - protected function validateResponseModel($model) - { - if(!$model->validate()) { - throw new BadRequestHttpException(json_encode($model->errors)); - } - - if (empty($model->user_id) or empty($model->question_id) or empty($model->user_questionnaire_uuid)) { - throw new BadRequestHttpException(json_encode($model->errors)); - } - } - - /** - * @throws ServerErrorHttpException - */ - protected function saveModel($model) - { - if ($model->save()) { - ScoreCalculatorService::rateOneResponse($model); - $response = Yii::$app->getResponse(); - $response->setStatusCode(201); - } elseif (!$model->hasErrors()) { - throw new ServerErrorHttpException('Failed to create the object for unknown reason.'); + $userResponseModels = UserResponseService::createUserResponses(Yii::$app->getRequest()->getBodyParams()); + foreach ($userResponseModels as $model) { + if ($model->errors) { + throw new ServerErrorHttpException(json_encode($model->errors)); + } } + return $userResponseModels; } } From 18d2b4327d5d1df91b3ab33abed54624cb226397 Mon Sep 17 00:00:00 2001 From: iironside Date: Tue, 15 Mar 2022 17:56:45 +0300 Subject: [PATCH 3/6] refactor api/UserQuestionnaire --- common/services/UserQuestionnaireService.php | 29 ++++++++++ .../UserQuestionnaireController.php | 53 +++++-------------- 2 files changed, 43 insertions(+), 39 deletions(-) diff --git a/common/services/UserQuestionnaireService.php b/common/services/UserQuestionnaireService.php index 9efaf69..9a8022d 100644 --- a/common/services/UserQuestionnaireService.php +++ b/common/services/UserQuestionnaireService.php @@ -2,7 +2,36 @@ namespace common\services; +use common\models\UserQuestionnaire; +use yii\web\NotFoundHttpException; + class UserQuestionnaireService { + public static function getQuestionnaireList($user_id): array + { + $userQuestionnaireModels = UserQuestionnaire::findActiveUserQuestionnaires($user_id); + array_walk($userQuestionnaireModels, function (&$arr) { + unset( + $arr['questionnaire_id'], + $arr['created_at'], + $arr['updated_at'], + $arr['id'], + ); + }); + return $userQuestionnaireModels; + } + /** + * @throws NotFoundHttpException + */ + public static function calculateScore($user_questionnaire_uuid) + { + $userQuestionnaireModel = UserQuestionnaire::findOne(['uuid' => $user_questionnaire_uuid]); + if(empty($userQuestionnaireModel)) { + throw new NotFoundHttpException('The questionnaire with this uuid does not exist'); + } + ScoreCalculatorService::rateResponses($userQuestionnaireModel); + ScoreCalculatorService::calculateScore($userQuestionnaireModel); + return $userQuestionnaireModel; + } } \ No newline at end of file diff --git a/frontend/modules/api/controllers/UserQuestionnaireController.php b/frontend/modules/api/controllers/UserQuestionnaireController.php index 79e450a..4dfe8cd 100644 --- a/frontend/modules/api/controllers/UserQuestionnaireController.php +++ b/frontend/modules/api/controllers/UserQuestionnaireController.php @@ -2,11 +2,9 @@ namespace frontend\modules\api\controllers; -use common\services\ScoreCalculatorService; -use common\models\UserQuestionnaire; -use Yii; -use yii\filters\auth\HttpBearerAuth; +use common\services\UserQuestionnaireService; use yii\web\NotFoundHttpException; +use yii\web\ServerErrorHttpException; class UserQuestionnaireController extends ApiController { @@ -21,51 +19,28 @@ class UserQuestionnaireController extends ApiController /** * @throws NotFoundHttpException */ - public function actionQuestionnairesList()//: array + public function actionQuestionnairesList($user_id): array { - $user_id = Yii::$app->request->get('user_id'); - - if(empty($user_id) or !is_numeric($user_id)) - { + if (empty($user_id) or !is_numeric($user_id)) { throw new NotFoundHttpException('Incorrect user ID'); } - - $userQuestionnaireModels = UserQuestionnaire::findActiveUserQuestionnaires($user_id); + $userQuestionnaireModels = UserQuestionnaireService::getQuestionnaireList($user_id); if(empty($userQuestionnaireModels)) { throw new NotFoundHttpException('Active questionnaire not found'); } - - array_walk( $userQuestionnaireModels, function(&$arr){ - unset( - $arr['questionnaire_id'], -// $arr['created_at'], -// $arr['updated_at'], - $arr['id'], - ); - }); - - return $userQuestionnaireModels; + return $userQuestionnaireModels; } - public function actionQuestionnaireCompleted() + /** + * @throws NotFoundHttpException + * @throws ServerErrorHttpException + */ + public function actionQuestionnaireCompleted($user_questionnaire_uuid) { -// return Yii::$app->request; - $user_questionnaire_uuid = Yii::$app->request->get('user_questionnaire_uuid'); - - if(empty($user_questionnaire_uuid)) - { - throw new NotFoundHttpException('Incorrect user ID'); + $userQuestionnaireModel = UserQuestionnaireService::calculateScore($user_questionnaire_uuid); + if ($userQuestionnaireModel->errors) { + throw new ServerErrorHttpException(json_encode($userQuestionnaireModel->errors)); } - - $userQuestionnaireModel = UserQuestionnaire::findOne(['uuid' => $user_questionnaire_uuid]); - - if(empty($userQuestionnaireModel)) { - throw new NotFoundHttpException('Active questionnaire not found'); - } - - ScoreCalculatorService::rateResponses($userQuestionnaireModel); - ScoreCalculatorService::calculateScore($userQuestionnaireModel); - return $userQuestionnaireModel; } } From 97e79f6c94bd3bbfe488330f0eb33bd09ebaef7a Mon Sep 17 00:00:00 2001 From: iironside Date: Tue, 15 Mar 2022 19:30:53 +0300 Subject: [PATCH 4/6] fix answerFlagLabel method in AnswerHelper --- .../questionnaire/views/user-response/index.php | 7 ------- common/helpers/AnswerHelper.php | 10 ++++++---- common/services/ScoreCalculatorService.php | 9 +++++++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/backend/modules/questionnaire/views/user-response/index.php b/backend/modules/questionnaire/views/user-response/index.php index ba97d41..52a3b82 100644 --- a/backend/modules/questionnaire/views/user-response/index.php +++ b/backend/modules/questionnaire/views/user-response/index.php @@ -20,13 +20,6 @@ $this->params['breadcrumbs'][] = $this->title; 'btn btn-success']) ?>

- render('_search_by_questionnaire', [ -// 'model' => $searchModel, -// ]) - ?> - - $dataProvider, 'filterModel' => $searchModel, diff --git a/common/helpers/AnswerHelper.php b/common/helpers/AnswerHelper.php index afd3d4b..baf5f3e 100644 --- a/common/helpers/AnswerHelper.php +++ b/common/helpers/AnswerHelper.php @@ -10,12 +10,14 @@ class AnswerHelper { const FLAG_TRUE = 1; const FLAG_FALSE = 0; + const FLAG_NOT_VERIFIED = null; public static function answerFlagsList(): array { return [ self::FLAG_TRUE => 'Верен', self::FLAG_FALSE => 'Ошибочный', + self::FLAG_NOT_VERIFIED => 'Не проверен', ]; } @@ -24,15 +26,15 @@ class AnswerHelper */ public static function answerFlagLabel($status): string { - switch ($status) { - case self::FLAG_FALSE: + switch (true) { + case ($status === self::FLAG_FALSE): $class = 'label label-danger'; break; - case self::FLAG_TRUE: + case ($status === self::FLAG_TRUE): $class = 'label label-success'; break; default: - $class = 'label label-default'; + $class = 'label label-warning'; } return Html::tag('span', ArrayHelper::getValue(self::answerFlagsList(), $status), [ diff --git a/common/services/ScoreCalculatorService.php b/common/services/ScoreCalculatorService.php index 4168319..b7318de 100644 --- a/common/services/ScoreCalculatorService.php +++ b/common/services/ScoreCalculatorService.php @@ -5,6 +5,7 @@ namespace common\services; use backend\modules\questionnaire\models\Answer; use common\models\UserQuestionnaire; use common\models\UserResponse; +use yii\base\InvalidConfigException; use yii\helpers\ArrayHelper; class ScoreCalculatorService @@ -44,6 +45,9 @@ class ScoreCalculatorService return true; } + /** + * @throws InvalidConfigException + */ public static function calculateScore(UserQuestionnaire $userQuestionnaire) { $responses_questions = $userQuestionnaire->hasMany(UserResponse::className(), ['user_questionnaire_uuid' => 'uuid']) @@ -71,11 +75,9 @@ class ScoreCalculatorService } } -// if($score !== null) { self::setPercentCorrectAnswers($user_correct_answers_num, $userQuestionnaire); $userQuestionnaire->score = round($score); $userQuestionnaire->save(); -// } } protected static function isCorrect($answer_flag): bool @@ -91,6 +93,9 @@ class ScoreCalculatorService return Answer::numCorrectAnswers($question_id); } + /** + * @throws InvalidConfigException + */ protected static function setPercentCorrectAnswers($user_correct_answers_num, UserQuestionnaire $userQuestionnaire) { if($user_correct_answers_num !== null) { From 86853f2a5924d51625238ba57b3d2920534bdcaa Mon Sep 17 00:00:00 2001 From: iironside Date: Wed, 16 Mar 2022 12:55:44 +0300 Subject: [PATCH 5/6] fix answerFlagLabel method in AnswerHelper, fix setPercentCorrectAnswers method in ScoreCalculatorService --- .../views/document-field-value/view.php | 4 +-- .../questionnaire/views/answer/index.php | 2 +- .../views/user-questionnaire/view.php | 2 +- .../views/user-response/index.php | 2 +- .../views/user-response/view.php | 2 +- common/helpers/AnswerHelper.php | 12 ++++----- common/models/UserQuestionnaire.php | 9 ++++--- frontend-access.log | 26 +++++++++++++++++++ 8 files changed, 42 insertions(+), 17 deletions(-) diff --git a/backend/modules/document/views/document-field-value/view.php b/backend/modules/document/views/document-field-value/view.php index c1fef11..e287486 100644 --- a/backend/modules/document/views/document-field-value/view.php +++ b/backend/modules/document/views/document-field-value/view.php @@ -33,11 +33,11 @@ $this->params['breadcrumbs'][] = $this->title; 'id', [ 'attribute' => 'field_id', - 'value' => ArrayHelper::getValue($model, 'field.title') // AnswerHelper::answerFlagLabel($model->answer_flag), + 'value' => ArrayHelper::getValue($model, 'field.title') ], [ 'attribute' => 'document_id', - 'value' => ArrayHelper::getValue($model, 'document.title') // AnswerHelper::answerFlagLabel($model->answer_flag), + 'value' => ArrayHelper::getValue($model, 'document.title') ], 'value', ], diff --git a/backend/modules/questionnaire/views/answer/index.php b/backend/modules/questionnaire/views/answer/index.php index 62ee1ce..53c489b 100644 --- a/backend/modules/questionnaire/views/answer/index.php +++ b/backend/modules/questionnaire/views/answer/index.php @@ -47,7 +47,7 @@ $this->params['breadcrumbs'][] = $this->title; 'format' => 'raw', 'filter' => AnswerHelper::answerFlagsList(), 'value' => function ($model) { - return AnswerHelper::answerFlagLabel($model->answer_flag); + return AnswerHelper:: answerFlagLabel($model->answer_flag); }, ], [ diff --git a/backend/modules/questionnaire/views/user-questionnaire/view.php b/backend/modules/questionnaire/views/user-questionnaire/view.php index 4342359..8ed5861 100644 --- a/backend/modules/questionnaire/views/user-questionnaire/view.php +++ b/backend/modules/questionnaire/views/user-questionnaire/view.php @@ -129,7 +129,7 @@ YiiAsset::register($this); 'attribute' => 'answer_flag', 'format' => 'raw', 'value' => function ($model) { - return AnswerHelper::answerStatusLabel($model->answer_flag); + return AnswerHelper::userResponseLabel($model->answer_flag); }, ], diff --git a/backend/modules/questionnaire/views/user-response/index.php b/backend/modules/questionnaire/views/user-response/index.php index 52a3b82..69b99bb 100644 --- a/backend/modules/questionnaire/views/user-response/index.php +++ b/backend/modules/questionnaire/views/user-response/index.php @@ -43,7 +43,7 @@ $this->params['breadcrumbs'][] = $this->title; 'format' => 'raw', 'filter' => AnswerHelper::answerFlagsList(), 'value' => function ($model) { - return AnswerHelper::answerStatusLabel($model->answer_flag); + return AnswerHelper::userResponseLabel($model->answer_flag); }, ], [ diff --git a/backend/modules/questionnaire/views/user-response/view.php b/backend/modules/questionnaire/views/user-response/view.php index c52cab3..3896b20 100644 --- a/backend/modules/questionnaire/views/user-response/view.php +++ b/backend/modules/questionnaire/views/user-response/view.php @@ -44,7 +44,7 @@ YiiAsset::register($this); [ 'attribute' => 'answer_flag', 'format' => 'raw', - 'value' => AnswerHelper::answerFlagLabel($model->answer_flag), + 'value' => AnswerHelper::userResponseLabel($model->answer_flag), ], 'user_questionnaire_uuid', ], diff --git a/common/helpers/AnswerHelper.php b/common/helpers/AnswerHelper.php index baf5f3e..4c68d83 100644 --- a/common/helpers/AnswerHelper.php +++ b/common/helpers/AnswerHelper.php @@ -10,14 +10,12 @@ class AnswerHelper { const FLAG_TRUE = 1; const FLAG_FALSE = 0; - const FLAG_NOT_VERIFIED = null; public static function answerFlagsList(): array { return [ self::FLAG_TRUE => 'Верен', self::FLAG_FALSE => 'Ошибочный', - self::FLAG_NOT_VERIFIED => 'Не проверен', ]; } @@ -26,15 +24,15 @@ class AnswerHelper */ public static function answerFlagLabel($status): string { - switch (true) { - case ($status === self::FLAG_FALSE): + switch ($status) { + case self::FLAG_FALSE: $class = 'label label-danger'; break; - case ($status === self::FLAG_TRUE): + case self::FLAG_TRUE: $class = 'label label-success'; break; default: - $class = 'label label-warning'; + $class = 'label label-default'; } return Html::tag('span', ArrayHelper::getValue(self::answerFlagsList(), $status), [ @@ -42,7 +40,7 @@ class AnswerHelper ]); } - public static function answerStatusLabel($answer_flag): string + public static function userResponseLabel($answer_flag): string { $class = 'label label-warning'; $content = 'Не проверен'; diff --git a/common/models/UserQuestionnaire.php b/common/models/UserQuestionnaire.php index ec875cd..55abb34 100644 --- a/common/models/UserQuestionnaire.php +++ b/common/models/UserQuestionnaire.php @@ -150,8 +150,8 @@ class UserQuestionnaire extends ActiveRecord */ public function numCorrectAnswersWithoutOpenQuestions() { - return $this->hasMany(Answer::className(), ['question_id' => 'question_id']) - ->viaTable('user_response', ['user_questionnaire_uuid' => 'uuid']) + return $this->hasMany(Answer::className(), ['question_id' => 'id']) + ->viaTable('question', ['questionnaire_id' => 'questionnaire_id']) ->where(['answer_flag' => '1']) ->andWhere(['status' => '1']) ->count(); @@ -162,9 +162,10 @@ class UserQuestionnaire extends ActiveRecord */ public function numOpenQuestionsAnswers() { - return $this->hasMany(Question::className(), ['id' => 'question_id']) - ->viaTable('user_response', ['user_questionnaire_uuid' => 'uuid']) + return $this->hasMany(Question::className(), ['questionnaire_id' => 'id']) + ->viaTable('questionnaire', ['id' => 'questionnaire_id']) ->where(['question_type_id' => '1']) + ->andWhere(['status' => '1']) ->count(); } diff --git a/frontend-access.log b/frontend-access.log index 1bc3112..708ac13 100644 --- a/frontend-access.log +++ b/frontend-access.log @@ -97005,3 +97005,29 @@ 127.0.0.1 - - [11/Mar/2022:10:48:38 +0300] "GET /reports/ajax/get-reports-for-day-by-date?date=2022-01-31 HTTP/1.1" 200 456 "http://backend.guild.loc/reports/reports" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" 127.0.0.1 - - [11/Mar/2022:10:48:38 +0300] "GET /reports/ajax/get-reports-for-month-by-id-year-month?&month=01&year=2022 HTTP/1.1" 200 12 "http://backend.guild.loc/reports/reports" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" 127.0.0.1 - - [11/Mar/2022:10:48:38 +0300] "GET /reports/ajax/get-reports-for-day-by-date?date=2022-01-31 HTTP/1.1" 200 456 "http://backend.guild.loc/reports/reports" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:51 +0300] "GET /questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5 HTTP/1.1" 200 13709 "http://backend.guild.loc/questionnaire/user-questionnaire/view?id=1" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /assets/39b83f70/css/select2.css HTTP/1.1" 200 17358 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /assets/71772193/css/bootstrap.css HTTP/1.1" 200 145933 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /assets/5b57ee2f/css/select2-addl.css HTTP/1.1" 200 994 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /assets/5b57ee2f/css/select2-krajee.css HTTP/1.1" 200 20817 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /assets/bae3a4f/css/kv-widgets.css HTTP/1.1" 200 813 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /css/site.css HTTP/1.1" 200 805 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /assets/ccdf1f3a/css/font-awesome.min.css HTTP/1.1" 200 31000 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /assets/cd83ad4b/css/AdminLTE.min.css HTTP/1.1" 200 106548 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /assets/cd83ad4b/css/skins/_all-skins.min.css HTTP/1.1" 200 41635 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /assets/cd83ad4b/img/user2-160x160.jpg HTTP/1.1" 200 7070 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /assets/5aa3f20b/jquery.js HTTP/1.1" 200 288580 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /assets/27128343/yii.js HTTP/1.1" 200 20934 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /assets/39b83f70/js/i18n/ru.js HTTP/1.1" 200 1171 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /assets/39b83f70/js/select2.full.js HTTP/1.1" 200 173566 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /assets/5b57ee2f/js/select2-krajee.js HTTP/1.1" 200 7344 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /assets/bae3a4f/js/kv-widgets.js HTTP/1.1" 200 1061 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /assets/27128343/yii.validation.js HTTP/1.1" 200 16405 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /assets/27128343/yii.activeForm.js HTTP/1.1" 200 36765 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /js/site.js HTTP/1.1" 200 1214 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /assets/71772193/js/bootstrap.js HTTP/1.1" 200 75484 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /assets/cd83ad4b/js/adminlte.min.js HTTP/1.1" 200 13611 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /debug/default/toolbar?tag=6231a1a376864 HTTP/1.1" 200 3426 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /assets/bae3a4f/img/loading-plugin.gif HTTP/1.1" 200 847 "http://backend.guild.loc/assets/bae3a4f/css/kv-widgets.css" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:52 +0300] "GET /assets/ccdf1f3a/fonts/fontawesome-webfont.woff2?v=4.7.0 HTTP/1.1" 200 77160 "http://backend.guild.loc/assets/ccdf1f3a/css/font-awesome.min.css" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" +127.0.0.1 - - [16/Mar/2022:11:36:53 +0300] "GET /favicon.ico HTTP/1.1" 200 318 "http://backend.guild.loc/questionnaire/user-response/update?id=218&user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5" "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0" From eb7e6ce59f00dfe52a2dcb0eba47817e5754e3cd Mon Sep 17 00:00:00 2001 From: iironside Date: Wed, 16 Mar 2022 14:27:01 +0300 Subject: [PATCH 6/6] some refactors --- .../views/user-response/_form.php | 19 +++++++++++++------ docs/api/main.md | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/backend/modules/questionnaire/views/user-response/_form.php b/backend/modules/questionnaire/views/user-response/_form.php index cba561b..69a82a2 100644 --- a/backend/modules/questionnaire/views/user-response/_form.php +++ b/backend/modules/questionnaire/views/user-response/_form.php @@ -38,12 +38,19 @@ use yii\widgets\ActiveForm; field($model, 'response_body')->textInput(['maxlength' => true]) ?> - field($model, 'answer_flag')->dropDownList( - AnswerHelper::answerFlagsList(), - [ - 'prompt' => 'Выберите' - ] - ) ?> + field($model, 'answer_flag')->dropDownList([ + '0.0' => 'Ошибочный', + '0.1' => '10%', + '0.2' => '20%', + '0.3' => '30%', + '0.4' => '40%', + '0.5' => '50%', + '0.6' => '60%', + '0.7' => '70%', + '0.8' => '80%', + '0.9' => '90%', + '1' => '100%', + ]); ?> field($model, 'user_questionnaire_uuid')->textInput(['maxlength' => true]) ?> diff --git a/docs/api/main.md b/docs/api/main.md index 9619bcb..c0f728c 100755 --- a/docs/api/main.md +++ b/docs/api/main.md @@ -1083,7 +1083,7 @@ answer_flag - Флаг ответа(1 - верно, 0 - ложно) + Флаг ответа(1 - верно, 0 - ложно). Если отправлен ответ на открытый вопрос, флаг ответа не будет возвращаться до момента проверки в админ панели.