<?
require_once('common.inc.php');
require_once('user.inc.php');

// authenticate the login
if($_POST['schoolid'] && $_POST['accesscode'])
{
	$q=mysql_query("SELECT * FROM schools WHERE id='".$_POST['schoolid']."' AND accesscode='".$_POST['accesscode']."' AND year='".$config['FAIRYEAR']."'");
	if(mysql_num_rows($q)==1)
	{
		$_SESSION['schoolid']=$_POST['schoolid'];
		$_SESSION['schoolaccesscode']=$_POST['accesscode'];
		mysql_query("UPDATE schools SET lastlogin=NOW() WHERE id='".$_POST['schoolid']."'");

	}
	else
		$errormsg="Invalid School ID or Access Code";
}

// handle a logout request
if($_GET['action']=="logout")
{
	unset($_SESSION['schoolid']);
	unset($_SESSION['schoolaccesscode']);
	$happymsg=i18n("You have been logged out from the school access page");
}


if($_SESSION['schoolid'] && $_SESSION['schoolaccesscode']){
	// we're logged in, draw the dashboard
	$q=mysql_query("SELECT * FROM schools WHERE id='".$_SESSION['schoolid']."' AND accesscode='".$_SESSION['schoolaccesscode']."' AND year='".$config['FAIRYEAR']."'");
	echo mysql_error();
	$school=mysql_fetch_object($q);
	if($school) {
		draw_dashboard();
	}else{
		echo error(i18n("Invalid School ID or Access Code"));
	}
}else {
	draw_login();
}
send_footer();


// FIXME - this needs to be beautified
function draw_dashboard(){
	send_header("School Home",
	 array(),
        "events_scheduling" );
	global $config;
	global $conference;
	echo "<ul>\n";
	echo '<li><a href="schoolinfo.php">' . i18n("School Information") . "</a></li>\n";
	if($conference['id'] == 0 || $conference['type'] == 'sciencefair'){
		if($config['participant_registration_type']=="schoolpassword"
		   || $config['participant_registration_type']=="invite"
		   || $config['participant_registration_type']=="openorinvite"
		){
			echo '<li><a href="schoolinvite.php">' . i18n("Participant Registration") . "</a></li>\n";
		}
	}else if($conference['type'] == 'scienceolympics'){
		echo  '<li><a href="schoolstudents.php">' . i18n("Manage Students") . "</a></li>\n";
		echo  '<li><a href="schoolteams.php">' . i18n("Manage Teams") . "</a></li>\n";
		echo  '<li><a href="schoolschedule.php">' . i18n("Register Teams for Events") . "</a></li>\n";
	}
	echo '<li><a href="schoolfeedback.php">' . i18n("School Feedback / Questions") . "</a></li>";
	echo "</ul>\n";
}

function draw_login(){
	send_header("School Home");
	global $errormsg, $happymsg, $config;
	if($errormsg) echo "<font color=red><b>$errormsg</b></font>";
	if($happymsg) echo happy($happymsg);

	echo "	<form method=POST action=\"schoolaccess.php\">\n";

	echo output_page_text("schoolaccess");

	if($config['participant_registration_type']=="open" || $config['participant_registration_type']=="openorinvite")
	{
		echo "<br><br>\n";
		echo i18n("Note: Schools do not need to login in order to have students register from their school.  Students can register by going to the Participant Registration Page.  The only benefit of logging in is to update your school contact information or submit feedback.:");
		echo "<br />";
		echo "&nbsp;&nbsp;&nbsp;<a href=\"register_participants.php\">",i18n("Participant Registration")."</a><br />";

	}
	echo "<br />";
	echo i18n("Please login below by selecting your school and entering your school <b>Access Code</b> that you received in your package");
?>

	<br><br>
	<table border=0 cellspacing=0 cellpadding=5>
	<tr><td><?=i18n("School")?>:</td><td>
	<select name="schoolid">
	<option value=""><?=i18n("Choose your school")?></option>
	<?
	$q=mysql_query("SELECT id,school,city FROM schools WHERE year='".$config['FAIRYEAR']."' ORDER BY school");
	$prev="somethingthatdoesnotexist";
	while($r=mysql_fetch_object($q))
	{
		if($r->school==$prev)
			echo "<option value=\"$r->id\">$r->school ($r->city)</option>\n";
		else
			echo "<option value=\"$r->id\">$r->school</option>\n";
		$prev=$r->school;
	}
	?>
	</select>
	</td></tr>
	<tr><td><?=i18n("Access Code")?>:</td><td><input type=text name=accesscode></td></tr>
	<tr><td align=center><input type=submit value="<?=i18n("Login")?>"></td></tr>
	</table>

	</form>

	<br><br>

<?
}
