<?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
+--------------------------------------------------------------------------
*/

class ACTION_LOG
{
	/** 
	 * Add a question
	 */
	const ADD_QUESTION = 101;
	/** 
	 * Modify Question title
	 */
	const MOD_QUESTON_TITLE = 102;
	/** 
	 * Modify the description of the issue 
	 */
	const MOD_QUESTION_DESCRI = 103;
	/**
	 * Add question concerns
	 */
	const ADD_REQUESTION_FOCUS = 105;
	/**
	 * Delete Question concerns
	 */
	const DELETE_REQUESTION_FOCUS = 106;
	/**
	 * Question redirect
	 */
	const REDIRECT_QUESTION = 107;
	/**
	 * Changing the question classification
	 */
	const MOD_QUESTION_CATEGORY = 108;
	/**
	 * Modification accessories
	 */
	const MOD_QUESTION_ATTACH = 109;
	/**
	 * Delete Redirect question
	 */
	const DEL_REDIRECT_QUESTION = 110;
	/**
	 * Modify the questions
	 */
	const MOD_QUESTION = 111;
	/** 
	 * Reply to question
	 */
	const ANSWER_QUESTION = 201;
	/** 
	 * Change reply
	 */
	const MOD_ANSWER = 202;
	/**
	 * Delete Reply 
	 */
	const DELETE_ANSWER = 203;
	/**
	 * Increase Agree
	 */
	const ADD_AGREE = 204;
	/**
	 * Increased voting against
	 */
	const ADD_AGANIST = 205;
	/**
	 * Increase thanks to the author
	 */
	const ADD_USEFUL = 206;
	/**
	 * Issue No help
	 */
	const ADD_UNUSEFUL = 207;
	/**
	 * Cancelled in favour of
	 */
	const DEL_AGREE = 208;
	/**
	 * Cancel opposing votes
	 */
	const DEL_AGANIST = 209;
	/** 
	 * Add note
	 */
	const ADD_COMMENT = 301;
	/**
	 * Delete a comment
	 */
	const DELETE_COMMENT = 302;
	/** 
	 * Create a topic
	 */
	const ADD_TOPIC = 401;
	/** 
	 * Edit question
	 */
	const MOD_TOPIC = 402;
	/** 
	 * Edit topic description
	 */
	const MOD_TOPIC_DESCRI = 403;
	/**
	 * Modify subject miniatures
	 */
	const MOD_TOPIC_PIC = 404;
	/**
	 * Delete Topic
	 */
	const DELETE_TOPIC = 405;
	/**
	 * Add topic concerns
	 */
	const ADD_TOPIC_FOCUS = 406;
	/**
	 * Delete topic concerns
	 */
	const DELETE_TOPIC_FOCUS = 407;
	/**
	 * Increase topic classification
	 */
	const ADD_TOPIC_PARENT = 408;
	/**
	 * Delete topic category
	 */
	const DELETE_TOPIC_PARENT = 409;
	/**
	 * Add Related topic
	 */
	const ADD_RELATED_TOPIC = 410;
	/**
	 * Delete Related topic
	 */
	const DELETE_RELATED_TOPIC = 411;
	/**
	 * Question
	 */
	const CATEGORY_QUESTION = 1;
	/**
	 * Reply
	 */
	const CATEGORY_ANSWER = 2;
	/**
	 * Comment
	 */
	const CATEGORY_COMMENT = 3;
	/**
	 * Topic 
	 */
	const CATEGORY_TOPIC = 4;
	
	/**
	 * 
	 * Type definition corresponding to the array
	 */
	public static $ACTION_STRING_ARRAY = array(
		self::ADD_QUESTION => 'Add a question', 
		self::MOD_QUESTON_TITLE => 'Modify Question title', 
		self::MOD_QUESTION_DESCRI => 'Modify the description of the issue', 
		self::ADD_REQUESTION_FOCUS => 'Add question concerns', 
		self::DELETE_REQUESTION_FOCUS => 'Delete Question concerns', 
		self::REDIRECT_QUESTION => 'Question redirect', 
		self::DEL_REDIRECT_QUESTION => 'Delete Redirect question', 
		self::ANSWER_QUESTION => 'Reply to question', 
		self::MOD_ANSWER => 'Change reply', 
		self::DELETE_ANSWER => 'Delete Reply', 
		self::ADD_AGREE => 'Increase in favour of', 
		self::ADD_AGANIST => 'Increases against ', 
		self::DEL_AGREE => 'Cancelled in favour of', 
		self::DEL_AGANIST => 'Canceled opposition ', 
		self::ADD_COMMENT => 'Add note', 
		self::DELETE_COMMENT => 'Delete a comment', 
		self::ADD_TOPIC => 'Add topic', 
		self::MOD_TOPIC => 'Edit question', 
		self::MOD_TOPIC_DESCRI => 'Edit topic description', 
		self::MOD_TOPIC_PIC => 'Modify topic thumbnails', 
		self::DELETE_TOPIC => 'Delete Topic', 
		self::ADD_TOPIC_FOCUS => 'Follow topic', 
		self::DELETE_TOPIC_FOCUS => 'Unfollow topic', 
		self::ADD_TOPIC_PARENT => 'Increase topic classification', 
		self::DELETE_TOPIC_PARENT => 'Delete topic category'
	);

	/**
	 * 
	 * Added user tracking
	 * @param int    $uid
	 * @param int    $associate_id   Correlation ID
	 * @param int    $action_type    Action type
	 * @param int    $action_id      More types
	 * @param string $action_content The action Content
	 * @param string $action_attch   Additional Content
	 * @param int    $add_time       Action transmission time
	 * 
	 * @return boolean true|false
	 */
	public static function save_action($uid, $associate_id, $action_type, $action_id, $action_content = '', $action_attch = '', $add_time = 0, $anonymous = 0, $addon_data = null)
	{
		if (intval($uid) == 0 || intval($associate_id) == 0)
		{
			return false;
		}
		
		//Increase user counter
		self::update_user_nums($action_id, uid);
		
		if (is_numeric($action_attch))
		{
			$action_attch_insert = $action_attch;
		}
		else
		{
			$action_attch_insert = '-1';
			$action_attch_update = $action_attch;
		}
		
		$history_id = ASK_APP::model()->insert('user_action_history', array(
			'uid' => intval($uid), 
			'associate_type' => $action_type, 
			'associate_action' => $action_id, 
			'associate_id' => $associate_id, 
			'associate_attached' => $action_attch_insert,
			'add_time' => ($add_time == 0) ? time() : $add_time,
			'anonymous' => $anonymous,
		));
		
		ASK_APP::model()->insert('user_action_history_data', array(
			'history_id' => $history_id, 
			'associate_content' => htmlspecialchars($action_content), 
			'associate_attached' => htmlspecialchars($action_attch_update),
			'addon_data' => $addon_data ? serialize($addon_data) : '',
		));
		
		return $history_id;
	}

	/**
	 * 
	 * Update User Counters
	 */
	public static function update_user_nums($action_id, $uid)
	{
		$account_class = new account_class();
		
		switch ($action_id)
		{
			case self::ADD_COMMENT :
				
				break;
			case self::ADD_QUESTION :
				
				break;
			case self::ADD_TOPIC :
				
				break;
			case self::ADD_TOPIC_FOCUS :
				
				break;
				
			case self::ANSWER_QUESTION :
				$account_class->increase_user_statistics(account_class::ANSWER_COUNT, $uid);
				break;
			case self::DELETE_ANSWER :
				$account_class->increase_user_statistics(account_class::ANSWER_COUNT, -1, $uid);
				break;
			case self::DELETE_COMMENT :
				
				break;
			case self::DELETE_REQUESTION_FOCUS :
				
				break;
			case self::DELETE_TOPIC :
				
				break;
			case self::DELETE_TOPIC_FOCUS :
				
				break;
			case self::DELETE_TOPIC_PARENT :
				
				break;
			case self::MOD_ANSWER :
			case self::MOD_QUESTION_DESCRI :
			case self::MOD_QUESTON_TITLE :
			case self::MOD_TOPIC :
			case self::MOD_TOPIC_DESCRI :
			case self::MOD_TOPIC_PIC :
		}
	}

	/**
	 * 
	 * Depending on the event ID, to get a list of events
	 * @param boolean $count
	 * @param int     $event_id
	 * @param int     $limit
	 * @param int     $action_type
	 * @param int     $action_id
	 * 
	 * @return array
	 */
	public static function get_action_by_event_id($event_id = 0, $limit = 20, $action_type = null, $action_id = null)
	{
		if ($event_id > 0)
		{
			$where[] = 'associate_id = ' . intval($event_id);
		}
		
		if ($action_type)
		{
			$where[] = 'associate_type IN (' . $action_type . ')';
		}
		
		if ($action_id)
		{
			$where[] = 'associate_action IN (' . $action_id . ')';
		}
		else
		{
			$where[] = 'associate_action NOT IN (' . implode(',', array(				
				self::ADD_REQUESTION_FOCUS,
				self::DELETE_REQUESTION_FOCUS,
				self::DELETE_ANSWER,
				self::ADD_AGREE,
				self::ADD_AGANIST,
				self::ADD_USEFUL,
				self::ADD_UNUSEFUL,
				self::DEL_AGREE,
				self::DEL_AGANIST,
			)) . ')';
		}
		
		if ($user_action_history = ASK_APP::model()->fetch_all('user_action_history', implode(' AND ', $where), 'add_time DESC', $limit))
		{
			foreach ($user_action_history AS $key => $val)
			{
				$history_ids[] = $val['history_id'];
			}
				
			$actions_data = self::get_action_data_by_history_ids($history_ids);
			
			foreach ($user_action_history AS $key => $val)
			{
				$user_action_history[$key]['addon_data'] = $actions_data[$val['history_id']]['addon_data'];
				$user_action_history[$key]['associate_content'] = $actions_data[$val['history_id']]['associate_content'];
				
				if ($val['associate_attached'] == -1)
				{
					$user_action_history[$key]['associate_attached'] = $actions_data[$val['history_id']]['associate_attached'];
				}
			}
		}
			
		return $user_action_history;
	}
	
	public static function get_action_data_by_history_ids($history_ids)
	{
		if ($action_data = ASK_APP::model()->fetch_all('user_action_history_data', 'history_id IN(' . implode(',', $history_ids) . ')'))
		{
			foreach ($action_data AS $key => $val)
			{
				if ($val['addon_data'])
				{
					$val['addon_data'] = unserialize($val['addon_data']);
				}
				
				$result[$val['history_id']] = $val;
			}
		}
		
		return $result;
	}
	
	public static function get_action_data_by_history_id($history_id)
	{
		return ASK_APP::model()->fetch_row('user_action_history_data', 'history_id = ' . intval($history_id));
	}

	public static function get_action_by_history_id($history_id)
	{
		if ($action_history = ASK_APP::model()->fetch_row('user_action_history', 'history_id = ' . intval($history_id)))
		{
			$action_history_data = self::get_action_data_by_history_id($action_history['history_id']);
			
			$action_history['associate_content'] = $action_history_data['associate_content'];
			
			if ($action_history['associate_attached'] == -1)
			{
				$action_history['associate_attached'] = $action_history_data['associate_attached'];
			}
		}
		
		return $action_history;
	}

	public static function update_action_time_by_history_id($history_id)
	{
		return ASK_APP::model()->update('user_action_history', array(
			'add_time' => time()
		), 'history_id = ' . intval($history_id));
	}

	/**
	 * 
	 * According to the conditions, to get a list of events
	 * @param int     $limit
	 * 
	 * @return array
	 */
	public static function get_action_by_where($where = '', $limit = 20, $show_anonymous = false, $order = 'add_time DESC')
	{
		if (! $where)
		{
			return false;
		}
		
		if (!$show_anonymous)
		{
			$where = '(' . $where . ') AND anonymous = 0';
		}
		
		if ($user_action_history = ASK_APP::model()->fetch_all('user_action_history', $where, $order, $limit))
		{
			foreach ($user_action_history AS $key => $val)
			{
				$history_ids[] = $val['history_id'];
			}
				
			$actions_data = self::get_action_data_by_history_ids($history_ids);
				
			foreach ($user_action_history AS $key => $val)
			{
				$user_action_history[$key]['associate_content'] = $actions_data[$val['history_id']]['associate_content'];
				
				if ($val['associate_attached'] == -1)
				{
					$user_action_history[$key]['associate_attached'] = $actions_data[$val['history_id']]['associate_attached'];
				}
			}
		}
		
		return $user_action_history;
	}
		
	public static function get_actions_distint_by_where($where = '', $limit = 20, $add_time = null, $show_anonymous = false)
	{
		if (!$where)
		{
			return false;
		}
		
		if ($add_time)
		{
			$where = '(' . $where . ') AND add_time > ' . intval($add_time);
		}
		
		if (!$show_anonymous)
		{
			$where = '(' . $where . ') AND anonymous = 0';
		}
		
		$sql = "SELECT MAX(history_id) history_id FROM " . get_table('user_action_history') . " WHERE " . $where . " GROUP BY associate_id, associate_type ORDER BY history_id DESC";
		
		if ($action_history = ASK_APP::model()->query_all($sql, $limit))
		{
			foreach ($action_history as $key => $val)
			{
				$history_ids[] = $val['history_id'];
			}
			
			if ($action_history = self::get_action_by_where('history_id IN(' . implode(',', $history_ids) . ')', null, $show_anonymous, null))
			{
				$last_history = array();
						
				foreach ($action_history as $key => $val)
				{
					$last_history[$val['history_id']] = $action_history[$key];
				}
						
				krsort($last_history);
						
				return $last_history;
			}	
		}
		
		return array();
	}

	/**
	 * 
	 * Get the log does not repeat, to associate the ID is unique
	 * @param int $uid
	 * @param int $limit
	 * @param int $action_type
	 * @param int $action_id
	 * 
	 * @return array
	 */
	public static function get_action_distinct($uid = 0, $limit = null, $action_type = null, $action_id = null)
	{
		$sql = "SELECT DISTINCT associate_id, associate_type FROM " . get_table('user_action_history') . " WHERE uid = " . intval($uid);
		
		if ($action_type)
		{
			$sql .= ' AND associate_type IN(' . $action_type . ')';
		}
		
		if ($action_id)
		{
			$sql .= ' AND associate_action IN(' . $action_id . ')';
		}
		
		$sql .= ' ORDER BY add_time DESC';
		
		return ASK_APP::model()->query_all($sql, $limit);
	}

	/**
	 * 
	 * According to the problem log for a log that information Article
	 * @param int $associate_id
	 * @param int $action_type
	 * @param int $limit
	 * 
	 * @return array
	 */
	public static function get_action_detail_by_action_type($associate_id = '', $action_type = '', $limit = 1, $action_id = '', $uid = 0)
	{		
		$sql = "SELECT * FROM " . get_table('user_action_history') . " WHERE associate_id = " . intval($associate_id);
		
		if ($action_type)
		{
			$sql .= ' AND associate_type IN (' . $action_type . ')';
		}
		
		if ($action_id)
		{
			$sql .= ' AND associate_action IN(' . $action_id . ')';
		}
		
		if ($uid)
		{
			$sql .= ' AND uid = ' . intval($uid);
		}
		
		$sql .= ' ORDER BY add_time DESC, history_id DESC';
		
		if ($user_action_history = ASK_APP::model()->query_all($sql, $limit))
		{
			foreach ($user_action_history AS $key => $val)
			{
				$history_ids[] = $val['history_id'];
			}
				
			$actions_data = self::get_action_data_by_history_ids($history_ids);
				
			foreach ($user_action_history AS $key => $val)
			{
				$user_action_history[$key]['associate_content'] = $actions_data[$val['history_id']]['associate_content'];
				
				if ($user_action_history[$key]['associate_attached'] == -1)
				{
					$user_action_history[$key]['associate_attached'] = $actions_data[$val['history_id']]['associate_attached'];
				}
			}
		}
		
		return $user_action_history;
	}
	
	public static function format_action_str($action, $uid = 0, $user_name = null, $question_info = array(), $topic_info = array())
	{
		$action = intval($action);
		$action_str = null;
		$user_tip = 'class="umsg" data-message="&uid=' . $uid . '&card=user"';
		$topic_tip = 'class="umsg" data-message="&uid=' . $topic_info['topic_id'] . '&card=topic"';
		$user_url = 'people/' . $uid;
		$topic_url = 'topic/' . $topic_info['url_token'];
		
		switch ($action)
		{
			case ACTION_LOG::ADD_QUESTION : //'Add a question',
				if ($question_info['anonymous'] == 1)
				{
					$action_str = "Anonymous user started a question";
				}
				else
				{
					$action_str = "<a href=\"{$user_url}\" {$user_tip}>{$user_name}</a> post a question";
				}
				break;
				
			case ACTION_LOG::MOD_QUESTON_TITLE : //'Modify Question title',
				$action_str = "<a href=\"{$user_url}\" {$user_tip}>{$user_name}</a> edited question title";
				break;
				
			case ACTION_LOG::MOD_QUESTION_DESCRI : // 'Modify the description of the issue',	
				$action_str = "<a href=\"{$user_url}\" {$user_tip}>{$user_name}</a> edited question";
				break;
				
			case ACTION_LOG::ADD_REQUESTION_FOCUS : // 'Add question concerns',
				$action_str = "<a href=\"{$user_url}\" {$user_tip}>{$user_name}</a> followed the question";
				break;
				
			case ACTION_LOG::DELETE_REQUESTION_FOCUS : // 'Delete Question concerns',
				$action_str = "<a href=\"{$user_url}\" {$user_tip}>{$user_name}</a> canceled following the question";
				break;
				
			case ACTION_LOG::ANSWER_QUESTION : // 'Reply to question',
				if ($topic_info)
				{
					$action_str = " <a href=\"{$topic_url}\" class='default_topic' {$topic_tip}>" . $topic_info['topic_title'] . "</a> a question reply is added to the topic";
				}
				else if ($user_name)
				{
					$action_str = "<a href=\"{$user_url}\" {$user_tip}>{$user_name}</a> replied";
				}
				else
				{
					$action_str = "A reply is added to the question";
				}
				break;
			
			case ACTION_LOG::MOD_ANSWER : // 'Change reply',
				$action_str = "<a href=\"{$user_url}\" {$user_tip}>{$user_name}</a> edited reply";
				break;
				
			case ACTION_LOG::ADD_AGREE : //'Increase Agree',
				$action_str = "<a href=\"{$user_url}\" {$user_tip}>{$user_name}</a> agreed with reply";
				break;
			
			case ACTION_LOG::ADD_COMMENT : // 'Add note',
				$action_str = "<a href=\"{$user_url}\" {$user_tip}>{$user_name}</a> add a comment";
				break;
				
			case ACTION_LOG::DELETE_COMMENT : //'Delete a comment',
				$action_str = "<a href=\"{$user_url}\" {$user_tip}>{$user_name}</a> deleted comment";
				break;
			
			case ACTION_LOG::ADD_TOPIC : //'Add topic',
				if ($topic_info && $user_name)
				{
					if (isset($topic_info[0]))
					{
						$action_str = "<a href=\"{$user_url}\" {$user_tip}>{$user_name}</a> add the question to ";
						$tmp_i = 0;
						
						foreach ($topic_info as $key => $val)
						{
							$action_str .= "<a href=\"topic/" . $val['url_token'] . " {$topic_tip}>" . $val['topic_title'] . "</a>";
							
							$tmp_i++;
							
							if ($tmp_i > 2)
							{
								break;
							}
						}
						
						if (count($topic_info) > 3)
						{
							$action_str .= "Wait";
						}
						
						$action_str .= "Topic";
					}
					else
					{
						$action_str = "<a href=\"{$user_url}\" {$user_tip}>{$user_name}</a> add the question to <a href=\"topic/" . rawurlencode($topic_info['topic_title']) . "\" class='default_topic' $topic_tip>" . $topic_info['topic_title'] . "</a> topic";
					
					}
				
				}
				else if ($topic_info)
				{
					if (isset($topic_info[0]))
					{
						$action_str = 'The question has been added to ';
						$tmp_i = 0;
						
						foreach ($topic_info as $key => $val)
						{
							$action_str .= "<a href=\"topic/" . $val['url_token'] . " {$topic_tip}>" . $val['topic_title'] . "</a>";
							
							$tmp_i++;
							
							if ($tmp_i > 2)
							{
								break;
							}
						}
						
						if (count($topic_info) > 3)
						{
							$action_str .= 'Etc';
						}
						
						$action_str .= 'Topic';
					}
					else
					{
						$action_str = "The question has been added to <a href=\"{$topic_url}\" {$topic_tip}>" . $topic_info['topic_title'] . "</a> topic";
					}
				}
				else if ($user_name)
				{
					$action_str = "<a href=\"{$user_url}\" {$user_tip}>{$user_name}</a> added a question";
				}
				else
				{
					$action_str = 'A topic has been added to the question';
				}
				break;
			
			case ACTION_LOG::MOD_TOPIC : // 'Edit question',
				$action_str = "<a href=\"{$user_url}\" {$user_tip}>{$user_name}</a> edit question";
				break;
			
			case ACTION_LOG::MOD_TOPIC_DESCRI : // 'Edit topic description',
				$action_str = "<a href=\"{$user_url}\" {$user_tip}>{$user_name}</a> edit topic description";
				break;
			
			case ACTION_LOG::MOD_TOPIC_PIC : // 'Modify topic thumbnails',
				$action_str = "<a href=\"{$user_url}\" {$user_tip}>{$user_name}</a> modify topic thumbnails";
				break;
			
			case ACTION_LOG::DELETE_TOPIC : // 'Delete Topic',
				$action_str = "<a href=\"{$user_url}\" {$user_tip}>{$user_name}</a> delete Topic";
				break;
			
			case ACTION_LOG::ADD_TOPIC_FOCUS : // 'Follow topic',
				$action_str = "<a href=\"{$user_url}\" {$user_tip}>{$user_name}</a> follow topic";
				break;
			
			case ACTION_LOG::DELETE_TOPIC_FOCUS : // 'Unfollow topic',
				$action_str = "<a href=\"{$user_url}\" {$user_tip}>{$user_name}</a> unfollow topic";
				break;
		}
		
		return $action_str;
	}
	
	public static function delete_action_history($where)
	{
		if ($action_history = ASK_APP::model()->fetch_all('user_action_history', $where))
		{
			foreach ($action_history AS $key => $val)
			{
				ASK_APP::model()->delete('user_action_history_data', 'history_id = ' . $val['history_id']);
			}
			
			$action_history = ASK_APP::model()->delete('user_action_history', $where);
		}
	}
}