<?php
/**
 *
 * PHP version 5.0
 *
 * @category	Anova
 * @package		Widget
 * @author		senthil_52ag05
 * @copyright	Copyright (c) 2009 {@link http://www.agriya.com Agriya Infoway}
 * @license		http://www.agriya.com Agriya Infoway Licence
 * @version		SVN: $Id : $
 * @since 		2008-11-05
 */
class Widget
	{
		private $_cache_user_details = array();
		private $_cache_user_log_details = array();
		private $_cache_question_details = array();
		private $_cache_debate_details = array();
		protected $tab_index = 1000;

		/**
		 *
		 *
		 * @access public
		 * @return void
		 **/
		public function __construct()
			{
				global $db, $CFG, $LANG;
				$this->setDBObject($db);
				$this->makeGlobalize($CFG, $LANG);
			}

		/**
		 *
		 *
		 * @access public
		 * @return void
		 **/
		public function setDBObject($dbObj)
			{
				$this->dbObj = $dbObj;
			}

		/**
		 *
		 *
		 * @access public
		 * @return void
		 **/
		public function makeGlobalize($cfg = array(), $lang=array())
			{
				$this->CFG = $cfg;
				$this->LANG = $lang;
			}

		/**
		 *
		 *
		 * @access public
		 * @return void
		 **/
		public function getTabIndex()
			{
				$this->tab_index = $this->tab_index + 5;
				return $this->tab_index;
			}

		/**
		 *
		 *
		 * @access public
		 * @return void
		 **/
		public function stripString($str = '', $length=256)
			{
				if (strlen($str) > $length )
					$str = substr($str, 0, ($length-3)).'...';
				return $str;
			}

		/**
		 *
		 * @access public
		 * @return void
		 **/
		public function getUserTableFields($user_fields, $trim=true)
		    {
				$str = '';
				foreach($user_fields as $field){
					if (isset($this->CFG['users'][$field]) AND $this->CFG['users'][$field])
						$str .= $this->CFG['users'][$field].' AS '.$field.', ';
				}
				if ($trim)
					$str = substr($str, 0, strrpos($str, ', '));
				return $str;
		    }

		/**
		 *
		 * @access public
		 * @return void
		 **/
		public function getUserTableField($user_field)
		    {
				return $this->CFG['users'][$user_field];
		    }

		/**
		 *
		 * @access public
		 * @return void
		 **/
		public function showCategoryName($cat_id)
		    {
				if (isset($this->_cache_category_details[$cat_id]))
					{
						$row = $this->_cache_category_details[$cat_id];
						$showCategoryName_val = '<a href="'.getAnovaUrl('questions', '?cat='.$row['seo_title'], 'dir/'.$row['seo_title'].'/').'">'.$row['cat_name'].'</a>';
						return $showCategoryName_val;
					}

				$sql = 'SELECT cat_name, seo_title, cat_id FROM '.$this->CFG['db']['tbl']['questions_category'].
						' WHERE cat_id='.$this->dbObj->Param('cat_id');
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($cat_id));
				if (!$rs)
					    trigger_db_error($this->dbObj);
				if ($rs->PO_RecordCount())
					{
						$row = $rs->FetchRow();
						//cache category details
						$this->_cache_category_details[$cat_id] = $row;
						$showCategoryName_val = '<a href="'.getAnovaUrl('questions', '?cat='.$row['seo_title'], 'dir/'.$row['seo_title'].'/').'">'.$row['cat_name'].'</a>';
						return $showCategoryName_val;
					}
				return false;
		    }

		/**
		 * QuestionFormHandler::hasAnyAttachment()
		 *
		 * @param mixed $id
		 * @param mixed $type
		 * @return
		 */
		public function hasAnyAttachment($id, $type)
			{
				$sql = 'SELECT attachment_id, attachment_name, photo_server_url FROM '.$this->CFG['db']['tbl']['attachments'].
						' WHERE content_id='.$id.
						' AND content_type=\''.$type.'\' ORDER BY attachment_id ';

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
				        trigger_db_error($this->dbObj);

				$attachment_details = array();
				if ($rs->PO_RecordCount())
					{
						$attachment_details = $rs->FetchRow();
					}
				return $attachment_details;
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function getQuestionDetails($ques_id=0)
			{
				if (isset($this->_cache_question_details[$ques_id]))
					{
						return $this->_cache_question_details[$ques_id];
					}

				$questionDetails = array();
				$sql = 'SELECT q.ques_id, q.total_answer, q.cat_id, q.pcat_id, q.best_ans_id, q.status, q.total_stars, q.question, UNIX_TIMESTAMP(date_asked) as date_asked, q.votes, q.external_video_id, q.external_audio_id'.
						', '.$this->getUserTableField('display_name').' as asked_by, '.$this->getUserTableFields(array('image_path', 'name', 'gender', 't_height', 't_width', 'photo_server_url', 'photo_ext', 's_width', 's_height'), false).' q.user_id'.
						', q.user_id AS img_user_id, video_id, audio_id, IF(status=\'Open\' AND date_closed>=NOW(), 1, 0) is_open, TIMEDIFF(date_closed, NOW()) as date_closed'.
						', q.total_videos, q.total_audios, q.total_views, q.seo_title FROM '.$this->CFG['db']['tbl']['questions'].' AS q, '.$this->CFG['db']['tbl']['users'].' AS u'.
						' WHERE q.ques_id='.$this->dbObj->Param($ques_id).
						' AND q.user_id=u.'.$this->getUserTableField('user_id').
						' AND q.status IN (\'Open\', \'Resolved\')'.
						' AND u.'.$this->getUserTableField('usr_status').'=\'Ok\'';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($ques_id));
				if (!$rs)
					    trigger_db_error($this->dbObj);

				if ($rs->PO_RecordCount())
					{
						$questionDetails = $rs->FetchRow();
						$questionDetails['qLink'] = getAnovaUrl('answer', '?title='.$questionDetails['seo_title'], $questionDetails['seo_title'].'/');
						$questionDetails['question_link'] = '<a href="'.getAnovaUrl('answer','?title='.$questionDetails['seo_title'], $questionDetails['seo_title'].'/').'">'.wordWrapManual($questionDetails['question'], $this->CFG['admin']['question']['line_length'], $this->CFG['admin']['question']['short_length']).'</a>';
						$questionDetails['asked_by_link'] = '<a href="'.getMemberUrl($questionDetails['user_id'], $questionDetails['name']).'">'.stripString($questionDetails['asked_by'], $this->CFG['username']['medium_length']).'</a>';

						$cat_id = ($questionDetails['cat_id']) ? $questionDetails['cat_id'] : $questionDetails['pcat_id'];
						$questionDetails['showCategoryName'] = $this->showCategoryName($cat_id);

						$questionDetails['audio_on_click'] = 'ques_audio_'.$questionDetails['audio_id'];
						$questionDetails['audio_url'] = getAnovaUrl('ansviewaudio', '?pg=Question_'.$questionDetails['audio_id'], '?pg=Question_'.$questionDetails['audio_id']);

						$questionDetails['video_on_click'] = 'ques_video_'.$questionDetails['video_id'];
						$questionDetails['video_url'] = getAnovaUrl('ansviewvideo', '?pg=Question_'.$questionDetails['video_id'], '?pg=Question_'.$questionDetails['video_id']);

						$questionDetails['ext_ans_view_video']['url'] = getAnovaUrl('ansviewvideo', '?pg=Question_'.$questionDetails['external_video_id'], '?pg=Question_'.$questionDetails['external_video_id']);
		  				$questionDetails['ext_ans_view_video']['on_click'] = 'external_video_ques_'.$questionDetails['external_video_id'];

						$questionDetails['ext_ans_view_audio']['url'] = getAnovaUrl('ansviewaudio', '?pg=Question_'.$questionDetails['external_audio_id'], '?pg=Question_'.$questionDetails['external_audio_id']);
						$questionDetails['ext_ans_view_audio']['on_click'] = 'external_audio_ques_'.$questionDetails['external_audio_id'];

						$questionDetails['has_attachment'] = false;
						if ($this->CFG['admin']['attachments_questions']['allowed'] && $attachment_details = $this->hasAnyAttachment($questionDetails['ques_id'], 'Question'))
							{
								$questionDetails['has_attachment'] = true;
								$extern = strtolower(substr($attachment_details['attachment_name'], strrpos($attachment_details['attachment_name'], '.')+1));
								$attachment_original_file_name = getImageName($attachment_details['attachment_id']).$this->CFG['admin']['ans_pictures']['large_name'].'.'.$extern;
								$questionDetails['img_id'] = 'Question_'.$ques_id.'_'.$attachment_details['attachment_id'];
								$questionDetails['attachment_path'] = getAnovaUrl('answer', '?title='.$questionDetails['seo_title'].'&amp;imgdetail='.$questionDetails['img_id'], $questionDetails['seo_title'].'/?imgdetail='.$questionDetails['img_id']);
								$questionDetails['image_large_path'] = $attachment_details['photo_server_url'].$this->CFG['admin']['ans_attachments_path'].$attachment_original_file_name;
							}
					}

				//cache questions details
				$this->_cache_question_details[$ques_id] = $questionDetails;

				return $questionDetails;
			}

		/**
		 * To get answers details
		 *
		 * @return 		void
		 * @access 		public
		 */

		public function getAnswerDetails($ans_id)
			{
				$sql = 'SELECT a.ans_id, a.user_id, a.answer, a.votes, a.source, UNIX_TIMESTAMP(date_answered) as date_answered'.
						', u.'.$this->getUserTableField('user_id').' as img_user_id, '.$this->getUserTableFields(array('image_path', 'gender', 't_height', 't_width', 's_height', 's_width','photo_server_url', 'photo_ext', 'name'), false).$this->getUserTableField('display_name').' as answered_by'.
						' FROM '.$this->CFG['db']['tbl']['answers'].' AS a'.
						', '.$this->CFG['db']['tbl']['users'].' AS u'.
						' WHERE a.user_id=u.'.$this->getUserTableField('user_id').
						' AND u.'.$this->getUserTableField('usr_status').'=\'Ok\''.
						' AND a.ans_id='.$this->dbObj->Param('aid');

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($ans_id));
				if (!$rs)
					    trigger_db_error($this->dbObj);
				$answerDetails = array();
				if ($rs->PO_RecordCount())
					{
						$answerDetails = $rs->FetchRow();
						$answerDetails['answered_by_link'] = '<a href="'.getMemberUrl($answerDetails['user_id'], $answerDetails['name']).'">'.$answerDetails['answered_by'].'</a>';
					}
				return $answerDetails;
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function getForumTitle($forum_id)
			{
				$sql = 'SELECT seo_title AS title FROM '.$this->CFG['db']['tbl']['forums'].
						' WHERE forum_id='.$this->dbObj->Param($forum_id);
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($forum_id));
				if (!$rs)
					    trigger_db_error($this->dbObj);
				if (!$rs->PO_RecordCount())
					return false;
				$row = $rs->FetchRow();
				return $row['title'];
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function getForumDetails($forum_id)
			{
				$forumDetails = array();

				$sql = 'SELECT ft.forum_id, ft.topic_id, ft.forum_topic, ft.seo_title, ft.user_id, ft.total_response'.
						', ft.total_views, ft.last_post_user_id, ft.last_post_date, UNIX_TIMESTAMP(date_added) as date_added,';
				if($this->getUserTableField('user_id')!=''){
					$sql.=' u.'.$this->getUserTableField('user_id').' AS topic_user_id,';
					$sql.=' u.'.$this->getUserTableField('user_id').' AS img_user_id,';
				}
				if($this->getUserTableField('name')!=''){
					$sql.=' u.'.$this->getUserTableField('name').' AS topic_user_name,';
					$sql.=' u.'.$this->getUserTableField('name').' AS name,';
				}
				if($this->getUserTableField('display_name')!='')
					$sql.=' u.'.$this->getUserTableField('display_name').' AS topic_user_display_name,';
				if($this->getUserTableField('image_path')!='')
					$sql.=' u.'.$this->getUserTableField('image_path').' AS image_path,';
				if($this->getUserTableField('gender')!='')
					$sql.=' u.'.$this->getUserTableField('gender').' AS gender,';
				if($this->getUserTableField('t_height')!='')
					$sql.=' u.'.$this->getUserTableField('t_height').' AS t_height,';
				if($this->getUserTableField('t_width')!='')
					$sql.=' u.'.$this->getUserTableField('t_width').' AS t_width,';
				if($this->getUserTableField('photo_server_url')!='')
					$sql.=' u.'.$this->getUserTableField('photo_server_url').' AS photo_server_url,';
				if($this->getUserTableField('s_width')!='')
					$sql.=' u.'.$this->getUserTableField('s_width').' AS s_width,';
				if($this->getUserTableField('s_height')!='')
					$sql.=' u.'.$this->getUserTableField('s_height').' AS s_height,';
				if($this->getUserTableField('photo_ext')!='')
					$sql.=' u.'.$this->getUserTableField('photo_ext').' AS photo_ext,';
				if($this->getUserTableField('user_id')!='')
					$sql.=' u1.'.$this->getUserTableField('user_id').' AS last_user_id,';
				if($this->getUserTableField('name')!='')
					$sql.=' u1.'.$this->getUserTableField('name').' AS last_user_name,';
				if($this->getUserTableField('display_name')!='')
					$sql.=' u1.'.$this->getUserTableField('display_name').' AS last_user_display_name,';
				$sql = substr($sql, 0, strlen($sql)-1);

				$sql.=	' FROM '.$this->CFG['db']['tbl']['forum_topics'].' AS ft'.
						' LEFT JOIN '.$this->CFG['db']['tbl']['users'].' AS u1 ON ft.last_post_user_id = u1.'.$this->getUserTableField('user_id').
						', '.$this->CFG['db']['tbl']['users'].' AS u'.
						' WHERE ft.topic_id='.$this->dbObj->Param($forum_id).
						' AND ft.user_id = u.'.$this->getUserTableField('user_id').
						' AND u.'.$this->getUserTableField('usr_status').' = \'Ok\''.
						' AND topic_status = \'Yes\'';
				//echo $sql;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($forum_id));
				if (!$rs)
					    trigger_db_error($this->dbObj);

				if ($rs->PO_RecordCount())
					{
						$forumDetails = $rs->FetchRow();
						if (!($forumDetails['title'] = $this->getForumTitle($forumDetails['forum_id'])))
							return false;
						$forumDetails['forum_link'] = '<a href="'.getForumsUrl('forumsresponses', '?title='.$forumDetails['title'].'&amp;topic_title='.$forumDetails['seo_title'], $forumDetails['title'].'/'.$forumDetails['seo_title'].'/').'">'.wordWrapManual($forumDetails['forum_topic'], $this->CFG['admin']['forum']['line_length'], $this->CFG['admin']['forum']['short_length']).'</a>';
						$forumDetails['topic_user_name_link'] = '<a href="'.getMemberUrl($forumDetails['topic_user_id'], $forumDetails['topic_user_name']).'">'.stripString($forumDetails['topic_user_display_name'], $this->CFG['username']['medium_length']).'</a>';
					}
				return $forumDetails;
			}
		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function getBlogCategory($cat_id)
			{
				if (!$cat_id) return false;

				$sql = 'SELECT blog_category_name FROM '.$this->CFG['db']['tbl']['blog_category'].
					   ' WHERE blog_category_id='.$cat_id;
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
					    trigger_db_error($this->dbObj);
				if ($rs->PO_RecordCount())
					{
						$row = $rs->FetchRow();
						return $row['blog_category_name'];
					}
				return false;
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function getBlogDetails($blog_id)
			{
				$blogDetails = array();

				$sql = 'SELECT blog_id, subject, seo_title, message, blog_category_id, total_comments, total_views, b.user_id, b.video_id, b.audio_id, b.external_video_id, b.external_audio_id, UNIX_TIMESTAMP(date_added) as date_added, u.user_id, u.user_id as img_user_id, '.
						$this->getUserTableFields(array('image_path', 't_height', 't_width', 'photo_server_url', 's_width', 's_height', 'gender', 'photo_ext', 'name')).
						' FROM '.$this->CFG['db']['tbl']['blogs'].' AS b'.', '.$this->CFG['db']['tbl']['users'].' AS u'.
						' WHERE status=\'Active\''.
						' AND b.user_id = u.user_id'.
						' AND u.'.$this->getUserTableField('usr_status').' = \'Ok\''.
						' AND blog_id='.$this->dbObj->Param($blog_id);

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($blog_id));
				if (!$rs)
					    trigger_db_error($this->dbObj);

				if ($rs->PO_RecordCount())
					{
						$blogDetails = $rs->FetchRow();
						$blogDetails['blog_link'] = '<a href="'.getBlogUrl('blogcomment','?title='.$blogDetails['seo_title'], $blogDetails['seo_title'].'/').'">'.wordWrapManual($blogDetails['subject'], $this->CFG['admin']['blog']['line_length'], $this->CFG['admin']['blog']['short_length']).'</a>';
						$blogDetails['blog_user_name'] = '<a href="'.getMemberUrl($blogDetails['user_id'], $blogDetails['name']).'">'.stripString($blogDetails['name'], $this->CFG['username']['medium_length']).'</a>';
						$blogDetails['blogcategoryname'] =  $this->getBlogCategory($blogDetails['blog_category_id']);

						$blogDetails['separator_class'] = 'clsNoLeftBgPadngMrgn';
						if((chkVideoModuleAllowed('blog') AND $blogDetails['video_id'])
							OR (chkAudioModuleAllowed('blog') AND $blogDetails['audio_id'])
							OR (chkExternalVideoModuleAllowed('blog') AND $blogDetails['external_video_id'])
							OR (chkExternalAudioModuleAllowed('blog') AND $blogDetails['external_audio_id']))
							{
								$blogDetails['separator_class'] = '';
							}

						$blogDetails['ans_view_video']['url'] = getAnovaUrl('ansviewvideo', '?pg=Blog_'.$blogDetails['video_id'], '?pg=Blog_'.$blogDetails['video_id']);
						$blogDetails['ans_view_video']['on_click'] = 'blog_video_'.$blogDetails['video_id'];

						$blogDetails['ans_view_audio']['url'] = getAnovaUrl('ansviewaudio', '?pg=Blog_'.$blogDetails['audio_id'], '?pg=Blog_'.$blogDetails['audio_id']);
						$blogDetails['ans_view_audio']['on_click'] = 'blog_audio_'.$blogDetails['audio_id'];

						$blogDetails['ext_ans_view_video']['url'] = getAnovaUrl('ansviewvideo', '?pg=Blog_'.$blogDetails['external_video_id'], '?pg=Blog_'.$blogDetails['external_video_id']);
		  				$blogDetails['ext_ans_view_video']['on_click'] = 'external_video_blog_'.$blogDetails['external_video_id'];

						$blogDetails['ext_ans_view_audio']['url'] = getAnovaUrl('ansviewaudio', '?pg=Blog_'.$blogDetails['external_audio_id'], '?pg=Blog_'.$blogDetails['external_audio_id']);
						$blogDetails['ext_ans_view_audio']['on_click'] = 'external_audio_blog_'.$blogDetails['external_audio_id'];
					}

				return $blogDetails;
			}

		/**
		* @access public
		* @return void
		*/
		public function debateRatio($total_fors_value, $total_againsts_value, $type)
			{
				$total = $total_fors_value + $total_againsts_value;
				if(!$total) return;
				$ratio_for = ($total_fors_value/$total)*100;
				$ratio_againsts = ($total_againsts_value/$total)*100;
				if($type=='For')
					return round($ratio_for).'%';
				else
					return round($ratio_againsts).'%';

			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function getDebateDetails($debate_id=0)
			{
				if (isset($this->_cache_debate_details[$debate_id]))
					{
						return $this->_cache_debate_details[$debate_id];
					}

				$debateDetails = array();
				$sql = 'SELECT d.debate_id, (d.total_fors+d.total_againsts) AS total_response, d.cat_id, d.best_response_id, d.status, d.total_stars, d.subject, UNIX_TIMESTAMP(date_asked) as date_asked, d.total_againsts, d.total_fors'.
						', '.$this->getUserTableField('display_name').' as asked_by, '.$this->getUserTableFields(array('image_path', 'name', 'gender', 't_height', 't_width', 'photo_server_url', 'photo_ext', 's_width', 's_height'), false).' d.user_id'.
						', d.user_id AS img_user_id, video_id, audio_id, IF(status=\'Open\' AND date_closed>=NOW(), 1, 0) is_open, TIMEDIFF(date_closed, NOW()) as date_closed'.
						', d.total_videos, d.total_audios, d.seo_title FROM '.$this->CFG['db']['tbl']['debates'].' AS d, '.$this->CFG['db']['tbl']['users'].' AS u'.
						' WHERE d.debate_id='.$this->dbObj->Param($debate_id).
						' AND d.user_id=u.'.$this->getUserTableField('user_id').
						' AND d.status IN (\'Open\', \'Resolved\')'.
						' AND u.'.$this->getUserTableField('usr_status').'=\'Ok\'';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($debate_id));
				if (!$rs)
					trigger_db_error($this->dbObj);
				if ($rs->PO_RecordCount())
					{
						$debateDetails = $rs->FetchRow();
						$debateDetails['debate_link'] = getDebatesUrl('debatesresponse', '?title='.$debateDetails['seo_title'], $debateDetails['seo_title'].'/');
						$debateDetails['subject'] = wordWrapManual($debateDetails['subject'], $this->CFG['admin']['debate']['line_length'], $this->CFG['admin']['debate']['short_length']);
						$debateDetails['asked_by_link'] = getMemberUrl($debateDetails['user_id'], $debateDetails['name']);
						$debateDetails['user_name'] = stripString($debateDetails['asked_by'], $this->CFG['username']['medium_length']);
						$total_responses = $debateDetails['total_againsts'] + $debateDetails['total_fors'];
			    		$debateDetails['size'] = 'size50';
			    		if ($total_responses)
			    			{
					    		$fors = round(($debateDetails['total_fors']/$total_responses)*100);
					    		$remainder = $fors%5;
					    		$fors = ($remainder)?($remainder>4)?($fors+$remainder+(5-$remainder)):($fors-$remainder):$fors;
					    		$size = 'size'.$fors;
					    		$debateDetails['size'] = $size;
							}
						$debateDetails['debateRatio_for'] = $this->debateRatio($debateDetails['total_fors'],$debateDetails['total_againsts'],'For');
						$debateDetails['debateRatio_againsts'] = $this->debateRatio($debateDetails['total_fors'],$debateDetails['total_againsts'],'Againsts');
					}

				//cache debates details
				$this->_cache_debate_details[$debate_id] = $debateDetails;

				return $debateDetails;
			}
		/**
		 * To get recent questions
		 *
		 * @return void
		 * @access public
		 **/
		public function getRecentQuestions($limit = 5, $filter = 'all')
			{
				$sql = 'SELECT q.ques_id FROM '.$this->CFG['db']['tbl']['questions'].' AS q'.
						' WHERE q.status IN (\'Open\', \'Resolved\')';
				if ($filter == 'audio')
					{
						$sql .= ' AND q.audio_id != 0';
					}
				elseif ($filter == 'video')
					{
						$sql .= ' AND q.video_id != 0';
					}
				$sql .= ' AND EXISTS(SELECT 1 FROM  '.$this->CFG['db']['tbl']['users'].' AS u WHERE u.user_id = q.user_id  AND u.usr_status = \'OK\')'.
						' ORDER BY ques_id DESC LIMIT 0,'.$limit;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
						trigger_db_error($this->dbObj);

				$recent_questions = array();
				while($row = $rs->FetchRow())
					{
						$recent_questions[] = $row['ques_id'];
					}
				return $recent_questions;
			}

		/**
		 * To get recently answered questions
		 *
		 * @return void
		 * @access public
		 **/
		public function getRecentlyAnswersQuestions($limit = 5)
			{
				$limit = 5;
				$sql = 'SELECT q.ques_id FROM '.$this->CFG['db']['tbl']['questions'].' AS q'.
						' WHERE q.status IN (\'Open\', \'Resolved\') AND total_answer >0  AND EXISTS(SELECT 1 FROM  '.$this->CFG['db']['tbl']['users'].''.
						' AS u WHERE u.user_id = q.user_id  AND u.usr_status = \'OK\')'.
						' ORDER BY date_answered DESC LIMIT 0,'.$limit;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
					trigger_db_error($this->dbObj);

				$recent_questions = array();
				while($row = $rs->FetchRow())
					{
						$recent_questions[] = $row['ques_id'];
					}
				return $recent_questions;
			}

		/**
		 * To display recent questions
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function displayRecentQuestions($limit = 5, $filter = 'all', $close = NULL)
			{
				$this->displayRecentQuestions_arr = array();
				$this->displayRecentQuestions_arr['recent_questions'] = $recent_questions = $this->getRecentQuestions($limit, $filter);
				$this->displayRecentQuestions_arr['have_questions'] = 	$have_questions = false;
				if ($recent_questions)
					{
						$i = 0;
						$inc = 0;
						$this->displayRecentQuestions_arr['row'] = array();
						foreach($recent_questions as $eachQuestion)
							{
								$this->displayRecentQuestions_arr['row'][$inc]['questionDetails'] = $questionDetails = $this->getQuestionDetails($eachQuestion);
								if (!$questionDetails)
									continue;
								$this->displayRecentQuestions_arr['have_questions'] = $have_questions = true;
								$i++;
								$this->displayRecentQuestions_arr['row'][$inc]['clsOddOrEvenQuestion'] = ($i%2) ? 'clsOddQuestion' : 'clsEvenQuestion';
								$this->displayRecentQuestions_arr['row'][$inc]['answer_plural'] = ($questionDetails['total_answer']!= 1) ? $this->LANG['answers'] : $this->LANG['common_total_answer'];
								$this->displayRecentQuestions_arr['row'][$inc]['views_plural'] = ($questionDetails['total_views']!= 1) ? $this->LANG['common_total_views'] : $this->LANG['common_total_view'];
								$this->displayRecentQuestions_arr['row'][$inc]['questionDetails']['image_id'] = $questionDetails['image_id'] = 'drq';
								$inc++;
							} //foreach
						if ($have_questions)
							{
								$this->displayRecentQuestions_arr['questions_url'] = getAnovaUrl('questions', '?view=recent', 'recent/');
								if ($filter != 'all')
									$this->displayRecentQuestions_arr['questions_url'] = getAnovaUrl('questions', '?view=recent&'.$filter.'=1', 'recent/?'.$filter.'=1');
							}
					}
				return	$this->displayRecentQuestions_arr;
			}

		/**
		 * To display recent questions
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function displayRecentlyAnsweredQuestions($limit = 5 , $close = NULL)
			{
				$this->displayRecentlyAnsweredQuestions_arr = array();
				$this->displayRecentlyAnsweredQuestions_arr['recently_answered'] = $recently_answered = $this->getRecentlyAnswersQuestions($limit);
				$this->displayRecentlyAnsweredQuestions_arr['have_recently_answered'] = 	$have_recently_answered = false;
				if ($recently_answered)
					{
						$i = 0;
						$inc = 0;
						$this->displayRecentlyAnsweredQuestions_arr['row'] = array();
						foreach($recently_answered as $eachQuestion)
							{
								$this->displayRecentlyAnsweredQuestions_arr['row'][$inc]['questionDetails'] = $questionDetails = $this->getQuestionDetails($eachQuestion);
								if (!$questionDetails)
									continue;
								$this->displayRecentlyAnsweredQuestions_arr['have_recently_answered'] = $have_recently_answered = true;
								$i++;
								$this->displayRecentlyAnsweredQuestions_arr['row'][$inc]['clsOddOrEvenQuestion'] = ($i%2) ? 'clsOddQuestion' : 'clsEvenQuestion';
								$this->displayRecentlyAnsweredQuestions_arr['row'][$inc]['answer_plural'] = ($questionDetails['total_answer']!= 1) ? $this->LANG['answers'] : $this->LANG['common_total_answer'];
								$this->displayRecentlyAnsweredQuestions_arr['row'][$inc]['views_plural'] = ($questionDetails['total_views']!= 1) ? $this->LANG['common_total_views'] : $this->LANG['common_total_view'];
								$this->displayRecentlyAnsweredQuestions_arr['row'][$inc]['questionDetails']['image_id'] = $questionDetails['image_id'] = 'draq';
								$inc++;
							} //foreach
						if ($have_recently_answered)
							{
								$this->displayRecentlyAnsweredQuestions_arr['questions_url'] = getAnovaUrl('questions', '?view=recentlyanswered', 'recentlyanswered/');
							}
					}
				return	$this->displayRecentlyAnsweredQuestions_arr;
			}

		/**
		 * To get popular questions
		 *
		 * @return void
		 * @access public
		 **/
		public function getPopularQuestions($limit = 5)
			{
				$sql = 'SELECT q.ques_id FROM '.$this->CFG['db']['tbl']['questions'].' AS q'.
						' WHERE q.status IN (\'Open\', \'Resolved\')  AND EXISTS(SELECT 1 FROM  '.$this->CFG['db']['tbl']['users'].''.
						' AS u WHERE u.user_id = q.user_id  AND u.usr_status = \'OK\')'.
						' AND total_views > 0 AND latest_viewed >= DATE_SUB(NOW(), INTERVAL '.$this->CFG['admin']['popular_days']['limit'].' DAY)'.
						' ORDER BY total_views DESC LIMIT 0,'.$limit;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
						trigger_db_error($this->dbObj);

				$popular_questions = array();
				while($row = $rs->FetchRow())
					{
						$popular_questions[] = $row['ques_id'];
					}
				return $popular_questions;
			}

		/**
		 * To display popular questions
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function displayPopularQuestions($limit = 5 ,  $close = NULL )
			{
				$this->displayPopularQuestions_arr = array();
				$this->displayPopularQuestions_arr['popular_questions'] = $popular_questions = $this->getPopularQuestions($limit);
				$this->displayPopularQuestions_arr['found'] = 	$found = false;
				if ($popular_questions)
					{
						$inc = 0;
						$this->displayPopularQuestions_arr['row'] = array();
						$i = 0;
						foreach($popular_questions as $eachQuestion)
							{
								$this->displayPopularQuestions_arr['row'][$inc]['questionDetails'] = $questionDetails = $this->getQuestionDetails($eachQuestion);
								if (!$questionDetails)
									continue;
								$i++;
								$this->displayPopularQuestions_arr['found'] = 	$found = true;
								$this->displayPopularQuestions_arr['row'][$inc]['clsOddOrEvenQuestion'] = ($i%2) ? 'clsOddQuestion' : 'clsEvenQuestion';
								$this->displayPopularQuestions_arr['row'][$inc]['answer_plural'] = ($questionDetails['total_answer']!= 1) ? $this->LANG['answers'] : $this->LANG['common_total_answer'];
								$this->displayPopularQuestions_arr['row'][$inc]['views_plural'] = ($questionDetails['total_views']!= 1) ? $this->LANG['common_total_views'] : $this->LANG['common_total_view'];
								$this->displayPopularQuestions_arr['row'][$inc]['questionDetails']['image_id'] = $questionDetails['image_id'] = 'dpq';
								$inc++;
							}
						if ($found)
							{
								$this->displayPopularQuestions_arr['questions_url'] = getAnovaUrl('questions', '?view=popular', 'popular/');
							}
					}
					return $this->displayPopularQuestions_arr;
			}

		/**
		 * To get video questions
		 *
		 * @return void
		 * @access public
		 **/
		public function getVideoQuestions($limit = 5)
			{
				$sql = 'SELECT q.ques_id FROM '.$this->CFG['db']['tbl']['questions'].' AS q'.
						' WHERE q.status IN (\'Open\', \'Resolved\') AND EXISTS(SELECT 1 FROM  '.$this->CFG['db']['tbl']['users'].
						' AS u WHERE u.user_id = q.user_id  AND u.usr_status = \'OK\')'.
						' AND (video_id != 0 OR external_video_id != 0) ORDER BY ques_id DESC LIMIT 0,'.$limit;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
						trigger_db_error($this->dbObj);

				$video_questions = array();
				while($row = $rs->FetchRow())
					{
						$video_questions[] = $row['ques_id'];
					}
				return $video_questions;
			}
		  	/**
		 * get username for user_id
		 *
		 * @param 		string $user_table
		 * @param 		integer $user_id
		 * @return 		string
		 * @access 		public
		 */
		public function getUserDetailsFromUsersTable($user_table, $user_id)
			{
				if (isset($this->_cache_user_details[$user_id]))
					{
						return $this->_cache_user_details[$user_id];
					}

				$sql = 'SELECT '.$this->getUserTableFields(array('user_id', 'name', 'display_name', 'email', 'image_path', 'bio', 'gender', 'usr_status', 'user_access', 't_height', 't_width', 's_height', 's_width', 'photo_server_url', 'photo_ext')).
						', '.$this->getUserTableField('user_id').' AS img_user_id'.
						' FROM '.$user_table.
						' WHERE '.$this->getUserTableField('user_id').' = '.$this->dbObj->Param($user_id);
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($user_id));
				if (!$rs)
						trigger_db_error($this->dbObj);
				$row = array('name'=>'', 'display_name'=>'', 'email'=>'', 'image_path'=>'', 'gender'=>'', 'usr_status'=>'', 'user_access'=>'', 'img_user_id'=>'', 't_height'=>'', 't_width'=>'', 's_height'=>'', 's_width'=>'', 'photo_server_url'=>'', 'photo_ext'=>'');
				if ($rs->PO_RecordCount())
					{
						$row = $rs->FetchRow();
					}

				//cache user detalis
				$this->_cache_user_details[$user_id] = $row;

				return	$row;
			}


		/**
		 * To display video questions
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function displayVideoQuestions($limit = 5, $close = NULL)
			{
				$this->displayVideoQuestions_arr = array();
				$this->displayVideoQuestions_arr['video_questions'] = $video_questions = $this->getVideoQuestions($limit);
				$this->displayVideoQuestions_arr['have_questions'] = $have_questions = false;
				if ($video_questions)
					{
						$inc = 0;
						$this->displayVideoQuestions_arr['row'] = array();
						$i = 0;
						foreach($video_questions as $eachQuestion)
							{
								$this->displayVideoQuestions_arr['row'][$inc]['questionDetails'] = $questionDetails = $this->getQuestionDetails($eachQuestion);
								if (!$questionDetails)
									continue;
								$this->displayVideoQuestions_arr['have_questions'] = $have_questions = true;
								$i++;
								$this->displayVideoQuestions_arr['row'][$inc]['clsOddOrEvenQuestion'] = ($i%2) ? 'clsOddQuestion' : 'clsEvenQuestion';
								$this->displayVideoQuestions_arr['row'][$inc]['answer_plural'] = ($questionDetails['total_answer']!= 1) ? $this->LANG['answers'] : $this->LANG['common_total_answer'];
								$this->displayVideoQuestions_arr['row'][$inc]['views_plural'] = ($questionDetails['total_views']!= 1) ? $this->LANG['common_total_views'] : $this->LANG['common_total_view'];
								$this->displayVideoQuestions_arr['row'][$inc]['questionDetails']['image_id'] = $questionDetails['image_id'] = 'dvq';
								$inc++;
							} //foreach
						if ($have_questions)
							{
								$this->displayVideoQuestions_arr['questions_url'] = getAnovaUrl('questions', '?view=search&video=1', 'search/?video=1');
							}
					}
					return $this->displayVideoQuestions_arr;
			}

		/**
		 * To get audio questions
		 *
		 * @return void
		 * @access public
		 **/
		public function getAudioQuestions($limit = 5)
			{
				$sql = 'SELECT q.ques_id FROM '.$this->CFG['db']['tbl']['questions'].' AS q'.
						' WHERE q.status IN (\'Open\', \'Resolved\')  AND EXISTS(SELECT 1 FROM  '.$this->CFG['db']['tbl']['users'].''.
						' AS u WHERE u.user_id = q.user_id  AND u.usr_status = \'OK\')'.
						' AND (audio_id != 0 OR external_audio_id != 0) ORDER BY ques_id DESC LIMIT 0,'.$limit;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
						trigger_db_error($this->dbObj);

				$audio_questions = array();
				while($row = $rs->FetchRow())
					{
						$audio_questions[] = $row['ques_id'];
					}
				return $audio_questions;
			}

		/**
		 * To display audio questions
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function displayAudioQuestions($limit = 5 , $close = NULL)
			{
				$this->displayAudioQuestions_arr = array();
				$this->displayAudioQuestions_arr['audio_questions'] = $audio_questions = $this->getAudioQuestions($limit);
				$this->displayAudioQuestions_arr['have_questions'] = $have_questions = false;
				if ($audio_questions)
					{
						$inc = 0;
						$this->displayAudioQuestions_arr['row'] = array();
						$i = 0;
						foreach($audio_questions as $eachQuestion)
							{
								$this->displayAudioQuestions_arr['row'][$inc]['questionDetails'] = $questionDetails = $this->getQuestionDetails($eachQuestion);
								if (!$questionDetails)
									continue;
								$this->displayAudioQuestions_arr['have_questions'] = $have_questions = true;
								$i++;
								$this->displayAudioQuestions_arr['row'][$inc]['clsOddOrEvenQuestion'] = ($i%2) ? 'clsOddQuestion' : 'clsEvenQuestion';
								$this->displayAudioQuestions_arr['row'][$inc]['answer_plural'] = ($questionDetails['total_answer']!= 1) ? $this->LANG['answers'] : $this->LANG['common_total_answer'];
								$this->displayAudioQuestions_arr['row'][$inc]['views_plural'] = ($questionDetails['total_views']!= 1) ? $this->LANG['common_total_views'] : $this->LANG['common_total_view'];
								$this->displayAudioQuestions_arr['row'][$inc]['questionDetails']['image_id'] = $questionDetails['image_id'] = 'daq';
								$inc++;
							} //foreach
						if ($have_questions)
							{
								$this->displayAudioQuestions_arr['questions_url'] = getAnovaUrl('questions', '?view=search&audio=1', 'search/?audio=1');
							}
					}
				return 	$this->displayAudioQuestions_arr;
			}

		/**
		 * To get recent forums
		 *
		 * @return void
		 * @access public
		 **/
		public function getRecentForums($limit = 5, $topic_user_id='')
			{
				$condition = '';
				if($topic_user_id)
					$condition = ' AND ft.user_id='.$topic_user_id;

				$sql = 'SELECT ft.topic_id FROM '.$this->CFG['db']['tbl']['forum_topics'].' AS ft'.
						' WHERE topic_status = \'Yes\''.$condition.' AND EXISTS(SELECT 1 FROM  '.$this->CFG['db']['tbl']['users'].
						' AS u WHERE u.user_id = ft.user_id  AND u.usr_status = \'OK\')'.
						' ORDER BY ft.topic_id DESC LIMIT 0,'.$limit;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
						trigger_db_error($this->dbObj);

				$recent_forums = array();
				while($row = $rs->FetchRow())
					{
						$recent_forums[] = $row['topic_id'];
					}
				return $recent_forums;
			}

		/**
		 * To display recent forums
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function displayRecentForums($limit = 5, $topic_user_id='', $close = NULL)
			{
				$this->displayRecentForums_arr = array();
				$this->displayRecentForums_arr['isUser'] = $isUser = false;
				$this->displayRecentForums_arr['common_recent_forum_title'] = $this->LANG['common_recent_forum_title'];

				if ($topic_user_id == 'most_replied')
					{
						$this->displayRecentForums_arr['recent_forums'] = $recent_forums = $this->getMostRepliedForums($limit);
					}
				elseif ($topic_user_id == 'recent_forums')
					{
						$this->displayRecentForums_arr['recent_forums'] = $recent_forums = $this->getRecentForums($limit);
					}
				elseif(is_numeric($topic_user_id))
					{
						$this->displayRecentForums_arr['isUser'] = $isUser = true;
						$this->displayRecentForums_arr['common_recent_forum_title'] = $this->LANG['common_recent_forum_title'] = $this->LANG['forums'];
						$this->displayRecentForums_arr['recent_forums'] = $recent_forums = $this->getRecentForums($limit, $topic_user_id);
					}
				else
					{
						$this->displayRecentForums_arr['recent_forums'] = $recent_forums = $this->getRecentForums($limit);
					}

				$this->displayRecentForums_arr['have_forums'] = $have_forums = false;
  				$this->displayRecentForums_arr['uname'] = $uname = '';

				if ($recent_forums)
					{
						$inc = 0;
						$i = 0;
						$this->displayRecentForums_arr['row'] = array();
						foreach($recent_forums as $eachForum)
							{
								$this->displayRecentForums_arr['row'][$inc]['forumDetails'] = $forumDetails = $this->getForumDetails($eachForum);
								if (!$forumDetails)
									continue;
								$this->displayRecentForums_arr['row'][$inc]['name'] = $name = $forumDetails['topic_user_name'];
								$this->displayRecentForums_arr['have_forums'] = $have_forums = true;
								$i++;
								$this->displayRecentForums_arr['row'][$inc]['clsOddOrEvenQuestion'] = $clsOddOrEvenQuestion = 'clsEvenQuestion';
								if ($i%2)
									$this->displayRecentForums_arr['row'][$inc]['clsOddOrEvenQuestion'] = $clsOddOrEvenQuestion = 'clsOddQuestion';
								$this->displayRecentForums_arr['row'][$inc]['resp_plural'] = $resp_plural = $this->LANG['common_total_response'];
								$this->displayRecentForums_arr['row'][$inc]['view_plural'] = $view_plural = $this->LANG['common_total_view'];
								if($forumDetails['total_response'] != 1)
									$this->displayRecentForums_arr['row'][$inc]['resp_plural'] = $resp_plural = $this->LANG['common_total_responses'];
								if($forumDetails['total_views'] != 1)
									$this->displayRecentForums_arr['row'][$inc]['view_plural'] = $view_plural = $this->LANG['common_total_views'];
								$this->displayRecentForums_arr['row'][$inc]['forumDetails']['image_id'] = $forumDetails['image_id'] = 'drf';
								$inc++;
							} //foreach
						if ($have_forums)
							{
								if($isUser)
									{
										$this->displayRecentForums_arr['forumsearch_url'] = getForumsUrl('forumsearch', '?uname='.$name, '?uname='.$name);
									}
								else
									{
										$this->displayRecentForums_arr['forumsearch_url'] = getForumsUrl('forums');
									}
							}
					}
				return $this->displayRecentForums_arr;
			}

		/**
		 * To get most replied forums
		 *
		 * @return void
		 * @access public
		 **/
		public function getMostRepliedForums($limit = 5)
			{
				$sql = 'SELECT ft.topic_id FROM '.$this->CFG['db']['tbl']['forum_topics'].' AS ft'.
						' WHERE topic_status = \'Yes\' AND total_response > 0 AND EXISTS(SELECT 1 FROM  '.$this->CFG['db']['tbl']['users'].
						' AS u WHERE u.user_id = ft.user_id  AND u.usr_status = \'OK\')'.
						' ORDER BY ft.total_response DESC LIMIT 0,'.$limit;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
						trigger_db_error($this->dbObj);

				$most_replied_forums = array();
				while($row = $rs->FetchRow())
					{
						$most_replied_forums[] = $row['topic_id'];
					}
				return $most_replied_forums;
			}

		/**
		 * To display most replied forums
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function displayMostForumReplies($limit = 5, $close =NULL)
			{
				$this->displayMostForumReplies_arr = array();
				$this->displayMostForumReplies_arr['most_replied_forums'] = $most_replied_forums = $this->getMostRepliedForums($limit);
				$this->displayMostForumReplies_arr['have_forums'] = $have_forums = false;
				if ($most_replied_forums)
					{
						$inc = 0;
						$this->displayMostForumReplies_arr['row'] = array();
						$i = 0;
						foreach($most_replied_forums as $eachForum)
							{
								$this->displayMostForumReplies_arr['row'][$inc]['forumDetails'] = $forumDetails = $this->getForumDetails($eachForum);
								if (!$forumDetails)
									continue;
								$this->displayMostForumReplies_arr['have_forums'] = $have_forums = true;
								$i++;
								$this->displayMostForumReplies_arr['row'][$inc]['clsOddOrEvenQuestion'] = $clsOddOrEvenQuestion = 'clsEvenQuestion';
								if ($i%2)
									$this->displayMostForumReplies_arr['row'][$inc]['clsOddOrEvenQuestion'] = $clsOddOrEvenQuestion = 'clsOddQuestion';
								$this->displayMostForumReplies_arr['row'][$inc]['resp_plural'] = $resp_plural = $this->LANG['common_total_response'];
								$this->displayMostForumReplies_arr['row'][$inc]['view_plural'] = $view_plural = $this->LANG['common_total_view'];
								if($forumDetails['total_response'] != 1)
									$this->displayMostForumReplies_arr['row'][$inc]['resp_plural'] = $resp_plural = $this->LANG['common_total_responses'];
								if($forumDetails['total_views'] != 1)
									$this->displayMostForumReplies_arr['row'][$inc]['view_plural'] = $view_plural = $this->LANG['common_total_views'];
								$this->displayMostForumReplies_arr['row'][$inc]['forumDetails']['image_id'] = $forumDetails['image_id'] = 'dmfr';
								$inc++;
							} //foreach
						if ($have_forums)
							{
								$this->displayMostForumReplies_arr['forums_url'] = getForumsUrl('forums');
							}
					}
					return $this->displayMostForumReplies_arr;
			}

		/**
		 * To get recent blogs
		 *
		 * @return void
		 * @access public
		 **/
		public function getRecentBlogs($limit = 5, $blog_user_id='')
			{
				$condition = '';
				if($blog_user_id)
					$condition = ' AND user_id='.$blog_user_id;

				$sql = 'SELECT blog_id FROM '.$this->CFG['db']['tbl']['blogs'].' AS b'.
						' WHERE status=\'Active\''.$condition.' AND EXISTS(SELECT 1 FROM  '.$this->CFG['db']['tbl']['users'].
						' AS u WHERE u.user_id = b.user_id  AND u.usr_status = \'OK\')'.
						' ORDER BY blog_id DESC LIMIT 0,'.$limit;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
						trigger_db_error($this->dbObj);

				$recent_blogs = array();
				while($row = $rs->FetchRow())
					{
						$recent_blogs[] = $row['blog_id'];
					}
				return $recent_blogs;
			}

		/**
		 * To display recent blogs
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function displayRecentBlogs($limit = 5, $blog_user_id='',  $close = NULL)
			{
				$this->displayRecentBlogs_arr = array();
				$this->displayRecentBlogs_arr['is_user'] = $is_user = false;

				if ($blog_user_id == 'most_commented')
					{
						$this->displayRecentBlogs_arr['recent_blogs'] = $recent_blogs = $this->getMostCommentedBlogs($limit);
					}
				elseif ($blog_user_id == 'recent_blogs')
					{
						$this->displayRecentBlogs_arr['recent_blogs'] = $recent_blogs = $this->getRecentBlogs($limit);
					}
				elseif(is_numeric($blog_user_id))
					{
						$this->displayRecentBlogs_arr['is_user'] = $is_user = true;
						$this->displayRecentBlogs_arr['common_recent_blog_title'] = $this->LANG['common_recent_blog_title'] = $this->LANG['blogs'];
						$this->displayRecentBlogs_arr['recent_blogs'] = $recent_blogs = $this->getRecentBlogs($limit,$blog_user_id);
					}
				else
					{
						$this->displayRecentBlogs_arr['recent_blogs'] = $recent_blogs = $this->getRecentBlogs($limit);
					}

				$this->displayRecentBlogs_arr['have_blogs'] = $have_blogs = false;
				if ($recent_blogs)
					{
						$inc = 0;
						$this->displayRecentBlogs_arr['row'] = array();
						$i = 0;
						foreach($recent_blogs as $eachBlog)
							{
								$this->displayRecentBlogs_arr['row'][$inc]['blogDetails'] = $blogDetails = $this->getBlogDetails($eachBlog);
								if (!$blogDetails)
									continue;
								$this->displayRecentBlogs_arr['have_blogs'] = $have_blogs = true;
								$i++;
								$this->displayRecentBlogs_arr['row'][$inc]['clsOddOrEvenQuestion'] = $clsOddOrEvenQuestion = 'clsEvenQuestion';
								if ($i%2)
									$this->displayRecentBlogs_arr['row'][$inc]['clsOddOrEvenQuestion'] = $clsOddOrEvenQuestion = 'clsOddQuestion';
								$this->displayRecentBlogs_arr['row'][$inc]['comment_plural'] = $comment_plural = $this->LANG['common_total_comment'];
								$this->displayRecentBlogs_arr['row'][$inc]['view_plural'] = $view_plural = $this->LANG['common_total_view'];
								if($blogDetails['total_comments'] != 1)
									$this->displayRecentBlogs_arr['row'][$inc]['comment_plural'] = $comment_plural = $this->LANG['common_total_comments'];
								if($blogDetails['total_views'] != 1)
									$this->displayRecentBlogs_arr['row'][$inc]['view_plural'] = $view_plural = $this->LANG['common_total_views'];
								$inc++;
							} //foreach
							if ($have_blogs)
								{
									if($is_user)
										{
											$this->displayRecentBlogs_arr['userDetails'] = $userDetails = $this->getUserDetails($blog_user_id);
											$this->displayRecentBlogs_arr['uname'] = $uname = $userDetails['name'];
											$this->displayRecentBlogs_arr['blogs_url'] = getBlogUrl('blogs', '?uname='.$uname, '?uname='.$uname);
										}
									else
										{
											$this->displayRecentBlogs_arr['blogs_url'] = getBlogUrl('blogs');
										}
								}
						}
					return $this->displayRecentBlogs_arr;
			}

		/**
		 * To get most commented blogs
		 *
		 * @return void
		 * @access public
		 **/
		public function getMostCommentedBlogs($limit = 5)
			{
				$most_commented_blogs = array();
				$sql = 'SELECT blog_id, total_comments, b.user_id, date_added, u.'.$this->getUserTableField('user_id').' AS img_user_id, '.
						$this->getUserTableFields(array('image_path', 'name', 'gender', 't_height', 't_width', 'photo_server_url', 'photo_ext', 's_width', 's_height')).
						' FROM '.$this->CFG['db']['tbl']['blogs'].' AS b'.', '.$this->CFG['db']['tbl']['users'].' AS u'.
						' WHERE status=\'Active\''.
						' AND total_comments>0 AND EXISTS(SELECT 1 FROM  '.$this->CFG['db']['tbl']['users'].
						' AS u WHERE u.user_id = b.user_id  AND u.usr_status = \'OK\')'.
						' AND b.user_id = u.user_id'.
						' AND u.'.$this->getUserTableField('usr_status').' = \'Ok\''.
						' ORDER BY total_comments DESC LIMIT 0,'.$limit;
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
						trigger_db_error($this->dbObj);
				while($row = $rs->FetchRow())
					{
						$most_commented_blogs[] = $row['blog_id'];
					}
				return $most_commented_blogs;
			}

		/**
		 * To display most commented blogs
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function displayMostBlogComments($limit = 5, $close = NULL)
			{
				$this->displayMostBlogComments_arr = array();
				$this->displayMostBlogComments_arr['most_commented_blogs'] = $most_commented_blogs = $this->getMostCommentedBlogs($limit);
				$this->displayMostBlogComments_arr['have_blogs'] = $have_blogs = false;
				if ($most_commented_blogs)
					{
						$i = 0;
						$inc = 0;
						$this->displayMostBlogComments_arr['row'] = array();
						foreach($most_commented_blogs as $eachBlog)
							{
								$this->displayMostBlogComments_arr['row'][$inc]['blogDetails'] = $blogDetails = $this->getBlogDetails($eachBlog);
								if (!$blogDetails)
									continue;
								$this->displayMostBlogComments_arr['have_blogs'] = $have_blogs = true;
								$i++;
								$this->displayMostBlogComments_arr['row'][$inc]['clsOddOrEvenQuestion'] = $clsOddOrEvenQuestion = 'clsEvenQuestion';
								if ($i%2)
								$this->displayMostBlogComments_arr['row'][$inc]['clsOddOrEvenQuestion'] = $clsOddOrEvenQuestion = 'clsOddQuestion';
								$this->displayMostBlogComments_arr['row'][$inc]['comment_plural'] = $comment_plural = $this->LANG['common_total_comment'];
								$this->displayMostBlogComments_arr['row'][$inc]['view_plural'] = $view_plural = $this->LANG['common_total_view'];
								if($blogDetails['total_comments'] != 1)
									$this->displayMostBlogComments_arr['row'][$inc]['comment_plural'] = $comment_plural = $this->LANG['common_total_comments'];
								if($blogDetails['total_views'] != 1)
									$this->displayMostBlogComments_arr['row'][$inc]['view_plural'] = $view_plural = $this->LANG['common_total_views'];
								$inc++;
							} //foreach
						if ($have_blogs)
							{
								$this->displayMostBlogComments_arr['blogs_url'] = getBlogUrl('blogs', '?comments=more', '?comments=more');

							}
					}
				return 	$this->displayMostBlogComments_arr;
			}
		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function getBestAnsQuestions($limit = 5)
			{
				$sql = 'SELECT q.ques_id FROM '.$this->CFG['db']['tbl']['questions'].' AS q'.
						' WHERE q.status IN (\'Open\', \'Resolved\') AND best_ans_id!=0'.
						' ORDER BY date_answered DESC LIMIT 0,'.$limit;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
						trigger_db_error($this->dbObj);

				$best_ans_questions = array();
				while($row = $rs->FetchRow())
					{
						$best_ans_questions[] = $row['ques_id'];
					}
				return $best_ans_questions;
			}


		/**
		 * get user details user_id
		 *
		 * @param 		integer $user_id
		 * @return 		string
		 * @access 		public
		 */
		public function getUserDetails($user_id)
			{
				if (isset($this->_cache_user_details[$user_id]))
					{
						return $this->_cache_user_details[$user_id];
					}

				$sql = 'SELECT '.$this->getUserTableFields(array('user_id', 'display_name', 'name', 'email', 'image_path', 'gender', 'usr_status', 'user_access', 't_height', 't_width', 's_height', 's_width', 'photo_server_url', 'photo_ext')).
						', '.$this->getUserTableField('user_id').' AS img_user_id'.
						' FROM '.$this->CFG['db']['tbl']['users'].
						' WHERE '.$this->getUserTableField('user_id').'='.$this->dbObj->Param($user_id);
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($user_id));
				if (!$rs)
						trigger_db_error($this->dbObj);
				$row = array('name'=>'', 'display_name'=>'', 'email'=>'', 'image_path'=>'', 'gender'=>'', 'usr_status'=>'', 'user_access'=>'', 'img_user_id'=>'', 't_height'=>'', 't_width'=>'', 's_height'=>'', 's_width'=>'', 'photo_server_url'=>'', 'photo_ext'=>'');
				if ($rs->PO_RecordCount())
					{
						$row = $rs->FetchRow();
					}
				//cache user details
				$this->_cache_user_details[$user_id] = $row;

				return	$row;
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function getUserLog($user_id)
			{
				if (isset($this->_cache_user_log_details[$user_id]))
					{
						return $this->_cache_user_log_details[$user_id];
					}

				$sql = 'SELECT total_debates, total_debate_response, total_best_debate_response, total_ques, total_ans, total_points'.
						', DATE_FORMAT(date_updated,\'%D %b %Y\') as date_updated'.
						' FROM '.$this->CFG['db']['tbl']['users_ans_log'].
						' WHERE user_id='.$this->dbObj->Param($user_id);
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($user_id));
				if (!$rs)
				        trigger_db_error($this->dbObj);
				$user_ans_log = array('total_ques'=>'0', 'total_ans'=>'0', 'total_points'=>'0', 'date_updated'=>'');
				if ($rs->PO_RecordCount())
					$user_ans_log = $rs->FetchRow();

				//cache user log details
				$this->_cache_user_log_details[$user_id] = $user_ans_log;

				return $user_ans_log;
			}

		/**
		 *
		 * @access public
		 * @return void
		 **/
		public function getResolvedQuestions($uid)
		    {
				$sql = 'SELECT COUNT(ques_id) AS resolved FROM '.$this->CFG['db']['tbl']['questions'].
						' WHERE user_id='.$this->dbObj->Param($uid).' AND status NOT IN(\'Blocked\',\'ToActivate\') AND date_closed<NOW()';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($uid));
				if (!$rs)
				        trigger_db_error($this->dbObj);
				$row = $rs->FetchRow();
				return $row['resolved'];
		    }

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function getFeaturedAnalystIds($limit)
			{
				$sql = 'SELECT user_id FROM '.$this->CFG['db']['tbl']['users_ans_log'].
						' as u WHERE date_updated >= DATE_SUB(NOW(), INTERVAL '.$this->CFG['admin']['index']['featured_analyst_period'].' DAY)'.
						' AND EXISTS(SELECT 1 FROM '.$this->CFG['db']['tbl']['users'].
						' WHERE user_id=u.user_id AND usr_status=\'Ok\')'.
						' ORDER BY total_points DESC LIMIT 0, '.$limit;
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
					    trigger_db_error($this->dbObj);

				$featured_analysts = array();
				while($row = $rs->FetchRow())
					{
						$featured_analysts[] = $row['user_id'];
					}
				return $featured_analysts;
			}

		/**
		 * To display featured analyst
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function displayFeaturedAnalyst($limit = 2, $close = NULL)
			{
				$this->displayFeaturedAnalyst_arr = array();
				$this->displayFeaturedAnalyst_arr['featured_analysts'] = $featured_analysts = $this->getFeaturedAnalystIds($limit);
				$this->displayFeaturedAnalyst_arr['have_users'] = $have_users = false;
				if ($featured_analysts)
					{
						$i = 0;
						$inc = 0;
						$this->displayFeaturedAnalyst_arr['row'] = array();
						foreach($featured_analysts as $eachMember) {
							$this->displayFeaturedAnalyst_arr['row'][$inc]['userDetails'] = $userDetails = $this->getUserDetails($eachMember);
							if (!$userDetails)
								continue;
							$this->displayFeaturedAnalyst_arr['have_users'] = $have_users = true;
							$i++;
							$this->displayFeaturedAnalyst_arr['row'][$inc]['clsOddOrEvenQuestion'] = $clsOddOrEvenQuestion = 'clsEvenQuestion';
							if ($i%2)
								$this->displayFeaturedAnalyst_arr['row'][$inc]['clsOddOrEvenQuestion'] = $clsOddOrEvenQuestion = 'clsOddQuestion';
							$this->displayFeaturedAnalyst_arr['row'][$inc]['uid'] = $uid = $userDetails['user_id'];
							$this->displayFeaturedAnalyst_arr['row'][$inc]['userLog'] = $userLog = $this->getUserLog($uid);
							$this->displayFeaturedAnalyst_arr['row'][$inc]['uname'] = $uname = $userDetails['name'];
							$this->displayFeaturedAnalyst_arr['row'][$inc]['getResolvedQuestions'] = $this->getResolvedQuestions($uid);
							$this->displayFeaturedAnalyst_arr['row'][$inc]['userLevelNumber'] = '';
							if($this->CFG['admin']['user_levels']['allowed'])
								$this->displayFeaturedAnalyst_arr['row'][$inc]['userLevelNumber'] = getUserLevelNumber($userLog['total_points']);
							$this->displayFeaturedAnalyst_arr['row'][$inc]['myanswers_url'] = getMemberUrl($userDetails['user_id'], $userDetails['name']);
							$this->displayFeaturedAnalyst_arr['row'][$inc]['questions_ques_url'] = getAnovaUrl('questions', '?view=search&amp;uname='.$uname.'&amp;opt=ques', 'search/?uname='.$uname.'&amp;opt=ques');
							$this->displayFeaturedAnalyst_arr['row'][$inc]['questions_resolve_url'] = getAnovaUrl('questions', '?view=search&amp;uname='.$uname.'&amp;opt=resolve', 'search/?uname='.$uname.'&amp;opt=resolve');
							$this->displayFeaturedAnalyst_arr['row'][$inc]['questions_ans_url'] = getAnovaUrl('questions', '?view=search&amp;uname='.$uname.'&amp;opt=ans', 'search/?uname='.$uname.'&amp;opt=ans');
							//debate module linkd
							if($this->CFG['admin']['module']['debate'])
								{
									$this->displayFeaturedAnalyst_arr['row'][$inc]['debates_asked']	=	getDebatesUrl('debates', '?view=search&uname='.$uname.'&opt=debt', 'search/?uname='.$uname.'&opt=debt');
									$this->displayFeaturedAnalyst_arr['row'][$inc]['resolved_debates_index']	=	getDebatesUrl('debates', '?view=search&uname='.$uname.'&opt=resolve', 'search/?uname='.$uname.'&opt=resolve');
									$this->displayFeaturedAnalyst_arr['row'][$inc]['debateResponse_total']	=	getDebatesUrl('debates', '?view=search&uname='.$uname.'&opt=resp', 'search/?uname='.$uname.'&opt=resp');
									$this->displayFeaturedAnalyst_arr['row'][$inc]['resolved_debate_text'] = $this->getResolvedDebates($uid);
								}
							$inc++;
						} //foreach
					}
				return $this->displayFeaturedAnalyst_arr;
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function getTopAnalystIds($limit)
			{
				$sql = 'SELECT user_id FROM '.$this->CFG['db']['tbl']['users_ans_log'].
						' as u WHERE total_points > 0'.
						' AND EXISTS(SELECT 1 FROM '.$this->CFG['db']['tbl']['users'].
						' WHERE user_id=u.user_id AND usr_status=\'Ok\')'.
						' ORDER BY total_points DESC LIMIT 0, '.$limit;
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
					    trigger_db_error($this->dbObj);

				$top_analysts = array();
				while($row = $rs->FetchRow())
					{
						$top_analysts[] = $row['user_id'];
					}
				return $top_analysts;
			}

		/**
		 * To display top analysts
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function displayTopAnalyst($limit=5, $close = NULL)
			{
				$this->displayTopAnalyst_arr = array();
				$this->displayTopAnalyst_arr['top_analysts'] = $top_analysts = $this->getTopAnalystIds($limit);
				$this->displayTopAnalyst_arr['have_analysts'] = $have_analysts = false;
				if ($top_analysts)
					{
						$i = 0;
						$inc = 0;
						$this->displayTopAnalyst_arr['row'] = array();
						foreach($top_analysts as $eachMember)
							{
								$this->displayTopAnalyst_arr['row'][$inc]['userDetails'] = $userDetails = $this->getUserDetails($eachMember);
								if (!$userDetails)
									continue;
								$this->displayTopAnalyst_arr['have_analysts'] = $have_analysts = true;
								$this->displayTopAnalyst_arr['row'][$inc]['uid'] = $uid = $userDetails['user_id'];
								$this->displayTopAnalyst_arr['row'][$inc]['userLog'] = $userLog = $this->getUserLog($uid);
								$this->displayTopAnalyst_arr['row'][$inc]['uname'] = $uname = $userDetails['name'];
								$this->displayTopAnalyst_arr['row'][$inc]['userLevelNumber'] = '';
								$this->displayTopAnalyst_arr['row'][$inc]['getResolvedQuestions'] = $this->getResolvedQuestions($uid);
								if($this->CFG['admin']['user_levels']['allowed'])
									$this->displayTopAnalyst_arr['row'][$inc]['userLevelNumber'] = getUserLevelNumber($userLog['total_points']);
								$i++;
								$this->displayTopAnalyst_arr['row'][$inc]['clsOddOrEvenQuestion'] = $clsOddOrEvenQuestion = 'clsEvenQuestion';
								if ($i%2)
									$this->displayTopAnalyst_arr['row'][$inc]['clsOddOrEvenQuestion'] = $clsOddOrEvenQuestion = 'clsOddQuestion';
								$this->displayTopAnalyst_arr['row'][$inc]['userDetails']['image_id'] = $userDetails['image_id'] = 'dta';
								$this->displayTopAnalyst_arr['row'][$inc]['myanswers_url'] = getMemberUrl($userDetails['user_id'], $uname);
								$this->displayTopAnalyst_arr['row'][$inc]['stripString_display_name'] = stripString($userDetails['display_name'], $this->CFG['username']['short_length']);
								$this->displayTopAnalyst_arr['row'][$inc]['questions_ques_url'] = getAnovaUrl('questions', '?view=search&amp;uname='.$uname.'&amp;opt=ques', 'search/?uname='.$uname.'&amp;opt=ques');
								$this->displayTopAnalyst_arr['row'][$inc]['questions_resolve_url'] = getAnovaUrl('questions', '?view=search&amp;uname='.$uname.'&amp;opt=resolve', 'search/?uname='.$uname.'&amp;opt=resolve');
								$this->displayTopAnalyst_arr['row'][$inc]['questions_ans_url'] = getAnovaUrl('questions', '?view=search&amp;uname='.$uname.'&amp;opt=ans', 'search/?uname='.$uname.'&amp;opt=ans');
								$inc++;
							} //foreach
						if ($have_analysts)
							{
								$this->displayTopAnalyst_arr['topanalysts_url'] = getAnovaUrl('topanalysts');
							}
					}
				return $this->displayTopAnalyst_arr;
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function showThisWeekExperts($limit=5, $close = NULL)
			{
				$this->showThisWeekExperts_arr = array();
				$this->showThisWeekExperts_arr['analysts_this_week_note'] = $this->LANG['analysts_this_week_note'] = str_ireplace('VAR_REFRESH_TIME',$this->CFG['admin']['week_experts']['refresh'],$this->LANG['analysts_this_week_note']);
				$this->showThisWeekExperts_arr['top_analysts'] = $top_analysts = $this->thisWeekExperts($limit);
				$this->showThisWeekExperts_arr['have_analysts'] = $have_analysts = false;
				if ($top_analysts)
					{
						$i=0;
						$inc = 0;
						$this->showThisWeekExperts_arr['row'] = array();
						foreach($top_analysts as $eachMember)
							{
								$this->showThisWeekExperts_arr['row'][$inc]['userDetails'] = $userDetails = $this->getUserDetails($eachMember);
								if (!$userDetails)
									continue;
								$this->showThisWeekExperts_arr['have_analysts'] = $have_analysts = true;

								$this->showThisWeekExperts_arr['row'][$inc]['uid'] = $uid = $userDetails['user_id'];
								$this->showThisWeekExperts_arr['row'][$inc]['userLog'] = $userLog = $this->getUserLog($uid);
								$this->showThisWeekExperts_arr['row'][$inc]['uname'] = $uname = $userDetails['name'];
								$this->showThisWeekExperts_arr['row'][$inc]['userLevelNumber'] = '';
								$this->showThisWeekExperts_arr['row'][$inc]['getResolvedQuestions'] = $this->getResolvedQuestions($uid);
								if($this->CFG['admin']['user_levels']['allowed'])
									$this->showThisWeekExperts_arr['row'][$inc]['userLevelNumber'] = getUserLevelNumber($userLog['total_points']);
								$i++;
								$this->showThisWeekExperts_arr['row'][$inc]['clsOddOrEvenQuestion'] = $clsOddOrEvenQuestion = 'clsEvenQuestion';
								if ($i%2)
									$this->showThisWeekExperts_arr['row'][$inc]['clsOddOrEvenQuestion'] = $clsOddOrEvenQuestion = 'clsOddQuestion';
								$this->showThisWeekExperts_arr['row'][$inc]['userDetails']['image_id'] = $userDetails['image_id'] = 'stwe';
								$this->showThisWeekExperts_arr['row'][$inc]['myanswers_url'] = 	getMemberUrl($userDetails['user_id'], $uname);
								$this->showThisWeekExperts_arr['row'][$inc]['stripString_display_name'] = stripString($userDetails['display_name'], $this->CFG['username']['short_length']);
								$this->showThisWeekExperts_arr['row'][$inc]['questions_ques_url'] = getAnovaUrl('questions', '?view=search&amp;uname='.$uname.'&amp;opt=ques', 'search/?uname='.$uname.'&amp;opt=ques');
								$this->showThisWeekExperts_arr['row'][$inc]['questions_resolve_url'] = getAnovaUrl('questions', '?view=search&amp;uname='.$uname.'&amp;opt=resolve', 'search/?uname='.$uname.'&amp;opt=resolve');
								$this->showThisWeekExperts_arr['row'][$inc]['questions_ans_url'] = getAnovaUrl('questions', '?view=search&amp;uname='.$uname.'&amp;opt=ans', 'search/?uname='.$uname.'&amp;opt=ans');
								$inc++;
							} //foreach
						if ($have_analysts)
							{
								$this->showThisWeekExperts_arr['topanalysts_url'] = getAnovaUrl('topanalysts', '?week=1', '?week=1');
							}
					}
				return 	$this->showThisWeekExperts_arr;
			}

		/**
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function thisWeekExperts($limit=5)
			{
				$sql = 'SELECT user_id FROM '.$this->CFG['db']['tbl']['view_weekly_experts'].
						' as we WHERE week_points>0'.
						' AND EXISTS(SELECT 1 FROM '.$this->CFG['db']['tbl']['users'].
						' WHERE user_id=we.user_id AND usr_status=\'Ok\')'.
						' ORDER BY week_points DESC LIMIT 0, '.$limit;
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
					trigger_db_error($this->dbObj);

				if ($rs->PO_RecordCount())
					{
						$user = array();
						$i = 0;
						while($row = $rs->FetchRow()){
							$user[$i] = $row['user_id'];
							$i++;
						}
						return $user;
					}
				return false;
			}
	  //debates fuction start from here
	 	/**
		 *
		 * @access public
		 * @return void
		 **/
		public function getResolvedDebates($uid)
		    {
				$sql = 'SELECT COUNT(debate_id) AS resolved FROM '.$this->CFG['db']['tbl']['debates'].
						' WHERE user_id='.$this->dbObj->Param($uid).' AND status NOT IN(\'Blocked\',\'ToActivate\') AND date_closed<NOW()';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($uid));
				if (!$rs)
				    trigger_db_error($this->dbObj);
				$row = $rs->FetchRow();
				return $row['resolved'];
		    }

	 	/**
		 * To get recent debates
		 *
		 * @return void
		 * @access public
		 **/
		public function getRecentDebates($limit = 5, $debate_user_id='')
			{
				$condition = '';
				if($debate_user_id)
					$condition = ' AND user_id='.$debate_user_id;

				$sql = 'SELECT d.debate_id FROM '.$this->CFG['db']['tbl']['debates'].' AS d'.
						' WHERE d.status IN (\'Open\', \'Resolved\')'.$condition.
						' ORDER BY debate_id DESC LIMIT 0,'.$limit;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
					trigger_db_error($this->dbObj);

				$recent_debates = array();
				while($row = $rs->FetchRow())
					{
						$recent_debates[] = $row['debate_id'];
					}
				return $recent_debates;
			}

		/**
		 * To get recent debates response
		 *
		 * @return void
		 * @access public
		 **/
		public function getRecentMyResponse($limit = 5, $debate_user_id='')
			{
				$condition = '';
				if($debate_user_id)
					$condition = ' AND main_response_id=0 AND user_id='.$debate_user_id;

				$sql = 'SELECT d.debate_id FROM '.$this->CFG['db']['tbl']['debate_responses'].' AS d'.
						' WHERE d.status IN (\'Active\')'.$condition.
						' ORDER BY response_id DESC LIMIT 0,'.$limit;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
					trigger_db_error($this->dbObj);

				$recent_debates = array();
				while($row = $rs->FetchRow())
					{
						$recent_debates[] = $row['debate_id'];
					}
				return $recent_debates;
			}

		/**
		 * To get recent favorite debates response
		 *
		 * @return void
		 * @access public
		 **/
		public function getRecentMyfavorite($limit = 5, $debate_user_id='')
			{
				$condition = '';
				if($debate_user_id)
					$condition = ' WHERE content_type = \'Debate\' AND user_id='.$debate_user_id;

				$content_id  = '(SELECT b.content_id AS debate_id  FROM '.$this->CFG['db']['tbl']['user_bookmarked'].' AS b'.$condition.')';
				$sql = 'SELECT d.debate_id FROM '.$this->CFG['db']['tbl']['debates'].' AS d WHERE d.debate_id IN '.$content_id.' AND d.status IN (\'Open\', \'Resolved\')
						AND NOT EXISTS ( SELECT 1 FROM user_ignored AS ui WHERE ui.user_id =d.user_id AND ui.ignored_id = d.user_id )
						AND (SELECT usr_status FROM users WHERE user_id=d.user_id)=\'Ok\'
						LIMIT 0,'.$limit;



				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
					trigger_db_error($this->dbObj);

				$recent_debates = array();
				while($row = $rs->FetchRow())
					{
						$recent_debates[] = $row['debate_id'];
					}
				return $recent_debates;
			}

		/**
		 * To display recent debates
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function displayRecentDebates($limit = 5, $debate_user_id='', $debate_type='')
			{
				$this->displayRecentDebate_arr	=	array();
				$this->displayRecentDebate_arr['recent_debates']	=	$popular_debates = $this->getRecentDebates($limit);
				$this->displayRecentDebate_arr['have_debates']	=	$have_debates = false;
				$is_user = false;
				if($debate_user_id)
					{
						$is_user = true;
						$this->LANG['common_recent_debate_title'] = $this->LANG['mydebate_debate_header'];
					}
				if($debate_type == 'MyFavorites')
					{
						$recent_debates = $this->getRecentMyfavorite($limit, $debate_user_id);
						$this->displayRecentDebate_arr['show_title']	=	$show_title = $this->LANG['common_recent_favorite_debate_title'];
					}
				else if($debate_type == 'MyDebates')
					{
						$recent_debates = $this->getRecentDebates($limit, $debate_user_id);
						$this->displayRecentDebate_arr['show_title']	=	$show_title = $this->LANG['common_recent_debate_title'];
					}
				else if($debate_type == 'MyResponse')
					{
						$recent_debates = $this->getRecentMyResponse($limit, $debate_user_id);
						$this->displayRecentDebate_arr['show_title']	=	$show_title = $this->LANG['common_recent_response_debate_title'];
					}
				else
					{
						$recent_debates = $this->getRecentDebates($limit, $debate_user_id);
					}
				$this->displayRecentDebate_arr['recordCount']	=	0;
				if($recent_debates)
					{
						$this->displayRecentDebate_arr['recordCount']	=	1;
						$this->displayRecentDebate_arr['row']	=	array();
						$i = 0;
						$inc = 0;
						foreach($recent_debates as $eachDebate)
							{
								$this->displayRecentDebate_arr['row'][$inc]['debateDetails']	=	$debateDetails = $this->getDebateDetails($eachDebate);
								if (!$debateDetails)
									continue;
								$this->displayRecentDebate_arr['have_debates']	=	$have_debates = true;
								$i++;
								$this->displayRecentDebate_arr['row'][$inc]['clsOddOrEvenQuestion']	=	$clsOddOrEvenQuestion = 'clsEvenQuestion';
								if ($i%2)
									$this->displayRecentDebate_arr['row'][$inc]['clsOddOrEvenQuestion']	=	$clsOddOrEvenQuestion = 'clsOddQuestion';
								$this->displayRecentDebate_arr['row'][$inc]['answer_plural']	=	$answer_plural = $this->LANG['common_total_response'];
								if($debateDetails['total_response']!= 1)
									$this->displayRecentDebate_arr['row'][$inc]['answer_plural']	=	$answer_plural = $this->LANG['common_total_responses'];
								$this->displayRecentDebate_arr['row'][$inc]['debateDetails']['image_id']	=	$debate_type.'drd';
								$inc++;
							}
						if($have_debates)
							{
								if($is_user)
									{
										$this->displayRecentDebate_arr['userDetails'] = $this->getUserDetails($debate_user_id);
										if($debate_type == 'MyDebates')
											$this->displayRecentDebate_arr['MyDebates_url'] = getDebatesUrl('debates', '?view=search&uname='.$this->displayRecentDebate_arr['userDetails']['name'].'&opt=debt', '?view=search&uname='.$this->displayRecentDebate_arr['userDetails']['name'].'&opt=debt');
										else if($debate_type == 'MyResponse')
											$this->displayRecentDebate_arr['MyDebates_url'] = getDebatesUrl('debates', '?view=search&uname='.$this->displayRecentDebate_arr['userDetails']['name'].'&opt=resp', '?view=search&uname='.$this->displayRecentDebate_arr['userDetails']['name'].'&opt=resp');
										else if($debate_type == 'MyFavorites')
											$this->displayRecentDebate_arr['MyDebates_url'] = getDebatesUrl('debates', '?view=search&uname='.$this->displayRecentDebate_arr['userDetails']['name'].'&opt=fav', '?view=search&uname='.$this->displayRecentDebate_arr['userDetails']['name'].'&opt=fav');
									}
								else
									{
										$this->displayRecentDebate_arr['MyDebates_url'] = getDebatesUrl('debates', '?view=recent&sort=date&order=d&al=All&', 'recent/?sort=date&order=d&al=All&');
									}
							}
					}
				return 	$this->displayRecentDebate_arr;
			}

		/**
		 * To get popular debates
		 *
		 * @return void
		 * @access public
		 **/
		public function getPopularDebates($limit = 5)
			{
				$sql = 'SELECT d.debate_id FROM '.$this->CFG['db']['tbl']['debates'].' AS d'.
						' WHERE d.status IN (\'Open\', \'Resolved\')'.
						' AND total_views > 0 AND latest_viewed >= DATE_SUB(NOW(), INTERVAL '.$this->CFG['admin']['popular_days']['limit'].' DAY)'.
						' ORDER BY total_views DESC LIMIT 0,'.$limit;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
					trigger_db_error($this->dbObj);

				$popular_debates = array();
				while($row = $rs->FetchRow())
					{
						$popular_debates[] = $row['debate_id'];
					}
				return $popular_debates;
			}

		/**
		 * To display popular debates
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function displayPopularDebates($limit = 5)
			{
				$this->displayPopularDebates_arr	=	array();
				$this->displayPopularDebates_arr['popular_debates']	=	$popular_debates = $this->getPopularDebates($limit);
				$this->displayPopularDebates_arr['have_debates']	=	$have_debates = false;
				if ($popular_debates)
					{
						$inc	=	0;
						$this->displayPopularDebates_arr['row']	=	array();
						$i = 0;
						foreach($popular_debates as $eachDebate)
							{
								$this->displayPopularDebates_arr['row'][$inc]['debateDetails']	=	$debateDetails = $this->getDebateDetails($eachDebate);
								if (!$debateDetails)
									continue;
								$this->displayPopularDebates_arr['have_debates']	=	$have_debates = true;
								$i++;
								$this->displayPopularDebates_arr['row'][$inc]['clsOddOrEvenQuestion']	=	$clsOddOrEvenQuestion = 'clsEvenQuestion';
								if ($i%2)
									$this->displayPopularDebates_arr['row'][$inc]['clsOddOrEvenQuestion']	=	$clsOddOrEvenQuestion = 'clsOddQuestion';
								$this->displayPopularDebates_arr['row'][$inc]['answer_plural']	=	$answer_plural = $this->LANG['common_total_response'];
								if($debateDetails['total_response']!= 1)
									$this->displayPopularDebates_arr['row'][$inc]['answer_plural']	=	$answer_plural = $this->LANG['common_total_responses'];
								$this->displayPopularDebates_arr['row'][$inc]['debateDetails']['image_id']	=	$debateDetails['image_id'] = 'daq';
								$inc++;
							} //foreach
						if ($have_debates)
							{
								$this->displayPopularDebates_arr['PopularDebates_url'] = getDebatesUrl('debates', '?view=popular&sort=date&order=d&al=All&', 'popular/?sort=date&order=d&al=All&');
							}
					}
				return 	$this->displayPopularDebates_arr;
			}

		/**
		 * To get video debates
		 *
		 * @return void
		 * @access public
		 **/
		public function getVideoDebates($limit = 5)
			{
				$sql = 'SELECT d.debate_id FROM '.$this->CFG['db']['tbl']['debates'].' AS d'.
						' WHERE d.status IN (\'Open\', \'Resolved\')'.
						' AND video_id != 0 ORDER BY debate_id DESC LIMIT 0,'.$limit;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
						trigger_db_error($this->dbObj);

				$video_debates = array();
				while($row = $rs->FetchRow())
					{
						$video_debates[] = $row['debate_id'];
					}
				return $video_debates;
			}

		/**
		 * To display video debates
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function displayVideoDebates($limit = 5)
			{
				$this->displayVideoDebates_arr	=	array();
				$this->displayVideoDebates_arr['video_debates']	=	$video_debates = $this->getVideoDebates($limit);
				$this->displayVideoDebates_arr['have_debates']	=	$have_debates = false;
				if ($video_debates)
					{
						$inc	=	0;
						$this->displayVideoDebates_arr['row']	=	array();
						$i = 0;
						foreach($video_debates as $eachDebate)
							{
								$this->displayVideoDebates_arr['row'][$inc]['debateDetails']	=	$debateDetails = $this->getDebateDetails($eachDebate);
								if (!$debateDetails)
									continue;
								$this->displayVideoDebates_arr['have_debates']	=	$have_debates = true;
								$i++;
								$this->displayVideoDebates_arr['row'][$inc]['clsOddOrEvenQuestion']	=	$clsOddOrEvenQuestion = 'clsEvenQuestion';
								if ($i%2)
									$this->displayVideoDebates_arr['row'][$inc]['clsOddOrEvenQuestion']	=	$clsOddOrEvenQuestion = 'clsOddQuestion';
								$this->displayVideoDebates_arr['row'][$inc]['answer_plural']	=	$answer_plural = $this->LANG['common_total_response'];
								if($debateDetails['total_response']!= 1)
									$this->displayVideoDebates_arr['row'][$inc]['answer_plural']	=	$answer_plural = $this->LANG['common_total_responses'];
								$this->displayVideoDebates_arr['row'][$inc]['debateDetails']['image_id']	=	$debateDetails['image_id'] = 'daq';
								$inc++;
							} //foreach
						if ($have_debates)
							{
								$this->displayVideoDebates_arr['VedioDebates_url'] = getDebatesUrl('debates','?view=search&video=1','search/?video=1');
							}
					}
				return 	$this->displayVideoDebates_arr;
			}

		/**
		 * To get audio debates
		 *
		 * @return void
		 * @access public
		 **/
		public function getAudioDebates($limit = 5)
			{
				$sql = 'SELECT d.debate_id FROM '.$this->CFG['db']['tbl']['debates'].' AS d'.
						' WHERE d.status IN (\'Open\', \'Resolved\')'.
						' AND audio_id != 0 ORDER BY debate_id DESC LIMIT 0,'.$limit;

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt);
				if (!$rs)
					trigger_db_error($this->dbObj);

				$audio_debates = array();
				while($row = $rs->FetchRow())
					{
						$audio_debates[] = $row['debate_id'];
					}
				return $audio_debates;
			}

		/**
		 * To display audio debates
		 *
		 * @return 		void
		 * @access 		public
		 */
		public function displayAudioDebates($limit = 5)
			{
				$this->displayAudioDebates_arr	=	array();
				$this->displayAudioDebates_arr['audio_debates']	=	$audio_debates = $this->getAudioDebates($limit);
				$this->displayAudioDebates_arr['have_debates']	=	$have_debates = false;
				if ($audio_debates)
					{
						$inc	=	0;
						$this->displayAudioDebates_arr['row']	=	array();
						$i = 0;
						foreach($audio_debates as $eachDebate)
							{
								$this->displayAudioDebates_arr['row'][$inc]['debateDetails']	=	$debateDetails = $this->getDebateDetails($eachDebate);
								if (!$debateDetails)
									continue;
								$this->displayAudioDebates_arr['have_debates']	=	$have_debates = true;
								$i++;
								$this->displayAudioDebates_arr['row'][$inc]['clsOddOrEvenQuestion']	=	$clsOddOrEvenQuestion = 'clsEvenQuestion';
								if ($i%2)
									$this->displayAudioDebates_arr['row'][$inc]['clsOddOrEvenQuestion']	=	$clsOddOrEvenQuestion = 'clsOddQuestion';
								$this->displayAudioDebates_arr['row'][$inc]['answer_plural']	=	$answer_plural = $this->LANG['common_total_response'];
								if($debateDetails['total_response']!= 1)
									$this->displayAudioDebates_arr['row'][$inc]['answer_plural']	=	$answer_plural = $this->LANG['common_total_responses'];
								$this->displayAudioDebates_arr['row'][$inc]['debateDetails']['image_id']	=	$debateDetails['image_id'] = 'daq';
								$inc++;
							} //foreach
						if ($have_debates)
							{
								$this->displayAudioDebates_arr['AudioDebates_url'] = getDebatesUrl('debates','?view=search&audio=1','search/?audio=1');
							}
					}
				return 	$this->displayAudioDebates_arr;
			}

		//------------------------ debates end here --------------------->>>>>>>>>>>>>>>

}
?>