<?php

/**
 * This is the model class for table "questionnaire".
 *
 * The followings are the available columns in table 'questionnaire':
 * @property integer $id
 * @property string $title
 * @property string $intro
 * @property string $create_datetime
 * @property string $update_datetime
 * @property string $publish_datetime
 * @property string $end_datetime
 * @property integer $qorder
 * @property integer $featured
 * @property string $title_en
 * @property string $intro_en
 * 
 * The followings are the available model relations:
 * @property QuestionnaireQuestion[] $questionnaireQuestions
 */
class Questionnaire extends CActiveRecord {

    public $verifyCode;

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

    /**
     * @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('title, title_en, create_datetime, update_datetime, publish_datetime', 'required'),
            array('qorder, featured', 'numerical', 'integerOnly'=>true),
            array('title, title_en', 'length', 'max'=>500),
            array('intro, intro_en', 'safe'),
            array(
                'end_datetime',
                'compare',
                'compareAttribute' => 'publish_datetime',
                'operator' => '>',
                'allowEmpty' => true,
                'message' => '{attribute} must be greater than "{compareAttribute}".'
            ),
            // array('verifyCode','safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, title, intro, create_datetime, update_datetime, publish_datetime, qorder, featured, title_en, intro_en', 'safe', 'on' => 'search'),
        );
    }

    protected function beforeValidate() {

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

        return parent::beforeValidate();
    }
    /////////////////////////////////////
   /* public function validateCaptcha($key)
    {
        // echo '123'.$key;
        // echo '<br>';
        // echo $key;
        $validator = new CCaptchaValidator();
                        $validator->attributes = array($key);
                        $captch_result= $validator->validate($this);
                        var_dump($captch_result);
    }*/
////////////////////////////////////////////////////////
    /**
     * @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(
            'questionnaireQuestions' => array(self::HAS_MANY, 'QuestionnaireQuestion', 'questionnaire_id'),
            'questionsCount' => array(self::STAT, 'QuestionnaireQuestion', 'questionnaire_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => Yii::t('admin', 'ID'),
            'title' => Yii::t('admin', 'Title'),
            'intro' => Yii::t('admin', 'Intro'),
            'create_datetime' => Yii::t('admin', 'Create Datetime'),
            'update_datetime' => Yii::t('admin', 'Update Datetime'),
            'publish_datetime' => Yii::t('admin', 'Publish Datetime'),
            'end_datetime' => Yii::t('admin', 'End Datetime'),
            'qorder' => Yii::t('admin','Qorder'),

            'title_en' => Yii::t('admin','Title En'),
            'intro_en' => Yii::t('admin','Intro En'),
        );
    }

    /**
     * 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->compare('id', $this->id);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('intro', $this->intro, true);
        $criteria->compare('create_datetime', $this->create_datetime, true);
        $criteria->compare('update_datetime', $this->update_datetime, true);
        $criteria->compare('publish_datetime', $this->publish_datetime, true);
        $criteria->compare('end_datetime', $this->end_datetime, true);
        $criteria->compare('qorder',$this->qorder);
        $criteria->compare('title_en',$this->title_en,true);
        $criteria->compare('intro_en',$this->intro_en,true);

        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 Questionnaire the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

}
