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;
|
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 verbs()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'questionnaires-list' => ['get'],
|
2022-03-14 17:47:01 +03:00
|
|
|
'questionnaire-completed' => ['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);
|
2022-02-15 11:29:16 +03:00
|
|
|
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(json_encode($userQuestionnaireModel->errors));
|
2022-03-14 17:47:01 +03:00
|
|
|
}
|
|
|
|
return $userQuestionnaireModel;
|
|
|
|
}
|
2021-10-28 10:51:14 +03:00
|
|
|
}
|