2021-10-22 17:02:28 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace common\models;
|
|
|
|
|
2021-11-12 14:30:01 +03:00
|
|
|
use yii\base\InvalidConfigException;
|
2021-10-22 17:02:28 +03:00
|
|
|
use yii\behaviors\TimestampBehavior;
|
2021-10-29 17:06:09 +03:00
|
|
|
use yii\db\ActiveQuery;
|
2021-10-22 17:02:28 +03:00
|
|
|
use yii\db\Expression;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the model class for table "answer".
|
|
|
|
*
|
|
|
|
* @property int $id
|
|
|
|
* @property int $question_id
|
|
|
|
* @property string $answer_body
|
|
|
|
* @property int $answer_flag
|
|
|
|
* @property int $status
|
|
|
|
* @property string $created_at
|
|
|
|
* @property string $updated_at
|
|
|
|
*
|
|
|
|
* @property Question $question
|
|
|
|
*/
|
|
|
|
class Answer extends \yii\db\ActiveRecord
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public static function tableName()
|
|
|
|
{
|
|
|
|
return 'answer';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function behaviors()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'class' => TimestampBehavior::class,
|
|
|
|
'createdAtAttribute' => 'created_at',
|
|
|
|
'updatedAtAttribute' => 'updated_at',
|
|
|
|
'value' => new Expression('NOW()'),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[['question_id', 'answer_flag', 'status'], 'integer'],
|
|
|
|
[['answer_body', 'question_id', 'answer_flag', 'status'], 'required'],
|
|
|
|
[['created_at', 'updated_at'], 'safe'],
|
|
|
|
[['answer_body'], 'string', 'max' => 255],
|
|
|
|
[['question_id'], 'exist', 'skipOnError' => true, 'targetClass' => Question::className(), 'targetAttribute' => ['question_id' => 'id']],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function attributeLabels()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => 'ID',
|
|
|
|
'question_id' => 'Вопрос',
|
|
|
|
'answer_body' => 'Ответ',
|
|
|
|
'answer_flag' => 'Флаг ответа',
|
|
|
|
'status' => 'Статус',
|
|
|
|
'created_at' => 'Created At',
|
|
|
|
'updated_at' => 'Updated At',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-29 17:06:09 +03:00
|
|
|
* @return ActiveQuery
|
2021-10-22 17:02:28 +03:00
|
|
|
*/
|
2021-10-29 17:06:09 +03:00
|
|
|
public function getQuestion(): ActiveQuery
|
2021-10-22 17:02:28 +03:00
|
|
|
{
|
|
|
|
return $this->hasOne(Question::className(), ['id' => 'question_id']);
|
|
|
|
}
|
|
|
|
|
2021-11-12 14:30:01 +03:00
|
|
|
/**
|
|
|
|
* @throws InvalidConfigException
|
|
|
|
*/
|
|
|
|
public function getQuestionnaire(): ActiveQuery
|
|
|
|
{
|
|
|
|
return $this->hasOne(Questionnaire::className(), ['id' => 'questionnaire_id'])
|
|
|
|
->viaTable('question', ['id' => 'question_id']);
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:41:39 +03:00
|
|
|
static function numCorrectAnswers($question_id)
|
2021-10-22 17:02:28 +03:00
|
|
|
{
|
|
|
|
return Answer::find()
|
|
|
|
->where('question_id=:question_id', [':question_id' => $question_id])
|
|
|
|
->andWhere('answer_flag=1')
|
|
|
|
->andWhere('status=1')
|
|
|
|
->count();
|
|
|
|
}
|
2021-10-28 10:46:59 +03:00
|
|
|
|
2021-11-08 12:41:39 +03:00
|
|
|
public static function activeAnswers($question_id): array
|
2021-10-28 10:46:59 +03:00
|
|
|
{
|
|
|
|
return self::find()->where(['question_id' => $question_id])
|
|
|
|
->andWhere(['status' => '1'])
|
|
|
|
->AsArray()
|
|
|
|
->all();
|
|
|
|
}
|
2021-10-22 17:02:28 +03:00
|
|
|
}
|