<?php

/**
* This is the model class for table "paper_weight".
*
* The followings are the available columns in table 'paper_weight':
* @property integer $id_paper
* @property string $created_date
* @property integer $created_id
* @property string $update_date
* @property integer $update_id
* @property integer $value_title
* @property integer $value_keyword
* @property integer $value_background
* @property integer $value_goal_benefit
* @property integer $value_literature_theory
* @property integer $value_methodology
* @property integer $value_result
* @property integer $value_conclusion
* @property integer $value_bibliography
* @property integer $value_systematics
* @property integer $value_language
* @property integer $value_master_material_and_response
* @property integer $status
*/
class WeightFinal extends CActiveRecord
{
	/**
	* @return string the associated database table name
	*/
	public function tableName()
	{
		return 'paper_weight_final';
	}

	/**
	* @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_date, created_id, value_title, value_keyword, value_background', 'required'),
			array('id_paper, created_id, update_id, value_title, value_keyword, value_background, value_goal_benefit, value_literature_theory, value_methodology, value_result, value_conclusion, value_bibliography, value_systematics, value_language, value_master_material_and_response, status, value_problem_formulation', 'numerical', 'integerOnly'=>true),
			// array('value_title, value_keyword, value_background, value_goal_benefit, value_literature_theory, value_methodology, value_result, value_conclusion, value_bibliography, value_systematics, value_language, value_master_material_and_response, value_problem_formulation', 'in', 'range'=>array(1,2,3,4,5,6,7,8,9,10)),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id_paper, created_date, created_id, update_date, update_id, value_title, value_keyword, value_background, value_goal_benefit, value_literature_theory, value_methodology, value_result, value_conclusion, value_bibliography, value_systematics, value_language, value_master_material_and_response, 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(
		);
	}

	/**
	* @return array customized attribute labels (name=>label)
	*/
	public function attributeLabels()
	{
		return array(
			'id_paper' => 'Id Paper',
			'created_date' => 'Tanggal Input',
			'created_id' => 'Created',
			'update_date' => 'Tanggal Update',
			'update_id' => 'Update',
			'value_title' => 'Kesesuaian Konsep Kreatif (Bobot 40%)',
			'value_keyword' => 'Kreativitas, Orisinalitas, Keunikan (Bobot 30%)',
			'value_background' => 'Craftmanship (Visualisasi, Artistik, Keterbacaan (Bobot 30%)',
			// 'value_problem_formulation' => 'Musik (Bobot 6%)',
			// 'value_goal_benefit' => 'Penyutradaraan (Bobot 12%)',
			// 'value_literature_theory' => 'Komunikasi (Bobot 40%)',
			// 'value_methodology' => 'Bobot Metodologi Penelitian',
			// 'value_result' => 'Bobot Hasil/Pembahasan',
			// 'value_conclusion' => 'Bobot Kesimpulan Penelitian',
			// 'value_bibliography' => 'Bobot Daftar Pustaka',
			// 'value_systematics' => 'Bobot 1. Sistematika Penyajian',
			// 'value_language' => 'Bobot 2. Penggunaan Bahasa',
			// 'value_master_material_and_response' => 'Bobot 3. Penguasaan Materi dan Respon terhadap pertanyaan',
			'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_paper',$this->id_paper);
			$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('value_title',$this->value_title);
			$criteria->compare('value_keyword',$this->value_keyword);
			$criteria->compare('value_background',$this->value_background);
			$criteria->compare('value_goal_benefit',$this->value_goal_benefit);
			$criteria->compare('value_literature_theory',$this->value_literature_theory);
			$criteria->compare('value_methodology',$this->value_methodology);
			$criteria->compare('value_result',$this->value_result);
			$criteria->compare('value_conclusion',$this->value_conclusion);
			$criteria->compare('value_bibliography',$this->value_bibliography);
			$criteria->compare('value_systematics',$this->value_systematics);
			$criteria->compare('value_language',$this->value_language);
			$criteria->compare('value_master_material_and_response',$this->value_master_material_and_response);
			$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 Weight the static model class
		*/
		public static function model($className=__CLASS__)
		{
			return parent::model($className);
		}

		public static function getTotal($id, $type)
		{
			// kriteria_penguasaan
			// kriteria_sistematika
			// kriteria_tanyajawab
			// kriteria_etika

			$paper = Review::model()->findByPk($id);
			$weight = WeightFinal::model()->findByPk(1);
			if ($type == 1) {
				$weight = PaperWeightFinal::model()->findByPk(1);
				$data1 = $paper->value_satu * $weight->kriteria_penguasaan / 100;
				$data2 = $paper->value_dua * $weight->kriteria_sistematika / 100;
				$data3 = $paper->value_tiga * $weight->kriteria_tanyajawab / 100;
				$data4 = $paper->value_empat * $weight->kriteria_etika / 100;
				return $data1 + $data2 + $data3 + $data4;
			} elseif ($type == 2) {
				$data1 = $paper->value_satu;
				$data2 = $paper->value_dua;
				$data3 = $paper->value_tiga;
				$data4 = $paper->value_empat;
				return $data1 + $data2 + $data3 + $data4;
			} elseif ($type == 3) {
				$data1 = $weight->kriteria_penguasaan;
				$data2 = $weight->kriteria_sistematika;
				$data3 = $weight->kriteria_tanyajawab;
				$data4 = $weight->kriteria_etika;
				return $data1 + $data2 + $data3 + $data4;
			}
		}

		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 +=  Review::model()->getTotal($data->id_rating, 1);
			}
			if ($type == 1) {
				return $total;
			} elseif ($type == 2) {
				if ($total == 0) {
					return "";
				} else {
					if ($dataProvider->totalItemCount == 4) {
						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
			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 = PaperWeightFinal::model()->findByPk(1);
			$data1 = $paper->value_satu * $weight->kriteria_penguasaan / 100;
			$data2 = $paper->value_dua * $weight->kriteria_sistematika / 100;
			$data3 = $paper->value_tiga * $weight->kriteria_tanyajawab / 100;
			$data4 = $paper->value_empat * $weight->kriteria_etika / 100;
			return $data1 + $data2 + $data3 + $data4;
		}

	}
