2021-10-28 10:51:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace frontend\modules\api\controllers;
|
|
|
|
|
|
|
|
use common\models\Answer;
|
|
|
|
use Yii;
|
|
|
|
use yii\filters\auth\HttpBearerAuth;
|
|
|
|
use yii\web\NotFoundHttpException;
|
|
|
|
use yii\rest\Controller;
|
|
|
|
|
2022-03-03 17:24:45 +03:00
|
|
|
class AnswerController extends ApiController
|
2021-10-28 10:51:14 +03:00
|
|
|
{
|
2022-03-03 17:24:45 +03:00
|
|
|
// public function behaviors(): array
|
|
|
|
// {
|
|
|
|
// $behaviors = parent::behaviors();
|
|
|
|
//
|
|
|
|
// $behaviors['authenticator']['authMethods'] = [
|
|
|
|
// HttpBearerAuth::className(),
|
|
|
|
// ];
|
|
|
|
//
|
|
|
|
// return $behaviors;
|
|
|
|
// }
|
2021-10-28 10:51:14 +03:00
|
|
|
|
|
|
|
public function verbs(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'get-answers' => ['get'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-10 15:06:01 +03:00
|
|
|
* @OA\Get(path="/answer/get-answers",
|
|
|
|
* summary="Список ответов на вопрос",
|
|
|
|
* description="Получение списка ответов",
|
|
|
|
* security={
|
|
|
|
* {"bearerAuth": {}}
|
|
|
|
* },
|
|
|
|
* tags={"Tests"},
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="question_id",
|
|
|
|
* in="query",
|
|
|
|
* required=true,
|
|
|
|
* description="id вопроса",
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="integer",
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Возвращает масив вопросов",
|
|
|
|
* @OA\MediaType(
|
|
|
|
* mediaType="application/json",
|
|
|
|
* @OA\Schema(ref="#/components/schemas/AnswerExampleArr"),
|
|
|
|
* ),
|
|
|
|
*
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*
|
2021-10-28 10:51:14 +03:00
|
|
|
* @throws NotFoundHttpException
|
|
|
|
*/
|
|
|
|
public function actionGetAnswers(): array
|
|
|
|
{
|
|
|
|
$question_id = Yii::$app->request->get('question_id');
|
|
|
|
if(empty($question_id) or !is_numeric($question_id))
|
|
|
|
{
|
2021-11-12 14:30:01 +03:00
|
|
|
throw new NotFoundHttpException('Incorrect question ID');
|
2021-10-28 10:51:14 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:41:39 +03:00
|
|
|
$answers = Answer::activeAnswers($question_id);
|
2021-10-28 10:51:14 +03:00
|
|
|
if(empty($answers)) {
|
2021-11-12 14:30:01 +03:00
|
|
|
throw new NotFoundHttpException('Answers not found or question inactive');
|
2021-10-28 10:51:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
array_walk( $answers, function(&$arr){
|
|
|
|
unset(
|
|
|
|
$arr['created_at'],
|
|
|
|
$arr['updated_at'],
|
2021-11-12 14:30:01 +03:00
|
|
|
$arr['answer_flag'],
|
|
|
|
$arr['status']
|
2021-10-28 10:51:14 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return $answers;
|
|
|
|
}
|
|
|
|
}
|