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,24 @@
<?php
namespace backend\modules\api;
/**
* api module definition class
*/
class Api extends \yii\base\Module
{
/**
* {@inheritdoc}
*/
public $controllerNamespace = 'backend\modules\api\controllers';
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
// custom initialization code goes here
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace backend\modules\api\controllers;
use yii\web\Controller;
/**
* Default controller for the `api` module
*/
class DefaultController extends Controller
{
/**
* Renders the index view for the module
* @return string
*/
public function actionIndex()
{
return $this->render('index');
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace backend\modules\api\controllers;
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth;
use yii\rest\Controller;
class UserQuestionnaireController extends Controller
{
public $modelClass = 'backend/modules/api/models/UserQuestionnaire';
public function behaviors()
{
return [
[
'class' => \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'],
// ];
// }
}

View File

@ -0,0 +1,96 @@
<?php
namespace backend\modules\api\models;
use Yii;
/**
* This is the model class for table "user".
*
* @property int $id
* @property string $username
* @property string $auth_key
* @property string $password_hash
* @property string $password_reset_token
* @property string $email
* @property int $status
* @property int $created_at
* @property int $updated_at
* @property string $access_token
* @property string $access_token_expired_at
*
* @property UserCard[] $userCards
* @property UserQuestionnaire[] $userQuestionnaires
* @property UserResponse[] $userResponses
*/
class User extends \common\models\User
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'user';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['username', 'auth_key', 'password_hash', 'email', 'created_at', 'updated_at'], 'required'],
[['status', 'created_at', 'updated_at'], 'integer'],
[['access_token_expired_at'], 'safe'],
[['username', 'password_hash', 'password_reset_token', 'email', 'access_token'], 'string', 'max' => 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']);
}
}

View File

@ -0,0 +1,9 @@
<?php
namespace backend\modules\api\models;
class UserQuestionnaire extends \common\models\UserQuestionnaire
{
}

View File

@ -0,0 +1,12 @@
<div class="api-default-index">
<h1><?= $this->context->action->uniqueId ?></h1>
<p>
This is the view content for action "<?= $this->context->action->id ?>".
The action belongs to the controller "<?= get_class($this->context) ?>"
in the "<?= $this->context->module->id ?>" module.
</p>
<p>
You may customize this page by editing the following file:<br>
<code><?= __FILE__ ?></code>
</p>
</div>

View File

@ -0,0 +1,9 @@
<?php
/* @var $this yii\web\View */
?>
<h1>user-questionnaire/index</h1>
<p>
You may change the content of this page by modifying
the file <code><?= __FILE__; ?></code>.
</p>

View File

@ -0,0 +1,9 @@
<?php
/* @var $this yii\web\View */
?>
<h1>question/index</h1>
<p>
You may change the content of this page by modifying
the file <code><?= __FILE__; ?></code>.
</p>

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;
}
}