<?
/* 
   This file is part of the 'Science Fair In A Box' project
   SFIAB Website: http://www.sfiab.ca

   Copyright (C) 2005 Sci-Tech Ontario Inc <info@scitechontario.org>
   Copyright (C) 2005 James Grant <james@lightbox.org>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation, version 2.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/
?>
<?
 require("../common.inc.php");
 require_once("../user.inc.php");
 user_auth_required('committee', 'admin');
 include "judges.inc.php";

	if(get_value_from_array($_GET,'edit')) $edit=get_value_from_array($_GET,'edit');
	if(get_value_from_array($_POST,'edit')) $edit=get_value_from_array($_POST,'edit');
	if(get_value_from_array($_GET,'action')) $action=get_value_from_array($_GET,'action');
	if(get_value_from_array($_POST,'action')) $action=get_value_from_array($_POST,'action');

	if($action == "delete" && get_value_from_array($_GET, 'delete'))
	{
		//ALSO DELETE: team members, timeslots, projects, awards
		
		$stmt = $pdo->prepare("DELETE FROM judges_teams_link WHERE judges_teams_id='".$_GET['delete']."' AND year='".$config['FAIRYEAR']."'");
		$stmt->execute();
		$stmt = $pdo->prepare("DELETE FROM judges_teams_timeslots_link WHERE judges_teams_id='".$_GET['delete']."' AND year='".$config['FAIRYEAR']."'");
		$stmt->execute();
		$stmt = $pdo->prepare("DELETE FROM judges_teams_timeslots_projects_link WHERE judges_teams_id='".$_GET['delete']."' AND year='".$config['FAIRYEAR']."'");
		$stmt->execute();
		$stmt = $pdo->prepare("DELETE FROM judges_teams_awards_link WHERE judges_teams_id='".$_GET['delete']."' AND year='".$config['FAIRYEAR']."'");
		$stmt->execute();
		$stmt = $pdo->prepare("DELETE FROM judges_teams WHERE id='".$_GET['delete']."' AND year='".$config['FAIRYEAR']."'");
		$stmt->execute();message_push(happy(i18n("Judge team successfully removed, and all of its corresponding members, timeslots, projects and awards unlinked from team")));
	}

	if($action=="deletealldivisional")
	{
		$q2=$pdo->prepare("SELECT *
				FROM 	
					judges_teams
				WHERE 
					year='".$config['FAIRYEAR']."'
					AND autocreate_type_id='1'
					");
					echo $pdo->errorInfo();
		$numdeleted=0;
		while($r2=$q2->fetch(PDO::FETCH_OBJ))
		{
			//okay now we can start deleting things! whew!
			//first delete any linkings to the team
			
			$stmt = $pdo->prepare("DELETE FROM judges_teams_link WHERE judges_teams_id='$r2->id' AND year='".$config['FAIRYEAR']."'");
			$stmt->execute();
			$stmt = $pdo->prepare("DELETE FROM judges_teams_timeslots_link WHERE judges_teams_id='$r2->id' AND year='".$config['FAIRYEAR']."'");
			$stmt->execute();
			$stmt = $pdo->prepare("DELETE FROM judges_teams_timeslots_projects_link WHERE judges_teams_id='$r2->id' AND year='".$config['FAIRYEAR']."'");
			$stmt->execute();
			$stmt = $pdo->prepare("DELETE FROM judges_teams_awards_link WHERE judges_teams_id='$r2->id' AND year='".$config['FAIRYEAR']."'");
			$stmt->execute();
			$stmt = $pdo->prepare("DELETE FROM judges_teams WHERE id='$r2->id' AND year='".$config['FAIRYEAR']."'");
			$stmt->execute();$numdeleted++;
		}
		if($numdeleted)
			message_push(happy(i18n("Successfully deleted %1 auto-created divisional team(s)",array($numdeleted))));
		else
			message_push(error(i18n("There were no auto-created divisional teams to delete")));
	}
	
	if($action=="deleteall")
	{
		$q2=$pdo->prepare("SELECT *
				FROM 	judges_teams
				WHERE 
					year='".$config['FAIRYEAR']."'
					");
		$q2->execute();
		$numdeleted=0;
		while($r2=$Q2->FETCH(PDO::FETCH_OBJ))
		{
			//okay now we can start deleting things! whew!
			//first delete any linkings to the team
			
			$stmt = $pdo->prepare("DELETE FROM judges_teams_link WHERE judges_teams_id='$r2->id' AND year='".$config['FAIRYEAR']."'");
			$stmt->execute();
			$stmt = $pdo->prepare("DELETE FROM judges_teams_timeslots_link WHERE judges_teams_id='$r2->id' AND year='".$config['FAIRYEAR']."'");
			$stmt->execute();
			$stmt = $pdo->prepare("DELETE FROM judges_teams_timeslots_projects_link WHERE judges_teams_id='$r2->id' AND year='".$config['FAIRYEAR']."'");
			$stmt->execute();
			$stmt = $pdo->prepare("DELETE FROM judges_teams_awards_link WHERE judges_teams_id='$r2->id' AND year='".$config['FAIRYEAR']."'");
			$stmt->execute();
			$stmt = $pdo->prepare("DELETE FROM judges_teams WHERE id='$r2->id' AND year='".$config['FAIRYEAR']."'");
			$stmt->execute();$numdeleted++;
		}
		if($numdeleted)
			message_push(happy(i18n("Successfully deleted %1 team(s)",array($numdeleted))));
		else
			message_push(error(i18n("There were no teams to delete")));
	}

	if(($action=="save" || $action=="assign") && $edit)
	{
		//if we're updating or assigning, it doesnt matter, lets do the same thing (save record, add award
		//but when we're done, if we're "assign" then go back to edit that team
		//if we're save, then go back to the team list
		$err=false;
		$q=$pdo->prepare("UPDATE judges_teams SET num='".$_POST['team_num']."', name='".(stripslashes($_POST['team_name']))."' WHERE id='$edit'");
		$q->execute();
		if($pdo->errorInfo())
		{
			$err=true;
			message_push(error($pdo->errorInfo()));
		}

		if($_POST['award'])
		{
			//we can only have 1 special award assigned to any given team so we'll be able to properly
			//manage the projects that we assign to the team.  If there was more than one special award
			//the judges wouldnt know which projects to judge for which award.  This doesnt apply for divisions
			//because the category/division is obvious based on project numbesr.  A divisional judge team could easily
			//be assigned to do all of Comp Sci - Junior, Intermediate and Senior without any problems.
			$q=$pdo->prepare("SELECT award_types.type FROM award_awards, award_types WHERE award_awards.award_types_id=award_types.id AND award_awards.id='".$_POST['award']."'");
			$q->execute();
			$aw=$q->fetch(PDO::FETCHH_OBJ);

			$addaward=true;
			if($aw->type=="Special")
			{
				$q=$pdo->prepare("SELECT COUNT(*) AS num FROM 
							judges_teams_awards_link,
							award_awards,
							award_types
						WHERE
							judges_teams_awards_link.judges_teams_id='$edit'
							AND judges_teams_awards_link.award_awards_id=award_awards.id
							AND award_awards.award_types_id=award_types.id
							AND award_types.type='Special'
						");
				$q->exxecute();
				$r=$q->fetch(PDO::FETCHH_OBJ);
				echo "special awards: $r->num";
				if($r->num)
				{
					$addaward=false;
					message_push(error(i18n("Sorry, only one Special Award can be assigned to a judging team")));
				}
				else
				{
					$addaward=true;
				}
			}
			
			if($addaward)
			{
				//link up the award
				$stmt = $pdo->prepare("INSERT INTO judges_teams_awards_link (award_awards_id,judges_teams_id,year) VALUES ('".$_POST['award']."','$edit','".$config['FAIRYEAR']."')");
				$stmt->execute();
				message_push(happy(i18n("Award assigned to team")));
			}
		}


		if($action=="assign")
			$action="edit";
		else if($action=="save")
		{
			if($err)
				$action="edit";
			else
			{
				message_push(happy(i18n("Team successfully saved")));
				unset($action);
				unset($edit);
			}
		}
	}

	if($action=="unassign")
	{
		$stmt = $pdo->prepare("DELETE FROM judges_teams_awards_link WHERE judges_teams_id='$edit' AND award_awards_id='".$_GET['unassign']."' AND year='".$config['FAIRYEAR']."'");
		$stmt->execute();
		message_push(happy(i18n("Award unassigned from judge team")));
		//keep editing the same team
		$action="edit";
	}

	if($action=="createall")
	{
		//first make sure we dont have any non-divisional award teams (dont want people hitting refresh and adding all the teams twice
		$q=$pdo->prepare("SELECT COUNT(*) AS c FROM judges_teams WHERE autocreate_type_id!='1' AND year='".$config['FAIRYEAR']."'");
		$q->execute();
		$r=$q->fetch(PDO::FETCHH_OBJ);
		if($r->c)
		{
			message_push(error(i18n("Cannot 'Create All' teams when any divisional teams currently exist.  Try deleting all existing non-divisional teams first.")));
		}
		else
		{
			//grab all the awards
			$q=$pdo->prepare("SELECT 
						award_awards.*, 
						award_types.type AS award_type, 
						award_types.order AS award_types_order 
					FROM 	
						award_awards,
						award_types 
					WHERE 	
						award_awards.award_types_id=award_types.id 
						AND award_awards.year='".$config['FAIRYEAR']."' 
						AND award_types.year='".$config['FAIRYEAR']."' 
						AND award_types_id!='1'
					ORDER BY 
						award_types_order, 
						award_awards.order,
						name");
			$q->execute();

			//startat
			$q2=$pdo->prepare("SELECT MAX(num) AS lastnum FROM judges_teams WHERE year='{$config['FAIRYEAR']}'");
			$q2->execute();
			$r2=$q2->fetch(PDO::FETCH_OBJ);
			if($r2->lastnum)
				$num=$r2->lastnum+1;
			else
				$num=1;

			while($r=$q->fetch(PDO::FETCHH_OBJ)) {
//				print_r($r);
				$name="($r->award_type) $r->name";
				$stmt = $pdo->prepare("INSERT INTO judges_teams(num,name,autocreate_type_id,year) VALUES ('$num','$name','$r->award_types_id','".$config['FAIRYEAR']."')");
				$stmt->execute();
				echo $pdo->errorInfo();
				$team_id=$pdo->lastInsertId();
				if($team_id) {
					//now link the new team to the award
					$stmt = $pdo->prepare("INSERT INTO judges_teams_awards_link (award_awards_id,judges_teams_id,year) VALUES ('$r->id','$team_id','".$config['FAIRYEAR']."')");
					$stmt->execute();
					message_push(happy(i18n("Created team #%1: %2",array($num,$name))));
				}
				else {
					message_push(error(i18n("Error creating team #%1: %2",array($num,$name))));
				}
				$num++;
			}
		}
	}

	if($action=="add" && $_GET['num'])
	{
		$stmt = $pdo->prepare("INSERT INTO judges_teams(num,year) VALUES ('".$_GET['num']."','".$config['FAIRYEAR']."')");
		$stmt->execute();
		echo $pdo->errorInfo();
		$edit=$pdo->lastInsertId();
		$action="edit";
	}

	if($action=="edit" && $edit)
	{
		send_header("Edit Judging Team",
 			array('Committee Main' => 'committee_main.php',
				'Administration' => 'admin/index.php',
				'Judges' => 'admin/judges.php',
				'Manage Judging Teams' => 'admin/judges_teams.php'));
?>
<script language="javascript" type="text/javascript">
function addclicked()
{
	document.forms.judges.action.value="assign";
	document.forms.judges.submit();
}


</script>

<?
	
		echo "<br />";
		$team=getJudgingTeam($edit);

		if(!$_SESSION['viewstate']['judges_teams_awards_show'])
			$_SESSION['viewstate']['judges_teams_awards_show']='unassigned';
		//now update the judges_teams_awards_show viewstate
		if(get_value_from_array($_GET, 'judges_teams_awards_show'))
			$_SESSION['viewstate']['judges_teams_awards_show']=$_GET['judges_teams_awards_show'];

		echo "<form name=\"judges\" method=\"post\" action=\"judges_teams.php\">";
		echo "<input type=\"hidden\" name=\"action\" value=\"save\">";
		echo "<input type=\"hidden\" name=\"edit\" value=\"$edit\">";

		echo "<table>";
		echo "<tr><td>".i18n("Team Number").":</td><td><input type=\"text\" size=\"4\" name=\"team_num\" value=\"".$team['num']."\"></td></tr>";
		echo "<tr><td>".i18n("Team Name").":</td><td><input type=\"text\" size=\"40\" name=\"team_name\" value=\"".$team['name']."\"></td></tr>";
		echo "<tr><td>".i18n("Awards").":</td><td>";

	
		if(count(get_value_from_array($team, 'awards')))
		{
			foreach($team['awards'] AS $award)
			{
				echo "<a onclick=\"return confirmClick('Are you sure you want to unassign this award from this team?')\" href=\"judges_teams.php?action=unassign&unassign=".$award['id']."&edit=".$team['id']."\"><img border=0 src=\"".$config['SFIABDIRECTORY']."/images/16/button_cancel.".$config['icon_extension']."\"></a>";
				echo " (".$award['award_type'].") ".$award['name']." <br />";
			}
		}

		echo "<table><tr>";
		if($_SESSION['viewstate']['judges_teams_awards_show']=='all')
		{
			echo "<td align=left><a href=\"judges_teams.php?action=edit&edit=$edit&judges_teams_awards_show=unassigned\">".i18n("show unassigned")."</a></td>";
			echo "<td align=right><b>".i18n("show all")."</b></td>";
		}
		else
		{
			echo "<td align=left><b>".i18n("show unassigned")."</b></td>";
			echo "<td align=right><a href=\"judges_teams.php?action=edit&edit=$edit&judges_teams_awards_show=all\">".i18n("show all")."</a></td>";

		}
		echo "</tr>";

		if($_SESSION['viewstate']['judges_teams_awards_show']=='all')
		{
			$querystr="SELECT 
					award_awards.id, 
					award_awards.name, 
					award_types.type AS award_type,
					award_types.order AS award_type_order
				FROM 
					award_awards, 
					award_types
				WHERE 
					award_awards.year='".$config['FAIRYEAR']."'
					AND award_types.id=award_awards.award_types_id
					AND award_types.year='{$config['FAIRYEAR']}'
				ORDER BY 
					award_type_order,
					name
				";
		}
		else
		{
			$querystr="SELECT 
					award_awards.id, 
					award_awards.name, 
					award_types.type AS award_type,
					award_types.order AS award_type_order
				FROM 
					(
					award_awards,
					award_types
					)
					LEFT JOIN judges_teams_awards_link ON award_awards.id = judges_teams_awards_link.award_awards_id
				WHERE 
					award_awards.year='".$config['FAIRYEAR']."' AND 
					judges_teams_awards_link.award_awards_id IS NULL
					AND award_types.id=award_awards.award_types_id
					AND award_types.year='{$config['FAIRYEAR']}'
				ORDER BY 
					award_type_order,
					name";
		}

		echo "<tr><td colspan=2>";
		$q=$pdo->prepare($querystr);
		$q->execute();
		
		show_pdo_errors_if_any($pdo);
		echo "<select name=\"award\">";
		echo "<option value=\"\">".i18n("Choose award to assign to team")."</option>\n";

		while($r=$q->fetch(PDO::FETCHH_OBJ))
		{
			echo "<option value=\"$r->id\">($r->award_type) $r->name</option>\n";
		}

		echo "</select>";
		echo "<input type=\"button\" value=\"Add\" onclick=\"addclicked()\">";
		echo "</td></tr>";
		echo "</table>";

		echo "</td></tr>";
		echo "</table>";
		echo "<input type=submit value=\"".i18n("Save Changes")."\">";
		echo "</form>";


	}
	else
	{
		send_header("Manage Judging Teams",
 			array('Committee Main' => 'committee_main.php',
				'Administration' => 'admin/index.php',
				'Judges' => 'admin/judges.php'));
		echo "<br />";

		$teams=getJudgingTeams();
		if(count($teams)) {
			//grab an array of all the current team numbers
			foreach($teams AS $team)
				$teamnumbers[$team['num']]=1;

			//start at 1, and find the next available team number
			$newteamnum=1;
			while($teamnumbers[$newteamnum]==1)
			{
				$newteamnum++;
			}
		}

		echo "<table width=\"95%\">";
		echo "<tr><td>";
		$q=$pdo->prepare("SELECT COUNT(*) AS c FROM judges_teams WHERE autocreate_type_id!='1' AND year='".$config['FAIRYEAR']."'");
		$q->execute();
		$r=$q->fetch(PDO::FETCH_OBJ);
		if(!$r->c) {
			echo "<a href=\"judges_teams.php?action=createall\">".i18n("Automatically create one new team for every non-divisional award")."</a><br />";
		}
		echo "<a href=\"judges_teams.php?action=add&num=$newteamnum\">".i18n("Manually add individual team")."</a><br />";
		echo "</td><td>";

		if(count($teams)) 
		{

			echo "<a onclick=\"return confirmClick('".i18n("Are you sure you want to delete all teams that are assigned to divisional awards?")."')\" href=\"judges_teams.php?action=deletealldivisional\">Delete all teams assigned to divisional awards</a>";
			echo "<br />";
			echo "<a onclick=\"return confirmClick('".i18n("Are you sure you want to delete all teams?")."')\" href=\"judges_teams.php?action=deleteall\">Delete all teams</a><br />";
			echo "</td></tr></table>";

			echo "<table class=\"summarytable\">\n";
			echo "<thead style=\"cursor:pointer\"><tr><th>Num</th>";
			echo "<th>Team Name</th>";
			echo "<th>Award(s)</th>";
			echo "<th>Actions</th>";
			echo "</tr></thead>";
			foreach($teams AS $team)
			{
				echo "<tr><td>#".$team['num']."</td><td>";
				echo $team['name'];
				echo "</td>";

				echo "<td>";
				if(count($team['awards']))
				{
					foreach($team['awards'] AS $award)
					{
						echo $award['name']." <br />";
					}
				}
				else
				{
					echo error(i18n("No award assigned to team"),"inline");
				}
				echo "</td>";

				echo " <td align=\"center\">";
				echo "<a href=\"judges_teams.php?action=edit&edit=".$team['id']."\"><img border=\"0\" src=\"".$config['SFIABDIRECTORY']."/images/16/edit.".$config['icon_extension']."\"></a>";
				echo "&nbsp;";
				echo "<a onclick=\"return confirmClick('Are you sure you want to remove this team?')\" href=\"judges_teams.php?action=delete&delete=".$team['id']."\"><img border=0 src=\"".$config['SFIABDIRECTORY']."/images/16/button_cancel.".$config['icon_extension']."\"></a>";

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



			}
			echo "</table>";
			echo "<script type=\"text/javascript\">$('.summarytable').tablesorter();</script>";
			echo "<br />";
		}
		else {
			echo "</td></tr></table>";
		}
	}
	send_footer();



?>
