2022-03-15 16:54:34 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace common\services;
|
|
|
|
|
2022-03-17 14:50:57 +03:00
|
|
|
use common\models\Question;
|
2022-03-15 17:56:45 +03:00
|
|
|
use common\models\UserQuestionnaire;
|
2022-03-17 14:50:57 +03:00
|
|
|
use yii\base\InvalidConfigException;
|
2022-03-15 17:56:45 +03:00
|
|
|
use yii\web\NotFoundHttpException;
|
2022-03-17 14:50:57 +03:00
|
|
|
use yii\web\ServerErrorHttpException;
|
2022-03-15 17:56:45 +03:00
|
|
|
|
2022-03-15 16:54:34 +03:00
|
|
|
class UserQuestionnaireService
|
|
|
|
{
|
2022-03-15 17:56:45 +03:00
|
|
|
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'],
|
2022-10-19 12:27:35 +03:00
|
|
|
$arr['id']
|
2022-03-15 17:56:45 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
return $userQuestionnaireModels;
|
|
|
|
}
|
2022-03-15 16:54:34 +03:00
|
|
|
|
2022-03-15 17:56:45 +03:00
|
|
|
/**
|
|
|
|
* @throws NotFoundHttpException
|
2022-03-17 14:50:57 +03:00
|
|
|
* @throws InvalidConfigException
|
2022-03-15 17:56:45 +03:00
|
|
|
*/
|
2022-03-17 14:50:57 +03:00
|
|
|
public static function calculateScore($user_questionnaire_uuid): UserQuestionnaire
|
2022-03-15 17:56:45 +03:00
|
|
|
{
|
|
|
|
$userQuestionnaireModel = UserQuestionnaire::findOne(['uuid' => $user_questionnaire_uuid]);
|
2022-03-17 14:50:57 +03:00
|
|
|
if (empty($userQuestionnaireModel)) {
|
2022-03-15 17:56:45 +03:00
|
|
|
throw new NotFoundHttpException('The questionnaire with this uuid does not exist');
|
|
|
|
}
|
|
|
|
ScoreCalculatorService::rateResponses($userQuestionnaireModel);
|
2022-03-17 14:50:57 +03:00
|
|
|
if (ScoreCalculatorService::checkAnswerFlagsForNull($userQuestionnaireModel)) {
|
|
|
|
ScoreCalculatorService::calculateScore($userQuestionnaireModel);
|
|
|
|
} else {
|
|
|
|
$userQuestionnaireModel->status = 3;
|
|
|
|
$userQuestionnaireModel->save();
|
|
|
|
}
|
2022-03-15 17:56:45 +03:00
|
|
|
return $userQuestionnaireModel;
|
|
|
|
}
|
2022-03-17 14:50:57 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws ServerErrorHttpException
|
|
|
|
*/
|
|
|
|
public static function getQuestionNumber($user_questionnaire_uuid): array
|
|
|
|
{
|
|
|
|
$userQuestionnaireModel = UserQuestionnaire::findOne(['uuid' => $user_questionnaire_uuid]);
|
|
|
|
if (empty($userQuestionnaireModel)) {
|
|
|
|
throw new ServerErrorHttpException(json_encode('Not found UserQuestionnaire'));
|
|
|
|
}
|
|
|
|
$count = Question::find()
|
|
|
|
->where(['questionnaire_id' => $userQuestionnaireModel->questionnaire_id])
|
|
|
|
->andWhere(['status' => 1])
|
|
|
|
->count();
|
|
|
|
return array('question_number' => $count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws ServerErrorHttpException
|
|
|
|
*/
|
|
|
|
public static function getPointsNumber($user_questionnaire_uuid)
|
|
|
|
{
|
|
|
|
$userQuestionnaireModel = UserQuestionnaire::findOne(['uuid' => $user_questionnaire_uuid]);
|
|
|
|
if (empty($userQuestionnaireModel)) {
|
|
|
|
throw new ServerErrorHttpException(json_encode('Not found UserQuestionnaire'));
|
|
|
|
}
|
|
|
|
$pointSum = Question::find()
|
|
|
|
->where(['questionnaire_id' => $userQuestionnaireModel->questionnaire_id])
|
|
|
|
->andWhere(['status' => 1])
|
|
|
|
->sum('score');
|
|
|
|
return array('sum_point' => $pointSum);
|
|
|
|
}
|
2022-03-15 16:54:34 +03:00
|
|
|
}
|