<?php
/**
 * To manage embed contents
 *
 */
//-------------- Class UserDetailHandler begins --------------->>>>>//
/**
 * UserDetailHandler
 *
 * @package
 * @author Senthil
 * @copyright Copyright (c) 2010
 * @version $Id$
 * @access public
 */
class UserDetailHandler extends AnovaHandler
	{

		/**
		 * to display embed content
		 *
		 * @return 		string
		 * @access 		public
		 */
		public function displayUserDetails()
			{
				$uid = $this->fields_arr['uid'];
				if ($userDetails = $this->chkIsValidUser($uid))
					{
						$displayUserDetails_arr = array();
						$uid 		= $userDetails['user_id'];
						$uname 		= $userDetails['name'];
						$display_uname 		= $userDetails['display_name'];
						$userLog	= $this->getUserLog($uid);
						$displayUserDetails_arr['userLevelNumber'] = '';
						if($this->CFG['admin']['user_levels']['allowed'])
							$displayUserDetails_arr['userLevelNumber'] = getUserLevelNumber($userLog['total_points']);
						$displayUserDetails_arr['image_for_openid'] = '';
						if ($this->CFG['admin']['module']['facebook'] || $this->CFG['admin']['module']['openid'])
							$displayUserDetails_arr['image_for_openid'] = $this->getImagesForOpenid($uid);
						$displayUserDetails_arr['user_myanswers_url'] = getMemberUrl($userDetails['user_id'], $userDetails['name']);
						$displayUserDetails_arr['user_display_uname'] = stripString($display_uname, $this->CFG['username']['medium_length']);
						$displayUserDetails_arr['uname']  = $uname;
						$displayUserDetails_arr['user_compose_url'] = getUrl('mailcompose', '?mcomp='.$uname, '?mcomp='.$uname, 'members');
						$displayUserDetails_arr['user_questions_ans_url'] = getAnovaUrl('questions', '?view=search&amp;uname='.$uname.'&amp;opt=ans', 'search/?uname='.$uname.'&amp;opt=ans');
						$displayUserDetails_arr['userLog_total_points'] = $userLog['total_points'];
						$displayUserDetails_arr['userLog_total_ans'] = $userLog['total_ans'];

						if($this->CFG['admin']['best_answers']['allowed'])
							{
								$displayUserDetails_arr['bestAnspercentage'] = 0;
								if($userLog['total_ans'])
									$displayUserDetails_arr['bestAnspercentage'] = round(($userLog['total_best_ans']/$userLog['total_ans'])*100);
							}

						$displayUserDetails_arr['user_questions_bestans_url'] = getAnovaUrl('questions', '?view=search&amp;uname='.$uname.'&amp;opt=bestans', 'search/?uname='.$uname.'&amp;opt=bestans');
						$displayUserDetails_arr['user_questions_ques_url'] = getAnovaUrl('questions', '?view=search&amp;uname='.$uname.'&amp;opt=ques', 'search/?uname='.$uname.'&amp;opt=ques');
						$displayUserDetails_arr['userLog_total_ques'] = $userLog['total_ques'];
						$displayUserDetails_arr['user_questions_resolved_url'] = getAnovaUrl('questions', '?view=search&amp;uname='.$uname.'&amp;opt=resolve', 'search/?uname='.$uname.'&amp;opt=resolve');
						$displayUserDetails_arr['resolved_questions'] = $this->getResolvedQuestions($uid);

						return $displayUserDetails_arr;
					}
			}

		/**
		 *
		 * @access public
		 * @return void
		 **/
		public function chkIsValidUser($uid)
		    {
				if (!$uid)
					return false;

				$sql = 'SELECT '.$this->getUserTableFields(array('user_id', 'name', 'openid_type', 'display_name', 'image_path', 't_height', 't_width', 'photo_server_url', 'photo_ext')).
						', u.'.$this->getUserTableField('user_id').' AS img_user_id'.
						' FROM '.$this->CFG['db']['tbl']['users'].' AS u'.
						' WHERE u.'.$this->getUserTableField('user_id').'='.$this->dbObj->Param($uid).
						' AND u.'.$this->getUserTableField('usr_status').'=\'Ok\'';
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($uid));
				if (!$rs)
				        trigger_db_error($this->dbObj);
				if (!$rs->PO_RecordCount())
					{
						return false;
					}
				$row = $rs->FetchRow();
				return $row;
		    }

		/**
		 * UserDetailHandler::getImagesForOpenid()
		 *
		 * @param mixed $uid
		 * @return
		 */
		public function getImagesForOpenid($uid)
			{
				$type= '';
				$sql = 'SELECT openid_type FROM  '.$this->CFG['db']['tbl']['users'].
				       ' WHERE openid_used IN( \'Yes\',\'FB\',\'NG\')' .
					   ' AND user_id = '.$uid.'';
				$rs = $this->dbObj->Execute($this->dbObj->Prepare($sql));
				if (!$rs)
					trigger_error($this->dbObj->ErrorNo().' '.$this->dbObj->ErrorMsg(), E_USER_ERROR);
				if($row = $rs->FetchRow())
					{
						if(isset($row['openid_type']))
							{
							 	$type = $this->CFG['site']['url'].'files/openid/'.$row['openid_type'].'.ico';
								$path=  $this->CFG['site']['project_path'].'files/openid/'.$row['openid_type'].'.ico';
								if(!file_exists($path))
									$type = $this->CFG['site']['url'].'files/openid/openid.ico';
							}
						else
							{
								$type = $this->CFG['site']['url'].'files/openid/openid.ico';
							}
					}
				if($type)
					{
						 return "<span class=\"clsOpenidImage\"><img src=\"".$type."\" alt=\"".$this->LANG['facebook_image_alt']."\"/></span>";
					}
		    }
	}
//----------------------------- Code begins ------------------>>>>>//
$userdetails = new UserDetailHandler();
$userdetails->setFormField('uid', '');

$userdetails->sanitizeFormInputs($_REQUEST);
if ($userdetails->isFormGETed($_REQUEST, 'uid'))
	{
		$userdetails->userdetails_arr = $userdetails->displayUserDetails();
	}
//<<<<<------------------------- Code ends ----------------------------------//
$userdetails->includeAjaxHeader();
//include the content of the page
setTemplateFolder('general/', $CFG['admin']['index']['anova_module']);
//<<<<<<--------------------Page block templates Ends--------------------//
//include the footer of the page
$smartyObj->display('userDetails.tpl');
$userdetails->includeAjaxFooter();
?>