diff --git a/common/helpers/ScoreCalculatorHelper.php b/common/helpers/ScoreCalculatorHelper.php index ad62260..6d6aa9a 100644 --- a/common/helpers/ScoreCalculatorHelper.php +++ b/common/helpers/ScoreCalculatorHelper.php @@ -3,8 +3,9 @@ namespace common\helpers; use backend\modules\questionnaire\models\Answer; -use backend\modules\questionnaire\models\UserQuestionnaire; -use backend\modules\questionnaire\models\UserResponse; +//use backend\modules\questionnaire\models\UserQuestionnaire; +use common\models\UserQuestionnaire; +use common\models\UserResponse; use yii\helpers\ArrayHelper; class ScoreCalculatorHelper @@ -71,11 +72,11 @@ class ScoreCalculatorHelper } } - if($score !== null) { +// if($score !== null) { self::setPercentCorrectAnswers($user_correct_answers_num, $userQuestionnaire); $userQuestionnaire->score = round($score); $userQuestionnaire->save(); - } +// } } protected static function isCorrect($answer_flag): bool @@ -93,12 +94,16 @@ class ScoreCalculatorHelper protected static function setPercentCorrectAnswers($user_correct_answers_num, UserQuestionnaire $userQuestionnaire) { - $all_correct_answers_num = $userQuestionnaire->numCorrectAnswersWithoutOpenQuestions(); - $all_correct_answers_num += $userQuestionnaire->numOpenQuestionsAnswers(); + if($user_correct_answers_num !== null) { + $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); + $percent = $user_correct_answers_num / $all_correct_answers_num; + $userQuestionnaire->percent_correct_answers = round($percent, 2); + } + else { + $userQuestionnaire->percent_correct_answers = round($user_correct_answers_num, 2); + } $userQuestionnaire->save(); } } \ No newline at end of file diff --git a/docs/api/main.md b/docs/api/main.md index 68314ad..9619bcb 100755 --- a/docs/api/main.md +++ b/docs/api/main.md @@ -666,6 +666,63 @@ "type": "yii\\web\\NotFoundHttpException" } ``` + +### Проверить ответы в анкете +`https://guild.craft-group.xyz/api/user-questionnaire/questionnaire-completed` +

+ Для выполнения проверки анкеты необходимо отправить GET запрос на URL https://guild.craft-group.xyz/api/user-questionnaire/questionnaire-completed +

+ +

+ Требуемые параметры запроса: +

+ + + + + + + + + +
+ Параметры + + Значение +
+ user_questionnaire_uuid + + UUID анкеты назначеной пользователю +
+

+ Пример запроса: +

+ +`https://guild.craft-group.xyz/api/user-questionnaire/questionnaire-completed?user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5` + +

+ Возвращает массив объектов Вопросов.
+ Каждый объект Вопрос имеет такой вид: +

+ +```json5 +{ + "id": 1, + "questionnaire_id": 1, + "user_id": 1, + "uuid": "d222f858-60fd-47fb-8731-dc9d5fc384c5", + "created_at": "2021-10-20 13:06:12", + "updated_at": { + "expression": "NOW()", + "params": [] + }, + "score": 4, + "status": 1, + "percent_correct_answers": 0.5, + "testing_date": null +} +``` + ### Вопросы анкеты `https://guild.craft-group.xyz/api/question/get-questions`

@@ -908,6 +965,14 @@ + + user_id + + + ID пользователя + + + question_id @@ -925,7 +990,7 @@ - uuid + user_questionnaire_uuid UUID анкеты назначенной пользователю(string 36) @@ -933,10 +998,18 @@

- Пример запроса: + Пример тела запроса:

-`https://guild.craft-group.xyz/api/user-response/set-responses?user_id=1&user_questionnaire_id=1&question_id=7&response_body=user response string` +```json5 +{ + "user_id": "1", + "question_id": "7", + "response_body": "oooooooooooo111111111", + "user_questionnaire_uuid": "d222f858-60fd-47fb-8731-dc9d5fc384c5" +} +``` +`https://guild.craft-group.xyz/api/user-response/set-response`

Возвращает объект Ответа.
@@ -945,21 +1018,76 @@ ```json5 { - "user_id": "1", - "question_id": "7", - "response_body": "user response string", - "user_questionnaire_uuid": "d222f858-60fd-47fb-8731-dc9d5fc384c5", - "created_at": { - "expression": "NOW()", - "params": [] - }, - "updated_at": { - "expression": "NOW()", - "params": [] - }, - "id": 90 + "user_id": "1", + "question_id": "7", + "response_body": "oooooooooooo111111111", + "user_questionnaire_uuid": "d222f858-60fd-47fb-8731-dc9d5fc384c5", + "created_at": { + "expression": "NOW()", + "params": [] + }, + "updated_at": { + "expression": "NOW()", + "params": [] + }, + "id": 191, + "answer_flag": 0 } ``` +

+ Ответ содержит: +

+ + + + + + + + + + + + + + + + + + + + + + + + + +
+ Параметры + + Значение +
+ user_id + + ID пользователя +
+ question_id + + ID вопроса(int) +
+ response_body + + Ответ пользователя(string 255) +
+ user_questionnaire_uuid + + UUID анкеты назначенной пользователю(string 36) +
+ answer_flag + + Флаг ответа(1 - верно, 0 - ложно) +
+

В случаии ошибки в запросе будет отправлено сообщение следующего вида:

@@ -991,6 +1119,14 @@ Значение + + + + user_id + + + ID пользователя + @@ -1010,7 +1146,7 @@ - uuid + user_questionnaire_uuid UUID анкеты назначенной пользователю(string 36) @@ -1060,12 +1196,13 @@ "expression": "NOW()", "params": [] }, - "id": 137 + "id": 192, + "answer_flag": 0 }, { "user_id": "1", - "question_id": "4", - "response_body": "oooooooooooo2222222", + "question_id": "7", + "response_body": "oooooooooooo111111111", "user_questionnaire_uuid": "d222f858-60fd-47fb-8731-dc9d5fc384c5", "created_at": { "expression": "NOW()", @@ -1075,10 +1212,64 @@ "expression": "NOW()", "params": [] }, - "id": 138 + "id": 193, + "answer_flag": 0 } ] ``` +

+ Ответ содержит: +

+ + + + + + + + + + + + + + + + + + + + + + + + + +
+ Параметры + + Значение +
+ user_id + + ID пользователя +
+ question_id + + ID вопроса(int) +
+ response_body + + Ответ пользователя(string 255) +
+ user_questionnaire_uuid + + UUID анкеты назначенной пользователю(string 36) +
+ answer_flag + + Флаг ответа(1 - верно, 0 - ложно) +

В случаии ошибки в запросе будет отправлено сообщение следующего вида:

diff --git a/frontend/modules/api/controllers/UserQuestionnaireController.php b/frontend/modules/api/controllers/UserQuestionnaireController.php index 7d665c5..1d77ccf 100644 --- a/frontend/modules/api/controllers/UserQuestionnaireController.php +++ b/frontend/modules/api/controllers/UserQuestionnaireController.php @@ -2,6 +2,7 @@ namespace frontend\modules\api\controllers; +use common\helpers\ScoreCalculatorHelper; use common\models\UserQuestionnaire; use Yii; use yii\filters\auth\HttpBearerAuth; @@ -25,6 +26,7 @@ class UserQuestionnaireController extends ApiController { return [ 'questionnaires-list' => ['get'], + 'questionnaire-completed' => ['get'], ]; } @@ -56,4 +58,26 @@ class UserQuestionnaireController extends ApiController return $userQuestionnaireModels; } + + public function actionQuestionnaireCompleted() + { +// 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 = UserQuestionnaire::findOne(['uuid' => $user_questionnaire_uuid]); + + if(empty($userQuestionnaireModel)) { + throw new NotFoundHttpException('Active questionnaire not found'); + } + + ScoreCalculatorHelper::rateResponses($userQuestionnaireModel); + ScoreCalculatorHelper::calculateScore($userQuestionnaireModel); + + return $userQuestionnaireModel; + } } diff --git a/frontend/modules/api/controllers/UserResponseController.php b/frontend/modules/api/controllers/UserResponseController.php index 59c059b..3662cc1 100644 --- a/frontend/modules/api/controllers/UserResponseController.php +++ b/frontend/modules/api/controllers/UserResponseController.php @@ -2,6 +2,7 @@ namespace frontend\modules\api\controllers; +use common\helpers\ScoreCalculatorHelper; use common\models\UserResponse; use Exception; use Yii; @@ -15,16 +16,16 @@ 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 behaviors(): array +// { +// $behaviors = parent::behaviors(); +// +// $behaviors['authenticator']['authMethods'] = [ +// HttpBearerAuth::className(), +// ]; +// +// return $behaviors; +// } public function verbs(): array { @@ -34,12 +35,12 @@ class UserResponseController extends ApiController ]; } - public function actions() - { - $actions = parent::actions(); - unset($actions['create']); - return $actions; - } +// public function actions() +// { +// $actions = parent::actions(); +// unset($actions['create']); +// return $actions; +// } /** * @throws InvalidConfigException @@ -57,7 +58,6 @@ class UserResponseController extends ApiController $this->saveModel($model); return $model; - } /** @@ -106,6 +106,7 @@ class UserResponseController extends ApiController protected function saveModel($model) { if ($model->save()) { + ScoreCalculatorHelper::rateOneResponse($model); $response = Yii::$app->getResponse(); $response->setStatusCode(201); } elseif (!$model->hasErrors()) {