update api for test
This commit is contained in:
@ -1,85 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace frontend\modules\api\controllers;
|
||||
|
||||
use common\models\Answer;
|
||||
use Yii;
|
||||
use yii\filters\auth\HttpBearerAuth;
|
||||
use yii\web\NotFoundHttpException;
|
||||
use yii\rest\Controller;
|
||||
|
||||
class AnswerController extends ApiController
|
||||
{
|
||||
// public function behaviors(): array
|
||||
// {
|
||||
// $behaviors = parent::behaviors();
|
||||
//
|
||||
// $behaviors['authenticator']['authMethods'] = [
|
||||
// HttpBearerAuth::className(),
|
||||
// ];
|
||||
//
|
||||
// return $behaviors;
|
||||
// }
|
||||
|
||||
public function verbs(): array
|
||||
{
|
||||
return [
|
||||
'get-answers' => ['get'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Get(path="/answer/get-answers",
|
||||
* summary="Список ответов на вопрос",
|
||||
* description="Получение списка ответов",
|
||||
* security={
|
||||
* {"bearerAuth": {}}
|
||||
* },
|
||||
* tags={"Tests"},
|
||||
* @OA\Parameter(
|
||||
* name="question_id",
|
||||
* in="query",
|
||||
* required=true,
|
||||
* description="id вопроса",
|
||||
* @OA\Schema(
|
||||
* type="integer",
|
||||
* )
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=200,
|
||||
* description="Возвращает масив вопросов",
|
||||
* @OA\MediaType(
|
||||
* mediaType="application/json",
|
||||
* @OA\Schema(ref="#/components/schemas/AnswerExampleArr"),
|
||||
* ),
|
||||
*
|
||||
* ),
|
||||
* )
|
||||
*
|
||||
* @throws NotFoundHttpException
|
||||
*/
|
||||
public function actionGetAnswers(): array
|
||||
{
|
||||
$question_id = Yii::$app->request->get('question_id');
|
||||
if(empty($question_id) or !is_numeric($question_id))
|
||||
{
|
||||
throw new NotFoundHttpException('Incorrect question ID');
|
||||
}
|
||||
|
||||
$answers = Answer::activeAnswers($question_id);
|
||||
if(empty($answers)) {
|
||||
throw new NotFoundHttpException('Answers not found or question inactive');
|
||||
}
|
||||
|
||||
array_walk( $answers, function(&$arr){
|
||||
unset(
|
||||
$arr['created_at'],
|
||||
$arr['updated_at'],
|
||||
$arr['answer_flag'],
|
||||
$arr['status']
|
||||
);
|
||||
});
|
||||
|
||||
return $answers;
|
||||
}
|
||||
}
|
@ -2,36 +2,35 @@
|
||||
|
||||
namespace frontend\modules\api\controllers;
|
||||
|
||||
use common\helpers\UUIDHelper;
|
||||
use common\models\Question;
|
||||
use common\models\UserQuestionnaire;
|
||||
use Exception;
|
||||
use frontend\modules\api\models\questionnaire\forms\QuestionnaireUuidForm;
|
||||
use frontend\modules\api\models\questionnaire\Question;
|
||||
use frontend\modules\api\models\questionnaire\UserQuestionnaire;
|
||||
use frontend\modules\api\services\UserQuestionnaireService;
|
||||
use Yii;
|
||||
use yii\filters\auth\HttpBearerAuth;
|
||||
use yii\rest\Controller;
|
||||
use yii\web\NotFoundHttpException;
|
||||
use yii\web\BadRequestHttpException;
|
||||
|
||||
class QuestionController extends ApiController
|
||||
{
|
||||
public function behaviors()
|
||||
{
|
||||
$behaviors = parent::behaviors();
|
||||
$behaviors['authenticator']['authMethods'] = [
|
||||
HttpBearerAuth::className(),
|
||||
];
|
||||
return $behaviors;
|
||||
}
|
||||
private UserQuestionnaireService $userQuestionnaireService;
|
||||
|
||||
public function verbs()
|
||||
public function __construct(
|
||||
$id,
|
||||
$module,
|
||||
UserQuestionnaireService $userQuestionnaireService,
|
||||
$config = []
|
||||
)
|
||||
{
|
||||
return [
|
||||
'get-questions' => ['get'],
|
||||
];
|
||||
$this->userQuestionnaireService = $userQuestionnaireService;
|
||||
parent::__construct($id, $module, $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Get(path="/question/get-questions",
|
||||
* summary="Список вопросов",
|
||||
* description="Получение списка вопросов",
|
||||
* description="Получение списка вопросов и возможные варианты ответа. Сохраняет временную метку начала тестирования,
|
||||
от которой будет отсчитываться временной интервал на выполнение теста. При наличии лимита времени на выполнение теста.
|
||||
При превышении лимита времени на выполнение будет возвращена ошибка: Time's up!",
|
||||
* security={
|
||||
* {"bearerAuth": {}}
|
||||
* },
|
||||
@ -53,40 +52,28 @@ class QuestionController extends ApiController
|
||||
* @OA\Schema(ref="#/components/schemas/QuestionExampleArr"),
|
||||
* ),
|
||||
*
|
||||
*
|
||||
* ),
|
||||
* )
|
||||
*
|
||||
* @throws NotFoundHttpException
|
||||
* @throws \Exception
|
||||
* @throws BadRequestHttpException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionGetQuestions(): array
|
||||
{
|
||||
$uuid = Yii::$app->request->get('uuid');
|
||||
$form = new QuestionnaireUuidForm();
|
||||
|
||||
if(empty($uuid) or !UUIDHelper::is_valid($uuid))
|
||||
{
|
||||
throw new NotFoundHttpException('Incorrect questionnaire UUID');
|
||||
if ($form->load(Yii::$app->request->get()) && !$form->validate()) {
|
||||
$errors = $form->errors;
|
||||
throw new BadRequestHttpException(array_shift($errors)[0]);
|
||||
}
|
||||
|
||||
$questionnaire_id = UserQuestionnaire::getQuestionnaireId($uuid);
|
||||
$userQuestionnaire = UserQuestionnaire::findOne(['uuid' => $form->uuid]);
|
||||
|
||||
$questions = Question::activeQuestions($questionnaire_id);
|
||||
if(empty($questions)) {
|
||||
throw new NotFoundHttpException('Questions not found');
|
||||
if (!$this->userQuestionnaireService->checkTimeLimit($userQuestionnaire)) {
|
||||
UserQuestionnaireService::calculateScore($userQuestionnaire->uuid);
|
||||
throw new BadRequestHttpException("Time's up!");
|
||||
}
|
||||
|
||||
array_walk( $questions, function(&$arr){
|
||||
unset(
|
||||
$arr['score'],
|
||||
$arr['created_at'],
|
||||
$arr['updated_at'],
|
||||
$arr['status'],
|
||||
$arr['questionnaire_id']
|
||||
);
|
||||
});
|
||||
|
||||
return $questions;
|
||||
return Question::activeQuestions($userQuestionnaire->questionnaire_id);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,8 +2,9 @@
|
||||
|
||||
namespace frontend\modules\api\controllers;
|
||||
|
||||
use frontend\modules\api\models\UserQuestionnaire;
|
||||
use frontend\modules\api\models\questionnaire\UserQuestionnaire;
|
||||
use frontend\modules\api\services\UserQuestionnaireService;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use yii\web\NotFoundHttpException;
|
||||
use yii\web\ServerErrorHttpException;
|
||||
@ -14,14 +15,11 @@ class UserQuestionnaireController extends ApiController
|
||||
public function behaviors(): array
|
||||
{
|
||||
return ArrayHelper::merge(parent::behaviors(), [
|
||||
|
||||
'verbs' => [
|
||||
'class' => \yii\filters\VerbFilter::class,
|
||||
'actions' => [
|
||||
'questionnaires-list' => ['get'],
|
||||
'questionnaire-completed' => ['get'],
|
||||
'get-points-number' => ['get'],
|
||||
'get-question-number' => ['get'],
|
||||
],
|
||||
]
|
||||
]);
|
||||
@ -73,7 +71,7 @@ class UserQuestionnaireController extends ApiController
|
||||
/**
|
||||
* @OA\Get(path="/user-questionnaire/questionnaire-completed",
|
||||
* summary="Проверка теста",
|
||||
* description="Выполнения проверки теста",
|
||||
* description="Выполнение проверки теста",
|
||||
* security={
|
||||
* {"bearerAuth": {}}
|
||||
* },
|
||||
@ -98,7 +96,7 @@ class UserQuestionnaireController extends ApiController
|
||||
* )
|
||||
*
|
||||
* @throws NotFoundHttpException
|
||||
* @throws ServerErrorHttpException
|
||||
* @throws ServerErrorHttpException|InvalidConfigException
|
||||
*/
|
||||
public function actionQuestionnaireCompleted($user_questionnaire_uuid): UserQuestionnaire
|
||||
{
|
||||
@ -108,90 +106,4 @@ class UserQuestionnaireController extends ApiController
|
||||
}
|
||||
return $userQuestionnaireModel;
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Get(path="/user-questionnaire/get-points-number",
|
||||
* summary="Количество балов в тесте",
|
||||
* description="Возвращает максимальное количество балов за тест",
|
||||
* security={
|
||||
* {"bearerAuth": {}}
|
||||
* },
|
||||
* tags={"Tests"},
|
||||
* @OA\Parameter(
|
||||
* name="user_questionnaire_uuid",
|
||||
* in="query",
|
||||
* required=true,
|
||||
* @OA\Schema(
|
||||
* type="string",
|
||||
* )
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=200,
|
||||
* description="Возвращает максимально возможное количество балов за тест",
|
||||
* @OA\MediaType(
|
||||
* mediaType="application/json",
|
||||
* @OA\Schema(
|
||||
* @OA\Property(
|
||||
* property="sum_point",
|
||||
* type="integer",
|
||||
* example="61",
|
||||
* ),
|
||||
* ),
|
||||
* ),
|
||||
*
|
||||
* ),
|
||||
* )
|
||||
* @throws ServerErrorHttpException
|
||||
*/
|
||||
public function actionGetPointsNumber($user_questionnaire_uuid): array
|
||||
{
|
||||
$questionPointsNumber = UserQuestionnaireService::getPointsNumber($user_questionnaire_uuid);
|
||||
if (empty($questionPointsNumber)) {
|
||||
throw new ServerErrorHttpException('Question points not found!');
|
||||
}
|
||||
return $questionPointsNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Get(path="/user-questionnaire/get-question-number",
|
||||
* summary="Число вопросов в тесте",
|
||||
* description="Возвращает число вопросов в тесте",
|
||||
* security={
|
||||
* {"bearerAuth": {}}
|
||||
* },
|
||||
* tags={"Tests"},
|
||||
* @OA\Parameter(
|
||||
* name="user_questionnaire_uuid",
|
||||
* in="query",
|
||||
* required=true,
|
||||
* @OA\Schema(
|
||||
* type="string",
|
||||
* )
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=200,
|
||||
* description="Возвращает число вопросов в тесте",
|
||||
* @OA\MediaType(
|
||||
* mediaType="application/json",
|
||||
* @OA\Schema(
|
||||
* @OA\Property(
|
||||
* property="question_number",
|
||||
* type="integer",
|
||||
* example="61",
|
||||
* ),
|
||||
* ),
|
||||
* ),
|
||||
*
|
||||
* ),
|
||||
* )
|
||||
* @throws ServerErrorHttpException
|
||||
*/
|
||||
public function actionGetQuestionNumber($user_questionnaire_uuid): array
|
||||
{
|
||||
$questionNumber = UserQuestionnaireService::getQuestionNumber($user_questionnaire_uuid);
|
||||
if (empty($questionNumber)) {
|
||||
throw new ServerErrorHttpException('Question number not found!');
|
||||
}
|
||||
return $questionNumber;
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
namespace frontend\modules\api\controllers;
|
||||
|
||||
use frontend\modules\api\models\questionnaire\UserQuestionnaire;
|
||||
use frontend\modules\api\services\UserQuestionnaireService;
|
||||
use frontend\modules\api\services\UserResponseService;
|
||||
use Yii;
|
||||
use yii\base\InvalidConfigException;
|
||||
@ -10,18 +12,24 @@ use yii\web\ServerErrorHttpException;
|
||||
|
||||
class UserResponseController extends ApiController
|
||||
{
|
||||
public function verbs(): array
|
||||
private UserQuestionnaireService $userQuestionnaireService;
|
||||
|
||||
public function __construct(
|
||||
$id,
|
||||
$module,
|
||||
UserQuestionnaireService $userQuestionnaireService,
|
||||
$config = []
|
||||
)
|
||||
{
|
||||
return [
|
||||
'set-response' => ['post'],
|
||||
'set-responses' => ['post'],
|
||||
];
|
||||
$this->userQuestionnaireService = $userQuestionnaireService;
|
||||
parent::__construct($id, $module, $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Post(path="/user-response/set-responses",
|
||||
* summary="Добавить массив ответов пользователя",
|
||||
* description="Добавление массива ответов на вопросы от пользователя",
|
||||
* description="Добавление массива ответов на вопросы от пользователя. При наличии лимита времени на выполнение теста,
|
||||
будет проведена проверка. При превышении лимита времени на выполнение будет возвращена ошибка: Time's up!",
|
||||
* security={
|
||||
* {"bearerAuth": {}}
|
||||
* },
|
||||
@ -58,7 +66,7 @@ class UserResponseController extends ApiController
|
||||
*
|
||||
* @OA\Response(
|
||||
* response=200,
|
||||
* description="Возвращает объект Запроса",
|
||||
* description="Возвращает масив ответов",
|
||||
* @OA\MediaType(
|
||||
* mediaType="application/json",
|
||||
* @OA\Schema(ref="#/components/schemas/UserResponseExampleArr"),
|
||||
@ -66,12 +74,25 @@ class UserResponseController extends ApiController
|
||||
* ),
|
||||
* )
|
||||
*
|
||||
* @return array
|
||||
* @throws BadRequestHttpException
|
||||
* @throws InvalidConfigException
|
||||
* @throws ServerErrorHttpException|BadRequestHttpException
|
||||
* @throws ServerErrorHttpException
|
||||
* @throws \yii\web\NotFoundHttpException
|
||||
*/
|
||||
public function actionSetResponses(): array
|
||||
{
|
||||
$userResponseModels = UserResponseService::createUserResponses(Yii::$app->getRequest()->getBodyParams());
|
||||
$uuid = Yii::$app->request->post('user_questionnaire_uuid');
|
||||
$userResponses = Yii::$app->request->post('userResponses');
|
||||
|
||||
$userQuestionnaire = UserQuestionnaire::findOne(['uuid' => $uuid]);
|
||||
|
||||
if (!$this->userQuestionnaireService->checkTimeLimit($userQuestionnaire)) {
|
||||
UserQuestionnaireService::calculateScore($userQuestionnaire->uuid);
|
||||
throw new BadRequestHttpException("Time's up!");
|
||||
}
|
||||
|
||||
$userResponseModels = UserResponseService::createUserResponses($userResponses, $uuid);
|
||||
foreach ($userResponseModels as $model) {
|
||||
if ($model->errors) {
|
||||
throw new ServerErrorHttpException(json_encode($model->errors));
|
||||
|
Reference in New Issue
Block a user