Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/standings.php
<?php
/**
 * Standings display page
 *
 * Displays {@link OFFL_League league} standings.  Includes Standard (Playoff), Breakdown, and Power rankings.
 *
 * @author Stephen Rochelle <hide@address.com>
 * @version OFFL v0.2
 * @copyright Copyright (c) 2004 Stephen Rochelle.  Some rights reserved.
 * @package offl-ui
 */

require_once("offlconfig.php");
require_once($DOC_ROOT . "/lib/functions.php");

if(empty($_GET["year"]))
{	$_GET["year"] = getThisYear();	}
$pageTitle = $_GET["year"] . " Standings";
$adminEditOnly = 1 ;
require_once($DOC_ROOT . "/lib/header.php");

if (empty($_GET["view"]))
{	$_GET["view"] = NULL;	}

/**
 * Used to sort for breakdown standings
 *
 * @param array $data
 * @return array
 */
function breakdown_pct_sort($data)
{
	// function to sort by breakdown %.
	$retArr = array();
	foreach ($data as $team_id=>$team_data)
	{
		$pct = $team_data["pct"];
		if (array_key_exists($pct, $retArr))
			$retArr[$pct] .= "," . $team_id;
		else
			$retArr[$pct] = $team_id;
	}
	krsort($retArr);
	return $retArr;
}

/**
 * Used to sort for power rankings via {@link usort()}
 */
function power_sort($t1, $t2)
{
	if ($t1["rank"] < $t2["rank"])
		return -1;
	return $t1["rank"] > $t2["rank"] ? 1 : 0;
}

// BEGIN INITIAL DATA COMPUTATION

// COMPUTATIONS FOR BREAKDOWN RANKINGS
$game_lookup = new OFFL_Game();
$game_lookup->setYear($_GET["year"]);
$game_lookup->setLeagueID($_SESSION["league_id"]);
$week = getCurrentWeek($_GET["year"]);
$teams = $myleague->getAllFFLTeams(FALSE, $_GET["year"]);
$divisions = $myleague->getAllDivisions(FALSE, $_GET["year"]);
$team_array = array();
foreach ($divisions as $division)
{
	$team_array[] = $division->getAllDivisionTeams($division->getDivisionID(), $_GET["year"], $_SESSION["league_id"]);
}
$maxtm = 0;
foreach ($team_array as $divisionlist)
{
	if (sizeof($divisionlist) > $maxtm)
		$maxtm = sizeof($divisionlist);
}

$team_record_array = array(); // records by opponent
$team_record_overall = array(); // records overall

foreach ($teams as $team) // init the record arrays
{
	$team_record_overall[$team->getFFLTeamID()]["W"] = 0;
	$team_record_overall[$team->getFFLTeamID()]["L"] = 0;
	$team_record_overall[$team->getFFLTeamID()]["T"] = 0;
	foreach ($teams as $opp)
	{
		if ($opp->getFFLTeamID() == $team->getFFLTeamID())
			continue;
		$team_record_array[$team->getFFLTeamID()][$opp->getFFLTeamID()]["W"] = 0;
		$team_record_array[$team->getFFLTeamID()][$opp->getFFLTeamID()]["L"] = 0;
		$team_record_array[$team->getFFLTeamID()][$opp->getFFLTeamID()]["T"] = 0;
	}
}

$season_init = sizeof($game_lookup->getGameWeeks());

//if ($season_init)
//{
/**********************

CHECK SEASON INITIALIZATION

**********************/

foreach ($teams as $team) // cycle teams
{
	$game_lookup->SQLQuery("SELECT MAX(week) AS \"maxweek\" FROM `games` WHERE final=1 AND year=" . $_GET["year"] . " AND league_id=" . $_SESSION["league_id"]);
	$bkdn_wk = $game_lookup->SQLResult(0, "maxweek");
	for($wk = 1; $wk <= $bkdn_wk; $wk++) // cycle weeks
	{
		$score = $game_lookup->getGameScore($_GET["year"], $wk, $team->getFFLTeamID());
		
		if (isset($score))
		{
			foreach ($teams as $opp)
			{
				if ($team->getFFLTeamID() == $opp->getFFLTeamID())
					continue; // don't check vs. self
				$oppscore = $game_lookup->getGameScore($_GET["year"], $wk, $opp->getFFLTeamID());
				
				if (isset($oppscore))
				{
					if ($score > $oppscore)
					{
						$team_record_array[$team->getFFLTeamID()][$opp->getFFLTeamID()]["W"]++;
						$team_record_overall[$team->getFFLTeamID()]["W"]++;
					}
					elseif ($score < $oppscore)
					{
						$team_record_array[$team->getFFLTeamID()][$opp->getFFLTeamID()]["L"]++;
						$team_record_overall[$team->getFFLTeamID()]["L"]++;
					}
					else
					{
						$team_record_array[$team->getFFLTeamID()][$opp->getFFLTeamID()]["T"]++;
						$team_record_overall[$team->getFFLTeamID()]["T"]++;
					}
				}
			} // end foreach
		}
	}
	// now compute the win %
	foreach ($teams as $opp)
	{
		if ($team->getFFLTeamID() == $opp->getFFLTeamID())
			continue;
		$totgames = $team_record_array[$team->getFFLTeamID()][$opp->getFFLTeamID()]["W"] + $team_record_array[$team->getFFLTeamID()][$opp->getFFLTeamID()]["L"] + $team_record_array[$team->getFFLTeamID()][$opp->getFFLTeamID()]["T"];
		if ($totgames)
			$pct = floor(1000 * ($team_record_array[$team->getFFLTeamID()][$opp->getFFLTeamID()]["W"] + $team_record_array[$team->getFFLTeamID()][$opp->getFFLTeamID()]["T"] / 2) / ($totgames) ) / 1000;
		else
			$pct = 0;
		$pctstr = strval($pct);
		if (strlen($pctstr) == 1)
			$pctstr .= ".000";
		else
			for ($d = strlen($pctstr); $d < 5; $d++)
				$pctstr .= "0";
		$team_record_array[$team->getFFLTeamID()][$opp->getFFLTeamID()]["pct"] = $pctstr;
	}
	$totgames = $team_record_overall[$team->getFFLTeamID()]["W"] + $team_record_overall[$team->getFFLTeamID()]["L"] + $team_record_overall[$team->getFFLTeamID()]["T"];
	if ($totgames > 0)
		$pct = floor(1000 * ($team_record_overall[$team->getFFLTeamID()]["W"] + $team_record_overall[$team->getFFLTeamID()]["T"] / 2) / ($totgames) ) / 1000;
	else
		$pct = 0;
	$pctstr = strval($pct);
	if (strlen($pctstr) == 1)
		$pctstr .= ".000";
	else
		for ($d = strlen($pctstr); $d < 5; $d++)
			$pctstr .= "0";
	$team_record_overall[$team->getFFLTeamID()]["pct"] = $pctstr;

}

// COMPUTATIONS FOR POWER RANKINGS
$team_bkdn_order = breakdown_pct_sort($team_record_overall);

$team_rec_order = array();
$team_pts_order = array();
$power_rank = array();
foreach ($teams as $team)
{
	$pct = $team->getOverallWinPct($_GET["year"]);
	if (array_key_exists($pct, $team_rec_order))
		$team_rec_order[$pct] .= "," . $team->getFFLTeamID();
	else
		$team_rec_order[$pct] = $team->getFFLTeamID();
	
	if ($team->getOverallGames($_GET["year"]))
	{	$pts = (string) ($team->getOverallPF($_GET["year"]) / $team->getOverallGames($_GET["year"]));	}
	else
	{	$pts = 0;	}
	if (array_key_exists($pts, $team_pts_order))
		$team_pts_order[$pts] .= "," . $team->getFFLTeamID();
	else
		$team_pts_order[$pts] = $team->getFFLTeamID();
}
krsort($team_rec_order);
krsort($team_pts_order);

$pt = sizeof($teams); // initial value for record ranking
foreach ($team_rec_order as $rec)
{
	$team_ids = explode(",", $rec);
	if (sizeof($team_ids) > 1)
		$curr_pt = (2 * $pt + 1 - sizeof($team_ids)) / 2;
	else
		$curr_pt = $pt;
	foreach ($team_ids as $team_id)
	{
		$power_rank[$team_id]["rec"] = $curr_pt;
	}
	$pt -= sizeof($team_ids);
}
$pt = sizeof($teams); // initial value for record ranking
foreach ($team_pts_order as $pts)
{
	$team_ids = explode(",", $pts);
	if (sizeof($team_ids) > 1)
		$curr_pt = (2 * $pt + 1 - sizeof($team_ids)) / 2;
	else
		$curr_pt = $pt;
	foreach ($team_ids as $team_id)
	{
		$power_rank[$team_id]["pts"] = $curr_pt;
	}
	$pt -= sizeof($team_ids);
}
$pt = sizeof($teams); // initial value for record ranking
foreach ($team_bkdn_order as $bkdn)
{
	$team_ids = explode(",", $bkdn);
	if (sizeof($team_ids) > 1)
		$curr_pt = (2 * $pt + 1 - sizeof($team_ids)) / 2;
	else
		$curr_pt = $pt;
	foreach ($team_ids as $team_id)
	{
		$power_rank[$team_id]["bkdn"] = $curr_pt;
	}
	$pt -= sizeof($team_ids);
}
foreach ($power_rank as $i=>$team_rank)
{
	$power_rank[$i]["power"] = $team_rank["rec"] + $team_rank["pts"] + $team_rank["bkdn"];
}
$team_power_order = array();
foreach ($teams as $team)
{
	$power = floor(1000 * $power_rank[$team->getFFLTeamID()]["power"]);
	settype($power, "integer");
	if (array_key_exists($power, $team_power_order))
		$team_power_order[$power] .= "," . $team->getFFLTeamID();
	else
		$team_power_order[$power] = $team->getFFLTeamID();
}
krsort($team_power_order);
$rank = 1;
foreach ($team_power_order as $power_rnk)
{
	$team_ids = explode (",", $power_rnk);
	foreach($team_ids as $team_id)
		$power_rank[$team_id]["rank"] = $rank;
	$rank += sizeof ($team_ids);
}
uasort($power_rank, "power_sort");

// END INITIAL DATA COMPUTATION

//}
/************************

END SEASON INITIALIZATION CHECK

************************/

	$x = new OFFL_Game();
	$x->setLeagueID($_SESSION["league_id"]);
	$years = $x->getGameYears();
	?>
	View Standings Season: <?php
	foreach($years as $i=>$year)
	{
		if($i>0)
			echo " | ";
		?><a href="<?php echo $WEB_ROOT; ?>/standings.php?year=<?php echo $year; if ($year == $_GET["year"]) echo "\"  class=\"current"; ?>"><?php echo $year; ?></a><?php
	}
	?><br />

<?php 
	switch ($_GET["view"])
	{
		case "standard":
		$teams = $myleague->getAllFFLTeams(FALSE, $_GET["year"]);
		$divisions = $myleague->getAllDivisions(FALSE, $_GET["year"]);

		?><table id="standings"><?php
		for($i=0; $i<sizeof($divisions); $i++)
		{
?>			<tr class="darkline">
				<th colspan="12"><?php echo $divisions[$i]->getDivisionDesc(); ?></th>
			</tr>
			<tr>
				<th width="50%"></th>
				<th colspan="4">Overall</th>
				<th></th>
				<th colspan="4">Division</th>
				<th colspan="2"></th>
			</tr>
			<tr class="medline">
				<th>Team</th>
				<th><abbr title="Overall Wins">W</abbr></th>
				<th><abbr title="Overall Losses">L</abbr></th>
				<th><abbr title="Overall Ties">T</abbr></th>
				<th><abbr title="Overall Win Percentage">%</abbr></th>
				<th></th>
				<th><abbr title="Division Wins">W</abbr></th>
				<th><abbr title="Division Losses">L</abbr></th>
				<th><abbr title="Division Ties">T</abbr></th>
				<th><abbr title="Division Win Percentage">%</abbr></th>
				<th><abbr title="Points Forced">PF</abbr></th>
				<th><abbr title="Points Allowed">PA</abbr></th>
			</tr>
	<?php
			// Sort and print
			$standings = $divisions[$i]->getAllDivisionTeams($divisions[$i]->getDivisionID(), $_GET["year"], $_SESSION["league_id"]);

			// Call my sort prog
			usort($standings, 'Compare');
			foreach($standings as $j=>$standing)
			{
				?>
				<tr valign="top" class="<?php if ($j % 2) echo "evenrow"; else echo "oddrow"; if ($standing->getFFLTeamID() == $_SESSION["fflteam_id"]) echo " active"; ?>">
					<td><a href="<?php echo $WEB_ROOT; ?>/teams.php?fflteam_id=<?php
						echo $standing->getFFLTeamID(); 
						if ($_GET["year"] < getThisYear())
						{	echo "&amp;league_id=" . $_SESSION["league_id"] . "&amp;year=" . $_GET["year"];	}
						?>"><?php
						echo $standing->getFFLTeamFullName();
						?></a></td>
					<td align="center"><?php echo $standing->getOverallWins($_GET["year"]); ?></td>
					<td align="center"><?php echo $standing->getOverallLosses($_GET["year"]); ?></td>
					<td align="center"><?php echo $standing->getOverallTies($_GET["year"]); ?></td>
					<td align="center"><?php echo $standing->getOverallWinPct($_GET["year"]); ?></td>
					<td align="center"></td>
					<td align="center"><?php echo $standing->getDivisionWins($_GET["year"]); ?></td>
					<td align="center"><?php echo $standing->getDivisionLosses($_GET["year"]); ?></td>
					<td align="center"><?php echo $standing->getDivisionTies($_GET["year"]); ?></td>
					<td align="center"><?php echo $standing->getDivisionWinPct($_GET["year"]); ?></td>
					<td align="center"><?php echo $standing->getOverallPF($_GET["year"]); ?></td>
					<td align="center"><?php echo $standing->getOverallPA($_GET["year"]); ?></td>
				</tr>
				<?php
			}
			?><tr><td colspan="9"><br></td></tr><?php
		}
		?></table><?php
		break;
	case "breakdown":

		echo "<table id=\"breakdown\">\n";
		for ($i = 0; $i < $maxtm; $i++)
		{
			echo "	<tr>\n";
			for ($j = 0; $j < sizeof($divisions); $j++)
			{
				if (sizeof($team_array[$j]) == 0)
				{	continue;	}
				if ($team_array[$j][$i]->getFFLTeamID() == $_SESSION["fflteam_id"])
				{	echo "		<td class=\"active\">\n";	}
				else
				{	echo "		<td>\n";	}
				echo "<h2><a name=\"" . $team_array[$j][$i]->getFFLTeamID() . "\" href=\"$WEB_ROOT/teams.php?fflteam_id=" . $team_array[$j][$i]->getFFLTeamID() . "\">" . $team_array[$j][$i]->getFFLTeamCity() . " " . $team_array[$j][$i]->getFFLTeamName() . "</a></h2>\n";
?>
	<table class="team-breakdown">
		<tr>
			<th>Team</th>
			<th><abbr title="Projected Wins">W</abbr></th>
			<th><abbr title="Projected Losses">L</abbr></th>
			<th><abbr title="Projected Ties">T</abbr></th>
			<th><abbr title="Projected Win Percentage">%</abbr></th>
		</tr>
<?php
				$k = 0;
				foreach ($teams as $team)
				{
					if ($team->getFFLTeamID() == $team_array[$j][$i]->getFFLTeamID())
						continue;
					echo "		<tr class=\"";
					if (++$k % 2)
						echo "oddrow";
					else
						echo "evenrow";
					
					if ($team->getFFLTeamID() == $_SESSION["fflteam_id"])
						echo " active";
					echo "\">\n";
					echo "			<td class=\"team\"><a href=\"$WEB_ROOT/teams.php?fflteam_id=" . $team->getFFLTeamID() . "\">" . $team->getFFLTeamCity() . " " . $team->getFFLTeamName() . "</a></td>\n";
					echo "			<td>" . $team_record_array[$team_array[$j][$i]->getFFLTeamID()][$team->getFFLTeamID()]["W"] . "</td>\n";
					echo "			<td>" . $team_record_array[$team_array[$j][$i]->getFFLTeamID()][$team->getFFLTeamID()]["L"] . "</td>\n";
					echo "			<td>" . $team_record_array[$team_array[$j][$i]->getFFLTeamID()][$team->getFFLTeamID()]["T"] . "</td>\n";
					echo "			<td>" . $team_record_array[$team_array[$j][$i]->getFFLTeamID()][$team->getFFLTeamID()]["pct"] . "</td>\n";
					echo "		</tr>\n";
				}
				echo "		<tr><td colspan=\"5\"><hr /></td></tr>\n";
				echo "		<tr class=\"total\"><td>Total:</td>";
				echo "<td>" . $team_record_overall[$team_array[$j][$i]->getFFLTeamID()]["W"] . "</td>";
				echo "<td>" . $team_record_overall[$team_array[$j][$i]->getFFLTeamID()]["L"] . "</td>";
				echo "<td>" . $team_record_overall[$team_array[$j][$i]->getFFLTeamID()]["T"] . "</td>";
				echo "<td>" . $team_record_overall[$team_array[$j][$i]->getFFLTeamID()]["pct"] . "</td>";

				echo "	</table>\n";
				echo "		</td>\n";
			}
			echo "	</tr>\n";
		}
		echo "</table>\n";

		break;
	case "power":
?>
<table id="power">
	<tr>
		<th>Rank</th>
		<th>Team</th>
		<th><abbr title="Record Ranking">Rec</abbr></th>
		<th><abbr title="Points Forced Ranking">Pts</abbr></th>
		<th><abbr title="Breakdown Ranking">Bkdn</abbr></th>
		<th><abbr title="Power Ranking (Sum Total)">Pwr</abbr></th>
	</tr>
<?php	
		$i = 0;
		foreach ($power_rank as $team_id=>$team_rank)
		{
			$team = new OFFL_FFLTeam($team_id);
			echo "		<tr class=\"";
			if (++$i % 2)
			{	echo "oddrow";	}
			else
			{	echo "evenrow";	}
			
			if ($team->getFFLTeamID() == $_SESSION["fflteam_id"])
			{	echo " active";	}
			echo "\">\n";
			echo "	<td>" . $team_rank["rank"] . "</td>\n";
			echo "	<td class=\"team\"><a href=\"$WEB_ROOT/teams.php?fflteam_id=" . $team->getFFLTeamID() . "\">" . $team->getFFLTeamCity() . " " . $team->getFFLTeamName() . "</a></td>\n";
			echo "	<td>" . $team_rank["rec"] . "</td>\n";
			echo "	<td>" . $team_rank["pts"] . "</td>\n";
			echo "	<td>" . $team_rank["bkdn"] . "</td>\n";
			echo "	<td>" . $team_rank["power"] . "</td>\n";
			echo "		</tr>\n";
		}

		echo "</table>\n";


		break;
	default:
		if (!($_SESSION["fflteam_id"] > 0))
		{
?>
		<div id="subnav"><ul>
			<li id="caption">Standings Subpages:</li>
			<li><a href="<?php echo $WEB_ROOT; ?>/standings.php?view=standard<?php if ($_GET["year"] != getThisYear()) { echo "&amp;year=" . $_GET["year"]; } ?>">Standard</a></li>
			<li><a href="<?php echo $WEB_ROOT; ?>/standings.php?view=breakdown<?php if ($_GET["year"] != getThisYear()) { echo "&amp;year=" . $_GET["year"]; } ?>">Breakdown</a></li>
			<li><a href="<?php echo $WEB_ROOT; ?>/standings.php?view=power<?php if ($_GET["year"] != getThisYear()) { echo "&amp;year=" . $_GET["year"]; } ?>">Power Rankings</a></li>
		</ul></div>
<?php
			break;
		}
		$thisTeam = new OFFL_FFLTeam($_SESSION["fflteam_id"]);
		$teams = $myleague->getAllFFLTeams();
		usort($teams, "league_rank");
		
		echo "<table id=\"standing-overview\">\n";
		echo "<thead><tr><th colspan=\"2\"><a href=\"$WEB_ROOT/teams.php?fflteam_id=" . $thisTeam->getFFLTeamID() . "\">" . $thisTeam->getFFLTeamFullName() . "</a></th></tr></thead>\n";
		echo "<tfoot><tr><td style=\"display:none;\"></td></tr></tfoot>\n";
		echo "<tbody>\n";
		echo "	<tr class=\"oddrow\"><th rowspan=\"3\"><a href=\"$WEB_ROOT/standings.php?view=standard";
		if ($_GET["year"] != getThisYear()) { echo "&amp;year=" . $_GET["year"]; }
		echo "\">Standard</a></th>\n";
		echo "		<td>" . $thisTeam->getOverallWins($_GET["year"]) . "-" . $thisTeam->getOverallLosses($_GET["year"]) . "-" . $thisTeam->getOverallTies($_GET["year"]) . " (" . $thisTeam->getDivisionWins($_GET["year"]) . "-" . $thisTeam->getDivisionLosses($_GET["year"]) . "-" . $thisTeam->getDivisionTies($_GET["year"]) . ")</td></tr>";
		foreach ($teams as $i=>$team)
		{	
			if ($team->getFFLTeamID() == $_SESSION["fflteam_id"])
			{	break;	}
		}
		echo "	<tr class=\"oddrow\"><td>#" . ++$i . " in League</td></tr>\n";
		$divisions = $myleague->getAllDivisions();
		$team_array = array();
		foreach ($divisions as $division)
		{	$team_array[$division->getDivisionID()] = $division->getAllDivisionTeams();	}
		usort($team_array[$thisTeam->getDivisionID()], "division_rank");
		foreach ($team_array[$thisTeam->getDivisionID()] as $i=>$team)
		{
			if ($team->getFFLTeamID() == $_SESSION["fflteam_id"])
			{	break;	}
		}
		echo "	<tr class=\"oddrow\"><td>#" . ++$i . " in " . $thisTeam->getDivisionDesc() . " Division</td></tr>\n";
		echo "	<tr class=\"evenrow\"><th rowspan=\"2\"><a href=\"$WEB_ROOT/standings.php?view=breakdown";
		if ($_GET["year"] != getThisYear()) { echo "&amp;year=" . $_GET["year"]; }
		echo "\">Breakdown</a></th>\n";
		echo "		<td>" . $team_record_overall[$_SESSION["fflteam_id"]]["pct"] . " Win Pct</td></tr>";
		$i = 1;
		foreach ($team_bkdn_order as $teamids)
		{
			$team_id_arr = explode(",", $teamids);
			if (in_array($_SESSION["fflteam_id"], $team_id_arr))
			{	break;	}
			$i+=sizeof($team_id_arr);
		}
		echo "	<tr class=\"evenrow\"><td>#$i in League</td></tr>\n";
		echo "	<tr class=\"oddrow\"><th rowspan=\"2\"><a href=\"$WEB_ROOT/standings.php?view=power";
		if ($_GET["year"] != getThisYear()) { echo "&amp;year=" . $_GET["year"]; }
		echo "\">Power Rankings</a></th>\n";
		echo "		<td>" . $power_rank[$_SESSION["fflteam_id"]]["power"] . " / " . 3*sizeof($teams) . " Pts</td></tr>";
		$i = 1;
		foreach ($team_power_order as $teamids)
		{
			$team_id_arr = explode(",", $teamids);
			if (in_array($_SESSION["fflteam_id"], $team_id_arr))
			{	break;	}
			$i += sizeof($team_id_arr);
		}
		echo "	<tr class=\"oddrow\"><td>#$i in League</td></tr>\n";
		
		echo "</tbody>\n";
		echo "</table>\n";
		break;
	}


?>

<?php include_once("$DOC_ROOT/lib/footer.php"); ?>
Return current item: Online Fantasy Football League