Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/lib/classes/offl_draft.php
<?php
/**
 * Defines the {@link OFFL_Draft} class.
 *
 * @package offl
 * @author Stephen Rochelle <hide@address.com>
 */

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

require_once($DOC_ROOT . "/lib/functions.php");
require_once($DOC_ROOT . "/lib/classes/offl_dbobject.php");
require_once($DOC_ROOT . "/lib/classes/offl_draftpick.php");
/**
 * Defines interfaces for info related to drafts.
 *
 * I don't really like the present interface much, it bears little resemblance to much of the system.
 * There's no save() feature, as OFFL_Draft is really a collection of means for manipulating related sets of
 * {@link OFFL_DraftPick} objects.
 *
 * @package offl
 */
class OFFL_Draft extends OFFL_DBObject
{
	/**
	 * @var integer
	 */
	var $_league_id = NULL;
	/**
	 * @var integer
	 */
	var $_year = NULL;
	/**
	 * @var integer
	 */
	var $_round = NULL;
	/**
	 * @var array Array of {@link OFFL_DraftPick} objects
	 */
	var $_picks = array();

	/**
	 * Constructor
	 *
	 * The first three parameters should be used / skipped together.  Final parameter is optional after that.
	 *
	 * @param integer $league_id Optional: If set with others, loads given draft from database
	 * @param integer $year Optional: If set with others, loads given draft from database
	 * @param integer $round Optional: If set with others, loads given draft from database
	 * @param boolean $all Optional: Retrieves picks from all rounds (instead of only $round round) if set TRUE
	 */
	function OFFL_Draft ($league_id = NULL, $year = NULL, $round = NULL, $all = FALSE)
	{
		OFFL_DBObject::OFFL_DBObject();

		$this->_league_id = $league_id;
		$this->_year = $year;
		$this->_round = $round;
		if ($all)
		{	$this->populate_all();	}
		else
		{	$this->populate();	}
	}
	
	/**
	 * Yanks info from database for a single round
	 * 
	 * @return boolean Returns TRUE on success, FALSE otherwise
	 */
	function populate ()
	{
		if(is_null($this->_year) || is_null($this->_league_id) || is_null($this->_round))
		{
			$this->_emsg = "Draft parameters are not set.  Cannot populate OFFL_Draft object.";
			error_log ($this->_emsg);
			return FALSE;
		}
		// Left outer join glory! w00t!
		$sql = "SELECT * FROM drafts " .
		"WHERE year=" . $this->_year .
		" AND league_id=" . $this->_league_id .
		" AND round_no=" . $this->_round .
		" ORDER BY pick_no";

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

		if(mysql_num_rows($result) == 0)
		{
			$this->_emsg = "No draft records found for draft year " . $this->_year . " and league " . $this->_league_id . ".";
			error_log ($this->_emsg);
			return FALSE;
		}
		// Pass mysql_result rows into OFFL_DraftPick object to populate
		while($row = mysql_fetch_assoc($result))
		{
			$tmpdp = new OFFL_DraftPick();
			$tmpdp->populateFromResult($row);
			$this->_picks[] = $tmpdp;
		}
		mysql_free_result($result);
		return TRUE;
	}

	/**
	 * Yanks info from database for all rounds of a given draft year/league
	 * 
	 * @return boolean Returns TRUE on success, FALSE otherwise
	 */
	function populate_all ()
	{
		if(is_null($this->_year) || is_null($this->_league_id))
		{
			$this->_emsg = "Draft parameters are not set.  Cannot populate OFFL_Draft object.";
			error_log ($this->_emsg);
			return FALSE;
		}
		// Left outer join glory! w00t!
		$sql = "SELECT * FROM drafts " .
		"WHERE year=" . $this->_year . 
		" AND league_id=" . $this->_league_id .
		" ORDER BY round_no, pick_no";

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

		if(mysql_num_rows($result) == 0)
		{
			$this->_emsg = "No draft records found for draft year $this->_year.";
			error_log ($this->_emsg);
			return FALSE;
		}
		// Pass mysql_result rows into DraftPick object to populate
		while($row = mysql_fetch_assoc($result))
		{
			$tmpdp = new OFFL_DraftPick();
			$tmpdp->populateFromResult($row);
			$this->_picks[] = $tmpdp;
		}
		mysql_free_result($result);
		return TRUE;
	}

	/**
	 * @return integer
	 */
	function getYear()
	{
		return $this->_year;
	}

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

	/**
	 * @param integer $league_id Optional: If set, returns max draft year for given league (otherwise max draft year from any league)
	 * @return integer
	 */
	function getMaxDraftYear($league_id = NULL)
	{
		$sql = "SELECT MAX(year) FROM drafts";
		if (!is_null($league_id))
		{	$sql .= " WHERE league_id=" . $league_id;	}
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
		$maxyear = mysql_result($result,0,0);
		mysql_free_result($result);

		if($maxyear < 1900)
			$maxyear = getThisYear() - 1;

		return $maxyear;
	}

	/**
	 * @param integer $league_id Optional: If set, returns draft years for given league (otherwise draft years from all league)
	 * @return array Array of integers (years with drafts)
	 */
	function getDraftYears($league_id = NULL)
	{
		$retArr = array();
		$sql = "SELECT DISTINCT year FROM drafts";
		if (!is_null($league_id))
		{	$sql .= " WHERE league_id=" . $league_id;	}
		$sql .= " ORDER BY year";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for($i=0; $i<mysql_num_rows($result); $i++)
		{	array_push($retArr, mysql_result($result,$i,"year"));	}

		mysql_free_result($result);

		return $retArr;
	}

	/**
	 * @return array Array of integers (draft rounds for this draft's year / league).  Returns NULL if those parameters not set.
	 */
	function getDraftRounds()
	{
		if (is_null($this->_year) || is_null($this->_league_id))
		{	return NULL;	}
		$retArr = array();
		$sql = "SELECT DISTINCT round_no FROM drafts WHERE year=" . $this->_year . " AND league_id=" . $this->_league_id . " ORDER BY round_no";
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for($i=0; $i<mysql_num_rows($result); $i++)
		{	array_push($retArr, mysql_result($result,$i,"round_no"));	}

		mysql_free_result($result);

		return $retArr;
	}

	/**
	 * @return array Array of {@link OFFL_DraftPick} objects as populated (either single round or all rounds) for this OFFL_Draft object
	 */
	function getPicks()
	{
		return $this->_picks;
	}

	/**
	 * Creates {@link OFFL_DraftPick} objects conforming to various parameters.
	 *
	 * Regarding $type:
	 * <ul>
	 * <li><b>straight</b>: Draft order is identical in each round.</li>
	 * <li><b>serpentine</b>: Draft order is flipped each round, i.e. A->B->C->D->D->C->B->A....</li>
	 * <li><b>random</b>: Draft order is randomly determined each round.</li>
	 * </ul>
	 *
	 * @param integer $league_id League to associate draft with
	 * @param integer $year Year to associate draft with
	 * @param integer $rounds Number of rounds in draft
	 * @param string $type May be "straight", "serpentine", or "random".
	 * @param string $order Order of teams.  Should be {@link OFFL_FFLTeam::getFFLTeamID() fflteam_ids} separated with "|".
	 */
	function createDraft($league_id,$year,$rounds,$type,$order)
	{
		$teams = explode("|",$order);
		if($type == "straight")
		{
			for($i=0; $i<$rounds; $i++)
			{
				for($j=0; $j<sizeof($teams); $j++)
				{ 
					$p = new OFFL_DraftPick($league_id,$year,$i+1,$j+1);
					$p->setFFLTeamID($teams[$j]);
					$p->save("add");
				}
			}
		}
		elseif ($type == "serpentine")
		{
			for($i=0; $i<$rounds; $i++)
			{
				if ($i % 2) // odd round, regular direction
				{
					for($j=0; $j<sizeof($teams); $j++)
					{ 
						$p = new OFFL_DraftPick($league_id,$year,$i+1,$j+1);
						$p->setFFLTeamID($teams[$j]);
						$p->save("add");
					}
				}
				else // even round, reverse direction
				{
					for($j=0; $j<sizeof($teams); $j++)
					{ 
						$p = new OFFL_DraftPick($league_id,$year,$i+1,$j+1);
						$p->setFFLTeamID($teams[sizeof($teams) - $j - 1]);
						$p->save("add");
					}
				}
			}
		}
		else // $type == "random"
		{
			for($i = 0; $i<$rounds; $i++)
			{
				$tm_order = array();
				$tm_count = sizeof($teams);
				$tms_left = $tm_count;
				while ($tms_left > 0)
				{
					$tm = mt_rand(0, $tm_count-1);
					if(!in_array($teams[$tm], $tm_order))
					{
						$tm_order[] = $teams[$tm];
						$tms_left--;
					}
				}
				for($j=0; $j<sizeof($teams); $j++)
				{ 
							$p = new OFFL_DraftPick($league_id,$year,$i+1,$j+1);
					$p->setFFLTeamID($tm_order[$j]);
					$p->save("add");
				}
				unset($tm_order);
			}
		}
	}

}
?>
Return current item: Online Fantasy Football League