<commit_before><?php
App::uses('GivrateAppModel', 'Givrate.Model');
/**
 * Rating Model
 *
 * @property User $User
 */
class Rating extends GivrateAppModel {

	public $actsAs = array(
		'Params'
	);

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(

		'user_id' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),

		'owner_id' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),

		'model' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),

		'foreign_key' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),

		'value' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'User' => array(
			'className' => 'Users.User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

	public function checking($token, $userId, $type, $status, $ownerId, $options = array()) {
		$Token = ClassRegistry::init('Givrate.Token');
		$tokenData = $Token->findByToken($token);
		$conditions = array(
			'Rating.model' => $tokenData['Token']['model'],
			'Rating.foreign_key' => $tokenData['Token']['foreign_key'],
			'Rating.user_id' => $userId,
			'Rating.owner_id' => $ownerId,
			'Rating.type' => $type,
			'Rating.status' => $status,
		);
		if (isset($options['params'])) {
			$conditions = Set::merge($conditions, array(
				'Rating.params' => $options['params']
			));
		}
		$result = $this->find('first', array(
			'recursive' => -1,
			'conditions' => $conditions
		));
		return $result;
	}

	protected function _calculateRating($data) {
		$RateCalculate = ClassRegistry::init('Givrate.RateCalculate');
		if (!empty($data)) {
			$alias = $data['Rating']['model'];
			$foreignKey = $data['Rating']['foreign_key'];
			$value = $data['Rating']['value'];
			$type = $data['Rating']['type'];
			$status = $data['Rating']['status'];
			$RateCalculate->calculating($alias, $foreignKey, $value, $type, $status);
		}
	}

	public function rate($token, $type, $rating, $userId, $status, $ownerId, $userPoint = null, $options = array()) {
		$rated = $this->checking($token, $userId, $type, $status, $ownerId, $options);
		if ($rated) {
			return false;
		}
		$Token = ClassRegistry::init('Givrate.Token');
		$tokenData = $Token->findByToken($token);
		$data = array(
			'user_id' => $userId,
			'owner_id' => $ownerId,
			'model' => $tokenData['Token']['model'],
			'foreign_key' => $tokenData['Token']['foreign_key'],
			'value' => $rating,
			'type' => $type,
			'status' => $status,
		);
		if ($this->save($data)) {
			if ($userPoint != null) {
				$UserPoint = ClassRegistry::init('Givrate.UserPoint');
				$UserPoint->countMyPoint($ownerId, $rating, $type, $status);
			}
			return true;
		} else {
			return false;
		}
	}

	public function afterSave($created) {
		if ($created) {
			$this->_calculateRating($this->data);
		}
	}
}
<commit_msg>adjust rate function for checking params<commit_after><?php
App::uses('GivrateAppModel', 'Givrate.Model');
/**
 * Rating Model
 *
 * @property User $User
 */
class Rating extends GivrateAppModel {

	public $actsAs = array(
		'Params'
	);

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(

		'user_id' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),

		'owner_id' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),

		'model' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),

		'foreign_key' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),

		'value' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'User' => array(
			'className' => 'Users.User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

	public function checking($token, $userId, $type, $status, $ownerId, $options = array()) {
		$Token = ClassRegistry::init('Givrate.Token');
		$tokenData = $Token->findByToken($token);
		$conditions = array(
			'Rating.model' => $tokenData['Token']['model'],
			'Rating.foreign_key' => $tokenData['Token']['foreign_key'],
			'Rating.user_id' => $userId,
			'Rating.owner_id' => $ownerId,
			'Rating.type' => $type,
			'Rating.status' => $status,
		);
		if (isset($options['params'])) {
			$conditions = Set::merge($conditions, array(
				'Rating.params' => $options['params']
			));
		}
		$result = $this->find('first', array(
			'recursive' => -1,
			'conditions' => $conditions
		));
		return $result;
	}

	protected function _calculateRating($data) {
		$RateCalculate = ClassRegistry::init('Givrate.RateCalculate');
		if (!empty($data)) {
			$alias = $data['Rating']['model'];
			$foreignKey = $data['Rating']['foreign_key'];
			$value = $data['Rating']['value'];
			$type = $data['Rating']['type'];
			$status = $data['Rating']['status'];
			$RateCalculate->calculating($alias, $foreignKey, $value, $type, $status);
		}
	}

	public function rate($token, $type, $rating, $userId, $status, $ownerId, $userPoint = null, $options = array()) {
		$rated = $this->checking($token, $userId, $type, $status, $ownerId, $options);
		if (!empty($rated)) {
			return false;
		}
		$Token = ClassRegistry::init('Givrate.Token');
		$tokenData = $Token->findByToken($token);
		$data = array(
			'user_id' => $userId,
			'owner_id' => $ownerId,
			'model' => $tokenData['Token']['model'],
			'foreign_key' => $tokenData['Token']['foreign_key'],
			'value' => $rating,
			'type' => $type,
			'status' => $status,
		);
		if (isset($options['params'])) {
			$data = Set::merge($data, array('params' => $options['params']));
		}
		if ($this->save($data)) {
			if ($userPoint == true) {
				$UserPoint = ClassRegistry::init('Givrate.UserPoint');
				$UserPoint->countMyPoint($ownerId, $rating, $type, $status);
			}
			return true;
		} else {
			return false;
		}
	}

	public function afterSave($created) {
		if ($created) {
			$this->_calculateRating($this->data);
		}
	}
}

{'fraction_non_alphanumeric': 0.21008307727324457, 'fraction_numerical': 0.00022760896779333106, 'mean_word_length': 3.449620253164557, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 146, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6766084', 'n_tokens_mistral': 3438, 'n_tokens_neox': 2907, 'n_words': 695}