<?

////////////////////////////////
$bShowDifficulty = true;
$EnforceSession = true;
include "includes/includes.php";
////////////////////////////////

// Header
include "includes/header.php";

$LessonCount = SelectScalar("SELECT COUNT(*) FROM student_lesson WHERE studentid = '" . $StudentID . "'");

// Begin Template
$tp = new PhpTemplate;
for( $tp->StartFromFile( "templates/main.template" ); $tp->NextAndEchoOutput(); ) 
{

	CheckGlobalEscapes( $tp );
	
	if( $tp->IsEscape("IF-NOT-STARTED") )
	{
		if( $LessonCount == 0 )
			$tp->PushArgumentToStream();
	}
	else if( $tp->IsEscape("IF-STARTED") )
	{
		if( $LessonCount > 0 )
			$tp->PushArgumentToStream();	
	}
	else if( $tp->IsEscape("IF-UNLOCKED-PUZZLES") )
	{
		if( $UnlockedPuzzles > 0 )
			$tp->PushArgumentToStream();
	}
	else if( $tp->IsEscape("IF-NO-UNLOCKED-PUZZLES") )
	{
		if( $UnlockedPuzzles == 0 )
			$tp->PushArgumentToStream();
	}
	else if( $tp->IsEscape("PUZZLE-COUNT") )
	{
		echo $UnlockedPuzzles;
	}
	else if( $tp->IsEscape("TOTAL-PUZZLES") )
	{
		echo SelectScalar("SELECT COUNT(*) FROM puzzles WHERE site = '" . $gblSiteName . "'");
	}
	else if( $tp->IsEscape("PUZZLES") )
	{
	
		// Connect
		$db = mysql_connect("localhost", $gblMySqlUser, $gblMySqlPass);
		mysql_select_db($gblMySqlDb, $db);
		
		// Retrieve data
		$query = "SELECT * FROM puzzles WHERE site = '" . $gblSiteName . "' AND number <= " . $UnlockedPuzzles . " ORDER BY number DESC LIMIT 4";
		$result = mysql_query($query, $db);
		
		$RowIndex = 0;
						
		// Loop results
		for( $RowIndex = 0; $a = mysql_fetch_array($result); $RowIndex++ )
		{
			$Record = new PhpSqlRow;
			$Record->SetAsArray($a);
			
			$tpElem = new PhpTemplate;						
			for( $tpElem->Start( $tp->GetEscapeArgument() ); $tpElem->NextAndEchoOutput(); ) 
			{
				CheckGlobalEscapes($tpElem);
				if( $tpElem->IsEscape("ID") ) echo $Record->GetValue("id");
				else if( $tpElem->IsEscape("TYPE") ) echo $Record->GetValue("puzzletype");
				else if( $tpElem->IsEscape("INDEX") ) echo $RowIndex;
				else if( $tpElem->IsEscape("IF-NEW-ROW") )
				{
					if( $RowIndex % $tpElem->GetParameter() == 1 )
						$tpElem->PushArgumentToStream();
				}
			}		
		}
	}
	else if( $tp->IsEscape("LESSONS") )
	{
	
		// Connect
		$db = mysql_connect("localhost", $gblMySqlUser, $gblMySqlPass);
		mysql_select_db($gblMySqlDb, $db);
		
		// Retrieve data
		$query = "SELECT L.id, L.name, L.description, L.puzzle, L.percentage, SL.correct, SL.complete FROM lesson L LEFT JOIN student_lesson SL ON SL.lessonid = L.id AND SL.studentid = '" . $StudentID . "' WHERE L.site = '" . $gblSiteName . "' ORDER BY L.number";
		$result = mysql_query($query, $db);
		
		$RowIndex = 0;
						
		// Loop results
		for( $RowIndex = 0; $a = mysql_fetch_array($result); $RowIndex++ )
		{
			$Record = new PhpSqlRow;
			$Record->SetAsArray($a);
								
			$tpElem = new PhpTemplate;						
			for( $tpElem->Start( $tp->GetEscapeArgument() ); $tpElem->NextAndEchoOutput(); ) 
			{
			
				CheckGlobalEscapes($tpElem);
				
				if( $tpElem->IsEscape("ID") ) echo $Record->GetValue("id");
				else if( $tpElem->IsEscape("INDEX") ) echo $RowIndex + 1;
				else if( $tpElem->IsEscape("NAME") ) echo $Record->GetValue("name");
				else if( $tpElem->IsEscape("DESCRIPTION") ) echo $Record->GetValue("description");
				else if( $tpElem->IsEscape("IF-COMPLETE") )
				{
					if( $Record->GetValue("complete") == "1" )
						$tpElem->PushArgumentToStream();
				}
				else if( $tpElem->IsEscape("IF-NOT-COMPLETE") )
				{
					if( $Record->GetValue("complete") != "1" )
						$tpElem->PushArgumentToStream();
				}
				else if( $tpElem->IsEscape("IF-UNLOCK-PUZZLE") )
				{
					if( $Record->GetValue("puzzle") != "0" )
						$tpElem->PushArgumentToStream();	
				}
				else if( $tpElem->IsEscape("UNLOCK-PUZZLE") )
				{
					echo $Record->GetValue("puzzle");
				}
			}
			
		}			
	
	}
	
}

// Footer
include "includes/footer.php";

?>