<?php
			
	include $SiteRoot . "includes/localsettings.php";	
	include $SiteRoot . "library/php-mysql.php";
	include $SiteRoot . "library/php-date.php";
	include $SiteRoot . "library/php-template.php";
	include $SiteRoot . "library/php-request.php";
	include $SiteRoot . "includes/functions.php";
		
	// Session enforcement
	$StudentID = request("StudentID");
	$StudentName = "";
	$StudentLoggedIn = false;
	//$StudentDifficulty = "";
	$StudentComplete = false;
	$UnlockedPuzzles = 0;

	// Update difficulty
	//if( request("selDifficulty") != "" )
	//	SqlExecute("UPDATE student SET difficulty = '" . request("selDifficulty") . "' WHERE id = '" . $StudentID . "'");

	if( $StudentID != "" )
	{
		$Record = SelectSingleRow("SELECT * FROM student WHERE id = '" . $StudentID . "' AND active = 1 AND site = '" . $gblSiteName . "'");
		if( $Record->GetValue("id") == "" )
		{
			if( $EnforceSession )
			{
				header("403 Forbidden");
				echo "<a href=/>Please return to the main page</a>";
				exit;
			}
			else
				$StudentID = "";
		}
		else
		{
			$StudentLoggedIn = true;
			$StudentName = $Record->GetValue("name");
			//$StudentDifficulty = $Record->GetValue("difficulty");			
			if( $Record->GetValue("complete") == "1" ) $StudentComplete = true;
			$UnlockedPuzzles = $Record->GetValue("puzzlesactive");
		}	
	}
	else if( $EnforceSession )
	{
		header("403 Forbidden");
		echo "<a href=/>Please return to the main page</a>";
		exit;
	}
	
	// Set global query variables
	$GlobalQuery = "StudentID=" . $StudentID;
	
	// Record hit
	
	// Check to see if record exists for the current day
	$Record = SelectSingleRow("SELECT count, studentcount FROM hits WHERE site = '" . $gblSiteName . "' AND hitdate = '" . date("y-m-d") . "'");
	if( $Record->GetValue("count") == "" )
	{
		// Add record
		if( $StudentLoggedIn )
			$StudentHits = 1;
		else
			$StudentHits = 0;
		
		SqlExecute("INSERT INTO hits (site, hitdate, count, studentcount) VALUES ('" . $gblSiteName . "', '" . date("y-m-d") . "', 1, " . $StudentHits . ")");
	}
	else
	{
		if( $StudentLoggedIn )
			SqlExecute("UPDATE hits SET count = count + 1, studentcount = studentcount + 1 WHERE site = '" . $gblSiteName . "' AND hitdate = '" . date("y-m-d") . "'");
		else
			SqlExecute("UPDATE hits SET count = count + 1 WHERE site = '" . $gblSiteName . "' AND hitdate = '" . date("y-m-d") . "'");
	}
			
			
	// Check global escapes
	function CheckGlobalEscapes( &$tpCurrent )
	{

		CheckCoreEscapes($tpCurrent);

		global $GlobalQuery;
		global $StudentName;
		global $StudentLoggedIn;
		global $gblSiteName;
		//global $StudentDifficulty;
		global $StudentComplete;
		global $gblDifficulties;
		//global $bShowDifficulty;
		
		if( $tpCurrent->IsEscape("GLOBAL-QUERY") ) echo $GlobalQuery;
		else if( $tpCurrent->IsEscape("GLOBAL-STUDENT-NAME") ) echo $StudentName;
		//else if( $tpCurrent->IsEscape("GLOBAL-STUDENT-DIFFICULTY") ) echo $StudentDifficulty;		
		else if( $tpCurrent->IsEscape("GLOBAL-IF-STUDENT") )
		{
			if( $StudentLoggedIn )
				$tpCurrent->PushArgumentToStream();
		}
		else if( $tpCurrent->IsEscape("GLOBAL-IF-NOT-STUDENT") )
		{
			if( !$StudentLoggedIn )
				$tpCurrent->PushArgumentToStream();
		}
		else if( $tpCurrent->IsEscape("GLOBAL-AD") )
		{
			$ad = SelectScalar("SELECT ad FROM ad WHERE site = '" . $gblSiteName . "'");
			if( $ad != "" )
				echo $ad . $tpCurrent->GetEscapeArgument();
		}
		
		else if( $tpCurrent->IsEscape("GLOBAL-IF-STUDENT-COMPLETE") )
		{
			if( $StudentComplete )
				$tpCurrent->PushArgumentToStream();
		}		
		else if( $tpCurrent->IsEscape("GLOBAL-IF-STUDENT-NOT-COMPLETE") )
		{
			if( !$StudentComplete )
				$tpCurrent->PushArgumentToStream();
		}
		/*else if( $tpCurrent->IsEscape("GLOBAL-DIFFICULTIES") )
		{
			for( $iIndex = 0; $iIndex < count($gblDifficulties); $iIndex++ )
			{
				$tpElem = new PhpTemplate;						
				for( $tpElem->Start( $tpCurrent->GetEscapeArgument() ); $tpElem->NextAndEchoOutput(); ) 
				{
					if( $tpElem->IsEscape("DIFFICULTY") ) echo $gblDifficulties[$iIndex];
					else if( $tpElem->IsEscape("IF-SELECTED") )
					{
						if( $StudentDifficulty == $gblDifficulties[$iIndex] )
							echo $tpElem->GetEscapeArgument();
					}
				}		
			}	
		}
		else if( $tpCurrent->IsEscape("GLOBAL-IF-SHOW-DIFFICULTY") )
		{
			if( $bShowDifficulty )
				$tpCurrent->PushArgumentToStream();
		}*/

	}	
	
?>