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 / 60
MeetingSurveyController
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 4
182.00
0.00% covered (danger)
0.00%
0 / 60
 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
90.00
0.00% covered (danger)
0.00%
0 / 33
 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\Meeting;
use common\models\MeetingSurvey;
use common\models\MeetingSurveyLock;
use frontend\models\SurveyForm;
use yii\filters\AccessControl;
use yii\web\BadRequestHttpException;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
/**
* MeetingSurveyController implements the answer action for MeetingSurvey model.
*/
class MeetingSurveyController 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, $meeting)
    {
        $model = $this->findModel($id);
        $meeting = Meeting::findOne($meeting);
        $lockModel = new MeetingSurveyLock();
        $participant = null;
        if($meeting->hostParticipant->user->id === Yii::$app->user->id){
            $lockModel->is_host = 1;
            $participant = $meeting->hostParticipant;
        }else if($meeting->guestParticipant->user->id === Yii::$app->user->id){
            $lockModel->is_host = 0;
            $participant = $meeting->guestParticipant;
        }
        if(empty($participant) || $this->isAnswered($model, $participant, $meeting))
            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->participant_id = $participant->id;
                $lockModel->meeting_id = $meeting->id;
                $lockModel->meeting_survey_id = $model->id;
                if($surveyForm->save($lockModel)) {
                    Yii::$app->session->setFlash('success', Yii::t('app', 'Survey saved.'));
                    return $this->redirect(['meeting/index']);
                }
            }
        }
        if($surveyForm->hasErrors()) {
            Yii::$app->session->setFlash('error', $surveyForm->getFirstError('surveyModel'));
        }
        return $this->render('/survey/fill-out',[
            'survey' => $surveyForm,
        ]);
    }
    protected function isAnswered($meetingSurvey, $participant, $meeting)
    {
        return $meetingSurvey
            ->getMeetingSurveyLocks()
            ->where(['participant_id' => $participant->id, 'meeting_id' => $meeting->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 = MeetingSurvey::find()->where(['id' => $id])->with(['event', 'survey'])->one()) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}