<?

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

// Get current lesson id	
$LessonID = request("lessonid");
if( $LessonID == "" )
{
	$Record = SelectSingleRow("SELECT L.* FROM lesson L LEFT JOIN student_lesson SL ON SL.lessonid = L.id AND SL.studentid = '" . $StudentID . "' WHERE L.site = '" . $gblSiteName . "' AND SL.complete IS NULL ORDER BY L.number LIMIT 1");	
	$LessonID = $Record->GetValue("id");
	
	if( $LessonID == "" )
	{
		header( "location: main.php?" . $GlobalQuery );
		exit;
	}
}
else
{
	$Record = SelectSingleRow("SELECT * FROM lesson WHERE id = " . $LessonID);
}
$LessonIllustration = $Record->GetValue("illustration");
$LessonName = $Record->GetValue("name");
$LessonText = $Record->GetValue("text");


$CorrectAnswer = false;
if( request("selectanswer") != "" )
{
	if( SelectScalar("SELECT isanswer FROM lesson_question_answer WHERE id = " . request("selectanswer")) == "1" ) $CorrectAnswer = true;
}

if( $CorrectAnswer || request("selectanswer") == "" )
{
	// Get next question
	$QuestionNumber = request("question");

	if( $QuestionNumber != "" )
		$Record = SelectSingleRow("SELECT * FROM lesson_question WHERE lessonid = " . $LessonID . " AND number > " . $QuestionNumber . " ORDER BY number LIMIT 1");
	else
		$Record = SelectSingleRow("SELECT * FROM lesson_question WHERE lessonid = " . $LessonID . " ORDER BY number LIMIT 1");
}
else
{
	$Record = SelectSingleRow("SELECT * FROM lesson_question WHERE id = " . request("questionid"));
}

$bLessonComplete = false;

// Is lesson complete?
if( $Record->GetValue("id") == "" )
{

	$Exists = SelectScalar("SELECT studentid FROM student_lesson WHERE studentid = '" . $StudentID . "' AND lessonid = " . $LessonID);
	if( $Exists == "" )
	{
		$Record = new PhpSqlRow;
		$Record->SetNameValue( "studentid", $StudentID );
		$Record->SetNameValue( "lessonid", $LessonID );
		$Record->SetNameValue( "complete", 1 );
		InsertRow($Record, "student_lesson");
		
		// Unlock puzzle
		$UnlockPuzzle = SelectScalar("SELECT puzzle FROM lesson WHERE id = " . $LessonID);
		if( $UnlockPuzzle != "" )
			SqlExecute("UPDATE student SET puzzlesactive = puzzlesactive + " . $UnlockPuzzle . " WHERE id = '" . $StudentID . "'");
	}
	
	// Is course complete?
	$CompletedLessons = SelectScalar("SELECT COUNT(*) FROM student_lesson WHERE studentid = '" . $StudentID . "' AND complete = 1");
	$TotalLessons = SelectScalar("SELECT COUNT(*) FROM lesson WHERE site = '" . $gblSiteName . "'");
	
	if( $CompletedLessons == $TotalLessons )
		SqlExecute("UPDATE student SET complete = 1 WHERE id = '" . $StudentID . "'");
		
	$bLessonComplete = true;
}

$QuestionID = $Record->GetValue("id");
$Question = $Record->GetValue("question");
$QuestionText = $Record->GetValue("text");
$QuestionIllustration = $Record->GetValue("illustration");
$QuestionNumber = $Record->GetValue("number");

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

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

	CheckGlobalEscapes( $tp );	
	if( $tp->IsEscape("QUESTION") ) echo $Question;	
	else if( $tp->IsEscape("QUESTION-TEXT") ) echo $QuestionText;
	else if( $tp->IsEscape("IF-LESSON-COMPLETE") )
	{
		if( $bLessonComplete )
			$tp->PushArgumentToStream();
	}
	else if( $tp->IsEscape("IF-LESSON-NOT-COMPLETE") )
	{
		if( !$bLessonComplete )
			$tp->PushArgumentToStream();
	}
	else if( $tp->IsEscape("IF-LESSON-PICTURE") )
	{
		if( $LessonIllustration != "" )
			$tp->PushArgumentToStream();
	}
	else if( $tp->IsEscape("IF-NO-LESSON-PICTURE") )
	{
		if( $LessonIllustration == "" )
			$tp->PushArgumentToStream();
	}
	else if( $tp->IsEscape("IF-QUESTION-TEXT") )
	{
		if( $QuestionText != "" )
			$tp->PushArgumentToStream();	
	}
	else if( $tp->IsEscape("LESSON-TEXT") )
	{
		echo $LessonText;
	}
	else if( $tp->IsEscape("LESSON-NAME") )
	{
		echo $LessonName;
	}
	else if( $tp->IsEscape("IF-QUESTION-PICTURE") )
	{
		if( $QuestionIllustration != "" )
			$tp->PushArgumentToStream();
	}
	else if( $tp->IsEscape("IF-NO-QUESTION-PICTURE") )
	{
		if( $QuestionIllustration == "" )
			$tp->PushArgumentToStream();
	}
	else if( $tp->IsEscape("QUESTION-PICTURE") )
	{
		echo $QuestionIllustration;
	}
	else if( $tp->IsEscape("LESSON-PICTURE") )
	{
		echo $LessonIllustration;
	}
	else if( $tp->IsEscape("IF-CORRECT") )
	{
		if( $CorrectAnswer )
			$tp->PushArgumentToStream();
	}
	else if( $tp->IsEscape("IF-NOT-CORRECT") )
	{
		if( !$CorrectAnswer )
			$tp->PushArgumentToStream();
	}
	else if( $tp->IsEscape("ANSWERS") )
	{
	
		// Connect
		$db = mysql_connect("localhost", $gblMySqlUser, $gblMySqlPass);
		mysql_select_db($gblMySqlDb, $db);
		
		// Retrieve data
		$query = "SELECT * FROM lesson_question_answer WHERE questionid = " . $QuestionID . " ORDER BY 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("ANSWER") ) echo $Record->GetValue("answer");
				else if( $tpElem->IsEscape("IF-CORRECT") )
				{
					if( $Record->GetValue("IsAnswer") == "1" )
						$tpElem->PushArgumentToStream();
				}
				else if( $tpElem->IsEscape("LESSONID") ) echo $LessonID;
				else if( $tpElem->IsEscape("QUESTION-NUMBER") ) echo $QuestionNumber;				
				else if( $tpElem->IsEscape("ID") ) echo $Record->GetValue("id");
				else if( $tpElem->IsEscape("QUESTION-ID") ) echo $QuestionID;
			}
			
		}			
	
	}
	
}

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

?>