adding forgotten files
This commit is contained in:
128
common/models/Answer.php
Normal file
128
common/models/Answer.php
Normal file
@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use Yii;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
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
|
||||
{
|
||||
const STATUS_PASSIVE = 0;
|
||||
const STATUS_ACTIVE = 1;
|
||||
|
||||
const FLAG_TRUE = 1;
|
||||
const FLAG_FALSE = 0;
|
||||
|
||||
/**
|
||||
* {@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',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getQuestion()
|
||||
{
|
||||
return $this->hasOne(Question::className(), ['id' => 'question_id']);
|
||||
}
|
||||
|
||||
|
||||
public function getQuestionBody()
|
||||
{
|
||||
return $this->getQuestion()->one()->question_body;
|
||||
}
|
||||
|
||||
public function getStatuses()
|
||||
{
|
||||
return [
|
||||
self::STATUS_ACTIVE => 'Активен',
|
||||
self::STATUS_PASSIVE => 'Не используется'
|
||||
];
|
||||
}
|
||||
|
||||
public function getStatusText()
|
||||
{
|
||||
return $this->statuses[$this->status];
|
||||
}
|
||||
|
||||
public function getFlags()
|
||||
{
|
||||
return [
|
||||
self::FLAG_TRUE => 'Правильный',
|
||||
self::FLAG_FALSE => 'Ошибочный',
|
||||
];
|
||||
}
|
||||
|
||||
public function getFlagText()
|
||||
{
|
||||
return $this->flags[$this->status];
|
||||
}
|
||||
|
||||
static function getCurrentAnswersNum($question_id)
|
||||
{
|
||||
return Answer::find()
|
||||
->where('question_id=:question_id', [':question_id' => $question_id])
|
||||
->andWhere('answer_flag=1')
|
||||
->andWhere('status=1')
|
||||
->count();
|
||||
}
|
||||
}
|
155
common/models/Question.php
Normal file
155
common/models/Question.php
Normal file
@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use Yii;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\db\Expression;
|
||||
|
||||
/**
|
||||
* This is the model class for table "question".
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $question_type_id
|
||||
* @property int $questionnaire_id
|
||||
* @property string $question_body
|
||||
* @property int $question_priority
|
||||
* @property int $next_question
|
||||
* @property int $status
|
||||
* @property int $score
|
||||
* @property int $time_limit
|
||||
* @property string $created_at
|
||||
* @property string $updated_at
|
||||
*
|
||||
* @property Answer[] $answers
|
||||
* @property Questionnaire $questionnaire
|
||||
* @property QuestionType $questionType
|
||||
* @property UserResponse[] $userResponses
|
||||
*/
|
||||
class Question extends \yii\db\ActiveRecord
|
||||
{
|
||||
const STATUS_PASSIVE = 0;
|
||||
const STATUS_ACTIVE = 1;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function tableName()
|
||||
{
|
||||
return 'question';
|
||||
}
|
||||
|
||||
public function behaviors()
|
||||
{
|
||||
return [
|
||||
[
|
||||
'class' => TimestampBehavior::class,
|
||||
'createdAtAttribute' => 'created_at',
|
||||
'updatedAtAttribute' => 'updated_at',
|
||||
'value' => new Expression('NOW()'),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['status', 'question_type_id', 'questionnaire_id', 'question_body', 'score'], 'required'],
|
||||
[['question_type_id', 'questionnaire_id', 'question_priority', 'next_question', 'status', 'score', 'time_limit'], 'integer'],
|
||||
[['created_at', 'updated_at'], 'safe'],
|
||||
[['question_body'], 'string', 'max' => 255],
|
||||
[['questionnaire_id'], 'exist', 'skipOnError' => true, 'targetClass' => Questionnaire::className(), 'targetAttribute' => ['questionnaire_id' => 'id']],
|
||||
[['question_type_id'], 'exist', 'skipOnError' => true, 'targetClass' => QuestionType::className(), 'targetAttribute' => ['question_type_id' => 'id']],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'id' => 'ID',
|
||||
'question_type_id' => 'Тип вопроса',
|
||||
'questionnaire_id' => 'Анкета',
|
||||
'question_body' => 'Вопрос',
|
||||
'question_priority' => 'Приоритет вопроса',
|
||||
'next_question' => 'Следующий вопрос',
|
||||
'status' => 'Статус',
|
||||
'created_at' => 'Created At',
|
||||
'updated_at' => 'Updated At',
|
||||
'time_limit' => 'Время на ответ',
|
||||
'score' => 'Балы за вопрос',
|
||||
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getAnswers()
|
||||
{
|
||||
return $this->hasMany(Answer::className(), ['question_id' => 'id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getQuestionnaire()
|
||||
{
|
||||
return $this->hasOne(Questionnaire::className(), ['id' => 'questionnaire_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getQuestionType()
|
||||
{
|
||||
return $this->hasOne(QuestionType::className(), ['id' => 'question_type_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getUserResponses()
|
||||
{
|
||||
return $this->hasMany(UserResponse::className(), ['question_id' => 'id']);
|
||||
}
|
||||
|
||||
public function getStatuses()
|
||||
{
|
||||
return [
|
||||
self::STATUS_ACTIVE => 'Активен',
|
||||
self::STATUS_PASSIVE => 'Не используется'
|
||||
];
|
||||
}
|
||||
|
||||
public function getStatusText()
|
||||
{
|
||||
return $this->statuses[$this->status];
|
||||
}
|
||||
|
||||
public function getQuestionnaireTitle()
|
||||
{
|
||||
return $this->getQuestionnaire()->one()->title;
|
||||
}
|
||||
|
||||
public function getQuestionTitle()
|
||||
{
|
||||
return $this->getQuestionType()->one()->question_type;
|
||||
}
|
||||
|
||||
public function getLimitTime()
|
||||
{
|
||||
if ($this->time_limit === null)
|
||||
{
|
||||
return 'Не ограничено';
|
||||
}
|
||||
|
||||
return date("i:s", mktime(null, null, $this->time_limit));
|
||||
}
|
||||
}
|
66
common/models/QuestionType.php
Normal file
66
common/models/QuestionType.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use Yii;
|
||||
use yii\behaviors\SluggableBehavior;
|
||||
|
||||
/**
|
||||
* This is the model class for table "question_type".
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $question_type
|
||||
* @property string $slug
|
||||
*
|
||||
* @property Question[] $questions
|
||||
*/
|
||||
class QuestionType extends \yii\db\ActiveRecord
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function tableName()
|
||||
{
|
||||
return 'question_type';
|
||||
}
|
||||
|
||||
public function behaviors()
|
||||
{
|
||||
return [
|
||||
[
|
||||
'class' => SluggableBehavior::class,
|
||||
'attribute' => 'question_type',
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['question_type'], 'string', 'max' => 255],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'id' => 'ID',
|
||||
'question_type' => 'Тип вопроса',
|
||||
'slug' => 'Slug',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getQuestions()
|
||||
{
|
||||
return $this->hasMany(Question::className(), ['question_type_id' => 'id']);
|
||||
}
|
||||
}
|
145
common/models/Questionnaire.php
Normal file
145
common/models/Questionnaire.php
Normal file
@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use Yii;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
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
|
||||
* @property int $time_limit
|
||||
*
|
||||
* @property Question[] $questions
|
||||
* @property QuestionnaireCategory $category
|
||||
* @property UserQuestionnaire[] $userQuestionnaires
|
||||
*/
|
||||
class Questionnaire extends \yii\db\ActiveRecord
|
||||
{
|
||||
const STATUS_PASSIVE = 0;
|
||||
const STATUS_ACTIVE = 1;
|
||||
|
||||
/**
|
||||
* {@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 [
|
||||
[['category_id', 'status', 'time_limit'], 'integer'],
|
||||
[['created_at', 'updated_at'], 'safe'],
|
||||
['title', 'unique'],
|
||||
[['title'], 'string', 'max' => 255],
|
||||
['status', 'default', 'value' => self::STATUS_ACTIVE],
|
||||
[['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',
|
||||
'time_limit' => 'Время на выполнение',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getQuestions()
|
||||
{
|
||||
return $this->hasMany(Question::className(), ['questionnaire_id' => 'id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getCategory()
|
||||
{
|
||||
return $this->hasOne(QuestionnaireCategory::className(), ['id' => 'category_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getUserQuestionnaires()
|
||||
{
|
||||
return $this->hasMany(UserQuestionnaire::className(), ['questionnaire_id' => 'id']);
|
||||
}
|
||||
|
||||
public function getStatuses()
|
||||
{
|
||||
return [
|
||||
self::STATUS_ACTIVE => 'Активна',
|
||||
self::STATUS_PASSIVE => 'Не используется'
|
||||
];
|
||||
}
|
||||
|
||||
public function getStatusText()
|
||||
{
|
||||
return $this->statuses[$this->status];
|
||||
}
|
||||
|
||||
public function getCategoryTitle()
|
||||
{
|
||||
return $this->getCategory()->one()->title;
|
||||
}
|
||||
|
||||
public function getLimitTime()
|
||||
{
|
||||
if ($this->time_limit === null)
|
||||
{
|
||||
return 'Не ограничено';
|
||||
}
|
||||
|
||||
return date("H:i:s", mktime(null, null, $this->time_limit));
|
||||
}
|
||||
|
||||
public static function getQuestionnaireByCategory($category_id)
|
||||
{
|
||||
$categories = self::find()->where(['category_id' => $category_id, 'status' => '1'])->all();
|
||||
$catArr = \yii\helpers\ArrayHelper::map($categories, 'id', 'title');
|
||||
|
||||
$formattedCatArr = array();
|
||||
foreach ($catArr as $key => $value){
|
||||
$formattedCatArr[] = array('id' => $key, 'name' => $value);
|
||||
}
|
||||
|
||||
return $formattedCatArr;
|
||||
}
|
||||
}
|
103
common/models/QuestionnaireCategory.php
Normal file
103
common/models/QuestionnaireCategory.php
Normal file
@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use Yii;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\db\Expression;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
/**
|
||||
* This is the model class for table "questionnaire_category".
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $title
|
||||
* @property int $status
|
||||
* @property string $created_at
|
||||
* @property string $updated_at
|
||||
*
|
||||
* @property Questionnaire[] $questionnaires
|
||||
*/
|
||||
class QuestionnaireCategory extends \yii\db\ActiveRecord
|
||||
{
|
||||
const STATUS_PASSIVE = 0;
|
||||
const STATUS_ACTIVE = 1;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function tableName()
|
||||
{
|
||||
return 'questionnaire_category';
|
||||
}
|
||||
|
||||
public function behaviors()
|
||||
{
|
||||
return [
|
||||
[
|
||||
'class' => TimestampBehavior::class,
|
||||
'createdAtAttribute' => 'created_at',
|
||||
'updatedAtAttribute' => 'updated_at',
|
||||
'value' => new Expression('NOW()'),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['title', 'status'], 'required'],
|
||||
[['status'], 'integer'],
|
||||
[['created_at', 'updated_at'], 'safe'],
|
||||
[['title'], 'string', 'max' => 255],
|
||||
['status', 'default', 'value' => self::STATUS_ACTIVE],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'id' => 'ID',
|
||||
'title' => 'Название категории',
|
||||
'status' => 'Статус',
|
||||
'created_at' => 'Created At',
|
||||
'updated_at' => 'Updated At',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getQuestionnaires()
|
||||
{
|
||||
return $this->hasMany(Questionnaire::className(), ['category_id' => 'id']);
|
||||
}
|
||||
|
||||
public function getStatuses()
|
||||
{
|
||||
return [
|
||||
self::STATUS_PASSIVE => 'Не используется',
|
||||
self::STATUS_ACTIVE => 'Активна'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string status text label
|
||||
*/
|
||||
public function getStatusText()
|
||||
{
|
||||
return $this->statuses[$this->status];
|
||||
}
|
||||
|
||||
public function getIdTitlesArr()
|
||||
{
|
||||
$categories = self::find()->select(['id', 'title'])->where(['status' => '1'])->all();
|
||||
return ArrayHelper::map($categories,'id','title');
|
||||
}
|
||||
}
|
243
common/models/UserQuestionnaire.php
Normal file
243
common/models/UserQuestionnaire.php
Normal file
@ -0,0 +1,243 @@
|
||||
<?php
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Yii;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\db\ActiveQuery;
|
||||
use yii\db\Expression;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use backend\modules\questionnaire\models\Question;
|
||||
use backend\modules\questionnaire\models\UserResponse;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @property Questionnaire $questionnaire
|
||||
* @property User $user
|
||||
* @property UserResponse[] $userResponses
|
||||
*/
|
||||
class UserQuestionnaire extends \yii\db\ActiveRecord
|
||||
{
|
||||
const STATUS_PASSIVE = 0;
|
||||
const STATUS_ACTIVE = 1;
|
||||
|
||||
/**
|
||||
* {@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'],
|
||||
[['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']],
|
||||
];
|
||||
}
|
||||
|
||||
public function beforeSave($insert)
|
||||
{
|
||||
if (parent::beforeSave($insert)) {
|
||||
$this->uuid = Uuid::uuid4()->toString();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'id' => 'ID',
|
||||
'questionnaire_id' => 'Анкета',
|
||||
'user_id' => 'Пользователь',
|
||||
'uuid' => 'UUID',
|
||||
'score' => 'Балы',
|
||||
'status' => 'Статус',
|
||||
'created_at' => 'Дата создания',
|
||||
'updated_at' => 'Дата обновления',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
{
|
||||
return $this->hasMany(UserResponse::className(), ['user_questionnaire_id' => 'id']);
|
||||
}
|
||||
|
||||
public function getQuestionnaireTitle()
|
||||
{
|
||||
return $this->getQuestionnaire()->one()->title;
|
||||
}
|
||||
|
||||
public function getUserName()
|
||||
{
|
||||
return $this->getUser()->one()->username;
|
||||
}
|
||||
|
||||
public function getCategoryId(): string
|
||||
{
|
||||
return $this->created_at;
|
||||
}
|
||||
|
||||
public static function getQuestionnaireByUser($id): array
|
||||
{
|
||||
$questionnaire = ArrayHelper::map(self::find()->where(['user_id' => $id])
|
||||
->with('questionnaire')->asArray()->all(),'id','questionnaire.title');
|
||||
|
||||
|
||||
$formatQuestionnaireArr = array();
|
||||
foreach ($questionnaire as $key => $value){
|
||||
$formatQuestionnaireArr[] = array('id' => $key, 'name' => $value);
|
||||
}
|
||||
|
||||
return $formatQuestionnaireArr;
|
||||
}
|
||||
|
||||
public function getStatuses(): array
|
||||
{
|
||||
return [
|
||||
self::STATUS_ACTIVE => 'Активен',
|
||||
self::STATUS_PASSIVE => 'Не используется'
|
||||
];
|
||||
}
|
||||
|
||||
public function getStatusText()
|
||||
{
|
||||
return $this->statuses[$this->status];
|
||||
}
|
||||
|
||||
public function checkAnswerFlagsForNull(): bool
|
||||
{
|
||||
$responses = $this->getUserResponses()->AsArray()->all();
|
||||
foreach ($responses as $response)
|
||||
{
|
||||
if (ArrayHelper::isIn(null, $response))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getQuestions(): ActiveQuery
|
||||
{
|
||||
return $this->hasMany(Question::className(), ['id' => 'question_id'])
|
||||
->viaTable('user_response', ['user_questionnaire_id' => 'id']);
|
||||
}
|
||||
|
||||
public function getScore()
|
||||
{
|
||||
$responses_questions = $this->hasMany(UserResponse::className(), ['user_questionnaire_id' => 'id'])
|
||||
->joinWith('question')->all();
|
||||
|
||||
$calc_score = $this->calculateScore($responses_questions);
|
||||
$this->score = $calc_score;
|
||||
$this->save();
|
||||
}
|
||||
|
||||
protected function calculateScore($responses_questions)
|
||||
{
|
||||
$score = null;
|
||||
foreach ($responses_questions as $response_question)
|
||||
{
|
||||
if($this->isCorrect($response_question['answer_flag']))
|
||||
{
|
||||
switch ($response_question['question']['question_type_id'])
|
||||
{
|
||||
case '1': // open question
|
||||
$score += $response_question['answer_flag'] * $response_question['question']['score'];
|
||||
break;
|
||||
case '2': // one answer
|
||||
$score += $response_question['question']['score'];
|
||||
break;
|
||||
case '3': // multi answer
|
||||
$score += $response_question['question']['score'] / $this->correctAnswersNum($response_question['question']['id']);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if($score === null) {
|
||||
return $score;
|
||||
}
|
||||
else {
|
||||
return round($score);
|
||||
}
|
||||
}
|
||||
|
||||
protected function correctAnswersNum($question_id)
|
||||
{
|
||||
return Answer::getCurrentAnswersNum($question_id);
|
||||
}
|
||||
|
||||
protected function isCorrect($answer_flag): bool
|
||||
{
|
||||
if ($answer_flag > 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function rateResponses()
|
||||
{
|
||||
$responses = $this->getUserResponses()->all();
|
||||
|
||||
foreach ($responses as $response)
|
||||
{
|
||||
$response->rateResponse();
|
||||
}
|
||||
}
|
||||
}
|
172
common/models/UserResponse.php
Normal file
172
common/models/UserResponse.php
Normal file
@ -0,0 +1,172 @@
|
||||
<?php
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use Yii;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\db\ActiveQuery;
|
||||
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 int $user_questionnaire_id
|
||||
* @property double $answer_flag
|
||||
*
|
||||
* @property UserQuestionnaire $userQuestionnaire
|
||||
* @property Question $question
|
||||
* @property User $user
|
||||
*/
|
||||
class UserResponse extends \yii\db\ActiveRecord
|
||||
{
|
||||
/**
|
||||
* {@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 [
|
||||
[['user_id', 'question_id', 'user_questionnaire_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']],
|
||||
[['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' => 'Вопрос',
|
||||
'response_body' => 'Ответ пользователя',
|
||||
'created_at' => 'Created At',
|
||||
'updated_at' => 'Updated At',
|
||||
'answer_flag' => 'Корректность',
|
||||
'user_questionnaire_id' => 'Анкеты',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getUserQuestionnaire()
|
||||
{
|
||||
return $this->hasOne(UserQuestionnaire::className(), ['id' => 'user_questionnaire_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getQuestion()
|
||||
{
|
||||
return $this->hasOne(Question::className(), ['id' => 'question_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function getUser()
|
||||
{
|
||||
return $this->hasOne(User::className(), ['id' => 'user_id']);
|
||||
}
|
||||
|
||||
public function getUserName()
|
||||
{
|
||||
return $this->getUser()->one()->username;
|
||||
}
|
||||
|
||||
public function getQuestionBody()
|
||||
{
|
||||
return $this->getQuestion()->one()->question_body;
|
||||
}
|
||||
|
||||
private 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();
|
||||
|
||||
$value = ArrayHelper::getValue($tmp, 'title');
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
||||
public function getQuestionType()
|
||||
{
|
||||
$qType = $this->hasOne(QuestionType::class, ['id' => 'question_type_id'])
|
||||
->viaTable('question', ['id' => 'question_id'])->one();
|
||||
|
||||
$value = ArrayHelper::getValue($qType, 'question_type');
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function getQuestionTypeValue()
|
||||
{
|
||||
$qType = $this->getQuestion()->one();
|
||||
|
||||
$value = ArrayHelper::getValue($qType, 'question_type_id');
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function rateResponse()
|
||||
{
|
||||
if ($this->answer_flag === null && $this->getQuestionTypeValue() != 1) // not open question
|
||||
{
|
||||
$correct_answers = $this->getCorrectAnswers();
|
||||
|
||||
foreach ($correct_answers as $correct_answerr)
|
||||
{
|
||||
if ($this->response_body === $correct_answerr['answer_body'])
|
||||
{
|
||||
$this->answer_flag = 1;
|
||||
$this->save();
|
||||
return;
|
||||
}
|
||||
}
|
||||
$this->answer_flag = 0;
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user