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;
|
namespace common\helpers;
|
||||||
|
|
||||||
use backend\modules\questionnaire\models\Answer;
|
use backend\modules\questionnaire\models\Answer;
|
||||||
use backend\modules\questionnaire\models\UserQuestionnaire;
|
//use backend\modules\questionnaire\models\UserQuestionnaire;
|
||||||
use backend\modules\questionnaire\models\UserResponse;
|
use common\models\UserQuestionnaire;
|
||||||
|
use common\models\UserResponse;
|
||||||
use yii\helpers\ArrayHelper;
|
use yii\helpers\ArrayHelper;
|
||||||
|
|
||||||
class ScoreCalculatorHelper
|
class ScoreCalculatorHelper
|
||||||
@ -71,11 +72,11 @@ class ScoreCalculatorHelper
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if($score !== null) {
|
// if($score !== null) {
|
||||||
self::setPercentCorrectAnswers($user_correct_answers_num, $userQuestionnaire);
|
self::setPercentCorrectAnswers($user_correct_answers_num, $userQuestionnaire);
|
||||||
$userQuestionnaire->score = round($score);
|
$userQuestionnaire->score = round($score);
|
||||||
$userQuestionnaire->save();
|
$userQuestionnaire->save();
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function isCorrect($answer_flag): bool
|
protected static function isCorrect($answer_flag): bool
|
||||||
@ -93,12 +94,16 @@ class ScoreCalculatorHelper
|
|||||||
|
|
||||||
protected static function setPercentCorrectAnswers($user_correct_answers_num, UserQuestionnaire $userQuestionnaire)
|
protected static function setPercentCorrectAnswers($user_correct_answers_num, UserQuestionnaire $userQuestionnaire)
|
||||||
{
|
{
|
||||||
$all_correct_answers_num = $userQuestionnaire->numCorrectAnswersWithoutOpenQuestions();
|
if($user_correct_answers_num !== null) {
|
||||||
$all_correct_answers_num += $userQuestionnaire->numOpenQuestionsAnswers();
|
$all_correct_answers_num = $userQuestionnaire->numCorrectAnswersWithoutOpenQuestions();
|
||||||
|
$all_correct_answers_num += $userQuestionnaire->numOpenQuestionsAnswers();
|
||||||
|
|
||||||
$percent = $user_correct_answers_num / $all_correct_answers_num;
|
$percent = $user_correct_answers_num / $all_correct_answers_num;
|
||||||
|
$userQuestionnaire->percent_correct_answers = round($percent, 2);
|
||||||
$userQuestionnaire->percent_correct_answers = round($percent, 2);
|
}
|
||||||
|
else {
|
||||||
|
$userQuestionnaire->percent_correct_answers = round($user_correct_answers_num, 2);
|
||||||
|
}
|
||||||
$userQuestionnaire->save();
|
$userQuestionnaire->save();
|
||||||
}
|
}
|
||||||
}
|
}
|
233
docs/api/main.md
233
docs/api/main.md
@ -666,6 +666,63 @@
|
|||||||
"type": "yii\\web\\NotFoundHttpException"
|
"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`
|
`https://guild.craft-group.xyz/api/question/get-questions`
|
||||||
<p>
|
<p>
|
||||||
@ -908,6 +965,14 @@
|
|||||||
</th>
|
</th>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
<td>
|
||||||
|
user_id
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
ID пользователя
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
question_id
|
question_id
|
||||||
</td>
|
</td>
|
||||||
@ -925,7 +990,7 @@
|
|||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
uuid
|
user_questionnaire_uuid
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
UUID анкеты назначенной пользователю(string 36)
|
UUID анкеты назначенной пользователю(string 36)
|
||||||
@ -933,10 +998,18 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
<p>
|
<p>
|
||||||
Пример запроса:
|
Пример тела запроса:
|
||||||
</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>
|
<p>
|
||||||
Возвращает объект <b>Ответа</b>. <br>
|
Возвращает объект <b>Ответа</b>. <br>
|
||||||
@ -945,21 +1018,76 @@
|
|||||||
|
|
||||||
```json5
|
```json5
|
||||||
{
|
{
|
||||||
"user_id": "1",
|
"user_id": "1",
|
||||||
"question_id": "7",
|
"question_id": "7",
|
||||||
"response_body": "user response string",
|
"response_body": "oooooooooooo111111111",
|
||||||
"user_questionnaire_uuid": "d222f858-60fd-47fb-8731-dc9d5fc384c5",
|
"user_questionnaire_uuid": "d222f858-60fd-47fb-8731-dc9d5fc384c5",
|
||||||
"created_at": {
|
"created_at": {
|
||||||
"expression": "NOW()",
|
"expression": "NOW()",
|
||||||
"params": []
|
"params": []
|
||||||
},
|
},
|
||||||
"updated_at": {
|
"updated_at": {
|
||||||
"expression": "NOW()",
|
"expression": "NOW()",
|
||||||
"params": []
|
"params": []
|
||||||
},
|
},
|
||||||
"id": 90
|
"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>
|
||||||
В случаии ошибки в запросе будет отправлено сообщение следующего вида:
|
В случаии ошибки в запросе будет отправлено сообщение следующего вида:
|
||||||
</p>
|
</p>
|
||||||
@ -991,6 +1119,14 @@
|
|||||||
<th>
|
<th>
|
||||||
Значение
|
Значение
|
||||||
</th>
|
</th>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
user_id
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
ID пользователя
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
@ -1010,7 +1146,7 @@
|
|||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
uuid
|
user_questionnaire_uuid
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
UUID анкеты назначенной пользователю(string 36)
|
UUID анкеты назначенной пользователю(string 36)
|
||||||
@ -1060,12 +1196,13 @@
|
|||||||
"expression": "NOW()",
|
"expression": "NOW()",
|
||||||
"params": []
|
"params": []
|
||||||
},
|
},
|
||||||
"id": 137
|
"id": 192,
|
||||||
|
"answer_flag": 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"user_id": "1",
|
"user_id": "1",
|
||||||
"question_id": "4",
|
"question_id": "7",
|
||||||
"response_body": "oooooooooooo2222222",
|
"response_body": "oooooooooooo111111111",
|
||||||
"user_questionnaire_uuid": "d222f858-60fd-47fb-8731-dc9d5fc384c5",
|
"user_questionnaire_uuid": "d222f858-60fd-47fb-8731-dc9d5fc384c5",
|
||||||
"created_at": {
|
"created_at": {
|
||||||
"expression": "NOW()",
|
"expression": "NOW()",
|
||||||
@ -1075,10 +1212,64 @@
|
|||||||
"expression": "NOW()",
|
"expression": "NOW()",
|
||||||
"params": []
|
"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>
|
||||||
В случаии ошибки в запросе будет отправлено сообщение следующего вида:
|
В случаии ошибки в запросе будет отправлено сообщение следующего вида:
|
||||||
</p>
|
</p>
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace frontend\modules\api\controllers;
|
namespace frontend\modules\api\controllers;
|
||||||
|
|
||||||
|
use common\helpers\ScoreCalculatorHelper;
|
||||||
use common\models\UserQuestionnaire;
|
use common\models\UserQuestionnaire;
|
||||||
use Yii;
|
use Yii;
|
||||||
use yii\filters\auth\HttpBearerAuth;
|
use yii\filters\auth\HttpBearerAuth;
|
||||||
@ -25,6 +26,7 @@ class UserQuestionnaireController extends ApiController
|
|||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'questionnaires-list' => ['get'],
|
'questionnaires-list' => ['get'],
|
||||||
|
'questionnaire-completed' => ['get'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,4 +58,26 @@ class UserQuestionnaireController extends ApiController
|
|||||||
|
|
||||||
return $userQuestionnaireModels;
|
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;
|
namespace frontend\modules\api\controllers;
|
||||||
|
|
||||||
|
use common\helpers\ScoreCalculatorHelper;
|
||||||
use common\models\UserResponse;
|
use common\models\UserResponse;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Yii;
|
use Yii;
|
||||||
@ -15,16 +16,16 @@ class UserResponseController extends ApiController
|
|||||||
{
|
{
|
||||||
public $modelClass = 'common\models\UserResponse';
|
public $modelClass = 'common\models\UserResponse';
|
||||||
|
|
||||||
public function behaviors(): array
|
// public function behaviors(): array
|
||||||
{
|
// {
|
||||||
$behaviors = parent::behaviors();
|
// $behaviors = parent::behaviors();
|
||||||
|
//
|
||||||
$behaviors['authenticator']['authMethods'] = [
|
// $behaviors['authenticator']['authMethods'] = [
|
||||||
HttpBearerAuth::className(),
|
// HttpBearerAuth::className(),
|
||||||
];
|
// ];
|
||||||
|
//
|
||||||
return $behaviors;
|
// return $behaviors;
|
||||||
}
|
// }
|
||||||
|
|
||||||
public function verbs(): array
|
public function verbs(): array
|
||||||
{
|
{
|
||||||
@ -34,12 +35,12 @@ class UserResponseController extends ApiController
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function actions()
|
// public function actions()
|
||||||
{
|
// {
|
||||||
$actions = parent::actions();
|
// $actions = parent::actions();
|
||||||
unset($actions['create']);
|
// unset($actions['create']);
|
||||||
return $actions;
|
// return $actions;
|
||||||
}
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws InvalidConfigException
|
* @throws InvalidConfigException
|
||||||
@ -57,7 +58,6 @@ class UserResponseController extends ApiController
|
|||||||
$this->saveModel($model);
|
$this->saveModel($model);
|
||||||
|
|
||||||
return $model;
|
return $model;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -106,6 +106,7 @@ class UserResponseController extends ApiController
|
|||||||
protected function saveModel($model)
|
protected function saveModel($model)
|
||||||
{
|
{
|
||||||
if ($model->save()) {
|
if ($model->save()) {
|
||||||
|
ScoreCalculatorHelper::rateOneResponse($model);
|
||||||
$response = Yii::$app->getResponse();
|
$response = Yii::$app->getResponse();
|
||||||
$response->setStatusCode(201);
|
$response->setStatusCode(201);
|
||||||
} elseif (!$model->hasErrors()) {
|
} elseif (!$model->hasErrors()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user