<?php

/**
 * This is the model class for table "questionnaire_answer".
 *
 * The followings are the available columns in table 'questionnaire_answer':
 * @property integer $id
 * @property string $data
 * @property integer $question_id
 * @property integer $ip_address
 * @property string $create_datetime
 * @property integer $uniq_id
 *
 * The followings are the available model relations:
 * @property QuestionnaireQuestion $question
 */
class QuestionnaireAnswer extends CActiveRecord {

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'questionnaire_answer';
    }

    protected function beforeValidate() {

        if ($this->isNewRecord) {
            $this->create_datetime = new CDbExpression("NOW()");
        }

        return parent::beforeValidate();
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('data, question_id, ip_address, create_datetime', 'required'),
            array('question_id', 'numerical', 'integerOnly' => true),
             array('ip_address', 'length', 'max'=>200),
             array('uniq_id','safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, data, question_id, ip_address, create_datetime,ip_address', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'question' => array(self::BELONGS_TO, 'QuestionnaireQuestion', 'question_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => Yii::t('admin', 'ID'),
            'data' => Yii::t('admin', 'Data'),
            'question_id' => Yii::t('admin', 'Question'),
            'ip_address' => Yii::t('admin','Ip Address'),
            'create_datetime' => Yii::t('admin', 'Create Datetime'),
            'uniq_id' => Yii::t('admin', 'uniq_id'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        // $criteria->distinct=true;
        // $criteria->group = 'uniq_id';
        $criteria->compare('id', $this->id);
        $criteria->compare('data', $this->data, true);
        $criteria->compare('question_id', $this->question_id);
        $criteria->compare('ip_address',$this->ip_address,true);
        $criteria->compare('create_datetime', $this->create_datetime, true);
        $criteria->compare('uniq_id', $this->uniq_id, true);
        $criteria->distinct=true;
        $criteria->order='create_datetime, uniq_id, question_id ASC';
        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return QuestionnaireAnswer the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

}
