add count current answers at user Questionnaire

This commit is contained in:
Kavalar 2024-02-07 00:38:10 +03:00
parent 4845f7944b
commit 6d23a9825a
3 changed files with 33 additions and 3 deletions

View File

@ -27,6 +27,10 @@ use yii\db\Expression;
*/
class Question extends \yii\db\ActiveRecord
{
const STATUS_ACTIVE = 1;
const STATUS_DISABLE = 0;
/**
* {@inheritdoc}
*/

View File

@ -35,6 +35,9 @@ use \backend\modules\questionnaire\models\Answer;
*/
class UserQuestionnaire extends ActiveRecord
{
const STATUS_ACTIVE = 1;
const STATUS_DISABLE = 0;
/**
* {@inheritdoc}
*/
@ -148,6 +151,15 @@ class UserQuestionnaire extends ActiveRecord
->viaTable('user_response', ['user_questionnaire_uuid' => 'uuid']);
}
/**
* @param $questionnaire_id
* @return bool|int|string|null
*/
public function countCorrectAnswers($questionnaire_id): bool|int|string|null
{
return UserResponse::find()->where(['user_questionnaire_uuid' => $questionnaire_id, 'answer_flag' => 1])->count();
}
/**
* @throws InvalidConfigException
*/
@ -182,7 +194,7 @@ class UserQuestionnaire extends ActiveRecord
public static function findActiveUserQuestionnaires($user_id): array
{
return self::find()
return self::find()
->where(['user_id' => $user_id])
->andWhere(['not', ['user_questionnaire.status' => 0]])
->all();

View File

@ -68,6 +68,12 @@ namespace frontend\modules\api\models\questionnaire;
* description="Количество вопросов"
* ),
* @OA\Property(
* property="count_correct_answers",
* type="int",
* example="18",
* description="Количество правильных ответов"
* ),
* @OA\Property(
* property="time_limit",
* type="string",
* example="00:50:00",
@ -88,8 +94,8 @@ namespace frontend\modules\api\models\questionnaire;
* schema="UserQuestionnaireArrExample",
* type="array",
* example={
* {"uuid": "d222f858-60fd-47fb-8731-dc9d5fc384c5", "score": 11, "status": 2, "percent_correct_answers": 0.25, "testing_date": "2022-04-03 09:23:45", "questionnaire_title": "Тест 2", "description": "Описание", "points_number": "22", "number_questions": "15", "time_limit": "00:50:00"},
* {"uuid": "gcjs77d9-vtyd-02jh-9467-dc8fbb6s6jdb", "score": 20, "status": 2, "percent_correct_answers": 0.85, "testing_date": "2022-03-17 11:14:22", "questionnaire_title": "Тест 1", "description": "Описание", "points_number": "80", "number_questions": "35", "time_limit": "01:10:00"},
* {"uuid": "d222f858-60fd-47fb-8731-dc9d5fc384c5", "score": 11, "status": 2, "percent_correct_answers": 0.25, "testing_date": "2022-04-03 09:23:45", "questionnaire_title": "Тест 2", "description": "Описание", "points_number": "22", "number_questions": "15", "count_correct_answers": "13", "time_limit": "00:50:00"},
* {"uuid": "gcjs77d9-vtyd-02jh-9467-dc8fbb6s6jdb", "score": 20, "status": 2, "percent_correct_answers": 0.85, "testing_date": "2022-03-17 11:14:22", "questionnaire_title": "Тест 1", "description": "Описание", "points_number": "80", "number_questions": "35", "count_correct_answers": "25", "time_limit": "01:10:00"},
* },
* @OA\Items(
* type="object",
@ -104,6 +110,7 @@ namespace frontend\modules\api\models\questionnaire;
* @OA\Property(
* property="status",
* type="integer",
* description="Статус опроса"
* ),
* @OA\Property(
* property="percent_correct_answers",
@ -130,6 +137,10 @@ namespace frontend\modules\api\models\questionnaire;
* type="int",
* ),
* @OA\Property(
* property="count_correct_answers",
* type="int",
* ),
* @OA\Property(
* property="time_limit",
* type="string",
* ),
@ -166,6 +177,9 @@ class UserQuestionnaire extends \common\models\UserQuestionnaire
->andWhere(['status' => 1])
->count();
},
'count_correct_answers' => function () {
return $this->countCorrectAnswers($this->questionnaire_id);
},
'time_limit' => function () {
return $this->questionnaire->time_limit;
}