diff --git a/backend/modules/api/Api.php b/backend/modules/api/Api.php new file mode 100644 index 0000000..fd7935e --- /dev/null +++ b/backend/modules/api/Api.php @@ -0,0 +1,24 @@ +render('index'); + } +} diff --git a/backend/modules/api/controllers/UserQuestionnaireController.php b/backend/modules/api/controllers/UserQuestionnaireController.php new file mode 100644 index 0000000..e0c148b --- /dev/null +++ b/backend/modules/api/controllers/UserQuestionnaireController.php @@ -0,0 +1,49 @@ + \yii\filters\ContentNegotiator::className(), + 'formats' => [ + 'application/json' => \yii\web\Response::FORMAT_JSON, + ], + ], +// 'authenticator' => [ +// 'class' => CompositeAuth::class, +// 'authMethods' => [ +// HttpBearerAuth::class, +// ], +// ] + ]; + } + + public function actionIndex() + { + return ['some' => 'rrr']; + } + +// /** +// * @inheritdoc +// */ +// protected function verbs() +// { +// return [ +// 'index' => ['GET', 'HEAD'], +// ]; +// } + + + +} diff --git a/backend/modules/api/models/User.php b/backend/modules/api/models/User.php new file mode 100644 index 0000000..ecdf04e --- /dev/null +++ b/backend/modules/api/models/User.php @@ -0,0 +1,96 @@ + 255], + [['auth_key'], 'string', 'max' => 32], + [['username'], 'unique'], + [['email'], 'unique'], + [['password_reset_token'], 'unique'], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'username' => 'Username', + 'auth_key' => 'Auth Key', + 'password_hash' => 'Password Hash', + 'password_reset_token' => 'Password Reset Token', + 'email' => 'Email', + 'status' => 'Status', + 'created_at' => 'Created At', + 'updated_at' => 'Updated At', + 'access_token' => 'Access Token', + 'access_token_expired_at' => 'Access Token Expired At', + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getUserCards() + { + return $this->hasMany(UserCard::className(), ['id_user' => 'id']); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getUserQuestionnaires() + { + return $this->hasMany(UserQuestionnaire::className(), ['user_id' => 'id']); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getUserResponses() + { + return $this->hasMany(UserResponse::className(), ['user_id' => 'id']); + } +} diff --git a/backend/modules/api/models/UserQuestionnaire.php b/backend/modules/api/models/UserQuestionnaire.php new file mode 100644 index 0000000..ee0ae51 --- /dev/null +++ b/backend/modules/api/models/UserQuestionnaire.php @@ -0,0 +1,9 @@ + +

context->action->uniqueId ?>

+

+ This is the view content for action "context->action->id ?>". + The action belongs to the controller "context) ?>" + in the "context->module->id ?>" module. +

+

+ You may customize this page by editing the following file:
+ +

+ diff --git a/backend/modules/api/views/user-questionnaire/index.php b/backend/modules/api/views/user-questionnaire/index.php new file mode 100644 index 0000000..d1e0ec8 --- /dev/null +++ b/backend/modules/api/views/user-questionnaire/index.php @@ -0,0 +1,9 @@ + +

user-questionnaire/index

+ +

+ You may change the content of this page by modifying + the file . +

diff --git a/backend/views/question/index.php b/backend/views/question/index.php new file mode 100644 index 0000000..575655c --- /dev/null +++ b/backend/views/question/index.php @@ -0,0 +1,9 @@ + +

question/index

+ +

+ You may change the content of this page by modifying + the file . +

diff --git a/frontend/modules/api/controllers/AnswerController.php b/frontend/modules/api/controllers/AnswerController.php new file mode 100644 index 0000000..5f12372 --- /dev/null +++ b/frontend/modules/api/controllers/AnswerController.php @@ -0,0 +1,58 @@ + ['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; + } +} diff --git a/frontend/modules/api/controllers/QuestionController.php b/frontend/modules/api/controllers/QuestionController.php new file mode 100644 index 0000000..ef72fa2 --- /dev/null +++ b/frontend/modules/api/controllers/QuestionController.php @@ -0,0 +1,60 @@ + ['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; + } + +} diff --git a/frontend/modules/api/controllers/UserQuestionnaireController.php b/frontend/modules/api/controllers/UserQuestionnaireController.php new file mode 100644 index 0000000..47b4201 --- /dev/null +++ b/frontend/modules/api/controllers/UserQuestionnaireController.php @@ -0,0 +1,60 @@ + ['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; + } +} diff --git a/frontend/modules/api/controllers/UserResponseController.php b/frontend/modules/api/controllers/UserResponseController.php new file mode 100644 index 0000000..013afd9 --- /dev/null +++ b/frontend/modules/api/controllers/UserResponseController.php @@ -0,0 +1,60 @@ + ['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; + } +}