Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/lib/classes/offl_waiver.php
<?php
/**
 * Defines the {@link OFFL_Waiver} 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/classes/offl_dbobject.php");
require_once($DOC_ROOT . "/lib/classes/offl_transaction.php");
require_once($DOC_ROOT . "/lib/classes/offl_player.php");
require_once($DOC_ROOT . "/lib/classes/offl_fflteam.php");
/**
 * Defines interfaces for waiver claims and priorities.
 *
 * Waiver deadline must be moved around.
 *
 * @see OFFL_WaiverPlayer
 * @package offl
 */
class OFFL_Waiver extends OFFL_DBObject
{
	/**
	 * @var integer
	 */
	var $_waiver_id = NULL;
	/**
	 * @var integer
	 */
	var $_league_id = NULL;
	/**
	 * @var integer
	 */
	var $_player_id = NULL;
	/**
	 * @var integer
	 */
	var $_fflteam_id = NULL;
	/**
	 * @var integer
	 */
	var $_dropplayer_id = NULL;
	/**
	 * @var integer
	 */
	var $_priority = NULL; // team's importance ranking
	/**
	 * @var integer
	 */
	var $_team_priority = NULL; // league resolution ranking
	/**
	 * @var string SQL DATETIME format, GMT
	 */
	var $_waiver_deadline = NULL;
	/**
	 * @var integer Not currently used anywhere
	 */
	var $_waiver_time_remaining = NULL;

	/**
	 * Constructor
	 *
	 * @param integer $waiver_id Optional: If set, loads waiver from database
	 * @see populate()
	 */
	function OFFL_Waiver ($waiver_id = NULL)
	{
		OFFL_DBObject::OFFL_DBObject();

	    if(isset($waiver_id))
		{
			$this->_waiver_id = $waiver_id;
			$this->populate();
		}
	}

	/**
	 * Pulls info from database.  {@link $_waiver_id} must be set.
	 *
	 * Tentative SQL format for new waiver claim time class.
	 * @return boolean TRUE if successful, FALSE otherwise.
	 */
	function populate ()
	{
		if(is_null($this->_waiver_id))
		{
			$this->_emsg = "\$_waiver_id is not set.  Cannot populate OFFL_Waiver object.";
			error_log ($this->_emsg);
			return FALSE;
		}
		$sql = "SELECT w.*, wp.waiver_deadline AS waiver_deadline, t.waiver_priority AS team_priority FROM `waivers` w " .
			"LEFT JOIN waiverplayers wp " .
			"ON w.player_id=wp.player_id AND w.league_id=wp.league_id " .
			"LEFT JOIN fflteams t " .
			"ON w.fflteam_id=t.fflteam_id " .
			"WHERE w.waiver_id=" . $this->_waiver_id;
	    $result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		if(mysql_num_rows($result) == 0)
		{
			$this->_emsg = "No waiver records found for waiver_id " . $this->_waiver_id . ".";
	        error_log ($this->_emsg);
			return FALSE;
		}
		$this->_league_id = mysql_result($result,0,"league_id");
        $this->_player_id = mysql_result($result,0,"player_id");
        $this->_fflteam_id = mysql_result($result,0,"fflteam_id");
        $this->_dropplayer_id = mysql_result($result,0,"dropplayer_id");
        $this->_priority = mysql_result($result,0,"priority");
		$this->_team_priority = mysql_result($result,0,"team_priority");
        $this->_waiver_deadline = mysql_result($result,0,"waiver_deadline");
		mysql_free_result($result);
		return TRUE;
	}

	/**
	 * Saves the transaction item to the database
	 *
	 * UPDATE vs INSERT is determined automagically
	 */
	function save ()
	{
		if(is_null($this->_player_id))
		{	$this->_player_id = "NULL";	}
		if(is_null($this->_dropplayer_id))
		{	$this->_dropplayer_id = "NULL";	}

		if (is_null($this->_waiver_id)) // insert a new waiver
		{
			$sql = "INSERT INTO waivers (league_id, player_id, fflteam_id, dropplayer_id, priority) VALUES (" . $this->_league_id . "," . $this->_player_id . ", " . $this->_fflteam_id . ", " . $this->_dropplayer_id . ", " . $this->_priority . ")";
		}
		else // update an existing waiver
		{
			$sql = "UPDATE waivers SET league_id=" . $this->_league_id . ", player_id=" . $this->_player_id . ", fflteam_id=" . $this->_fflteam_id . ", dropplayer_id=" . $this->_dropplayer_id . ", priority=" . $this->_priority . " WHERE waiver_id=" . $this->_waiver_id;
		}
	    $result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
	}

	/**
	 * @return integer
	 */
	function getWaiverID()
	{
		return $this->_waiver_id;
	}

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

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

	/**
	 * @param integer $player_id
	 */
	function setPlayerID($player_id)
	{
		$this->_player_id = $player_id;
	}

	/**
	 * @return integer
	 */
	function getPlayerID()
	{
		return $this->_player_id;
	}

	/**
	 * @param integer $fflteam_id
	 */
	function setFFLTeamID($fflteam_id)
	{
		$this->_fflteam_id = $fflteam_id;
	}

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

	/**
	 * @param integer $dropplayer_id
	 */
	function setDropPlayerID($dropplayer_id)
	{
		$this->_dropplayer_id = $dropplayer_id;
	}

	/**
	 * @return integer
	 */
	function getDropPlayerID()
	{
		return $this->_dropplayer_id;
	}

	/**
	 * Sets the team-specific priority for waiver resolution
	 * @param integer $priority Optional: if set, forces priority.  Default places this waiver claim at the end of the team priority queue.
	 * @return boolean TRUE on success, FALSE otherwise
	 */
	function setPriority($priority=NULL)
	{
		if (isset($priority))
		{
			$this->_priority = $priority;
			return TRUE;
		}
		if (is_null($this->_fflteam_id))
		{
			$this->_emsg = "Unable to set priority: fflteam_id not defined.";
			error_log ($this->_emsg);
			return FALSE;
		}
		$x = new OFFL_Waiver();
		$teamWaivers = $x->getAllTeamWaivers($this->_fflteam_id);

		if (sizeof($teamWaivers) == 0)
		{ // only waiver, priority 1
			$this->_priority = 1;
		}
		else
		{ // 1 higher than highest priority number; that is, lowest priority
			$temp = end($teamWaivers);
			$this->_priority = $temp->getPriority() + 1;
		}
		return TRUE;
	}

	/**
	 * @return integer
	 */
	function getPriority()
	{
		return $this->_priority;
	}

	/**
	 * @return integer
	 */
	function getTeamPriority()
	{
		return $this->_team_priority;
	}

	/**
	 * Returns output from {@link gmmktime() gmmktime} corresponding to the SQL DATETIME format
	 * @return integer Timestamp, GMT
	 */
	function getWaiverDeadline()
	{
		if (is_null($this->_waiver_deadline))
		{	return $this->_waiver_deadline;	}
		// assemble DATETIME format into UNIX timestamp
		list($date, $time) = explode(" ", $this->_waiver_deadline);
		list($Y, $M, $D) = explode("-", $date);
		list($h, $i, $s) = explode(":", $time);
		$time = gmmktime($h, $i, $s, $M, $D, $Y);
		return $time;
	}

	/**
	 * Looks up all waiver claims, optionally for a single league
	 * @param integer $league_id Optional: if set, returns claims only for this league
	 * @return array Array of {@link OFFL_Waiver} objects
	 */
	function getAllWaivers($league_id = NULL)
	{
		$retArr = array();
		$sql = "SELECT waiver_id FROM waivers";
		if (isset($league_id))
		{	$sql .= " WHERE league_id=" . $league_id;	}
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for($i=0; $i<mysql_num_rows($result); $i++)
		{
			$w = new OFFL_Waiver(mysql_result($result,$i,"waiver_id"));
			array_push($retArr, $w);
		}
		mysql_free_result($result);

		usort($retArr, "waiver_sort");
		return $retArr;
	}

	/**
	 * Looks up all waiver claims for a given team
	 * @param integer $fflteam_id Returns claims for this ffl team
	 * @return array Array of {@link OFFL_Waiver} objects
	 */
	function getAllTeamWaivers($fflteam_id)
	{
		$retArr = array();
		$sql = "select waiver_id from waivers where fflteam_id=" . $fflteam_id;
		$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");

		for($i=0; $i<mysql_num_rows($result); $i++)
		{
			$w = new OFFL_Waiver(mysql_result($result,$i,"waiver_id"));
			array_push($retArr, $w);
		}
		mysql_free_result($result);

		usort($retArr, "waiver_sort");
		return $retArr;
	}

	/**
	 * Deletes this waiver claim and adjusts team-specific priorities
	 *
	 * This function will adjust other claims for this team such that their priorities are a continuous 1..n range.
	 * It will not adjust the interteam priorities (that is, which team resolves its #1 claim first).
	 * @return boolean TRUE on success, FALSE otherwise
	 */
	function deleteWaiver()
	{
		if(isset($this->_waiver_id))
		{
			// Should add priority management to this?
			// advance the priority of all other waiver claims by this team
			// alter team priorities
			// But: it might be better served in the resolve_waiver area.
			// I think single-team priority management is appropriate here (always happens when waivers deleted)

			// so:  here's management of intra-team priorities.
			$x = new OFFL_Waiver();
			$thisTeamWaivers = $x->getAllTeamWaivers($this->_fflteam_id);
			foreach ($thisTeamWaivers as $waiver)
			{
				// only bump the ones behind this, leave higher priorities alone
				// (bigger number == lower priority)
				if ($waiver->getPriority() > $this->getPriority)
				{
					$waiver->setPriority($waiver->getPriority() - 1);
					$waiver->save();
				}
			}

			$sql = "DELETE FROM waivers WHERE waiver_id=" . $this->_waiver_id;
			$result = mysql_query($sql,$this->_conn) or die (mysql_error() . ": $sql");
			return TRUE;
		}
		$this->_emsg = "Unable to delete waiver: waiver_id not defined.";
        error_log ($this->_emsg);
		return FALSE;
	}

	/**
	 * Alters the interteam claim priorities of the teams present in the league associated with the provided fflteam_id
	 *
	 * The given team is placed at lowest priority in its league, and all teams with previously lower priorities
	 * in the league are advanced one priority level each.  Used when waiver claims are successfully resolved.
	 * @param integer $fflteam_id Team to be set at back of interteam priority queue
	 * @return boolean TRUE on success, FALSE otherwise
	 */
	function resetTeamPriority($fflteam_id)
	/*	This function moves FFL Team $fflteam_id to the back of the waiver queue
		and advances all teams behind it by one spot.
	*/
	{
		$thisTeam = new OFFL_FFLTeam($fflteam_id);
		$thisLeague = new OFFL_League($thisTeam->getLeagueID());
		$allTeams = $thisLeague->getAllFFLTeams();
		$highPriority = $thisTeam->getWaiverPriority();
		foreach ($allTeams as $team)
		{
			if ($team->getWaiverPriority() > $thisTeam->getWaiverPriority())
			{
				if ($team->getWaiverPriority() > $highPriority)
				{
					$highPriority = $team->getWaiverPriority();
				}
				$team->setWaiverPriority($team->getWaiverPriority() - 1);
				$team->save();
			}
		}
		$thisTeam->setWaiverPriority($highPriority);
		$thisTeam->save();
	}

} // end OFFL_Waiver class

/**
 * Sorts {@link OFFL_Waiver} objects for use with {@link usort() usort()}, et al.
 *
 * Waiver claims (presumably for a single league) are sorted to fit the preferred resolution order.
 * Inter-team priorities are first taken, then intra-team.  That is, if team 1 pick 1 is invalid, 
 * team 1 pick 2 should execute before team 2 pick 1.
 *
 * Resorts must be done after every successful claim resolution.
 */
function waiver_sort ($w1, $w2)
/*	custom sort function for waivers (since relevant sorts aren't actually in waiver table)
	order is team 1 pick 1, team 1 pick 2, team 2 pick 1, team 2 pick 2
	THIS IS NOT THE REGULAR RESOLUTION ORDER
	However, it is used because, if T1P1 is not due, T1P2 should resolve before T2P1
	Waivers must be re-sorted after each resolution.
*/
// N.B. THIS MUST BE OUTSIDE OF THE CLASS DEFINITION OR IT WON'T WORK (MEMBER FUNCTION)
{

	if ($w1->getTeamPriority() < $w2->getTeamPriority())
		return -1;
	elseif ($w1->getTeamPriority() > $w2->getTeamPriority())
		return 1;
	elseif ($w1->getPriority() < $w2->getPriority())
		return -1;
	elseif ($w1->getPriority() > $w2->getPriority())
		return 1;

	return 0;
}

?>
Return current item: Online Fantasy Football League