<%page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" %>
<%
@ include file="../Connections/cnJP.jsp" %>
<%
  
// gradeit.jsp by John Phillips on 9/13/2003
  // This jsp script is called from showquiz.jsp. Its purpose is to grade 
  // the quiz the user has submitted and to record the results.

  // Retrieve the params passed from showquiz.jsp.
  // cqid is the primary key for the quizzes table (computer quiz id)
  // cuid is the primary key for the user table (computer user id)
  
String cqid request.getParameter("cqid");
  
String cuid request.getParameter("cuid");
  
  
// connect to the database
  
Driver driverRS = (Driver)Class.forName(MM_cnJP_DRIVER).newInstance();
  
Connection connRS DriverManager.getConnection(MM_cnJP_STRING,MM_cnJP_USERNAME,MM_cnJP_PASSWORD);

  
// look up answers from table JPquizzes
  
String q = new String"SELECT qname, answers FROM JPquizzes WHERE cqid = '" cqid "'" );
  
PreparedStatement ps connRS.prepareStatement);
  
ResultSet rs ps.executeQuery();
  if( 
rs.next() ) {
    
String qname rs.getString("qname");    
    
String answers rs.getString("answers");
    
    
// determine length of answers which is also equal to the number of questions/answers
    
int numAnswers answers.length();

    
// retrieve proper number of answers from form and store in a String
    // also compare to real answers and count number right and wrong
    
String allStudentAnswers "";
    
int correct 0;
    
int wrong 0;
    for( 
int i=1i<=numAnswersi++ ) {
      
String studentAnswer request.getParameter( ("a" i) );
      if( 
studentAnswer == null ) {
        
studentAnswer "z";
      }
      if( 
studentAnswer.charAt(0) == answers.charAt(i-1) ) {
        
correct++;
      }
      else {
        
wrong++;
      }
      
allStudentAnswers += studentAnswer;
    }
  
    
// calculate grade
    
int score 0;
    
score = (int) Math.roundcorrect 100.0numAnswers ); 
    
    
// find the most recent results record for this quiz and this user
    
"SELECT crid FROM JPresults WHERE qid = " cqid " AND uid = " cuid " ORDER BY start DESC";
    
ps connRS.prepareStatement);
    
rs ps.executeQuery();
    if( 
rs.next() ) {
      
int crid rs.getInt("crid");
      
      
// store results in JPresults table in this user's current record
      
"UPDATE JPresults SET stop = GETDATE(), answers = '" allStudentAnswers "', score = " score +
        
" WHERE crid = " crid;
      
ps connRS.prepareStatement);
      
ps.executeUpdate();
    }
    else {
      
out.print( "There was an error (gradeit#1) with this quiz. Please inform the instructor." );
    }    
    
    
// display grade to student
    
%>
    <html>
    <head>
    <title>Grade It</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>    
    <h2>Quiz Results</h2>
    <p>Quiz name: <%= qname %></p>
    <p>Correct: <%= correct %></p>
    <p>Incorrect: <%= wrong %></p>
    <p>Score: <%= score %></p>
    <p>Your instructor will discuss the quiz answers after all students have completed the quiz.</p>
    <p><a href="p2form.html">Click here to continue</a></p>
    </body>
    </html>
    <%
  

  else {
    
out.print( "There was an error (gradeit#2) with this quiz. Please inform the instructor." );
  }
  
  
rs.close();
  
ps.close();
  
connRS.close();
%>