<%@ Page Language="VB" %>
<%@ Import Namespace="System.Xml" %>
<html>
<head>
<title>World Flags Quiz</title>
<script language="VB" runat="server">
' Set relative file path to XML data
Dim strXmlFilePath as String = Server.MapPath("quiz.xml")
Dim xDoc as XmlDocument = new XmlDocument()
Dim intTotalQuestions as Integer
Dim intQuestionNbr as Integer = 1
Dim intScore as Integer = 0
Dim arrAnswerHistory as new ArrayList()
Sub Page_Load(src as Object, e as EventArgs)
' Load XML data
xDoc.Load(strXmlFilePath)
' If not a postback, start a new quiz
If Not Page.IsPostBack Then
' Retrieve count of total questions
intTotalQuestions = xDoc.SelectNodes("/quiz/question").Count
' Initialize start time
ViewState("StartTime") = DateTime.Now
' Display next question
ShowQuestion(intQuestionNbr)
End If
End Sub
' Submit the answer and either go to the next question or finish the quiz
Sub btnSubmit_Click(src as Object, e as EventArgs)
' Retrieve viewstate variables
intTotalQuestions = ViewState("TotalQuestions")
intQuestionNbr = ViewState("QuestionNbr")
intScore = ViewState("Score")
arrAnswerHistory = ViewState("AnswerHistory")
' Determine if correct answer submitted
If rblAnswer.SelectedItem.Value = ViewState("CorrectAnswer") Then
intScore += 1
arrAnswerHistory.Add(0)
Else
arrAnswerHistory.Add(rblAnswer.SelectedItem.Value)
End If
' Determine if this is the last question
If intQuestionNbr = intTotalQuestions Then
' Show the results page
QuizPage.Visible = False
ResultsPage.Visible = True
ShowResult()
Else
' Show the next question
QuizPage.Visible = True
ResultsPage.Visible = False
intQuestionNbr += 1
ShowQuestion(intQuestionNbr)
End If
End Sub
Sub ShowQuestion(intQuestionNbr as Integer)
Dim strXPath as String
Dim xNodeList as XmlNodeList
Dim xNodeAttr as Object
Dim i as Integer
Dim tsTimeTaken as TimeSpan
' Set reference to specified XML question element
strXPath = "/quiz/question[" & intQuestionNbr.ToString() & "]"
' Extract hint
lblQuestion.Text = intQuestionNbr.ToString() & ". " & xDoc.SelectSingleNode(strXPath & "/hint").InnerXml
' Extract image
Image1.ImageURL = "images//" & xDoc.SelectSingleNode(strXPath & "/image").InnerXml
' Set reference to answer elements
xNodeList = xDoc.SelectNodes(strXPath & "/answer")
'Clear previous radiobutton items
rblAnswer.Items.Clear
' Populate radiobuttons with answer elements
For i = 0 to xNodeList.Count - 1
'Add item to radiobuttonlist
rblAnswer.Items.Add(new ListItem(xNodeList.Item(i).InnerText, i + 1))
'Extract correct answer
xNodeAttr = xNodeList.Item(i).Attributes.ItemOf("correct")
If Not xNodeAttr is Nothing Then
If xNodeAttr.Value = "yes" Then
ViewState("CorrectAnswer") = i + 1
End If
End If
Next
' Display total Questions
lblTotalQuestions.Text = intTotalQuestions
' Display time taken
tsTimeTaken = DateTime.Now.Subtract(ViewState("StartTime"))
Dim sTmp as String = ""
If tsTimeTaken.Seconds < 10 Then
sTmp = "0"
End If
lblTimeTaken.Text = tsTimeTaken.Minutes.ToString() & ":" & sTmp & tsTimeTaken.Seconds.ToString()
'Store viewstate data
ViewState("TotalQuestions") = intTotalQuestions
ViewState("Score") = intScore
ViewState("QuestionNbr") = intQuestionNbr
ViewState("AnswerHistory") = arrAnswerHistory
End Sub
Sub ShowResult()
Dim xNodeList as XmlNodeList
Dim xNodeAttr as Object
Dim strResult as String
Dim intCompetency as Integer
Dim i as Integer
Dim j as Integer
Dim strXPath as String
Dim tsTimeTaken as TimeSpan
' Calculate length of time taken for quiz
tsTimeTaken = DateTime.Now.Subtract(ViewState("StartTime"))
Dim sTmp as String = ""
If tsTimeTaken.Seconds < 10 Then
sTmp = "0"
End If
' Concatenate a string that contains the results of the quiz
strResult = "<center>"
strResult += "<h3>Quiz Result</h3>"
strResult += "<p>Number Correct: " & intScore.ToString() & " of " & intTotalQuestions.ToString()
strResult += "<p>Percent Correct: " & Int(intScore/intTotalQuestions * 100).ToString() & "%"
strResult += "<p>Time Taken: " & tsTimeTaken.Minutes.ToString() & ":" & sTmp & tsTimeTaken.Seconds.ToString()
strResult += "</center>"
strResult += "<h3>Quiz Breakdown:</h3>"
For i = 1 to intTotalQuestions
strXPath = "/quiz/question[" & i.ToString() & "]"
strResult += "<b>" & i.ToString() & ". " & xDoc.SelectNodes(strXPath & "/hint").Item(0).InnerXml & "</b><br><br>"
strResult += "<img src=images//" & xDoc.SelectNodes(strXPath & "/image").Item(0).InnerXml & "><br>"
xNodeList = xDoc.SelectNodes(strXPath & "/answer")
For j = 0 to xNodeList.Count - 1
xNodeAttr = xNodeList.Item(j).Attributes.ItemOf("correct")
If Not xNodeAttr is Nothing Then
If xNodeAttr.Value = "yes" Then
strResult += xNodeList.Item(j).InnerText & "<br>"
End If
End If
Next
If arrAnswerHistory.Item(i - 1) = 0 Then
strResult += "<font color=""green""><b>Correct</b></font><br><br>"
Else
strResult += "<b>You answered:</b> " & xDoc.SelectNodes(strXPath & "/answer[" & arrAnswerHistory.Item(i-1).ToString() & "]").Item(0).InnerXml & "<br>"
strResult += "<font color=""red""><b>Incorrect</b></font><br><br>"
End If
Next
lblResults.Text = strResult
End Sub
</script>
...HTML tags...
|