2021-10-22 17:02:28 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace common\models;
|
|
|
|
|
|
|
|
use yii\behaviors\TimestampBehavior;
|
2021-11-08 12:41:39 +03:00
|
|
|
use yii\db\ActiveQuery;
|
|
|
|
use yii\db\ActiveRecord;
|
2021-10-22 17:02:28 +03:00
|
|
|
use yii\db\Expression;
|
|
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the model class for table "questionnaire".
|
|
|
|
*
|
|
|
|
* @property int $id
|
|
|
|
* @property int $category_id
|
|
|
|
* @property string $title
|
|
|
|
* @property int $status
|
|
|
|
* @property string $created_at
|
|
|
|
* @property string $updated_at
|
2021-11-02 17:29:15 +03:00
|
|
|
* @property string $time_limit
|
2021-10-22 17:02:28 +03:00
|
|
|
*
|
|
|
|
* @property Question[] $questions
|
|
|
|
* @property QuestionnaireCategory $category
|
|
|
|
* @property UserQuestionnaire[] $userQuestionnaires
|
|
|
|
*/
|
2021-11-08 12:41:39 +03:00
|
|
|
class Questionnaire extends ActiveRecord
|
2021-10-22 17:02:28 +03:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public static function tableName()
|
|
|
|
{
|
|
|
|
return 'questionnaire';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function behaviors()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'class' => TimestampBehavior::class,
|
|
|
|
'createdAtAttribute' => 'created_at',
|
|
|
|
'updatedAtAttribute' => 'updated_at',
|
|
|
|
'value' => new Expression('NOW()'),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
{
|
|
|
|
return [
|
2021-11-02 17:29:15 +03:00
|
|
|
[['category_id', 'status', 'title'], 'required'],
|
|
|
|
[['category_id', 'status'], 'integer'],
|
|
|
|
[['created_at', 'updated_at', 'time_limit'], 'safe'],
|
2021-10-22 17:02:28 +03:00
|
|
|
['title', 'unique'],
|
|
|
|
[['title'], 'string', 'max' => 255],
|
2021-11-09 17:41:44 +03:00
|
|
|
['status', 'default', 'value' => true],
|
2021-10-22 17:02:28 +03:00
|
|
|
[['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => QuestionnaireCategory::className(), 'targetAttribute' => ['category_id' => 'id']],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function attributeLabels()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => 'ID',
|
|
|
|
'category_id' => 'Категория',
|
|
|
|
'title' => 'Название анкеты',
|
|
|
|
'status' => 'Статус',
|
|
|
|
'created_at' => 'Created At',
|
|
|
|
'updated_at' => 'Updated At',
|
2021-11-02 17:29:15 +03:00
|
|
|
'time_limit' => 'Время на выполнение (HH:mm)',
|
2021-10-22 17:02:28 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-11-02 17:29:15 +03:00
|
|
|
public function beforeSave($insert): bool
|
|
|
|
{
|
|
|
|
if (parent::beforeSave($insert)) {
|
|
|
|
if (strtotime($this->time_limit, '0') === 0)
|
|
|
|
{
|
|
|
|
$this->time_limit = null;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 17:02:28 +03:00
|
|
|
/**
|
2021-11-08 12:41:39 +03:00
|
|
|
* @return ActiveQuery
|
2021-10-22 17:02:28 +03:00
|
|
|
*/
|
|
|
|
public function getQuestions()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Question::className(), ['questionnaire_id' => 'id']);
|
|
|
|
}
|
|
|
|
|
2021-10-28 10:46:59 +03:00
|
|
|
/**
|
2021-11-08 12:41:39 +03:00
|
|
|
* @return ActiveQuery
|
2021-10-28 10:46:59 +03:00
|
|
|
*/
|
|
|
|
public function getAnswers()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Answer::className(), ['question_id' => 'id'])
|
|
|
|
->viaTable('question', ['questionnaire_id' => 'id']);
|
|
|
|
}
|
|
|
|
|
2021-10-22 17:02:28 +03:00
|
|
|
/**
|
2021-11-08 12:41:39 +03:00
|
|
|
* @return ActiveQuery
|
2021-10-22 17:02:28 +03:00
|
|
|
*/
|
2021-11-08 12:41:39 +03:00
|
|
|
public function getCategory(): ActiveQuery
|
2021-10-22 17:02:28 +03:00
|
|
|
{
|
|
|
|
return $this->hasOne(QuestionnaireCategory::className(), ['id' => 'category_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-08 12:41:39 +03:00
|
|
|
* @return ActiveQuery
|
2021-10-22 17:02:28 +03:00
|
|
|
*/
|
|
|
|
public function getUserQuestionnaires()
|
|
|
|
{
|
|
|
|
return $this->hasMany(UserQuestionnaire::className(), ['questionnaire_id' => 'id']);
|
|
|
|
}
|
|
|
|
|
2021-11-12 14:30:01 +03:00
|
|
|
public static function questionnairesByCategoryArr($category_id): array
|
2021-10-22 17:02:28 +03:00
|
|
|
{
|
|
|
|
$categories = self::find()->where(['category_id' => $category_id, 'status' => '1'])->all();
|
2021-11-08 12:41:39 +03:00
|
|
|
return ArrayHelper::map($categories, 'id', 'title');
|
2021-10-22 17:02:28 +03:00
|
|
|
}
|
|
|
|
}
|