guild/frontend/modules/api/controllers/UserQuestionnaireController.php

81 lines
2.5 KiB
PHP
Raw Normal View History

2021-10-28 10:51:14 +03:00
<?php
namespace frontend\modules\api\controllers;
2022-03-15 17:56:45 +03:00
use common\services\UserQuestionnaireService;
use yii\helpers\ArrayHelper;
2021-10-28 10:51:14 +03:00
use yii\web\NotFoundHttpException;
2022-03-15 17:56:45 +03:00
use yii\web\ServerErrorHttpException;
2021-10-28 10:51:14 +03:00
2022-03-03 17:24:45 +03:00
class UserQuestionnaireController extends ApiController
2021-10-28 10:51:14 +03:00
{
public function behaviors(): array
2021-10-28 10:51:14 +03:00
{
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'],
],
]
]);
2021-10-28 10:51:14 +03:00
}
/**
* @throws NotFoundHttpException
*/
2022-03-15 17:56:45 +03:00
public function actionQuestionnairesList($user_id): array
2021-10-28 10:51:14 +03:00
{
2022-03-15 17:56:45 +03:00
if (empty($user_id) or !is_numeric($user_id)) {
2021-10-28 10:51:14 +03:00
throw new NotFoundHttpException('Incorrect user ID');
}
2022-03-15 17:56:45 +03:00
$userQuestionnaireModels = UserQuestionnaireService::getQuestionnaireList($user_id);
if(empty($userQuestionnaireModels)) {
2021-10-28 10:51:14 +03:00
throw new NotFoundHttpException('Active questionnaire not found');
}
2022-03-15 17:56:45 +03:00
return $userQuestionnaireModels;
2021-10-28 10:51:14 +03:00
}
2022-03-14 17:47:01 +03:00
2022-03-15 17:56:45 +03:00
/**
* @throws NotFoundHttpException
* @throws ServerErrorHttpException
*/
public function actionQuestionnaireCompleted($user_questionnaire_uuid)
2022-03-14 17:47:01 +03:00
{
2022-03-15 17:56:45 +03:00
$userQuestionnaireModel = UserQuestionnaireService::calculateScore($user_questionnaire_uuid);
if ($userQuestionnaireModel->errors) {
throw new ServerErrorHttpException($userQuestionnaireModel->errors);
2022-03-14 17:47:01 +03:00
}
return $userQuestionnaireModel;
}
/**
* @throws ServerErrorHttpException
*/
public function actionGetPointsNumber($user_questionnaire_uuid)
{
$questionPointsNumber = UserQuestionnaireService::getPointsNumber($user_questionnaire_uuid);
if (empty($questionPointsNumber)) {
throw new ServerErrorHttpException('Question points not found!');
}
return $questionPointsNumber;
}
/**
* @throws ServerErrorHttpException
*/
public function actionGetQuestionNumber($user_questionnaire_uuid)
{
$questionNumber = UserQuestionnaireService::getQuestionNumber($user_questionnaire_uuid);
if (empty($questionNumber)) {
throw new ServerErrorHttpException('Question number not found!');
}
return $questionNumber;
}
2021-10-28 10:51:14 +03:00
}