<!--BuildTest.asp-->
<!--Copyright (c) 1998 by Charles W. McNichols. All rights reserved.-->
<!--Generate test from test bank stored as Access database-->
<HTML>
<HEAD>
<TITLE>Test Builder</TITLE>
</HEAD>
<BODY BGCOLOR="#ffffff">

<% Call Main %>

<SCRIPT RUNAT="SERVER" LANGUAGE="VBSCRIPT">

Sub Main

    Dim i
    Dim tempStr
    Dim formTopic   'form variable values - each is a requested topic
    Dim Topics()    'stores topics requested for test
    Dim topicCount  'counts topics requested for test
    Dim topicList   'for IN list in SQL statement
    Dim SQLCommand  'build SQL command for question retrieval here
    Dim QNumber     'question number for output
    Dim QFormat     'leading blanks for question number
    Dim tempIndex   'index number of question currently being processed
    Dim moveFlag    'forces move through records in defective test bank
    Dim firstFlag   'flags first response in multiple choice for default

    '[1] CREATE A LIST OF REQUESTED TOPICS FROM FORM VARIABLES
    'Topic form element names begin with TOPIC
    topicCount = 0
    For each formTopic in Request.Form
        If UCase(Left(formTopic, 5)) = "TOPIC" Then
            ReDim Preserve Topics(topicCount)
            Topics(topicCount) = Mid(formTopic, 6) 'requested topic number
            topicCount = topicCount + 1
        End If
    Next
    tempStr = ""
    topicList = ""
    For i = topicCount - 1 to 0 Step -1
        If Len(topicList) > 0 Then
            tempStr = tempStr & ", "
            topicList = topicList & ","
        End If
        tempStr = tempStr & Topics(i)
        topicList = topicList & "'" & Topics(i) & "'"
    Next
    If topicCount > 0 Then
        Response.Write("<P><B>This test includes questions from the " & _
            "following topic(s): " & tempStr & "</B></P>")
    Else
        Response.Write("<P><B>You have not selected any topics to " & _
            "include in the test. Please select one or more topics by " & _
            "clicking the appropriate check boxes, then click the " & _
            "<I>BUILD TEST</I> button." & "</B></P>")
        Exit Sub    'quit
    End If
    Response.Write("<P><B>Please enter your responses, then click the " & _
        "button labeled <I>SUBMIT RESPONSES FOR GRADING</I> at " & _
        "the end of the test. You may change your choices at any time " & _
        "before they are submitted.</B></P>" & vbNewLine)

    '[2] Test bank ODBC DSN from home page
    TestBank = Request.Form("TestBankDSN")

    '[3] SETUP THE TEST AS AN HTML FORM 
    Response.Write("<FORM METHOD=""POST"" " & _
        "ACTION=""/scripts/GradeTest.asp"">")

    '[4] Pass the test bank DSN on to GradeTest.asp as hidden form var.
    Response.Write("<INPUT TYPE=""hidden"" NAME=""TestBankDSN""" & _
        "VALUE=""" & TestBank & """>")

    'PASS THE EXPLANATION FLAG ON TO THE GradeTest.asp PROGRAM
    tempStr = Request.Form("Explanation")
    If Len(tempStr) = 0 Then    'default is always output explanations
        tempStr = "Always"
    End If
    Response.Write("<INPUT TYPE=""hidden"" NAME=""Explanation"" " & _
        "VALUE=""" & tempStr & """>")

    '[5] OPEN TEST DB AND EXTRACT QUESTIONS
    SQLCommand = "SELECT Question.Index,MatchIndex,QType,QuestionText," & _
        "Response,ResponseText FROM question LEFT OUTER JOIN answer " & _
        "ON question.index=answer.index WHERE topic " & _
        "IN  (" & topicList & ") ORDER BY Question.Index,Response"

    Set objConn = Server.CreateObject("ADODB.Connection")
    objConn.Open TestBank  'ODBC DSN for test bank

    Set objRS = objConn.Execute(SQLCommand) 'get question data

    '[6] PRODUCE THE TEST
    QNumber = 1
    Do While Not objRS.EOF
        moveFlag = False    'have not advanced a row in test bank yet
        QFormat = ""
        If QNumber < 100 Then
            QFormat = "&nbsp;&nbsp;"
            If QNumber < 10 Then
                QFormat = QFormat & "&nbsp;&nbsp;"
            End If
        End If
        '[7] Each question is presented as a table
        Select Case objRS("QType") 
            Case 1  'True/False
                Response.Write("<TABLE CELLSPACING=""0"" CELLPADDING=""0"">")
                'Output the question stem
                Response.Write("<TR><TD VALIGN=""TOP"">" & QFormat & _
                    QNumber & ". <TD>" & objRS("QuestionText"))
                QNumber = QNumber + 1   'next question number
                'Then the response set
                tempIndex = objRS("Index")
                Response.Write("<TR><TD ALIGN=""CENTER""><INPUT " & _
                    "TYPE=""radio"" NAME=""" & tempIndex & _
                    """ VALUE=""A"" CHECKED><TD>A. True")
                Response.Write("<TR><TD ALIGN=""CENTER""><INPUT " & _
                    "TYPE=""radio"" NAME=""" &  tempIndex & _
                    """ VALUE=""B""><TD>B. False")
                objRs.MoveNext  'there are no responses stored for T/F
                moveFlag = True 'row advanced in test bank
                Response.Write("</TABLE>")
                Response.Write("<BR>" & vbNewLine)
            Case 2  '[8] Multiple choice
                'Outer table for question number/control and stem
                Response.Write("<TABLE CELLPADDING=""0"" CELLSPACING=""0"">")
                'Output the question stem
                Response.Write("<TR><TD VALIGN=""TOP"">" & QFormat & _
                    QNumber & ". <TD>" & objRS("QuestionText"))
                QNumber = QNumber + 1   'next question number
                'Output the response set
                tempIndex = objRS("Index")
                firstFlag = False
                Do While Not objRS.EOF '[9] process responses
                    If objRS("Index") <> tempIndex Then
                        Exit Do
                    End If
                    If Not firstFlag Then
                        tempStr = "CHECKED" 'first response is default
                        firstFlag = True
                    Else
                        tempStr = ""
                    End If
                    Response.Write("<TR><TD ALIGN=""CENTER"">" & _
                        "<INPUT TYPE=""radio"" NAME=""" & _
                        tempIndex & """ VALUE=""" & objRS("Response") & _
                        """" & tempStr & ">")
                    '[10] Inner table for response letter and response text
                    Response.Write("<TD><TABLE CELLPADDING=""0"" " & _
                        "CELLSPACING=""0""><TR><TD VALIGN=""TOP"">" & _
                        objRS("Response") & ". </TD><TD>" & _
                        objRS("ResponseText") & "</TABLE>")
                    i = i + 1
                    objRs.MoveNext
                    moveFlag = True 'row advanced in test bank
                Loop    'multiple choice response loop
                Response.Write("</TABLE>")  'table for entire question
                Response.Write("<BR>" & vbNewLine)
            Case 3  'Multiple-multiple choice
                tempIndex = objRS("Index")
                'Following hidden element makes sure question is included
                'for grading even if no responses are selected
                Response.Write("<INPUT TYPE=""hidden"" NAME=""" & _
                        tempIndex & """>")
                'outer table for question number/control and stem
                Response.Write("<TABLE CELLPADDING=""0"" CELLSPACING=""0"">")
                'Output the question stem
                Response.Write("<TR><TD VALIGN=""TOP"">" & QFormat & _
                    QNumber & ". <TD>" & objRS("QuestionText"))
                QNumber = QNumber + 1   'next question number
                'Output the response set
                Do While Not objRS.EOF
                    If objRS("Index") <> tempIndex Then
                        Exit Do
                    End If
                    Response.Write("<TR><TD ALIGN=""CENTER"">" & _
                        "<INPUT TYPE=""checkbox"" NAME=""" & _
                        tempIndex & """ VALUE=""" & objRS("Response") & _
                        """>")
                    'Inner table for response letter and response
                    Response.Write("<TD><TABLE CELLPADDING=""0"" " & _
                        "CELLSPACING=""0"">" & _
                        "<TR><TD VALIGN=""TOP"">" & objRS("Response") & _
                        ". </TD><TD>" & objRS("ResponseText") & "</TABLE>")
                    i = i + 1
                    objRs.MoveNext
                    moveFlag = True 'row advanced in test bank
                Loop    'multiple-multiple choice response loop
                Response.Write("</TABLE>")  'table for entire question
                Response.Write("<BR>" & vbNewLine)
            Case 4  'Matching
                'Output hidden value for stem
                tempIndex = objRS("Index")
                Response.Write("<INPUT TYPE=""hidden"" NAME=""" & _
                    tempIndex & """VALUE=""0"">")
                'Output the matching question stem of 1st question (no number)
                Response.Write(objRS("QuestionText"))
                'Then the response set. Only the 1st question has these
                Response.Write("<TABLE CELLSPACING=""0"" CELLPADDING=""0"">")
                Do While Not objRS.EOF
                    If objRS("Index") <> tempIndex Then
                        Exit Do
                    End If
                    Response.Write("<TR><TD><TD VALIGN=""TOP"">" & _
                        objRS("Response") & ". <TD COLSPAN=""2"">" & _
                        objRS("ResponseText") & "<TD>")
                    objRS.MoveNext
                    moveFlag = True 'row advanced in test bank
                    i = i + 1
                Loop    'response loop - values to match
                Response.Write("<TR><TD>&nbsp;<TD>") 'blank row in table
                'Output the questions for the match
                Do While Not objRS.EOF
                    'Test below required because MatchIndex could be null
                    'due to bad test bank -- would result in infinite loop
                    If objRS("MatchIndex") <> tempIndex Or _
                        objRS("QType") <> 4  Then
                        Exit Do
                    End If
                    Response.Write("<TR><TD VALIGN=""TOP"">" & QFormat _
                        & QNumber & ". " & "<TD VALIGN=""TOP"">" & _
                        "<INPUT TYPE=""text"" MAXLENGTH=""1"" SIZE=""1"" " & _
                        " NAME=""" & objRS("Index") & """><TD>" & _
                        objRS("QuestionText"))
                    QNumber = QNumber + 1   'next question number
                    objRS.MoveNext
                    moveFlag = True 'row advanced in test bank
                Loop
                Response.Write("</TABLE>")
                Response.Write("<BR>" & vbNewLine)
            Case Else
                objRS.MoveNext  'skip record: can't process this QType
                moveFlag = True
        End Select
        If Not moveFlag Then    'force row advance in defective test bank
            objRS.MoveNext
        End If
    Loop    'end of loop through recordset

    '[11] TEST GENERATED, CLOSE DATABASE AND GENERATE SUBMIT BUTTON
    objRS.Close         'close the recordset
    objConn.Close       'close the connection

    Response.Write("<CENTER><INPUT TYPE=""submit"" VALUE=""SUBMIT " & _
        "RESPONSES FOR GRADING""></CENTER>")
    Response.Write("</FORM>")

End Sub 'Main
</SCRIPT>

</BODY>
</HTML>