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 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 67
SurveyController
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 5
132.00
0.00% covered (danger)
0.00%
0 / 67
 behaviors
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 14
 actionIndex
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 16
 actionFillOut
0.00% covered (danger)
0.00%
0 / 1
42.00
0.00% covered (danger)
0.00%
0 / 24
 isAnswered
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 6
 findModel
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 7
<?php
namespace frontend\controllers;
use Yii;
use frontend\models\AccompanyingEventSurveySearch;
use frontend\models\EventSurveySearch;
use frontend\models\SurveyForm;
use frontend\models\SurveySearch;
use common\models\Survey;
use common\models\SurveyLock;
use yii\filters\AccessControl;
use yii\web\BadRequestHttpException;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
/**
* SurveyController implements the answer action for Survey model.
*/
class SurveyController extends \yii\web\Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index', 'fill-out'],
                        'allow' => true,
                        'roles' => ['user'],
                    ],
                ],
            ],
        ];
    }
    /**
     * Lists all Survey models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new SurveySearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $eventSearchModel = new EventSurveySearch();
        $eventDataProvider = $eventSearchModel->search(Yii::$app->request->queryParams);
        $accompanyingEventSearchModel = new AccompanyingEventSurveySearch();
        $accompanyingEventDataProvider = $accompanyingEventSearchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'eventSearchModel' => $eventSearchModel,
            'eventDataProvider' => $eventDataProvider,
            'accompanyingEventSearchModel' => $accompanyingEventSearchModel,
            'accompanyingEventDataProvider' => $accompanyingEventDataProvider,
        ]);
    }
    /**
    * Quickly finds a single survey which is available to fill out for current user and display it as a questions-answers list
    * If model is submited successfully, it will send back home.
    * @return mixed
    */
    public function actionFillOut($id)
    {
        $model = $this->findModel($id);
        $user = Yii::$app->user;
        if($this->isAnswered($model, $user))
            throw new ForbiddenHttpException('You do not have permition to see requested page.');
        $surveyForm = new SurveyForm($model);
        
        if($surveyForm->load(Yii::$app->request->post())) {
            if($surveyForm->validate()) {
                $lockModel = new SurveyLock();
                $lockModel->user_id = $user->id;
                $lockModel->survey_id = $model->id;
                if($surveyForm->save($lockModel)) {
                    Yii::$app->session->setFlash('success', Yii::t('app', 'Survey saved.'));
                    return $this->redirect(['survey/index']);
                }
            }
        }
        if($surveyForm->hasErrors()) {
            Yii::$app->session->setFlash('error', $surveyForm->getFirstError('surveyModel'));
        }
        return $this->render('fill-out',[
            'survey' => $surveyForm,
        ]);
    }
    protected function isAnswered($survey, $user)
    {
        return $survey
            ->getSurveyLocks()
            ->where(['user_id' => $user->id])
            ->count() != 0;
    }
    /**
    * 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.');
        }
    }
}