Location: PHPKode > projects > Online Fantasy Football League > offl-0.2.6/www/upgrade/upgrade-016.php
<?php
/**
 * Upgrade script for OFFL v0.1.6 (only)
 *
 * Long description
 *
 * @author Stephen Rochelle <hide@address.com>
 * @version OFFL v0.2
 * @copyright Copyright (c) 2004 Stephen Rochelle.  Some rights reserved.
 * @package offl-ui
 */

$pageTitle = "";
require_once("offlconfig.php");
require_once($DOC_ROOT . "/lib/classes/offl_dbobject.php"); // gets database object for upgrading the DB.
require_once($DOC_ROOT . "/lib/classes/offl_league.php");
require_once($DOC_ROOT . "/lib/classes/offl_fflteam.php");
require_once($DOC_ROOT . "/lib/classes/offl_rosterplayer.php");
require_once($DOC_ROOT . "/lib/classes/offl_playerstat.php");
require_once($DOC_ROOT . "/lib/classes/offl_pastroster.php");

$fix = new OFFL_DBObject();

// UPGRADE TO OFFL 0.1.7

$fix->SQLQuery("ALTER TABLE `pastrosters` CHANGE `player_id_array` `player_id_array` VARCHAR( 100 ) DEFAULT NULL");
// The following line should be commented out if you've already added the `league_id` field
$fix->SQLQuery("ALTER TABLE `pastrosters` ADD `league_id` SMALLINT AFTER `fflteam_id`");
$fix->SQLQuery("ALTER TABLE `pastrosters` ADD `nflteam_id_array` VARCHAR( 100 ) AFTER `player_id_array`");
$fix->SQLQuery("UPDATE `control` SET `control_value`='2004-09-07 12:00:00' WHERE `control_key`='MISC_SEASON_START_2004'");

// UNCOMMENT the following line if your future games show scores already.  Be sure to change the week number (at the end of the string) to match the current week (that is, make sure it affects future games only).
//$fix->SQLQuery("UPDATE `games` SET v_fflteam_score=0, h_fflteam_score=0 WHERE week > 1");

// UPGRADE TO OFFL 0.2

// create the new database table
$fix->SQLQuery("DROP TABLE IF EXISTS `rosterplayers`");
$fix->SQLQuery("CREATE TABLE `rosterplayers` (`league_id` smallint unsigned not null, `player_id` smallint unsigned not null, `starter` bit default 0, `fflteam_id` smallint unsigned default 0, primary key (`player_id`, `league_id`))");
echo "<p>Database table `rosterplayers` created.</p>\n";

// now transfer the data (parse by fflteam, then insert data based on rosters)
$lg = new OFFL_League();
$teams = $lg->getAllFFLTeams(TRUE);
foreach($teams as $team)
{
	$fix->SQLQuery("SELECT player_ids, starters FROM fflteams WHERE fflteam_id=" . $team->getFFLTeamID());
	$pid = array_diff(explode(",",$fix->SQLResult(0, "player_ids")), array(""));
	$sts = array_diff(explode(",",$fix->SQLResult(0, "starters")), array(""));
	for($i = 0; $i < sizeof($pid); $i++)
	{
		$RP = new OFFL_RosterPlayer();
		$RP->setPlayerID($pid[$i]);
		$RP->setStarter($sts[$i]);
		$RP->setLeagueID($team->getLeagueID());
		$RP->setFFLTeamID($team->getFFLTeamID());
		$RP->save();
	}
}
echo "<p>Roster data migrated to `rosterplayers` successfully.</p>\n";

// now add nflteam data to pastrosters

$fix->SQLQuery("SELECT year, week, fflteam_id FROM `pastrosters`");
$num_rows = $fix->SQLNumRows();
for ($i = 0; $i < $num_rows; $i++)
{
	$PR = new OFFL_PastRoster($fix->SQLResult($i, "year"), $fix->SQLResult($i, "week"), $fix->SQLResult($i, "fflteam_id"));
	$player_ids = $PR->getPlayerIDArray();
	$nflteam_ids = array();
	foreach($player_ids as $player_id)
	{
		$player = new OFFL_Player($player_id);
		$nflteam_ids[] = $player->getNFLTeamID();
	}
	$PR->setNFLTeamIDArray($nflteam_ids);
	$PR->save();
}
$fix->SQLFreeResult();
echo "<p>`pastrosters` data upgrade complete.</p>\n";

// add the new field to `playerstats`
$fix->SQLQuery("ALTER TABLE `playerstats` ADD `nflteam_id` TINYINT UNSIGNED AFTER `week`");
echo "<p>`playerstats` table updated successfully.</p>\n";
// now add nflteam data to playerstats

$fix->SQLQuery("SELECT stat_id FROM `playerstats`");
$num_rows = $fix->SQLNumRows();
for ($i = 0; $i < $num_rows; $i++)
{
	$PS = new OFFL_PlayerStat($fix->SQLResult($i, "stat_id"));
	$player = new OFFL_Player($PS->getPlayerID());
	if (is_null($player->getNFLTeamID()))
	{	continue;	}
	$PS->setNFLTeamID($player->getNFLTeamID());
	$PS->save();
}
$fix->SQLFreeResult();
echo "<p>`playerstats` data upgrade complete.</p>\n";


echo "<p>Upgrade to OFFL v";
include($DOC_ROOT . "/lib/VERSION");
echo " successful.</p>\n";

exit();

?>
Return current item: Online Fantasy Football League