Merge pull request #91 from apuc/update_user_answers
Update user answers
This commit is contained in:
commit
d3e2076bbb
@ -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();
|
||||
}
|
||||
}
|
233
docs/api/main.md
233
docs/api/main.md
@ -666,6 +666,63 @@
|
||||
"type": "yii\\web\\NotFoundHttpException"
|
||||
}
|
||||
```
|
||||
|
||||
### Проверить ответы в анкете
|
||||
`https://guild.craft-group.xyz/api/user-questionnaire/questionnaire-completed`
|
||||
<p>
|
||||
Для выполнения проверки анкеты необходимо отправить <b>GET</b> запрос на URL https://guild.craft-group.xyz/api/user-questionnaire/questionnaire-completed
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Требуемые параметры запроса:
|
||||
</p>
|
||||
<table>
|
||||
<tr>
|
||||
<th>
|
||||
Параметры
|
||||
</th>
|
||||
<th>
|
||||
Значение
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
user_questionnaire_uuid
|
||||
</td>
|
||||
<td>
|
||||
UUID анкеты назначеной пользователю
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<p>
|
||||
Пример запроса:
|
||||
</p>
|
||||
|
||||
`https://guild.craft-group.xyz/api/user-questionnaire/questionnaire-completed?user_questionnaire_uuid=d222f858-60fd-47fb-8731-dc9d5fc384c5`
|
||||
|
||||
<p>
|
||||
Возвращает <b>массив</b> объектов <b>Вопросов</b>. <br>
|
||||
Каждый объект <b>Вопрос</b> имеет такой вид:
|
||||
</p>
|
||||
|
||||
```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`
|
||||
<p>
|
||||
@ -908,6 +965,14 @@
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
user_id
|
||||
</td>
|
||||
<td>
|
||||
ID пользователя
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
question_id
|
||||
</td>
|
||||
@ -925,7 +990,7 @@
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
uuid
|
||||
user_questionnaire_uuid
|
||||
</td>
|
||||
<td>
|
||||
UUID анкеты назначенной пользователю(string 36)
|
||||
@ -933,10 +998,18 @@
|
||||
</tr>
|
||||
</table>
|
||||
<p>
|
||||
Пример запроса:
|
||||
Пример тела запроса:
|
||||
</p>
|
||||
|
||||
`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`
|
||||
|
||||
<p>
|
||||
Возвращает объект <b>Ответа</b>. <br>
|
||||
@ -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
|
||||
}
|
||||
```
|
||||
<p>
|
||||
Ответ содержит:
|
||||
</p>
|
||||
<table>
|
||||
<tr>
|
||||
<th>
|
||||
Параметры
|
||||
</th>
|
||||
<th>
|
||||
Значение
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
user_id
|
||||
</td>
|
||||
<td>
|
||||
ID пользователя
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
question_id
|
||||
</td>
|
||||
<td>
|
||||
ID вопроса(int)
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
response_body
|
||||
</td>
|
||||
<td>
|
||||
Ответ пользователя(string 255)
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
user_questionnaire_uuid
|
||||
</td>
|
||||
<td>
|
||||
UUID анкеты назначенной пользователю(string 36)
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
answer_flag
|
||||
</td>
|
||||
<td>
|
||||
Флаг ответа(1 - верно, 0 - ложно)
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<p>
|
||||
В случаии ошибки в запросе будет отправлено сообщение следующего вида:
|
||||
</p>
|
||||
@ -991,6 +1119,14 @@
|
||||
<th>
|
||||
Значение
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
user_id
|
||||
</td>
|
||||
<td>
|
||||
ID пользователя
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
@ -1010,7 +1146,7 @@
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
uuid
|
||||
user_questionnaire_uuid
|
||||
</td>
|
||||
<td>
|
||||
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
|
||||
}
|
||||
]
|
||||
```
|
||||
<p>
|
||||
Ответ содержит:
|
||||
</p>
|
||||
<table>
|
||||
<tr>
|
||||
<th>
|
||||
Параметры
|
||||
</th>
|
||||
<th>
|
||||
Значение
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
user_id
|
||||
</td>
|
||||
<td>
|
||||
ID пользователя
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
question_id
|
||||
</td>
|
||||
<td>
|
||||
ID вопроса(int)
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
response_body
|
||||
</td>
|
||||
<td>
|
||||
Ответ пользователя(string 255)
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
user_questionnaire_uuid
|
||||
</td>
|
||||
<td>
|
||||
UUID анкеты назначенной пользователю(string 36)
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
answer_flag
|
||||
</td>
|
||||
<td>
|
||||
Флаг ответа(1 - верно, 0 - ложно)
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<p>
|
||||
В случаии ошибки в запросе будет отправлено сообщение следующего вида:
|
||||
</p>
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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()) {
|
||||
|
Loading…
Reference in New Issue
Block a user