guild/common/models/UserResponse.php

148 lines
4.1 KiB
PHP
Raw Normal View History

2021-10-22 17:02:28 +03:00
<?php
namespace common\models;
use yii\base\InvalidConfigException;
2021-10-22 17:02:28 +03:00
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;
2021-11-08 12:41:39 +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\UserQuestionnaire;
/**
* This is the model class for table "user_response".
*
* @property int $id
* @property int $user_id
* @property int $question_id
* @property string $response_body
* @property string $created_at
* @property string $updated_at
* @property double $answer_flag
* @property string $user_questionnaire_uuid
2021-10-22 17:02:28 +03:00
*
* @property UserQuestionnaire $userQuestionnaire
* @property Question $question
* @property User $user
*/
2021-11-08 12:41:39 +03:00
class UserResponse extends ActiveRecord
2021-10-22 17:02:28 +03:00
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'user_response';
}
public function behaviors()
{
return [
[
'class' => TimestampBehavior::class,
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => new Expression('NOW()'),
],
];
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
2023-12-06 00:15:52 +03:00
[['user_id', 'question_id', 'answer_id'], 'integer'],
2021-10-22 17:02:28 +03:00
[['created_at', 'updated_at'], 'safe'],
[['answer_flag'], 'number'],
[['response_body'], 'string', 'max' => 255],
[['user_questionnaire_uuid'], 'string', 'max' => 36],
[['user_questionnaire_uuid'], 'exist', 'skipOnError' => true, 'targetClass' => UserQuestionnaire::className(), 'targetAttribute' => ['user_questionnaire_uuid' => 'uuid']],
2021-10-22 17:02:28 +03:00
[['question_id'], 'exist', 'skipOnError' => true, 'targetClass' => Question::className(), 'targetAttribute' => ['question_id' => 'id']],
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'user_id' => 'Пользователь',
'question_id' => 'Вопрос',
2023-12-06 00:15:52 +03:00
'answer_id' => 'Идентификатор ответа',
2021-10-22 17:02:28 +03:00
'response_body' => 'Ответ пользователя',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
'answer_flag' => 'Корректность',
'user_questionnaire_uuid' => 'UUID анкеты',
2021-10-22 17:02:28 +03:00
];
}
/**
* @return ActiveQuery
*/
public function getUserQuestionnaire()
{
return $this->hasOne(UserQuestionnaire::className(), ['uuid' => 'user_questionnaire_uuid']);
2021-10-22 17:02:28 +03:00
}
/**
* @return ActiveQuery
*/
public function getQuestion()
{
return $this->hasOne(Question::className(), ['id' => 'question_id']);
}
/**
* @return ActiveQuery
2023-12-06 00:15:52 +03:00
*/
public function getAnswer(): ActiveQuery
{
return $this->hasOne(Answer::class, ['id' => 'answer_id']);
}
/**
* @return ActiveQuery
2021-10-22 17:02:28 +03:00
*/
public function getUser()
{
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
/**
* @throws InvalidConfigException
*/
public function getQuestionnaire()
2021-10-22 17:02:28 +03:00
{
return $this->hasOne(Questionnaire::className(), ['id' => 'questionnaire_id'])
->viaTable('user_questionnaire', ['uuid' => 'user_questionnaire_uuid']);
2021-10-22 17:02:28 +03:00
}
/**
* @throws InvalidConfigException
*/
public function getQuestionType()
2021-10-22 17:02:28 +03:00
{
return $this->hasOne(QuestionType::class, ['id' => 'question_type_id'])
->viaTable('question', ['id' => 'question_id']);
2021-10-22 17:02:28 +03:00
}
public function getCorrectAnswers()
2021-10-22 17:02:28 +03:00
{
return $this->hasMany(Answer::class, ['question_id' => 'question_id'])
->where(['answer_flag' => '1'])->all();
2021-10-22 17:02:28 +03:00
}
public function getQuestionTypeValue()
{
$qType = $this->getQuestion()->one();
2021-11-08 12:41:39 +03:00
return ArrayHelper::getValue($qType, 'question_type_id');
2021-10-22 17:02:28 +03:00
}
}