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 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 73
SurveyForm
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 9
306.00
0.00% covered (danger)
0.00%
0 / 73
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 4
 init
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 17
 getSurveyModel
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 getQuestions
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 getAnswers
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 getOptions
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 load
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 validate
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 6
 save
0.00% covered (danger)
0.00%
0 / 1
56.00
0.00% covered (danger)
0.00%
0 / 31
<?php
namespace frontend\models;
use Yii;
use common\models\SurveyAnswer;
use common\models\SurveyOpinion;
use common\models\SurveyOption;
use yii\base\Model;
use yii\helpers\ArrayHelper;
use yii\web\BadRequestHttpException;
/**
 * This is the model class for form "survey_answer".
 *
 * @property string $surveyModel
 * @property string $questions
 * @property string $answers
 * @property string $options
 */
class SurveyForm extends Model
{
    private $surveyModel;
    private $questions;
    private $answers;
    private $options;
    public function __construct($surveyModel, $options = [])
    {
        $this->surveyModel = $surveyModel;
        parent::__construct($options);
    }
    public function init()
    {
        $this->questions = $this->surveyModel
            ->getSurveyQuestions()
            ->orderBy('[[on]] ASC')
            ->indexBy('id')
            ->all();
        $options = SurveyOption::find()
            ->where(['survey_question_id' => array_keys($this->questions)])
            ->orderBy('[[value]] ASC')
            ->all();
        $this->options = ArrayHelper::map($options, 'value', 'content', 'survey_question_id');
        $answers = [];
        foreach($this->questions as $question){
            $answers[] = new SurveyAnswerForm($question->id);
        }
        $this->answers = $answers;
    }
    public function getSurveyModel()
    {
        return $this->surveyModel;
    }
    public function getQuestions()
    {
        return $this->questions;
    }
    public function getAnswers()
    {
        return $this->answers;
    }
    public function getOptions()
    {
        return $this->options;
    }
    public function load($data, $formName = null)
    {
        return static::loadMultiple($this->answers, $data);
    }
    public function validate($attributeNames = null, $clearErrors = true)
    {
        if ($clearErrors) { 
            $this->clearErrors(); 
        } 
        return static::validateMultiple($this->answers, $attributeNames);
    }
    public function save($lockModel)
    {
        $transaction = Yii::$app->db->beginTransaction();
        try {
            $opinion = new SurveyOpinion();
            $opinion->survey_id = $this->surveyModel->id;
            if($opinion->save()){
                if(array_key_exists('survey_opinion_id', $lockModel->attributes)){
                    $lockModel->survey_opinion_id = $opinion->id;
                }
                if($lockModel->save()){
                    foreach ($this->answers as $answer) {
                        if(!$answer->save($opinion->id, $this->options)){
                            throw new BadRequestHttpException(Yii::t('app', 'The survey answer can not be saved.'), 3);
                        }
                    }
                    $this->surveyModel->computeAnsweredQuantity();
                    $this->surveyModel->save();
                    $transaction->commit();
                    return true;
                }else{
                    throw new BadRequestHttpException(Yii::t('app', 'The survey lock can not be saved.'), 2);
                }
            }else{
                throw new BadRequestHttpException(Yii::t('app', 'The survey opinion can not be saved.'), 1);
            }
        } catch(\Exception $e) {
            $transaction->rollBack();
            $this->addError('surveyModel', $e->getMessage());
        }
        return false;
    }
}