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 class Question extends \yii\db\ActiveRecord
{ {
const STATUS_ACTIVE = 1;
const STATUS_DISABLE = 0;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -35,6 +35,9 @@ use \backend\modules\questionnaire\models\Answer;
*/ */
class UserQuestionnaire extends ActiveRecord class UserQuestionnaire extends ActiveRecord
{ {
const STATUS_ACTIVE = 1;
const STATUS_DISABLE = 0;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -148,6 +151,15 @@ class UserQuestionnaire extends ActiveRecord
->viaTable('user_response', ['user_questionnaire_uuid' => 'uuid']); ->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 * @throws InvalidConfigException
*/ */

View File

@ -68,6 +68,12 @@ namespace frontend\modules\api\models\questionnaire;
* description="Количество вопросов" * description="Количество вопросов"
* ), * ),
* @OA\Property( * @OA\Property(
* property="count_correct_answers",
* type="int",
* example="18",
* description="Количество правильных ответов"
* ),
* @OA\Property(
* property="time_limit", * property="time_limit",
* type="string", * type="string",
* example="00:50:00", * example="00:50:00",
@ -88,8 +94,8 @@ namespace frontend\modules\api\models\questionnaire;
* schema="UserQuestionnaireArrExample", * schema="UserQuestionnaireArrExample",
* type="array", * type="array",
* example={ * 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": "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", "time_limit": "01:10: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( * @OA\Items(
* type="object", * type="object",
@ -104,6 +110,7 @@ namespace frontend\modules\api\models\questionnaire;
* @OA\Property( * @OA\Property(
* property="status", * property="status",
* type="integer", * type="integer",
* description="Статус опроса"
* ), * ),
* @OA\Property( * @OA\Property(
* property="percent_correct_answers", * property="percent_correct_answers",
@ -130,6 +137,10 @@ namespace frontend\modules\api\models\questionnaire;
* type="int", * type="int",
* ), * ),
* @OA\Property( * @OA\Property(
* property="count_correct_answers",
* type="int",
* ),
* @OA\Property(
* property="time_limit", * property="time_limit",
* type="string", * type="string",
* ), * ),
@ -166,6 +177,9 @@ class UserQuestionnaire extends \common\models\UserQuestionnaire
->andWhere(['status' => 1]) ->andWhere(['status' => 1])
->count(); ->count();
}, },
'count_correct_answers' => function () {
return $this->countCorrectAnswers($this->questionnaire_id);
},
'time_limit' => function () { 'time_limit' => function () {
return $this->questionnaire->time_limit; return $this->questionnaire->time_limit;
} }