adding forgotten files for last commit

This commit is contained in:
iIronside
2021-10-28 10:51:14 +03:00
parent 79c197f673
commit 34c2998844
12 changed files with 466 additions and 0 deletions

View File

@ -0,0 +1,58 @@
<?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;
class AnswerController extends Controller
{
public function behaviors(): array
{
$behaviors = parent::behaviors();
$behaviors['authenticator']['authMethods'] = [
HttpBearerAuth::className(),
];
return $behaviors;
}
public function verbs(): array
{
return [
'get-answers' => ['get'],
];
}
/**
* @throws NotFoundHttpException
*/
public function actionGetAnswers(): array
{
$question_id = Yii::$app->request->get('question_id');
if(empty($question_id) or !is_numeric($question_id))
{
throw new NotFoundHttpException('Incorrect questionnaire ID');
}
$answers = Answer::getActiveAnswers($question_id);
if(empty($answers)) {
throw new NotFoundHttpException('Active questionnaire not found');
}
array_walk( $answers, function(&$arr){
unset(
$arr['created_at'],
$arr['updated_at'],
$arr['answer_flag']
);
});
return $answers;
}
}

View File

@ -0,0 +1,60 @@
<?php
namespace frontend\modules\api\controllers;
use common\models\Question;
use common\models\Questionnaire;
use Yii;
use yii\filters\auth\HttpBearerAuth;
use yii\web\NotFoundHttpException;
class QuestionController extends \yii\rest\Controller
{
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator']['authMethods'] = [
HttpBearerAuth::className(),
];
return $behaviors;
}
public function verbs()
{
return [
'get-questions' => ['get'],
];
}
/**
* @throws NotFoundHttpException
*/
public function actionGetQuestions()
{
$questionnaire_id = Yii::$app->request->get('questionnaire_id');
if(empty($questionnaire_id) or !is_numeric($questionnaire_id))
{
throw new NotFoundHttpException('Incorrect questionnaire ID');
}
$questions = Question::getActiveQuestions($questionnaire_id);
if(empty($questions)) {
throw new NotFoundHttpException('Active questionnaire not found');
}
array_walk( $questions, function(&$arr){
unset(
$arr['score'],
$arr['created_at'],
$arr['updated_at'],
$arr['status'],
);
});
return $questions;
}
}

View File

@ -0,0 +1,60 @@
<?php
namespace frontend\modules\api\controllers;
use common\models\UserQuestionnaire;
use Yii;
use yii\filters\auth\HttpBearerAuth;
use yii\rest\Controller;
use yii\web\NotFoundHttpException;
class UserQuestionnaireController extends Controller
{
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator']['authMethods'] = [
HttpBearerAuth::className(),
];
return $behaviors;
}
public function verbs()
{
return [
'questionnaires-list' => ['get'],
];
}
/**
* @throws NotFoundHttpException
*/
public function actionQuestionnairesList(): array
{
$user_id = Yii::$app->request->get('user_id');
if(empty($user_id) or !is_numeric($user_id))
{
throw new NotFoundHttpException('Incorrect user ID');
}
$userQuestionnaireModel = UserQuestionnaire::findActiveUserQuestionnaires($user_id);
if(empty($userQuestionnaireModel)) {
throw new NotFoundHttpException('Active questionnaire not found');
}
array_walk( $userQuestionnaireModel, function(&$arr){
unset(
$arr['uuid'],
$arr['created_at'],
$arr['updated_at'],
$arr['score'],
$arr['percent_correct_answers']
);
});
return $userQuestionnaireModel;
}
}

View File

@ -0,0 +1,60 @@
<?php
namespace frontend\modules\api\controllers;
use common\models\Reports;
use common\models\UserResponse;
use Yii;
use yii\filters\auth\HttpBearerAuth;
use yii\helpers\Url;
use yii\rest\ActiveController;
use yii\web\BadRequestHttpException;
use yii\web\ServerErrorHttpException;
class UserResponseController extends \yii\rest\ActiveController
{
public $modelClass = 'common\models\UserResponse';
public function behaviors(): array
{
$behaviors = parent::behaviors();
$behaviors['authenticator']['authMethods'] = [
HttpBearerAuth::className(),
];
return $behaviors;
}
public function verbs(): array
{
return [
// 'set-responses' => ['post'],
'create' => ['post'],
];
}
public function actions()
{
$actions = parent::actions();
unset($actions['create']);
return $actions;
}
public function actionCreate()
{
$model = new UserResponse();
$model->load(Yii::$app->getRequest()->getBodyParams(), '');
if ($model->save()) {
$response = Yii::$app->getResponse();
$response->setStatusCode(201);
// $id = implode(',', array_values($model->getPrimaryKey(true)));
// $response->getHeaders()->set('Location', Url::toRoute(['view', 'id' => $id], true));
} elseif (!$model->hasErrors()) {
throw new ServerErrorHttpException('Failed to create the object for unknown reason.');
}
return $model;
}
}