<?

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

// Page values
$LessonID = request("lessonid");

$bEdit = false;
if( $LessonID != "" ) $bEdit = true;

$Error = "";

// Form submitted
if( request("SUBMIT") == "true" )
{
	$Name = trim(request("txtName"));
	$Description = trim(request("txtDescription"));
	$Text = trim(request("txtText"));
	$Number = trim(request("txtNumber"));
	$Illustration = trim(request("selIllustration"));
	$Puzzle = trim(request("txtPuzzle"));
	
	// Check for errors
	if( $Name == "" ) $Error .= "Please enter a name<BR>";
	
	if( $Error == "" )
	{
		// Update database
		$Record = new PhpSqlRow;

		if( $bEdit ) 
			$Record->SetNameValue( "id", $LessonID );

		$Record->SetNameValue( "site", $gblSiteName );		
		$Record->SetNameValue( "name", $Name );
		$Record->SetNameValue( "description", $Description );
		$Record->SetNameValue( "text", $Text );
		$Record->SetNameValue( "illustration", $Illustration );		
		$Record->SetNameValue( "puzzle", $Puzzle );
		$Record->SetNameValue( "number", $Number );		
		
		if( $bEdit )			
			UpdateRow($Record, "lesson", "id");
		else
			$LessonID = InsertRow($Record, "lesson");

		if( $bEdit )
		{
			header("Location: lessons.php?" . $GlobalQuery);
			exit;		
		}
		else
		{
			header("Location: lesson_edit.php?" . $GlobalQuery . "&lessonid=" . $LessonID);
			exit;					
		}
	}
	
}
else if( request("DELETE") == "true" )
{
	// Delete lesson
	SqlExecute("DELETE FROM lesson_question_answer WHERE lessonid = " . $LessonID);
	SqlExecute("DELETE FROM lesson_question WHERE lessonid = " . $LessonID);
	SqlExecute("DELETE FROM lesson WHERE id = " . $LessonID);

	header("Location: lessons.php?" . $GlobalQuery);
	exit;
}
else
{
	if( $bEdit )
	{
		// Load lesson from database
		$Record = SelectSingleRow("SELECT * FROM lesson WHERE id = " . $LessonID);
		$Name = $Record->GetValue("name");
		$Description = $Record->GetValue("description");
		$Text = $Record->GetValue("text");
		$Illustration = $Record->GetValue("illustration");
		$Puzzle = $Record->GetValue("puzzle");
		$Number = $Record->GetValue("number");
	}
	else
	{
		$Number = SelectScalar("SELECT MAX(number) FROM lesson WHERE site = '" . $gblSiteName . "'") + 1;
	}
}

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

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

	CheckGlobalEscapes( $tp );

	if( $tp->IsEscape("IF-ERROR") )
	{
		if( $Error != "" ) 
			$tp->PushArgumentToStream();
	}
	else if( $tp->IsEscape("ERROR") )
		echo $Error;
	else if( $tp->IsEscape("NAME") )
		echo $Name;
	else if( $tp->IsEscape("DESCRIPTION") )
		echo $Description;
	else if( $tp->IsEscape("TEXT") )
		echo $Text;
	else if( $tp->IsEscape("ILLUSTRATIONS") )
	{

		$tpElem = new PhpTemplate;						
		$dirHandle = @opendir ("../illustrations");
		while (false !== ($fileName = @readdir ($dirHandle))) 
		{			
			if( $fileName != "." && $fileName != ".." )
			{
				for( $tpElem->Start( $tp->GetEscapeArgument() ); $tpElem->NextAndEchoOutput(); ) 
				{	
					if( $tpElem->IsEscape("FILE-NAME") ) echo $fileName;
					else if( $tpElem->IsEscape("IF-SELECTED") )
					{
						if( $fileName == $Illustration ) 
							echo $tpElem->GetEscapeArgument();
					}
				}			
			}
		}
	
	}
	else if( $tp->IsEscape("PUZZLE") )
	{
		echo $Puzzle;
	}
	else if( $tp->IsEscape("UNUSED-PUZZLE-COUNT") )
	{
		$PuzzleCount = SelectScalar("SELECT COUNT(*) FROM puzzles WHERE site = '" . $gblSiteName . "'");
		echo $PuzzleCount - SelectScalar("SELECT SUM(puzzle) FROM lesson WHERE site = '" . $gblSiteName . "'");
	}
	else if( $tp->IsEscape("IF-EDIT") )
	{
		if( $bEdit )
			$tp->PushArgumentToStream();
	}
	else if( $tp->IsEscape("NUMBER") )
		echo $Number;
	else if( $tp->IsEscape("QUESTIONS-ANSWERS") )
	{
			
		$CurrentQuestionID = 0;
	
		// Connect to database
		$db = mysql_connect("localhost", $gblMySqlUser, $gblMySqlPass);
		mysql_select_db($gblMySqlDb, $db);
							
		// Load questions / answers
		$RowIndex = 0;
		$query = "SELECT Q.difficulty, Q.id, Q.question, A.answer, A.isanswer FROM lesson_question Q LEFT JOIN lesson_question_answer A ON A.questionid = Q.ID WHERE Q.lessonid = " . $LessonID . " ORDER BY Q.number, A.number";			
		$result = mysql_query($query, $db);
		while($a = mysql_fetch_array($result))
		{				
			$Record = new PhpSqlRow;
			$Record->SetAsArray($a);
			$RowIndex++;
			
			$bNewQuestion = false;
			if( $Record->GetValue("id") != $CurrentQuestionID )
			{
				$bNewQuestion = true;
				$CurrentQuestionID = $Record->GetValue("id");
			}
									
			$tpElem = new PhpTemplate;						
			for( $tpElem->Start( $tp->GetEscapeArgument() ); $tpElem->NextAndEchoOutput(); ) 
			{
				CheckGlobalEscapes( $tpElem );
				
				if( $tpElem->IsEscape("IF-NEW-QUESTION") )
				{
					if( $bNewQuestion )
						$tpElem->PushArgumentToStream();
				}
				else if( $tpElem->IsEscape("IF-CURRENT-QUESTION") )
				{
					if( !$bNewQuestion )
						$tpElem->PushArgumentToStream();
				}
				else if( $tpElem->IsEscape("IF-NOT-FIRST-QUEST") )
				{
					if( $RowIndex > 1 )
						echo $tpElem->GetEscapeArgument();
				}
				else if( $tpElem->IsEscape("QUESTION") )
					echo $Record->GetValue("question");				
				else if( $tpElem->IsEscape("DIFFICULTY") )
					echo $Record->GetValue("difficulty");	
				else if( $tpElem->IsEscape("ID") )
					echo $Record->GetValue("id");
				else if( $tpElem->IsEscape("ANSWER") )
				{
					echo $Record->GetValue("answer");
				}
				else if( $tpElem->IsEscape("IF-CORRECT-ANSWER") )
				{
					if( $Record->GetValue("isanswer") == "1" )
						echo $tpElem->GetEscapeArgument();
				}
			}
				
		}
	
	}
		
}

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

?>
