<?

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

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

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

$Error = "";

// Form submitted
if( request("SUBMIT") == "true" )
{
	$Question = trim(request("txtQuestion"));	
	$Number = trim(request("txtNumber"));
	$Illustration = trim(request("selIllustration"));
	//$Difficulty = trim(request("selDifficulty"));
	$Text = trim(request("txtText"));	
	
	// Check for errors
	if( $Question == "" ) $Error .= "Please enter a question<BR>";
	if( $Number == "" ) $Error .= "Please enter an order number<BR>";
	//if( $Difficulty == "" ) $Error .= "Please select a difficulty<BR>";
	//if( $Text == "" ) $Error .= "Please enter question text<BR>";
	
	if( $Error == "" )
	{
			
		// Update question record
		$Record = new PhpSqlRow;

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

		$Record->SetNameValue( "lessonid", $LessonID );
		$Record->SetNameValue( "question", $Question );
		$Record->SetNameValue( "answer", 0 );
		$Record->SetNameValue( "number", $Number );
		$Record->SetNameValue( "illustration", $Illustration );
		//$Record->SetNameValue( "difficulty", $Difficulty );
		$Record->SetNameValue( "text", $Text );

		if( $bEdit )
			UpdateRow($Record, "lesson_question", "id");
		else
			$QuestionID = InsertRow($Record, "lesson_question");		
			
		// Update answers
		for( $Index = 1; $Index < 11; $Index++ )
		{
		
			if( trim(request("txtAnswer" . $Index)) != "" )
			{
		
				$Record = new PhpSqlRow;
		
				if( request("hdnAnswerID" . $Index) != "" ) 
					$Record->SetNameValue( "id", request("hdnAnswerID" . $Index) );

				$Record->SetNameValue( "lessonid", $LessonID );
				$Record->SetNameValue( "questionid", $QuestionID );
				$Record->SetNameValue( "answer", request("txtAnswer" . $Index) );				
				
				if( request("chkCorrect") == $Index )
				{
					$Record->SetNameValue( "isanswer", 1 );
					$Answer = $Index;
				}
				else
					$Record->SetNameValue( "isanswer", 0 );
					
				$Record->SetNameValue( "number", $Index );

				if( request("hdnAnswerID" . $Index) != "" )
					UpdateRow($Record, "lesson_question_answer", "id");
				else
					InsertRow($Record, "lesson_question_answer");

			}
			else if( request("hdnAnswerID" . $Index) != "" )
			{
				// There is a database record, but no text, so delete record
				SqlExecute("DELETE FROM lesson_question_answer WHERE id = " . request("hdnAnswerID" . $Index));
			}
		}
					
		// Update answer string
		SqlExecute("UPDATE lesson_question SET answer = '" . $Answer . "' WHERE id = " . $QuestionID);
					
		header("Location: lesson_edit.php?" . $GlobalQuery . "&lessonid=" . $LessonID);
		exit;		
	}
	
}
else if( request("DELETE") == "true" )
{
	// Delete worksheet
	SqlExecute("DELETE FROM lesson_question WHERE id = " . $QuestionID);
	SqlExecute("DELETE FROM lesson_question_answer WHERE questionid = " . $QuestionID);

	header("Location: lesson_edit.php?" . $GlobalQuery . "&lessonid=" . $LessonID);
	exit;
}
else
{
	if( $bEdit )
	{
		// Load worksheet from database
		$Record = SelectSingleRow("SELECT * FROM lesson_question WHERE id = " . $QuestionID);
		$Question = $Record->GetValue("question");
		$Number = $Record->GetValue("number");
		$Illustration = $Record->GetValue("illustration");
		//$Difficulty = $Record->GetValue("difficulty");
		$Text = $Record->GetValue("text");
	}
	else
	{
		$Number = SelectScalar("SELECT MAX(number) FROM lesson_question WHERE lessonid = " . $LessonID);
		$Number++;
	}
}

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

// Begin Template
$tp = new PhpTemplate;
for( $tp->StartFromFile( "templates/lesson_question_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("QUESTION") )
		echo $Question;
	else if( $tp->IsEscape("TEXT") )
		echo $Text;
	else if( $tp->IsEscape("NUMBER") )
		echo $Number;	
	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("IF-EDIT") )
	{
		if( $bEdit )
			$tp->PushArgumentToStream();
	}
	/*else if( $tp->IsEscape("DIFFICULTIES") )
	{
	
		$tpElem = new PhpTemplate;						
		for( $index = 0; $index < count($gblDifficulties); $index++ )
		{
			for( $tpElem->Start( $tp->GetEscapeArgument() ); $tpElem->NextAndEchoOutput(); ) 
			{	
				if( $tpElem->IsEscape("DIFFICULTY") ) echo $gblDifficulties[$index];
				else if( $tpElem->IsEscape("IF-SELECTED") )
				{
					if( $gblDifficulties[$index] == $Difficulty ) 
						echo $tpElem->GetEscapeArgument();
				}
			}			
		}
	
	}*/
	else if( $tp->IsEscape("ANSWERS") )
	{

		$RowIndex = 0;		
		if( $bEdit )
		{
			// Connect to database
			$db = mysql_connect("localhost", $gblMySqlUser, $gblMySqlPass);
			mysql_select_db($gblMySqlDb, $db);

			// Load questions / answers			
			$query = "SELECT * FROM lesson_question_answer WHERE questionid = " . $QuestionID . " ORDER BY number";
			$result = mysql_query($query, $db);
			while($a = mysql_fetch_array($result))
			{				
				$Record = new PhpSqlRow;
				$Record->SetAsArray($a);
				$RowIndex++;

				$tpElem = new PhpTemplate;						
				for( $tpElem->Start( $tp->GetEscapeArgument() ); $tpElem->NextAndEchoOutput(); ) 
				{
					CheckGlobalEscapes( $tpElem );

					if( $tpElem->IsEscape("INDEX") )
						echo $RowIndex;
					else if( $tpElem->IsEscape("id") )
					{
						if( $Error != "" )
							echo request("hdnAnswerID" . $RowIndex);
						else
							echo $Record->GetValue("id");
					}
					else if( $tpElem->IsEscape("ANSWER") )
					{
						if( $Error != "" )
							echo request("txtAnswer" . $RowIndex);
						else				
							echo $Record->GetValue("answer");
					}
					else if( $tpElem->IsEscape("IF-CORRECT") )
					{
						if( $Error != "" )
						{
							if( request("chkCorrect") == $RowIndex )
								echo $tpElem->GetEscapeArgument();					
						}
						else
						{
							if( $Record->GetValue("isanswer") == "1" )
								echo $tpElem->GetEscapeArgument();
						}
					}
				}			

			}

		}
		
		// Space for new answers
		if( $RowIndex < 11 )
		{

			$RowIndex++;

			for( ; $RowIndex < 11; $RowIndex++ )
			{
						
				$tpElem = new PhpTemplate;						
				for( $tpElem->Start( $tp->GetEscapeArgument() ); $tpElem->NextAndEchoOutput(); ) 
				{
					CheckGlobalEscapes( $tpElem );

					if( $tpElem->IsEscape("INDEX") )
						echo $RowIndex;
					else if( $tpElem->IsEscape("ID") )
						echo request("hdnAnswerID" . $RowIndex);
					else if( $tpElem->IsEscape("ANSWER") )
						echo request("txtAnswer" . $RowIndex);
					else if( $tpElem->IsEscape("IF-CORRECT") )
					{
						if( request("chkCorrect") == $RowIndex )
							echo $tpElem->GetEscapeArgument();					
					}
				}					
			}

		}
	
	}
		
}

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

?>
