add filters, update api, update api docs
This commit is contained in:
@ -34,22 +34,22 @@ class AnswerController extends Controller
|
||||
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');
|
||||
throw new NotFoundHttpException('Incorrect question ID');
|
||||
}
|
||||
|
||||
$answers = Answer::activeAnswers($question_id);
|
||||
if(empty($answers)) {
|
||||
throw new NotFoundHttpException('Active questionnaire not found');
|
||||
throw new NotFoundHttpException('Answers not found or question inactive');
|
||||
}
|
||||
|
||||
array_walk( $answers, function(&$arr){
|
||||
unset(
|
||||
$arr['created_at'],
|
||||
$arr['updated_at'],
|
||||
$arr['answer_flag']
|
||||
$arr['answer_flag'],
|
||||
$arr['status']
|
||||
);
|
||||
});
|
||||
|
||||
|
@ -2,22 +2,22 @@
|
||||
|
||||
namespace frontend\modules\api\controllers;
|
||||
|
||||
use common\helpers\UUIDHelper;
|
||||
use common\models\Question;
|
||||
use common\models\Questionnaire;
|
||||
use common\models\UserQuestionnaire;
|
||||
use Yii;
|
||||
use yii\filters\auth\HttpBearerAuth;
|
||||
use yii\rest\Controller;
|
||||
use yii\web\NotFoundHttpException;
|
||||
|
||||
class QuestionController extends \yii\rest\Controller
|
||||
class QuestionController extends Controller
|
||||
{
|
||||
public function behaviors()
|
||||
{
|
||||
$behaviors = parent::behaviors();
|
||||
|
||||
$behaviors['authenticator']['authMethods'] = [
|
||||
HttpBearerAuth::className(),
|
||||
];
|
||||
|
||||
return $behaviors;
|
||||
}
|
||||
|
||||
@ -30,19 +30,22 @@ class QuestionController extends \yii\rest\Controller
|
||||
|
||||
/**
|
||||
* @throws NotFoundHttpException
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function actionGetQuestions()
|
||||
public function actionGetQuestions(): array
|
||||
{
|
||||
$questionnaire_id = Yii::$app->request->get('questionnaire_id');
|
||||
$uuid = Yii::$app->request->get('uuid');
|
||||
|
||||
if(empty($questionnaire_id) or !is_numeric($questionnaire_id))
|
||||
if(empty($uuid) or !UUIDHelper::is_valid($uuid))
|
||||
{
|
||||
throw new NotFoundHttpException('Incorrect questionnaire ID');
|
||||
throw new NotFoundHttpException('Incorrect questionnaire UUID');
|
||||
}
|
||||
|
||||
$questionnaire_id = UserQuestionnaire::getQuestionnaireId($uuid);
|
||||
|
||||
$questions = Question::activeQuestions($questionnaire_id);
|
||||
if(empty($questions)) {
|
||||
throw new NotFoundHttpException('Active questionnaire not found');
|
||||
throw new NotFoundHttpException('Questions not found');
|
||||
}
|
||||
|
||||
array_walk( $questions, function(&$arr){
|
||||
@ -51,6 +54,7 @@ class QuestionController extends \yii\rest\Controller
|
||||
$arr['created_at'],
|
||||
$arr['updated_at'],
|
||||
$arr['status'],
|
||||
$arr['questionnaire_id']
|
||||
);
|
||||
});
|
||||
|
||||
|
@ -47,11 +47,10 @@ class UserQuestionnaireController extends Controller
|
||||
|
||||
array_walk( $userQuestionnaireModel, function(&$arr){
|
||||
unset(
|
||||
$arr['uuid'],
|
||||
$arr['questionnaire_id'],
|
||||
$arr['created_at'],
|
||||
$arr['updated_at'],
|
||||
$arr['score'],
|
||||
$arr['percent_correct_answers']
|
||||
$arr['id'],
|
||||
);
|
||||
});
|
||||
|
||||
|
@ -2,16 +2,15 @@
|
||||
|
||||
namespace frontend\modules\api\controllers;
|
||||
|
||||
use common\models\Reports;
|
||||
use common\models\UserResponse;
|
||||
use Yii;
|
||||
use yii\base\InvalidConfigException;
|
||||
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
|
||||
class UserResponseController extends ActiveController
|
||||
{
|
||||
public $modelClass = 'common\models\UserResponse';
|
||||
|
||||
@ -29,8 +28,7 @@ class UserResponseController extends \yii\rest\ActiveController
|
||||
public function verbs(): array
|
||||
{
|
||||
return [
|
||||
// 'set-responses' => ['post'],
|
||||
'create' => ['post'],
|
||||
'set-response' => ['post'],
|
||||
];
|
||||
}
|
||||
|
||||
@ -41,16 +39,28 @@ class UserResponseController extends \yii\rest\ActiveController
|
||||
return $actions;
|
||||
}
|
||||
|
||||
public function actionCreate()
|
||||
/**
|
||||
* @throws InvalidConfigException
|
||||
* @throws BadRequestHttpException
|
||||
* @throws ServerErrorHttpException
|
||||
*/
|
||||
public function actionSetResponse(): UserResponse
|
||||
{
|
||||
$model = new UserResponse();
|
||||
|
||||
$model->load(Yii::$app->getRequest()->getBodyParams(), '');
|
||||
|
||||
if(!$model->validate()){
|
||||
throw new BadRequestHttpException(json_encode($model->errors));
|
||||
}
|
||||
|
||||
if (empty($model->user_id) or empty($model->question_id) or empty($model->user_questionnaire_uuid)) {
|
||||
throw new BadRequestHttpException(json_encode($model->errors));
|
||||
}
|
||||
|
||||
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.');
|
||||
}
|
||||
|
Reference in New Issue
Block a user