<?php

$sqlusercurrentinfocall = "SELECT id, survey_score, total_survey_number, paid_account FROM users WHERE username = '$username'";
$usercurrentinfocallquery = mysqli_query($mysqli,$sqlusercurrentinfocall);
$usercurrentinfocallgrab  = mysqli_fetch_assoc($usercurrentinfocallquery);
$LastSurveyScore          = $usercurrentinfocallgrab['survey_score'];
$TotalSurveyNumber        = $usercurrentinfocallgrab['total_survey_number'];
$PaidAccount              = $usercurrentinfocallgrab['paid_account'];
$id                       = $usercurrentinfocallgrab['id'];

if($PaidAccount == "True")
{

  $sqlscores = "SELECT * FROM scores WHERE user_id = '$id'";
  $scores = mysqli_query($mysqli,$sqlscores);

  $usertotalrow = $scores->num_rows;
  $userscore  = array();
  $totalscore = 0;

  while ($row  = mysqli_fetch_assoc($scores)) {
    $userscore[] = $row['score'];
  }

  function median($array=array()) {
    $iCount = count($array);
    if ($iCount == 0) {
      $median = 0;
    }
    $middle_index = floor($iCount / 2);
    sort($array, SORT_NUMERIC);
    $median = $array[$middle_index]; // assume an odd # of items
    // Handle the even case by averaging the middle 2 items
    if ($iCount % 2 == 0) {
      $median = ($median + $array[$middle_index - 1]) / 2;
    }
    return $median;
  }

  if (!empty($userscore)) {
    $usermin  = min($userscore);
    $usermax  = max($userscore);
    $mean     = array_sum($userscore)/count($userscore);
    $mean     = floatValueScore($mean);
    $median   = median($userscore);
    $median   = floatValueScore($median);
  }else{
    $usermin  = 0;
    $usermax  = 0;
    $mean     = 0;
    $median   = 0;
  }
 
}

?>

<div class="st-index-right">
  <div class="my-stress-tracker">
    <h2>MY STRESS TRACKER</h2>
    <div class="st-chart">

    </div>
    <div class="my-stress-tracker-count">
      <!-- <ul>
        <li><a href="#">WEEK</a></li>
        <li><span>|</span></li>
        <li><a href="#">MONTH</a></li>
        <li><span>|</span></li>
        <li><a href="#">YEAR</a></li>
      </ul> -->
    </div>
    <div class="my-st-value">
      <ul>
        <?php
       
          echo '<li><a href="#">Median: <span>'.$median.'</span> </a></li>';
          echo '<li><a href="#">Min:<span>'.$usermin.'</span> </a></li>';
          echo '<li><a href="#">Mean:<span>'.$mean.'</span> </a></li>';
          echo '<li><a href="#">Max: <span>'.$usermax.'</span> </a></li>';
          ?>
        </ul>
      </div>
    </div>
  </div>