<commit_before><?php
/**
 * @module Users
 */
/**
 * Class representing 'Vote' rows in the 'Users' database
 * You can create an object of this class either to
 * access its non-static methods, or to actually
 * represent a vote row in the Users database.
 *
 * @class Users_Vote
 * @extends Base_Users_Vote
 */
class Users_Vote extends Base_Users_Vote
{
	/**
	 * The setUp() method is called the first time
	 * an object of this class is constructed.
	 * @method setUp
	 */
	function setUp()
	{
		parent::setUp();
	}
	/**
	 * Calculates total votes
	 * @method beforeSave
	 * @param {array} $modifiedFields
	 * @return {array}
	 */
	function beforeSave($modifiedFields)
	{
		$total = new Users_Total();
		$total->forType = $this->forType;
		$total->forId = $this->forId;
		if (!$total->retrieve(null, array("begin" => true))) {
			$total->weightTotal = 0;
			$total->voteCount = 0;
			$total->value = 0;
		}
		$total->set('transaction', true);
		
		$vote = new Users_Vote();
		$vote->userId = $modifiedFields['userId'];
		$vote->forType = $modifiedFields['forType'];
		$vote->forId = $modifiedFields['forId'];
		$weightTotal = $total->weightTotal;
		if ($vote->retrieve()) {
			if (!$total->voteCount) {
				// something is wrong
				$total->voteCount = 1;
			}
			$total->weightTotal += ($modifiedFields['weight'] - $vote->weight);
			if (!$total->weightTotal) {
				throw new Q_Exception_BadValue(array(
					'internal' => 'Users_Vote_Total table', 
					'problem' => 'weight is 0'
				));
			}
			$total->value = 
				($total->value * $weightTotal 
					- $vote->value * $vote->weight 
					+ $modifiedFields['value'] * $modifiedFields['weight'])
				/ ($total->weightTotal);
		} else {
			if (!isset($modifiedFields['weight'])) {
				$modifiedFields['weight'] = 1;
			}
			$total->weightTotal += $modifiedFields['weight'];
			$total->voteCount += 1;
			$total->value = ($total->value * $weightTotal + $modifiedFields['value'] * $modifiedFields['weight'])
				/ ($total->weightTotal);
		}
	
		$this->set('total', $total);
		return parent::beforeSave($modifiedFields);
	}
	
	/**
	 * Updates total votes in Total table
	 * @method afterSaveExecute
	 * @param {Db_Result} $result
	 * @param {Db_Query} $query
	 * @param {array} $modifiedFields
	 * @param {array} $where
	 * @return {Db_Result}
	 */
	function afterSaveExecute($result, $query, $modifiedFields, $where)
	{<commit_msg>Fixed edge case in Users/Vote.php<commit_after><?php
/**
 * @module Users
 */
/**
 * Class representing 'Vote' rows in the 'Users' database
 * You can create an object of this class either to
 * access its non-static methods, or to actually
 * represent a vote row in the Users database.
 *
 * @class Users_Vote
 * @extends Base_Users_Vote
 */
class Users_Vote extends Base_Users_Vote
{
	/**
	 * The setUp() method is called the first time
	 * an object of this class is constructed.
	 * @method setUp
	 */
	function setUp()
	{
		parent::setUp();
	}
	/**
	 * Calculates total votes
	 * @method beforeSave
	 * @param {array} $modifiedFields
	 * @return {array}
	 */
	function beforeSave($modifiedFields)
	{
		$total = new Users_Total();
		$total->forType = $this->forType;
		$total->forId = $this->forId;
		if (!$total->retrieve(null, array("begin" => true))) {
			$total->weightTotal = 0;
			$total->voteCount = 0;
			$total->value = 0;
		}
		$total->set('transaction', true);
		
		$vote = new Users_Vote();
		$vote->userId = $modifiedFields['userId'];
		$vote->forType = $modifiedFields['forType'];
		$vote->forId = $modifiedFields['forId'];
		$weightTotal = $total->weightTotal;
		if (isset($modifiedFields['weight'])
		and $vote->retrieve()) {
			$weight = $modifiedFields['weight'];
			if (!$total->voteCount) {
				// something is wrong
				$total->voteCount = 1;
			}
			$total->weightTotal += ($weight - $vote->weight);
			if (!$total->weightTotal) {
				throw new Q_Exception_BadValue(array(
					'internal' => 'Users_Vote_Total table', 
					'problem' => 'weight is 0'
				));
			}
			$total->value = 
				($total->value * $weightTotal 
					- $vote->value * $vote->weight 
					+ $modifiedFields['value'] * $weight)
				/ ($total->weightTotal);
		} else {
			if (!isset($modifiedFields['weight'])) {
				$modifiedFields['weight'] = 1;
			}
			$total->weightTotal += $modifiedFields['weight'];
			$total->voteCount += 1;
			$total->value = ($total->value * $weightTotal + $modifiedFields['value'] * $modifiedFields['weight'])
				/ ($total->weightTotal);
		}
	
		$this->set('total', $total);
		return parent::beforeSave($modifiedFields);
	}
	
	/**
	 * Updates total votes in Total table
	 * @method afterSaveExecute
	 * @param {Db_Result} $result
	 * @param {Db_Query} $query
	 * @param {array} $modifiedFields
	 * @param {array} $where
	 * @return {Db_Result}
	 */
	function afterSaveExecute($result, $query, $modifiedFields, $where)
	{
{'fraction_non_alphanumeric': 0.17208531787119485, 'fraction_numerical': 0.002899150962932284, 'mean_word_length': 3.657666345226615, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, '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': '405051', 'n_tokens_mistral': 1782, 'n_tokens_neox': 1558, 'n_words': 376}