added get-points-number, get-question-number methods, update status in UserQuestionnaire, some refactoring

This commit is contained in:
iironside
2022-03-17 14:50:57 +03:00
parent 120cf406c3
commit 55089accb5
14 changed files with 895 additions and 15 deletions

View File

@ -0,0 +1,54 @@
<?php
namespace common\helpers;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use Exception;
class UserQuestionnaireStatusHelper
{
const STATUS_PASSIVE = 0;
const STATUS_ACTIVE = 1;
const STATUS_COMPLETED = 2;
const STATUS_ON_INSPECTION = 3;
public static function statusList() :array
{
return [
self::STATUS_PASSIVE => 'Не используется',
self::STATUS_ACTIVE => 'Активен',
self::STATUS_COMPLETED => 'Завершён',
self::STATUS_ON_INSPECTION => 'На проверке'
];
}
/**
* @throws Exception
*/
public static function statusName($status): string
{
return ArrayHelper::getValue(self::statusList(), $status);
}
/**
* @throws Exception
*/
public static function statusLabel($status): string
{
switch ($status) {
case self::STATUS_PASSIVE:
$class = 'label label-danger';
break;
case ($status === self::STATUS_ACTIVE or $status === self::STATUS_COMPLETED):
$class = 'label label-success';
break;
default:
$class = 'label label-default';
}
return Html::tag('span', ArrayHelper::getValue(self::statusList(), $status), [
'class' => $class,
]);
}
}

View File

@ -41,4 +41,9 @@ class Position extends \yii\db\ActiveRecord
'name' => 'Название',
];
}
public function getUserCard(): \yii\db\ActiveQuery
{
return $this->hasMany(UserCard::class, ['position_id' => 'id']);
}
}

View File

@ -181,7 +181,7 @@ class UserQuestionnaire extends ActiveRecord
{
$models = self::find()
->where(['user_id' => $user_id])
->andWhere(['user_questionnaire.status' => '1'])
->andWhere(['not', ['user_questionnaire.status' => 0]])
->all();
$modelsArr = array();

View File

@ -77,6 +77,8 @@ class ScoreCalculatorService
self::setPercentCorrectAnswers($user_correct_answers_num, $userQuestionnaire);
$userQuestionnaire->score = round($score);
$userQuestionnaire->status = 2;
$userQuestionnaire->testing_date = date('Y:m:d H:i:s');
$userQuestionnaire->save();
}
@ -108,6 +110,5 @@ class ScoreCalculatorService
else {
$userQuestionnaire->percent_correct_answers = round($user_correct_answers_num, 2);
}
$userQuestionnaire->save();
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace common\services;
use common\models\UserCard;
use yii\web\ServerErrorHttpException;
class UserCardService
{
/**
* @throws ServerErrorHttpException
*/
public static function getUserCard($user_id): array
{
$userCard = UserCard::findOne(['id_user' => $user_id]);
if (empty($userCard)) {
throw new ServerErrorHttpException(json_encode($userCard->errors));
}
return array('fio' => $userCard->fio,
'photo' => $userCard->photo,
'gender' => $userCard->gender,
'level' => $userCard->level,
'years_of_exp' => $userCard->years_of_exp,
'specification' => $userCard->specification,
'position_name' => $userCard->position->name);
}
}

View File

@ -2,8 +2,11 @@
namespace common\services;
use common\models\Question;
use common\models\UserQuestionnaire;
use yii\base\InvalidConfigException;
use yii\web\NotFoundHttpException;
use yii\web\ServerErrorHttpException;
class UserQuestionnaireService
{
@ -23,15 +26,53 @@ class UserQuestionnaireService
/**
* @throws NotFoundHttpException
* @throws InvalidConfigException
*/
public static function calculateScore($user_questionnaire_uuid)
public static function calculateScore($user_questionnaire_uuid): UserQuestionnaire
{
$userQuestionnaireModel = UserQuestionnaire::findOne(['uuid' => $user_questionnaire_uuid]);
if(empty($userQuestionnaireModel)) {
if (empty($userQuestionnaireModel)) {
throw new NotFoundHttpException('The questionnaire with this uuid does not exist');
}
ScoreCalculatorService::rateResponses($userQuestionnaireModel);
ScoreCalculatorService::calculateScore($userQuestionnaireModel);
if (ScoreCalculatorService::checkAnswerFlagsForNull($userQuestionnaireModel)) {
ScoreCalculatorService::calculateScore($userQuestionnaireModel);
} else {
$userQuestionnaireModel->status = 3;
$userQuestionnaireModel->save();
}
return $userQuestionnaireModel;
}
/**
* @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);
}
}