changing foreign key in user_response from id to uuid, completed add greedy loading, some refactoring
This commit is contained in:
@ -21,12 +21,6 @@ use yii\db\Expression;
|
||||
*/
|
||||
class Answer extends \yii\db\ActiveRecord
|
||||
{
|
||||
const STATUS_PASSIVE = 0;
|
||||
const STATUS_ACTIVE = 1;
|
||||
|
||||
const FLAG_TRUE = 1;
|
||||
const FLAG_FALSE = 0;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
@ -27,9 +27,6 @@ use yii\db\Expression;
|
||||
*/
|
||||
class Question extends \yii\db\ActiveRecord
|
||||
{
|
||||
const STATUS_PASSIVE = 0;
|
||||
const STATUS_ACTIVE = 1;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
@ -25,9 +25,6 @@ use yii\helpers\ArrayHelper;
|
||||
*/
|
||||
class Questionnaire extends ActiveRecord
|
||||
{
|
||||
const STATUS_PASSIVE = 0;
|
||||
const STATUS_ACTIVE = 1;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@ -59,7 +56,7 @@ class Questionnaire extends ActiveRecord
|
||||
[['created_at', 'updated_at', 'time_limit'], 'safe'],
|
||||
['title', 'unique'],
|
||||
[['title'], 'string', 'max' => 255],
|
||||
['status', 'default', 'value' => self::STATUS_ACTIVE],
|
||||
['status', 'default', 'value' => true],
|
||||
[['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => QuestionnaireCategory::className(), 'targetAttribute' => ['category_id' => 'id']],
|
||||
];
|
||||
}
|
||||
@ -129,7 +126,6 @@ class Questionnaire extends ActiveRecord
|
||||
public static function questionnairesOfCategoryArr($category_id): array
|
||||
{
|
||||
$categories = self::find()->where(['category_id' => $category_id, 'status' => '1'])->all();
|
||||
|
||||
return ArrayHelper::map($categories, 'id', 'title');
|
||||
}
|
||||
}
|
||||
|
@ -20,9 +20,6 @@ use yii\helpers\ArrayHelper;
|
||||
*/
|
||||
class QuestionnaireCategory extends \yii\db\ActiveRecord
|
||||
{
|
||||
const STATUS_PASSIVE = 0;
|
||||
const STATUS_ACTIVE = 1;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@ -53,7 +50,7 @@ class QuestionnaireCategory extends \yii\db\ActiveRecord
|
||||
[['status'], 'integer'],
|
||||
[['created_at', 'updated_at'], 'safe'],
|
||||
[['title'], 'string', 'max' => 255],
|
||||
['status', 'default', 'value' => self::STATUS_ACTIVE],
|
||||
['status', 'default', 'value' => true],
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -2,9 +2,10 @@
|
||||
|
||||
namespace common\models;
|
||||
|
||||
//use Ramsey\Uuid\Uuid;
|
||||
use common\helpers\UUIDHelper;
|
||||
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\Question;
|
||||
@ -28,11 +29,8 @@ use \backend\modules\questionnaire\models\Answer;
|
||||
* @property User $user
|
||||
* @property UserResponse[] $userResponses
|
||||
*/
|
||||
class UserQuestionnaire extends \yii\db\ActiveRecord
|
||||
class UserQuestionnaire extends ActiveRecord
|
||||
{
|
||||
const STATUS_PASSIVE = 0;
|
||||
const STATUS_ACTIVE = 1;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@ -71,15 +69,18 @@ class UserQuestionnaire extends \yii\db\ActiveRecord
|
||||
];
|
||||
}
|
||||
|
||||
// public function beforeSave($insert)
|
||||
// {
|
||||
// if (parent::beforeSave($insert)) {
|
||||
// $this->uuid = Uuid::uuid4()->toString();
|
||||
// return true;
|
||||
// } else {
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
public function beforeSave($insert)
|
||||
{
|
||||
if (parent::beforeSave($insert)) {
|
||||
if (empty($this->uuid))
|
||||
{
|
||||
$this->uuid = UUIDHelper::v4();
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
@ -120,7 +121,7 @@ class UserQuestionnaire extends \yii\db\ActiveRecord
|
||||
*/
|
||||
public function getUserResponses(): ActiveQuery
|
||||
{
|
||||
return $this->hasMany(UserResponse::className(), ['user_questionnaire_id' => 'id']);
|
||||
return $this->hasMany(UserResponse::className(), ['user_questionnaire_uuid' => 'uuid']);
|
||||
}
|
||||
|
||||
public function getQuestionnaireTitle()
|
||||
@ -147,16 +148,16 @@ class UserQuestionnaire extends \yii\db\ActiveRecord
|
||||
return $formatQuestionnaireArr;
|
||||
}
|
||||
|
||||
public function getQuestions(): ActiveQuery
|
||||
public function getQuestions()
|
||||
{
|
||||
return $this->hasMany(Question::className(), ['id' => 'question_id'])
|
||||
->viaTable('user_response', ['user_questionnaire_id' => 'id']);
|
||||
->viaTable('user_response', ['user_questionnaire_uuid' => 'uuid']);
|
||||
}
|
||||
|
||||
public function numCorrectAnswersWithoutOpenQuestions()
|
||||
{
|
||||
return $this->hasMany(Answer::className(), ['question_id' => 'question_id'])
|
||||
->viaTable('user_response', ['user_questionnaire_id' => 'id'])
|
||||
->viaTable('user_response', ['user_questionnaire_uuid' => 'uuid'])
|
||||
->where(['answer_flag' => '1'])
|
||||
->andWhere(['status' => '1'])
|
||||
->count();
|
||||
@ -165,7 +166,7 @@ class UserQuestionnaire extends \yii\db\ActiveRecord
|
||||
public function numOpenQuestionsAnswers()
|
||||
{
|
||||
return $this->hasMany(Question::className(), ['id' => 'question_id'])
|
||||
->viaTable('user_response', ['user_questionnaire_id' => 'id'])
|
||||
->viaTable('user_response', ['user_questionnaire_uuid' => 'uuid'])
|
||||
->where(['question_type_id' => '1'])
|
||||
->count();
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\db\ActiveQuery;
|
||||
use yii\db\ActiveRecord;
|
||||
@ -18,8 +19,8 @@ use \backend\modules\questionnaire\models\UserQuestionnaire;
|
||||
* @property string $response_body
|
||||
* @property string $created_at
|
||||
* @property string $updated_at
|
||||
* @property int $user_questionnaire_id
|
||||
* @property double $answer_flag
|
||||
* @property string $user_questionnaire_uuid
|
||||
*
|
||||
* @property UserQuestionnaire $userQuestionnaire
|
||||
* @property Question $question
|
||||
@ -53,11 +54,12 @@ class UserResponse extends ActiveRecord
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['user_id', 'question_id', 'user_questionnaire_id'], 'integer'],
|
||||
[['user_id', 'question_id'], 'integer'],
|
||||
[['created_at', 'updated_at'], 'safe'],
|
||||
[['answer_flag'], 'number'],
|
||||
[['response_body'], 'string', 'max' => 255],
|
||||
[['user_questionnaire_id'], 'exist', 'skipOnError' => true, 'targetClass' => UserQuestionnaire::className(), 'targetAttribute' => ['user_questionnaire_id' => 'id']],
|
||||
[['user_questionnaire_uuid'], 'string', 'max' => 36],
|
||||
[['user_questionnaire_uuid'], 'exist', 'skipOnError' => true, 'targetClass' => UserQuestionnaire::className(), 'targetAttribute' => ['user_questionnaire_uuid' => 'uuid']],
|
||||
[['question_id'], 'exist', 'skipOnError' => true, 'targetClass' => Question::className(), 'targetAttribute' => ['question_id' => 'id']],
|
||||
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
|
||||
];
|
||||
@ -76,7 +78,7 @@ class UserResponse extends ActiveRecord
|
||||
'created_at' => 'Created At',
|
||||
'updated_at' => 'Updated At',
|
||||
'answer_flag' => 'Корректность',
|
||||
'user_questionnaire_id' => 'Анкеты',
|
||||
'user_questionnaire_uuid' => 'UUID анкеты',
|
||||
];
|
||||
}
|
||||
|
||||
@ -85,7 +87,7 @@ class UserResponse extends ActiveRecord
|
||||
*/
|
||||
public function getUserQuestionnaire()
|
||||
{
|
||||
return $this->hasOne(UserQuestionnaire::className(), ['id' => 'user_questionnaire_id']);
|
||||
return $this->hasOne(UserQuestionnaire::className(), ['uuid' => 'user_questionnaire_uuid']);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -104,25 +106,30 @@ class UserResponse extends ActiveRecord
|
||||
return $this->hasOne(User::className(), ['id' => 'user_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidConfigException
|
||||
*/
|
||||
public function getQuestionnaire()
|
||||
{
|
||||
return $this->hasOne(Questionnaire::className(), ['id' => 'questionnaire_id'])
|
||||
->viaTable('user_questionnaire', ['uuid' => 'user_questionnaire_uuid']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidConfigException
|
||||
*/
|
||||
public function getQuestionType()
|
||||
{
|
||||
return $this->hasOne(QuestionType::class, ['id' => 'question_type_id'])
|
||||
->viaTable('question', ['id' => 'question_id']);
|
||||
}
|
||||
|
||||
public function getCorrectAnswers()
|
||||
{
|
||||
return $this->hasMany(Answer::class, ['question_id' => 'question_id'])
|
||||
->where(['answer_flag' => '1'])->all();
|
||||
}
|
||||
|
||||
public function getQuestionnaireTitle()
|
||||
{
|
||||
$tmp = $this->hasOne(Questionnaire::className(), ['id' => 'questionnaire_id'])
|
||||
->viaTable('user_questionnaire', ['id' => 'user_questionnaire_id'])->one();
|
||||
return ArrayHelper::getValue($tmp, 'title');
|
||||
}
|
||||
|
||||
public function getQuestionType()
|
||||
{
|
||||
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();
|
||||
|
Reference in New Issue
Block a user