guild/frontend/modules/api/controllers/QuestionController.php

65 lines
1.5 KiB
PHP
Raw Normal View History

2021-10-28 10:51:14 +03:00
<?php
namespace frontend\modules\api\controllers;
use common\helpers\UUIDHelper;
2021-10-28 10:51:14 +03:00
use common\models\Question;
use common\models\UserQuestionnaire;
2021-10-28 10:51:14 +03:00
use Yii;
use yii\filters\auth\HttpBearerAuth;
use yii\rest\Controller;
2021-10-28 10:51:14 +03:00
use yii\web\NotFoundHttpException;
2022-01-11 13:37:30 +03:00
class QuestionController extends Controller
2021-10-28 10:51:14 +03:00
{
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator']['authMethods'] = [
HttpBearerAuth::className(),
];
return $behaviors;
}
public function verbs()
{
return [
'get-questions' => ['get'],
];
}
/**
* @throws NotFoundHttpException
* @throws \Exception
2021-10-28 10:51:14 +03:00
*/
public function actionGetQuestions(): array
2021-10-28 10:51:14 +03:00
{
$uuid = Yii::$app->request->get('uuid');
2021-10-28 10:51:14 +03:00
if(empty($uuid) or !UUIDHelper::is_valid($uuid))
2021-10-28 10:51:14 +03:00
{
throw new NotFoundHttpException('Incorrect questionnaire UUID');
2021-10-28 10:51:14 +03:00
}
$questionnaire_id = UserQuestionnaire::getQuestionnaireId($uuid);
2021-11-08 12:41:39 +03:00
$questions = Question::activeQuestions($questionnaire_id);
2021-10-28 10:51:14 +03:00
if(empty($questions)) {
throw new NotFoundHttpException('Questions not found');
2021-10-28 10:51:14 +03:00
}
array_walk( $questions, function(&$arr){
unset(
$arr['score'],
$arr['created_at'],
$arr['updated_at'],
$arr['status'],
$arr['questionnaire_id']
2021-10-28 10:51:14 +03:00
);
});
return $questions;
}
}