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

72 lines
2.0 KiB
PHP
Raw Normal View History

2021-10-28 10:51:14 +03:00
<?php
namespace frontend\modules\api\controllers;
2022-03-15 16:54:34 +03:00
use common\services\ScoreCalculatorService;
2021-10-28 10:51:14 +03:00
use common\models\UserQuestionnaire;
use Yii;
use yii\filters\auth\HttpBearerAuth;
use yii\web\NotFoundHttpException;
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
*/
public function actionQuestionnairesList()//: array
2021-10-28 10:51:14 +03:00
{
$user_id = Yii::$app->request->get('user_id');
if(empty($user_id) or !is_numeric($user_id))
{
throw new NotFoundHttpException('Incorrect user ID');
}
$userQuestionnaireModels = UserQuestionnaire::findActiveUserQuestionnaires($user_id);
if(empty($userQuestionnaireModels)) {
2021-10-28 10:51:14 +03:00
throw new NotFoundHttpException('Active questionnaire not found');
}
array_walk( $userQuestionnaireModels, function(&$arr){
2021-10-28 10:51:14 +03:00
unset(
$arr['questionnaire_id'],
// $arr['created_at'],
// $arr['updated_at'],
$arr['id'],
2021-10-28 10:51:14 +03:00
);
});
return $userQuestionnaireModels;
2021-10-28 10:51:14 +03:00
}
2022-03-14 17:47:01 +03:00
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');
}
2022-03-15 16:54:34 +03:00
ScoreCalculatorService::rateResponses($userQuestionnaireModel);
ScoreCalculatorService::calculateScore($userQuestionnaireModel);
2022-03-14 17:47:01 +03:00
return $userQuestionnaireModel;
}
2021-10-28 10:51:14 +03:00
}