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
|
|
|
|
*/
|
2022-02-15 11:29:16 +03:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2022-02-15 11:29:16 +03:00
|
|
|
$userQuestionnaireModels = UserQuestionnaire::findActiveUserQuestionnaires($user_id);
|
|
|
|
if(empty($userQuestionnaireModels)) {
|
2021-10-28 10:51:14 +03:00
|
|
|
throw new NotFoundHttpException('Active questionnaire not found');
|
|
|
|
}
|
|
|
|
|
2022-02-15 11:29:16 +03:00
|
|
|
array_walk( $userQuestionnaireModels, function(&$arr){
|
2021-10-28 10:51:14 +03:00
|
|
|
unset(
|
2021-11-12 14:30:01 +03:00
|
|
|
$arr['questionnaire_id'],
|
2022-02-15 11:29:16 +03:00
|
|
|
// $arr['created_at'],
|
|
|
|
// $arr['updated_at'],
|
2021-11-12 14:30:01 +03:00
|
|
|
$arr['id'],
|
2021-10-28 10:51:14 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-02-15 11:29:16 +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
|
|
|
}
|