#!/user/bin/perl -w

# analyze.pl by John Phillips on 9/14/2003 last revised 9/23/2003
# Program to retreive quiz results from a database and to
# generate a report of the results in XML format.


use strict;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use DBI;

# System dependent variables to be set by user

my $databaseName = "DBI:ODBC:VLESQL";
my $databaseUser = "jphillips";
my $databasePw = "atlanta";
my $columnWidth = 20;

# Local variable definitions

my $dbh;       # Database handle representing a connection to the database.
my $sth;       # Statement handle representing our prepared SQL statement.
my $q;         # Variable to hold our SQL query.
my $xml;       # Variable to hold XML output.
my $cqid;      # Computer assigned quiz id number stored in db.
my $qname;     # The name of the quiz associated with the cqid #.
my @row;       # Temp loop var to hold a row of result data.
my $item;      # Temp loop var to hold one data element.

# Start our HTML page

print header;
print start_html("John Phillips' Quiz Results Analysis Tool");
print '<h2>Quiz Results Analysis Tool</h2>';

# Establish a connection to our SQL database

$dbh = DBI->connect($databaseName, $databaseUser, $databasePw ) or
    die "Connect failed: $DBI::errstr\n";

if( ! param() ) {

  # get the cuid numbers and quiz names and allow the user to choose the one they want.

  $q = "SELECT cqid, qname FROM JPquizzes";

  $sth = $dbh->prepare( $q ) or die "prepare: $q: $DBI::errstr\n";
  $sth->execute or die "execute: $q: $DBI::errstr\n";

  print '<form name="form1" method="post" action="analyze.pl"><p>';

  while( ($cqid, $qname) = $sth->fetchrow_array() ) {
    print '<label><input type="radio" name="cqid" value="';
    print $cqid;
    print '">';
    print $qname;
    print '</label><br>';
  }

  print '</p><p>';
  print '<input type="submit" name="Submit" value="Choose Quiz"></p></form>';
}

else {
  
  # we received a parameter therefore a quiz has been selected

  $cqid = param('cqid');

  # get the quiz name for this cqid from the db

  $q = "SELECT qname FROM JPquizzes WHERE cqid = $cqid";

  $sth = $dbh->prepare( $q ) or die "prepare: $q: $DBI::errstr\n";
  $sth->execute or die "execute: $q: $DBI::errstr\n";

  if( ($qname) = $sth->fetchrow_array() ) {

    print "<h3>Quiz Name: $qname</h3>";
  
    # get all of the students' scores, answers, and times for this quiz

    $q = "SELECT u.userId, r.score, r.answers, DATEDIFF(minute, r.start, r.stop) AS elasped, ";
    $q.= "r.start, r.stop, r.qid, r.crid ";
    $q.= "FROM JPresults as r, JPuser as u ";
    $q.= "WHERE r.qid = $cqid AND r.uid = u.cuid ";
    $q.= "ORDER BY u.userId, r.start";

    $sth = $dbh->prepare( $q ) or die "prepare: $q: $DBI::errstr\n";
    $sth->execute or die "execute: $q: $DBI::errstr\n";

    # store the report in XML format for later display or saving to disk

    $xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
    $xml.= '<quiz xmlns="http://jphillips.capella.edu/namespace">' . "\n";

    # also display the report in standard HTML format on the screen
    
    print "<table bgcolor='#EEEEEE' cellpadding='10' border='0'>";
    print "<tr bgcolor='#EEEEFF'><td><b>Name</b></td><td><b>Score</b></td><td><b>Answers</b></td>";
    print "<td><b>Elasped</b></td><td><b>Start</b></td>";
    print "<td><b>Stop</b></td><td><b>qid</b></td><td><b>crid</b></td></tr>\n";

    while( @row = $sth->fetchrow_array() ) {

      # display a record of data in a table row

      print '<tr>';
      foreach $item ( @row ) {
        print "<td>$item</td>";
      }
      print "</tr>\n";

      # write XML record to string

      $xml.= "<record>\n";
      $xml.= "<name>$row[0]</name>\n";
      $xml.= "<score>$row[1]</score>\n";
      $xml.= "</record>\n";
    }

    warn "Data fetching terminated early by error: $DBI::errstr\n" if $DBI::err;

    # finish up output

    $xml.= '</quiz>' . "\n";
    print '</table>';

    print "\n<br><br><h3>XML results - choose View Source to see XML view</h3>\n";
    print "<p>This would have been written to a file but Capella's server would not allow it.</p>\n";
    print "\n$xml\n";
  }
}

# Close any open database handles

$sth->finish();
$dbh->disconnect();

print end_html;