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 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 108
MeetingSurveyController
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 8
342.00
0.00% covered (danger)
0.00%
0 / 108
 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 / 13
 actionView
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 10
 actionCreate
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 18
 actionUpdate
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 14
 actionDelete
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 7
 actionDeleteAnswers
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 18
 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\Event;
use common\models\MeetingSurvey;
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;
/**
 * MeetingSurveyController implements the CRUD actions for Meeting Survey model.
 */
class MeetingSurveyController 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\MeetingSurvey',
            ],
            'import-answers' => [
                'class' => 'backend\controllers\survey\ImportAction',
                'surveyClassName' => 'common\models\MeetingSurvey',
                'surveyLockClassName' => 'common\models\MeetingSurveyLock',
            ],
        ];
    }
    /**
     * Displays a single Meeting 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 MeetingSurvey model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new MeetingSurvey();
        $surveyModel = new Survey();
        $events = Event::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,
            'events' => ArrayHelper::map($events, 'id', 'name'),
        ]);
    }
    /**
     * Updates an existing Meeting 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;
        $events = Event::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,
            'events' => ArrayHelper::map($events, 'id', 'name'),
        ]);
    }
    /**
     * Deletes an existing Meeting 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 Meeting 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->unlinkAll('meetingSurveyLocks', true);
            $model->survey->unlinkAll('surveyLocks', true);
            $model->survey->unlinkAll('surveyOpinions', 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 Meeting 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 Meeting Survey the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = MeetingSurvey::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}