<?php

include("configuration.php");

# Check the parameters
if (isset($q)) { 
	if (($q < $min_questions) || ($q > $max_questions)) $q = 10;
}
else $q = 10;

$num_questions = $q;

$db = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name,$db) or die("Could not select db.");

# Number of questions in db
$count_query = "SELECT count(qid) FROM $t_questions";
$count_result = mysql_query($count_query) or die ("Could not get count.");
$count = mysql_result($count_result, 0, "count(qid)");

# Initialize the seed
srand((double)microtime()*1000000);

# Print the head of the page
print("<html>\n\t<head>\n\t\t<title>| YLE | Opinportti :: Visakisa | yle.fi | Arkistoitu| yle.fi | Arkistoitu</title>\n");
print("\t\t<link href=\"css/interface.css\" rel=\"stylesheet\" type=\"text/css\">\n");

include("/www//opinportti/ssi/tappa/head.shtml");

print("\n\t\t<script language=\"JavaScript\" src=\"../js/mm.js\" type=\"text/javascript\"></script>\n");
print("\t\t<script language=\"JavaScript\" src=\"../js/game.js\" type=\"text/javascript\"></script>\n");
print("\t\t<script language=\"JavaScript\" type=\"text/javascript\">\n<!--\n\n");
print("var answer = new Array($num_questions);\nvar points = new Array($num_questions);\nvar afound = new Array($num_questions);\n\n");
print("function start() {\n\n");

# Setup the query
$query = "SELECT q.question, t.tip, a.answer FROM $t_questions AS q, $t_tips AS t, $t_answers AS a WHERE (q.qid = t.qid AND q.qid = a.qid) AND (q.qid = ";

$i = 0;
while ($i < ($num_questions + 1)) {
	$chosen[] = rand(1, $count);
	if ($i == $num_questions) {
		$query .= "'$chosen[$i]') ORDER BY q.qid, a.answer, t.points DESC";
	}
	else {
		$query .= "'$chosen[$i]' OR q.qid = ";
	}
	$i+=1;
}

# Get results from the database
$result = mysql_query($query) or die ("Could not get the results.");

# Read the data
$finished = false;
$row = mysql_fetch_array($result);
$numq = 0;

while (!$finished) {
	$question[$numq] = $row["question"];
	$answer[$numq][] = $row["answer"];
	
	for ($i=0; $i<$num_tips; $i+=1) {
		$tip[$numq][$i] = $row["tip"];
		$row = mysql_fetch_array($result);
	}
	
	if (!$row) $finished = true;
	else {
		$finished2 = false;
		while(!$finished2) {
			$new_question = $row["question"];
			$new_tip = $row["tip"];
			if (($question[$numq] == $new_question) && ($tip[$numq][0] == $new_tip)) {
				$answer[$numq][] = $row["answer"];
				for ($i=0; $i<$num_tips; $i+=1) $row = mysql_fetch_array($result);
				if (!$row) {
					$finished2 = true;
					$finished = true;
				}
			}
			else $finished2 = true;
		}
		$numq+=1;
	}
}

mysql_free_result($result);

# Print the rest of the page
for ($i=0; $i<$num_questions; $i+=1) {
	$num_answers=0;

	while($answer[$i][$num_answers] != "") {
		$num_answers+=1;
	}
	
	printf("\tanswer[%s] = new Array(%s);\n", $i, $num_answers);
	for ($j=0; $j < $num_answers; $j+=1) printf("\tanswer[%s][%s] = \"%s\";\n", $i, $j, $answer[$i][$j]);
}

printf("\n\tfor (i=0; i<%s; i+=1) {\n", $num_questions);

print("\t\teval(\"document.vastaus_\"+i+\".vastaus.value = \\\"\\\"\");\n");
print("\t\teval(\"document.vastaus_\"+i+\".tulos.value = \\\"\\\"\");\n");
print("\t\tdocument.yhteensa.tulos.value = 0;\n");

printf("\t\tpoints[i] = %s;\n", $num_tips);

print("\t\tafound[i] = false;\n");
print("\t}\n}\n\n// -->\n\t\t</script>\n\t</head>\n\t<body onLoad=\"init(); start();\">\n");

include("/www//opinportti/valikot/opinpopup.shtml");
include("/www//opinportti/ssi/tappa/body.shtml");

if ($QUERY_STRING == "") {
	$qst = "$PHP_SELF";
}
else {
	$qst = "$PHP_SELF?$QUERY_STRING";
}

print("\n\t\t<br>\n\t\t<table class=\"teksti\" width=\"95%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n\t\t\t<tr>\n\t\t\t\t<td width=\"33%\" align=\"left\"><a href=\"../\">Visakisan etusivulle</a></td>\n\t\t\t\t<td width=\"33%\" align=\"center\"><a href=\"$qst\">Uudet kysymykset</a></td>\n\t\t\t\t<td width=\"33%\" align=\"right\"><a href=\"/kouluportti/\">Kouluportin etusivulle</a></td>\n\t\t\t</tr>\n\t\t</table>\n");
print("\t\t<table class=\"otsikko\" width=\"95%\" height=\"50\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n\t\t\t<tr>\n\t\t\t\t<td class=\"otsikko_v\" width=\"30\"><img src=\"../images/dot_trans.gif\" width=\"30\" height=\"30\" border=\"0\" alt=\"\"></td>\n\t\t\t\t<td class=\"txtotsikko\">\n\t\t\t\t\t<table width=\"100%\" border=\"0\" cellspacing=\"10\" cellpadding=\"0\">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td width=\"20%\"><strong>Kysymys</strong></td>\n");

for ($i=$num_tips; $i>0; $i-=1) printf("\t\t\t\t\t\t\t<td width=\"20%%\"><strong>%s pistett&auml;</strong></td>\n", $i);

print("\t\t\t\t\t\t\t<td width=\"20%\"><strong>Vastaus</strong></td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td class=\"otsikko_o\" width=\"30\"><img src=\"../images/dot_trans.gif\" width=\"30\" height=\"30\" border=\"0\" alt=\"\"></td>\n\t\t\t</tr>\n\t\t</table>\n");

for ($i=0; $i<$num_questions; $i+=1) {
	$color = $i % 2;
	
	printf("\t\t<table class=\"vari_%s\" width=\"95%%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n\t\t\t<tr>\n\t\t\t\t<td width=\"30\" class=\"vari_%s_vy\"><img src=\"../images/dot_trans.gif\" width=\"30\" height=\"30\" border=\"0\" alt=\"\"></td>\n\t\t\t\t<td><img src=\"../images/dot_trans.gif\" width=\"1\" height=\"1\" border=\"0\" alt=\"\"></td>\n\t\t\t\t<td width=\"30\" class=\"vari_%s_oy\"><img src=\"../images/dot_trans.gif\" width=\"30\" height=\"30\" border=\"0\" alt=\"\"></td>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td width=\"30\"><img src=\"../images/dot_trans.gif\" width=\"30\" height=\"30\" border=\"0\" alt=\"\"></td>\n", $color, $color, $color);
	printf("\t\t\t\t<td>\n\t\t\t\t\t<form name=\"vastaus_%s\">\n\t\t\t\t\t\t<table width=\"100%%\" border=\"0\" cellspacing=\"10\" cellpadding=\"0\">\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<td width=\"20%%\"><strong>%s<br>&nbsp;</strong></td>\n", $i, $question[$i]);
	
	for ($j=0; $j < $num_tips; $j+=1) printf("\t\t\t\t\t\t\t\t<td width=\"20%%\">\n\t\t\t\t\t\t\t\t\t<div id=\"vihje_%s_%s\" style=\"visibility: %s;\">\n\t\t\t\t\t\t\t\t\t\t%s\n\t\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t\t</td>\n", $i, ($num_tips - $j), (($j == 0) ? visible : hidden), $tip[$i][$j]);
	
	printf("\t\t\t\t\t\t\t\t<td width=\"20%%\">\n\t\t\t\t\t\t\t\t\t<div id=\"vihje_%s_0\" style=\"visibility: hidden;\">\n\t\t\t\t\t\t\t\t\t\t%s\n\t\t\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t\t</td>\n", $i, $answer[$i][0]);
	printf("\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t\t<td colspan=\"4\">\n\t\t\t\t\t\t\t\t\t<input name=\"vastaus\" type=\"text\" autocomplete=\"off\">\n\t\t\t\t\t\t\t\t\t<input value=\"Vastaa\" type=\"button\" onClick=\"submit_answer(%s);\">\n\t\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t\t\t<td align=\"right\"><input name=\"tulos\" type=\"text\" size=\"4\" readonly></td>\n\t\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t</table>\n\t\t\t\t\t</form>\n\t\t\t\t</td>\n\t\t\t\t<td width=\"30\"><img src=\"../images/dot_trans.gif\" width=\"30\" height=\"30\" border=\"0\" alt=\"\"></td>\n\t\t\t</tr>\n", $i);
	printf("\t\t\t<tr>\n\t\t\t\t<td width=\"30\" class=\"vari_%s_va\"><img src=\"../images/dot_trans.gif\" width=\"30\" height=\"30\" border=\"0\" alt=\"\"></td>\n\t\t\t\t<td><img src=\"../images/dot_trans.gif\" width=\"1\" height=\"1\" border=\"0\" alt=\"\"></td>\n\t\t\t\t<td width=\"30\" class=\"vari_%s_oa\"><img src=\"../images/dot_trans.gif\" width=\"30\" height=\"30\" border=\"0\" alt=\"\"></td>\n\t\t\t</tr>\n\t\t</table>\n", $color, $color);

}

$max_points = $num_questions * $num_tips;

printf("\t\t<table class=\"otsikko\" width=\"95%%\" height=\"50\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n\t\t\t<tr>\n\t\t\t\t<td class=\"otsikko_v\" width=\"30\"><img src=\"../images/dot_trans.gif\" width=\"30\" height=\"30\" border=\"0\" alt=\"\"></td>\n\t\t\t\t<td class=\"otsikko\">\n\t\t\t\t\t<table width=\"100%%\" border=\"0\" cellspacing=\"10\" cellpadding=\"0\">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td width=\"80%%\"><strong>Yhteens&auml;</strong></td>\n\t\t\t\t\t\t\t<td width=\"20%%\" align=\"right\">\n\t\t\t\t\t\t\t\t<form name=\"yhteensa\">\n\t\t\t\t\t\t\t\t\t<input name=\"tulos\" type=\"text\" size=\"4\" readonly><strong> / %s</strong>\n\t\t\t\t\t\t\t\t</form>\n\t\t\t\t\t\t\t</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t</table>\n\t\t\t\t</td>\n\t\t\t\t<td class=\"otsikko_o\" width=\"30\"><img src=\"../images/dot_trans.gif\" width=\"30\" height=\"30\" border=\"0\" alt=\"\"></td>\n\t\t\t</tr>\n\t\t</table>\n", $max_points);
print("\t\t<table class=\"teksti\" width=\"95%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n\t\t\t<tr>\n\t\t\t\t<td width=\"33%\" align=\"left\"><a href=\"../\">Visakisan etusivulle</a></td>\n\t\t\t\t<td width=\"33%\" align=\"center\"><a href=\"$qst\">Uudet kysymykset</a></td>\n\t\t\t\t<td width=\"33%\" align=\"right\">&copy; YLE Opetusohjelmat 2002</td>\n\t\t\t</tr>\n\t\t</table>\n");

include("ssi/webmeasure.html");

print("\n\t<script type="text/javascript" src="../tmv11.js"></script></body>\n</html>\n");

print("<script language=\"javascript\" src=\"//opinportti/js/init3.js\"></script>\n");

?>