2021-10-22 17:02:28 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace common\models;
|
|
|
|
|
2021-11-09 17:41:44 +03:00
|
|
|
use common\helpers\UUIDHelper;
|
2021-11-12 14:30:01 +03:00
|
|
|
use Exception;
|
|
|
|
use yii\base\InvalidConfigException;
|
2021-10-22 17:02:28 +03:00
|
|
|
use yii\behaviors\TimestampBehavior;
|
|
|
|
use yii\db\ActiveQuery;
|
2021-11-09 17:41:44 +03:00
|
|
|
use yii\db\ActiveRecord;
|
2021-10-22 17:02:28 +03:00
|
|
|
use yii\db\Expression;
|
|
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
use backend\modules\questionnaire\models\Question;
|
|
|
|
use backend\modules\questionnaire\models\UserResponse;
|
2021-10-25 13:41:12 +03:00
|
|
|
use \backend\modules\questionnaire\models\Answer;
|
2021-10-22 17:02:28 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the model class for table "user_questionnaire".
|
|
|
|
*
|
|
|
|
* @property int $id
|
|
|
|
* @property int $questionnaire_id
|
|
|
|
* @property int $user_id
|
|
|
|
* @property string $uuid
|
|
|
|
* @property string $created_at
|
|
|
|
* @property string $updated_at
|
|
|
|
* @property int $score
|
|
|
|
* @property int $status
|
2021-10-25 13:41:12 +03:00
|
|
|
* @property double $percent_correct_answers
|
2021-10-22 17:02:28 +03:00
|
|
|
*
|
|
|
|
* @property Questionnaire $questionnaire
|
|
|
|
* @property User $user
|
|
|
|
* @property UserResponse[] $userResponses
|
|
|
|
*/
|
2021-11-09 17:41:44 +03:00
|
|
|
class UserQuestionnaire extends ActiveRecord
|
2021-10-22 17:02:28 +03:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public static function tableName()
|
|
|
|
{
|
|
|
|
return 'user_questionnaire';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function behaviors()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'class' => TimestampBehavior::class,
|
|
|
|
'createdAtAttribute' => 'created_at',
|
|
|
|
'updatedAtAttribute' => 'updated_at',
|
|
|
|
'value' => new Expression('NOW()'),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[['questionnaire_id', 'user_id', 'status'], 'required'],
|
|
|
|
[['questionnaire_id', 'user_id', 'score', 'status'], 'integer'],
|
2021-10-25 13:41:12 +03:00
|
|
|
[['percent_correct_answers'], 'number'],
|
2021-10-22 17:02:28 +03:00
|
|
|
[['created_at', 'updated_at'], 'safe'],
|
|
|
|
[['uuid'], 'string', 'max' => 36],
|
|
|
|
[['uuid'], 'unique'],
|
|
|
|
[['questionnaire_id'], 'exist', 'skipOnError' => true, 'targetClass' => Questionnaire::className(), 'targetAttribute' => ['questionnaire_id' => 'id']],
|
|
|
|
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-11-09 17:41:44 +03:00
|
|
|
public function beforeSave($insert)
|
|
|
|
{
|
|
|
|
if (parent::beforeSave($insert)) {
|
|
|
|
if (empty($this->uuid))
|
|
|
|
{
|
|
|
|
$this->uuid = UUIDHelper::v4();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-10-22 17:02:28 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function attributeLabels()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => 'ID',
|
|
|
|
'questionnaire_id' => 'Анкета',
|
|
|
|
'user_id' => 'Пользователь',
|
|
|
|
'uuid' => 'UUID',
|
|
|
|
'score' => 'Балы',
|
|
|
|
'status' => 'Статус',
|
|
|
|
'created_at' => 'Дата создания',
|
|
|
|
'updated_at' => 'Дата обновления',
|
2021-10-25 13:41:12 +03:00
|
|
|
'percent_correct_answers' => 'Процент правильных ответов',
|
2021-10-22 17:02:28 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ActiveQuery
|
|
|
|
*/
|
|
|
|
public function getQuestionnaire(): ActiveQuery
|
|
|
|
{
|
|
|
|
return $this->hasOne(Questionnaire::className(), ['id' => 'questionnaire_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ActiveQuery
|
|
|
|
*/
|
|
|
|
public function getUser()
|
|
|
|
{
|
|
|
|
return $this->hasOne(User::className(), ['id' => 'user_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ActiveQuery
|
|
|
|
*/
|
|
|
|
public function getUserResponses(): ActiveQuery
|
|
|
|
{
|
2021-11-09 17:41:44 +03:00
|
|
|
return $this->hasMany(UserResponse::className(), ['user_questionnaire_uuid' => 'uuid']);
|
2021-10-22 17:02:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getQuestionnaireTitle()
|
|
|
|
{
|
|
|
|
return $this->getQuestionnaire()->one()->title;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUserName()
|
|
|
|
{
|
|
|
|
return $this->getUser()->one()->username;
|
|
|
|
}
|
|
|
|
|
2021-11-12 14:30:01 +03:00
|
|
|
/**
|
|
|
|
* @throws InvalidConfigException
|
|
|
|
*/
|
2021-11-09 17:41:44 +03:00
|
|
|
public function getQuestions()
|
2021-10-22 17:02:28 +03:00
|
|
|
{
|
|
|
|
return $this->hasMany(Question::className(), ['id' => 'question_id'])
|
2021-11-09 17:41:44 +03:00
|
|
|
->viaTable('user_response', ['user_questionnaire_uuid' => 'uuid']);
|
2021-10-22 17:02:28 +03:00
|
|
|
}
|
|
|
|
|
2021-11-12 14:30:01 +03:00
|
|
|
/**
|
|
|
|
* @throws InvalidConfigException
|
|
|
|
*/
|
2021-11-08 12:41:39 +03:00
|
|
|
public function numCorrectAnswersWithoutOpenQuestions()
|
2021-10-25 13:41:12 +03:00
|
|
|
{
|
|
|
|
return $this->hasMany(Answer::className(), ['question_id' => 'question_id'])
|
2021-11-09 17:41:44 +03:00
|
|
|
->viaTable('user_response', ['user_questionnaire_uuid' => 'uuid'])
|
2021-10-25 13:41:12 +03:00
|
|
|
->where(['answer_flag' => '1'])
|
|
|
|
->andWhere(['status' => '1'])
|
|
|
|
->count();
|
|
|
|
}
|
|
|
|
|
2021-11-12 14:30:01 +03:00
|
|
|
/**
|
|
|
|
* @throws InvalidConfigException
|
|
|
|
*/
|
2021-11-08 12:41:39 +03:00
|
|
|
public function numOpenQuestionsAnswers()
|
2021-10-25 13:41:12 +03:00
|
|
|
{
|
|
|
|
return $this->hasMany(Question::className(), ['id' => 'question_id'])
|
2021-11-09 17:41:44 +03:00
|
|
|
->viaTable('user_response', ['user_questionnaire_uuid' => 'uuid'])
|
2021-10-25 13:41:12 +03:00
|
|
|
->where(['question_type_id' => '1'])
|
|
|
|
->count();
|
|
|
|
}
|
|
|
|
|
2021-11-12 14:30:01 +03:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public static function getQuestionnaireId($uuid)
|
|
|
|
{
|
|
|
|
return ArrayHelper::getValue(self::find()->where(['uuid' => $uuid])->one(), 'id');
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function findActiveUserQuestionnaires($user_id): array
|
2021-10-28 10:46:59 +03:00
|
|
|
{
|
|
|
|
return self::find()->where(['user_id' => $user_id])
|
|
|
|
->andWhere(['status' => '1'])
|
|
|
|
->all();
|
|
|
|
}
|
2021-10-22 17:02:28 +03:00
|
|
|
}
|