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` + +
+ Возвращает массив объектов Вопросов.
+ Каждый объект Вопрос имеет такой вид:
+
@@ -908,6 +965,14 @@
- Пример запроса: + Пример тела запроса:
-`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 пользователя + | +
+ 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()) {