<?
function getJudgingTeams()
{
	global $config, $pdo;
	$q=$pdo->prepare("SELECT judges_teams.id,
				judges_teams.num,
				judges_teams.name
			FROM 
				judges_teams
			WHERE 
				judges_teams.year='".$config['FAIRYEAR']."'
			ORDER BY 
				num,name");
	$q->execute();

	$lastteamid=-1;
	$lastteamnum=-1;
	show_pdo_errors_if_any($pdo);
	$teams=array();
	while($r=$q->fetch(PDO::FETCH_OBJ))
	{
		$teams[$r->id]['id']=$r->id;
		$teams[$r->id]['num']=$r->num;
		$teams[$r->id]['name']=$r->name;
		$lastteamid=$r->id;
		$lastteamnum=$r->num;

		/* Load timeslots */
		$rounds = array();
		$tq = $pdo->prepare("SELECT * FROM judges_teams_timeslots_link
					LEFT JOIN judges_timeslots ON judges_timeslots.id=judges_teams_timeslots_link.judges_timeslots_id
					WHERE judges_teams_timeslots_link.judges_teams_id='{$r->id}'");
		$tq->execute();
		$teams[$r->id]['timeslots'] = array();
		$teams[$r->id]['rounds'] = array();

		while($ts = $tq->fetch(PDO::FETCH_ASSOC)) {
			$teams[$r->id]['timeslots'][] = $ts;
			$rounds[$ts['round_id']] = $ts['round_id'];
		}

		foreach($rounds as $round_id) {
			$tq = $pdo->prepare("SELECT * FROM judges_timeslots WHERE id='{$round_id}'");
			$tq->execute();
			$teams[$r->id]['rounds'][] = $tq->fetch(PDO::FETCH_ASSOC);
		}
		
		//get the members for this team
		$mq = $pdo->prepare("SELECT 	
					users.id AS judges_id,
					users.firstname,
					users.lastname,
					judges_teams_link.captain
				FROM 
					users,
					judges_teams_link
				WHERE 
					judges_teams_link.users_id=users.id AND
					judges_teams_link.judges_teams_id='$r->id'
				ORDER BY 
					captain DESC,
					lastname,
					firstname");
		$mq->execute();
		show_pdo_errors_if_any($pdo);

		
        $teamlangs=array();
		while($mr=$mq->fetch(PDO::FETCH_OBJ))
		{
			$u = user_load($mr->judges_id, false);
			$judgelangs = join('/', $u['languages']);
			foreach($u['languages'] AS $l) {
				if(!in_array($l,$teamlangs))
					$teamlangs[]=$l;
			}
			
			$teams[$lastteamid]['members'][]=array(
				"id"=>$mr->judges_id,
				"firstname"=>$mr->firstname,
				"lastname"=>$mr->lastname,
				"captain"=>$mr->captain,
				"languages"=>$judgelangs,
				"languages_array"=>$u['languages']
				);
		}
        $teams[$r->id]['languages_members']=$teamlangs;

		//we also need to add all the languages that the team must JUDGE to the teams languages.
		$lq=$pdo->prepare("SELECT projects.language
			FROM judges_teams_timeslots_projects_link 
			LEFT JOIN projects ON judges_teams_timeslots_projects_link.projects_id=projects.id
			WHERE judges_teams_timeslots_projects_link.year='{$config['FAIRYEAR']}' AND
			judges_teams_id='$r->id' AND language!='' ");
		$lq->execute();
		show_pdo_errors_if_any($pdo);
		$projectlangs=array();
		while($lr=$lq->fetch(PDO::FETCH_OBJ)) {
			if(!in_array($lr->language,$projectlangs))
				$projectlangs[]=$lr->language;
			if(!in_array($lr->language,$teamlangs))
				$teamlangs[]=$lr->language;
		}
        $teams[$r->id]['languages_projects']=$projectlangs;
        $teams[$r->id]['languages']=$teamlangs;

		//get the awards for this team
		$aq=$pdo->prepare("SELECT award_awards.id,
					award_awards.name,
					award_awards.criteria,
					award_awards.award_types_id,
					award_types.type AS award_type
				FROM
					award_awards,
					judges_teams_awards_link,
					award_types
				WHERE
					judges_teams_awards_link.award_awards_id=award_awards.id
					AND judges_teams_awards_link.judges_teams_id='$r->id'
					AND award_awards.award_types_id=award_types.id
					AND award_types.year='{$config['FAIRYEAR']}'
				ORDER BY
					name
				");
			$aq->execute();
		while($ar=$aq->fetch(PDO::FETCH_OBJ))
		{
			$teams[$r->id]['awards'][]=array(
					"id"=>$ar->id,
					"name"=>$ar->name,
					"criteria"=>$ar->criteria,
					"award_types_id"=>$ar->award_types_id,
					"award_type"=>$ar->award_type
					);
		}
	}
	return $teams;
}

function getJudgingTeam($teamid)
{
	global $config;
	global $pdo;
	$q=$pdo->prepare("SELECT 	judges_teams.id,
				judges_teams.num,
				judges_teams.name
				
			FROM 
				judges_teams
			WHERE 
				judges_teams.year='".$config['FAIRYEAR']."' AND
				judges_teams.id='$teamid'
			ORDER BY 
				num,
				name
				");
	$q->execute();

	$team=array();

	$first=true;
	while($r=$q->fetch(PDO::FETCH_OBJ))
	{
		$team['id']=$r->id;
		$team['num']=$r->num;
		$team['name']=$r->name;

		//get the members for this team
		$mq=$pdo->prepare("SELECT 	
			users.id AS judges_id,
			users.firstname,
			users.lastname,
			judges_teams_link.captain
			
		FROM 
			users,
			judges_teams_link
		WHERE 
			judges_teams_link.users_id=users.id AND
			judges_teams_link.judges_teams_id='$r->id'
		ORDER BY 
			captain DESC,
			lastname,
			firstname");
		$mq->execute();
		show_pdo_errors_if_any($pdo);

		
		while($mr=$mq->fetch(PDO::FETCH_OBJ))
		{
			$team['members'][]=array(
				"id"=>$mr->judges_id,
				"firstname"=>$mr->firstname,
				"lastname"=>$mr->lastname,
				"captain"=>$mr->captain
				);
		}


		//get the awards for this team
		$aq=$pdo->prepare("SELECT award_awards.id,
					award_awards.name,
					award_awards.award_types_id,
					award_types.type AS award_type
				FROM
					award_awards,
					judges_teams_awards_link,
					award_types
				WHERE
					judges_teams_awards_link.award_awards_id=award_awards.id
					AND judges_teams_awards_link.judges_teams_id='$r->id'
					AND award_awards.award_types_id=award_types.id
					AND award_types.year='{$config['FAIRYEAR']}'
				ORDER BY
					name
				");
		$aq->execute();
		while($ar=$aq->fetch(PDO::FETCH_OBJ))
		{
			$team['awards'][]=array(
					"id"=>$ar->id,
					"name"=>$ar->name,
					"award_types_id"=>$ar->award_types_id,
					"award_type"=>$ar->award_type
					);
		}


	}

	return $team;

}

function getJudgingEligibilityCode() {
	global $config;
	switch($config['project_status']) {
		case 'open' :
			return " AND registrations.status != 'open' ";
			break;
		case 'payment_pending' :
			return " AND registrations.status IN ('paymentpending', 'complete')";
			break;
		case 'complete' :
			return " AND registrations.status = 'complete'";
			break;
	}
}

function teamMemberToName($member)
{
       return $member["firstname"] . " " . $member["lastname"];
}

function judges_load_all()
{
	global $config, $pdo;

	$ret = array();

	$query = "SELECT id FROM users WHERE types LIKE '%judge%' 
				AND year='{$config['FAIRYEAR']}'
				AND deleted='no'
				ORDER BY lastname, firstname";
	$r = $pdo->prepare($query);
	$r->execute();
	while($i = $r->fetch(PDO::FETCH_ASSOC)) {
		$u = user_load($i['id']);
		if($u['judge_complete'] == 'no') continue;
		if($u['judge_active'] == 'no') continue;

		$ret[$i['id']] = $u;
	}
	return $ret;
}

?>
