Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 129
SurveyController
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 9
600.00
0.00% covered (danger)
0.00%
0 / 129
 behaviors
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 21
 actions
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 10
 actionIndex
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 8
 actionView
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 19
 actionCreate
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 10
 actionUpdate
0.00% covered (danger)
0.00%
0 / 1
42.00
0.00% covered (danger)
0.00%
0 / 19
 actionDelete
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 18
 actionDeleteAnswers
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 17
 findModel
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 7
<?php
namespace backend\controllers;
use Yii;
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;
/**
 * SurveyController implements the CRUD actions for Survey model.
 */
class SurveyController 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',
            ],
            'import-answers' => [
                'class' => 'backend\controllers\survey\ImportAction',
            ],
        ];
    }
    /**
     * Lists all Survey models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new SurveySearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    /**
     * Displays a single Survey model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        $model=$this->findModel($id);
        
        if($model->eventSurvey != null) {
            return $this->run('/event-survey/view', ['id' => $model->eventSurvey->id]);
        }
        if($model->meetingSurvey != null) {
            return $this->run('/meeting-survey/view', ['id' => $model->meetingSurvey->id]);
        }
        if($model->accompanyingEventSurvey != null) {
            return $this->run('/accompanying-event-survey/view', ['id' => $model->accompanyingEventSurvey->id]);
        }
        $searchModel = new SurveyQuestionSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $model->getSurveyQuestions());
        return $this->render('view', [
            'model' => $model,
            'questionsSearchModel' => $searchModel,
            'questionsDataProvider' => $dataProvider,
        ]);
    }
    /**
     * Creates a new Survey model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Survey();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }
    /**
     * Updates an existing 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);
        if($model->eventSurvey != null) {
            return $this->run('/event-survey/update', ['id' => $model->eventSurvey->id]);
        }
        if($model->meetingSurvey != null) {
            return $this->run('/meeting-survey/update', ['id' => $model->meetingSurvey->id]);
        }
        if($model->accompanyingEventSurvey != null) {
            return $this->run('/accompanying-event-survey/update', ['id' => $model->accompanyingEventSurvey->id]);
        }
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }
    /**
     * Deletes an existing 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);
        if($model->eventSurvey != null) {
            return $this->run('/event-survey/delete', ['id' => $model->eventSurvey->id]);
        }
        if($model->meetingSurvey != null) {
            return $this->run('/meeting-survey/delete', ['id' => $model->meetingSurvey->id]);
        }
        if($model->accompanyingEventSurvey != null) {
            return $this->run('/accompanying-event-survey/delete', ['id' => $model->accompanyingEventSurvey->id]);
        }
        $this->actionDeleteAnswers($id);
        $ids = $model->getSurveyQuestions()->column();
        SurveyOption::deleteAll(['survey_question_id' => $ids]); 
        $model->unlinkAll('surveyQuestions', true);
        $model->delete();
        return $this->redirect(['index']);
    }
    /**
     * Deletes all answers of existing 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->getSurveyOpinions()->column();
            SurveyAnswer::deleteAll(['survey_opinion_id' => $ids]); 
            $model->unlinkAll('surveyLocks', true);
            $model->unlinkAll('surveyOpinions', true);
            $model->computeAnsweredQuantity();
            $model->save();
            $transaction->commit();
        } catch(\Exception $e) {
            $transaction->rollBack();
            throw new BadRequestHttpException($e->getMessage(), 1);
        }
        return $this->redirect(['view', 'id' => $id]);
    }
    /**
     * Finds the 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 Survey the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Survey::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}