fix answerFlagLabel method in AnswerHelper

This commit is contained in:
iironside 2022-03-15 19:30:53 +03:00
parent 18d2b4327d
commit 97e79f6c94
3 changed files with 13 additions and 13 deletions

View File

@ -20,13 +20,6 @@ $this->params['breadcrumbs'][] = $this->title;
<?= Html::a('Новый ответ пользователя', ['create'], ['class' => 'btn btn-success']) ?> <?= Html::a('Новый ответ пользователя', ['create'], ['class' => 'btn btn-success']) ?>
</p> </p>
<?php
//echo $this->render('_search_by_questionnaire', [
// 'model' => $searchModel,
// ])
?>
<?= GridView::widget([ <?= GridView::widget([
'dataProvider' => $dataProvider, 'dataProvider' => $dataProvider,
'filterModel' => $searchModel, 'filterModel' => $searchModel,

View File

@ -10,12 +10,14 @@ class AnswerHelper
{ {
const FLAG_TRUE = 1; const FLAG_TRUE = 1;
const FLAG_FALSE = 0; const FLAG_FALSE = 0;
const FLAG_NOT_VERIFIED = null;
public static function answerFlagsList(): array public static function answerFlagsList(): array
{ {
return [ return [
self::FLAG_TRUE => 'Верен', self::FLAG_TRUE => 'Верен',
self::FLAG_FALSE => 'Ошибочный', self::FLAG_FALSE => 'Ошибочный',
self::FLAG_NOT_VERIFIED => 'Не проверен',
]; ];
} }
@ -24,15 +26,15 @@ class AnswerHelper
*/ */
public static function answerFlagLabel($status): string public static function answerFlagLabel($status): string
{ {
switch ($status) { switch (true) {
case self::FLAG_FALSE: case ($status === self::FLAG_FALSE):
$class = 'label label-danger'; $class = 'label label-danger';
break; break;
case self::FLAG_TRUE: case ($status === self::FLAG_TRUE):
$class = 'label label-success'; $class = 'label label-success';
break; break;
default: default:
$class = 'label label-default'; $class = 'label label-warning';
} }
return Html::tag('span', ArrayHelper::getValue(self::answerFlagsList(), $status), [ return Html::tag('span', ArrayHelper::getValue(self::answerFlagsList(), $status), [

View File

@ -5,6 +5,7 @@ namespace common\services;
use backend\modules\questionnaire\models\Answer; use backend\modules\questionnaire\models\Answer;
use common\models\UserQuestionnaire; use common\models\UserQuestionnaire;
use common\models\UserResponse; use common\models\UserResponse;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
class ScoreCalculatorService class ScoreCalculatorService
@ -44,6 +45,9 @@ class ScoreCalculatorService
return true; return true;
} }
/**
* @throws InvalidConfigException
*/
public static function calculateScore(UserQuestionnaire $userQuestionnaire) public static function calculateScore(UserQuestionnaire $userQuestionnaire)
{ {
$responses_questions = $userQuestionnaire->hasMany(UserResponse::className(), ['user_questionnaire_uuid' => 'uuid']) $responses_questions = $userQuestionnaire->hasMany(UserResponse::className(), ['user_questionnaire_uuid' => 'uuid'])
@ -71,11 +75,9 @@ class ScoreCalculatorService
} }
} }
// if($score !== null) {
self::setPercentCorrectAnswers($user_correct_answers_num, $userQuestionnaire); self::setPercentCorrectAnswers($user_correct_answers_num, $userQuestionnaire);
$userQuestionnaire->score = round($score); $userQuestionnaire->score = round($score);
$userQuestionnaire->save(); $userQuestionnaire->save();
// }
} }
protected static function isCorrect($answer_flag): bool protected static function isCorrect($answer_flag): bool
@ -91,6 +93,9 @@ class ScoreCalculatorService
return Answer::numCorrectAnswers($question_id); return Answer::numCorrectAnswers($question_id);
} }
/**
* @throws InvalidConfigException
*/
protected static function setPercentCorrectAnswers($user_correct_answers_num, UserQuestionnaire $userQuestionnaire) protected static function setPercentCorrectAnswers($user_correct_answers_num, UserQuestionnaire $userQuestionnaire)
{ {
if($user_correct_answers_num !== null) { if($user_correct_answers_num !== null) {