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 / 84
SurveyQuestionController
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 8
272.00
0.00% covered (danger)
0.00%
0 / 84
 behaviors
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 20
 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
20.00
0.00% covered (danger)
0.00%
0 / 16
 actionUpdate
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 13
 actionDelete
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 8
 actionMoveUp
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 5
 actionMoveDown
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 5
 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 backend\models\SurveyOptionSearch;
use common\models\Survey;
use common\models\SurveyQuestion;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
/**
 * SurveyQuestionController implements the CRUD actions for SurveyQuestion model.
 */
class SurveyQuestionController extends \yii\web\Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['view', 'create', 'update', 'delete', 'move-up', 'move-down'],
                        'allow' => true,
                        'roles' => ['supervisor', 'admin'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }
    /**
     * Displays a single SurveyQuestion model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        $model = $this->findModel($id);
        
        $searchModel = new SurveyOptionSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $model->getSurveyOptions());
        return $this->render('view', [
            'model' => $model,
            'optionsSearchModel' => $searchModel,
            'optionsDataProvider' => $dataProvider,
        ]);
    }
    /**
     * Creates a new SurveyQuestion model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @param string $survey
     * @return mixed
     */
    public function actionCreate($survey)
    {
        if (Survey::findOne($survey)->answered_quantity > 0)
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        $model = new SurveyQuestion();
        $model->survey_id = $survey;
        if ($model->load(Yii::$app->request->post())) {
            $model->survey_id = $survey;
            if ($model->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            }
        }
        return $this->render('create', [
            'model' => $model,
            'types' => $model->typeLabels(),
        ]);
    }
    /**
     * Updates an existing SurveyQuestion 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->survey->answered_quantity > 0)
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
                'types' => $model->typeLabels(),
            ]);
        }
    }
    /**
     * Deletes an existing SurveyQuestion model.
     * If deletion is successful, the browser will be redirected to the '/survey/view' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        if ($model->survey->answered_quantity > 0)
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        $model->unlinkAll('surveyOptions', true);
        $model->delete();
        return $this->redirect(['/survey/view', 'id' => $model->survey_id]);
    }
    /**
     * Deletes an existing SurveyQuestion model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionMoveUp($id)
    {
        $model = $this->findModel($id);
        $model->moveUpOn();
        return $this->redirect(['/survey/view', 'id' => $model->survey_id]);
    }
    /**
     * Deletes an existing SurveyQuestion model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionMoveDown($id)
    {
        $model = $this->findModel($id);
        $model->moveDownOn();
        return $this->redirect(['/survey/view', 'id' => $model->survey_id]);
    }
    /**
     * Finds the SurveyQuestion model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return SurveyQuestion the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = SurveyQuestion::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}