2022-03-15 16:54:34 +03:00
|
|
|
<?php
|
|
|
|
|
2023-10-26 11:37:04 +03:00
|
|
|
namespace frontend\modules\api\services;
|
2022-03-15 16:54:34 +03:00
|
|
|
|
2023-11-16 11:55:49 +03:00
|
|
|
use common\helpers\UserQuestionnaireStatusHelper;
|
2023-10-26 11:37:04 +03:00
|
|
|
use common\services\ScoreCalculatorService;
|
2023-11-16 11:55:49 +03:00
|
|
|
use frontend\modules\api\models\questionnaire\UserQuestionnaire;
|
2022-03-17 14:50:57 +03:00
|
|
|
use yii\base\InvalidConfigException;
|
2023-11-16 11:55:49 +03:00
|
|
|
use yii\web\BadRequestHttpException;
|
2022-03-15 17:56:45 +03:00
|
|
|
use yii\web\NotFoundHttpException;
|
|
|
|
|
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
|
|
|
|
{
|
2023-10-26 11:37:04 +03:00
|
|
|
return UserQuestionnaire::findActiveUserQuestionnaires($user_id);
|
2022-03-15 17:56:45 +03:00
|
|
|
}
|
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');
|
|
|
|
}
|
2023-11-16 11:55:49 +03:00
|
|
|
|
|
|
|
if (!in_array($userQuestionnaireModel->status, UserQuestionnaireStatusHelper::listCompleteStatuses() )) {
|
|
|
|
ScoreCalculatorService::rateResponses($userQuestionnaireModel);
|
|
|
|
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
|
|
|
|
2023-11-16 11:55:49 +03:00
|
|
|
public function checkTimeLimit(UserQuestionnaire $userQuestionnaire): bool
|
2022-03-17 14:50:57 +03:00
|
|
|
{
|
2023-11-16 11:55:49 +03:00
|
|
|
if (!$userQuestionnaire->start_testing) {
|
|
|
|
$userQuestionnaire->start_testing = date('Y:m:d H:i:s');
|
|
|
|
$userQuestionnaire->save();
|
|
|
|
} elseif ($userQuestionnaire->questionnaire->time_limit) {
|
|
|
|
$limitTime = strtotime($userQuestionnaire->questionnaire->time_limit) - strtotime("00:00:00");
|
|
|
|
$currentTime = strtotime(date('Y-m-d H:i:s'));
|
|
|
|
$startTesting = strtotime($userQuestionnaire->start_testing);
|
2022-03-17 14:50:57 +03:00
|
|
|
|
2023-11-16 11:55:49 +03:00
|
|
|
if ($currentTime - $startTesting > $limitTime) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-03-17 14:50:57 +03:00
|
|
|
}
|
2023-11-16 11:55:49 +03:00
|
|
|
return true;
|
2022-03-17 14:50:57 +03:00
|
|
|
}
|
2022-03-15 16:54:34 +03:00
|
|
|
}
|