Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 108 |
| AccompanyingEventSurveyController | |
0.00% |
0 / 1 |
|
0.00% |
0 / 8 |
342.00 | |
0.00% |
0 / 108 |
| behaviors | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 21 |
|||
| actions | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 13 |
|||
| actionView | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 10 |
|||
| actionCreate | |
0.00% |
0 / 1 |
30.00 | |
0.00% |
0 / 18 |
|||
| actionUpdate | |
0.00% |
0 / 1 |
30.00 | |
0.00% |
0 / 14 |
|||
| actionDelete | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 7 |
|||
| actionDeleteAnswers | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 18 |
|||
| findModel | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 7 |
|||
| <?php | |
| namespace backend\controllers; | |
| use Yii; | |
| use common\models\AccompanyingEvent; | |
| use common\models\AccompanyingEventSurvey; | |
| use common\models\Survey; | |
| use common\models\SurveyAnswer; | |
| use common\models\SurveyOption; | |
| use backend\models\SurveyQuestionSearch; | |
| use backend\models\SurveySearch; | |
| use yii\web\BadRequestHttpException; | |
| use yii\web\NotFoundHttpException; | |
| use yii\filters\AccessControl; | |
| use yii\filters\VerbFilter; | |
| use yii\helpers\ArrayHelper; | |
| /** | |
| * AccompanyingEventSurveyController implements the CRUD actions for AccompanyingEventSurvey model. | |
| */ | |
| class AccompanyingEventSurveyController extends \yii\web\Controller | |
| { | |
| public function behaviors() | |
| { | |
| return [ | |
| 'access' => [ | |
| 'class' => AccessControl::className(), | |
| 'rules' => [ | |
| [ | |
| 'actions' => ['index', 'view', 'create', 'update', 'delete', 'view-answers', 'import-answers', 'delete-answers'], | |
| 'allow' => true, | |
| 'roles' => ['supervisor', 'admin'], | |
| ], | |
| ], | |
| ], | |
| 'verbs' => [ | |
| 'class' => VerbFilter::className(), | |
| 'actions' => [ | |
| 'delete' => ['POST'], | |
| 'delete-answers' => ['POST'], | |
| ], | |
| ], | |
| ]; | |
| } | |
| /** | |
| * @inheritdoc | |
| */ | |
| public function actions() | |
| { | |
| return [ | |
| 'view-answers' => [ | |
| 'class' => 'backend\controllers\survey\ViewAction', | |
| 'surveyClassName' => 'common\models\AccompanyingEventSurvey', | |
| ], | |
| 'import-answers' => [ | |
| 'class' => 'backend\controllers\survey\ImportAction', | |
| 'surveyClassName' => 'common\models\AccompanyingEventSurvey', | |
| 'surveyLockClassName' => 'common\models\AccompanyingEventSurveyLock', | |
| ], | |
| ]; | |
| } | |
| /** | |
| * Displays a single Accompanying Event Survey model. | |
| * @param string $id | |
| * @return mixed | |
| */ | |
| public function actionView($id) | |
| { | |
| $model=$this->findModel($id); | |
| $searchModel = new SurveyQuestionSearch(); | |
| $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $model->survey->getSurveyQuestions()); | |
| return $this->render('view', [ | |
| 'model' => $model, | |
| 'questionsSearchModel' => $searchModel, | |
| 'questionsDataProvider' => $dataProvider, | |
| ]); | |
| } | |
| /** | |
| * Creates a new Accompanying Event Survey model. | |
| * If creation is successful, the browser will be redirected to the 'view' page. | |
| * @return mixed | |
| */ | |
| public function actionCreate() | |
| { | |
| $model = new AccompanyingEventSurvey(); | |
| $surveyModel = new Survey(); | |
| $accompanyingEvents = AccompanyingEvent::find()->all(); | |
| if ($model->load(Yii::$app->request->post()) && $surveyModel->load(Yii::$app->request->post())) { | |
| if ($surveyModel->save()) { | |
| $model->survey_id = $surveyModel->id; | |
| if ($model->save()) { | |
| return $this->redirect(['view', 'id' => $model->id]); | |
| } | |
| } | |
| } | |
| return $this->render('create', [ | |
| 'model' => $model, | |
| 'surveyModel' => $surveyModel, | |
| 'accompanyingEvents' => ArrayHelper::map($accompanyingEvents, 'id', 'name'), | |
| ]); | |
| } | |
| /** | |
| * Updates an existing Accompanying Event Survey model. | |
| * If update is successful, the browser will be redirected to the 'view' page. | |
| * @param string $id | |
| * @return mixed | |
| */ | |
| public function actionUpdate($id) | |
| { | |
| $model = $this->findModel($id); | |
| $surveyModel = $model->survey; | |
| $accompanyingEvents = AccompanyingEvent::find()->all(); | |
| if ($model->load(Yii::$app->request->post()) && $surveyModel->load(Yii::$app->request->post())) | |
| if ($model->save() && $surveyModel->save()) { | |
| return $this->redirect(['view', 'id' => $model->id]); | |
| } | |
| return $this->render('update', [ | |
| 'model' => $model, | |
| 'surveyModel' => $surveyModel, | |
| 'accompanyingEvents' => ArrayHelper::map($accompanyingEvents, 'id', 'name'), | |
| ]); | |
| } | |
| /** | |
| * Deletes an existing Accompanying Event Survey model. | |
| * If deletion is successful, the browser will be redirected to the 'index' page. | |
| * @param string $id | |
| * @return mixed | |
| */ | |
| public function actionDelete($id) | |
| { | |
| $model = $this->findModel($id); | |
| $survey_id = $model->survey->id; | |
| $this->actionDeleteAnswers($id); | |
| $model->delete(); | |
| return $this->run('/survey/delete', ['id' => $survey_id]); | |
| } | |
| /** | |
| * Deletes all answers of existing Accompanying Event Survey model, allows users to fill survey again and clears opinion about specific survey. | |
| * If deletion is successful, the browser will be redirected to the 'view' page. | |
| * @param string $id | |
| * @return mixed | |
| */ | |
| public function actionDeleteAnswers($id) | |
| { | |
| $model = $this->findModel($id); | |
| $transaction = Yii::$app->db->beginTransaction(); | |
| try { | |
| $ids = $model->survey->getSurveyOpinions()->column(); | |
| SurveyAnswer::deleteAll(['survey_opinion_id' => $ids]); | |
| $model->survey->unlinkAll('surveyLocks', true); | |
| $model->survey->unlinkAll('surveyOpinions', true); | |
| $model->unlinkAll('accompanyingEventSurveyLocks', true); | |
| $model->survey->computeAnsweredQuantity(); | |
| $model->survey->save(); | |
| $transaction->commit(); | |
| } catch(\Exception $e) { | |
| $transaction->rollBack(); | |
| throw new BadRequestHttpException($e->getMessage(), 1); | |
| } | |
| return $this->redirect(['view', 'id' => $id]); | |
| } | |
| /** | |
| * Finds the Accompanying Event Survey model based on its primary key value. | |
| * If the model is not found, a 404 HTTP exception will be thrown. | |
| * @param string $id | |
| * @return AccompanyingEventSurvey the loaded model | |
| * @throws NotFoundHttpException if the model cannot be found | |
| */ | |
| protected function findModel($id) | |
| { | |
| if (($model = AccompanyingEventSurvey::findOne($id)) !== null) { | |
| return $model; | |
| } else { | |
| throw new NotFoundHttpException('The requested page does not exist.'); | |
| } | |
| } | |
| } |