<?php
/**
 * View all forum created
 *
 * PHP version 5.0
 *
 * @category	###Anova###
 * @package		###Forum###
 * @author 		sridhar_68Ag07
 * @copyright	Copyright (c) 2009 {@link http://www.agriya.com Agriya Infoway}
 * @license		http://www.agriya.com Agriya Infoway Licence
 * @version		SVN: $Id:
 * @since 		2008-12-22
 **/
require_once('../../common/configs/config.inc.php');
$CFG['lang']['include_files'][] = 'languages/%s/'.$CFG['admin']['index']['anova_module'].'/admin/manageStats.php';
$CFG['html']['header'] = 'admin/html_header.php';
$CFG['html']['footer'] = 'admin/html_footer.php';
$CFG['lang']['include_files'][] = 'languages/%s/lists_array/months_list_array.inc.php';
$CFG['mods']['include_files'][] = 'common/classes/class_ListRecordsHandler.lib.php';
$CFG['mods']['include_files'][] = 'common/classes/class_AnovaHandler.lib.php';
//compulsory
$CFG['mods']['is_include_only']['html_header'] = false;
$CFG['html']['is_use_header'] = false;
/**
 * File to include the header file, database access file, session management file, help file and necessary functions
 */
require_once($CFG['site']['project_path'].'common/application_top.inc.php');

/**
 * ManageStatsHandler
 *
 * @package
 * @author Senthil
 * @copyright Copyright (c) 2010
 * @version $Id$
 * @access public
 */
class ManageStatsHandler extends AnovaHandler
	{
		/**
		 * ManageStatsHandler::buildConditionQuery()
		 *
		 * @return
		 */
		public function buildConditionQuery()
			{
				if($this->getFormField('mode') == 'audio')
					$this->sql_condition = 'vav.content_type = \'Audio\' AND aa.audio_status = \'Ok\'';
				else
					$this->sql_condition = 'vav.content_type = \'Video\' AND av.video_status =\'Ok\'';

				if($this->getFormField('srch_stats_cnt') != '')
					{
						$this->sql_condition .= $this->getFormField('condition');
					}
				if($this->getFormField('srch_date_added_from') != '')
					{
						$this->sql_condition .= ' AND DATE_FORMAT(vav.date_viewed, \''.$this->CFG['mysql_format']['date_time'].'\')  >=  DATE_FORMAT(\''.$this->getFormField('srch_date_added_from').'\', \''.$this->CFG['mysql_format']['date_time'].'\')';
					}
				if($this->getFormField('srch_date_added_to') != '')
					{
						$this->sql_condition .= '  AND DATE_FORMAT(vav.date_viewed, \''.$this->CFG['mysql_format']['date_time'].'\')  <= DATE_FORMAT(\''.$this->getFormField('srch_date_added_to').'\', \''.$this->CFG['mysql_format']['date_time'].'\')';
					}

				if($this->getFormField('mode') == 'audio')
					$this->sql_condition .= ' GROUP BY aa.content_id, content_for';
				else
					$this->sql_condition .= ' GROUP BY av.content_id, content_for';
			}

		/**
		 * ManageStatsHandler::buildSortQuery()
		 *
		 * @return
		 */
		public function buildSortQuery()
			{
				$this->sql_sort = $this->fields_arr['orderby_field'].' '.$this->fields_arr['orderby'];
			}

		/**
		 * ManageStatsHandler::displayVideoStats()
		 *
		 * @return
		 */
		public function displayVideoStats()
			{
				if(!$this->isResultsFound())
					{
						return ;
					}
				$displayVideoStats_arr = array();
				$inc = 0;

				while($row = $this->fetchResultRecord())
					{
						$displayVideoStats_arr[$inc]['record'] = $row;
						if($row['content_for'] == 'Answer')
							{
								$video_details = $this->getAnswerDetails($row['content_id']);

								if($this->getFormField('mode') != 'audio')
									{
										// for external video and recorder video
										if($video_details['external_video_id'] == $row['video_id'])
											$video_details['stats_video_id'] = $video_details['external_video_id'];
										else if($video_details['video_id'] == $row['video_id'])
											$video_details['stats_video_id'] = $video_details['video_id'];
									}

								if($this->getFormField('mode') == 'audio')
									{
										// for external audio and recorder audio
										if($video_details['external_audio_id'] == $row['audio_id'])
											$video_details['stats_audio_id'] = $video_details['external_audio_id'];
										else if($video_details['audio_id'] == $row['audio_id'])
											$video_details['stats_audio_id'] = $video_details['audio_id'];
									}
								$displayVideoStats_arr[$inc]['video_details']= $video_details;
								$displayVideoStats_arr[$inc]['video_details']['answer'] = nl2br(wordWrapManual($displayVideoStats_arr[$inc]['video_details']['answer'], $this->CFG['admin']['stats']['stats_line_length'], $this->CFG['admin']['stats']['short_length']));
								$displayVideoStats_arr[$inc]['video_details']['imgSrc'] = $this->CFG['site']['url'].'files/ans_videos/ans_thumbnails/'.getImageName($displayVideoStats_arr[$inc]['video_details']['stats_video_id']).'T.'.$this->CFG['video']['image']['extensions'];
							}
						else
							{
								$video_details = $this->getQuestionDetails($row['content_id']);
								if($this->getFormField('mode') != 'audio')
									{
										// for external video and recorder video
										if($video_details['external_video_id'] == $row['video_id'])
											$video_details['stats_video_id'] = $video_details['external_video_id'];
										else if($video_details['video_id'] == $row['video_id'])
											$video_details['stats_video_id'] = $video_details['video_id'];
									}

								if($this->getFormField('mode') == 'audio')
									{
										// for external audio and recorder audio
										if($video_details['external_audio_id'] == $row['audio_id'])
											$video_details['stats_video_id'] = $video_details['external_audio_id'];
										else if($video_details['audio_id'] == $row['audio_id'])
											$video_details['stats_video_id'] = $video_details['audio_id'];
									}

								$displayVideoStats_arr[$inc]['video_details']= $video_details;
								$displayVideoStats_arr[$inc]['video_details']['question'] = nl2br(wordWrapManual($displayVideoStats_arr[$inc]['video_details']['question'], $this->CFG['admin']['stats']['stats_line_length'], $this->CFG['admin']['stats']['short_length']));
								$displayVideoStats_arr[$inc]['video_details']['imgSrc'] = $this->CFG['site']['url'].'files/ans_videos/ans_thumbnails/'.getImageName($displayVideoStats_arr[$inc]['video_details']['stats_video_id']).'T.'.$this->CFG['video']['image']['extensions'];
							}
						$inc++;
					}
				return $displayVideoStats_arr;
			}

		/**
		 * ManageStatsHandler::getVideoViews()
		 *
		 * @param mixed $content_id
		 * @param mixed $content_for
		 * @return
		 */
		public function getVideoViews($content_id, $content_for)
			{
				$sql = 'SELECT COUNT( content_id ) as total_views , content_for, content_type, content_id FROM '.$this->CFG['db']['tbl']['video_audio_views'].
				        ' WHERE content_type = \'Video\' AND content_for = \''.$content_for.'\' and content_id = '.$content_id.' GROUP BY content_type, content_id ';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
			   if (!$rs)
				    trigger_db_error($this->dbObj);
				$row = $rs->FetchRow();
				return 	$row;
			}

		/**
		 * ManageStatsHandler::chkIsDateTimeGreaterThanNow()
		 *
		 * @param mixed $field_name
		 * @param string $err_tip
		 * @return
		 */
		public function chkIsDateTimeGreaterThanNow($field_name, $err_tip = '')
			{
				$sql = 'SELECT IF(DATE_FORMAT(NOW() , \''.$this->CFG['mysql_format']['date_time'].'\')<\''.$this->getFormField($field_name).'\',\'false\',\'true\') AS d';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
			    if (!$rs)
				    trigger_db_error($this->dbObj);

				$row = $rs->FetchRow();
				if($row['d'] == 'false')
					{
						$this->setFormFieldErrorTip($field_name, $err_tip);
						return false;
					}
				return true;
			}

		/**
		 * ManageStatsHandler::getVideosInLastSevenDays()
		 *
		 * @return
		 */
		public function getVideosInLastSevenDays()
			{
				$sql = 'SELECT video_id  FROM '.$this->CFG['db']['tbl']['ans_video'].
			    	   ' WHERE DATE_FORMAT(date_added , \''.$this->CFG['mysql_format']['date_time'].'\') >DATE_FORMAT((select DATE_SUB(now(), INTERVAL 7 DAY)),\''.$this->CFG['mysql_format']['date_time'].'\') AND video_encoded_status =\'Yes\' AND video_status = \'Ok\' ';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
			   	if (!$rs)
				    trigger_db_error($this->dbObj);
				$numrows = $rs->PO_RecordCount();
				return $numrows;
			}

		/**
		 * ManageStatsHandler::getVideoViewInLastSevenDays()
		 *
		 * @return
		 */
		public function getVideoViewInLastSevenDays()
			{
				$sql = 'SELECT COUNT( av.content_id) AS total_views, content_type, content_for, av.content_id FROM '.$this->CFG['db']['tbl']['ans_video'].' AS av'.  				     ' LEFT JOIN '.$this->CFG['db']['tbl']['video_audio_views'].' AS vav ON av.video_id = vav.content_id '.
						 ' WHERE vav.content_type = \'Video\' AND av.video_status = \'Ok\''.
						 ' AND DATE_FORMAT(vav.date_viewed, \''.$this->CFG['mysql_format']['date_time'].'\') >= DATE_FORMAT((select DATE_SUB(now(), INTERVAL 7 DAY)),\''.$this->CFG['mysql_format']['date_time'].'\') GROUP BY  vav.content_type';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
			   	if (!$rs)
				    trigger_db_error($this->dbObj);
				$row = $rs->FetchRow();
				return $row['total_views'];
			}

		/**
		 * ManageStatsHandler::getAudiosInLastSevenDays()
		 *
		 * @return
		 */
		public function getAudiosInLastSevenDays()
			{
				$sql = 'SELECT audio_id  FROM '.$this->CFG['db']['tbl']['ans_audio'].
			    	    ' WHERE DATE_FORMAT(date_added , \''.$this->CFG['mysql_format']['date_time'].'\') >DATE_FORMAT((select DATE_SUB(now(), INTERVAL 7 DAY)),\''.$this->CFG['mysql_format']['date_time'].'\') AND audio_encoded_status =\'Yes\' AND audio_status = \'Ok\'';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
			   	if (!$rs)
				    trigger_db_error($this->dbObj);
				$numrows = $rs->PO_RecordCount();
				return $numrows;
			}

		/**
		 * ManageStatsHandler::getAudioViewInLastSevenDays()
		 *
		 * @return
		 */
		public function getAudioViewInLastSevenDays()
			{
				$sql = 'SELECT COUNT( aa.content_id) AS total_views, content_type, content_for, aa.content_id FROM '.$this->CFG['db']['tbl']['ans_audio'].' AS aa'.  				     ' LEFT JOIN '.$this->CFG['db']['tbl']['video_audio_views'].' AS vav ON aa.audio_id = vav.content_id '.
						 ' WHERE vav.content_type = \'Audio\' AND aa.audio_status = \'Ok\''.
						 ' AND DATE_FORMAT(vav.date_viewed, \''.$this->CFG['mysql_format']['date_time'].'\') >= DATE_FORMAT((select DATE_SUB(now(), INTERVAL 7 DAY)),\''.$this->CFG['mysql_format']['date_time'].'\') GROUP BY  vav.content_type';

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
			   	if (!$rs)
				    trigger_db_error($this->dbObj);
				$row = $rs->FetchRow();
				return $row['total_views'];
			}

		/**
		 * ManageStatsHandler::getTotalAudiosCount()
		 *
		 * @param mixed $date1
		 * @param mixed $date2
		 * @return
		 */
		public function getTotalAudiosCount($date1, $date2)
			{
				if($date2 == '')
					$date2 = date('Y-m-d G:i:s');
				if($date1 == '')
					{
						$sql1= ' SELECT DATE_SUB(now(), INTERVAL '.$this->CFG['admin']['default']['days'].' DAY) AS date_from';
						$stmt1 = $this->dbObj->Prepare($sql1);
						$rs1 = $this->dbObj->Execute($stmt1);
					   	if (!$rs1)
							trigger_db_error($this->dbObj);
						$row = $rs1->FetchRow();
						$date1 = $row['date_from'];
					}

				$sql = 'SELECT audio_id  FROM '.$this->CFG['db']['tbl']['ans_audio'].
			    	   ' WHERE DATE_FORMAT(date_added , \''.$this->CFG['mysql_format']['date_time'].'\') >DATE_FORMAT(\''.$date1.'\',\''.$this->CFG['mysql_format']['date_time'].'\') AND DATE_FORMAT(date_added , \''.$this->CFG['mysql_format']['date_time'].'\') < DATE_FORMAT(\''.$date2.'\',\''.$this->CFG['mysql_format']['date_time'].'\') AND  audio_encoded_status =\'Yes\' AND audio_status = \'Ok\'';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
			   	if (!$rs)
				    trigger_db_error($this->dbObj);
				$numrows = $rs->PO_RecordCount();
				return $numrows;
			}

		/**
		 * ManageStatsHandler::getTotalAudioViewsCount()
		 *
		 * @param mixed $date1
		 * @param mixed $date2
		 * @return
		 */
		public function getTotalAudioViewsCount($date1, $date2)
			{
				global $smartyObj;
				if($date2 == '')
					$date2 = date('Y-m-d G:i:s');
				if($date1 == '')
					{
						$sql1= 'SELECT DATE_SUB(now(), INTERVAL '.$this->CFG['admin']['default']['days'].' DAY) AS date_from';
						$stmt1 = $this->dbObj->Prepare($sql1);
						$rs1 = $this->dbObj->Execute($stmt1);
					   	if (!$rs1)
							trigger_db_error($this->dbObj);
						$row = $rs1->FetchRow();
						$date1 = $row['date_from'];
					}
				$smartyObj->assign('since_date_audio',$date1);
				$smartyObj->assign('date_audio_to',$date2);

				$sql = 'SELECT COUNT( aa.content_id) AS total_views, content_type, content_for, aa.content_id FROM '.$this->CFG['db']['tbl']['ans_audio'].' AS aa'.  				     ' LEFT JOIN '.$this->CFG['db']['tbl']['video_audio_views'].' AS vav ON aa.audio_id = vav.content_id '.
						 ' WHERE vav.content_type = \'Audio\' AND aa.audio_status = \'Ok\''.
						 ' AND DATE_FORMAT(vav.date_viewed, \''.$this->CFG['mysql_format']['date_time'].'\') > DATE_FORMAT(\''.$date1.'\',\''.$this->CFG['mysql_format']['date_time'].'\') AND DATE_FORMAT(vav.date_viewed, \''.$this->CFG['mysql_format']['date_time'].'\') < DATE_FORMAT(\''.$date2.'\',\''.$this->CFG['mysql_format']['date_time'].'\') GROUP BY  vav.content_type';

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
			   	if (!$rs)
				    trigger_db_error($this->dbObj);
				$row = $rs->FetchRow();
				return $row['total_views'];
			}

		/**
		 * ManageStatsHandler::getTotalVideosCount()
		 *
		 * @param mixed $date1
		 * @param mixed $date2
		 * @return
		 */
		public function getTotalVideosCount($date1, $date2)
			{
				if($date2 == '')
					$date2 = date('Y-m-d G:i:s');
				if($date1 == '')
					{
						$sql1= 'SELECT DATE_SUB(now(), INTERVAL '.$this->CFG['admin']['default']['days'].' DAY) AS date_from';
						$stmt1 = $this->dbObj->Prepare($sql1);
						$rs1 = $this->dbObj->Execute($stmt1);
					   	if (!$rs1)
							trigger_db_error($this->dbObj);
						$row = $rs1->FetchRow();
						$date1 = $row['date_from'];
					}

				$sql = 'SELECT video_id  FROM '.$this->CFG['db']['tbl']['ans_video'].
			    	   ' WHERE DATE_FORMAT(date_added , \''.$this->CFG['mysql_format']['date_time'].'\') >DATE_FORMAT(\''.$date1.'\',\''.$this->CFG['mysql_format']['date_time'].'\') AND DATE_FORMAT(date_added , \''.$this->CFG['mysql_format']['date_time'].'\') < DATE_FORMAT(\''.$date2.'\',\''.$this->CFG['mysql_format']['date_time'].'\') AND  video_encoded_status =\'Yes\' AND video_status = \'Ok\'';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
			   	if (!$rs)
				    trigger_db_error($this->dbObj);
				$numrows = $rs->PO_RecordCount();
				return $numrows;
			}

		/**
		 * ManageStatsHandler::getTotalVideoViewsCount()
		 *
		 * @param mixed $date1
		 * @param mixed $date2
		 * @return
		 */
		public function getTotalVideoViewsCount($date1, $date2)
			{
				global $smartyObj;
				if($date2 == '')
					$date2 = date('Y-m-d G:i:s');
				if($date1 == '')
					{
						$sql1= 'SELECT DATE_SUB(now(), INTERVAL '.$this->CFG['admin']['default']['days'].' DAY) AS date_from';
						$stmt1 = $this->dbObj->Prepare($sql1);
						$rs1 = $this->dbObj->Execute($stmt1);
					   	if (!$rs1)
							trigger_db_error($this->dbObj);
						$row = $rs1->FetchRow();
						$date1 = $row['date_from'];
					}
				$smartyObj->assign('since_date_video',$date1);
				$smartyObj->assign('date_video_to',$date2);

				$sql = 'SELECT COUNT( av.content_id) AS total_views, content_type, content_for, av.content_id FROM '.$this->CFG['db']['tbl']['ans_video'].' AS av'.  				     ' LEFT JOIN '.$this->CFG['db']['tbl']['video_audio_views'].' AS vav ON av.video_id = vav.content_id '.
						 ' WHERE vav.content_type = \'Video\' AND av.video_status = \'Ok\''.
						 ' AND DATE_FORMAT(vav.date_viewed, \''.$this->CFG['mysql_format']['date_time'].'\') > DATE_FORMAT(\''.$date1.'\',\''.$this->CFG['mysql_format']['date_time'].'\') AND DATE_FORMAT(vav.date_viewed, \''.$this->CFG['mysql_format']['date_time'].'\') < DATE_FORMAT(\''.$date2.'\',\''.$this->CFG['mysql_format']['date_time'].'\') GROUP BY  vav.content_type';

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
			   	if (!$rs)
				    trigger_db_error($this->dbObj);
				$row = $rs->FetchRow();
				return $row['total_views'];
			}
	}
//<<<<<<<--------------class ForumsFormHandler---------------//
//--------------------Code begins-------------->>>>>//
$stats = new ManageStatsHandler();
if (!$CFG['admin']['module']['video'] &&  !$CFG['admin']['module']['audio'])
	Redirect2URL($stats->CFG['site']['url']."admin/index.php");

$stats->setPageBlockNames(array('form_show_stats', 'form_search', 'form_default_stats'));
$stats->setAllPageBlocksHide();

//default form fields and values...
$stats->setFormField('srch', '');
$stats->setFormField('numpg',0);
$stats->setFormField('start',0);

$stats->setFormField('stats_condition', '');
$stats->setFormField('srch_stats_cnt', '');
$stats->setFormField('stats_video', 'Video');
$stats->setFormField('stats_audio', '');
$stats->setFormField('srch_date_added_from', '');
$stats->setFormField('srch_date_added_to', '');
$stats->setFormField('srch_date', '');
$stats->setFormField('srch_month', '');
$stats->setFormField('srch_year', '');
$stats->setFormField('srch_status', '');
$stats->setFormField('orderby_field', '');
$stats->setFormField('orderby', '');
$stats->setFormField('msg', '');
$stats->setFormField('mode', '');
$stats->setFormField('condition', '');

$condition_operators = array('equalto', 'greaterthan', 'greaterthanequal', 'lessthan', 'lessthanequal', 'notequal');
$operators_arr = array('equalto' => "=", 'greaterthan' => ">", 'greaterthanequal' => ">=", 'lessthan' => "<", 'lessthanequal' => "<=", 'notequal' => "!=");
$stats->populateConditionOperators_arr = array("equalto" => $LANG['stats_equal_to'], "greaterthan" => $LANG['stats_greater_than'],
											"greaterthanequal" => $LANG['stats_greater_than_equal'], "lessthan" => $LANG['stats_less_than'],
											"lessthanequal" => $LANG['stats_less_than_equal'], "notequal" => $LANG['stats_not_equal']);
$stats->setMonthsListArr($LANG_LIST_ARR['months']);
/***********set form field for navigation****/

/*************Start navigation******/
//fetch number of records show in the result--numpg
$condition = '';
$stats->numpg = $CFG['data_tbl']['numpg'];
$stats->setFormField('start', 0);
$stats->setFormField('numpg', $CFG['data_tbl']['numpg']); //$CFG['data_tbl']['numpg']
$stats->setMinRecordSelectLimit($CFG['data_tbl']['min_record_select_limit']);
$stats->setMaxRecordSelectLimit($CFG['data_tbl']['max_record_select_limit']);
$stats->setNumPerPageListArr($CFG['data_tbl']['numperpage_list_arr']);
//Set tables and fields to return
$stats->setTableNames(array());
$stats->setReturnColumns(array());
/*************End navigation******/
$stats->sanitizeFormInputs($_REQUEST);
if(!$CFG['admin']['module']['audio'])
	$stats->setFormField('mode', '');
if(!$CFG['admin']['module']['video'])
	$stats->setFormField('mode', 'audio');
$last_seven = $stats->getVideosInLastSevenDays();
$smartyObj->assign('videos_for_seven_days', $last_seven);

$last_seven_views = $stats->getVideoViewInLastSevenDays();
$smartyObj->assign('video_views_for_seven_days', $last_seven_views);

$last_seven_audio = $stats->getAudiosInLastSevenDays();
$smartyObj->assign('audios_for_seven_days', $last_seven_audio);

$last_seven_audio_views = $stats->getAudioViewInLastSevenDays();
$smartyObj->assign('audio_views_for_seven_days', $last_seven_audio_views);

$total_audios = $stats->getTotalAudiosCount($stats->getFormField('srch_date_added_from'), $stats->getFormField('srch_date_added_to'));
$smartyObj->assign('total_audios', $total_audios);

$total_audio_views = $stats->getTotalAudioViewsCount($stats->getFormField('srch_date_added_from'), $stats->getFormField('srch_date_added_to'));
$smartyObj->assign('total_audio_views', $total_audio_views);

$total_videos = $stats->getTotalVideosCount($stats->getFormField('srch_date_added_from'), $stats->getFormField('srch_date_added_to'));
$smartyObj->assign('total_videos', $total_videos);

$total_video_viewa = $stats->getTotalVideoViewsCount($stats->getFormField('srch_date_added_from'), $stats->getFormField('srch_date_added_to'));
$smartyObj->assign('total_video_views', $total_video_viewa);

if($stats->getFormField('mode') == 'audio')
	$stats->setFormField('orderby_field', 'aa.total_views');
else
	$stats->setFormField('orderby_field', 'av.total_views');
$stats->setFormField('orderby', 'DESC');

$stats->setPageBlockShow('form_search');
$stats->setPageBlockShow('form_show_stats');
$stats->setPageBlockShow('form_default_stats');
if($stats->isFormPOSTed($_POST, 'stats_search'))
	{
		if($stats->getFormField('srch_date_added_from') != '')
			{
				$stats->chkIsValidDate('srch_date_added_from', $stats->LANG['manage_stats_err_tip_invalid_date']);
				$stats->chkIsDateTimeGreaterThanNow('srch_date_added_from', $LANG['manage_stats_err_tip_invalid_date']);
			}
		if($stats->getFormField('srch_date_added_to') != '')
			{
				$stats->chkIsValidDate('srch_date_added_to', $stats->LANG['manage_stats_err_tip_invalid_date']);
				$stats->chkIsDateTimeGreaterThanNow('srch_date_added_to', $LANG['manage_stats_err_tip_invalid_date']);
			}
		if($stats->getFormField('srch_stats_cnt')!= '')
			$stats->chkIsNumeric('srch_stats_cnt', $LANG['stats_err_tip_enter_numbers']);
		if($stats->getFormField('srch_date_added_from')!= '' && $stats->getFormField('srch_date_added_to') != '')
			{
					$stats->chkIsDateDiff('srch_date_added_from', 'srch_date_added_to', $LANG['stats_date_err_tip_enter_numbers'], $LANG['stats_date_err_tip_enter_numbers2']);
			}
		if($stats->isValidFormInputs())
			{
				if($stats->getFormField('srch_stats_cnt')!= '')
					{
						$operator = '=';
						if (in_array($stats->getFormField('stats_condition'), $condition_operators))
							$operator = $operators_arr[$stats->getFormField('stats_condition')];
						if($stats->getFormField('mode') == 'audio')
							$search_condition = ' AND aa.total_views '.$operator.' \''.addslashes($stats->getFormField('srch_stats_cnt')).'\'';
						else
							$search_condition = ' AND av.total_views '.$operator.' \''.addslashes($stats->getFormField('srch_stats_cnt')).'\'';
						$stats->setFormField('condition', $search_condition);
					}
			}
		else
			{
				$stats->setPageBlockHide('form_default_stats');
			}
	}
elseif($stats->isFormPOSTed($_POST, 'stats_reset'))
	{
		$stats->setFormField('stats_condition', '');
		$stats->setFormField('srch_stats_cnt', '');
		$stats->setFormField('srch_date_added_from', '');
		$stats->setFormField('srch_date_added_to', '');
	}
if($stats->isShowPageBlock('form_show_stats'))
	{
		if($stats->getFormField('mode') != 'audio')
			{
				$stats->setTableNames(array($CFG['db']['tbl']['ans_video'].' as av  LEFT JOIN '.$CFG['db']['tbl']['video_audio_views'].' AS vav ON av.video_id = vav.content_id' ));
				$stats->setReturnColumns(array( 'av.video_id', 'COUNT( av.content_id) AS total_views'  , 'content_type', 'content_for', 'av.content_id', 'av.total_views'));
			}
		elseif($stats->getFormField('mode') == 'audio')
			{
				$stats->setTableNames(array($CFG['db']['tbl']['ans_audio'].' as aa  LEFT JOIN '.$CFG['db']['tbl']['video_audio_views'].' AS vav ON aa.audio_id = vav.content_id' ));
				$stats->setReturnColumns(array( 'aa.audio_id', 'COUNT( aa.content_id) AS total_views'  , 'content_type', 'content_for', 'aa.content_id', 'aa.total_views'));
			}
		if ($stats->isShowPageBlock('form_show_stats'))
			{
				$stats->buildSelectQuery();
				$stats->buildConditionQuery();
				$stats->buildSortQuery();
				$stats->buildQuery();
				//$stats->printQuery();
				$stats->groupByExecuteQuery();

				if ($stats->isResultsFound())
					{
						$stats->form_show_stats['video_stats'] = $stats->displayVideoStats();
						$smartyObj->assign('smarty_paging_list', $stats->populatePageLinksGET($stats->getFormField('start'), array('orderby_field', 'orderby')));
					}
			}
	}
//<<<<--------------------Code Ends----------------------//
$stats->hidden_arr =  array('orderby_field', 'orderby');
$stats->current_year = date('Y');
if ($stats->isShowPageBlock('form_search'))
	{
		$stats->form_search['hidden_arr'] = array('orderby_field', 'orderby');
	}
//--------------------Page block templates begins-------------------->>>>>//
//include the header file
$stats->includeHeader();
//include the content of the page
setTemplateFolder('admin/', $CFG['admin']['index']['anova_module']);
$smartyObj->display('manageStats.tpl');
?>