2021-10-28 10:51:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace frontend\modules\api\controllers;
|
|
|
|
|
2021-11-12 14:30:01 +03:00
|
|
|
use common\helpers\UUIDHelper;
|
2021-10-28 10:51:14 +03:00
|
|
|
use common\models\Question;
|
2021-11-12 14:30:01 +03:00
|
|
|
use common\models\UserQuestionnaire;
|
2021-10-28 10:51:14 +03:00
|
|
|
use Yii;
|
|
|
|
use yii\filters\auth\HttpBearerAuth;
|
2021-11-12 14:30:01 +03:00
|
|
|
use yii\rest\Controller;
|
2021-10-28 10:51:14 +03:00
|
|
|
use yii\web\NotFoundHttpException;
|
|
|
|
|
2022-03-03 17:24:45 +03:00
|
|
|
class QuestionController extends ApiController
|
2021-10-28 10:51:14 +03:00
|
|
|
{
|
|
|
|
public function behaviors()
|
|
|
|
{
|
|
|
|
$behaviors = parent::behaviors();
|
|
|
|
$behaviors['authenticator']['authMethods'] = [
|
|
|
|
HttpBearerAuth::className(),
|
|
|
|
];
|
|
|
|
return $behaviors;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function verbs()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'get-questions' => ['get'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-10 15:06:01 +03:00
|
|
|
* @OA\Get(path="/question/get-questions",
|
|
|
|
* summary="Список вопросов",
|
|
|
|
* description="Получение списка вопросов",
|
|
|
|
* security={
|
|
|
|
* {"bearerAuth": {}}
|
|
|
|
* },
|
|
|
|
* tags={"Tests"},
|
|
|
|
* @OA\Parameter(
|
2023-10-26 09:55:39 +03:00
|
|
|
* name="uuid",
|
2023-10-10 15:06:01 +03:00
|
|
|
* in="query",
|
|
|
|
* required=true,
|
|
|
|
* description="UUID анкеты назначеной пользователю",
|
|
|
|
* @OA\Schema(
|
2023-10-26 09:55:39 +03:00
|
|
|
* type="string",
|
2023-10-10 15:06:01 +03:00
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Возвращает масив вопросов",
|
|
|
|
* @OA\MediaType(
|
|
|
|
* mediaType="application/json",
|
|
|
|
* @OA\Schema(ref="#/components/schemas/QuestionExampleArr"),
|
|
|
|
* ),
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*
|
2021-10-28 10:51:14 +03:00
|
|
|
* @throws NotFoundHttpException
|
2021-11-12 14:30:01 +03:00
|
|
|
* @throws \Exception
|
2021-10-28 10:51:14 +03:00
|
|
|
*/
|
2021-11-12 14:30:01 +03:00
|
|
|
public function actionGetQuestions(): array
|
2021-10-28 10:51:14 +03:00
|
|
|
{
|
2021-11-12 14:30:01 +03:00
|
|
|
$uuid = Yii::$app->request->get('uuid');
|
2021-10-28 10:51:14 +03:00
|
|
|
|
2021-11-12 14:30:01 +03:00
|
|
|
if(empty($uuid) or !UUIDHelper::is_valid($uuid))
|
2021-10-28 10:51:14 +03:00
|
|
|
{
|
2021-11-12 14:30:01 +03:00
|
|
|
throw new NotFoundHttpException('Incorrect questionnaire UUID');
|
2021-10-28 10:51:14 +03:00
|
|
|
}
|
|
|
|
|
2021-11-12 14:30:01 +03:00
|
|
|
$questionnaire_id = UserQuestionnaire::getQuestionnaireId($uuid);
|
|
|
|
|
2021-11-08 12:41:39 +03:00
|
|
|
$questions = Question::activeQuestions($questionnaire_id);
|
2021-10-28 10:51:14 +03:00
|
|
|
if(empty($questions)) {
|
2021-11-12 14:30:01 +03:00
|
|
|
throw new NotFoundHttpException('Questions not found');
|
2021-10-28 10:51:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
array_walk( $questions, function(&$arr){
|
|
|
|
unset(
|
|
|
|
$arr['score'],
|
|
|
|
$arr['created_at'],
|
|
|
|
$arr['updated_at'],
|
|
|
|
$arr['status'],
|
2021-11-12 14:30:01 +03:00
|
|
|
$arr['questionnaire_id']
|
2021-10-28 10:51:14 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return $questions;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|