add filters, refactoring

This commit is contained in:
iIronside
2021-11-08 12:41:39 +03:00
parent 5be69526ac
commit c1c24fc5e4
42 changed files with 12364 additions and 443 deletions

View File

@ -85,13 +85,7 @@ class Answer extends \yii\db\ActiveRecord
return $this->hasOne(Question::className(), ['id' => 'question_id']);
}
public function getQuestionBody()
{
return $this->getQuestion()->one()->question_body;
}
static function getCorrectAnswersNum($question_id)
static function numCorrectAnswers($question_id)
{
return Answer::find()
->where('question_id=:question_id', [':question_id' => $question_id])
@ -100,7 +94,7 @@ class Answer extends \yii\db\ActiveRecord
->count();
}
public static function getActiveAnswers($question_id): array
public static function activeAnswers($question_id): array
{
return self::find()->where(['question_id' => $question_id])
->andWhere(['status' => '1'])

View File

@ -2,7 +2,6 @@
namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;
@ -133,32 +132,7 @@ class Question extends \yii\db\ActiveRecord
return $this->hasMany(UserResponse::className(), ['question_id' => 'id']);
}
public function getStatusText()
{
return $this->statuses[$this->status];
}
public function getQuestionnaireTitle()
{
return $this->getQuestionnaire()->one()->title;
}
public function getQuestionTitle()
{
return $this->getQuestionType()->one()->question_type;
}
public function getLimitTime()
{
if ($this->time_limit === null)
{
return 'Не ограничено';
}
return date("i:s", mktime(null, null, $this->time_limit));
}
public static function getActiveQuestions($questionnaire_id)
public static function activeQuestions($questionnaire_id)
{
return self::find()->where(['questionnaire_id' => $questionnaire_id])
->andWhere(['status' => '1'])

View File

@ -2,8 +2,8 @@
namespace common\models;
use Yii;
use yii\behaviors\SluggableBehavior;
use yii\db\ActiveRecord;
/**
* This is the model class for table "question_type".
@ -14,7 +14,7 @@ use yii\behaviors\SluggableBehavior;
*
* @property Question[] $questions
*/
class QuestionType extends \yii\db\ActiveRecord
class QuestionType extends ActiveRecord
{
/**
* {@inheritdoc}

View File

@ -3,6 +3,8 @@
namespace common\models;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
use yii\db\Expression;
use yii\helpers\ArrayHelper;
@ -21,7 +23,7 @@ use yii\helpers\ArrayHelper;
* @property QuestionnaireCategory $category
* @property UserQuestionnaire[] $userQuestionnaires
*/
class Questionnaire extends \yii\db\ActiveRecord
class Questionnaire extends ActiveRecord
{
const STATUS_PASSIVE = 0;
const STATUS_ACTIVE = 1;
@ -92,7 +94,7 @@ class Questionnaire extends \yii\db\ActiveRecord
}
/**
* @return \yii\db\ActiveQuery
* @return ActiveQuery
*/
public function getQuestions()
{
@ -100,7 +102,7 @@ class Questionnaire extends \yii\db\ActiveRecord
}
/**
* @return \yii\db\ActiveQuery
* @return ActiveQuery
*/
public function getAnswers()
{
@ -109,36 +111,25 @@ class Questionnaire extends \yii\db\ActiveRecord
}
/**
* @return \yii\db\ActiveQuery
* @return ActiveQuery
*/
public function getCategory()
public function getCategory(): ActiveQuery
{
return $this->hasOne(QuestionnaireCategory::className(), ['id' => 'category_id']);
}
/**
* @return \yii\db\ActiveQuery
* @return ActiveQuery
*/
public function getUserQuestionnaires()
{
return $this->hasMany(UserQuestionnaire::className(), ['questionnaire_id' => 'id']);
}
public function getCategoryTitle()
{
return $this->getCategory()->one()->title;
}
public static function getQuestionnaireByCategory($category_id)
public static function questionnairesOfCategoryArr($category_id): array
{
$categories = self::find()->where(['category_id' => $category_id, 'status' => '1'])->all();
$catArr = ArrayHelper::map($categories, 'id', 'title');
$formattedCatArr = array();
foreach ($catArr as $key => $value){
$formattedCatArr[] = array('id' => $key, 'name' => $value);
}
return $formattedCatArr;
return ArrayHelper::map($categories, 'id', 'title');
}
}

View File

@ -3,6 +3,7 @@
namespace common\models;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;
use yii\db\Expression;
use yii\helpers\ArrayHelper;
@ -71,16 +72,10 @@ class QuestionnaireCategory extends \yii\db\ActiveRecord
}
/**
* @return \yii\db\ActiveQuery
* @return ActiveQuery
*/
public function getQuestionnaires()
{
return $this->hasMany(Questionnaire::className(), ['category_id' => 'id']);
}
public function getIdTitlesArr()
{
$categories = self::find()->select(['id', 'title'])->where(['status' => '1'])->all();
return ArrayHelper::map($categories,'id','title');
}
}

View File

@ -3,7 +3,6 @@
namespace common\models;
use Ramsey\Uuid\Uuid;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;
use yii\db\Expression;
@ -134,11 +133,6 @@ class UserQuestionnaire extends \yii\db\ActiveRecord
return $this->getUser()->one()->username;
}
public function getCategoryId(): string
{
return $this->created_at;
}
public static function getQuestionnaireByUser($id): array
{
$questionnaire = ArrayHelper::map(self::find()->where(['user_id' => $id])
@ -153,105 +147,13 @@ class UserQuestionnaire extends \yii\db\ActiveRecord
return $formatQuestionnaireArr;
}
public function getStatuses(): array
{
return [
self::STATUS_ACTIVE => 'Активен',
self::STATUS_PASSIVE => 'Не используется'
];
}
public function getStatusText()
{
return $this->statuses[$this->status];
}
public function checkAnswerFlagsForNull(): bool
{
$responses = $this->getUserResponses()->AsArray()->all();
foreach ($responses as $response)
{
if (ArrayHelper::isIn(null, $response))
return false;
}
return true;
}
public function getQuestions(): ActiveQuery
{
return $this->hasMany(Question::className(), ['id' => 'question_id'])
->viaTable('user_response', ['user_questionnaire_id' => 'id']);
}
public function getScore()
{
$responses_questions = $this->hasMany(UserResponse::className(), ['user_questionnaire_id' => 'id'])
->joinWith('question')->asArray()->all();
$calc_score = $this->calculateScore($responses_questions);
$this->score = $calc_score;
$this->save();
}
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
$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'] / $this->correctAnswersNum($response_question['question']['id']);
break;
}
}
}
$this->setPercentCorrectAnswers($user_correct_answers_num);
if($score === null) {
return $score;
}
else {
return round($score);
}
}
protected function correctAnswersNum($question_id)
{
return Answer::getCorrectAnswersNum($question_id);
}
protected function isCorrect($answer_flag): bool
{
if ($answer_flag > 0) {
return true;
}
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()
public function numCorrectAnswersWithoutOpenQuestions()
{
return $this->hasMany(Answer::className(), ['question_id' => 'question_id'])
->viaTable('user_response', ['user_questionnaire_id' => 'id'])
@ -260,7 +162,7 @@ class UserQuestionnaire extends \yii\db\ActiveRecord
->count();
}
protected function numOpenQuestionsAnswers()
public function numOpenQuestionsAnswers()
{
return $this->hasMany(Question::className(), ['id' => 'question_id'])
->viaTable('user_response', ['user_questionnaire_id' => 'id'])
@ -268,16 +170,6 @@ class UserQuestionnaire extends \yii\db\ActiveRecord
->count();
}
public function rateResponses()
{
$responses = $this->getUserResponses()->all();
foreach ($responses as $response)
{
$response->rateResponse();
}
}
public static function findActiveUserQuestionnaires($user_id)
{
return self::find()->where(['user_id' => $user_id])

View File

@ -2,9 +2,9 @@
namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
use yii\db\Expression;
use yii\helpers\ArrayHelper;
use \backend\modules\questionnaire\models\UserQuestionnaire;
@ -25,7 +25,7 @@ use \backend\modules\questionnaire\models\UserQuestionnaire;
* @property Question $question
* @property User $user
*/
class UserResponse extends \yii\db\ActiveRecord
class UserResponse extends ActiveRecord
{
/**
* {@inheritdoc}
@ -104,17 +104,7 @@ class UserResponse extends \yii\db\ActiveRecord
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
public function getUserName()
{
return $this->getUser()->one()->username;
}
public function getQuestionBody()
{
return $this->getQuestion()->one()->question_body;
}
private function getCorrectAnswers()
public function getCorrectAnswers()
{
return $this->hasMany(Answer::class, ['question_id' => 'question_id'])
->where(['answer_flag' => '1'])->all();
@ -124,49 +114,18 @@ class UserResponse extends \yii\db\ActiveRecord
{
$tmp = $this->hasOne(Questionnaire::className(), ['id' => 'questionnaire_id'])
->viaTable('user_questionnaire', ['id' => 'user_questionnaire_id'])->one();
$value = ArrayHelper::getValue($tmp, 'title');
return $value;
return ArrayHelper::getValue($tmp, 'title');
}
public function getQuestionType()
{
$qType = $this->hasOne(QuestionType::class, ['id' => 'question_type_id'])
->viaTable('question', ['id' => 'question_id'])->one();
$value = ArrayHelper::getValue($qType, 'question_type');
return $value;
return ArrayHelper::getValue($this->hasOne(QuestionType::class, ['id' => 'question_type_id'])
->viaTable('question', ['id' => 'question_id'])->one(), 'question_type');
}
public function getQuestionTypeValue()
{
$qType = $this->getQuestion()->one();
$value = ArrayHelper::getValue($qType, 'question_type_id');
return $value;
}
public function rateResponse()
{
if ($this->answer_flag === null && $this->getQuestionTypeValue() != 1) // not open question
{
$correct_answers = $this->getCorrectAnswers();
foreach ($correct_answers as $correct_answer)
{
if ($this->response_body === $correct_answer['answer_body'])
{
$this->answer_flag = 1;
$this->save();
return;
}
}
$this->answer_flag = 0;
$this->save();
}
return ArrayHelper::getValue($qType, 'question_type_id');
}
}