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 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 52
AccompanyingEventSurveyController
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 4
132.00
0.00% covered (danger)
0.00%
0 / 52
 behaviors
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 14
 actionFillOut
0.00% covered (danger)
0.00%
0 / 1
56.00
0.00% covered (danger)
0.00%
0 / 25
 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 common\models\AccompanyingEventSurvey;
use common\models\AccompanyingEventSurveyLock;
use frontend\models\SurveyForm;
use yii\filters\AccessControl;
use yii\web\BadRequestHttpException;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
/**
* AccompanyingEventSurveyController implements the answer action for AccompanyingEventSurvey model.
*/
class AccompanyingEventSurveyController extends \yii\web\Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['fill-out'],
                        'allow' => true,
                        'roles' => ['user'],
                    ],
                ],
            ],
        ];
    }
    /**
    * 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);
        $accompanyingEvent = $model->accompanyingEvent;
        $participant = $accompanyingEvent->getParticipants()->where(['user_id' => Yii::$app->user->id])->one();
        if(empty($participant) || $this->isAnswered($model, $participant))
            throw new ForbiddenHttpException('You do not have permition to see requested page.');
        $surveyForm = new SurveyForm($model->survey);
        
        if($surveyForm->load(Yii::$app->request->post())) {
            if($surveyForm->validate()) {
                $lockModel = new AccompanyingEventSurveyLock();
                $lockModel->participant_id = $participant->id;
                $lockModel->accompanying_event_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('/survey/fill-out',[
            'survey' => $surveyForm,
        ]);
    }
    protected function isAnswered($accompanyingEventSurvey, $participant)
    {
        return $accompanyingEventSurvey
            ->getAccompanyingEventSurveyLocks()
            ->where(['participant_id' => $participant->id])
            ->count() != 0;
    }
    /**
    * 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::find()->where(['id' => $id])->with(['accompanyingEvent', 'survey'])->one()) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}