Location: PHPKode > projects > Standardized Test Practice Engine > stpe/admin/viewresults.php
<?php
/*
Copyright 2004 by Jonathan Bell and Daniel Perelman
This file is part of STPE - the Standardized Test Practice Engine.

    STPE is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    STPE is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
include_once("../globals.inc.php");
?>
<?php
include("login.inc.php");

include_once("../dbinit.inc.php");

$testresid = $_GET['id'];

$test_result_sql = "SELECT * FROM `answers` WHERE ID = $testresid;";
$test_result = mysql_query($test_result_sql);
$test_result_row = mysql_fetch_assoc($test_result);

//echo("\n" . '<!-- $test_result_row: ');
//print_r($test_result_row);
//echo("-->\n");

$time_taken = $test_result_row['dts'];
$test_user_id = $test_result_row['user_id'];
$time_start = $test_result_row['starttime'];
$post_text = $test_result_row['answers'];
$post_array = explode("\n", $post_text);
foreach($post_array as $value)
{
	$postvar = explode("\r", $value);
	$_POST[$postvar[0]] = $postvar[1];
}

//echo("\n" . '<!-- $_POST: ');
//print_r($_POST);
//echo("-->\n");

$user_name = $_POST['student_name'];

$wantedtopic = $_POST['top'];
$wantedsection = $_POST['sub'];

$sql = "SELECT * FROM `users` WHERE topicnum=$wantedtopic && sectionnum=$wantedsection";
$total_result = @mysql_query($sql, $connection) or die("Error #". mysql_errno() . ": " . mysql_error());
$total_found = @mysql_num_rows($total_result);

while ($row = mysql_fetch_array($total_result)) {
	$groupid=$row['group_id'];
    $topicnum=$row['topicnum'];
    $topicdesc=$row['topicdesc'];
    $sectionnum=$row['sectionnum'];
    $sectiondesc=$row['sectiondesc'];
    $names=$row['names'];
    $code=$row['code'];
	$x++;
}

$sql = "SELECT * FROM `questions` where group_id = $groupid ORDER by id";
$total_result = @mysql_query($sql, $connection) or die("Error #". mysql_errno() . ": " . mysql_error());
$total_found = @mysql_num_rows($total_result);
$x=0;
unset($id);
while ($row = mysql_fetch_assoc($total_result)) {
    $id[$x]=$row['id'];
    $question[$x]=$row['question'];
    $answers[$x]=$row['answers'];
    $ans[$x]=$row['ans'];
    $type[$x]=$row['type'];
    $x++;
}

//get the number of matching questions
$numberofmatch=0;
foreach($type as $x){
	if($x==2){
	$numberofmatch++;
	}
}
$numberoftype2=0;
$numberoftype3=0;

foreach($type as $sd){
if($sd==2){
$numberoftype2++;
}
if($sd==3){
$numberoftype3++;
}
}
if($numberoftype2>0){
$numberoftype2--;
}

//get the number of matching questions
$numberofmatch=0;
foreach($type as $x){
	if($x==2){
	$numberofmatch++;
	}
}
//come up wtih the order for the matchign questions
// set some globals
$s = array();
$n = $numberoftype2; // the range you require

function set_num() {
  global $s, $n;
  $add = "yes";
  $ran = rand(0, $n);
  if(count($s) >= 0) {
   foreach($s as $sh) {
     if($ran == $sh) {
       $add = "no";
     }
   }
  }
  if($add == "yes") {
   $s[] = $ran;
  } else {
   set_num();
  }
}
// call the function as needed
while(count($s) <= $n) {
  set_num();
 }
$qseed=$s;

//scoring
$points = 0;
$maxpoints = count($question);

function showAns($uans, $ans, $numans, $answer, $value = 1)
{
	global $points;
	$earned = 0;

	if($ans == $numans)
	{
		$correct_type = "correct";
		if($uans == $numans)
		{
			$points += $value;
			$earned = $value;
		}
	}
	else if($uans == $numans)
	{
		$correct_type = "wrong";
	}
	if(isset($correct_type))
	{
		echo("<span class = \"$correct_type\">$answer</span>");
		unset($correct_type);
	}
	else
	{
		echo $answer;
	}

	return $earned; //number of points earned, remember that this function adds this value to $points for you
}
function showTF($key, $n)
{
	global $ans, $_POST, $id;
	$uans = $_POST["$id[$key]$n"];
	$rans = $ans[$key . $n];

	//echo("<!-- showAns($uans == \$_POST[$id[$key]$n], $rans, T, T, 1/3); -->");
	showAns($uans, $rans, 'T', 'T', 1/3);
	echo " ";
	showAns($uans, $rans, 'F', 'F', 1/3);
}
function fix_img($text)
{
	return str_replace("<img src = \"reviewpix", "<img src = \"../reviewpix", $text);
}


//Calculate the time taken to complete the test
//Start = $time_start
//End = $time_taken
$endunix = strtotime($time_taken);
$totaltime = $endunix - $time_start;
$totaltime = round($totaltime/60,2);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title><?php echo $site_name; ?> Test: <?php
echo $topicdesc." : ".$sectiondesc;
?></title>

<link rel="stylesheet" type="text/css" href="../style.css" />
<style type = "text/css">
span.wrong { color: red; }
span.correct { color: green; }
</style>
</head>
<body>
<?php include("../header.inc.php"); ?>
	<table>
		<tr valign="top">
			<?php include("../nav.inc.php"); ?>
			<td class="maincontent">
				<p>Showing <?php echo($user_name); ?>'s results for the test for the subject:
				<?php
					echo $topicdesc." : ".$sectiondesc;
				?> submited at
				<?php
					echo(date("l dS of F Y", strtotime($time_taken)) .
						' at ' . date("h:i:s A", strtotime($time_taken)).". It took ".$totaltime." minutes for this test to be finished");
				?>.
				</p>
				<?php
					$count=1;
					$timesrun=0;
					$SCORE=0;
					foreach($question as $key => $thisquestion){
						$question[$key] = fix_img($question[$key]);
						$thisquestion = fix_img($thisquestion);
						$answers[$key] = fix_img($answers[$key]);
						
						if($type[$key]=="0"){
							echo "<p>\r\n";
							echo $count.": ".$thisquestion."<br />";
							$theanswers = explode("\n",$answers[$key]);
							foreach($theanswers as $key2 => $answer){
								showAns($_POST[$id[$key]] + 1, $ans[$key], $key2 + 1, $answer);
								echo "<br />";
							}
							echo "</p>\r\n";
						}
						if($type[$key]=="1"){
							echo "\r\n";

							foreach(explode("\n",$thisquestion) as $key2 => $questiontoask){
								if($key2 != "0"){
									echo "<li>";
								}
								$questiontoask = str_replace("&","&amp;",$questiontoask);
								echo "<p>".$questiontoask."</p>\r\n";
								if($key2 == "0"){
									echo "<ol style=\"list-style-type: upper-roman;\">";
								}
								else{
									echo "</li>";
								}
							}
							echo "</ol>";
							foreach(explode("\n",$answers[$key]) as $key3 => $answer){
								$answer = str_replace("&","&amp;",$answer);
								echo "<p>\r\n";
								showAns($_POST[$id[$key]] + 1, $ans[$key], $key3 + 1, $answer);
								echo "<br /></p>\r\n";
							}
							echo "\r\n";
						}
						if($type[$key]=="2"){
							if($timesrun==0)
							{
								echo "<table>";
								echo "<tr><th>Column one</th><th>Column two</th></tr>";
							}
							$timesrun++;
							echo("<tr><td>" . ($key + 1) . ": $question[$key]</td><td>");
							//show answer
							$ua = $_POST[$id[$key]]; //user answer
							$ra = $answers[$key]; //right answer
							if(showAns($ua, $ra, $ua, $ua) == 0)
							{
								echo(" (<span class = \"correct\">$answers[$key]</span>)");
							}
							echo "</td></tr>\r\n";
							if($timesrun==$numberofmatch)
							{
								echo "</table>";
							}
						}
						if($type[$key]=="3"){
							if($SCORE == 0){
								echo "<table><tr><th>I</th><th></th><th>II</th></tr>";
							}
							$questionstoask=explode("\n",$thisquestion);
							echo "<tr><td>$count: $questionstoask[0]</td><td><b>Because</b></td><td>$questionstoask[1]</td></tr>\r\n";
							echo "<tr><td>";
							//echo("<!-- \$ans[$key] == $ans[$key] -->");
							$ans[$key . 'I'] = ($ans[$key] & 0x04) == 0 ? 'F' : 'T';
							$ans[$key . 'II'] = ($ans[$key] & 0x02) == 0 ? 'F' : 'T';
							$ans[$key . 'CE'] = ($ans[$key] & 0x01) == 0 ? false : true;
							showTF($key, 'I');
							echo "</td>\r\n";
							echo "<td></td><td>";
							showTF($key, 'II');
							echo "\r\n";
							showAns($_POST["$id[$key]"], $ans[$key . 'CE'], true, 'CE', 1/3);
							echo " ";
							showAns($_POST["$id[$key]"], $ans[$key . 'CE'], false, 'Not CE', 1/3);
							echo "</td></tr>\r\n";
							$SCORE++;
							if($numberoftype3 == $SCORE){
								echo "</table>";
							}
						}
						$count++;
					}

				?>
				<p>This test was created by: <?php echo $names; ?></p>
				<?php
					$points = $test_result_row['score'] / 3;
					echo("<h2>$user_name got " . round($points,2) .
						" of $maxpoints questions right. $user_name's score was " .
						round(($points/$maxpoints)*100, 2) . "%!</h2>");
				?>
			</td>
		</tr>
	</table>
<?php
?>
<?php include("../footer.inc.php"); ?>
</body>
</html>

Return current item: Standardized Test Practice Engine