add output of the percentage of correct answers

This commit is contained in:
iIronside 2021-10-25 13:41:12 +03:00
parent e362ad45a4
commit 84ec9ef9c7
10 changed files with 1313 additions and 6 deletions

View File

@ -19,6 +19,7 @@ class UserQuestionnaireSearch extends UserQuestionnaire
return [
[['id', 'questionnaire_id', 'user_id', 'score', 'status'], 'integer'],
[['uuid', 'created_at', 'updated_at'], 'safe'],
[['percent_correct_answers'], 'number'],
];
}
@ -65,6 +66,7 @@ class UserQuestionnaireSearch extends UserQuestionnaire
'updated_at' => $this->updated_at,
'score' => $this->score,
'status' => $this->status,
'percent_correct_answers' => $this->percent_correct_answers,
]);
$query->andFilterWhere(['like', 'uuid', $this->uuid]);

View File

@ -31,6 +31,8 @@ use yii\widgets\ActiveForm;
<?php // echo $form->field($model, 'status') ?>
<?php // echo $form->field($model, 'percent_correct_answers') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>

View File

@ -39,6 +39,13 @@ $this->params['breadcrumbs'][] = $this->title;
],
// 'UUID',
'score',
[
'attribute' => 'percent_correct_answers',
'value' => function($model) {
$percent = $model->percent_correct_answers * 100;
return $percent . '%';
}
],
[
'attribute' => 'status',
'format' => 'raw',

View File

@ -20,6 +20,8 @@ YiiAsset::register($this);
?>
<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']) ?>
@ -46,10 +48,17 @@ YiiAsset::register($this);
],
'uuid',
'score',
[
'attribute' => 'percent_correct_answers',
'value' => function($model) {
$percent = $model->percent_correct_answers * 100;
return $percent . '%';
}
],
[
'attribute' => 'status',
'format' => 'raw',
'value' => function ($model) {
'value' => function($model) {
return Html::tag(
'span',
$model->status ? 'Active' : 'Not Active',

View File

@ -117,7 +117,7 @@ class Answer extends \yii\db\ActiveRecord
return $this->flags[$this->status];
}
static function getCurrentAnswersNum($question_id)
static function getCorrectAnswersNum($question_id)
{
return Answer::find()
->where('question_id=:question_id', [':question_id' => $question_id])

View File

@ -10,6 +10,7 @@ use yii\db\Expression;
use yii\helpers\ArrayHelper;
use backend\modules\questionnaire\models\Question;
use backend\modules\questionnaire\models\UserResponse;
use \backend\modules\questionnaire\models\Answer;
/**
* This is the model class for table "user_questionnaire".
@ -22,6 +23,7 @@ use backend\modules\questionnaire\models\UserResponse;
* @property string $updated_at
* @property int $score
* @property int $status
* @property double $percent_correct_answers
*
* @property Questionnaire $questionnaire
* @property User $user
@ -61,6 +63,7 @@ class UserQuestionnaire extends \yii\db\ActiveRecord
return [
[['questionnaire_id', 'user_id', 'status'], 'required'],
[['questionnaire_id', 'user_id', 'score', 'status'], 'integer'],
[['percent_correct_answers'], 'number'],
[['created_at', 'updated_at'], 'safe'],
[['uuid'], 'string', 'max' => 36],
[['uuid'], 'unique'],
@ -93,6 +96,7 @@ class UserQuestionnaire extends \yii\db\ActiveRecord
'status' => 'Статус',
'created_at' => 'Дата создания',
'updated_at' => 'Дата обновления',
'percent_correct_answers' => 'Процент правильных ответов',
];
}
@ -182,7 +186,7 @@ class UserQuestionnaire extends \yii\db\ActiveRecord
public function getScore()
{
$responses_questions = $this->hasMany(UserResponse::className(), ['user_questionnaire_id' => 'id'])
->joinWith('question')->all();
->joinWith('question')->asArray()->all();
$calc_score = $this->calculateScore($responses_questions);
$this->score = $calc_score;
@ -192,10 +196,12 @@ class UserQuestionnaire extends \yii\db\ActiveRecord
protected function calculateScore($responses_questions)
{
$score = null;
$user_correct_answers_num = null;
foreach ($responses_questions as $response_question)
{
if($this->isCorrect($response_question['answer_flag']))
{
$user_correct_answers_num += 1;
switch ($response_question['question']['question_type_id'])
{
case '1': // open question
@ -210,6 +216,9 @@ class UserQuestionnaire extends \yii\db\ActiveRecord
}
}
}
$this->setPercentCorrectAnswers($user_correct_answers_num);
if($score === null) {
return $score;
}
@ -220,7 +229,7 @@ class UserQuestionnaire extends \yii\db\ActiveRecord
protected function correctAnswersNum($question_id)
{
return Answer::getCurrentAnswersNum($question_id);
return Answer::getCorrectAnswersNum($question_id);
}
protected function isCorrect($answer_flag): bool
@ -231,6 +240,34 @@ class UserQuestionnaire extends \yii\db\ActiveRecord
return false;
}
protected function setPercentCorrectAnswers($user_correct_answers_num)
{
$all_correct_answers_num = $this->numCorrectAnswersWithoutOpenQuestions();
$all_correct_answers_num += $this->numOpenQuestionsAnswers();
$percent = $user_correct_answers_num / $all_correct_answers_num;
$this->percent_correct_answers = round($percent, 2);
$this->save();
}
protected function numCorrectAnswersWithoutOpenQuestions()
{
return $this->hasMany(Answer::className(), ['question_id' => 'question_id'])
->viaTable('user_response', ['user_questionnaire_id' => 'id'])
->where(['answer_flag' => '1'])
->andWhere(['status' => '1'])
->count();
}
protected function numOpenQuestionsAnswers()
{
return $this->hasMany(Question::className(), ['id' => 'question_id'])
->viaTable('user_response', ['user_questionnaire_id' => 'id'])
->where(['question_type_id' => '1'])
->count();
}
public function rateResponses()
{
$responses = $this->getUserResponses()->all();

View File

@ -156,9 +156,9 @@ class UserResponse extends \yii\db\ActiveRecord
{
$correct_answers = $this->getCorrectAnswers();
foreach ($correct_answers as $correct_answerr)
foreach ($correct_answers as $correct_answer)
{
if ($this->response_body === $correct_answerr['answer_body'])
if ($this->response_body === $correct_answer['answer_body'])
{
$this->answer_flag = 1;
$this->save();

View File

@ -0,0 +1,25 @@
<?php
use yii\db\Migration;
/**
* Handles adding columns to table `{{%user_questionnaire}}`.
*/
class m211025_065459_add_percent_correct_answers_column_to_user_questionnaire_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->addColumn('user_questionnaire', 'percent_correct_answers', $this->double());
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropColumn('user_questionnaire', 'percent_correct_answers');
}
}

File diff suppressed because it is too large Load Diff

View File

@ -167,3 +167,11 @@ Stack trace:
#8 /var/www/guild.loc/vendor/yiisoft/yii2/web/ErrorAction.php(118): yii\web\" while reading upstream, client: 127.0.0.1, server: backend.guild.loc, request: "GET /questionnaire/questionnaire-controller/index/ HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "backend.guild.loc"
2021/10/22 13:39:42 [error] 729#729: *502 FastCGI sent in stderr: "PHP message: PHP Fatal error: Declaration of backend\modules\questionnaire\models\UserQuestionnaireSearch::rules() must be compatible with common\models\UserQuestionnaire::rules(): array in /var/www/guild.loc/backend/modules/questionnaire/models/UserQuestionnaireSearch.php on line 17" while reading response header from upstream, client: 127.0.0.1, server: backend.guild.loc, request: "GET /questionnaire/user-questionnaire HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "backend.guild.loc", referrer: "http://backend.guild.loc/questionnaire/user-response"
2021/10/22 13:40:28 [error] 729#729: *506 FastCGI sent in stderr: "PHP message: PHP Fatal error: Declaration of backend\modules\questionnaire\models\UserQuestionnaireSearch::rules() must be compatible with common\models\UserQuestionnaire::rules(): array in /var/www/guild.loc/backend/modules/questionnaire/models/UserQuestionnaireSearch.php on line 17" while reading response header from upstream, client: 127.0.0.1, server: backend.guild.loc, request: "GET /questionnaire/user-questionnaire HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "backend.guild.loc", referrer: "http://backend.guild.loc/questionnaire/user-response"
2021/10/25 10:28:05 [error] 784#784: *167 FastCGI sent in stderr: "PHP message: PHP Notice: Undefined index: telegramBotToken in /var/www/guild.loc/frontend/config/main.php on line 90PHP message: PHP Notice: Undefined index: telegramBotChatId in /var/www/guild.loc/frontend/config/main.php on line 91" while reading response header from upstream, client: 127.0.0.1, server: guild.loc, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "guild.loc"
2021/10/25 10:28:05 [error] 784#784: *167 FastCGI sent in stderr: "PHP message: PHP Notice: Undefined index: telegramBotToken in /var/www/guild.loc/frontend/config/main.php on line 90PHP message: PHP Notice: Undefined index: telegramBotChatId in /var/www/guild.loc/frontend/config/main.php on line 91" while reading response header from upstream, client: 127.0.0.1, server: guild.loc, request: "GET /site/login HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "guild.loc"
2021/10/25 10:28:06 [error] 784#784: *167 FastCGI sent in stderr: "PHP message: PHP Notice: Undefined index: telegramBotToken in /var/www/guild.loc/frontend/config/main.php on line 90PHP message: PHP Notice: Undefined index: telegramBotChatId in /var/www/guild.loc/frontend/config/main.php on line 91" while reading response header from upstream, client: 127.0.0.1, server: guild.loc, request: "GET /debug/default/toolbar?tag=61765c85e35c4 HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "guild.loc", referrer: "http://guild.loc/site/login"
2021/10/25 10:28:13 [error] 784#784: *167 FastCGI sent in stderr: "PHP message: PHP Notice: Undefined index: telegramBotToken in /var/www/guild.loc/frontend/config/main.php on line 90PHP message: PHP Notice: Undefined index: telegramBotChatId in /var/www/guild.loc/frontend/config/main.php on line 91" while reading response header from upstream, client: 127.0.0.1, server: guild.loc, request: "POST /site/login HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "guild.loc", referrer: "http://guild.loc/site/login"
2021/10/25 10:28:14 [error] 784#784: *167 FastCGI sent in stderr: "PHP message: PHP Notice: Undefined index: telegramBotToken in /var/www/guild.loc/frontend/config/main.php on line 90PHP message: PHP Notice: Undefined index: telegramBotChatId in /var/www/guild.loc/frontend/config/main.php on line 91" while reading response header from upstream, client: 127.0.0.1, server: guild.loc, request: "GET /debug/default/toolbar?tag=61765c8cee0ef HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "guild.loc", referrer: "http://guild.loc/site/login"
2021/10/25 10:28:23 [error] 784#784: *167 FastCGI sent in stderr: "PHP message: PHP Notice: Undefined index: telegramBotToken in /var/www/guild.loc/frontend/config/main.php on line 90PHP message: PHP Notice: Undefined index: telegramBotChatId in /var/www/guild.loc/frontend/config/main.php on line 91" while reading response header from upstream, client: 127.0.0.1, server: guild.loc, request: "POST /site/login HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "guild.loc", referrer: "http://guild.loc/site/login"
2021/10/25 10:28:23 [error] 784#784: *167 FastCGI sent in stderr: "PHP message: PHP Notice: Undefined index: telegramBotToken in /var/www/guild.loc/frontend/config/main.php on line 90PHP message: PHP Notice: Undefined index: telegramBotChatId in /var/www/guild.loc/frontend/config/main.php on line 91" while reading response header from upstream, client: 127.0.0.1, server: guild.loc, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "guild.loc", referrer: "http://guild.loc/site/login"
2021/10/25 10:28:23 [error] 784#784: *167 FastCGI sent in stderr: "PHP message: PHP Notice: Undefined index: telegramBotToken in /var/www/guild.loc/frontend/config/main.php on line 90PHP message: PHP Notice: Undefined index: telegramBotChatId in /var/www/guild.loc/frontend/config/main.php on line 91" while reading response header from upstream, client: 127.0.0.1, server: guild.loc, request: "GET /debug/default/toolbar?tag=61765c97b7d67 HTTP/1.1", upstream: "fastcgi://unix:/run/php/php-fpm.sock:", host: "guild.loc", referrer: "http://guild.loc/"