move ScoreCalculator fom helpers to services

This commit is contained in:
iironside 2022-03-15 14:54:35 +03:00
parent d3e2076bbb
commit 675d78b958
4 changed files with 12 additions and 46 deletions

View File

@ -4,7 +4,7 @@ namespace backend\modules\questionnaire\controllers;
use backend\modules\questionnaire\models\Questionnaire;
use backend\modules\questionnaire\models\QuestionnaireCategory;
use common\helpers\ScoreCalculatorHelper;
use common\services\ScoreCalculatorService;
use Yii;
use backend\modules\questionnaire\models\UserQuestionnaire;
use backend\modules\questionnaire\models\UserQuestionnaireSearch;
@ -169,7 +169,7 @@ class UserQuestionnaireController extends Controller
public function actionRateResponses($id)
{
$user_questionnaire = $this->findModel($id);
ScoreCalculatorHelper::rateResponses($user_questionnaire);
ScoreCalculatorService::rateResponses($user_questionnaire);
return $this->actionView($id);
}
@ -177,7 +177,7 @@ class UserQuestionnaireController extends Controller
public function actionCalculateScore($id)
{
$user_questionnaire = $this->findModel($id);
ScoreCalculatorHelper::calculateScore($user_questionnaire);
ScoreCalculatorService::calculateScore($user_questionnaire);
return $this->actionView($id);
}

View File

@ -1,6 +1,6 @@
<?php
use common\helpers\ScoreCalculatorHelper;
use common\services\ScoreCalculatorService;
use common\helpers\AnswerHelper;
use common\helpers\StatusHelper;
use yii\bootstrap\Modal;
@ -23,20 +23,8 @@ $this->params['breadcrumbs'][] = ['label' => 'User Questionnaires', 'url' => ['i
$this->params['breadcrumbs'][] = $this->title;
YiiAsset::register($this);
?>
<?php
//$this->registerJs(
// '$("document").ready(function(){
// $("#new_note").on("pjax:end", function() {
// $.pjax.reload({container:"#user_responses"}); //Reload GridView
// });
// });'
//);
?>
<div class="user-questionnaire-view">
<!-- --><?php //var_dump($model->setPercentCorrectAnswers(4)); die();?>
<p>
<?= Html::a('Список', ['index'], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Изменить', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
@ -98,7 +86,7 @@ YiiAsset::register($this);
'class' => 'btn btn-success',
],
]);
if(ScoreCalculatorHelper::checkAnswerFlagsForNull($model))
if(ScoreCalculatorService::checkAnswerFlagsForNull($model))
{
echo 'Ответы проверены. Посчитать баллы?';
echo Html::a('Посчитать баллы', ['calculate-score', 'id' => $model->id], [
@ -169,5 +157,4 @@ YiiAsset::register($this);
]);
?>
<?php Pjax::end(); ?>
</div>

View File

@ -1,14 +1,13 @@
<?php
namespace common\helpers;
namespace common\services;
use backend\modules\questionnaire\models\Answer;
//use backend\modules\questionnaire\models\UserQuestionnaire;
use common\models\UserQuestionnaire;
use common\models\UserResponse;
use yii\helpers\ArrayHelper;
class ScoreCalculatorHelper
class ScoreCalculatorService
{
public static function rateResponses(UserQuestionnaire $user_questionnaire)
{
@ -73,9 +72,9 @@ class ScoreCalculatorHelper
}
// if($score !== null) {
self::setPercentCorrectAnswers($user_correct_answers_num, $userQuestionnaire);
$userQuestionnaire->score = round($score);
$userQuestionnaire->save();
self::setPercentCorrectAnswers($user_correct_answers_num, $userQuestionnaire);
$userQuestionnaire->score = round($score);
$userQuestionnaire->save();
// }
}

View File

@ -2,13 +2,10 @@
namespace frontend\modules\api\controllers;
use common\helpers\ScoreCalculatorHelper;
use common\services\ScoreCalculatorService;
use common\models\UserResponse;
use Exception;
use Yii;
use yii\base\InvalidConfigException;
use yii\filters\auth\HttpBearerAuth;
use yii\rest\ActiveController;
use yii\web\BadRequestHttpException;
use yii\web\ServerErrorHttpException;
@ -16,17 +13,6 @@ class UserResponseController extends ApiController
{
public $modelClass = 'common\models\UserResponse';
// public function behaviors(): array
// {
// $behaviors = parent::behaviors();
//
// $behaviors['authenticator']['authMethods'] = [
// HttpBearerAuth::className(),
// ];
//
// return $behaviors;
// }
public function verbs(): array
{
return [
@ -35,12 +21,6 @@ class UserResponseController extends ApiController
];
}
// public function actions()
// {
// $actions = parent::actions();
// unset($actions['create']);
// return $actions;
// }
/**
* @throws InvalidConfigException
@ -106,7 +86,7 @@ class UserResponseController extends ApiController
protected function saveModel($model)
{
if ($model->save()) {
ScoreCalculatorHelper::rateOneResponse($model);
ScoreCalculatorService::rateOneResponse($model);
$response = Yii::$app->getResponse();
$response->setStatusCode(201);
} elseif (!$model->hasErrors()) {