2021-11-08 12:41:39 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace common\helpers;
|
|
|
|
|
|
|
|
use backend\modules\questionnaire\models\Answer;
|
2022-03-14 17:47:01 +03:00
|
|
|
//use backend\modules\questionnaire\models\UserQuestionnaire;
|
|
|
|
use common\models\UserQuestionnaire;
|
2022-03-14 13:06:20 +03:00
|
|
|
use common\models\UserResponse;
|
2021-11-08 12:41:39 +03:00
|
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
|
|
|
|
class ScoreCalculatorHelper
|
|
|
|
{
|
|
|
|
public static function rateResponses(UserQuestionnaire $user_questionnaire)
|
|
|
|
{
|
|
|
|
$responses = $user_questionnaire->getUserResponses()->all();
|
|
|
|
|
2021-11-12 14:30:01 +03:00
|
|
|
foreach ($responses as $response) {
|
2021-11-08 12:41:39 +03:00
|
|
|
self::rateOneResponse($response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function rateOneResponse(UserResponse $response)
|
|
|
|
{
|
2021-11-12 14:30:01 +03:00
|
|
|
if ($response->getQuestionTypeValue() != 1) { // not open question
|
2021-11-08 12:41:39 +03:00
|
|
|
$correct_answers = $response->getCorrectAnswers();
|
2021-11-12 14:30:01 +03:00
|
|
|
foreach ($correct_answers as $correct_answer) {
|
|
|
|
if ($response->response_body === $correct_answer['answer_body']) {
|
2021-11-08 12:41:39 +03:00
|
|
|
$response->answer_flag = 1;
|
|
|
|
$response->save();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$response->answer_flag = 0;
|
|
|
|
$response->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function checkAnswerFlagsForNull(UserQuestionnaire $userQuestionnaire): bool
|
|
|
|
{
|
|
|
|
$responses = $userQuestionnaire->getUserResponses()->AsArray()->all();
|
2021-11-12 14:30:01 +03:00
|
|
|
foreach ($responses as $response) {
|
2021-11-08 12:41:39 +03:00
|
|
|
if (ArrayHelper::isIn(null, $response))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function calculateScore(UserQuestionnaire $userQuestionnaire)
|
|
|
|
{
|
2021-11-09 17:41:44 +03:00
|
|
|
$responses_questions = $userQuestionnaire->hasMany(UserResponse::className(), ['user_questionnaire_uuid' => 'uuid'])
|
2021-11-08 12:41:39 +03:00
|
|
|
->joinWith('question')->asArray()->all();
|
|
|
|
|
|
|
|
$score = null;
|
|
|
|
$user_correct_answers_num = null;
|
2021-11-12 14:30:01 +03:00
|
|
|
foreach ($responses_questions as $response_question) {
|
|
|
|
if(self::isCorrect($response_question['answer_flag'])) {
|
2021-11-08 12:41:39 +03:00
|
|
|
$user_correct_answers_num += 1;
|
2021-11-12 14:30:01 +03:00
|
|
|
switch ($response_question['question']['question_type_id']) {
|
2021-11-08 12:41:39 +03:00
|
|
|
case '1': // open question
|
|
|
|
$score += $response_question['answer_flag'] * $response_question['question']['score'];
|
|
|
|
break;
|
|
|
|
case '2': // one answer
|
|
|
|
$score += $response_question['question']['score'];
|
|
|
|
break;
|
|
|
|
case '3': // multi answer
|
|
|
|
$score += $response_question['question']['score'] / self::correctAnswersNum($response_question['question']['id']);
|
|
|
|
break;
|
2021-11-15 17:15:59 +03:00
|
|
|
case '4': // istina-loz
|
|
|
|
$score += $response_question['question']['score'];
|
|
|
|
break;
|
2021-11-08 12:41:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 17:47:01 +03:00
|
|
|
// if($score !== null) {
|
2021-11-08 12:41:39 +03:00
|
|
|
self::setPercentCorrectAnswers($user_correct_answers_num, $userQuestionnaire);
|
|
|
|
$userQuestionnaire->score = round($score);
|
|
|
|
$userQuestionnaire->save();
|
2022-03-14 17:47:01 +03:00
|
|
|
// }
|
2021-11-08 12:41:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected static function isCorrect($answer_flag): bool
|
|
|
|
{
|
|
|
|
if ($answer_flag > 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function correctAnswersNum($question_id)
|
|
|
|
{
|
|
|
|
return Answer::numCorrectAnswers($question_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function setPercentCorrectAnswers($user_correct_answers_num, UserQuestionnaire $userQuestionnaire)
|
|
|
|
{
|
2022-03-14 17:47:01 +03:00
|
|
|
if($user_correct_answers_num !== null) {
|
|
|
|
$all_correct_answers_num = $userQuestionnaire->numCorrectAnswersWithoutOpenQuestions();
|
|
|
|
$all_correct_answers_num += $userQuestionnaire->numOpenQuestionsAnswers();
|
2021-11-08 12:41:39 +03:00
|
|
|
|
2022-03-14 17:47:01 +03:00
|
|
|
$percent = $user_correct_answers_num / $all_correct_answers_num;
|
|
|
|
$userQuestionnaire->percent_correct_answers = round($percent, 2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$userQuestionnaire->percent_correct_answers = round($user_correct_answers_num, 2);
|
|
|
|
}
|
2021-11-08 12:41:39 +03:00
|
|
|
$userQuestionnaire->save();
|
|
|
|
}
|
|
|
|
}
|