refactor api/UserQuestionnaire
This commit is contained in:
parent
a5ef1d5008
commit
18d2b4327d
@ -2,7 +2,36 @@
|
|||||||
|
|
||||||
namespace common\services;
|
namespace common\services;
|
||||||
|
|
||||||
|
use common\models\UserQuestionnaire;
|
||||||
|
use yii\web\NotFoundHttpException;
|
||||||
|
|
||||||
class UserQuestionnaireService
|
class UserQuestionnaireService
|
||||||
{
|
{
|
||||||
|
public static function getQuestionnaireList($user_id): array
|
||||||
|
{
|
||||||
|
$userQuestionnaireModels = UserQuestionnaire::findActiveUserQuestionnaires($user_id);
|
||||||
|
array_walk($userQuestionnaireModels, function (&$arr) {
|
||||||
|
unset(
|
||||||
|
$arr['questionnaire_id'],
|
||||||
|
$arr['created_at'],
|
||||||
|
$arr['updated_at'],
|
||||||
|
$arr['id'],
|
||||||
|
);
|
||||||
|
});
|
||||||
|
return $userQuestionnaireModels;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws NotFoundHttpException
|
||||||
|
*/
|
||||||
|
public static function calculateScore($user_questionnaire_uuid)
|
||||||
|
{
|
||||||
|
$userQuestionnaireModel = UserQuestionnaire::findOne(['uuid' => $user_questionnaire_uuid]);
|
||||||
|
if(empty($userQuestionnaireModel)) {
|
||||||
|
throw new NotFoundHttpException('The questionnaire with this uuid does not exist');
|
||||||
|
}
|
||||||
|
ScoreCalculatorService::rateResponses($userQuestionnaireModel);
|
||||||
|
ScoreCalculatorService::calculateScore($userQuestionnaireModel);
|
||||||
|
return $userQuestionnaireModel;
|
||||||
|
}
|
||||||
}
|
}
|
@ -2,11 +2,9 @@
|
|||||||
|
|
||||||
namespace frontend\modules\api\controllers;
|
namespace frontend\modules\api\controllers;
|
||||||
|
|
||||||
use common\services\ScoreCalculatorService;
|
use common\services\UserQuestionnaireService;
|
||||||
use common\models\UserQuestionnaire;
|
|
||||||
use Yii;
|
|
||||||
use yii\filters\auth\HttpBearerAuth;
|
|
||||||
use yii\web\NotFoundHttpException;
|
use yii\web\NotFoundHttpException;
|
||||||
|
use yii\web\ServerErrorHttpException;
|
||||||
|
|
||||||
class UserQuestionnaireController extends ApiController
|
class UserQuestionnaireController extends ApiController
|
||||||
{
|
{
|
||||||
@ -21,51 +19,28 @@ class UserQuestionnaireController extends ApiController
|
|||||||
/**
|
/**
|
||||||
* @throws NotFoundHttpException
|
* @throws NotFoundHttpException
|
||||||
*/
|
*/
|
||||||
public function actionQuestionnairesList()//: array
|
public function actionQuestionnairesList($user_id): array
|
||||||
{
|
|
||||||
$user_id = Yii::$app->request->get('user_id');
|
|
||||||
|
|
||||||
if(empty($user_id) or !is_numeric($user_id))
|
|
||||||
{
|
{
|
||||||
|
if (empty($user_id) or !is_numeric($user_id)) {
|
||||||
throw new NotFoundHttpException('Incorrect user ID');
|
throw new NotFoundHttpException('Incorrect user ID');
|
||||||
}
|
}
|
||||||
|
$userQuestionnaireModels = UserQuestionnaireService::getQuestionnaireList($user_id);
|
||||||
$userQuestionnaireModels = UserQuestionnaire::findActiveUserQuestionnaires($user_id);
|
|
||||||
if(empty($userQuestionnaireModels)) {
|
if(empty($userQuestionnaireModels)) {
|
||||||
throw new NotFoundHttpException('Active questionnaire not found');
|
throw new NotFoundHttpException('Active questionnaire not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
array_walk( $userQuestionnaireModels, function(&$arr){
|
|
||||||
unset(
|
|
||||||
$arr['questionnaire_id'],
|
|
||||||
// $arr['created_at'],
|
|
||||||
// $arr['updated_at'],
|
|
||||||
$arr['id'],
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
return $userQuestionnaireModels;
|
return $userQuestionnaireModels;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function actionQuestionnaireCompleted()
|
/**
|
||||||
|
* @throws NotFoundHttpException
|
||||||
|
* @throws ServerErrorHttpException
|
||||||
|
*/
|
||||||
|
public function actionQuestionnaireCompleted($user_questionnaire_uuid)
|
||||||
{
|
{
|
||||||
// return Yii::$app->request;
|
$userQuestionnaireModel = UserQuestionnaireService::calculateScore($user_questionnaire_uuid);
|
||||||
$user_questionnaire_uuid = Yii::$app->request->get('user_questionnaire_uuid');
|
if ($userQuestionnaireModel->errors) {
|
||||||
|
throw new ServerErrorHttpException(json_encode($userQuestionnaireModel->errors));
|
||||||
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');
|
|
||||||
}
|
|
||||||
|
|
||||||
ScoreCalculatorService::rateResponses($userQuestionnaireModel);
|
|
||||||
ScoreCalculatorService::calculateScore($userQuestionnaireModel);
|
|
||||||
|
|
||||||
return $userQuestionnaireModel;
|
return $userQuestionnaireModel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user