Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/lib/classes/offl_fflteam.php
<?php
/**
 * This file defines the {@link OFFL_FFLTeam} class as well as the comparison functions used by {@link usort() usort()} and its associated functions
 *
 * @package offl
 * @author Stephen Rochelle <hide@address.com>
 */

if (strtr(__FILE__, "\\", "/") == $_SERVER["SCRIPT_FILENAME"])
{	die ("Cannot access file directly!");	}

require_once($DOC_ROOT . "/lib/classes/offl_dbobject.php");
require_once($DOC_ROOT . "/lib/classes/offl_control.php");
require_once($DOC_ROOT . "/lib/classes/offl_position.php");
require_once($DOC_ROOT . "/lib/classes/offl_player.php");
require_once($DOC_ROOT . "/lib/classes/offl_rosterplayer.php");

/**
 * This class provides the interface to FFL teams and their info, including rosters.
 *
 * Integration is currently proceeding with the new {@link OFFL_RosterPlayer} class, intended to accelerate database performance.
 *
 * @package offl
 */
class OFFL_FFLTeam extends OFFL_DBObject
{
	/**
	 * @var integer
	 */
	var $_fflteam_id = NULL;
	/**
	 * @var string
	 */
	var $_fflteam_city = "";
	/**
	 * @var string
	 */
	var $_fflteam_name = "";
	/**
	 * @var string
	 */
	var $_fflteam_logo = "";
	/**
	 * @var integer
	 */
	var $_league_id = 0;
	/**
	 * @var integer
	 */
	var $_user_id = 0;
	/**
	 * @var string
	 */
	var $_stadium_name = "";
	/**
	 * @var string
	 */
	var $_coach_head = "";
	/**
	 * @var string
	 */
	var $_coach_off = "";
	/**
	 * @var string
	 */
	var $_coach_def = "";
	/**
	 * @var string
	 */
	var $_coach_spec = "";
	/**
	 * @var integer
	 */
	var $_division_id = 0;
	/**
	 * @var string
	 */
	var $_division_desc = "";
	/**
	 * @var integer
	 */
	var $_waiver_priority = 0;
	/**
	 * @var array
	 */
	var $_roster_array = array();

	/**
	 * @var boolean
	 */
	var $_computed_year = FALSE;
	/**
	 * @var integer
	 */
	var $_overall_wins = 0;
	/**
	 * @var integer
	 */
	var $_overall_losses = 0;
	/**
	 * @var integer
	 */
	var $_overall_ties = 0;
	/**
	 * @var float
	 */
	var $_overall_win_pct = NULL;
	/**
	 * @var integer
	 */
	var $_division_wins = 0;
	/**
	 * @var integer
	 */
	var $_division_losses = 0;
	/**
	 * @var integer
	 */
	var $_division_ties = 0;
	/**
	 * @var float
	 */
	var $_division_win_pct = NULL;
	/**
	 * @var number
	 */
	var $_overall_pf = 0;
	/**
	 * @var number
	 */
	var $_overall_pa = 0;
	/**
	 * @var integer
	 */
	var $_overall_games = 0;
	/**
	 * @var number
	 */
	var $_division_pf = 0;
	/**
	 * @var number
	 */
	var $_division_pa = 0;
	/**
	 * @var integer
	 */
	var $_division_games = 0;

	/**
	 * Class constructor
	 *
	 * @param integer $fflteam_id  Optional.  If set, loads team with given fflteam_id from database.
	 */
	function OFFL_FFLTeam ($fflteam_id = NULL)
	{
		OFFL_DBObject::OFFL_DBObject();

		if(!is_null($fflteam_id))
		{
			$this->_fflteam_id = $fflteam_id;
			$this->populate();
		}
	}
	
	/**
	 * Loads relevant info from the database for this object.  {@link $_fflteam_id} must be set.
	 * @return boolean Returns TRUE on success, FALSE otherwise
	 */
	function populate ()
	{
		if(is_null($this->_fflteam_id))
		{
			$this->_emsg = "\$_fflteam_id is not set.	Cannot populate OFFL_FFLTeam object.";
			error_log ($this->_emsg);
			return FALSE;
		}
		$sql = "select * from fflteams left join divisions on fflteams.division_id=divisions.division_id where fflteam_id=$this->_fflteam_id";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		if(mysql_num_rows($result) == 0)
		{
			$this->_emsg = "No team records found for fflteam_id $this->_fflteam_id.";
			error_log ($this->_emsg);
			return FALSE;
		}
		$this->_user_id = mysql_result($result,0,"user_id");
		$this->_league_id = mysql_result($result,0,"league_id");
		$this->_division_id = mysql_result($result,0,"division_id");
		$this->_fflteam_city = htmlspecialchars(stripslashes(mysql_result($result,0,"fflteam_city")));
		$this->_fflteam_name = htmlspecialchars(stripslashes(mysql_result($result,0,"fflteam_name")));
		$this->_fflteam_logo = htmlspecialchars(stripslashes(mysql_result($result,0,"fflteam_logo")));
		$this->_stadium_name = htmlspecialchars(stripslashes(mysql_result($result,0,"stadium_name")));
		$this->_coach_head = htmlspecialchars(stripslashes(mysql_result($result,0,"coach_head")));
		$this->_coach_off = htmlspecialchars(stripslashes(mysql_result($result,0,"coach_off")));
		$this->_coach_def = htmlspecialchars(stripslashes(mysql_result($result,0,"coach_def")));
		$this->_coach_spec = htmlspecialchars(stripslashes(mysql_result($result,0,"coach_spec")));
		$this->_waiver_priority = mysql_result($result,0,"waiver_priority");
		$this->_division_desc = htmlspecialchars(stripslashes(mysql_result($result,0,"division_desc")));

		$roster_lookup = new OFFL_RosterPlayer();
		$roster_lookup->getRosterInfo($this->_fflteam_id, $roster_player_id_array, $roster_starters_array);

//		$roster_player_id_array = array_diff(explode(",", mysql_result($result,0,"player_ids")), array(""));
//		$roster_starters_array = array_diff(explode(",", mysql_result($result,0,"starters")), array(""));
		while(sizeof($roster_player_id_array) > 0)
		{	$this->_roster_array[array_pop($roster_player_id_array)] = array_pop($roster_starters_array);	}

		mysql_free_result($result);
		return TRUE;
	}

	/**
	 * Saves info to database.
	 *
	 * will INSERT if {@link $_fflteam_id} is not set (allowing MySQL to autoincrement), or UPDATE otherwise.
	 */
	function save ()
	{
		$roster_player_id_array = implode(",", array_keys($this->_roster_array));
		$roster_starters_array = implode(",", array_values($this->_roster_array));
		if(is_null($this->_fflteam_id))
		{
			// insert
			$sql = "INSERT INTO fflteams (fflteam_city, fflteam_name, fflteam_logo, user_id, league_id, division_id, waiver_priority, stadium_name, coach_head, coach_off, coach_def, coach_spec, player_ids, starters) VALUES ('" . mysql_escape_string($this->_fflteam_city) . "', '" . mysql_escape_string($this->_fflteam_name) . "', '" . mysql_escape_string($this->_fflteam_logo) . "', $this->_user_id, $this->_league_id, $this->_division_id, $this->_waiver_priority, '" . mysql_escape_string($this->_stadium_name) . "', '" . mysql_escape_string($this->_coach_head) . "', '" . mysql_escape_string($this->_coach_off) . "', '" . mysql_escape_string($this->_coach_def) . "', '" . mysql_escape_string($this->_coach_spec) . "', '" . $roster_player_id_array . "', '" . $roster_starters_array . "')";
		}
		else
		{
			// update
			$sql = "UPDATE fflteams SET fflteam_city='" . mysql_escape_string($this->_fflteam_city) . "', fflteam_name='" . mysql_escape_string($this->_fflteam_name) . "', fflteam_logo='" . mysql_escape_string($this->_fflteam_logo) . "', user_id=$this->_user_id, league_id=$this->_league_id, division_id=$this->_division_id, waiver_priority=$this->_waiver_priority, stadium_name='" . mysql_escape_string($this->_stadium_name) . "', coach_head='" . mysql_escape_string($this->_coach_head) . "', coach_off='" . mysql_escape_string($this->_coach_off) . "', coach_def='" . mysql_escape_string($this->_coach_def) . "', coach_spec='" . mysql_escape_string($this->_coach_spec) . "', player_ids='" . $roster_player_id_array . "', starters='" . $roster_starters_array . "' WHERE fflteam_id=$this->_fflteam_id";
		}

		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		// populate the UID
		if (is_null($this->_fflteam_id))
		{	$this->_fflteam_id = mysql_insert_id();	}
	}

	/**
	 * @return integer
	 */
	function getFFLTeamID()
	{
		return $this->_fflteam_id;
	}

	/**
	 * @param string $fflteam_city
	 */
	function setFFLTeamCity($fflteam_city)
	{
		$this->_fflteam_city = $fflteam_city; 
	}

	/**
	 * @return string
	 */
	function getFFLTeamCity()
	{
		return $this->_fflteam_city;
	}

	/**
	 * @param string $fflteam_name
	 */
	function setFFLTeamName($fflteam_name)
	{
		$this->_fflteam_name = $fflteam_name; 
	}

	/**
	 * @return string
	 */
	function getFFLTeamName()
	{
		return $this->_fflteam_name;
	}

	/**
	 * Properly conjoins {@link $_fflteam_city} and {@link $_fflteam_name} to avoid unnecessary whitespace.
	 * @return string
	 */
	function getFFLTeamFullName()
	{
		return trim($this->_fflteam_city . " " . $this->_fflteam_name);
	}

	/**
	 * @param string $fflteam_logo filename of team logo, full path required
	 */
	function setFFLTeamLogo($fflteam_logo)
	{
		$this->_fflteam_logo = $fflteam_logo; 
	}

	/**
	 * @return string
	 */
	function getFFLTeamLogo()
	{
		return $this->_fflteam_logo;
	}

	/**
	 * @param integer $user_id
	 */
	function setUserID($user_id)
	{
		$this->_user_id = $user_id; 
	}

	/**
	 * @return integer
	 */
	function getUserID()
	{
		return $this->_user_id;
	}

	/**
	 * @param integer $league_id
	 */
	function setLeagueID($league_id)
	{
		$this->_league_id = $league_id; 
	}

	/**
	 * @return integer
	 */
	function getLeagueID()
	{
		return $this->_league_id;
	}

	/**
	 * @param integer $division_id
	 */
	function setDivisionID($division_id)
	{
		$this->_division_id = $division_id; 
	}

	/**
	 * @return integer
	 */
	function getDivisionID()
	{
		return $this->_division_id;
	}

	/**
	 * @return string
	 */
	function getDivisionDesc()
	{
		return $this->_division_desc;
	}

	/**
	 * Sets the waiver claim priority for an FFL team
	 *
	 * If the parameter is included, the team's priority is set to that.  No management to avoid priority conflicts is done.
	 * Parameter omission results in giving the calling team highest priority and bumping all other teams (in league, once
	 * revised) back one spot.
	 * @param integer $priority optional
	 */
	function setWaiverPriority($priority=NULL)
	{
		// the downside here is that this affects all preexisting teams if autosetting
		if (isset($priority))
		{
			// NO INHERENT MANAGEMENT OF ALL TEAM PRIORITIES
			// use Waiver->resetTeamPriority() for that
			$this->_waiver_priority = $priority;
			return;
		}
		// do autodetect, insert in front
		// this way new teams (say those added late)
		// get initial waiver priority
		$x = new OFFL_League($this->_league_id);
		$teams = $x->getAllFFLTeams();

		$lowpri = 1000;
		foreach ($teams as $team)
		{
			if ($lowpri > $team->getWaiverPriority())
				$lowpri = $team->getWaiverPriority();
			$team->setWaiverPriority($team->getWaiverPriority() + 1);
			$team->save();
		}
		$this->_waiver_priority = $lowpri;
	}

	/**
	 * @return integer
	 */
	function getWaiverPriority()
	{
		return $this->_waiver_priority;
	}

	/**
	 * @param string $stadium_name
	 */
	function setStadiumName($stadium_name)
	{
		$this->_stadium_name = $stadium_name; 
	}

	/**
	 * @return string
	 */
	function getStadiumName()
	{
		return $this->_stadium_name;
	}

	/**
	 * @param string $coach_head
	 */
	function setCoachHead($coach_head)
	{
		$this->_coach_head = $coach_head; 
	}

	/**
	 * @return string
	 */
	function getCoachHead()
	{
		return $this->_coach_head;
	}

	/**
	 * @param string $coach_off
	 */
	function setCoachOff($coach_off)
	{
		$this->_coach_off = $coach_off; 
	}

	/**
	 * @return string
	 */
	function getCoachOff()
	{
		return $this->_coach_off;
	}

	/**
	 * @param string $coach_def
	 */
	function setCoachDef($coach_def)
	{
		$this->_coach_def = $coach_def; 
	}

	/**
	 * @return string
	 */
	function getCoachDef()
	{
		return $this->_coach_def;
	}

	/**
	 * @param $coach_spec
	 */
	function setCoachSpec($coach_spec)
	{
		$this->_coach_spec = $coach_spec; 
	}

	/**
	 * @return string
	 */
	function getCoachSpec()
	{
		return $this->_coach_spec;
	}

	/**
	 * Forms {@link $_roster_array} based on an array of {@link OFFL_Player} objects.
	 *
	 * This function assumes advance validation via {@link validateRosterFlex()}; no internal validation is done.
	 *
	 * @param array $players {@link OFFL_Player} objects representing a team's roster.
	 */
	function setRoster($players)
	{
		$this->SQLQuery("DELETE FROM `rosterplayers` WHERE fflteam_id='" . $this->_fflteam_id . "'");
		$this->_roster_array = array();
		foreach ($players as $player)
		{
			$this->_roster_array[$player->getPlayerID()] = $player->getStarter();
			$RP = new OFFL_RosterPlayer();
			$RP->setLeagueID($this->_league_id);
			$RP->setPlayerID($player->getPlayerID());
			$RP->setFFLTeamID($this->_fflteam_id);
			$RP->setStarter($player->getStarter());
			$RP->save();
		}
	}

	/**
	 * Sets the start status of a {@link OFFL_Player player}.  Assumption is that player is already on roster.
	 *
	 * @param integer $player_id
	 * @param integer $starter Uses integral boolean values (1/0).  1 is starter, 0 is bench.
	 */
	function setStartStatus($player_id, $starter)
	{
		if ($starter)
		{	$starter = 1;	}
		else
		{	$starter = 0;	}
		$this->_roster_array[$player_id] = $starter;
		$RP = new OFFL_RosterPlayer();
		$RP->setPlayerID($player_id);
		$RP->setFFLTeamID($this->_fflteam_id);
		$RP->setLeagueID($this->_league_id);
		$RP->setStarter($starter);
		$RP->save();
	}

	/**
	 * Returns an array of {@link OFFL_Player} objects associated with the FFL team, modified by parameters
	 *
	 * This function executes {@link OFFL_Player::populateLeagueSpecific()} to get all player info, not just generic
	 *
	 * @param boolean $starters_only Optional: If TRUE, only returns starters, if FALSE, returns only bench, if NULL, returns both
	 * @param integer $position Optional: If set, returns only players with the given {@link OFFL_Position} position_id.
	 * @return array Returns an array of {@link OFFL_Player} objects
	 */
	function getRoster($starters_only=NULL, $position=0)
	{
		$retArr = array();
		$sql = "SELECT player_id FROM players WHERE (";
		foreach ($this->_roster_array as $id=>$starter)
		{
			if ((($starters_only == TRUE) && !$starter) || (($starters_only === FALSE) && $starter))
			{	continue;	}
			$sql .= "player_id=$id OR ";
		}
		$sql .= "1=0)";
		if ($position)
		{	$sql .= " AND position_id=$position";	}
		$sql .= " ORDER BY position_id, lname, fname";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for($i=0; $i<mysql_num_rows($result); $i++)
		{
			$player = new OFFL_Player(mysql_result($result,$i,"player_id"));
			$player->populateLeagueSpecific($this->_league_id);
			array_push($retArr, $player);
		}
	
		mysql_free_result($result);

		usort($retArr, "cmp_players_starter");

		return $retArr;
	}

	/**
	 * Adds a player to the roster
	 *
	 * @param integer $player_id {@link OFFL_Player::$_player_id UID} of {@link OFFL_Player} to add to team
	 * @param boolean $starter Integral boolean (1/0); Optional: starter status of player (default 0)
	 */
	function addPlayer($player_id, $starter = 0)
	{
		$this->_roster_array[$player_id] = $starter;
		$RP = new OFFL_RosterPlayer();
		$RP->setLeagueID($this->_league_id);
		$RP->setPlayerID($player_id);
		$RP->setFFLTeamID($this->_fflteam_id);
		$RP->setStarter($starter);
		$RP->save();
	}

	/**
	 * Removes a player from the roster 
	 * @param integer $player_id {@link OFFL_Player::$_player_id UID} of {@link OFFL_Player} to remove from team
	 * @return boolean TRUE if dropped, FALSE if not on team initially
	 */
	function dropPlayer($player_id)
	{
		if (array_key_exists($player_id, $this->_roster_array))
		{
			unset($this->_roster_array[$player_id]);
			$RP = new OFFL_RosterPlayer($player_id, $this->_league_id);
			$RP->deleteRosterPlayer();
			return TRUE;
		}
		return FALSE;
	}

	/**
	 * Returns the internal array structure, used by {@link OFFL_Player}
	 *
	 * @param boolean $keys_only Optional: if TRUE, returns only the player ids, not starter status
	 * @return array {@link $_roster_array}
	 */
	function getRosterIDs($keys_only = FALSE)
	{
		if ($keys_only)
		{	return array_keys($this->_roster_array);	}
		return $this->_roster_array;
	}

	/**
	 * Tests for presence of player on roster
	 *
	 * @return boolean TRUE if found, FALSE otherwise
	 */
	function onRoster($player_id)
	{
		if (in_array($player_id, array_keys($this->_roster_array)))
		{	return TRUE;	}
		return FALSE;
	}

	/**
	 * @deprecated Deprecated in favor of {@link validateRosterFlex()}.
	 */
	function validateRoster($roster)
	{
		$control = new OFFL_Control();
		$errors = array();
		if(sizeof($roster) > $control->getValue("CONFIG_MAX_TOTAL_PLAYERS"))
		{	array_push($errors,"Total roster size limit (" . $control->getValue("CONFIG_MAX_TOTAL_PLAYERS") . ") violated.");	}
		$x = new OFFL_Position();
		$positions = $x->getAllPositions();
		for($i=0; $i<sizeof($positions); $i++)
		{
			$k = "CONFIG_MAX_TOTAL_" . $positions[$i]->getPositionAbbv();
			$v = $control->getValue($k);
			$c = 0;
			for($j=0; $j<sizeof($roster); $j++)
			{	if($roster[$j]->getPositionID() == $positions[$i]->getPositionID())
				{	$c++;	}	}

			if($c > $v)
			{	array_push($errors,$positions[$i]->getPositionDesc() . " roster size limit (" . $v . ") violated.");	}
		}
		
		return $errors;
	}

	/**
	 * Tests an array of {@link OFFL_Player OFFL_Player objects} for roster rule correctness.
	 *
	 * This function does not test the internal {@link $_roster_array} to allow scripts to assemble sample rosters without
	 * committing changes.  May be altered once I check new rules regarding team assignment.
	 *
	 * @param array $roster Array of {@link OFFL_Player} objects.
	 * @return array Returned string array contains error messages.  If empty, roster is legal.
	 */
	function validateRosterFlex($roster)
	{
		$errors = array();

		$x = new OFFL_Position;
		$positions = $x->getAllPositions();

		$x = new OFFL_Control();
		$x->setLeagueID($this->_league_id);
		$numstarters = $x->getValue("CONFIG_MAX_START_PLAYERS");
		$numplayers = $x->getValue("CONFIG_MAX_TOTAL_PLAYERS");

		if (sizeof($roster) > $numplayers)
		{	array_push($errors, "Total roster size limit (" . $numplayers . ") violated.");	}

		$pos_count = array();
		$max_pos_count = array();
		$max_pos_allowed = array();
		$pos_name = array();
		$pos_count_index = array();
		$lastposid = 0;

		foreach ($positions as $position)
		{
			$posid = $position->getPositionID();
			$pos_count[$position->getPositionAbbv()] = 0;
			$pos_count_index[$posid] = 0;
			$pos_name[$posid] = $position->getPositionAbbv();
			if ($lastposid < $posid)
			{	$lastposid = $posid;	}

			if (strpos($position->getPositionAbbv(), "FLEX") === FALSE)
			{
				$max_pos_count[$position->getPositionAbbv()] = 0;
				$max_pos_allowed[$position->getPositionAbbv()] = $x->getValue("CONFIG_MAX_TOTAL_" . $position->getPositionAbbv());
			}
		}
		$lastposid++;

		// check the allowed players
		foreach ($roster as $player)
		{	$max_pos_count[$player->getPositionAbbv()]++;	}
		foreach ($max_pos_allowed as $key=>$limit)
		{	if ($max_pos_count[$key] > $limit)
			{	array_push($errors, "Total $key limit ($limit) violated.");	}	}

		for ($i = 0; $i < sizeof($pos_count); $i++)
		{
			$key = key($pos_count);
			$key_index = key($pos_count_index);
			$pos = $x->getValue("CONFIG_MAX_START_$key");
			$pos_count[$key] = $pos;
			$pos_count_index[$key_index] = $pos;
			next($pos_count);
			next($pos_count_index);
		}

		$pos_count_index_orig = $pos_count_index; // backup for referencing allowed overflow
		$mystarters = 0;

		for ($i = 0; $i < sizeof($roster); $i++)
		// now I have to check by positions.	Flex is tough to account for.
		{
			if ($roster[$i]->getStarter() == 0)
			{	continue;	}

			$player_complete = 0; // this is the flag variable for finding a spot in the starting lineup.
			$mystarters++; // check allowed number of starters

			$posid = $roster[$i]->getPositionID();
			$posname = $pos_name[$posid];
			// if the string is part of index x on pos_name, subtract 1 from index x on pox_count_index
			foreach($pos_name as $posnum => $pos)
			{
				if (strstr($pos, $posname)) // this is a valid flex position
				{
					if ($pos_count_index[$posnum] > 0)
					{
						$pos_count_index[$posnum]--;
						$player_complete = 1;
						break; // don't subtract more positions
					}
				}
			}
			if (!$player_complete)
			// didn't find a spot to put this starter!
			{
				$pos_allowed = 0; // count max allowed (with flex) for this position
				foreach($pos_name as $posnum => $pos)
					if (strstr($pos, $posname)) // this is a valid standard / flex position
						$pos_allowed += $pos_count_index_orig[$posnum];
				array_push($errors, "Total starting $pos_name[$posid] limit ($pos_allowed regular and flex) violated.");
			}
		}
		if ($mystarters > $numstarters)
		{	array_push($errors, "Total starter limit (" . $numstarters . ") violated.");	}

		return $errors;
	}

	/**
	 * Retrieves a team's schedule for a given year.
	 *
	 * @param integer $year
	 * @return array Returns an array of {@link OFFL_Game} objects.
	 */
	function getSchedule($year)
	{
		$retArr = array();
		$sql = "select game_id from games where year=" . $year . " and (h_fflteam_id=$this->_fflteam_id or v_fflteam_id=$this->_fflteam_id) order by week";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": (__FILE__ / __LINE__) $sql");

		for($i=0; $i<mysql_num_rows($result); $i++)
		{
			$game = new OFFL_Game(mysql_result($result,$i,"game_id"));
			array_push($retArr, $game);
		}

		mysql_free_result($result);
		return $retArr;
	}

	/**
	 * Sets the internal variables related to the team's record.  Called as needed by retrieval functions.
	 *
	 * @param integer $year.
	 */
	function computeRecord($year)
	{
		// Get division games first
		$divisionteams = array();
		$control = new OFFL_Control($this->_league_id);
		
		if ($year < getThisYear())
		{
			$pastLeague = new OFFL_PastLeague($this->_league_id, $year);
			foreach ($pastLeague->getDivisionIDs() as $div_id)
			{
				if (in_array($this->_fflteam_id, $pastLeague->getFFLTeamIDs($div_id)))
				{
					$this->_division_id = $div_id;
					break;
				}
			}
			$divisionteams = $pastLeague->getFFLTeamIDs($this->_division_id);
		}
		else
		{
			$sql = "select fflteam_id from fflteams where division_id=$this->_division_id";
			$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
			for($i=0; $i<mysql_num_rows($result); $i++)
			{
				$v = mysql_result($result,$i,"fflteam_id");
				array_push($divisionteams, $v);
			}
		}
			$sql = "select g.* from games g,fflteams f " . 
			"where g.year=$year " . 
			"and ((g.h_fflteam_id=$this->_fflteam_id and f.fflteam_id=g.v_fflteam_id) " . 
			"or (g.v_fflteam_id=$this->_fflteam_id and f.fflteam_id=g.h_fflteam_id)) " . 
			"order by week";

		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
		for($i=0; $i<mysql_num_rows($result); $i++)
		{
			$final = mysql_result($result,$i,"final");
			if(mysql_result($result,$i,"h_fflteam_id") == $this->_fflteam_id)
			{
				$their_team_id = mysql_result($result, $i, "v_fflteam_id");
				$my_score = mysql_result($result,$i,"h_fflteam_score"); 
				$their_score = mysql_result($result,$i,"v_fflteam_score"); 
			}
			else
			{
				$their_team_id = mysql_result($result, $i, "h_fflteam_id");
				$my_score = mysql_result($result,$i,"v_fflteam_score"); 
				$their_score = mysql_result($result,$i,"h_fflteam_score"); 
			}
			if (in_array(sprintf("%d",$their_team_id), $divisionteams))
			{
				if ($final)
				{
					$this->_division_games++;
					if($my_score > $their_score)
					{	$this->_division_wins++;	}
					elseif($my_score < $their_score)
					{	$this->_division_losses++;	}
					elseif(($my_score != "")&&($their_score != ""))
					{	$this->_division_ties++;	}

					$this->_division_pf += $my_score;
					$this->_division_pa += $their_score;
				}
			} // end if mysql_result
			if ($final)
			{
				$this->_overall_games++;
				if($my_score > $their_score)
				{	$this->_overall_wins++;	}
				elseif($my_score < $their_score)
				{	$this->_overall_losses++;	}
				elseif(($my_score != "")&&($their_score != ""))
				{	$this->_overall_ties++;	}
				$this->_overall_pf += $my_score;
				$this->_overall_pa += $their_score;
			}

		} // end for mysql results

		$overall_games = $this->_overall_wins + $this->_overall_losses + $this->_overall_ties;
		$division_games = $this->_division_wins + $this->_division_losses + $this->_division_ties;

		if ($overall_games)
		{	$this->_overall_win_pct = intval( 1000 * ($this->_overall_wins + $this->_overall_ties/2) / ($overall_games) ) / 1000;	}
		else
		{	$this->_overall_win_pct = 0;	}
		if ($division_games)
		{	$this->_division_win_pct = intval( 1000 * ($this->_division_wins + $this->_division_ties/2) / ($division_games) ) / 1000;	}
		else
		{	$this->_division_win_pct = 0;	}

		$this->_computed_year = TRUE;
	}

	/**
	 * @param integer $year
	 * @return integer
	 */
	function getOverallWins($year)
	{
		if(!$this->_computed_year)
		{	$this->computeRecord($year);	}
		return $this->_overall_wins;
	}

	/**
	 * @param integer $year
	 * @return integer
	 */
	function getOverallLosses($year)
	{
		if(!$this->_computed_year)
		{	$this->computeRecord($year);	}
		return $this->_overall_losses;
	}

	/**
	 * @param integer $year
	 * @return integer
	 */
	function getOverallTies($year)
	{
		if(!$this->_computed_year)
		{	$this->computeRecord($year);	}
		return $this->_overall_ties;
	}

	/**
	 * @param integer $year
	 * @return integer
	 */
	function getOverallWinPct($year)
	{
		if(!$this->_computed_year)
		{	$this->computeRecord($year);	}
		$str = strval($this->_overall_win_pct);
		if (strlen($str) == 0)
		{	$str = "0.000";	}
		elseif (strlen($str) == 1)
		{	$str .= ".000";	}
		else
		{	for ($i = strlen($str); $i < 5; $i++)
			{	$str .= "0";	}	}
		return $str;
	}

	/**
	 * @param integer $year
	 * @return integer
	 */
	function getDivisionWins($year)
	{
		if(!$this->_computed_year)
		{	$this->computeRecord($year);	}
		return $this->_division_wins;
	}

	/**
	 * @param integer $year
	 * @return integer
	 */
	function getDivisionLosses($year)
	{
		if(!$this->_computed_year)
		{	$this->computeRecord($year);	}
		return $this->_division_losses;
	}

	/**
	 * @param integer $year
	 * @return integer
	 */
	function getDivisionTies($year)
	{
		if(!$this->_computed_year)
		{	$this->computeRecord($year);	}
		return $this->_division_ties;
	}

	/**
	 * @param integer $year
	 * @return integer
	 */
	function getDivisionWinPct($year)
	{
		if(!$this->_computed_year)
		{	$this->computeRecord($year);	}
		$str = strval($this->_division_win_pct);
		if (strlen($str) == 0)
		{	$str = "0.000";	}
		elseif (strlen($str) == 1)
		{	$str .= ".000";	}
		else
		{	for ($i = strlen($str); $i < 5; $i++)
			{	$str .= "0";	}	}
		return $str;
	}

	/**
	 * @param integer $year
	 * @return number
	 */
	function getOverallPF($year)
	{
		if(!$this->_computed_year)
		{	$this->computeRecord($year);	}
		return $this->_overall_pf;
	}

	/**
	 * @param integer $year
	 * @return number
	 */
	function getOverallPA($year)
	{
		if(!$this->_computed_year)
		{	$this->computeRecord($year);	}
		return $this->_overall_pa;
	}

	/**
	 * @param integer $year
	 * @return integer
	 */
	function getOverallGames($year)
	{
		if(!$this->_computed_year)
		{	$this->computeRecord($year);	}
		return $this->_overall_games;
	}

	/**
	 * @param integer $year
	 * @return number
	 */
	function getDivisionPF($year)
	{
		if(!$this->_computed_year)
		{	$this->computeRecord($year);	}
		return $this->_division_pf;
	}

	/**
	 * @param integer $year
	 * @return number
	 */
	function getDivisionPA($year)
	{
		if(!$this->_computed_year)
		{	$this->computeRecord($year);	}
		return $this->_division_pa;
	}

	/**
	 * @param integer $year
	 * @return integer
	 */
	function getDivisionGames($year)
	{
		if(!$this->_computed_year)
		{	$this->computeRecord($year);	}
		return $this->_division_games;
	}

} // end OFFL_FFLTeam class

// THESE ARE SORT FUNCTIONS FOR DETERMINING PLAYOFF RANK

/**
 * Ranks {@link OFFL_FFLTeam FFL teams} for standings display.  For use with {@link usort() usort()}, et al, only.
 *
 * This function will be renamed for clarity
 */
function Compare($team1, $team2)
{
	if(!isset($_REQUEST["year"]) && isset($_GET["year"]))
	{	$_REQUEST["year"] = $_GET["year"];	}

	// check for season init
	if (($team1->getOverallGames($_REQUEST["year"]) == 0) && ($team2->getOverallGames($_REQUEST["year"]) == 0))
		return 0; // not initialized, random tiebreaker

	if($team1->getOverallWinPct($_REQUEST["year"]) > $team2->getOverallWinPct($_REQUEST["year"]))
		return -1;
	elseif($team1->getOverallWinPct($_REQUEST["year"]) < $team2->getOverallWinPct($_REQUEST["year"]))
		return 1;
		elseif($team1->getOverallWins($_REQUEST["year"]) > $team2->getOverallWins($_REQUEST["year"]))
			return -1;
		elseif($team1->getOverallWins($_REQUEST["year"]) < $team2->getOverallWins($_REQUEST["year"]))
			return 1;
		elseif($team1->getOverallLosses($_REQUEST["year"]) < $team2->getOverallLosses($_REQUEST["year"]))
			return -1;
		elseif($team1->getOverallLosses($_REQUEST["year"]) > $team2->getOverallLosses($_REQUEST["year"]))
			return 1;
			elseif($team1->getDivisionWinPct($_REQUEST["year"]) > $team2->getDivisionWinPct($_REQUEST["year"]))
				return -1;
			elseif($team1->getDivisionWinPct($_REQUEST["year"]) < $team2->getDivisionWinPct($_REQUEST["year"]))
				return 1;
				elseif($team1->getDivisionWins($_REQUEST["year"]) > $team2->getDivisionWins($_REQUEST["year"]))
						return -1;
				elseif($team1->getDivisionWins($_REQUEST["year"]) < $team2->getDivisionWins($_REQUEST["year"]))
						return 1;
				elseif($team1->getDivisionLosses($_REQUEST["year"]) < $team2->getDivisionLosses($_REQUEST["year"]))
						return -1;
				elseif($team1->getDivisionLosses($_REQUEST["year"]) > $team2->getDivisionLosses($_REQUEST["year"]))
						return 1;
					elseif(($team1->getOverallPF($_REQUEST["year"]) / $team1->getOverallGames($_REQUEST["year"])) > ($team2->getOverallPF($_REQUEST["year"]) / $team2->getOverallGames($_REQUEST["year"])))
						return -1;
					elseif(($team1->getOverallPF($_REQUEST["year"]) / $team1->getOverallGames($_REQUEST["year"])) < ($team2->getOverallPF($_REQUEST["year"]) / $team2->getOverallGames($_REQUEST["year"])))
						return 1;
					elseif(($team1->getDivisionPF($_REQUEST["year"]) / $team1->getDivisionGames($_REQUEST["year"])) > ($team2->getDivisionPF($_REQUEST["year"]) / $team2->getDivisionGames($_REQUEST["year"])))
						return -1;
					elseif(($team1->getDivisionPF($_REQUEST["year"]) / $team1->getDivisionGames($_REQUEST["year"])) < ($team2->getDivisionPF($_REQUEST["year"]) / $team2->getDivisionGames($_REQUEST["year"])))
						return 1;
						elseif(($team1->getOverallPA($_REQUEST["year"]) / $team1->getOverallGames($_REQUEST["year"])) < ($team2->getOverallPA($_REQUEST["year"]) / $team2->getOverallGames($_REQUEST["year"])))
							return -1;
						elseif(($team1->getOverallPA($_REQUEST["year"]) / $team1->getOverallGames($_REQUEST["year"])) > ($team2->getOverallPA($_REQUEST["year"]) / $team2->getOverallGames($_REQUEST["year"])))
							return 1;
						elseif(($team1->getDivisionPA($_REQUEST["year"]) / $team1->getDivisionGames($_REQUEST["year"])) < ($team2->getDivisionPA($_REQUEST["year"]) / $team2->getDivisionGames($_REQUEST["year"])))
							return -1;
						elseif(($team1->getDivisionPA($_REQUEST["year"]) / $team1->getDivisionGames($_REQUEST["year"])) > ($team2->getDivisionPA($_REQUEST["year"]) / $team2->getDivisionGames($_REQUEST["year"])))
							return 1;
	return 0; // random tiebreaker
}

/**
 * Ranks {@link OFFL_FFLTeam FFL teams} for playoff (league-wide) seeding determination.  For use with {@link usort() usort()}, et al, only.
 *
 * This function will be renamed for clarity
 */
function league_rank($tm1, $tm2)
{
	if(!isset($_REQUEST["year"]) && isset($_GET["year"]))
	{	$_REQUEST["year"] = $_GET["year"];	}

	// check for season init
	if (($tm1->getOverallGames($_REQUEST["year"]) == 0) && ($tm2->getOverallGames($_REQUEST["year"]) == 0))
		return 0; // not initialized, random tiebreaker

	if($tm1->getOverallWinPct($_REQUEST["year"]) > $tm2->getOverallWinPct($_REQUEST["year"]))
		return -1;
	if($tm1->getOverallWinPct($_REQUEST["year"]) < $tm2->getOverallWinPct($_REQUEST["year"]))
		return 1;
		if($tm1->getOverallWins($_REQUEST["year"]) > $tm2->getOverallWins($_REQUEST["year"]))
			return -1;
		if($tm1->getOverallWins($_REQUEST["year"]) < $tm2->getOverallWins($_REQUEST["year"]))
			return 1;
			if(($tm1->getOverallPF($_REQUEST["year"]) / $tm1->getOverallGames($_REQUEST["year"])) > ($tm2->getOverallPF($_REQUEST["year"]) / $tm2->getOverallGames($_REQUEST["year"])))
				return -1;
			if(($tm1->getOverallPF($_REQUEST["year"]) / $tm1->getOverallGames($_REQUEST["year"])) < ($tm2->getOverallPF($_REQUEST["year"]) / $tm2->getOverallGames($_REQUEST["year"])))
				return 1;
	return 0; // random tiebreaker
}

/**
 * Ranks {@link OFFL_FFLTeam FFL teams} for playoff (divisional) seeding determination.  For use with {@link usort() usort()}, et al, only.
 *
 * This function will be renamed for clarity
 */
function division_rank($tm1, $tm2)
{
	if(!isset($_REQUEST["year"]) && isset($_GET["year"]))
	{	$_REQUEST["year"] = $_GET["year"];	}

	// check for season init
	if (($tm1->getOverallGames($_REQUEST["year"]) == 0) && ($tm2->getOverallGames($_REQUEST["year"]) == 0))
		return 0; // not initialized, random tiebreaker

	if($tm1->getOverallWinPct($_REQUEST["year"]) > $tm2->getOverallWinPct($_REQUEST["year"]))
		return -1;
	if($tm1->getOverallWinPct($_REQUEST["year"]) < $tm2->getOverallWinPct($_REQUEST["year"]))
		return 1;
	if($tm1->getOverallWins($_REQUEST["year"]) > $tm2->getOverallWins($_REQUEST["year"]))
		return -1;
	if($tm1->getOverallWins($_REQUEST["year"]) < $tm2->getOverallWins($_REQUEST["year"]))
		return 1;
		if($tm1->getDivisionWinPct($_REQUEST["year"]) > $tm2->getDivisionWinPct($_REQUEST["year"]))
			return -1;
		if($tm1->getDivisionWinPct($_REQUEST["year"]) < $tm2->getDivisionWinPct($_REQUEST["year"]))
			return 1;
		if($tm1->getDivisionWins($_REQUEST["year"]) > $tm2->getDivisionWins($_REQUEST["year"]))
			return -1;
		if($tm1->getDivisionWins($_REQUEST["year"]) < $tm2->getDivisionWins($_REQUEST["year"]))
			return 1;
			if(($tm1->getOverallPF($_REQUEST["year"]) / $tm1->getOverallGames($_REQUEST["year"])) > ($tm2->getOverallPF($_REQUEST["year"]) / $tm2->getOverallGames($_REQUEST["year"])))
				return -1;
			if(($tm1->getOverallPF($_REQUEST["year"]) / $tm1->getOverallGames($_REQUEST["year"])) < ($tm2->getOverallPF($_REQUEST["year"]) / $tm2->getOverallGames($_REQUEST["year"])))
				return 1;
			if(($tm1->getDivisionPF($_REQUEST["year"]) / $tm1->getDivisionGames($_REQUEST["year"])) > ($tm2->getDivisionPF($_REQUEST["year"]) / $tm2->getDivisionGames($_REQUEST["year"])))
				return -1;
			if(($tm1->getDivisionPF($_REQUEST["year"]) / $tm1->getDivisionGames($_REQUEST["year"])) < ($tm2->getDivisionPF($_REQUEST["year"]) / $tm2->getDivisionGames($_REQUEST["year"])))
				return 1;
	return 0; // random tiebreaker
}

/**
 * Ranks {@link OFFL_FFLTeam FFL teams} by team name.  For use with {@link usort() usort()}, et al, only.
 *
 * This function will be renamed for clarity
 */
function fflteam_name_rank($tm1, $tm2)
{	return strcasecmp($tm1->getFFLTeamFullName(), $tm2->getFFLTeamFullName());	}
?>
Return current item: Online Fantasy Football League