<?

/*
 * This file is part of the 'Science Fair In A Box' project
 * SFIAB Website: http://www.sfiab.ca
 *
 * Copyright (C) 2005-2006 Sci-Tech Ontario Inc <info@scitechontario.org>
 * Copyright (C) 2005-2006 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');
require_once ('judges.inc.php');
user_auth_required('committee', 'admin');
send_header('Judging Score Entry - Update',
	array('Committee Main' => 'committee_main.php',
		'Administration' => 'admin/index.php',
		'Judging Score Entry' => 'admin/judging_score_entry.php'));

$year = $config['FAIRYEAR'];
$project_id = NULL;
if ($_GET['projectid']) {
	$project_id = $_GET['projectid'];
} else if ($_POST['projectid']) {
	$project_id = $_POST['projectid'];
	$curr_team = $_POST['score_count'];
	while ($curr_team > 0) {
		if ($_POST['team_' . $curr_team . '_score'] != '') {
			$score = $_POST['team_' . $curr_team . '_score'];
			if ($score == 0) {
				$score = 'NULL';
			} else {
				$score = $score;
			}
			if ($score > 100.0) {
				$score_error = '*** ERROR **** You entered a value greater than 100.00';
			}
			$stmt = $pdo->prepare("UPDATE judges_teams_timeslots_projects_link 
			SET score=?
			WHERE judges_teams_id =?
			and projects_id =? and year=?");
			$stmt->execute([$score,$_POST['team_' . $curr_team . '_id'],$project_id,$year]);
			show_pdo_errors_if_any($pdo);
		}
		$curr_team--;
	}
}

?>
<?
if ($project_id) {
	$q = $pdo->prepare("SELECT * FROM projects WHERE projects.id =?");
	$q->execute([$project_id]);
	$r = $q->fetch(PDO::FETCH_OBJ);
	$project_number = $r->projectnumber;
	$project_title = $r->title;
	$q = $pdo->prepare("SELECT * FROM projectcategories WHERE year=? ORDER BY id");
	$q->execute([$year]);
	while ($r = $q->fetch(PDO::FETCH_OBJ))
		$cats[$r->id] = $r->category;

	$q = $pdo->prepare("SELECT * FROM projectdivisions WHERE year=? ORDER BY id");
	$q->execute([$year]);

	$q = $pdo->prepare("SELECT judges_teams_timeslots_projects_link.judges_teams_id, 
											\t score, 
											\t judges_teams.num
		\t            FROM judges_teams_timeslots_projects_link, 
								\t     judges_teams
		\t            WHERE judges_teams_timeslots_projects_link.judges_teams_id = judges_teams.id 
								\t     AND projects_id =?  ORDER BY judges_teams_id");
	$q->execute([$project_id]);
	show_pdo_errors_if_any($pdo);
	echo 'Project# ' . $project_number . '  ' . $project_title . '<br />';
	if ($score_error != '') {
		echo $score_error . '<br />';
	}
	echo '<form action="judging_score_edit.php" method="post">';
	echo '<input type="hidden" name="score_count" value="' . $q->rowCount() . '"/>';
	echo "<input type=\"hidden\" name=\"projectid\" value=\"$project_id\"/>";
	echo '<table class="tableview">';
	echo '<tr>';
	echo '<th>' . i18n('Team Number') . '</th>';
	echo '<th>' . i18n('Judges') . '</th>';
	echo '<th>' . i18n('Score') . '</th>';
	echo '<th>' . i18n('New Score') . '</th>';
	echo '</tr>';

	$i = 1;
	while ($r = $q->fetch(PDO::FETCH_OBJ)) {
		$team = getJudgingTeam($r->judges_teams_id);
		$teamNames = array_map('teamMemberToName', $team['members']);
		echo "<tr>\n";
		echo "<td style=\"vertical-align: middle\">\n";
		echo $r->num;
		echo '<input type="hidden" name="team_' . $i . "_id\" value=\"$r->judges_teams_id\"/>\n";
		echo "</td>\n";
		echo '<td style="vertical-align: middle">';
		echo implode(', ', $teamNames);
		echo "</td>\n";
		echo "<td style=\"vertical-align: middle; text-align: center\">\n";
		if ($r->score) {
			echo $r->score;
		} else {
			echo 'None';
		}
		echo "\n</td>\n";
		echo "<td style=\"vertical-align: middle; text-align: center\">\n";
		echo '<input type="text" size="5" maxlength="5" name="team_' . $i . "_score\" value=\"$r->score\"/>\n";
		echo "</td>\n";
		echo "</tr>\n";
		$i++;
	}
	echo "</table>\n";
	echo "<input type=\"submit\" />\n";
	echo "</form>\n";
} else {
	echo i18n('Invalid Project ID.');
}