
<?php

/**
 * This is the model class for table "paper_rating".
 *
 * The followings are the available columns in table 'paper_rating':
 * @property integer $id_rating
 * @property string $created_date
 * @property integer $created_id
 * @property string $update_date
 * @property integer $update_id
 * @property integer $evaluator_id
 * @property integer $team_id
 * @property integer $paper_id
 * @property integer $value_satu
 * @property integer $value_dua
 * @property integer $value_tiga
 * @property integer $value_empat
 * @property integer $value_lima
 * @property integer $value_enam
 * @property integer $value_tujuh
 * @property integer $value_delapan
 * @property integer $value_sembilan
 * @property integer $value_sepuluh
 * @property integer $value_sebelas
 * @property integer $value_duabelas
 * @property integer $value_tigabelas
 * @property integer $status
 */
class Review extends CActiveRecord
{
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
    	return 'paper_rating';
    }

    /**
     * @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('created_id, update_id, evaluator_id, team_id, paper_id, value_satu, value_dua, value_tiga, value_empat, value_lima, value_enam, value_tujuh, value_delapan, value_sembilan, value_sepuluh, value_sebelas, value_duabelas, value_tigabelas, status', 'numerical', 'integerOnly'=>true),
    		array('created_date, update_date', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
    		array('id_rating, created_date, created_id, update_date, update_id, evaluator_id, team_id, paper_id, value_satu, value_dua, value_tiga, value_empat, value_lima, value_enam, value_tujuh, value_delapan, value_sembilan, value_sepuluh, value_sebelas, value_duabelas, value_tigabelas, status', '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(
    		'Evaluator' => array(self::BELONGS_TO, 'Users', 'evaluator_id'),
    		'Paper' => array(self::BELONGS_TO, 'Paper', 'paper_id'),
    	);
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
    	return array(
    		'id_rating' => 'Id Rating',
    		'created_date' => 'Created Date',
    		'created_id' => 'Created',
    		'update_date' => 'Update Date',
    		'update_id' => 'Update',
    		'evaluator_id' => 'Evaluator',
    		'team_id' => 'Team',
    		'paper_id' => 'Paper',
    		'value_satu' => 'Value Satu',
    		'value_dua' => 'Value Dua',
    		'value_tiga' => 'Value Tiga',
    		'value_empat' => 'Value Empat',
    		'value_lima' => 'Value Lima',
    		'value_enam' => 'Value Enam',
    		'value_tujuh' => 'Value Tujuh',
    		'value_delapan' => 'Value Delapan',
    		'value_sembilan' => 'Value Sembilan',
    		'value_sepuluh' => 'Value Sepuluh',
    		'value_sebelas' => 'Value Sebelas',
    		'value_duabelas' => 'Value Duabelas',
    		'value_tigabelas' => 'Value Tigabelas',
    		'status' => 'Status',
    	);
    }

    /**
     * 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_rating',$this->id_rating);
    	$criteria->compare('created_date',$this->created_date,true);
    	$criteria->compare('created_id',$this->created_id);
    	$criteria->compare('update_date',$this->update_date,true);
    	$criteria->compare('update_id',$this->update_id);
    	$criteria->compare('evaluator_id',$this->evaluator_id);
    	$criteria->compare('team_id',$this->team_id);
    	$criteria->compare('paper_id',$this->paper_id);
    	$criteria->compare('value_satu',$this->value_satu);
    	$criteria->compare('value_dua',$this->value_dua);
    	$criteria->compare('value_tiga',$this->value_tiga);
    	$criteria->compare('value_empat',$this->value_empat);
    	$criteria->compare('value_lima',$this->value_lima);
    	$criteria->compare('value_enam',$this->value_enam);
    	$criteria->compare('value_tujuh',$this->value_tujuh);
    	$criteria->compare('value_delapan',$this->value_delapan);
    	$criteria->compare('value_sembilan',$this->value_sembilan);
    	$criteria->compare('value_sepuluh',$this->value_sepuluh);
    	$criteria->compare('value_sebelas',$this->value_sebelas);
    	$criteria->compare('value_duabelas',$this->value_duabelas);
    	$criteria->compare('value_tigabelas',$this->value_tigabelas);
    	$criteria->compare('status',$this->status);

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

    public static function getTotal($id, $type)
    {
    	$paper = Review::model()->findByPk($id);
    	$weight = Weight::model()->findByPk(1);
    	if ($type == 1) {
    		$weight = PaperWeightUmum::model()->findByPk(1);
    		$data1 = $paper->value_satu * $weight->kriteria_tema / 100;
    		$data2 = $paper->value_dua * $weight->kriteria_penulisan / 100;
    		$data3 = $paper->value_tiga * $weight->kriteria_inovasi / 100;
    		$data4 = $paper->value_empat * $weight->kriteria_masalah / 100;
    		$data5 = $paper->value_lima * $weight->kriteria_rekomendasi_kebijakan / 100;
    		return $data1 + $data2 + $data3 + $data4 + $data5;
    	} elseif ($type == 2) {
    		$data1 = $paper->value_satu;
    		$data2 = $paper->value_dua;
    		$data3 = $paper->value_tiga;
    		$data4 = $paper->value_empat;
    		$data5 = $paper->value_lima;
    		return $data1 + $data2 + $data3 + $data4 + $data5;
    	} elseif ($type == 3) {
    		$data1 = $weight->kriteria_tema;
    		$data2 = $weight->kriteria_penulisan;
    		$data3 = $weight->kriteria_inovasi;
    		$data4 = $weight->kriteria_masalah;
    		$data5 = $weight->kriteria_rekomendasi_kebijakan;
    		return $data1 + $data2 + $data3 + $data4 + $data5;
    	}
    }

    public static function getTotalTeam($id, $type)
    {
    	$dataProvider = new CActiveDataProvider('Review', array('criteria' => array('condition' => 'team_id=' . $id)));
    	$total = 0;
    	foreach ($dataProvider->getData() as $data) {
    		$total +=  $data->value_total;
    	}
    	if ($type == 1) {
    		return $total;
    	} elseif ($type == 2) {
    		if ($total == 0) {
    			return "-";
    		} else {
    			if ($dataProvider->totalItemCount == 3) {
    				return round(@($total / $dataProvider->totalItemCount), 2);
    			} else {
    				return "-";
    			}
    		}
    	} elseif ($type == 3) {
    		return $dataProvider->totalItemCount;
    	}
    }

    public static function getTotalList($id, $type)
    {
    	$dataProvider = new CActiveDataProvider('Review', array('criteria' => array('condition' => 'paper_id=' . $id)));
    	$total = 0;
    	foreach ($dataProvider->getData() as $data) {
    		$total +=  Review::model()->getTotal($data->id_rating, 1);
    	}
    	if ($type == 1) {
    		return $total;
    	} elseif ($type == 2) {
    		if ($total == 0) {
    			return 0;
    		} else {
    			return round(@($total / $dataProvider->totalItemCount), 2);
    		}
    	} elseif ($type == 3) {
    		return $dataProvider->totalItemCount;
    	}
    }

    public static function getReview($type)
    {
    	if ($type == 1) {
    		$where = "where a.paper_file!=''";
    	} elseif ($type == 2) {
    		$where = "where c.type = '1' AND a.paper_file!=''";
    	} elseif ($type == 3) {
    		$where = "where c.type = '2' AND a.paper_file!=''";
    	}
    	$sql = "SELECT
    	c.id_user,
    	c.fullname,
    	c.type,
    	a.id_paper,
    	a.paper_name,
    	b.id_rating,
    	b.value_satu,
    	b.value_dua,
    	b.value_tiga,
    	b.value_empat,
    	b.value_lima
    	FROM
    	paper AS a
    	LEFT JOIN paper_rating AS b ON a.id_paper = b.paper_id
    	LEFT JOIN user AS c ON a.researchers_id = c.id_user
    	" . $where . " GROUP BY c.id_user";
    	$command = Yii::app()->db->createCommand($sql);
    	return $command->queryAll();
    }

    public static function getEvaluator($id)
    {
    	$sql = "SELECT
    	b.id_user,
    	b.fullname,
    	b.description,
    	a.paper_id
    	FROM
    	paper_rating AS a
    	LEFT JOIN user AS b ON b.id_user = a.evaluator_id
    	WHERE a.paper_id=" . $id;
    	$command = Yii::app()->db->createCommand($sql);
    	$dataProvider = $command->queryAll();
    	foreach ($dataProvider as $data) {
    		echo $data['fullname'] . " ( " . Review::model()->getValueEvaluator($data['paper_id'], $data['id_user']) . " ) " . ", ";
    	}
            // return $command->queryAll();
    }

    public static function getValueEvaluator($paper, $evaluator)
    {
    	$paper = Review::model()->findByAttributes(array('paper_id' => $paper, 'evaluator_id' => $evaluator));
    	$weight = PaperWeightUmum::model()->findByPk(1);
    	$data1 = $paper->value_satu * $weight->kriteria_tema / 100;
    	$data2 = $paper->value_dua * $weight->kriteria_penulisan / 100;
    	$data3 = $paper->value_tiga * $weight->kriteria_inovasi / 100;
    	$data4 = $paper->value_empat * $weight->kriteria_masalah / 100;
    	$data5 = $paper->value_lima * $weight->kriteria_rekomendasi_kebijakan / 100;
    	return $data1 + $data2 + $data3 + $data4 + $data5;
    }

}