1 Reply Latest reply on Sep 19, 2011 11:06 AM by MurraySummers

    Online Quiz

    NeilSBob

      Hello,

       

      Ive made an online quiz. There is a timer to time out the questiona and go to the next question after 30secs, but it does not go to the next question automatically. The code is here:

       

      <%@ Import Namespace="System.Xml" %>

      <script language="VB" runat="server">

       

       

      'Relative file path to XML data

      Dim strXmlFilePath as String = Server.MapPath("quiz.xml")

       

       

      Dim xDoc as XmlDocument = New XmlDocument()

      Dim intTotalQuestion as Integer

      Dim intQuestionNo 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)

       

       

                'Start a new quiz?

                If Not Page.IsPostBack Then

       

       

                          'Yes! Count total question

                          intTotalQuestion = xDoc.SelectNodes("/quiz/mchoice").Count

       

       

                          'Record start time

                          ViewState("StartTime") = DateTime.Now

       

       

                          ShowQuestion(intQuestionNo)

                End If

      End Sub

       

       

       

       

      Sub btnSubmit_Click(src as Object, e as EventArgs)

       

       

                'Retrieve essential variables from state bag

                intTotalQuestion = ViewState("TotalQuestion")

                intQuestionNo = ViewState("QuestionNo")

                intScore = ViewState("Score")

                arrAnswerHistory = ViewState("AnswerHistory")

       

       

                'Correct answer?

                If rblAnswer.SelectedItem.Value = ViewState("CorrectAnswer") Then

                          intScore += 1

                          arrAnswerHistory.Add(0)

                Else

                          arrAnswerHistory.Add(rblAnswer.SelectedItem.Value)

                End If

       

       

                'End of quiz?

                If intQuestionNo=intTotalQuestion Then

       

       

                          'Yes! Show the result...

                          QuizScreen.Visible = False

                          ResultScreen.Visible = True

       

       

                          'Render result screen

                          ShowResult()

       

       

                Else

       

       

                          'Not yet! Show another question...

                          QuizScreen.Visible = True

                          ResultScreen.Visible = False

                          intQuestionNo += 1

       

                          'Render next question

                          ShowQuestion(intQuestionNo)

                End If

      End Sub

       

       

       

       

      Sub ShowQuestion(intQuestionNo as Integer)

                Dim xNodeList as XmlNodeList

                Dim xNodeAttr as Object

                Dim strXPath as String

                Dim i as Integer

                Dim tsTimeSpent as TimeSpan

       

       

                strXPath = "/quiz/mchoice[" & intQuestionNo.ToString() & "]"

       

       

                'Extract question

                lblQuestion.Text = intQuestionNo.ToString() & ". " & xDoc.SelectSingleNode(strXPath & "/question").InnerXml

       

       

                'Extract answers

                xNodeList = xDoc.SelectNodes(strXPath & "/answer")

       

       

                'Clear previous listitems

                rblAnswer.Items.Clear

       

                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

       

       

                'Output Total Question

                lblTotalQuestion.Text = intTotalQuestion

       

       

                'Output Time Spent

                tsTimeSpent = DateTime.Now.Subtract(ViewState("StartTime"))

                lblTimeSpent.Text = tsTimeSpent.Minutes.ToString() & ":" & tsTimeSpent.Seconds.ToString()

       

       

                'Store essential data to viewstate

                ViewState("TotalQuestion") = intTotalQuestion

                ViewState("Score") = intScore

                ViewState("QuestionNo") = intQuestionNo

                ViewState("AnswerHistory") = arrAnswerHistory

       

       

      End Sub

       

       

      Sub ShowResult()

                Dim strResult as String

                Dim intCompetency as Integer

                Dim i as Integer

                Dim strXPath as String

                Dim tsTimeSpent as TimeSpan

       

                tsTimeSpent = DateTime.Now.Subtract(ViewState("StartTime"))

       

       

                strResult  = "<center>"

                strResult += "<h3>Quiz Result</h3>"

                strResult += "<p>Points: " & intScore.ToString() & " of " & intTotalQuestion.ToString()

                strResult += "<p>Your Competency: " & Int(intScore/intTotalQuestion*100).ToString() & "%"

                strResult += "</center>"

       

       

                strResult += "<h3>Quiz Breakdown:</h3>"

                For i = 1 to intTotalQuestion

                          strXPath = "/quiz/mchoice[" & i.ToString() & "]"

                          strResult += "<b>" & i.ToString() & ". " & xDoc.SelectNodes(strXPath & "/question").Item(0).InnerXml & "</b><br>"

                          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

       

       

                lblResult.Text = strResult

      End Sub

       

       

      </script>

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

      <script type="text/javascript">

      $(document).ready(function(){

                $(".chp1").click(function(){

                $(".quizlist1").slideToggle("slow");

        });

                $(".chp2").click(function(){

                $(".quizlist2").slideToggle("slow");

        });

          $(".chp3").click(function(){

                $(".quizlist3").slideToggle("slow");

        });

          $(".chp4").click(function(){

                $(".quizlist4").slideToggle("slow");

        });

          $(".chp5").click(function(){

                $(".quizlist5").slideToggle("slow");

        });

          $(".chp6").click(function(){

                $(".quizlist6").slideToggle("slow");

        });

          $(".chp7").click(function(){

                $(".quizlist7").slideToggle("slow");

        });

          $(".chp8").click(function(){

                $(".quizlist8").slideToggle("slow");

        });

      });

      </script>

      <script type="text/javascript">

      var sec = 30;   // set the seconds

      var min = 00;   // set the minutes

       

       

      function countDown() {

        sec--;

        if (sec == -01) {

          sec = 59;

          min = min - 1;

        } else {

         min = min;

        }

      if (sec<=9) { sec = "0" + sec; }

        time = (min<=9 ? "0" + min : min) + " min and " + sec + " sec ";

      if (document.getElementById) { theTime.innerHTML = time; }

        SD=window.setTimeout("countDown();", 1000);

      if (min == '00' && sec == '00') { sec = "00"; window.clearTimeout(SD); }

      }

       

       

      function addLoadEvent(func) {

        var oldonload = window.onload;

        if (typeof window.onload != 'function') {

          window.onload = func;

        } else {

          window.onload = function() {

            if (oldonload) {

              oldonload();

            }

            func();

          }

        }

      }

       

       

      addLoadEvent(function() {

        countDown();

      });

       

       

      </script>

      <html>

      <head>

      <title>Dhruv Asher | Chemistry Quiz</title>

      <link rel="stylesheet" href="style.css" type="text/css" media="screen" />

      <link rel="stylesheet" type="text/css" href="quiz.css">

      </head>

      <body>

      <div align="center" style="background-color:#333">

        <div class="wrapper">

          <div class="innerwrapper">

            <div class="header">

            <a href="http://crazypencilmedia.com/da.html"><img src="images/DA.png" style="width:264px; height:75px; float:left"></a>

            <a id="home" href="/da.html" title="Home"><span></span></a>

            <a id="about" href="#" title="About Me"><span></span></a>

            <a id="Courses" href="/courses.html" title="Courses Offered"><span></span></a>

            <a id="Forum" href="#" title="Forums"><span></span></a>

            <a id="Gallery" href="/images.html" title="Image/Video Gallery"><span></span></a>

            <a id="Students" href="#" title="Students Page"><span></span></a>

            <a id="contact" href="#" title="contact Page"><span></span></a>

            </div>

            <div class="container">

              <div class="quiz"> <span id="QuizScreen" runat="server" style="text-align:center">

                <form runat="server">

                  <div class="headingq">CHEMISTRY QUIZ</div>

                  <br/>

                  <div class="containerq">

                  <div class="instructions">Instructions: <br />

                    1. There are 30 questions for 30 marks.<br />

                    2. You have 30 seconds for each question.<br />

                    3. After you have answered the question, please click on the next button to continue. <br />

                    4. Print out a copy of the marksheet for submission. <br />

                    <br />

                    Time Left: <span id="theTime" class="timeClass"></span><br /><br/>

                  <asp:label id="lblQuestion" runat="server" />

                  </b><br>

                  <div style="padding-left:40px;">

                    <asp:radiobuttonlist

                     id="rblAnswer"

                           RepeatDirection="vertical"

                           TextAlign="right"

                           RepeatLayout="table"

                           runat="server" />

                    <br>

                    <asp:requiredfieldvalidator

                           ControlToValidate="rblAnswer"

                           ErrorMessage="Please pick an answer!"

                           runat="server" />

                    <br>

                  </div>

                  <div class="submit">

                    <asp:button id="btnSubmit" class="button" text="  Next  " onClick="btnSubmit_Click" runat="server" />

                  </div>

                  <div class="footerq">Total No. of questions:

                    <asp:label id="lblTotalQuestion" runat="server" />

                           Time spent:

                    <asp:label id="lblTimeSpent" runat="server" />

                  </div>

                </form>

                </span> <span id="ResultScreen" runat="server">

                <asp:label id="lblResult" runat="server" />

                </span> </div>

            </div>

        </div>

      </div>

      <div class="footer">

        <div style="float:left; font-size:15px; color:#FFF; margin-top:10px; margin-left:5px;"><img src="images/copy.png" width="10px" height="10px;"> 2011. Dhruv Asher. All Rights Reserved.</div>

        <div style="float:right; font-size:15px; color:#FFF; margin-top:10px; margin-right:5px;">Site Maintained by <a href="http://www.crazypencilmedia.com">Crazy Pencil Media</a></div>

      </div>

      </div>

      </div>

      </div>

      </body>

      </html>

       

      Please Help