add column description to questionnaire table

This commit is contained in:
iIronside 2023-10-26 11:37:04 +03:00
parent 93f51f39c6
commit 700f79eb5c
9 changed files with 66 additions and 27 deletions

View File

@ -46,6 +46,8 @@ use yii\widgets\ActiveForm;
] ]
]) ?> ]) ?>
<?= $form->field($model, 'description')->textInput(['maxlength' => true]) ?>
<div class="form-group"> <div class="form-group">
<?= Html::submitButton('Создать', ['class' => 'btn btn-success']) ?> <?= Html::submitButton('Создать', ['class' => 'btn btn-success']) ?>
</div> </div>

View File

@ -49,6 +49,7 @@ $this->params['breadcrumbs'][] = $this->title;
return TimeHelper::limitTime($model->time_limit); return TimeHelper::limitTime($model->time_limit);
} }
], ],
'description',
['class' => 'yii\grid\ActionColumn'], ['class' => 'yii\grid\ActionColumn'],
], ],
]); ?> ]); ?>

View File

@ -54,6 +54,7 @@ YiiAsset::register($this);
'format' => 'raw', 'format' => 'raw',
'value' => TimeHelper::limitTime($model->time_limit), 'value' => TimeHelper::limitTime($model->time_limit),
], ],
'description',
], ],
]) ?> ]) ?>

View File

@ -15,6 +15,7 @@ use yii\helpers\ArrayHelper;
* @property int $category_id * @property int $category_id
* @property string $title * @property string $title
* @property int $status * @property int $status
* @property string $description
* @property string $created_at * @property string $created_at
* @property string $updated_at * @property string $updated_at
* @property string $time_limit * @property string $time_limit
@ -55,7 +56,7 @@ class Questionnaire extends ActiveRecord
[['category_id', 'status'], 'integer'], [['category_id', 'status'], 'integer'],
[['created_at', 'updated_at', 'time_limit'], 'safe'], [['created_at', 'updated_at', 'time_limit'], 'safe'],
['title', 'unique'], ['title', 'unique'],
[['title'], 'string', 'max' => 255], [['title', 'description'], 'string', 'max' => 255],
['status', 'default', 'value' => true], ['status', 'default', 'value' => true],
[['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => QuestionnaireCategory::className(), 'targetAttribute' => ['category_id' => 'id']], [['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => QuestionnaireCategory::className(), 'targetAttribute' => ['category_id' => 'id']],
]; ];
@ -70,6 +71,7 @@ class Questionnaire extends ActiveRecord
'id' => 'ID', 'id' => 'ID',
'category_id' => 'Категория', 'category_id' => 'Категория',
'title' => 'Название анкеты', 'title' => 'Название анкеты',
'description' => 'Описание',
'status' => 'Статус', 'status' => 'Статус',
'created_at' => 'Created At', 'created_at' => 'Created At',
'updated_at' => 'Updated At', 'updated_at' => 'Updated At',

View File

@ -179,18 +179,9 @@ class UserQuestionnaire extends ActiveRecord
public static function findActiveUserQuestionnaires($user_id): array public static function findActiveUserQuestionnaires($user_id): array
{ {
$models = self::find() return self::find()
->where(['user_id' => $user_id]) ->where(['user_id' => $user_id])
->andWhere(['not', ['user_questionnaire.status' => 0]]) ->andWhere(['not', ['user_questionnaire.status' => 0]])
->all(); ->all();
$modelsArr = array();
foreach ($models as $model) {
$modelsArr[] = array_merge($model->toArray(), [
'questionnaire_title' => $model->getQuestionnaireTitle()
]);
}
return $modelsArr;
} }
} }

View File

@ -0,0 +1,25 @@
<?php
use yii\db\Migration;
/**
* Class m231026_071555_add_column_description_to_questionnaire_table
*/
class m231026_071555_add_column_description_to_questionnaire_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->addColumn('questionnaire', 'description', $this->string(255));
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropColumn('questionnaire', 'description');
}
}

View File

@ -2,7 +2,8 @@
namespace frontend\modules\api\controllers; namespace frontend\modules\api\controllers;
use common\services\UserQuestionnaireService; use frontend\modules\api\models\UserQuestionnaire;
use frontend\modules\api\services\UserQuestionnaireService;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\web\ServerErrorHttpException; use yii\web\ServerErrorHttpException;
@ -99,7 +100,7 @@ class UserQuestionnaireController extends ApiController
* @throws NotFoundHttpException * @throws NotFoundHttpException
* @throws ServerErrorHttpException * @throws ServerErrorHttpException
*/ */
public function actionQuestionnaireCompleted($user_questionnaire_uuid) public function actionQuestionnaireCompleted($user_questionnaire_uuid): UserQuestionnaire
{ {
$userQuestionnaireModel = UserQuestionnaireService::calculateScore($user_questionnaire_uuid); $userQuestionnaireModel = UserQuestionnaireService::calculateScore($user_questionnaire_uuid);
if ($userQuestionnaireModel->errors) { if ($userQuestionnaireModel->errors) {
@ -142,7 +143,7 @@ class UserQuestionnaireController extends ApiController
* ) * )
* @throws ServerErrorHttpException * @throws ServerErrorHttpException
*/ */
public function actionGetPointsNumber($user_questionnaire_uuid) public function actionGetPointsNumber($user_questionnaire_uuid): array
{ {
$questionPointsNumber = UserQuestionnaireService::getPointsNumber($user_questionnaire_uuid); $questionPointsNumber = UserQuestionnaireService::getPointsNumber($user_questionnaire_uuid);
if (empty($questionPointsNumber)) { if (empty($questionPointsNumber)) {
@ -185,7 +186,7 @@ class UserQuestionnaireController extends ApiController
* ) * )
* @throws ServerErrorHttpException * @throws ServerErrorHttpException
*/ */
public function actionGetQuestionNumber($user_questionnaire_uuid) public function actionGetQuestionNumber($user_questionnaire_uuid): array
{ {
$questionNumber = UserQuestionnaireService::getQuestionNumber($user_questionnaire_uuid); $questionNumber = UserQuestionnaireService::getQuestionNumber($user_questionnaire_uuid);
if (empty($questionNumber)) { if (empty($questionNumber)) {

View File

@ -99,5 +99,29 @@ namespace frontend\modules\api\models;
*/ */
class UserQuestionnaire extends \common\models\UserQuestionnaire class UserQuestionnaire extends \common\models\UserQuestionnaire
{ {
public function fields(): array
{
return [
'user_id',
'uuid',
'score',
'status',
'percent_correct_answers',
'testing_date',
'questionnaire_title' => function() {
return $this->questionnaire->title;
},
'description' => function() {
return $this->questionnaire->description;
},
];
}
/**
* @return string[]
*/
public function extraFields(): array
{
return [];
}
} }

View File

@ -1,9 +1,10 @@
<?php <?php
namespace common\services; namespace frontend\modules\api\services;
use common\models\Question; use common\models\Question;
use common\models\UserQuestionnaire; use common\services\ScoreCalculatorService;
use frontend\modules\api\models\UserQuestionnaire;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\web\ServerErrorHttpException; use yii\web\ServerErrorHttpException;
@ -12,16 +13,7 @@ class UserQuestionnaireService
{ {
public static function getQuestionnaireList($user_id): array public static function getQuestionnaireList($user_id): array
{ {
$userQuestionnaireModels = UserQuestionnaire::findActiveUserQuestionnaires($user_id); return UserQuestionnaire::findActiveUserQuestionnaires($user_id);
array_walk($userQuestionnaireModels, function (&$arr) {
unset(
$arr['questionnaire_id'],
$arr['created_at'],
$arr['updated_at'],
$arr['id']
);
});
return $userQuestionnaireModels;
} }
/** /**