2021-10-28 10:51:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace frontend\modules\api\controllers;
|
|
|
|
|
2021-11-12 14:30:01 +03:00
|
|
|
use common\helpers\UUIDHelper;
|
2021-10-28 10:51:14 +03:00
|
|
|
use common\models\Question;
|
2021-11-12 14:30:01 +03:00
|
|
|
use common\models\UserQuestionnaire;
|
2021-10-28 10:51:14 +03:00
|
|
|
use Yii;
|
|
|
|
use yii\filters\auth\HttpBearerAuth;
|
2021-11-12 14:30:01 +03:00
|
|
|
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
|
2021-11-12 14:30:01 +03:00
|
|
|
* @throws \Exception
|
2021-10-28 10:51:14 +03:00
|
|
|
*/
|
2021-11-12 14:30:01 +03:00
|
|
|
public function actionGetQuestions(): array
|
2021-10-28 10:51:14 +03:00
|
|
|
{
|
2021-11-12 14:30:01 +03:00
|
|
|
$uuid = Yii::$app->request->get('uuid');
|
2021-10-28 10:51:14 +03:00
|
|
|
|
2021-11-12 14:30:01 +03:00
|
|
|
if(empty($uuid) or !UUIDHelper::is_valid($uuid))
|
2021-10-28 10:51:14 +03:00
|
|
|
{
|
2021-11-12 14:30:01 +03:00
|
|
|
throw new NotFoundHttpException('Incorrect questionnaire UUID');
|
2021-10-28 10:51:14 +03:00
|
|
|
}
|
|
|
|
|
2021-11-12 14:30:01 +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)) {
|
2021-11-12 14:30:01 +03:00
|
|
|
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'],
|
2021-11-12 14:30:01 +03:00
|
|
|
$arr['questionnaire_id']
|
2021-10-28 10:51:14 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return $questions;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|