<?php

class QuestionnaireController extends Controller {

    /**
     * Displays a particular model.
     * @param integer $id the ID of the model to be displayed
     */
    public function actionView($id) {

        $model = $this->loadModel($id);

        if (isset($_POST['QuestionnaireQuestion']) && count($_POST['QuestionnaireQuestion'])) {

            $allowing_multiple_participations = (bool) Yii::app()->options->get('allowing_multiple_participations');
            if ($allowing_multiple_participations === false && isset(Yii::app()->request->cookies["questionnaire_{$id}"])) {
                Yii::app()->user->setFlash('danger', 'You have already answered this questionnaire!.');
            } else {

                $answers = $_POST['QuestionnaireQuestion'];

                if ($model->questionnaireQuestions && count($model->questionnaireQuestions)) {

                    $answers_result = true;

                    foreach ($answers as $question_id => $question_answer) {

                        $answer = new QuestionnaireAnswer();
                        $answer->question_id = $question_id;
                        $answer->data = CJSON::encode($question_answer);
                        $ip_address = Yii::app()->request->getUserHostAddress();

                        $answer->ip_address = ip2long($ip_address == "::1" ? '127.0.0.1' : $ip_address);

                        $answers_result = $answers_result && $answer->save();
                    }

                    if ($answers_result) {
                        // write the cookies;
                        Yii::app()->request->cookies["questionnaire_{$id}"] = new CHttpCookie("questionnaire_{$id}", true);
                        Yii::app()->user->setFlash('success', 'You have successfully answred this questionnaire!.');
                        $this->redirect(array('view', 'id' => $id));
                    } else {
                        Yii::app()->user->setFlash('danger', 'Something went wrong!.');
                    }
                }
            }
        }


        $this->render('view', array(
            'model' => $model,
        ));
    }

    /**
     * Lists all models.
     */
    public function actionIndex() {
        $criteria = new CDbCriteria;

        $criteria->condition = "t.end_datetime is NULL OR t.end_datetime > NOW()";
        $criteria->with = 'questionsCount';

        $dataProvider = new CActiveDataProvider('Questionnaire', array('criteria' => $criteria));
        $this->render('index', array(
            'dataProvider' => $dataProvider
        ));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer $id the ID of the model to be loaded
     * @return Questionnaire the loaded model
     * @throws CHttpException
     */
    public function loadModel($id) {
        $model = Questionnaire::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, '');
        return $model;
    }

}
