2021-10-28 10:51:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace frontend\modules\api\controllers;
|
|
|
|
|
2022-03-15 14:54:35 +03:00
|
|
|
use common\services\ScoreCalculatorService;
|
2021-10-28 10:51:14 +03:00
|
|
|
use common\models\UserResponse;
|
|
|
|
use Yii;
|
2021-11-12 14:30:01 +03:00
|
|
|
use yii\base\InvalidConfigException;
|
2021-10-28 10:51:14 +03:00
|
|
|
use yii\web\BadRequestHttpException;
|
|
|
|
use yii\web\ServerErrorHttpException;
|
|
|
|
|
2022-03-11 14:37:44 +03:00
|
|
|
class UserResponseController extends ApiController
|
2021-10-28 10:51:14 +03:00
|
|
|
{
|
|
|
|
public $modelClass = 'common\models\UserResponse';
|
|
|
|
|
|
|
|
public function verbs(): array
|
|
|
|
{
|
|
|
|
return [
|
2021-11-12 14:30:01 +03:00
|
|
|
'set-response' => ['post'],
|
2021-11-15 12:57:21 +03:00
|
|
|
'set-responses' => ['post'],
|
2021-10-28 10:51:14 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-12 14:30:01 +03:00
|
|
|
/**
|
|
|
|
* @throws InvalidConfigException
|
|
|
|
* @throws BadRequestHttpException
|
|
|
|
* @throws ServerErrorHttpException
|
|
|
|
*/
|
2021-11-15 12:57:21 +03:00
|
|
|
public function actionSetResponse()
|
2021-10-28 10:51:14 +03:00
|
|
|
{
|
2021-11-15 12:57:21 +03:00
|
|
|
$request = Yii::$app->getRequest()->getBodyParams();
|
|
|
|
|
2021-10-28 10:51:14 +03:00
|
|
|
$model = new UserResponse();
|
2021-11-15 12:57:21 +03:00
|
|
|
$model->load($request, '');
|
|
|
|
|
|
|
|
$this->validateResponseModel($model);
|
|
|
|
$this->saveModel($model);
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws InvalidConfigException
|
|
|
|
* @throws ServerErrorHttpException
|
|
|
|
* @throws BadRequestHttpException
|
|
|
|
*/
|
|
|
|
public function actionSetResponses(): array
|
|
|
|
{
|
|
|
|
$requests = Yii::$app->getRequest()->getBodyParams();
|
|
|
|
|
|
|
|
$responseModels = array();
|
2021-10-28 10:51:14 +03:00
|
|
|
|
2021-11-15 12:57:21 +03:00
|
|
|
foreach ($requests['userResponses'] as $request) {
|
|
|
|
$model = new UserResponse();
|
|
|
|
$model->load($request, '');
|
|
|
|
$this->validateResponseModel($model);
|
|
|
|
|
|
|
|
array_push($responseModels, $model);
|
|
|
|
}
|
2021-11-12 14:30:01 +03:00
|
|
|
|
2021-11-15 12:57:21 +03:00
|
|
|
foreach ($responseModels as $responseModel) {
|
|
|
|
$this->saveModel($responseModel);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $responseModels;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws BadRequestHttpException
|
|
|
|
*/
|
|
|
|
protected function validateResponseModel($model)
|
|
|
|
{
|
|
|
|
if(!$model->validate()) {
|
2021-11-12 14:30:01 +03:00
|
|
|
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));
|
|
|
|
}
|
2021-11-15 12:57:21 +03:00
|
|
|
}
|
2021-11-12 14:30:01 +03:00
|
|
|
|
2021-11-15 12:57:21 +03:00
|
|
|
/**
|
|
|
|
* @throws ServerErrorHttpException
|
|
|
|
*/
|
|
|
|
protected function saveModel($model)
|
|
|
|
{
|
2021-10-28 10:51:14 +03:00
|
|
|
if ($model->save()) {
|
2022-03-15 14:54:35 +03:00
|
|
|
ScoreCalculatorService::rateOneResponse($model);
|
2021-10-28 10:51:14 +03:00
|
|
|
$response = Yii::$app->getResponse();
|
|
|
|
$response->setStatusCode(201);
|
|
|
|
} elseif (!$model->hasErrors()) {
|
|
|
|
throw new ServerErrorHttpException('Failed to create the object for unknown reason.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|