<?php
include "settings.php";

if(isset($_GET["id"])) {
    $user = new User;
    $CurrentUser = $user->getUser($_GET["id"]);

    if($CurrentUser == null) {
        $_SESSION["usererror"] = "Det finns ingen användare på denna adress";
        header("Location: /");
        exit;
    }

    $userQuestions = $user->getUserQuestions($_GET["id"]);
    $userPostCount = $user->getUserPosts($_GET["id"]);
    $userAnswerCount = $user->getUserAnswers($_GET["id"]);
    $userBestAnswerCount = $user->getUserBestAnswers($_GET["id"]);
    $userMonthPos = $user->getPosition($CurrentUser->uMonthpoints);
    $userPos = $user->getTotalPosition($CurrentUser->uPoints);

    if($userAnswerCount > 0 && $userBestAnswerCount > 0) {
        $prc = round(($userBestAnswerCount/$userAnswerCount), 2)*100;
        $BestP = (100-$prc).",".$prc;
    }
    else {
        $prc = 0;
        $BestP = "100,0";
    }

    if($CurrentUser->uPoints > 0) {
        $rom = round(($CurrentUser->uPoints/1500), 2)*100;
    }
    else {
        $rom = 0;
    }
}
else {
    header("Location: /");
    exit;
}
$title = "Visar ".$CurrentUser->uNickname;
include "header.php";
?>
		<!-- start of content -->
			<div id="content">
				<div id="content_inside">
					<div id="content_left">
						<h2>Profil för <?= $CurrentUser->uNickname; ?></h2>
						
						<div class="user_info_box">
							<span class="user_info_text">

								<img src="<?=$CurrentUser->uAvatar;?>" class="user_info_avatar"/>
								Namn: <strong><?=$CurrentUser->uName;?></strong><br />
								Medlem sedan: <strong><?=$CurrentUser->uSince;?></strong><br />
								<?php
									$tidsen = get_time_difference($CurrentUser->uLogged, date("Y-m-d H:i:s"));
								?>
								Senast sedd: <strong><?=$tidsen;?></strong><br />
								<?php if(strlen($CurrentUser->uPlace)>1): ?>
								Ort: <strong><?=$CurrentUser->uPlace;?></strong><br />
								<?php endif; ?>
								
								<?php if(strlen($CurrentUser->uHomepage)>1): ?>
								Hemsida: <strong><?=do_link($CurrentUser->uHomepage);?></strong>
								<?php endif; ?>
							</span>
							
							<?php
							if(isset($_SESSION['uID']) && $CurrentUser->uID == $_SESSION["uID"]) {
								echo "<br />";
								echo "<span class='user_info_text'>";
								echo "Epost: <strong>" . $CurrentUser->uEmail . "</strong>";
								echo "</span>";
								echo "<br /><br />";
								echo "<span class='button_left'><a href='user_settings.php' class='button_link'>Ändra inställningar</a></span>";	
							}
						    ?>
						<div style="clear: both"></div>
						</div>
						
						<?php if(strlen($CurrentUser->uInfo) > 1): ?>
						<div class="user_info_box">
							<span class="user_info_text">
								<?=nl2br(htmlspecialchars(stripslashes($CurrentUser->uInfo)));?>
							</span>
						<div style="clear: both"></div>
						</div>
						<?php endif; ?>
						
						
						<br />
						<h2>Användarstatistik</h2>
						<div class="user_info_box">
							
							<div style="float: right;">
								<img src="http://chart.apis.google.com/chart?chs=220x100&chd=t:<?=$BestP?>&chco=2782c1&chf=bg,s,efefef&cht=p3&chl=Svar|Bästa" />
							</div>
							
							<div class="user_info_text" style="width: 180px; float: left;">
								Antal frågor: <strong><?=$userPostCount;?> st</strong><br />
								Antal svar: <strong><?=$userAnswerCount;?> st</strong><br />
								Antal bästa svar: <strong><?=$userBestAnswerCount;?> st</strong><br />
								Bästa svaret: <strong><?php echo $prc ?>%</strong><br />
								Röster: 
								<strong>
								<?php
									$arr = $user->getUserVotes($CurrentUser->uID);
									echo $arr[0] . " upp";
									echo ", ";
									echo $arr[1] . " ner";
								?>
								</strong>
							</div>
							
							<div class="user_info_text">
								Rank: <strong><?=$user->getUserLevel($CurrentUser->uPoints);?></strong> <br />
								Poäng: <strong><?=$CurrentUser->uPoints;?></strong><br />
								Poäng denna månad: <strong><?=$CurrentUser->uMonthpoints;?></strong><br />
								Placering på Answer: <strong><?=$userPos;?></strong><br />
								Tävlingsplacering: <strong><?=$userMonthPos;?></strong><br />
							</div>
							
							<div style="clear: both"></div>
						</div>

						<br />
						<div style="float: right; margin-top: 6px;">
							<div class="page<?php if(!isset($_GET["svar"])){ ?>_active<?php } ?>"><a href="/anvandare/<?=$CurrentUser->uID;?>.html">Frågor</a></div>
							<div class="page<?php if(isset($_GET["svar"])){ ?>_active<?php } ?>"><a href="/anvandare/<?=$CurrentUser->uID;?>,svar.html">Svar</a></div>
						</div>
						
						<?php
                        if(isset($_GET["svar"])) {
                            echo "<h1 class='head'>Senaste svaren av " . $CurrentUser->uNickname . "</h1>";
                            include 'answer_list.php';

                        }
                        else {
                            echo "<h1 class='head'>Senaste frågorna av " . $CurrentUser->uNickname . "</h1>";
                            if($userQuestions) {
                                include "question_list.php";
                            }
                            else{
                                echo "<em>" . $CurrentUser->uNickname . " har inte ställt några frågor</em>";
                            }
                        }
						?>
						
					</div>
					<div id="content_right">
						<?php include "sidebar.php"; ?>
						
						<div style="clear: both;"></div>
					</div>
					
					
				</div>
			</div>
		<!-- end of content -->
<?php include "footer.php"; ?>