<?php
/*
+--------------------------------------------------------------------------
|   TanyaCMS - Question and Answer Sofware
|   -----------------------------------------------------------------------
|   by fbschool dev team
|   (c) 2011 - 2012 FBSCHOOL
|   http://www.fbschool.com/tanyacms
|   -----------------------------------------------------------------------
|   This is NOT a freeware, use is subject to license terms
|   Support: 13455n@gmail.com
+--------------------------------------------------------------------------
*/


if (!defined('IN_TANYACMS'))
{
	die;
}

class answer_class extends ASK_MODEL
{
	public function get_answer_by_id($answer_id)
	{
		static $answers;
		
		if ($answers[$answer_id])
		{
			return $answers[$answer_id];
		}
		
		$answers[$answer_id] = $this->fetch_row('answer', 'answer_id = ' . intval($answer_id));
		
		return $answers[$answer_id];
	}
	
	public function get_answers_by_ids($answer_ids)
	{
		if (!is_array($answer_ids))
		{
			return false;
		}
		
		if ($answers = $this->fetch_all('answer', "answer_id IN (" . implode(', ', $answer_ids) . ")"))
		{
			foreach ($answers AS $key => $val)
			{
				$result[$val['answer_id']] = $val;
			}
		}
		
		return $result;
	}

	public function get_answer_count_by_question_id($question_id, $where = null)
	{
		if ($where)
		{
			$where = ' AND ' . $where;
		}
		
		return $this->count('answer', "question_id = " . $question_id . $where);
	}

	/**
	 * 
	 * According to issue ID to receive replies
	 * @param array    $question_id Issue ID
	 * @param string $limit By the relevant number
	 * 
	 * @return array
	 */
	public function get_answer_list_by_question_id($question_id, $limit = 20, $where = null, $order_by = null)
	{
		if (is_array($question_id))
		{
			array_walk_recursive($question_id, 'intval_string');
			
			$question_id = implode(',', $question_id);
		}
		else
		{
			$question_id = intval($question_id);
		}
		
		$sql = "SELECT answer.*, users.user_name, users.verified, users.url_token, attrib.* FROM " . $this->get_table('answer') . " AS answer LEFT JOIN " . $this->get_table('users') . " AS users ON answer.uid = users.uid LEFT JOIN " . $this->get_table('users_attrib') . " AS attrib ON answer.uid = attrib.uid WHERE answer.question_id IN(" . $question_id . ")";
		
		if ($where)
		{
			$sql = $sql . ' ' . $where;
		}
		
		if (!$order_by)
		{
			$sql .= " ORDER BY answer_id DESC";
		}
		else
		{
			$sql .= " ORDER BY " . $order_by;
		}
		
		$answer_list = $this->query_all($sql, $limit);
		
		if ($answer_list)
		{
			foreach($answer_list as $key => $val)
			{
				if (!$val['url_token'])
				{
					$answer_list[$key]['url_token'] = urlencode($val['user_name']);
				}
			}
		}
		
		return $answer_list;
	}

	/**
	 * 
	 * According response to question ID, users get the vote
	 * @param int $answer_id
	 * 
	 * @return array
	 */
	public function get_vote_user_by_answer_id($answer_id)
	{
		if (!$answer_id)
		{
			return array();
		}
		
		$sql = "SELECT vote_uid FROM " . $this->get_table('answer_vote') . " WHERE answer_id = " . intval($answer_id) . " AND vote_value = 1";
		
		if ($users = $this->query_all($sql))
		{
			foreach ($users as $key => $val)
			{
				$vote_users_ids[] = $val['vote_uid'];
			}
			
			$vote_users_info = $this->model('account')->get_user_info_by_uids($vote_users_ids);
			
			foreach ($users as $key => $val)
			{				
				$data[$val['vote_uid']] = $vote_users_info[$val['vote_uid']]['user_name'];
			}
		}
		
		return $data;
	}

	public function get_vote_user_by_answer_ids($answer_ids)
	{
		if (!is_array($answer_ids))
		{
			return array();
		}
		
		array_walk_recursive($answer_ids, 'intval_string');
	
		$sql = "SELECT vote_uid, answer_id FROM " . $this->get_table('answer_vote') . " WHERE answer_id IN(" . implode(',', $answer_ids) . ") AND vote_value = 1";
	
		if ($users = $this->query_all($sql))
		{
			foreach ($users as $key => $val)
			{
				$vote_users_ids[] = $val['vote_uid'];
			}
			
			$vote_users_info = $this->model('account')->get_user_info_by_uids($vote_users_ids);
			
			foreach ($users as $key => $val)
			{				
				$data[$val['answer_id']][$val['vote_uid']] = $vote_users_info[$val['vote_uid']];
			}
		}
	
		return $data;
	}	

	public function get_vote_agree_by_answer_ids($answer_ids)
	{
		if (!is_array($answer_ids))
		{
			return array();
		}
		
		array_walk_recursive($answer_ids, 'intval_string');
		
		if ($users = $this->fetch_all('answer_vote', "answer_id IN(" . implode(',', $answer_ids) . ") AND vote_value = 1"))
		{				
			foreach ($users as $key => $val)
			{
				$data[$val['answer_id']] = $val;				
			}
		}
		
		return $data;
	}	
	
	/**
	 *
	 * According response to question ID, users get the opposition
	 * @param int $answer_id
	 *
	 * @return array
	 */	
	public function get_vote_against_user_by_answer_id($answer_id)
	{
		if (!$answer_id)
		{
			return array();
		}
		
		$sql = "SELECT vote_uid FROM " . $this->get_table('answer_vote') . " WHERE answer_id = " . intval($answer_id) . " AND vote_value = -1";
		
		if ($users = $this->query_all($sql))
		{
			foreach ($users as $key => $val)
			{
				$vote_users_ids[] = $val['vote_uid'];
			}
			
			$vote_users_info = $this->model('account')->get_user_info_by_uids($vote_users_ids);
			
			foreach ($users as $key => $val)
			{				
				$data[$val['vote_uid']] = $vote_users_info[$val['vote_uid']]['user_name'];
			}
		}
		
		return $data;
	}	
	
	/**
	 *
	 * According response to question ID, users get the opposition
	 * @param int $answer_id
	 *
	 * @return array
	 */
	public function get_vote_against_user_by_answer_ids($answer_ids)
	{
		if (!is_array($answer_ids))
		{
			return array();
		}
		
		array_walk_recursive($answer_ids, 'intval_string');
	
		$sql = "SELECT vote_uid, answer_id FROM " . $this->get_table('answer_vote') . " WHERE answer_id IN(" . implode(',', $answer_ids) . ") AND vote_value = -1";
	
		if ($users = $this->query_all($sql))
		{
			foreach ($users as $key => $val)
			{
				$vote_users_ids[] = $val['vote_uid'];
			}
			
			$vote_users_info = $this->model('account')->get_user_info_by_uids($vote_users_ids);
			
			foreach ($users as $key => $val)
			{				
				$data[$val['answer_id']][$val['vote_uid']] = $vote_users_info[$val['vote_uid']]['user_name'];
			}
		}
	
		return $data;
	}
	
	public function get_vote_against_by_answer_ids($answer_ids)
	{
		if (!is_array($answer_ids))
		{
			return array();
		}
		
		array_walk_recursive($answer_ids, 'intval_string');
		
		if ($users = $this->fetch_all('answer_vote', "answer_id IN(" . implode(',',$answer_ids) . ") AND vote_value = -1"))
		{
			foreach ($users as $key => $val)
			{
				$data[$val['answer_id']] = $val;				
			}
		}
	
		return $data;
	}
		
	/**
	 * 
	 * Preservation Reply content
	 */
	public function save_answer($question_id, $answer_content, $uid, $anonymous = 0, $ip_address = null)
	{
		if (!$question_info = $this->model('question')->get_question_info_by_id($question_id))
		{
			return false;
		}
		
		if (!$ip_address)
		{
			$ip_address = fetch_ip();
		}
		
		if (!$answer_id = $this->insert('answer', array(
			'question_id' => $question_info['question_id'], 
			'answer_content' => htmlspecialchars($answer_content), 
			'add_time' => time(), 
			'uid' => intval($uid),
			'category_id' => $question_info['category_id'],
			'anonymous' => intval($anonymous),
			'ip' => ip2long($ip_address)
		)))
		{
			return false;
		}
		
		// Last time update problems
		$this->update('question', array(
			'update_time' => time(),
		), "question_id = " . intval($question_id));
		
		// Update Issues Number of replies volume, and time
		if ($answer_id)
		{
			// Reply count update problems
			$this->model('question')->update_answer_count($question_id);
			$this->model('question')->update_answer_users_count($question_id);
			
			// Update User response counts
			$this->model('account')->increase_user_statistics(account_class::ANSWER_COUNT, -1, $uid);
		}
		
		return $answer_id;
	}

	/**
	 * 
	 * Reply content update problems
	 */
	public function update_answer($answer_id, $question_id, $answer_content, $attach_access_key)
	{
		$answer_id = intval($answer_id);
		$question_id = intval($question_id);
		
		if (!$answer_id OR !$question_id)
		{
			return false;
		}
		
		$data = array(
			'answer_content' => htmlspecialchars($answer_content)
		);
		
		// Last time update problems		
		$this->update('question', array(
			'update_time' => time(),
		), 'question_id = ' . intval($question_id));
		
		if ($attach_access_key)
		{
			$this->model('publish')->update_attach('answer', $answer_id, $attach_access_key);
		}
		
		return $this->update('answer', $data, 'answer_id = ' . intval($answer_id));
	}
	
	public function update_answer_by_id($answer_id, $answer_info)
	{
		return $this->update('answer', $answer_info, 'answer_id = ' . intval($answer_id));
	}

	/**
	 * 
	 * Reply Vote
	 * @param int $answer_id   //Reply id
	 * @param int $question_id //Issue ID
	 * @param int $vote_value  //-1 Against 1 Agree
	 * @param int $uid         //User ID
	 * 
	 * @return boolean true|false
	 */
	public function change_answer_vote($answer_id, $vote_value = 1, $uid = 0,$reputation_factor=0)
	{
		$answer_id = intval($answer_id);
		
		$uid = (intval($uid) == 0) ? USER::get_client_uid() : $uid;
		
		if ($answer_id == 0)
		{
			return false;
		}
		
		if (! in_array($vote_value, array(
			- 1, 
			0, 
			1
		)))
		{
			return false;
		}
		
		$answer_info = $this->get_answer_by_id($answer_id);
		
		$question_id = $answer_info['question_id'];
		$answer_uid = $answer_info['uid'];
		
		$vote_info = $this->get_answer_vote_status($answer_id, $uid);
		
		if (empty($vote_info)) //Add Record
		{
			$data = array(
				"answer_id" => $answer_id, 
				"answer_uid" => $answer_uid, 
				"vote_uid" => $uid, 
				"add_time" => time(), 
				"vote_value" => $vote_value,
				"reputation_factor"=>$reputation_factor
			);
			
			$this->insert('answer_vote', $data);
			
			if ($vote_value == 1)
			{
				ACTION_LOG::save_action($uid, $question_id, ACTION_LOG::CATEGORY_QUESTION, ACTION_LOG::ADD_AGREE, '', $answer_id);
			}
			else if ($vote_value == -1)
			{
				ACTION_LOG::save_action($uid, $question_id, ACTION_LOG::CATEGORY_QUESTION, ACTION_LOG::ADD_AGANIST, '', $answer_id);
			}
			
		}
		else if ($vote_info['vote_value'] == $vote_value)	//Click again, delete the poll
		{
			$this->delete_answer_vote($vote_info['voter_id']);
				
			if ($vote_value == 1)
			{
				ACTION_LOG::save_action($uid, $question_id, ACTION_LOG::CATEGORY_QUESTION, ACTION_LOG::DEL_AGREE, '', $answer_id);
			}
			else if ($vote_value == -1)
			{
				ACTION_LOG::save_action($uid, $question_id, ACTION_LOG::CATEGORY_QUESTION, ACTION_LOG::DEL_AGANIST, '', $answer_id);
			}
		}
		else
		{
			$this->set_answer_vote_status($vote_info['voter_id'], $vote_value);
			
			if ($vote_value == 1)
			{
				ACTION_LOG::save_action($uid, $question_id, ACTION_LOG::CATEGORY_QUESTION, ACTION_LOG::ADD_AGREE, '', $answer_id);
			}
			else if ($vote_value == - 1)
			{
				ACTION_LOG::save_action($uid, $question_id, ACTION_LOG::CATEGORY_QUESTION, ACTION_LOG::ADD_AGANIST, '', $answer_id);
			}
		}
		
		if ($vote_value == 1 AND $vote_info['vote_value'] != 1 AND $answer_info['uid'] != $uid)
		{
			$this->model('notify')->send($uid, $answer_info['uid'], notify_class::TYPE_ANSWER_AGREE, notify_class::CATEGORY_QUESTION, $question_id, array(
				'from_uid' => $uid,
				'question_id' => $question_id,
				'item_id' => $answer_id,
			));
		}
		
		$this->update_vote_count($answer_id, 'against');
		$this->update_vote_count($answer_id, 'agree');
		
		$this->update_question_vote_count($question_id);
		
		//Updated reply to author by Agree
		$this->model('account')->update_users_fields(array('agree_count' => $this->user_agree_count($answer_uid)), $answer_uid);
		
		return true;
	}
	
	
	public function user_agree_count($uid)
	{
		$uid = intval($uid);
		
		if ($uid == 0)
		{
			return false;
		}
		
		return $this->count('answer_vote', 'vote_value = 1 AND answer_uid = ' . $uid);
	}

	/**
	 * Delete Reply Vote
	 * Enter description here ...
	 * @param unknown_type $voter_id
	 */
	public function delete_answer_vote($voter_id)
	{
		return $this->delete('answer_vote', "voter_id = " . intval($voter_id));
	}

	public function update_vote_count($answer_id, $type)
	{
		if (! in_array($type, array(
			'against', 
			'agree'
		)))
		{
			return false;
		}
		
		$vote_value = ($type == 'agree') ? '1' : '-1';
		
		$count = $this->count('answer_vote', 'answer_id = ' . $answer_id . ' AND vote_value = ' . $vote_value);
		
		return $this->query("UPDATE " . $this->get_table('answer') . " SET {$type}_count = {$count} WHERE answer_id = " . $answer_id);
	}
	
	public function update_question_vote_count($question_id)
	{
		$answers = $this->get_answer_list_by_question_id($question_id, null);
		
		if (empty($answers))
		{
			return false;
		}
		
		$answer_ids = array();
		
		foreach($answers as $key => $val)
		{
			$answer_ids[] = $val['answer_id'];
		}
		
		$agree_count = $this->count('answer_vote', 'answer_id IN (' . implode(',', $answer_ids) . ') AND vote_value = 1');
		
		$against_count = $this->count('answer_vote', 'answer_id IN (' . implode(',', $answer_ids) . ') AND vote_value = -1');
		
		return $this->query("UPDATE " . $this->get_table('question') . " SET agree_count = {$agree_count}, against_count = {$against_count} WHERE question_id = " . $question_id);
	}

	/**
	 * Set back Polls Status
	 */
	function set_answer_vote_status($voter_id, $vote_value)
	{
		return $this->update('answer_vote', array(
			"add_time" => time(), 
			"vote_value" => $vote_value
		), "voter_id = " . intval($voter_id));
	}

	function get_answer_vote_status($answer_id, $uid)
	{
		if (is_array($answer_id))
		{
			if ($result = $this->query_all("SELECT answer_id, vote_value FROM " . get_table('answer_vote') . " WHERE answer_id IN(" . implode(',', $answer_id) . ") AND vote_uid = " . intval($uid)))
			{
				foreach ($result AS $key => $val)
				{
					$vote_status[$val['answer_id']] = $val;
				}
			}
			
			foreach ($answer_id AS $aid)
			{
				if ($vote_status[$aid])
				{
					$result[$aid] = $vote_status[$aid]['vote_value'];
				}
				else
				{
					$result[$aid] = '0';
				}
			}
			
			return $result;
		}
		else
		{
			return $this->fetch_row('answer_vote', "answer_id = " . intval($answer_id) . " AND vote_uid = " . intval($uid));
		}
	}

	/**
	 * Delete question all replies associated and related Content
	 */
	public function remove_answers_by_question_id($question_id)
	{
		$answers = $this->get_answer_list_by_question_id($question_id);
		
		if (empty($answers))
		{
			return false;
		}
		
		$answer_ids = array();
		
		foreach ($answers as $key => $val)
		{
			$answer_ids[] = $val['answer_id'];
		}
		
		return $this->remove_answers_by_ids($answer_ids);
	}
	
	/**
	 * According to the response collection bulk Delete Reply
	 */
	public function remove_answers_by_ids($answer_id)
	{
		if (empty($answer_id))
		{
			return false;
		}
		
		$answers = array();
		
		if (is_array($answer_id))
		{
			$answers = $answer_id;
		}
		else
		{
			$answers[] = intval($answer_id);
		}
		
		foreach ($answers as $answer_id)
		{
			$answer_info = $this->model('answer')->get_answer_by_id($answer_id);
			
			$this->delete('answer_vote', "answer_id = " . intval($answer_id)); //Delete Agree
			
			$this->delete('answer_comments', 'answer_id = ' . intval($answer_id));	//Delete a comment
	
			$this->delete('answer_thanks', 'answer_id = ' . intval($answer_id));	//Delete thanks
	
			$this->delete('answer_uninterested', 'answer_id = ' . intval($answer_id));	//Delete No help
		
			ACTION_LOG::delete_action_history('associate_type = ' . ACTION_LOG::CATEGORY_ANSWER . ' AND associate_id = ' . intval($answer_id));	// Delete action
			
			ACTION_LOG::delete_action_history('associate_type = ' . ACTION_LOG::CATEGORY_QUESTION . ' AND associate_action = ' . ACTION_LOG::ANSWER_QUESTION . ' AND associate_attached = ' . intval($answer_id));	// Delete action
			
			if ($attachs = $this->model('publish')->get_attach('answer', $answer_id, 'min'))
			{
				foreach ($attachs as $key => $val)
				{
					$this->model('publish')->remove_attach($val['id'], $val['access_key']);
				}
			}
			
			$this->delete('answer', "answer_id = " . intval($answer_id));
			
			$this->model('question')->update_answer_count($answer_info['question_id']);
		}
		
		return true;
	}
	
	/**
	 * Determine whether the user responded to questions
	 */
	function has_answer_by_uid($question_id, $uid)
	{
		return $this->count('answer', "question_id = " . intval($question_id) . " AND uid = " . intval($uid));
	}
	
	public function get_last_answer($question_id)
	{
		return $this->fetch_row('answer', 'question_id = ' . intval($question_id), 'answer_id DESC');
	}
	
	public function get_last_answer_by_question_ids($question_ids)
	{
		if (!is_array($question_ids) OR sizeof($question_ids) == 0)
		{
			return false;
		}
		
		array_walk_recursive($question_ids, 'intval_string');
		
		if ($last_answer_query = $this->query_all("SELECT last_answer FROM " . get_table('question') . " WHERE question_id IN (" . implode(',', $question_ids) . ")"))
		{
			foreach ($last_answer_query AS $key => $val)
			{
				if ($val['last_answer'])
				{
					$last_answer_ids[] = $val['last_answer'];
				}
			}
			
			if ($last_answer_ids)
			{
				if ($last_answer = $this->fetch_all('answer', "answer_id IN (" . implode(',', $last_answer_ids) . ")"))
				{
					foreach ($last_answer AS $key => $val)
					{
						$result[$val['question_id']] = $val;
					}
				}
			}
		}
		
		return $result;
	}
	
	public function get_answer_agree_users($answer_id)
	{
		if ($agrees = $this->fetch_all('answer_vote', "answer_id = " . intval($answer_id) . " AND vote_value = 1"))
		{
			foreach ($agrees as $key => $val)
			{
				$agree_uids[] = $val['vote_uid'];
			}
		}
		
		if ($users = $this->model('account')->get_user_info_by_uids($agree_uids))
		{
			foreach ($users as $key => $val)
			{
				$user_infos[$val['uid']] = $val;
			}
		}
		
		if ($agree_uids)
		{
			foreach ($agree_uids as $key => $val)
			{
				$agree_users[$val] = $user_infos[$val]['user_name'];
			}
		}
		return $agree_users;
	}

	public function update_answer_comments_count($answer_id)
	{
		$count = $this->count('answer_comments', "answer_id = " . intval($answer_id));
		
		$this->update('answer', array(
			'comment_count' => $count
		), "answer_id = " . intval($answer_id));
	}

	public function insert_answer_comment($answer_id, $uid, $message)
	{
		$data = array(
			'uid' => intval($uid), 
			'answer_id' => intval($answer_id), 
			'message' => htmlspecialchars($message), 
			'time' => time()
		);
		
		$comment_id = $this->insert('answer_comments', $data);
		
		$this->update_answer_comments_count($answer_id);
		
		return $comment_id;
	}

	public function get_answer_comments($answer_id)
	{
		return $this->fetch_all('answer_comments', "answer_id = " . intval($answer_id), "time ASC");
	}
	
	public function get_comment_by_id($comment_id)
	{
		return $this->fetch_row('answer_comments', "id = " . intval($comment_id));
	}
	
	public function remove_comment($comment_id)
	{
		return $this->delete('answer_comments', "id = " . intval($comment_id));
	}
	
	public function user_rated($type, $answer_id, $uid)
	{
		if (!$uid)
		{
			return false;
		}
		
		switch ($type)
		{
			default:
				return false;
			break;
			
			case 'thanks':
			case 'uninterested':
				
			break;
		}
		
		static $user_rated;
		
		if ($user_rated[$type . '_' . $answer_id . '_' . $uid])
		{
			return $user_rated[$type . '_' . $answer_id . '_' . $uid];
		}
		
		$user_rated[$type . '_' . $answer_id . '_' . $uid] = $this->fetch_row('answer_' . $type, 'uid = ' . intval($uid) . ' AND answer_id = ' . intval($answer_id));
		
		return $user_rated[$type . '_' . $answer_id . '_' . $uid];
	}
	
	public function users_rated($type, $answer_ids, $uid)
	{
		if (!$uid)
		{
			return false;
		}
		
		if (!is_array($answer_ids))
		{
			return false;
		}
		
		switch ($type)
		{
			default:
				return false;
			break;
			
			case 'thanks':
			case 'uninterested':
				
			break;
		}
		
		$all_rated = $this->fetch_all('answer_' . $type, 'uid = ' . intval($uid) . ' AND answer_id IN(' . implode(',', $answer_ids) . ')');
		
		foreach ($all_rated AS $key => $val)
		{
			$users_rated[$val['answer_id']] = $val;
		}
		
		return $users_rated;
	}
	
	public function user_rate($type, $answer_id, $uid, $user_name)
	{		
		switch ($type)
		{
			default:
				return false;
			break;
			
			case 'thanks':
			case 'uninterested':
				
			break;
		}
		
		if ($user_rated = $this->user_rated($type, $answer_id, $uid))
		{
			if ($type == 'thanks')
			{
				return true;
			}
			
			$this->delete('answer_' . $type, 'uid = ' . intval($uid) . ' AND answer_id = ' . intval($answer_id));
		}
		else
		{
			$this->insert('answer_' . $type, array(
				'uid' => $uid,
				'answer_id' => $answer_id,
				'user_name' => $user_name,
				'time' => time()
			));
		}
		
		$this->update_user_rate_count($type, $answer_id);
		
		$answer_info = $this->get_answer_by_id($answer_id);
		
		if ($type == 'thanks')
		{
			$this->model('integral')->process($uid, 'ANSWER_THANKS', get_setting('integral_system_config_thanks'), 'Thank a reply #' . $answer_info['answer_id'], $answer_info['answer_id']);
			
			$this->model('integral')->process($answer_info['uid'], 'THANKS_ANSWER', -get_setting('integral_system_config_thanks'), 'Reply thanks #' . $answer_info['answer_id'], $answer_info['answer_id']);
		}
		else if ($answer_info['uninterested_count'] >= get_setting('uninterested_fold'))
		{
			if (!$this->model('integral')->fetch_log($answer_info['uid'], 'ANSWER_FOLD_' . $answer_info['answer_id']))
			{
				$this->model('integral')->process($answer_info['uid'], 'ANSWER_FOLD_' . $answer_info['answer_id'], get_setting('integral_system_config_answer_fold'), 'Reply fold #' . $answer_info['answer_id']);
				
				ACTION_LOG::delete_action_history('associate_type = ' . ACTION_LOG::CATEGORY_ANSWER . ' AND associate_id = ' . intval($answer_id));	// Delete action
			
				ACTION_LOG::delete_action_history('associate_type = ' . ACTION_LOG::CATEGORY_QUESTION . ' AND associate_action = ' . ACTION_LOG::ANSWER_QUESTION . ' AND associate_attached = ' . intval($answer_id));	// Delete action
			}
		}
		
		$this->model('account')->update_thanks_count($answer_info['uid']);
		
		return !$user_rated;
	}
	
	public function update_user_rate_count($type, $answer_id)
	{
		switch ($type)
		{
			default:
				return false;
			break;
			
			case 'thanks':
			case 'uninterested':
				
			break;
		}
		
		$this->update('answer', array(
			$type . '_count' => $this->count('answer_' . $type, 'answer_id = ' . intval($answer_id))
		), 'answer_id = ' . intval($answer_id));
	}
	
	function calc_best_answer()
	{
		if (!intval(get_setting('best_answer_day')))
		{
			return false;
		}
		
		$start_time = time() - get_setting('best_answer_day') * 3600 * 24;
		
		if ($questions = $this->query_all("SELECT question_id FROM " . $this->get_table('question') . " WHERE add_time < " . $start_time . " AND best_answer = 0 AND answer_count > " . get_setting('best_answer_min_count')))
		{
			foreach ($questions AS $key => $val)
			{
				$best_answer = $this->model('answer')->get_answer_list_by_question_id($val['question_id'], 1, null, 'agree_count DESC');
				
				if ($best_answer[0]['agree_count'] > get_setting('best_agree_min_count'))
				{
					$this->model('integral')->process($best_answer[0]['uid'], 'BEST_ANSWER', get_setting('integral_system_config_best_answer'), 'Question #' . $val['question_id'] . ' Best reply');
					
					$this->update('question', array(
						'best_answer' => $best_answer[0]['answer_id']
					), 'question_id = ' . intval($best_answer[0]['question_id']));
				}
			}
		}
		
		return true;
	}
	
	public function check_answer_question($question_id, $uid)
	{
		return $this->fetch_row('answer', "question_id = " . intval($question_id) . " AND uid = " . intval($uid));
	}
	
	public function get_answer_list($where, $limit = 10, $order = 'answer_id DESC')
	{
		return $this->fetch_all('answer', $where, $order, $limit);
	}
}