• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Week: 3 At a Glance > Day 19—Quiz Question Screen (quiz.asp)

Day 19—Quiz Question Screen (quiz.asp)

When quizzing the user, the program must know what quiz item is being displayed in order to show the possible answers for that question. We made this possible by basing one of the recordsets on a previous query that returned the question ID. The recordset definitions also needed to be rearranged in order to make them execute in the correct order. Additionally, custom Show Region code was added to indicate that the quiz had been finished. The final code for the question generation screen is shown here:

<%@LANGUAGE="VBSCRIPT"%> <!--#include file="../Connections/connChapter19.asp"
--><%
' *** Restrict Access To Page: Grant or deny access to this page
MM_authorizedUsers="student,teacher"
MM_authFailedURL="index.asp"
MM_grantAccess=false
If Session("MM_Username") <> "" Then
  If (false Or CStr(Session("MM_UserAuthorization"))="") Or _
         (InStr(1,MM_authorizedUsers,Session("MM_UserAuthorization"))>=1) Then
    MM_grantAccess = true
  End If
End If
If Not MM_grantAccess Then
  MM_qsChar = "?"
  If (InStr(1,MM_authFailedURL,"?") >= 1) Then MM_qsChar = "&"
  MM_referrer = Request.ServerVariables("URL")
  if (Len(Request.QueryString()) > 0) Then MM_referrer = MM_referrer & "?" &
Request.QueryString()
  MM_authFailedURL = MM_authFailedURL & MM_qsChar & "accessdenied=" &
Server.URLEncode(MM_referrer)
  Response.Redirect(MM_authFailedURL)
End If
%> <%
' *** Edit Operations: declare variables

MM_editAction = CStr(Request("URL"))
If (Request.QueryString <> "") Then
  MM_editAction = MM_editAction & "?" & Request.QueryString
End If
' boolean to abort record edit
MM_abortEdit = false

' query string to execute
MM_editQuery = ""
%> <%
' *** Insert Record: set variables

If (CStr(Request("MM_insert")) <> "") Then

  MM_editConnection = MM_connChapter19_STRING
  MM_editTable = "response"
  MM_editRedirectUrl = ""
  MM_fieldsStr  = "answerID|value|questionID|value|username|value"
  MM_columnsStr =
"answerID|none,none,NULL|questionID|none,none,NULL|username|',none,''"

  ' create the MM_fields and MM_columns arrays
  MM_fields = Split(MM_fieldsStr, "|")
  MM_columns = Split(MM_columnsStr, "|")

  ' set the form values
  For i = LBound(MM_fields) To UBound(MM_fields) Step 2
    MM_fields(i+1) = CStr(Request.Form(MM_fields(i)))
  Next

  ' set the redirect URL
  If (MM_editRedirectUrl = "") Then
    MM_editRedirectUrl = CStr(Request("URL"))
  End If
  If (Request.QueryString <> "") Then
    If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0 And
Request.QueryString <> "") Then
      MM_editRedirectUrl = MM_editRedirectUrl & "?" & Request.QueryString
    Else
      MM_editRedirectUrl = MM_editRedirectUrl & "&" & Request.QueryString
    End If
  End If

End If
%> <%
' *** Insert Record: construct a sql insert statement and execute it

If (CStr(Request("MM_insert")) <> "") Then

  ' create the sql insert statement
  MM_tableValues = ""
  MM_dbValues = ""
  For i = LBound(MM_fields) To UBound(MM_fields) Step 2
    FormVal = MM_fields(i+1)
    MM_typeArray = Split(MM_columns(i+1),",")
    Delim = MM_typeArray(0)
    If (Delim = "none") Then Delim = ""
    AltVal = MM_typeArray(1)
    If (AltVal = "none") Then AltVal = ""
    EmptyVal = MM_typeArray(2)
    If (EmptyVal = "none") Then EmptyVal = ""
    If (FormVal = "") Then
      FormVal = EmptyVal
    Else
      If (AltVal <> "") Then
        FormVal = AltVal
      ElseIf (Delim = "'") Then  ' escape quotes
        FormVal = "'" & Replace(FormVal,"'","''") & "'"
      Else
        FormVal = Delim + FormVal + Delim
      End If
    End If
    If (i <> LBound(MM_fields)) Then
      MM_tableValues = MM_tableValues & ","
      MM_dbValues = MM_dbValues & ","
    End if
    MM_tableValues = MM_tableValues & MM_columns(i)
    MM_dbValues = MM_dbValues & FormVal
  Next
  MM_editQuery = "insert into " & MM_editTable & " (" & MM_tableValues & ")
values (" & MM_dbValues & ")"

  If (Not MM_abortEdit) Then
    ' execute the insert
    Set MM_editCmd = Server.CreateObject("ADODB.Command")
    MM_editCmd.ActiveConnection = MM_editConnection
    MM_editCmd.CommandText = MM_editQuery
    MM_editCmd.Execute

    If (MM_editRedirectUrl <> "") Then
      Call Response.Redirect(MM_editRedirectUrl)
    End If
  End If

End If
%>
<%
set rsQuestion = Server.CreateObject("ADODB.Recordset")
rsQuestion.ActiveConnection = MM_connChapter19_STRING
rsQuestion.Source = "SELECT *  FROM question"
rsQuestion.CursorType = 0
rsQuestion.CursorLocation = 2
rsQuestion.LockType = 3
rsQuestion.Open()
rsQuestion_numRows = 0
%>
<%
set rsResponse = Server.CreateObject("ADODB.Recordset")
rsResponse.ActiveConnection = MM_connChapter19_STRING
rsResponse.Source = "SELECT *  FROM response"
rsResponse.CursorType = 0
rsResponse.CursorLocation = 2
rsResponse.LockType = 3
rsResponse.Open()
rsResponse_numRows = 0
%> <%
Dim rsCorrect__varUsername
rsCorrect__varUsername = "jray"
if (Session("MM_Username") <> "") then rsCorrect__varUsername =
Session("MM_Username")
%>
<%
set rsCorrect = Server.CreateObject("ADODB.Recordset")
rsCorrect.ActiveConnection = MM_connChapter19_STRING
rsCorrect.Source = "SELECT *  FROM response,question  WHERE response.username='"
+ Replace(rsCorrect__varUsername, "'", "''") + "' AND
response.answerID=question.answerID AND response.questionID=question.questionID"
rsCorrect.CursorType = 0
rsCorrect.CursorLocation = 2
rsCorrect.LockType = 3
rsCorrect.Open()
rsCorrect_numRows = 0
%>
<%
Dim rsResponseAll__MMColParam
rsResponseAll__MMColParam = "0"
if (Session("MM_Username") <> "") then rsResponseAll__MMColParam =
Session("MM_Username")
%> <%
set rsResponseAll = Server.CreateObject("ADODB.Recordset")
rsResponseAll.ActiveConnection = MM_connChapter19_STRING
rsResponseAll.Source = "SELECT * FROM response WHERE username = '" +
Replace(rsResponseAll__MMColParam, "'", "''") + "'"
rsResponseAll.CursorType = 0
rsResponseAll.CursorLocation = 2
rsResponseAll.LockType = 3
rsResponseAll.Open()
rsResponseAll_numRows = 0
%>
<%
'  *** Recordset Stats, Move To Record, and Go To Record: declare stats
variables

' set the record count
rsQuestion_total = rsQuestion.RecordCount
' set the number of rows displayed on this page
If (rsQuestion_numRows < 0) Then
  rsQuestion_numRows = rsQuestion_total
Elseif (rsQuestion_numRows = 0) Then
  rsQuestion_numRows = 1
End If

' set the first and last displayed record
rsQuestion_first = 1
rsQuestion_last  = rsQuestion_first + rsQuestion_numRows - 1

' if we have the correct record count, check the other stats
If (rsQuestion_total <> -1) Then
  If (rsQuestion_first > rsQuestion_total) Then rsQuestion_first =
rsQuestion_total
  If (rsQuestion_last > rsQuestion_total) Then rsQuestion_last = rsQuestion_total
  If (rsQuestion_numRows > rsQuestion_total) Then rsQuestion_numRows =
rsQuestion_total
End If
%> <%
'  *** Recordset Stats, Move To Record, and Go To Record: declare stats
variables

' set the record count
rsCorrect_total = rsCorrect.RecordCount

' set the number of rows displayed on this page
If (rsCorrect_numRows < 0) Then
  rsCorrect_numRows = rsCorrect_total
Elseif (rsCorrect_numRows = 0) Then
  rsCorrect_numRows = 1
End If

' set the first and last displayed record
rsCorrect_first = 1
rsCorrect_last  = rsCorrect_first + rsCorrect_numRows - 1

' if we have the correct record count, check the other stats
If (rsCorrect_total <> -1) Then
  If (rsCorrect_first > rsCorrect_total) Then rsCorrect_first = rsCorrect_total
  If (rsCorrect_last > rsCorrect_total) Then rsCorrect_last = rsCorrect_total
  If (rsCorrect_numRows > rsCorrect_total) Then rsCorrect_numRows =
rsCorrect_total
End If
%> <%
'  *** Recordset Stats, Move To Record, and Go To Record: declare stats
variables

' set the record count
rsResponseAll_total = rsResponseAll.RecordCount
' set the number of rows displayed on this page
If (rsResponseAll_numRows < 0) Then
  rsResponseAll_numRows = rsResponseAll_total
Elseif (rsResponseAll_numRows = 0) Then
  rsResponseAll_numRows = 1
End If

' set the first and last displayed record
rsResponseAll_first = 1
rsResponseAll_last  = rsResponseAll_first + rsResponseAll_numRows - 1

' if we have the correct record count, check the other stats
If (rsResponseAll_total <> -1) Then
  If (rsResponseAll_first > rsResponseAll_total) Then rsResponseAll_first =
rsResponseAll_total
  If (rsResponseAll_last > rsResponseAll_total) Then rsResponseAll_last =
rsResponseAll_total
  If (rsResponseAll_numRows > rsResponseAll_total) Then rsResponseAll_numRows =
rsResponseAll_total
End If
%> <%
' *** Recordset Stats: if we don't know the record count, manually count them

If (rsQuestion_total = -1) Then

  ' count the total records by iterating through the recordset
  rsQuestion_total=0
  While (Not rsQuestion.EOF)
    rsQuestion_total = rsQuestion_total + 1
    rsQuestion.MoveNext
  Wend

  ' reset the cursor to the beginning
  If (rsQuestion.CursorType > 0) Then
    rsQuestion.MoveFirst
  Else
    rsQuestion.Requery
  End If

  ' set the number of rows displayed on this page
  If (rsQuestion_numRows < 0 Or rsQuestion_numRows > rsQuestion_total) Then
    rsQuestion_numRows = rsQuestion_total
  End If

  ' set the first and last displayed record
  rsQuestion_first = 1
  rsQuestion_last = rsQuestion_first + rsQuestion_numRows - 1
  If (rsQuestion_first > rsQuestion_total) Then rsQuestion_first =
rsQuestion_total
  If (rsQuestion_last > rsQuestion_total) Then rsQuestion_last = rsQuestion_total

End If
%> <%
' *** Recordset Stats: if we don't know the record count, manually count them

If (rsCorrect_total = -1) Then

  ' count the total records by iterating through the recordset
  rsCorrect_total=0
  While (Not rsCorrect.EOF)
    rsCorrect_total = rsCorrect_total + 1
    rsCorrect.MoveNext
  Wend

  ' reset the cursor to the beginning
  If (rsCorrect.CursorType > 0) Then
    rsCorrect.MoveFirst
  Else
    rsCorrect.Requery
  End If

  ' set the number of rows displayed on this page
  If (rsCorrect_numRows < 0 Or rsCorrect_numRows > rsCorrect_total) Then
    rsCorrect_numRows = rsCorrect_total
  End If

  ' set the first and last displayed record
  rsCorrect_first = 1
  rsCorrect_last = rsCorrect_first + rsCorrect_numRows - 1
  If (rsCorrect_first > rsCorrect_total) Then rsCorrect_first = rsCorrect_total
  If (rsCorrect_last > rsCorrect_total) Then rsCorrect_last = rsCorrect_total

End If
%> <%
' *** Recordset Stats: if we don't know the record count, manually count them

If (rsResponseAll_total = -1) Then

  ' count the total records by iterating through the recordset
  rsResponseAll_total=0
  While (Not rsResponseAll.EOF)
    rsResponseAll_total = rsResponseAll_total + 1
    rsResponseAll.MoveNext
  Wend

  ' reset the cursor to the beginning
  If (rsResponseAll.CursorType > 0) Then
    rsResponseAll.MoveFirst
  Else
    rsResponseAll.Requery
  End If

  ' set the number of rows displayed on this page
  If (rsResponseAll_numRows < 0 Or rsResponseAll_numRows > rsResponseAll_total)
Then
    rsResponseAll_numRows = rsResponseAll_total
  End If

  ' set the first and last displayed record
  rsResponseAll_first = 1
  rsResponseAll_last = rsResponseAll_first + rsResponseAll_numRows - 1
  If (rsResponseAll_first > rsResponseAll_total) Then rsResponseAll_first =
rsResponseAll_total
  If (rsResponseAll_last > rsResponseAll_total) Then rsResponseAll_last =
rsResponseAll_total

End If
%> <%
' *** Move To Record and Go To Record: declare variables

Set MM_rs    = rsQuestion
MM_rsCount   = rsQuestion_total
MM_size      = rsQuestion_numRows
MM_uniqueCol = ""
MM_paramName = ""
MM_offset = 0
MM_atTotal = false
MM_paramIsDefined = false
If (MM_paramName <> "") Then
  MM_paramIsDefined = (Request.QueryString(MM_paramName) <> "")
End If
%> <%
' *** Move To Record: handle 'index' or 'offset' parameter

if (Not MM_paramIsDefined And MM_rsCount <> 0) then

  ' use index parameter if defined, otherwise use offset parameter
  r = Request.QueryString("index")
  If r = "" Then r = Request.QueryString("offset")
  If r <> "" Then MM_offset = Int(r)

  ' if we have a record count, check if we are past the end of the recordset
  If (MM_rsCount <> -1) Then
    If (MM_offset >= MM_rsCount Or MM_offset = -1) Then  ' past end or move last
      If ((MM_rsCount Mod MM_size) > 0) Then         ' last page not a full
repeat region
        MM_offset = MM_rsCount - (MM_rsCount Mod MM_size)
      Else
        MM_offset = MM_rsCount - MM_size
      End If
    End If
  End If

  ' move the cursor to the selected record
  i = 0
  While ((Not MM_rs.EOF) And (i < MM_offset Or MM_offset = -1))
    MM_rs.MoveNext
    i = i + 1
  Wend
  If (MM_rs.EOF) Then MM_offset = i  ' set MM_offset to the last possible record

End If
%> <%
' *** Move To Record: if we dont know the record count, check the display range

If (MM_rsCount = -1) Then

  ' walk to the end of the display range for this page
  i = MM_offset
  While (Not MM_rs.EOF And (MM_size < 0 Or i < MM_offset + MM_size))
    MM_rs.MoveNext
    i = i + 1
  Wend

  ' if we walked off the end of the recordset, set MM_rsCount and MM_size
  If (MM_rs.EOF) Then
    MM_rsCount = i
    If (MM_size < 0 Or MM_size > MM_rsCount) Then MM_size = MM_rsCount
  End If

  ' if we walked off the end, set the offset based on page size
  If (MM_rs.EOF And Not MM_paramIsDefined) Then
    If (MM_offset > MM_rsCount - MM_size Or MM_offset = -1) Then
      If ((MM_rsCount Mod MM_size) > 0) Then
        MM_offset = MM_rsCount - (MM_rsCount Mod MM_size)
      Else
        MM_offset = MM_rsCount - MM_size
      End If
    End If
  End If

  ' reset the cursor to the beginning
  If (MM_rs.CursorType > 0) Then
    MM_rs.MoveFirst
  Else
    MM_rs.Requery
  End If

  ' move the cursor to the selected record
  i = 0
  While (Not MM_rs.EOF And i < MM_offset)
    MM_rs.MoveNext
    i = i + 1
  Wend
End If
%> <%
set rsAnswer = Server.CreateObject("ADODB.Recordset")
rsAnswer.ActiveConnection = MM_connChapter19_STRING
rsAnswer.Source = "SELECT *  FROM answer WHERE questionID='" &
rsQuestion.Fields.Item("questionID").Value & "'"
rsAnswer.CursorType = 0
rsAnswer.CursorLocation = 2
rsAnswer.LockType = 3
rsAnswer.Open()
rsAnswer_numRows = 0
%>
 <%
' *** Move To Record: update recordset stats

' set the first and last displayed record
rsQuestion_first = MM_offset + 1
rsQuestion_last  = MM_offset + MM_size
If (MM_rsCount <> -1) Then
  If (rsQuestion_first > MM_rsCount) Then rsQuestion_first = MM_rsCount
  If (rsQuestion_last > MM_rsCount) Then rsQuestion_last = MM_rsCount
End If

' set the boolean used by hide region to check if we are on the last record
MM_atTotal = (MM_rsCount <> -1 And MM_offset + MM_size >= MM_rsCount)
%> <%
' *** Go To Record and Move To Record: create strings for maintaining URL and
Form parameters

' create the list of parameters which should not be maintained
MM_removeList = "&index="
If (MM_paramName <> "") Then MM_removeList = MM_removeList & "&" & MM_paramName &
"="
MM_keepURL="":MM_keepForm="":MM_keepBoth="":MM_keepNone=""

' add the URL parameters to the MM_keepURL string
For Each Item In Request.QueryString
  NextItem = "&" & Item & "="
  If (InStr(1,MM_removeList,NextItem,1) = 0) Then
    MM_keepURL = MM_keepURL & NextItem &
Server.URLencode(Request.QueryString(Item))
  End If
Next

' add the Form variables to the MM_keepForm string
For Each Item In Request.Form
  NextItem = "&" & Item & "="
  If (InStr(1,MM_removeList,NextItem,1) = 0) Then
    MM_keepForm = MM_keepForm & NextItem & Server.URLencode(Request.Form(Item))
  End If
Next

' create the Form + URL string and remove the intial '&' from each of the
strings
MM_keepBoth = MM_keepURL & MM_keepForm
if (MM_keepBoth <> "") Then MM_keepBoth = Right(MM_keepBoth, Len(MM_keepBoth) -
1)
if (MM_keepURL <> "")  Then MM_keepURL  = Right(MM_keepURL, Len(MM_keepURL) - 1)
if (MM_keepForm <> "") Then MM_keepForm = Right(MM_keepForm, Len(MM_keepForm) -
1)

' a utility function used for adding additional parameters to these strings
Function MM_joinChar(firstItem)
  If (firstItem <> "") Then
    MM_joinChar = "&"
  Else
    MM_joinChar = ""
  End If
End Function
%> <%
' *** Move To Record: set the strings for the first, last, next, and previous
links

MM_keepMove = MM_keepBoth
MM_moveParam = "index"

' if the page has a repeated region, remove 'offset' from the maintained
parameters
If (MM_size > 0) Then
  MM_moveParam = "offset"
  If (MM_keepMove <> "") Then
    params = Split(MM_keepMove, "&")
    MM_keepMove = ""
    For i = 0 To UBound(params)
      nextItem = Left(params(i), InStr(params(i),"=") - 1)
      If (StrComp(nextItem,MM_moveParam,1) <> 0) Then
        MM_keepMove = MM_keepMove & "&" & params(i)
      End If
    Next
    If (MM_keepMove <> "") Then
      MM_keepMove = Right(MM_keepMove, Len(MM_keepMove) - 1)
    End If
  End If
End If

' set the strings for the move to links
If (MM_keepMove <> "") Then MM_keepMove = MM_keepMove & "&"
urlStr = Request.ServerVariables("URL") & "?" & MM_keepMove & MM_moveParam & "="
MM_moveFirst = urlStr & "0"
MM_moveLast  = urlStr & "-1"
MM_moveNext  = urlStr & Cstr(MM_offset + MM_size)
prev = MM_offset - MM_size
If (prev < 0) Then prev = 0
MM_movePrev  = urlStr & Cstr(prev)
%>
<html>
<head>
<title>Quiz Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF">
<form name="saveResponse" method="post" action="<%=MM_moveNext%>">
  <% If rsResponseAll_total <> rsQuestion_total Then %>
  <table width="100%" border="0" cellspacing="0" cellpadding="2">
    <tr align="center" bgcolor="#CCCCFF">
      <td><a href="admin1.asp"><font face="Arial, Helvetica,
sans-serif">Admin</font></a></td>
      <td>
        <p><font face="Arial, Helvetica, sans-serif" size="+1">Quiz Question
</font></p>
        <p><font face="Arial, Helvetica, sans-serif" size="+1">(You've Answered
          <%=(rsResponseAll_total)%>/<%=(rsQuestion_total)%>)<br>
          Topic:
<b><%=(rsQuestion.Fields.Item("questionTitle").Value)%></b></font></p>
      </td>
    </tr>
    <tr bgcolor="#FFCC99">
      <td width="17%"><font face="Arial, Helvetica, sans-serif">The
Question:</font></td>
      <td width="83%"><font face="Arial, Helvetica,
sans-serif"><%=(rsQuestion.Fields.Item("questionText").Value)%></font></td>
    </tr>
    <tr bgcolor="#FFCC99">
      <td width="17%"><font face="Arial, Helvetica, sans-serif">Your
Answer:</font></td>
      <td width="83%">
        <select name="answerID">
          <%
While (NOT rsAnswer.EOF)
%>
          <option
value="<%=(rsAnswer.Fields.Item("answerID").Value)%>"><%=(rsAnswer.Fields.Item("answerText").Value)%></option>
          <%
rsAnswer.MoveNext()
Wend
%>
        </select>
      </td>
    </tr>
    <tr align="center" bgcolor="#CCCCFF">
      <td><A HREF="<%=MM_moveNext%>">SKIP
        QUESTION</A></td>
      <td>
        <input type="submit" name="Submit" value="Submit Answer">
        <input type="hidden" name="questionID"
value="<%=rsQuestion.Fields.Item("questionID").Value%>">
        <input type="hidden" name="username"
value="<%=Session("MM_Username")%>">
        <input type="hidden" name="MM_insert" value="true">
      </td>
    </tr>
  </table>
  <% End If %>
  <% If rsResponseAll_total = rsQuestion_total Then %>
  Congratulations! You've finished the quiz! <% End If %>
</form>
<%=(rsCorrect_total)%>
</body>
</html>
<%
rsQuestion.Close()
%> <%
rsResponse.Close()
%> <%
rsAnswer.Close()
%> <%
rsCorrect.Close()
%> <%
rsResponseAll.Close()
%>


					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint